rcuja range: bugfixes and validation
[userspace-rcu.git] / tests / test_urcu_ja_range.c
CommitLineData
91233b5b
MD
1/*
2 * test_urcu_ja_range.c
3 *
4 * Userspace RCU library - test program
5 *
6 * Copyright 2009-2012 - 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 "test_urcu_ja_range.h"
25#include <inttypes.h>
26#include <stdint.h>
27
28DEFINE_URCU_TLS(unsigned int, rand_lookup);
29DEFINE_URCU_TLS(unsigned long, nr_add);
30DEFINE_URCU_TLS(unsigned long, nr_addexist);
31DEFINE_URCU_TLS(unsigned long, nr_del);
32DEFINE_URCU_TLS(unsigned long, nr_delnoent);
33DEFINE_URCU_TLS(unsigned long, lookup_fail);
34DEFINE_URCU_TLS(unsigned long, lookup_ok);
35
36struct cds_ja *test_ja;
37
38volatile int test_go, test_stop;
39
40unsigned long wdelay;
41
42unsigned long duration;
43
44/* read-side C.S. duration, in loops */
45unsigned long rduration;
46
47unsigned long init_populate;
48int add_only;
49
50unsigned long init_pool_offset, lookup_pool_offset, write_pool_offset;
51unsigned long init_pool_size = DEFAULT_RAND_POOL,
52 lookup_pool_size = DEFAULT_RAND_POOL,
53 write_pool_size = DEFAULT_RAND_POOL;
54int validate_lookup;
55int sanity_test;
56unsigned int key_bits = 32;
57
58int count_pipe[2];
59
60int verbose_mode;
61
62unsigned int cpu_affinities[NR_CPUS];
63unsigned int next_aff = 0;
64int use_affinity = 0;
65
66pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
67
68DEFINE_URCU_TLS(unsigned long long, nr_writes);
69DEFINE_URCU_TLS(unsigned long long, nr_reads);
70
71unsigned int nr_readers;
72unsigned int nr_writers;
73
74static unsigned int add_ratio = 50;
75static uint64_t key_mul = 1ULL;
76
77static int add_unique, add_replace;
78
79static pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
80
81static int leak_detection;
82static unsigned long test_nodes_allocated, test_nodes_freed;
83
84void set_affinity(void)
85{
86 cpu_set_t mask;
87 int cpu;
88 int ret;
89
90 if (!use_affinity)
91 return;
92
93#if HAVE_SCHED_SETAFFINITY
94 ret = pthread_mutex_lock(&affinity_mutex);
95 if (ret) {
96 perror("Error in pthread mutex lock");
97 exit(-1);
98 }
99 cpu = cpu_affinities[next_aff++];
100 ret = pthread_mutex_unlock(&affinity_mutex);
101 if (ret) {
102 perror("Error in pthread mutex unlock");
103 exit(-1);
104 }
105 CPU_ZERO(&mask);
106 CPU_SET(cpu, &mask);
107#if SCHED_SETAFFINITY_ARGS == 2
108 sched_setaffinity(0, &mask);
109#else
110 sched_setaffinity(0, sizeof(mask), &mask);
111#endif
112#endif /* HAVE_SCHED_SETAFFINITY */
113}
114
115void rcu_copy_mutex_lock(void)
116{
117 int ret;
118 ret = pthread_mutex_lock(&rcu_copy_mutex);
119 if (ret) {
120 perror("Error in pthread mutex lock");
121 exit(-1);
122 }
123}
124
125void rcu_copy_mutex_unlock(void)
126{
127 int ret;
128
129 ret = pthread_mutex_unlock(&rcu_copy_mutex);
130 if (ret) {
131 perror("Error in pthread mutex unlock");
132 exit(-1);
133 }
134}
135
136void show_usage(int argc, char **argv)
137{
138 printf("Usage : %s nr_readers nr_writers duration (s)\n", argv[0]);
139#ifdef DEBUG_YIELD
140 printf(" [-r] [-w] (yield reader and/or writer)\n");
141#endif
142 printf(" [-d delay] (writer period (us))\n");
143 printf(" [-c duration] (reader C.S. duration (in loops))\n");
144 printf(" [-v] (verbose output)\n");
145 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
146 printf(" [-u] Add unique keys.\n");
147 printf(" [-s] Replace existing keys.\n");
148printf(" [not -u nor -s] Add entries (supports redundant keys).\n");
149 printf(" [-r ratio] Add ratio (in %% of add+removal).\n");
150 printf(" [-k] Populate init nodes.\n");
151 printf(" [-R offset] Lookup pool offset.\n");
152 printf(" [-S offset] Write pool offset.\n");
153 printf(" [-T offset] Init pool offset.\n");
154 printf(" [-M size] Lookup pool size.\n");
155 printf(" [-N size] Write pool size.\n");
156 printf(" [-O size] Init pool size.\n");
157 printf(" [-V] Validate lookups of init values (use with filled init pool, same lookup range, with different write range).\n");
158 printf(" [-t] Do sanity test.\n");
159 printf(" [-B] Key bits for multithread test (default: 32).\n");
160 printf(" [-m factor] Key multiplication factor.\n");
161 printf(" [-l] Memory leak detection.\n");
162 printf("\n\n");
163}
164
165enum urcu_ja_addremove {
166 AR_RANDOM = 0,
167 AR_ADD = 1,
168 AR_REMOVE = -1,
169}; /* 1: add, -1 remove, 0: random */
170
171static enum urcu_ja_addremove addremove; /* 1: add, -1 remove, 0: random */
172
173static
174void test_ja_rw_sigusr1_handler(int signo)
175{
176 switch (addremove) {
177 case AR_ADD:
178 printf("Add/Remove: random.\n");
179 addremove = AR_RANDOM;
180 break;
181 case AR_RANDOM:
182 printf("Add/Remove: remove only.\n");
183 addremove = AR_REMOVE;
184 break;
185 case AR_REMOVE:
186 printf("Add/Remove: add only.\n");
187 addremove = AR_ADD;
188 break;
189 }
190}
191
192static
193void *test_ja_rw_thr_reader(void *_count)
194{
195 unsigned long long *count = _count;
196 struct cds_ja_range *range;
197 uint64_t key;
198
199 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
200 "reader", pthread_self(), (unsigned long) gettid());
201
202 set_affinity();
203
204 rcu_register_thread();
205
206 while (!test_go)
207 {
208 }
209 cmm_smp_mb();
210
211 for (;;) {
212 rcu_read_lock();
213
214 /* note: only looking up ulong keys */
215 key = ((unsigned long) rand_r(&URCU_TLS(rand_lookup)) % lookup_pool_size) + lookup_pool_offset;
216 key *= key_mul;
217 range = cds_ja_range_lookup(test_ja, key);
218 if (!range) {
219 if (validate_lookup) {
220 printf("[ERROR] Lookup cannot find initial node.\n");
221 exit(-1);
222 }
223 URCU_TLS(lookup_fail)++;
224 } else {
225 range = cds_ja_range_lock(range);
226 if (!range) {
227 if (validate_lookup) {
228 printf("[ERROR] Lookup cannot find initial node.\n");
229 exit(-1);
230 }
231 } else {
232 URCU_TLS(lookup_ok)++;
233 cds_ja_range_unlock(range);
234 }
235 }
236 rcu_debug_yield_read();
237 if (caa_unlikely(rduration))
238 loop_sleep(rduration);
239 rcu_read_unlock();
240 URCU_TLS(nr_reads)++;
241 if (caa_unlikely(!test_duration_read()))
242 break;
243 if (caa_unlikely((URCU_TLS(nr_reads) & ((1 << 10) - 1)) == 0))
244 rcu_quiescent_state();
245 }
246
247 rcu_unregister_thread();
248
249 *count = URCU_TLS(nr_reads);
250 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
251 "reader", pthread_self(), (unsigned long) gettid());
252 printf_verbose("readid : %lx, lookupfail %lu, lookupok %lu\n",
253 pthread_self(), URCU_TLS(lookup_fail),
254 URCU_TLS(lookup_ok));
255 return ((void*)1);
256}
257
258static
259int is_add(void)
260{
261 return ((unsigned int) rand_r(&URCU_TLS(rand_lookup)) % 100) < add_ratio;
262}
263
264static
265void *test_ja_rw_thr_writer(void *_count)
266{
267 struct wr_count *count = _count;
268 int ret;
269
270 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
271 "writer", pthread_self(), (unsigned long) gettid());
272
273 set_affinity();
274
275 rcu_register_thread();
276
277 while (!test_go)
278 {
279 }
280 cmm_smp_mb();
281
282 for (;;) {
283 if ((addremove == AR_ADD)
284 || (addremove == AR_RANDOM && is_add())) {
285 struct cds_ja_range *range;
286 uint64_t start, end, tmp;
287
288 /* note: only inserting ulong keys */
289 start = ((unsigned long) rand_r(&URCU_TLS(rand_lookup)) % write_pool_size) + write_pool_offset;
290 end = ((unsigned long) rand_r(&URCU_TLS(rand_lookup)) % write_pool_size) + write_pool_offset;
291 start *= key_mul;
292 end *= key_mul;
293 if (start > end) {
294 tmp = start;
295 start = end;
296 end = tmp;
297 }
298 rcu_read_lock();
cd7eb7a3
MD
299 ret = cds_ja_range_add(test_ja, start, end, NULL);
300 if (ret) {
301 if (ret == -EEXIST) {
302 URCU_TLS(nr_addexist)++;
303 } else {
304 assert(0);
305 }
91233b5b
MD
306 } else {
307 URCU_TLS(nr_add)++;
308 }
309 rcu_read_unlock();
310 } else {
311 struct cds_ja_range *range;
312 uint64_t key;
313
314 /* May delete */
315 /* note: only deleting ulong keys */
316 key = ((unsigned long) rand_r(&URCU_TLS(rand_lookup)) % write_pool_size) + write_pool_offset;
317 key *= key_mul;
318
319 rcu_read_lock();
320
321 range = cds_ja_range_lookup(test_ja, key);
322 if (range) {
323 ret = cds_ja_range_del(test_ja, range);
324 if (!ret) {
325 URCU_TLS(nr_del)++;
326 } else {
327 URCU_TLS(nr_delnoent)++;
328 }
329 } else {
330 URCU_TLS(nr_delnoent)++;
331 }
332 rcu_read_unlock();
333 }
334
335 URCU_TLS(nr_writes)++;
336 if (caa_unlikely(!test_duration_write()))
337 break;
338 if (caa_unlikely(wdelay))
339 loop_sleep(wdelay);
340 if (caa_unlikely((URCU_TLS(nr_writes) & ((1 << 10) - 1)) == 0))
341 rcu_quiescent_state();
342 }
343
344 rcu_unregister_thread();
345
346 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
347 "writer", pthread_self(), (unsigned long) gettid());
348 printf_verbose("info id %lx: nr_add %lu, nr_addexist %lu, nr_del %lu, "
349 "nr_delnoent %lu\n", pthread_self(), URCU_TLS(nr_add),
350 URCU_TLS(nr_addexist), URCU_TLS(nr_del),
351 URCU_TLS(nr_delnoent));
352 count->update_ops = URCU_TLS(nr_writes);
353 count->add = URCU_TLS(nr_add);
354 count->add_exist = URCU_TLS(nr_addexist);
355 count->remove = URCU_TLS(nr_del);
356 return ((void*)2);
357}
358
359static
360int do_mt_populate_ja(void)
361{
362 uint64_t iter;
363 int ret;
364
365 if (!init_populate)
366 return 0;
367
368 printf("Starting rw test\n");
369
370 for (iter = init_pool_offset; iter < init_pool_offset + init_pool_size; iter++) {
371 struct cds_ja_range *range;
372 uint64_t key;
373
374 /* note: only inserting ulong keys */
375 key = (unsigned long) iter;
376 key *= key_mul;
377 rcu_read_lock();
cd7eb7a3 378 ret = cds_ja_range_add(test_ja, key, key, NULL);
91233b5b
MD
379 URCU_TLS(nr_add)++;
380 URCU_TLS(nr_writes)++;
381 rcu_read_unlock();
cd7eb7a3
MD
382 if (ret) {
383 fprintf(stderr, "Error (%d) adding range %" PRIu64 "\n",
384 ret, key);
91233b5b
MD
385 assert(0);
386 }
387 }
388 return 0;
389}
390
391static
392int do_mt_test(void)
393{
394 pthread_t *tid_reader, *tid_writer;
395 void *tret;
396 int ret, i, err;
397 unsigned long long *count_reader;
398 struct wr_count *count_writer;
399 unsigned long long tot_reads = 0, tot_writes = 0,
400 tot_add = 0, tot_add_exist = 0, tot_remove = 0;
401 unsigned int remain;
402
403 tid_reader = malloc(sizeof(*tid_reader) * nr_readers);
404 tid_writer = malloc(sizeof(*tid_writer) * nr_writers);
405 count_reader = malloc(sizeof(*count_reader) * nr_readers);
406 count_writer = malloc(sizeof(*count_writer) * nr_writers);
407
408 printf("Allocating Judy Array for ranges\n");
409 test_ja = cds_ja_range_new();
410 if (!test_ja) {
411 printf("Error allocating judy array.\n");
412 ret = -1;
413 goto end;
414 }
415
416 do_mt_populate_ja();
417
418 next_aff = 0;
419
420 for (i = 0; i < nr_readers; i++) {
421 err = pthread_create(&tid_reader[i],
422 NULL, test_ja_rw_thr_reader,
423 &count_reader[i]);
424 if (err != 0)
425 exit(1);
426 }
427 for (i = 0; i < nr_writers; i++) {
428 err = pthread_create(&tid_writer[i],
429 NULL, test_ja_rw_thr_writer,
430 &count_writer[i]);
431 if (err != 0)
432 exit(1);
433 }
434
435 cmm_smp_mb();
436
437 test_go = 1;
438
439 rcu_thread_offline_qsbr();
440
441 remain = duration;
442 do {
443 remain = sleep(remain);
444 } while (remain > 0);
445
446 test_stop = 1;
447
448 for (i = 0; i < nr_readers; i++) {
449 err = pthread_join(tid_reader[i], &tret);
450 if (err != 0)
451 exit(1);
452 tot_reads += count_reader[i];
453 }
454 for (i = 0; i < nr_writers; i++) {
455 err = pthread_join(tid_writer[i], &tret);
456 if (err != 0)
457 exit(1);
458 tot_writes += count_writer[i].update_ops;
459 tot_add += count_writer[i].add;
460 tot_add_exist += count_writer[i].add_exist;
461 tot_remove += count_writer[i].remove;
462 }
463 rcu_thread_online_qsbr();
464
9a655aa8
MD
465 ret = cds_ja_range_validate(test_ja);
466 assert(!ret);
467
91233b5b
MD
468 ret = cds_ja_range_destroy(test_ja, NULL);
469 if (ret) {
470 fprintf(stderr, "Error destroying judy array\n");
471 goto end;
472 }
473
474 free(tid_reader);
475 free(tid_writer);
476 free(count_reader);
477 free(count_writer);
478 ret = 0;
479end:
480 return ret;
481}
482
483int main(int argc, char **argv)
484{
485 int i, j, a, ret, err;
486 uint64_t key;
487 struct sigaction act;
488
489 if (argc < 4) {
490 show_usage(argc, argv);
491 return -1;
492 }
493
494 err = sscanf(argv[1], "%u", &nr_readers);
495 if (err != 1) {
496 show_usage(argc, argv);
497 return -1;
498 }
499
500 err = sscanf(argv[2], "%u", &nr_writers);
501 if (err != 1) {
502 show_usage(argc, argv);
503 return -1;
504 }
505
506 err = sscanf(argv[3], "%lu", &duration);
507 if (err != 1) {
508 show_usage(argc, argv);
509 return -1;
510 }
511
512 for (i = 4; i < argc; i++) {
513 if (argv[i][0] != '-')
514 continue;
515 switch (argv[i][1]) {
516#ifdef DEBUG_YIELD
517 case 'r':
518 yield_active |= YIELD_READ;
519 break;
520 case 'w':
521 yield_active |= YIELD_WRITE;
522 break;
523#endif
524 case 'a':
525 if (argc < i + 2) {
526 show_usage(argc, argv);
527 return -1;
528 }
529 a = atoi(argv[++i]);
530 cpu_affinities[next_aff++] = a;
531 use_affinity = 1;
532 printf_verbose("Adding CPU %d affinity\n", a);
533 break;
534 case 'c':
535 if (argc < i + 2) {
536 show_usage(argc, argv);
537 return -1;
538 }
539 rduration = atol(argv[++i]);
540 break;
541 case 'd':
542 if (argc < i + 2) {
543 show_usage(argc, argv);
544 return -1;
545 }
546 wdelay = atol(argv[++i]);
547 break;
548 case 'v':
549 verbose_mode = 1;
550 break;
551 case 'r':
552 add_ratio = atoi(argv[++i]);
553 break;
554 case 'k':
555 init_populate = 1;
556 break;
557 case 'R':
558 lookup_pool_offset = atol(argv[++i]);
559 break;
560 case 'S':
561 write_pool_offset = atol(argv[++i]);
562 break;
563 case 'T':
564 init_pool_offset = atol(argv[++i]);
565 break;
566 case 'M':
567 lookup_pool_size = atol(argv[++i]);
568 break;
569 case 'N':
570 write_pool_size = atol(argv[++i]);
571 break;
572 case 'O':
573 init_pool_size = atol(argv[++i]);
574 break;
575 case 'V':
576 validate_lookup = 1;
577 break;
578 case 't':
579 sanity_test = 1;
580 break;
581 case 'B':
582 key_bits = atol(argv[++i]);
583 break;
584 case 'm':
585 key_mul = atoll(argv[++i]);
586 break;
587 case 'u':
588 add_unique = 1;
589 break;
590 case 's':
591 add_replace = 1;
592 break;
593 case 'l':
594 leak_detection = 1;
595 break;
596 }
597 }
598
599 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
600 duration, nr_readers, nr_writers);
601 printf_verbose("Writer delay : %lu loops.\n", wdelay);
602 printf_verbose("Reader duration : %lu loops.\n", rduration);
603 printf_verbose("Add ratio: %u%%.\n", add_ratio);
604 printf_verbose("Mode:%s%s.\n",
605 " add/remove",
606 add_unique ? " uniquify" : ( add_replace ? " replace" : " insert"));
607 printf_verbose("Key multiplication factor: %" PRIu64 ".\n", key_mul);
608 printf_verbose("Init pool size offset %lu size %lu.\n",
609 init_pool_offset, init_pool_size);
610 printf_verbose("Lookup pool size offset %lu size %lu.\n",
611 lookup_pool_offset, lookup_pool_size);
612 printf_verbose("Update pool size offset %lu size %lu.\n",
613 write_pool_offset, write_pool_size);
614 if (validate_lookup)
615 printf_verbose("Validating lookups.\n");
616 if (leak_detection)
617 printf_verbose("Memory leak dection activated.\n");
618 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
619 "main", pthread_self(), (unsigned long)gettid());
620
621 memset(&act, 0, sizeof(act));
622 ret = sigemptyset(&act.sa_mask);
623 if (ret == -1) {
624 perror("sigemptyset");
625 return -1;
626 }
627 act.sa_handler = test_ja_rw_sigusr1_handler;
628 act.sa_flags = SA_RESTART;
629 ret = sigaction(SIGUSR1, &act, NULL);
630 if (ret == -1) {
631 perror("sigaction");
632 return -1;
633 }
634
635 err = create_all_cpu_call_rcu_data(0);
636 if (err) {
637 printf("Per-CPU call_rcu() worker threads unavailable. Using default global worker thread.\n");
638 }
639
640 rcu_register_thread();
641
642 ret = do_mt_test();
643
644 /* Wait for in-flight call_rcu free to complete for leak detection */
645 rcu_barrier();
646
647 rcu_unregister_thread();
648 free_all_cpu_call_rcu_data();
649
650 if (ret) {
651 printf("Test ended with error: %d\n", ret);
652 }
653 return ret;
654}
This page took 0.045284 seconds and 4 git commands to generate.