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