test rcuja range: fix print format
[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();
299 range = cds_ja_range_add(test_ja, start, end, NULL);
300 if (!range) {
5bdedcd3 301 fprintf(stderr, "Error in cds_ja_range_add\n");
91233b5b
MD
302 } else {
303 URCU_TLS(nr_add)++;
304 }
305 rcu_read_unlock();
306 } else {
307 struct cds_ja_range *range;
308 uint64_t key;
309
310 /* May delete */
311 /* note: only deleting ulong keys */
312 key = ((unsigned long) rand_r(&URCU_TLS(rand_lookup)) % write_pool_size) + write_pool_offset;
313 key *= key_mul;
314
315 rcu_read_lock();
316
317 range = cds_ja_range_lookup(test_ja, key);
318 if (range) {
319 ret = cds_ja_range_del(test_ja, range);
320 if (!ret) {
321 URCU_TLS(nr_del)++;
322 } else {
323 URCU_TLS(nr_delnoent)++;
324 }
325 } else {
326 URCU_TLS(nr_delnoent)++;
327 }
328 rcu_read_unlock();
329 }
330
331 URCU_TLS(nr_writes)++;
332 if (caa_unlikely(!test_duration_write()))
333 break;
334 if (caa_unlikely(wdelay))
335 loop_sleep(wdelay);
336 if (caa_unlikely((URCU_TLS(nr_writes) & ((1 << 10) - 1)) == 0))
337 rcu_quiescent_state();
338 }
339
340 rcu_unregister_thread();
341
342 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
343 "writer", pthread_self(), (unsigned long) gettid());
344 printf_verbose("info id %lx: nr_add %lu, nr_addexist %lu, nr_del %lu, "
345 "nr_delnoent %lu\n", pthread_self(), URCU_TLS(nr_add),
346 URCU_TLS(nr_addexist), URCU_TLS(nr_del),
347 URCU_TLS(nr_delnoent));
348 count->update_ops = URCU_TLS(nr_writes);
349 count->add = URCU_TLS(nr_add);
350 count->add_exist = URCU_TLS(nr_addexist);
351 count->remove = URCU_TLS(nr_del);
352 return ((void*)2);
353}
354
355static
356int do_mt_populate_ja(void)
357{
358 uint64_t iter;
359 int ret;
360
361 if (!init_populate)
362 return 0;
363
364 printf("Starting rw test\n");
365
366 for (iter = init_pool_offset; iter < init_pool_offset + init_pool_size; iter++) {
367 struct cds_ja_range *range;
368 uint64_t key;
369
370 /* note: only inserting ulong keys */
371 key = (unsigned long) iter;
372 key *= key_mul;
373 rcu_read_lock();
374 range = cds_ja_range_add(test_ja, key, key, NULL);
375 URCU_TLS(nr_add)++;
376 URCU_TLS(nr_writes)++;
377 rcu_read_unlock();
378 if (!range) {
379 fprintf(stderr, "Error adding range %" PRIu64 "\n",
380 key);
381 assert(0);
382 }
383 }
384 return 0;
385}
386
387static
388int do_mt_test(void)
389{
390 pthread_t *tid_reader, *tid_writer;
391 void *tret;
392 int ret, i, err;
393 unsigned long long *count_reader;
394 struct wr_count *count_writer;
395 unsigned long long tot_reads = 0, tot_writes = 0,
396 tot_add = 0, tot_add_exist = 0, tot_remove = 0;
397 unsigned int remain;
398
399 tid_reader = malloc(sizeof(*tid_reader) * nr_readers);
400 tid_writer = malloc(sizeof(*tid_writer) * nr_writers);
401 count_reader = malloc(sizeof(*count_reader) * nr_readers);
402 count_writer = malloc(sizeof(*count_writer) * nr_writers);
403
404 printf("Allocating Judy Array for ranges\n");
405 test_ja = cds_ja_range_new();
406 if (!test_ja) {
407 printf("Error allocating judy array.\n");
408 ret = -1;
409 goto end;
410 }
411
412 do_mt_populate_ja();
413
414 next_aff = 0;
415
416 for (i = 0; i < nr_readers; i++) {
417 err = pthread_create(&tid_reader[i],
418 NULL, test_ja_rw_thr_reader,
419 &count_reader[i]);
420 if (err != 0)
421 exit(1);
422 }
423 for (i = 0; i < nr_writers; i++) {
424 err = pthread_create(&tid_writer[i],
425 NULL, test_ja_rw_thr_writer,
426 &count_writer[i]);
427 if (err != 0)
428 exit(1);
429 }
430
431 cmm_smp_mb();
432
433 test_go = 1;
434
435 rcu_thread_offline_qsbr();
436
437 remain = duration;
438 do {
439 remain = sleep(remain);
440 } while (remain > 0);
441
442 test_stop = 1;
443
444 for (i = 0; i < nr_readers; i++) {
445 err = pthread_join(tid_reader[i], &tret);
446 if (err != 0)
447 exit(1);
448 tot_reads += count_reader[i];
449 }
450 for (i = 0; i < nr_writers; i++) {
451 err = pthread_join(tid_writer[i], &tret);
452 if (err != 0)
453 exit(1);
454 tot_writes += count_writer[i].update_ops;
455 tot_add += count_writer[i].add;
456 tot_add_exist += count_writer[i].add_exist;
457 tot_remove += count_writer[i].remove;
458 }
459 rcu_thread_online_qsbr();
460
461 ret = cds_ja_range_destroy(test_ja, NULL);
462 if (ret) {
463 fprintf(stderr, "Error destroying judy array\n");
464 goto end;
465 }
466
467 free(tid_reader);
468 free(tid_writer);
469 free(count_reader);
470 free(count_writer);
471 ret = 0;
472end:
473 return ret;
474}
475
476int main(int argc, char **argv)
477{
478 int i, j, a, ret, err;
479 uint64_t key;
480 struct sigaction act;
481
482 if (argc < 4) {
483 show_usage(argc, argv);
484 return -1;
485 }
486
487 err = sscanf(argv[1], "%u", &nr_readers);
488 if (err != 1) {
489 show_usage(argc, argv);
490 return -1;
491 }
492
493 err = sscanf(argv[2], "%u", &nr_writers);
494 if (err != 1) {
495 show_usage(argc, argv);
496 return -1;
497 }
498
499 err = sscanf(argv[3], "%lu", &duration);
500 if (err != 1) {
501 show_usage(argc, argv);
502 return -1;
503 }
504
505 for (i = 4; i < argc; i++) {
506 if (argv[i][0] != '-')
507 continue;
508 switch (argv[i][1]) {
509#ifdef DEBUG_YIELD
510 case 'r':
511 yield_active |= YIELD_READ;
512 break;
513 case 'w':
514 yield_active |= YIELD_WRITE;
515 break;
516#endif
517 case 'a':
518 if (argc < i + 2) {
519 show_usage(argc, argv);
520 return -1;
521 }
522 a = atoi(argv[++i]);
523 cpu_affinities[next_aff++] = a;
524 use_affinity = 1;
525 printf_verbose("Adding CPU %d affinity\n", a);
526 break;
527 case 'c':
528 if (argc < i + 2) {
529 show_usage(argc, argv);
530 return -1;
531 }
532 rduration = atol(argv[++i]);
533 break;
534 case 'd':
535 if (argc < i + 2) {
536 show_usage(argc, argv);
537 return -1;
538 }
539 wdelay = atol(argv[++i]);
540 break;
541 case 'v':
542 verbose_mode = 1;
543 break;
544 case 'r':
545 add_ratio = atoi(argv[++i]);
546 break;
547 case 'k':
548 init_populate = 1;
549 break;
550 case 'R':
551 lookup_pool_offset = atol(argv[++i]);
552 break;
553 case 'S':
554 write_pool_offset = atol(argv[++i]);
555 break;
556 case 'T':
557 init_pool_offset = atol(argv[++i]);
558 break;
559 case 'M':
560 lookup_pool_size = atol(argv[++i]);
561 break;
562 case 'N':
563 write_pool_size = atol(argv[++i]);
564 break;
565 case 'O':
566 init_pool_size = atol(argv[++i]);
567 break;
568 case 'V':
569 validate_lookup = 1;
570 break;
571 case 't':
572 sanity_test = 1;
573 break;
574 case 'B':
575 key_bits = atol(argv[++i]);
576 break;
577 case 'm':
578 key_mul = atoll(argv[++i]);
579 break;
580 case 'u':
581 add_unique = 1;
582 break;
583 case 's':
584 add_replace = 1;
585 break;
586 case 'l':
587 leak_detection = 1;
588 break;
589 }
590 }
591
592 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
593 duration, nr_readers, nr_writers);
594 printf_verbose("Writer delay : %lu loops.\n", wdelay);
595 printf_verbose("Reader duration : %lu loops.\n", rduration);
596 printf_verbose("Add ratio: %u%%.\n", add_ratio);
597 printf_verbose("Mode:%s%s.\n",
598 " add/remove",
599 add_unique ? " uniquify" : ( add_replace ? " replace" : " insert"));
600 printf_verbose("Key multiplication factor: %" PRIu64 ".\n", key_mul);
601 printf_verbose("Init pool size offset %lu size %lu.\n",
602 init_pool_offset, init_pool_size);
603 printf_verbose("Lookup pool size offset %lu size %lu.\n",
604 lookup_pool_offset, lookup_pool_size);
605 printf_verbose("Update pool size offset %lu size %lu.\n",
606 write_pool_offset, write_pool_size);
607 if (validate_lookup)
608 printf_verbose("Validating lookups.\n");
609 if (leak_detection)
610 printf_verbose("Memory leak dection activated.\n");
611 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
612 "main", pthread_self(), (unsigned long)gettid());
613
614 memset(&act, 0, sizeof(act));
615 ret = sigemptyset(&act.sa_mask);
616 if (ret == -1) {
617 perror("sigemptyset");
618 return -1;
619 }
620 act.sa_handler = test_ja_rw_sigusr1_handler;
621 act.sa_flags = SA_RESTART;
622 ret = sigaction(SIGUSR1, &act, NULL);
623 if (ret == -1) {
624 perror("sigaction");
625 return -1;
626 }
627
628 err = create_all_cpu_call_rcu_data(0);
629 if (err) {
630 printf("Per-CPU call_rcu() worker threads unavailable. Using default global worker thread.\n");
631 }
632
633 rcu_register_thread();
634
635 ret = do_mt_test();
636
637 /* Wait for in-flight call_rcu free to complete for leak detection */
638 rcu_barrier();
639
640 rcu_unregister_thread();
641 free_all_cpu_call_rcu_data();
642
643 if (ret) {
644 printf("Test ended with error: %d\n", ret);
645 }
646 return ret;
647}
This page took 0.044658 seconds and 4 git commands to generate.