Merge branch 'master' into urcu/rcuja-range-merge
[userspace-rcu.git] / tests / regression / 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
22789e8f 74static unsigned int add_ratio = 50, range_max_len = 0;
91233b5b
MD
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");
22789e8f 162 printf(" [-L len] Range max len.\n");
91233b5b
MD
163 printf("\n\n");
164}
165
166enum urcu_ja_addremove {
167 AR_RANDOM = 0,
168 AR_ADD = 1,
169 AR_REMOVE = -1,
170}; /* 1: add, -1 remove, 0: random */
171
172static enum urcu_ja_addremove addremove; /* 1: add, -1 remove, 0: random */
173
174static
175void test_ja_rw_sigusr1_handler(int signo)
176{
177 switch (addremove) {
178 case AR_ADD:
179 printf("Add/Remove: random.\n");
180 addremove = AR_RANDOM;
181 break;
182 case AR_RANDOM:
183 printf("Add/Remove: remove only.\n");
184 addremove = AR_REMOVE;
185 break;
186 case AR_REMOVE:
187 printf("Add/Remove: add only.\n");
188 addremove = AR_ADD;
189 break;
190 }
191}
192
193static
194void *test_ja_rw_thr_reader(void *_count)
195{
196 unsigned long long *count = _count;
197 struct cds_ja_range *range;
198 uint64_t key;
199
5bcf8326
MD
200 printf_verbose("thread_begin %s, tid %lu\n",
201 "reader", urcu_get_thread_id());
91233b5b 202
5bcf8326 203 URCU_TLS(rand_lookup) = (unsigned int) urcu_get_thread_id() ^ time(NULL);
b10c8ba8 204
91233b5b
MD
205 set_affinity();
206
207 rcu_register_thread();
208
209 while (!test_go)
210 {
211 }
212 cmm_smp_mb();
213
214 for (;;) {
215 rcu_read_lock();
216
b10c8ba8
MD
217 key = (uint64_t) rand_r(&URCU_TLS(rand_lookup));
218 key += (uint64_t) rand_r(&URCU_TLS(rand_lookup)) << 32ULL;
219 key = (key % lookup_pool_size) + lookup_pool_offset;
91233b5b 220 key *= key_mul;
b10c8ba8 221
91233b5b
MD
222 range = cds_ja_range_lookup(test_ja, key);
223 if (!range) {
224 if (validate_lookup) {
225 printf("[ERROR] Lookup cannot find initial node.\n");
226 exit(-1);
227 }
228 URCU_TLS(lookup_fail)++;
229 } else {
230 range = cds_ja_range_lock(range);
231 if (!range) {
232 if (validate_lookup) {
233 printf("[ERROR] Lookup cannot find initial node.\n");
234 exit(-1);
235 }
236 } else {
237 URCU_TLS(lookup_ok)++;
238 cds_ja_range_unlock(range);
239 }
240 }
241 rcu_debug_yield_read();
242 if (caa_unlikely(rduration))
243 loop_sleep(rduration);
244 rcu_read_unlock();
245 URCU_TLS(nr_reads)++;
246 if (caa_unlikely(!test_duration_read()))
247 break;
248 if (caa_unlikely((URCU_TLS(nr_reads) & ((1 << 10) - 1)) == 0))
249 rcu_quiescent_state();
250 }
251
252 rcu_unregister_thread();
253
254 *count = URCU_TLS(nr_reads);
5bcf8326
MD
255 printf_verbose("thread_end %s, tid %lu\n",
256 "reader", urcu_get_thread_id());
257 printf_verbose("read tid : %lu, lookupfail %lu, lookupok %lu\n",
258 urcu_get_thread_id(), URCU_TLS(lookup_fail),
91233b5b
MD
259 URCU_TLS(lookup_ok));
260 return ((void*)1);
261}
262
263static
264int is_add(void)
265{
266 return ((unsigned int) rand_r(&URCU_TLS(rand_lookup)) % 100) < add_ratio;
267}
268
269static
270void *test_ja_rw_thr_writer(void *_count)
271{
272 struct wr_count *count = _count;
273 int ret;
274
5bcf8326
MD
275 printf_verbose("thread_begin %s, tid %lu\n",
276 "writer", urcu_get_thread_id());
91233b5b 277
5bcf8326 278 URCU_TLS(rand_lookup) = (unsigned int) urcu_get_thread_id() ^ time(NULL);
b10c8ba8 279
91233b5b
MD
280 set_affinity();
281
282 rcu_register_thread();
283
284 while (!test_go)
285 {
286 }
287 cmm_smp_mb();
288
289 for (;;) {
290 if ((addremove == AR_ADD)
291 || (addremove == AR_RANDOM && is_add())) {
292 struct cds_ja_range *range;
293 uint64_t start, end, tmp;
294
b10c8ba8
MD
295 start = (uint64_t) rand_r(&URCU_TLS(rand_lookup));
296 start += (uint64_t) rand_r(&URCU_TLS(rand_lookup)) << 32ULL;
297 start = (start % write_pool_size) + write_pool_offset;
298
299 end = (uint64_t) rand_r(&URCU_TLS(rand_lookup));
300 end += (uint64_t) rand_r(&URCU_TLS(rand_lookup)) << 32ULL;
301 end = (end % write_pool_size) + write_pool_offset;
302
91233b5b
MD
303 start *= key_mul;
304 end *= key_mul;
305 if (start > end) {
306 tmp = start;
307 start = end;
308 end = tmp;
309 }
22789e8f
MD
310 if (end - start > range_max_len) {
311 end = start + range_max_len;
312 }
91233b5b 313 rcu_read_lock();
cd7eb7a3
MD
314 ret = cds_ja_range_add(test_ja, start, end, NULL);
315 if (ret) {
316 if (ret == -EEXIST) {
317 URCU_TLS(nr_addexist)++;
318 } else {
319 assert(0);
320 }
91233b5b
MD
321 } else {
322 URCU_TLS(nr_add)++;
323 }
324 rcu_read_unlock();
325 } else {
326 struct cds_ja_range *range;
327 uint64_t key;
328
329 /* May delete */
b10c8ba8
MD
330 key = (uint64_t) rand_r(&URCU_TLS(rand_lookup));
331 key += (uint64_t) rand_r(&URCU_TLS(rand_lookup)) << 32ULL;
332 key = (key % write_pool_size) + write_pool_offset;
91233b5b
MD
333 key *= key_mul;
334
335 rcu_read_lock();
336
337 range = cds_ja_range_lookup(test_ja, key);
338 if (range) {
339 ret = cds_ja_range_del(test_ja, range);
340 if (!ret) {
341 URCU_TLS(nr_del)++;
342 } else {
343 URCU_TLS(nr_delnoent)++;
344 }
345 } else {
346 URCU_TLS(nr_delnoent)++;
347 }
348 rcu_read_unlock();
349 }
350
351 URCU_TLS(nr_writes)++;
352 if (caa_unlikely(!test_duration_write()))
353 break;
354 if (caa_unlikely(wdelay))
355 loop_sleep(wdelay);
356 if (caa_unlikely((URCU_TLS(nr_writes) & ((1 << 10) - 1)) == 0))
357 rcu_quiescent_state();
358 }
359
360 rcu_unregister_thread();
361
5bcf8326
MD
362 printf_verbose("thread_end %s, tid %lu\n",
363 "writer", urcu_get_thread_id());
364 printf_verbose("info tid %lu: nr_add %lu, nr_addexist %lu, nr_del %lu, "
365 "nr_delnoent %lu\n", urcu_get_thread_id(),
366 URCU_TLS(nr_add),
91233b5b
MD
367 URCU_TLS(nr_addexist), URCU_TLS(nr_del),
368 URCU_TLS(nr_delnoent));
369 count->update_ops = URCU_TLS(nr_writes);
370 count->add = URCU_TLS(nr_add);
371 count->add_exist = URCU_TLS(nr_addexist);
372 count->remove = URCU_TLS(nr_del);
373 return ((void*)2);
374}
375
376static
377int do_mt_populate_ja(void)
378{
379 uint64_t iter;
380 int ret;
381
382 if (!init_populate)
383 return 0;
384
385 printf("Starting rw test\n");
386
387 for (iter = init_pool_offset; iter < init_pool_offset + init_pool_size; iter++) {
388 struct cds_ja_range *range;
389 uint64_t key;
390
391 /* note: only inserting ulong keys */
392 key = (unsigned long) iter;
393 key *= key_mul;
394 rcu_read_lock();
cd7eb7a3 395 ret = cds_ja_range_add(test_ja, key, key, NULL);
91233b5b
MD
396 URCU_TLS(nr_add)++;
397 URCU_TLS(nr_writes)++;
398 rcu_read_unlock();
012ca80d
MD
399 /* Hash table resize only occurs in call_rcu thread */
400 if (!(iter % 100))
401 rcu_quiescent_state();
cd7eb7a3
MD
402 if (ret) {
403 fprintf(stderr, "Error (%d) adding range %" PRIu64 "\n",
404 ret, key);
91233b5b
MD
405 assert(0);
406 }
407 }
408 return 0;
409}
410
411static
412int do_mt_test(void)
413{
414 pthread_t *tid_reader, *tid_writer;
415 void *tret;
416 int ret, i, err;
417 unsigned long long *count_reader;
418 struct wr_count *count_writer;
419 unsigned long long tot_reads = 0, tot_writes = 0,
420 tot_add = 0, tot_add_exist = 0, tot_remove = 0;
421 unsigned int remain;
422
423 tid_reader = malloc(sizeof(*tid_reader) * nr_readers);
424 tid_writer = malloc(sizeof(*tid_writer) * nr_writers);
425 count_reader = malloc(sizeof(*count_reader) * nr_readers);
426 count_writer = malloc(sizeof(*count_writer) * nr_writers);
427
63d2de6a
MD
428 printf("Allocating %u-bit Judy Array for ranges\n",
429 key_bits);
430 test_ja = cds_ja_range_new(key_bits);
91233b5b
MD
431 if (!test_ja) {
432 printf("Error allocating judy array.\n");
433 ret = -1;
434 goto end;
435 }
436
437 do_mt_populate_ja();
438
439 next_aff = 0;
440
441 for (i = 0; i < nr_readers; i++) {
442 err = pthread_create(&tid_reader[i],
443 NULL, test_ja_rw_thr_reader,
444 &count_reader[i]);
445 if (err != 0)
446 exit(1);
447 }
448 for (i = 0; i < nr_writers; i++) {
449 err = pthread_create(&tid_writer[i],
450 NULL, test_ja_rw_thr_writer,
451 &count_writer[i]);
452 if (err != 0)
453 exit(1);
454 }
455
456 cmm_smp_mb();
457
458 test_go = 1;
459
460 rcu_thread_offline_qsbr();
461
462 remain = duration;
463 do {
464 remain = sleep(remain);
465 } while (remain > 0);
466
467 test_stop = 1;
468
469 for (i = 0; i < nr_readers; i++) {
470 err = pthread_join(tid_reader[i], &tret);
471 if (err != 0)
472 exit(1);
473 tot_reads += count_reader[i];
474 }
475 for (i = 0; i < nr_writers; i++) {
476 err = pthread_join(tid_writer[i], &tret);
477 if (err != 0)
478 exit(1);
479 tot_writes += count_writer[i].update_ops;
480 tot_add += count_writer[i].add;
481 tot_add_exist += count_writer[i].add_exist;
482 tot_remove += count_writer[i].remove;
483 }
484 rcu_thread_online_qsbr();
485
9a655aa8
MD
486 ret = cds_ja_range_validate(test_ja);
487 assert(!ret);
488
91233b5b
MD
489 ret = cds_ja_range_destroy(test_ja, NULL);
490 if (ret) {
491 fprintf(stderr, "Error destroying judy array\n");
492 goto end;
493 }
494
495 free(tid_reader);
496 free(tid_writer);
497 free(count_reader);
498 free(count_writer);
499 ret = 0;
500end:
501 return ret;
502}
503
504int main(int argc, char **argv)
505{
506 int i, j, a, ret, err;
507 uint64_t key;
508 struct sigaction act;
509
510 if (argc < 4) {
511 show_usage(argc, argv);
512 return -1;
513 }
514
515 err = sscanf(argv[1], "%u", &nr_readers);
516 if (err != 1) {
517 show_usage(argc, argv);
518 return -1;
519 }
520
521 err = sscanf(argv[2], "%u", &nr_writers);
522 if (err != 1) {
523 show_usage(argc, argv);
524 return -1;
525 }
526
527 err = sscanf(argv[3], "%lu", &duration);
528 if (err != 1) {
529 show_usage(argc, argv);
530 return -1;
531 }
532
533 for (i = 4; i < argc; i++) {
534 if (argv[i][0] != '-')
535 continue;
536 switch (argv[i][1]) {
537#ifdef DEBUG_YIELD
538 case 'r':
539 yield_active |= YIELD_READ;
540 break;
541 case 'w':
542 yield_active |= YIELD_WRITE;
543 break;
544#endif
545 case 'a':
546 if (argc < i + 2) {
547 show_usage(argc, argv);
548 return -1;
549 }
550 a = atoi(argv[++i]);
551 cpu_affinities[next_aff++] = a;
552 use_affinity = 1;
553 printf_verbose("Adding CPU %d affinity\n", a);
554 break;
555 case 'c':
556 if (argc < i + 2) {
557 show_usage(argc, argv);
558 return -1;
559 }
560 rduration = atol(argv[++i]);
561 break;
562 case 'd':
563 if (argc < i + 2) {
564 show_usage(argc, argv);
565 return -1;
566 }
567 wdelay = atol(argv[++i]);
568 break;
569 case 'v':
570 verbose_mode = 1;
571 break;
572 case 'r':
573 add_ratio = atoi(argv[++i]);
574 break;
575 case 'k':
576 init_populate = 1;
577 break;
578 case 'R':
579 lookup_pool_offset = atol(argv[++i]);
580 break;
581 case 'S':
582 write_pool_offset = atol(argv[++i]);
583 break;
584 case 'T':
585 init_pool_offset = atol(argv[++i]);
586 break;
587 case 'M':
588 lookup_pool_size = atol(argv[++i]);
589 break;
590 case 'N':
591 write_pool_size = atol(argv[++i]);
592 break;
593 case 'O':
594 init_pool_size = atol(argv[++i]);
595 break;
596 case 'V':
597 validate_lookup = 1;
598 break;
599 case 't':
600 sanity_test = 1;
601 break;
602 case 'B':
603 key_bits = atol(argv[++i]);
604 break;
605 case 'm':
606 key_mul = atoll(argv[++i]);
607 break;
608 case 'u':
609 add_unique = 1;
610 break;
611 case 's':
612 add_replace = 1;
613 break;
614 case 'l':
615 leak_detection = 1;
616 break;
22789e8f
MD
617 case 'L':
618 range_max_len = atol(argv[++i]);
619 break;
91233b5b
MD
620 }
621 }
622
623 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
624 duration, nr_readers, nr_writers);
625 printf_verbose("Writer delay : %lu loops.\n", wdelay);
626 printf_verbose("Reader duration : %lu loops.\n", rduration);
627 printf_verbose("Add ratio: %u%%.\n", add_ratio);
628 printf_verbose("Mode:%s%s.\n",
629 " add/remove",
630 add_unique ? " uniquify" : ( add_replace ? " replace" : " insert"));
631 printf_verbose("Key multiplication factor: %" PRIu64 ".\n", key_mul);
632 printf_verbose("Init pool size offset %lu size %lu.\n",
633 init_pool_offset, init_pool_size);
634 printf_verbose("Lookup pool size offset %lu size %lu.\n",
635 lookup_pool_offset, lookup_pool_size);
636 printf_verbose("Update pool size offset %lu size %lu.\n",
637 write_pool_offset, write_pool_size);
22789e8f
MD
638 printf_verbose("Range max len: %lu.\n",
639 range_max_len);
91233b5b
MD
640 if (validate_lookup)
641 printf_verbose("Validating lookups.\n");
642 if (leak_detection)
643 printf_verbose("Memory leak dection activated.\n");
5bcf8326
MD
644 printf_verbose("thread %-6s, tid %lu\n",
645 "main", urcu_get_thread_id());
91233b5b
MD
646
647 memset(&act, 0, sizeof(act));
648 ret = sigemptyset(&act.sa_mask);
649 if (ret == -1) {
650 perror("sigemptyset");
651 return -1;
652 }
653 act.sa_handler = test_ja_rw_sigusr1_handler;
654 act.sa_flags = SA_RESTART;
655 ret = sigaction(SIGUSR1, &act, NULL);
656 if (ret == -1) {
657 perror("sigaction");
658 return -1;
659 }
660
661 err = create_all_cpu_call_rcu_data(0);
662 if (err) {
663 printf("Per-CPU call_rcu() worker threads unavailable. Using default global worker thread.\n");
664 }
665
666 rcu_register_thread();
667
668 ret = do_mt_test();
669
670 /* Wait for in-flight call_rcu free to complete for leak detection */
671 rcu_barrier();
672
673 rcu_unregister_thread();
674 free_all_cpu_call_rcu_data();
675
676 if (ret) {
677 printf("Test ended with error: %d\n", ret);
678 }
679 return ret;
680}
This page took 0.052534 seconds and 4 git commands to generate.