Add compiler barriers to read-side for mutex ordering
[userspace-rcu.git] / tests / test_urcu_gc.c
CommitLineData
a813abf8
MD
1/*
2 * test_urcu_gc.c
3 *
4 * Userspace RCU library - test program (with baatch reclamation)
5 *
6982d6d7 6 * Copyright February 2009 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
a813abf8
MD
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
d8540fc5 24#include "../config.h"
a813abf8
MD
25#include <stdio.h>
26#include <pthread.h>
27#include <stdlib.h>
28#include <string.h>
29#include <sys/types.h>
30#include <sys/wait.h>
31#include <unistd.h>
32#include <stdio.h>
33#include <assert.h>
34#include <sys/syscall.h>
35#include <sched.h>
94b343fd 36#include <errno.h>
a813abf8 37
ec4e58a3 38#include <urcu/arch.h>
a813abf8 39
2a7ac59d
MD
40/* hardcoded number of CPUs */
41#define NR_CPUS 16384
42
a813abf8
MD
43#if defined(_syscall0)
44_syscall0(pid_t, gettid)
45#elif defined(__NR_gettid)
46static inline pid_t gettid(void)
47{
48 return syscall(__NR_gettid);
49}
50#else
51#warning "use pid as tid"
52static inline pid_t gettid(void)
53{
54 return getpid();
55}
56#endif
57
58#ifndef DYNAMIC_LINK_TEST
59#define _LGPL_SOURCE
60#else
61#define debug_yield_read()
62#endif
ec4e58a3 63#include <urcu.h>
a813abf8
MD
64
65struct test_array {
66 int a;
67};
68
69static volatile int test_go, test_stop;
70
71static unsigned long wdelay;
72
73static struct test_array *test_rcu_pointer;
74
4a3e0004 75static unsigned int reclaim_batch = 1;
a813abf8
MD
76
77struct reclaim_queue {
78 void **queue; /* Beginning of queue */
79 void **head; /* Insert position */
80};
81
82static struct reclaim_queue *pending_reclaims;
83
84static unsigned long duration;
85
86/* read-side C.S. duration, in loops */
87static unsigned long rduration;
88
31b598e0
MD
89/* write-side C.S. duration, in loops */
90static unsigned long wduration;
91
a813abf8
MD
92static inline void loop_sleep(unsigned long l)
93{
94 while(l-- != 0)
95 cpu_relax();
96}
97
98static int verbose_mode;
99
100#define printf_verbose(fmt, args...) \
101 do { \
102 if (verbose_mode) \
103 printf(fmt, args); \
104 } while (0)
105
2a7ac59d
MD
106static unsigned int cpu_affinities[NR_CPUS];
107static unsigned int next_aff = 0;
108static int use_affinity = 0;
109
6af882ba
MD
110pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
111
d8540fc5
PA
112#ifndef HAVE_CPU_SET_T
113typedef unsigned long cpu_set_t;
114# define CPU_ZERO(cpuset) do { *(cpuset) = 0; } while(0)
115# define CPU_SET(cpu, cpuset) do { *(cpuset) |= (1UL << (cpu)); } while(0)
116#endif
117
2a7ac59d
MD
118static void set_affinity(void)
119{
120 cpu_set_t mask;
121 int cpu;
6af882ba 122 int ret;
2a7ac59d
MD
123
124 if (!use_affinity)
125 return;
126
d8540fc5 127#if HAVE_SCHED_SETAFFINITY
6af882ba
MD
128 ret = pthread_mutex_lock(&affinity_mutex);
129 if (ret) {
130 perror("Error in pthread mutex lock");
131 exit(-1);
132 }
2a7ac59d 133 cpu = cpu_affinities[next_aff++];
6af882ba
MD
134 ret = pthread_mutex_unlock(&affinity_mutex);
135 if (ret) {
136 perror("Error in pthread mutex unlock");
137 exit(-1);
138 }
d8540fc5 139
2a7ac59d
MD
140 CPU_ZERO(&mask);
141 CPU_SET(cpu, &mask);
d8540fc5
PA
142#if SCHED_SETAFFINITY_ARGS == 2
143 sched_setaffinity(0, &mask);
144#else
2a7ac59d 145 sched_setaffinity(0, sizeof(mask), &mask);
d8540fc5
PA
146#endif
147#endif /* HAVE_SCHED_SETAFFINITY */
2a7ac59d
MD
148}
149
a813abf8
MD
150/*
151 * returns 0 if test should end.
152 */
153static int test_duration_write(void)
154{
155 return !test_stop;
156}
157
158static int test_duration_read(void)
159{
160 return !test_stop;
161}
162
163static unsigned long long __thread nr_writes;
164static unsigned long long __thread nr_reads;
165
166static
167unsigned long long __attribute__((aligned(CACHE_LINE_SIZE))) *tot_nr_writes;
168
169static unsigned int nr_readers;
170static unsigned int nr_writers;
171
172pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
173
174void rcu_copy_mutex_lock(void)
175{
176 int ret;
177 ret = pthread_mutex_lock(&rcu_copy_mutex);
178 if (ret) {
179 perror("Error in pthread mutex lock");
180 exit(-1);
181 }
182}
183
184void rcu_copy_mutex_unlock(void)
185{
186 int ret;
187
188 ret = pthread_mutex_unlock(&rcu_copy_mutex);
189 if (ret) {
190 perror("Error in pthread mutex unlock");
191 exit(-1);
192 }
193}
194
195void *thr_reader(void *_count)
196{
197 unsigned long long *count = _count;
198 struct test_array *local_ptr;
199
200 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
201 "reader", pthread_self(), (unsigned long)gettid());
202
2a7ac59d
MD
203 set_affinity();
204
a813abf8
MD
205 rcu_register_thread();
206
207 while (!test_go)
208 {
209 }
210 smp_mb();
211
212 for (;;) {
213 rcu_read_lock();
214 local_ptr = rcu_dereference(test_rcu_pointer);
215 debug_yield_read();
216 if (local_ptr)
217 assert(local_ptr->a == 8);
218 if (unlikely(rduration))
219 loop_sleep(rduration);
220 rcu_read_unlock();
221 nr_reads++;
222 if (unlikely(!test_duration_read()))
223 break;
224 }
225
226 rcu_unregister_thread();
227
228 *count = nr_reads;
229 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
230 "reader", pthread_self(), (unsigned long)gettid());
231 return ((void*)1);
232
233}
234
53091fe5 235static void rcu_gc_clear_queue(unsigned long wtidx)
a813abf8
MD
236{
237 void **p;
238
53091fe5 239 /* Wait for Q.S and empty queue */
a813abf8
MD
240 synchronize_rcu();
241
242 for (p = pending_reclaims[wtidx].queue;
243 p < pending_reclaims[wtidx].head; p++) {
244 /* poison */
245 if (*p)
246 ((struct test_array *)*p)->a = 0;
247 free(*p);
248 }
249 pending_reclaims[wtidx].head = pending_reclaims[wtidx].queue;
250}
251
53091fe5
MD
252/* Using per-thread queue */
253static void rcu_gc_reclaim(unsigned long wtidx, void *old)
a813abf8 254{
53091fe5
MD
255 /* Queue pointer */
256 *pending_reclaims[wtidx].head = old;
257 pending_reclaims[wtidx].head++;
a813abf8 258
53091fe5
MD
259 if (likely(pending_reclaims[wtidx].head - pending_reclaims[wtidx].queue
260 < reclaim_batch))
261 return;
a813abf8 262
53091fe5 263 rcu_gc_clear_queue(wtidx);
a813abf8
MD
264}
265
266void *thr_writer(void *data)
267{
268 unsigned long wtidx = (unsigned long)data;
321e29d9
MD
269#ifdef TEST_LOCAL_GC
270 struct test_array *old = NULL;
271#else
a813abf8 272 struct test_array *new, *old;
321e29d9 273#endif
a813abf8
MD
274
275 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
276 "writer", pthread_self(), (unsigned long)gettid());
277
2a7ac59d
MD
278 set_affinity();
279
a813abf8
MD
280 while (!test_go)
281 {
282 }
283 smp_mb();
284
285 for (;;) {
321e29d9 286#ifndef TEST_LOCAL_GC
a813abf8 287 new = malloc(sizeof(*new));
a813abf8
MD
288 new->a = 8;
289 old = rcu_xchg_pointer(&test_rcu_pointer, new);
321e29d9 290#endif
31b598e0
MD
291 if (unlikely(wduration))
292 loop_sleep(wduration);
a813abf8
MD
293 rcu_gc_reclaim(wtidx, old);
294 nr_writes++;
295 if (unlikely(!test_duration_write()))
296 break;
297 if (unlikely(wdelay))
298 loop_sleep(wdelay);
299 }
300
301 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
302 "writer", pthread_self(), (unsigned long)gettid());
303 tot_nr_writes[wtidx] = nr_writes;
304 return ((void*)2);
305}
306
307void show_usage(int argc, char **argv)
308{
309 printf("Usage : %s nr_readers nr_writers duration (s)", argv[0]);
310#ifdef DEBUG_YIELD
311 printf(" [-r] [-w] (yield reader and/or writer)");
312#endif
313 printf(" [-d delay] (writer period (us))");
314 printf(" [-c duration] (reader C.S. duration (in loops))");
31b598e0 315 printf(" [-e duration] (writer C.S. duration (in loops))");
a813abf8
MD
316 printf(" [-v] (verbose output)");
317 printf(" [-a cpu#] [-a cpu#]... (affinity)");
318 printf("\n");
319}
320
a813abf8
MD
321int main(int argc, char **argv)
322{
323 int err;
324 pthread_t *tid_reader, *tid_writer;
325 void *tret;
326 unsigned long long *count_reader;
327 unsigned long long tot_reads = 0, tot_writes = 0;
328 int i, a;
a813abf8
MD
329
330 if (argc < 4) {
331 show_usage(argc, argv);
332 return -1;
333 }
334
335 err = sscanf(argv[1], "%u", &nr_readers);
336 if (err != 1) {
337 show_usage(argc, argv);
338 return -1;
339 }
340
341 err = sscanf(argv[2], "%u", &nr_writers);
342 if (err != 1) {
343 show_usage(argc, argv);
344 return -1;
345 }
346
347 err = sscanf(argv[3], "%lu", &duration);
348 if (err != 1) {
349 show_usage(argc, argv);
350 return -1;
351 }
352
a813abf8
MD
353 for (i = 4; i < argc; i++) {
354 if (argv[i][0] != '-')
355 continue;
356 switch (argv[i][1]) {
357#ifdef DEBUG_YIELD
358 case 'r':
359 yield_active |= YIELD_READ;
360 break;
361 case 'w':
362 yield_active |= YIELD_WRITE;
363 break;
364#endif
365 case 'a':
366 if (argc < i + 2) {
367 show_usage(argc, argv);
368 return -1;
369 }
370 a = atoi(argv[++i]);
2a7ac59d 371 cpu_affinities[next_aff++] = a;
a813abf8
MD
372 use_affinity = 1;
373 printf_verbose("Adding CPU %d affinity\n", a);
374 break;
375 case 'b':
376 if (argc < i + 2) {
377 show_usage(argc, argv);
378 return -1;
379 }
380 reclaim_batch = atol(argv[++i]);
381 break;
382 case 'c':
383 if (argc < i + 2) {
384 show_usage(argc, argv);
385 return -1;
386 }
387 rduration = atol(argv[++i]);
388 break;
389 case 'd':
390 if (argc < i + 2) {
391 show_usage(argc, argv);
392 return -1;
393 }
394 wdelay = atol(argv[++i]);
395 break;
31b598e0
MD
396 case 'e':
397 if (argc < i + 2) {
398 show_usage(argc, argv);
399 return -1;
400 }
401 wduration = atol(argv[++i]);
402 break;
a813abf8
MD
403 case 'v':
404 verbose_mode = 1;
405 break;
406 }
407 }
408
409 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
410 duration, nr_readers, nr_writers);
411 printf_verbose("Writer delay : %lu loops.\n", wdelay);
412 printf_verbose("Reader duration : %lu loops.\n", rduration);
413 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
414 "main", pthread_self(), (unsigned long)gettid());
415
a813abf8
MD
416 tid_reader = malloc(sizeof(*tid_reader) * nr_readers);
417 tid_writer = malloc(sizeof(*tid_writer) * nr_writers);
418 count_reader = malloc(sizeof(*count_reader) * nr_readers);
419 tot_nr_writes = malloc(sizeof(*tot_nr_writes) * nr_writers);
420 pending_reclaims = malloc(sizeof(*pending_reclaims) * nr_writers);
421 if (reclaim_batch * sizeof(*pending_reclaims[i].queue)
422 < CACHE_LINE_SIZE)
423 for (i = 0; i < nr_writers; i++)
424 pending_reclaims[i].queue = calloc(1, CACHE_LINE_SIZE);
425 else
426 for (i = 0; i < nr_writers; i++)
427 pending_reclaims[i].queue = calloc(reclaim_batch,
428 sizeof(*pending_reclaims[i].queue));
429 for (i = 0; i < nr_writers; i++)
430 pending_reclaims[i].head = pending_reclaims[i].queue;
431
2a7ac59d
MD
432 next_aff = 0;
433
a813abf8
MD
434 for (i = 0; i < nr_readers; i++) {
435 err = pthread_create(&tid_reader[i], NULL, thr_reader,
436 &count_reader[i]);
437 if (err != 0)
438 exit(1);
439 }
440 for (i = 0; i < nr_writers; i++) {
441 err = pthread_create(&tid_writer[i], NULL, thr_writer,
442 (void *)(long)i);
443 if (err != 0)
444 exit(1);
445 }
446
447 smp_mb();
448
449 test_go = 1;
450
451 sleep(duration);
452
453 test_stop = 1;
454
455 for (i = 0; i < nr_readers; i++) {
456 err = pthread_join(tid_reader[i], &tret);
457 if (err != 0)
458 exit(1);
459 tot_reads += count_reader[i];
460 }
461 for (i = 0; i < nr_writers; i++) {
462 err = pthread_join(tid_writer[i], &tret);
463 if (err != 0)
464 exit(1);
465 tot_writes += tot_nr_writes[i];
53091fe5 466 rcu_gc_clear_queue(i);
a813abf8
MD
467 }
468
469 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
470 tot_writes);
a50a7b43 471 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu "
a813abf8 472 "nr_writers %3u "
4a3e0004
MD
473 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu "
474 "batch %u\n",
a50a7b43 475 argv[0], duration, nr_readers, rduration, wduration,
a813abf8 476 nr_writers, wdelay, tot_reads, tot_writes,
4a3e0004 477 tot_reads + tot_writes, reclaim_batch);
a813abf8
MD
478 free(tid_reader);
479 free(tid_writer);
480 free(count_reader);
481 free(tot_nr_writes);
482 for (i = 0; i < nr_writers; i++)
483 free(pending_reclaims[i].queue);
484 free(pending_reclaims);
485
486 return 0;
487}
This page took 0.044115 seconds and 4 git commands to generate.