]> pd.if.org Git - nbds/blob - test/ll_test.c
generalize list into an updatable list-based map
[nbds] / test / ll_test.c
1 #include <stdio.h>
2 #include <errno.h>
3 #include <pthread.h>
4 #include <sys/time.h>
5
6 #include "common.h"
7 #include "runtime.h"
8 #include "struct.h"
9
10 #define NUM_ITERATIONS 10000000
11
12 static volatile int wait_;
13 static long num_threads_;
14 static list_t *ll_;
15
16 void *worker (void *arg) {
17
18     // Wait for all the worker threads to be ready.
19     SYNC_ADD(&wait_, -1);
20     do {} while (wait_); 
21
22     for (int i = 0; i < NUM_ITERATIONS/num_threads_; ++i) {
23         unsigned r = nbd_rand();
24         uint64_t key = r & 0xF;
25 #if 1
26         char key_str[10];
27         sprintf(key_str, "%llX", key);
28         if (r & (1 << 8)) {
29             ll_cas(ll_, key_str, strlen(key_str) + 1, EXPECT_WHATEVER, 1);
30         } else {
31             ll_remove(ll_, key_str, strlen(key_str) + 1);
32         }
33 #else
34         if (r & (1 << 8)) {
35             ll_cas(ll_, (void *)key, -1, EXPECT_WHATEVER, 1);
36         } else {
37             ll_remove(ll_, (void *)key, -1);
38         }
39 #endif
40
41         rcu_update();
42     }
43
44     return NULL;
45 }
46
47 int main (int argc, char **argv) {
48     nbd_init();
49     //lwt_set_trace_level("l3");
50
51     char* program_name = argv[0];
52     pthread_t thread[MAX_NUM_THREADS];
53
54     if (argc > 2) {
55         fprintf(stderr, "Usage: %s num_threads\n", program_name);
56         return -1;
57     }
58
59     num_threads_ = 2;
60     if (argc == 2)
61     {
62         errno = 0;
63         num_threads_ = strtol(argv[1], NULL, 10);
64         if (errno) {
65             fprintf(stderr, "%s: Invalid argument for number of threads\n", program_name);
66             return -1;
67         }
68         if (num_threads_ <= 0) {
69             fprintf(stderr, "%s: Number of threads must be at least 1\n", program_name);
70             return -1;
71         }
72         if (num_threads_ > MAX_NUM_THREADS) {
73             fprintf(stderr, "%s: Number of threads cannot be more than %d\n", program_name, MAX_NUM_THREADS);
74             return -1;
75         }
76     }
77
78     ll_ = ll_alloc();
79
80     struct timeval tv1, tv2;
81     gettimeofday(&tv1, NULL);
82
83     wait_ = num_threads_;
84
85     for (int i = 0; i < num_threads_; ++i) {
86         int rc = nbd_thread_create(thread + i, i, worker, (void*)(size_t)i);
87         if (rc != 0) { perror("pthread_create"); return rc; }
88     }
89
90     for (int i = 0; i < num_threads_; ++i) {
91         pthread_join(thread[i], NULL);
92     }
93
94     gettimeofday(&tv2, NULL);
95     int ms = (int)(1000000*(tv2.tv_sec - tv1.tv_sec) + tv2.tv_usec - tv1.tv_usec) / 1000;
96     ll_print(ll_);
97     printf("Th:%ld Time:%dms\n", num_threads_, ms);
98
99     return 0;
100 }