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