]> pd.if.org Git - nbds/blob - test/ll_test.c
54ad96b9613015fa93f054433d86e175709841e6
[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         int key = r & 0xF;
25         char key_str[10];
26         sprintf(key_str, "%X", key);
27         if (r & (1 << 8)) {
28             ll_add(ll_, key_str, strlen(key_str) + 1, 1);
29         } else {
30             ll_remove(ll_, key_str, strlen(key_str) + 1);
31         }
32
33         rcu_update();
34     }
35
36     return NULL;
37 }
38
39 int main (int argc, char **argv) {
40     nbd_init();
41     //lwt_set_trace_level("m0l0");
42
43     char* program_name = argv[0];
44     pthread_t thread[MAX_NUM_THREADS];
45
46     if (argc > 2) {
47         fprintf(stderr, "Usage: %s num_threads\n", program_name);
48         return -1;
49     }
50
51     num_threads_ = 2;
52     if (argc == 2)
53     {
54         errno = 0;
55         num_threads_ = strtol(argv[1], NULL, 10);
56         if (errno) {
57             fprintf(stderr, "%s: Invalid argument for number of threads\n", program_name);
58             return -1;
59         }
60         if (num_threads_ <= 0) {
61             fprintf(stderr, "%s: Number of threads must be at least 1\n", program_name);
62             return -1;
63         }
64         if (num_threads_ > MAX_NUM_THREADS) {
65             fprintf(stderr, "%s: Number of threads cannot be more than %d\n", program_name, MAX_NUM_THREADS);
66             return -1;
67         }
68     }
69
70     ll_ = ll_alloc();
71
72     struct timeval tv1, tv2;
73     gettimeofday(&tv1, NULL);
74
75     wait_ = num_threads_;
76
77     for (int i = 0; i < num_threads_; ++i) {
78         int rc = nbd_thread_create(thread + i, i, worker, (void*)(size_t)i);
79         if (rc != 0) { perror("pthread_create"); return rc; }
80     }
81
82     for (int i = 0; i < num_threads_; ++i) {
83         pthread_join(thread[i], NULL);
84     }
85
86     gettimeofday(&tv2, NULL);
87     int ms = (int)(1000000*(tv2.tv_sec - tv1.tv_sec) + tv2.tv_usec - tv1.tv_usec) / 1000;
88     ll_print(ll_);
89     printf("Th:%ld Time:%dms\n", num_threads_, ms);
90
91     return 0;
92 }