Update makefile
[urcu.git] / test_urcu_gc.c
1 /*
2 * test_urcu_gc.c
3 *
4 * Userspace RCU library - test program (with baatch reclamation)
5 *
6 * Copyright February 2009 - Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23 #define _GNU_SOURCE
24 #include <stdio.h>
25 #include <pthread.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/types.h>
29 #include <sys/wait.h>
30 #include <unistd.h>
31 #include <stdio.h>
32 #include <assert.h>
33 #include <sys/syscall.h>
34 #include <sched.h>
35
36 #include "arch.h"
37
38 /* Make this big enough to include the POWER5+ L3 cacheline size of 256B */
39 #define CACHE_LINE_SIZE 4096
40
41 #if defined(_syscall0)
42 _syscall0(pid_t, gettid)
43 #elif defined(__NR_gettid)
44 static inline pid_t gettid(void)
45 {
46 return syscall(__NR_gettid);
47 }
48 #else
49 #warning "use pid as tid"
50 static inline pid_t gettid(void)
51 {
52 return getpid();
53 }
54 #endif
55
56 #ifndef DYNAMIC_LINK_TEST
57 #define _LGPL_SOURCE
58 #else
59 #define debug_yield_read()
60 #endif
61 #include "urcu.h"
62
63 struct test_array {
64 int a;
65 };
66
67 static volatile int test_go, test_stop;
68
69 static unsigned long wdelay;
70
71 static struct test_array *test_rcu_pointer;
72
73 static int reclaim_batch = 1;
74
75 struct reclaim_queue {
76 void **queue; /* Beginning of queue */
77 void **head; /* Insert position */
78 };
79
80 static struct reclaim_queue *pending_reclaims;
81
82 static unsigned long duration;
83
84 /* read-side C.S. duration, in loops */
85 static unsigned long rduration;
86
87 static inline void loop_sleep(unsigned long l)
88 {
89 while(l-- != 0)
90 cpu_relax();
91 }
92
93 static int verbose_mode;
94
95 #define printf_verbose(fmt, args...) \
96 do { \
97 if (verbose_mode) \
98 printf(fmt, args); \
99 } while (0)
100
101 /*
102 * returns 0 if test should end.
103 */
104 static int test_duration_write(void)
105 {
106 return !test_stop;
107 }
108
109 static int test_duration_read(void)
110 {
111 return !test_stop;
112 }
113
114 static unsigned long long __thread nr_writes;
115 static unsigned long long __thread nr_reads;
116
117 static
118 unsigned long long __attribute__((aligned(CACHE_LINE_SIZE))) *tot_nr_writes;
119
120 static unsigned int nr_readers;
121 static unsigned int nr_writers;
122
123 pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
124
125 void rcu_copy_mutex_lock(void)
126 {
127 int ret;
128 ret = pthread_mutex_lock(&rcu_copy_mutex);
129 if (ret) {
130 perror("Error in pthread mutex lock");
131 exit(-1);
132 }
133 }
134
135 void rcu_copy_mutex_unlock(void)
136 {
137 int ret;
138
139 ret = pthread_mutex_unlock(&rcu_copy_mutex);
140 if (ret) {
141 perror("Error in pthread mutex unlock");
142 exit(-1);
143 }
144 }
145
146 void *thr_reader(void *_count)
147 {
148 unsigned long long *count = _count;
149 struct test_array *local_ptr;
150
151 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
152 "reader", pthread_self(), (unsigned long)gettid());
153
154 rcu_register_thread();
155
156 while (!test_go)
157 {
158 }
159 smp_mb();
160
161 for (;;) {
162 rcu_read_lock();
163 local_ptr = rcu_dereference(test_rcu_pointer);
164 debug_yield_read();
165 if (local_ptr)
166 assert(local_ptr->a == 8);
167 if (unlikely(rduration))
168 loop_sleep(rduration);
169 rcu_read_unlock();
170 nr_reads++;
171 if (unlikely(!test_duration_read()))
172 break;
173 }
174
175 rcu_unregister_thread();
176
177 *count = nr_reads;
178 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
179 "reader", pthread_self(), (unsigned long)gettid());
180 return ((void*)1);
181
182 }
183
184 /* Using per-thread queue */
185 static void rcu_gc_reclaim(unsigned long wtidx, void *old)
186 {
187 void **p;
188
189 /* Queue pointer */
190 *pending_reclaims[wtidx].head = old;
191 pending_reclaims[wtidx].head++;
192
193 if (likely(pending_reclaims[wtidx].head - pending_reclaims[wtidx].queue
194 < reclaim_batch))
195 return;
196
197 /* If queue is full, wait for Q.S and empty queue */
198 synchronize_rcu();
199
200 for (p = pending_reclaims[wtidx].queue;
201 p < pending_reclaims[wtidx].head; p++) {
202 /* poison */
203 if (*p)
204 ((struct test_array *)*p)->a = 0;
205 free(*p);
206 }
207 pending_reclaims[wtidx].head = pending_reclaims[wtidx].queue;
208 }
209
210 static void rcu_gc_cleanup(unsigned long wtidx)
211 {
212 void **p;
213
214 /* Wait for Q.S and empty queue */
215 synchronize_rcu();
216
217 for (p = pending_reclaims[wtidx].queue;
218 p < pending_reclaims[wtidx].head; p++) {
219 /* poison */
220 if (*p)
221 ((struct test_array *)*p)->a = 0;
222 free(*p);
223 }
224 pending_reclaims[wtidx].head = pending_reclaims[wtidx].queue;
225 }
226
227 void *thr_writer(void *data)
228 {
229 unsigned long wtidx = (unsigned long)data;
230 struct test_array *new, *old;
231
232 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
233 "writer", pthread_self(), (unsigned long)gettid());
234
235 while (!test_go)
236 {
237 }
238 smp_mb();
239
240 for (;;) {
241 new = malloc(sizeof(*new));
242 rcu_copy_mutex_lock();
243 old = test_rcu_pointer;
244 if (old)
245 assert(old->a == 8);
246 new->a = 8;
247 old = rcu_xchg_pointer(&test_rcu_pointer, new);
248 rcu_copy_mutex_unlock();
249 rcu_gc_reclaim(wtidx, old);
250 nr_writes++;
251 if (unlikely(!test_duration_write()))
252 break;
253 if (unlikely(wdelay))
254 loop_sleep(wdelay);
255 }
256
257 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
258 "writer", pthread_self(), (unsigned long)gettid());
259 tot_nr_writes[wtidx] = nr_writes;
260 return ((void*)2);
261 }
262
263 void show_usage(int argc, char **argv)
264 {
265 printf("Usage : %s nr_readers nr_writers duration (s)", argv[0]);
266 #ifdef DEBUG_YIELD
267 printf(" [-r] [-w] (yield reader and/or writer)");
268 #endif
269 printf(" [-d delay] (writer period (us))");
270 printf(" [-c duration] (reader C.S. duration (in loops))");
271 printf(" [-v] (verbose output)");
272 printf(" [-a cpu#] [-a cpu#]... (affinity)");
273 printf("\n");
274 }
275
276 cpu_set_t affinity;
277
278 int main(int argc, char **argv)
279 {
280 int err;
281 pthread_t *tid_reader, *tid_writer;
282 void *tret;
283 unsigned long long *count_reader;
284 unsigned long long tot_reads = 0, tot_writes = 0;
285 int i, a;
286 int use_affinity = 0;
287
288 if (argc < 4) {
289 show_usage(argc, argv);
290 return -1;
291 }
292
293 err = sscanf(argv[1], "%u", &nr_readers);
294 if (err != 1) {
295 show_usage(argc, argv);
296 return -1;
297 }
298
299 err = sscanf(argv[2], "%u", &nr_writers);
300 if (err != 1) {
301 show_usage(argc, argv);
302 return -1;
303 }
304
305 err = sscanf(argv[3], "%lu", &duration);
306 if (err != 1) {
307 show_usage(argc, argv);
308 return -1;
309 }
310
311 CPU_ZERO(&affinity);
312
313 for (i = 4; i < argc; i++) {
314 if (argv[i][0] != '-')
315 continue;
316 switch (argv[i][1]) {
317 #ifdef DEBUG_YIELD
318 case 'r':
319 yield_active |= YIELD_READ;
320 break;
321 case 'w':
322 yield_active |= YIELD_WRITE;
323 break;
324 #endif
325 case 'a':
326 if (argc < i + 2) {
327 show_usage(argc, argv);
328 return -1;
329 }
330 a = atoi(argv[++i]);
331 CPU_SET(a, &affinity);
332 use_affinity = 1;
333 printf_verbose("Adding CPU %d affinity\n", a);
334 break;
335 case 'b':
336 if (argc < i + 2) {
337 show_usage(argc, argv);
338 return -1;
339 }
340 reclaim_batch = atol(argv[++i]);
341 break;
342 case 'c':
343 if (argc < i + 2) {
344 show_usage(argc, argv);
345 return -1;
346 }
347 rduration = atol(argv[++i]);
348 break;
349 case 'd':
350 if (argc < i + 2) {
351 show_usage(argc, argv);
352 return -1;
353 }
354 wdelay = atol(argv[++i]);
355 break;
356 case 'v':
357 verbose_mode = 1;
358 break;
359 }
360 }
361
362 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
363 duration, nr_readers, nr_writers);
364 printf_verbose("Writer delay : %lu loops.\n", wdelay);
365 printf_verbose("Reader duration : %lu loops.\n", rduration);
366 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
367 "main", pthread_self(), (unsigned long)gettid());
368
369 if (use_affinity
370 && sched_setaffinity(0, sizeof(affinity), &affinity) < 0) {
371 perror("sched_setaffinity");
372 exit(-1);
373 }
374
375 tid_reader = malloc(sizeof(*tid_reader) * nr_readers);
376 tid_writer = malloc(sizeof(*tid_writer) * nr_writers);
377 count_reader = malloc(sizeof(*count_reader) * nr_readers);
378 tot_nr_writes = malloc(sizeof(*tot_nr_writes) * nr_writers);
379 pending_reclaims = malloc(sizeof(*pending_reclaims) * nr_writers);
380 if (reclaim_batch * sizeof(*pending_reclaims[i].queue)
381 < CACHE_LINE_SIZE)
382 for (i = 0; i < nr_writers; i++)
383 pending_reclaims[i].queue = calloc(1, CACHE_LINE_SIZE);
384 else
385 for (i = 0; i < nr_writers; i++)
386 pending_reclaims[i].queue = calloc(reclaim_batch,
387 sizeof(*pending_reclaims[i].queue));
388 for (i = 0; i < nr_writers; i++)
389 pending_reclaims[i].head = pending_reclaims[i].queue;
390
391 for (i = 0; i < nr_readers; i++) {
392 err = pthread_create(&tid_reader[i], NULL, thr_reader,
393 &count_reader[i]);
394 if (err != 0)
395 exit(1);
396 }
397 for (i = 0; i < nr_writers; i++) {
398 err = pthread_create(&tid_writer[i], NULL, thr_writer,
399 (void *)(long)i);
400 if (err != 0)
401 exit(1);
402 }
403
404 smp_mb();
405
406 test_go = 1;
407
408 sleep(duration);
409
410 test_stop = 1;
411
412 for (i = 0; i < nr_readers; i++) {
413 err = pthread_join(tid_reader[i], &tret);
414 if (err != 0)
415 exit(1);
416 tot_reads += count_reader[i];
417 }
418 for (i = 0; i < nr_writers; i++) {
419 err = pthread_join(tid_writer[i], &tret);
420 if (err != 0)
421 exit(1);
422 tot_writes += tot_nr_writes[i];
423 rcu_gc_cleanup(i);
424 }
425
426 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
427 tot_writes);
428 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu "
429 "nr_writers %3u "
430 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
431 argv[0], duration, nr_readers, rduration,
432 nr_writers, wdelay, tot_reads, tot_writes,
433 tot_reads + tot_writes);
434 free(tid_reader);
435 free(tid_writer);
436 free(count_reader);
437 free(tot_nr_writes);
438 for (i = 0; i < nr_writers; i++)
439 free(pending_reclaims[i].queue);
440 free(pending_reclaims);
441
442 return 0;
443 }
This page took 0.037304 seconds and 4 git commands to generate.