]> pd.if.org Git - nbds/blob - test/rcu_test.c
work in progress
[nbds] / test / rcu_test.c
1 #include <stdio.h>
2 #include <errno.h>
3 #include <pthread.h>
4 #include <unistd.h>
5 #include <sys/time.h>
6 #include "common.h"
7 #include "runtime.h"
8 #include "mem.h"
9 #include "rcu.h"
10
11 #define NUM_ITERATIONS 10000000
12
13 typedef struct node {
14     struct node *next;
15 } node_t;
16
17 typedef struct lifo {
18     node_t *head;
19 } lifo_t;
20
21 static volatile int wait_;
22 static lifo_t *stk_;
23
24 static lifo_t *lifo_alloc (void) {
25     lifo_t *stk = (lifo_t *)nbd_malloc(sizeof(lifo_t)); 
26     memset(stk, 0, sizeof(lifo_t));
27     return stk;
28 }
29
30 static void lifo_aba_push (lifo_t *stk, node_t *x) {
31     node_t *head;
32     do {
33         head = VOLATILE_DEREF(stk).head;
34         VOLATILE_DEREF(x).next = head;
35     } while (SYNC_CAS(&stk->head, head, x) != head);
36 }
37
38 node_t *lifo_aba_pop (lifo_t *stk) {
39     node_t *head;
40     do {
41         head = VOLATILE_DEREF(stk).head;
42         if (head == NULL)
43             return NULL;
44     } while (SYNC_CAS(&stk->head, head, head->next) != head);
45     head->next = NULL;
46     return head;
47 }
48
49 node_t *node_alloc (void) {
50     node_t *node = (node_t *)nbd_malloc(sizeof(node_t));
51     memset(node, 0, sizeof(node_t));
52     return node;
53 }
54
55 void *worker (void *arg) {
56     nbd_thread_init();
57
58     // Wait for all the worker threads to be ready.
59     (void)__sync_fetch_and_add(&wait_, -1);
60     do {} while (wait_); 
61
62     int i;
63     for (i = 0; i < NUM_ITERATIONS; ++ i) {
64         int n = nbd_rand();
65         if (n & 0x1) {
66             lifo_aba_push(stk_, node_alloc());
67         } else {
68             node_t *x = lifo_aba_pop(stk_);
69             if (x) {
70                 rcu_defer_free(x);
71             }
72         }
73         rcu_update();
74     }
75
76     return NULL;
77 }
78
79 int main (int argc, char **argv) {
80     nbd_thread_init();
81     lwt_set_trace_level("m3r3");
82
83     int num_threads = sysconf(_SC_NPROCESSORS_CONF);
84     if (argc == 2)
85     {
86         errno = 0;
87         num_threads = strtol(argv[1], NULL, 10);
88         if (errno) {
89             fprintf(stderr, "%s: Invalid argument for number of threads\n", argv[0]);
90             return -1;
91         }
92         if (num_threads <= 0) {
93             fprintf(stderr, "%s: Number of threads must be at least 1\n", argv[0]);
94             return -1;
95         }
96     }
97
98     stk_ = lifo_alloc();
99     wait_ = num_threads;
100
101     struct timeval tv1, tv2;
102     gettimeofday(&tv1, NULL);
103     wait_ = num_threads;
104
105     pthread_t thread[num_threads];
106     for (int i = 0; i < num_threads; ++i) {
107         int rc = pthread_create(thread + i, NULL, worker, (void *)(size_t)i);
108         if (rc != 0) { perror("pthread_create"); return rc; }
109     }
110     for (int i = 0; i < num_threads; ++i) {
111         pthread_join(thread[i], NULL);
112     }
113
114     gettimeofday(&tv2, NULL);
115     int ms = (int)(1000000*(tv2.tv_sec - tv1.tv_sec) + tv2.tv_usec - tv1.tv_usec) / 1000;
116     printf("Th:%d Time:%dms\n\n", num_threads, ms);
117     fflush(stdout);
118
119     return 0;
120 }