]> pd.if.org Git - nbds/blob - runtime/rcu.c
rename util/ to runtime/
[nbds] / runtime / rcu.c
1 /* 
2  * Written by Josh Dybnis and released to the public domain, as explained at
3  * http://creativecommons.org/licenses/publicdomain
4  *
5  * safe memory reclemation using a simple technique from rcu
6  */
7 #include <string.h>
8 #include "common.h"
9 #include "rcu.h"
10 #include "lwt.h"
11 #include "mem.h"
12 #include "nbd.h"
13 #include "tls.h"
14
15 #define RCU_POST_THRESHOLD 10
16 #define RCU_QUEUE_SCALE 20
17
18 typedef struct fifo {
19     uint32_t head;
20     uint32_t tail;
21     uint32_t scale;
22     void *x[0];
23 } fifo_t;
24
25 static uint64_t rcu_[MAX_NUM_THREADS][MAX_NUM_THREADS] = {};
26 static uint64_t rcu_last_posted_[MAX_NUM_THREADS][MAX_NUM_THREADS] = {};
27 static fifo_t *pending_[MAX_NUM_THREADS] = {};
28 static int num_threads_ = 0;
29
30 static fifo_t *fifo_alloc(int scale) {
31     fifo_t *q = (fifo_t *)nbd_malloc(sizeof(fifo_t) + (1 << scale) * sizeof(void *)); 
32     memset(q, 0, sizeof(fifo_t));
33     q->scale = scale;
34     q->head = 0;
35     q->tail = 0;
36     return q;
37 }
38
39 static uint32_t fifo_index (fifo_t *q, uint32_t i) {
40     return i & MASK(q->scale);
41 }
42
43 static void fifo_enqueue (fifo_t *q, void *x) {
44     assert(fifo_index(q, q->head + 1) != fifo_index(q, q->tail));
45     uint32_t i = fifo_index(q, q->head++);
46     q->x[i] = x;
47 }
48
49 static void *fifo_dequeue (fifo_t *q) {
50     uint32_t i = fifo_index(q, q->tail++);
51     return q->x[i];
52 }
53
54 void rcu_thread_init (int id) {
55     assert(id < MAX_NUM_THREADS);
56     if (pending_[id] == NULL) {
57         pending_[id] = fifo_alloc(RCU_QUEUE_SCALE);
58         SYNC_ADD(&num_threads_, 1);
59     }
60 }
61
62 static void rcu_post (uint64_t x) {
63     LOCALIZE_THREAD_LOCAL(tid_, int);
64     if (x - rcu_last_posted_[tid_][tid_] < RCU_POST_THRESHOLD)
65         return;
66
67     int next_thread_id = (tid_ + 1) % num_threads_;
68
69     TRACE("r0", "rcu_post: %llu", x, 0);
70     rcu_[next_thread_id][tid_] = rcu_last_posted_[tid_][tid_] = x;
71 }
72
73 void rcu_update (void) {
74     LOCALIZE_THREAD_LOCAL(tid_, int);
75     assert(tid_ < num_threads_);
76     int next_thread_id = (tid_ + 1) % num_threads_;
77     int i;
78     for (i = 0; i < num_threads_; ++i) {
79         if (i == tid_)
80             continue;
81
82         // No need to post an update if the value hasn't changed
83         if (rcu_[tid_][i] == rcu_last_posted_[tid_][i])
84             continue;
85
86         uint64_t x = rcu_[tid_][i];
87         rcu_[next_thread_id][i] = rcu_last_posted_[tid_][i] = x;
88     }
89
90     // free
91     while (pending_[tid_]->tail != rcu_[tid_][tid_]) {
92         nbd_free(fifo_dequeue(pending_[tid_]));
93     }
94 }
95
96 void nbd_defer_free (void *x) {
97     LOCALIZE_THREAD_LOCAL(tid_, int);
98     fifo_enqueue(pending_[tid_], x);
99     TRACE("r0", "nbd_defer_free: put %p on queue at position %llu", x, pending_[tid_]->head);
100     rcu_post(pending_[tid_]->head);
101 }
102
103 #ifdef MAKE_rcu_test
104 #include <pthread.h>
105 #include <errno.h>
106 #include <stdio.h>
107
108 #define NUM_ITERATIONS 10000000
109
110 typedef struct node {
111     struct node *next;
112 } node_t;
113
114 typedef struct lifo {
115     node_t *head;
116 } lifo_t;
117
118 static volatile int wait_;
119 static lifo_t *stk_;
120
121 static lifo_t *lifo_alloc (void) {
122     lifo_t *stk = (lifo_t *)nbd_malloc(sizeof(lifo_t)); 
123     memset(stk, 0, sizeof(lifo_t));
124     return stk;
125 }
126
127 static void lifo_aba_push (lifo_t *stk, node_t *x) {
128     node_t *head;
129     do {
130         head = ((volatile lifo_t *)stk)->head;
131         ((volatile node_t *)x)->next = head;
132     } while (__sync_val_compare_and_swap(&stk->head, head, x) != head);
133 }
134
135 node_t *lifo_aba_pop (lifo_t *stk) {
136     node_t *head;
137     do {
138         head = ((volatile lifo_t *)stk)->head;
139         if (head == NULL)
140             return NULL;
141     } while (__sync_val_compare_and_swap(&stk->head, head, head->next) != head);
142     head->next = NULL;
143     return head;
144 }
145
146 node_t *node_alloc (void) {
147     node_t *node = (node_t *)nbd_malloc(sizeof(node_t));
148     memset(node, 0, sizeof(node_t));
149     return node;
150 }
151
152 void *worker (void *arg) {
153     int id = (int)(size_t)arg;
154     unsigned int rand_seed = (unsigned int)id + 1;
155     nbd_thread_init(id);
156
157     // Wait for all the worker threads to be ready.
158     __sync_fetch_and_add(&wait_, -1);
159     do {} while (wait_); 
160
161     int i;
162     for (i = 0; i < NUM_ITERATIONS; ++ i) {
163         int n = rand_r(&rand_seed);
164         if (n & 0x1) {
165             lifo_aba_push(stk_, node_alloc());
166         } else {
167             node_t *x = lifo_aba_pop(stk_);
168             if (x) {
169                 nbd_defer_free(x);
170             }
171         }
172         rcu_update();
173     }
174
175     return NULL;
176 }
177
178 int main (int argc, char **argv) {
179     nbd_init();
180     //lwt_set_trace_level("m0r0");
181
182     int num_threads = 2;
183     if (argc == 2)
184     {
185         errno = 0;
186         num_threads = strtol(argv[1], NULL, 10);
187         if (errno) {
188             fprintf(stderr, "%s: Invalid argument for number of threads\n", argv[0]);
189             return -1;
190         }
191         if (num_threads <= 0) {
192             fprintf(stderr, "%s: Number of threads must be at least 1\n", argv[0]);
193             return -1;
194         }
195     }
196
197     stk_ = lifo_alloc();
198     wait_ = num_threads;
199
200     pthread_t thread[num_threads];
201     for (int i = 0; i < num_threads; ++i) {
202         int rc = pthread_create(thread + i, NULL, worker, (void *)(size_t)i);
203         if (rc != 0) { perror("pthread_create"); return rc; }
204     }
205     for (int i = 0; i < num_threads; ++i) {
206         pthread_join(thread[i], NULL);
207     }
208
209     return 0;
210 }
211 #endif//rcu_test