]> pd.if.org Git - nbds/blob - test/map_test1.c
use a vtable approach for generic map interface
[nbds] / test / map_test1.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 "map.h"
9
10 #define NUM_ITERATIONS 10000000
11
12 static volatile int wait_;
13 static long num_threads_;
14 static map_t *map_;
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             map_set(map_, key_str, strlen(key_str) + 1, 1);
30         } else {
31             map_remove(map_, key_str, strlen(key_str) + 1);
32         }
33 #else
34         if (r & (1 << 8)) {
35             map_set(map_, (void *)key, -1, 1);
36         } else {
37             map_remove(map_, (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     map_type_t map_types[] = { MAP_TYPE_LIST, MAP_TYPE_SKIPLIST, MAP_TYPE_HASHTABLE };
79     for (int i = 0; i < sizeof(map_types)/sizeof(*map_types); ++i) {
80         map_ = map_alloc(map_types[i]);
81
82         struct timeval tv1, tv2;
83         gettimeofday(&tv1, NULL);
84
85         wait_ = num_threads_;
86
87         for (int i = 0; i < num_threads_; ++i) {
88             int rc = nbd_thread_create(thread + i, i, worker, (void*)(size_t)i);
89             if (rc != 0) { perror("pthread_create"); return rc; }
90         }
91
92         for (int i = 0; i < num_threads_; ++i) {
93             pthread_join(thread[i], NULL);
94         }
95
96         gettimeofday(&tv2, NULL);
97         int ms = (int)(1000000*(tv2.tv_sec - tv1.tv_sec) + tv2.tv_usec - tv1.tv_usec) / 1000;
98         map_print(map_);
99         printf("Th:%ld Time:%dms\n", num_threads_, ms);
100         fflush(stdout);
101     }
102
103     return 0;
104 }