rculfhash tests: modularize tests
[urcu.git] / tests / test_urcu_hash.c
CommitLineData
ab7d5fc6 1/*
cd1ae16a 2 * test_urcu_hash.c
ab7d5fc6
MD
3 *
4 * Userspace RCU library - test program
5 *
18ca7a5b 6 * Copyright 2009-2012 - Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
ab7d5fc6
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
18ca7a5b 24#include "test_urcu_hash.h"
ab7d5fc6 25
18ca7a5b
MD
26unsigned int __thread rand_lookup;
27unsigned long __thread nr_add;
28unsigned long __thread nr_addexist;
29unsigned long __thread nr_del;
30unsigned long __thread nr_delnoent;
31unsigned long __thread lookup_fail;
32unsigned long __thread lookup_ok;
b8e1907c 33
18ca7a5b 34struct cds_lfht *test_ht;
5e28c532 35
18ca7a5b 36volatile int test_go, test_stop;
ab7d5fc6 37
18ca7a5b 38unsigned long wdelay;
ab7d5fc6 39
18ca7a5b 40unsigned long duration;
ab7d5fc6
MD
41
42/* read-side C.S. duration, in loops */
18ca7a5b
MD
43unsigned long rduration;
44
45unsigned long init_hash_size = DEFAULT_HASH_SIZE;
46unsigned long min_hash_alloc_size = DEFAULT_MIN_ALLOC_SIZE;
47unsigned long max_hash_buckets_size = (1UL << 20);
48unsigned long init_populate;
49int opt_auto_resize;
50int add_only, add_unique, add_replace;
51const struct cds_lfht_mm_type *memory_backend;
52
53unsigned long init_pool_offset, lookup_pool_offset, write_pool_offset;
54unsigned long init_pool_size = DEFAULT_RAND_POOL,
d837911d
MD
55 lookup_pool_size = DEFAULT_RAND_POOL,
56 write_pool_size = DEFAULT_RAND_POOL;
18ca7a5b 57int validate_lookup;
8008a032 58
18ca7a5b 59int count_pipe[2];
7ed7682f 60
18ca7a5b 61int verbose_mode;
ab7d5fc6 62
18ca7a5b
MD
63unsigned int cpu_affinities[NR_CPUS];
64unsigned int next_aff = 0;
65int use_affinity = 0;
ab7d5fc6 66
18ca7a5b 67pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
ab7d5fc6 68
18ca7a5b
MD
69unsigned long long __thread nr_writes;
70unsigned long long __thread nr_reads;
ab7d5fc6 71
18ca7a5b
MD
72unsigned int nr_readers;
73unsigned int nr_writers;
ab7d5fc6 74
18ca7a5b 75static pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
fb6d173d 76
18ca7a5b 77void set_affinity(void)
ab7d5fc6
MD
78{
79 cpu_set_t mask;
80 int cpu;
81 int ret;
82
83 if (!use_affinity)
84 return;
85
fb6d173d 86#if HAVE_SCHED_SETAFFINITY
ab7d5fc6
MD
87 ret = pthread_mutex_lock(&affinity_mutex);
88 if (ret) {
89 perror("Error in pthread mutex lock");
90 exit(-1);
91 }
92 cpu = cpu_affinities[next_aff++];
93 ret = pthread_mutex_unlock(&affinity_mutex);
94 if (ret) {
95 perror("Error in pthread mutex unlock");
96 exit(-1);
97 }
98 CPU_ZERO(&mask);
99 CPU_SET(cpu, &mask);
fb6d173d
MD
100#if SCHED_SETAFFINITY_ARGS == 2
101 sched_setaffinity(0, &mask);
102#else
103 sched_setaffinity(0, sizeof(mask), &mask);
104#endif
105#endif /* HAVE_SCHED_SETAFFINITY */
ab7d5fc6
MD
106}
107
ab7d5fc6
MD
108void rcu_copy_mutex_lock(void)
109{
110 int ret;
111 ret = pthread_mutex_lock(&rcu_copy_mutex);
112 if (ret) {
113 perror("Error in pthread mutex lock");
114 exit(-1);
115 }
116}
117
118void rcu_copy_mutex_unlock(void)
119{
120 int ret;
121
122 ret = pthread_mutex_unlock(&rcu_copy_mutex);
123 if (ret) {
124 perror("Error in pthread mutex unlock");
125 exit(-1);
126 }
127}
128
996ff57c
MD
129unsigned long test_compare(const void *key1, size_t key1_len,
130 const void *key2, size_t key2_len)
732ad076 131{
8ed51e04 132 if (caa_unlikely(key1_len != key2_len))
732ad076
MD
133 return -1;
134 assert(key1_len == sizeof(unsigned long));
135 if (key1 == key2)
136 return 0;
137 else
138 return 1;
abc490a1 139}
ab7d5fc6 140
7ed7682f
MD
141void *thr_count(void *arg)
142{
143 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
144 "counter", pthread_self(), (unsigned long)gettid());
145
146 rcu_register_thread();
147
148 for (;;) {
caf3653d 149 unsigned long count;
d933dd0e 150 long approx_before, approx_after;
7ed7682f
MD
151 ssize_t len;
152 char buf[1];
153
154 rcu_thread_offline();
155 len = read(count_pipe[0], buf, 1);
156 rcu_thread_online();
8ed51e04 157 if (caa_unlikely(!test_duration_read()))
7ed7682f
MD
158 break;
159 if (len != 1)
160 continue;
161 /* Accounting */
162 printf("Counting nodes... ");
163 fflush(stdout);
164 rcu_read_lock();
caf3653d 165 cds_lfht_count_nodes(test_ht, &approx_before, &count,
7ed7682f
MD
166 &approx_after);
167 rcu_read_unlock();
168 printf("done.\n");
d933dd0e 169 printf("Approximation before node accounting: %ld nodes.\n",
7ed7682f
MD
170 approx_before);
171 printf("Accounting of nodes in the hash table: "
caf3653d
MD
172 "%lu nodes.\n",
173 count);
d933dd0e 174 printf("Approximation after node accounting: %ld nodes.\n",
7ed7682f
MD
175 approx_after);
176 }
177 rcu_unregister_thread();
178 return NULL;
179}
180
abc490a1
MD
181void free_node_cb(struct rcu_head *head)
182{
3c692076 183 struct lfht_test_node *node =
81d91005 184 caa_container_of(head, struct lfht_test_node, head);
abc490a1
MD
185 free(node);
186}
187
175ec0eb
MD
188static
189void test_delete_all_nodes(struct cds_lfht *ht)
190{
191 struct cds_lfht_iter iter;
3c692076 192 struct lfht_test_node *node;
175ec0eb
MD
193 unsigned long count = 0;
194
6d320126 195 cds_lfht_for_each_entry(ht, &iter, node, node) {
175ec0eb
MD
196 int ret;
197
bc8c3c74 198 ret = cds_lfht_del(test_ht, cds_lfht_iter_get_node(&iter));
175ec0eb 199 assert(!ret);
81d91005 200 call_rcu(&node->head, free_node_cb);
175ec0eb
MD
201 count++;
202 }
203 printf("deleted %lu nodes.\n", count);
204}
205
ab7d5fc6
MD
206void show_usage(int argc, char **argv)
207{
48ed1c18 208 printf("Usage : %s nr_readers nr_writers duration (s)\n", argv[0]);
ab7d5fc6 209#ifdef DEBUG_YIELD
48ed1c18 210 printf(" [-r] [-w] (yield reader and/or writer)\n");
ab7d5fc6 211#endif
48ed1c18
MD
212 printf(" [-d delay] (writer period (us))\n");
213 printf(" [-c duration] (reader C.S. duration (in loops))\n");
214 printf(" [-v] (verbose output)\n");
215 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
b99436d7
LJ
216 printf(" [-h size] (initial number of buckets)\n");
217 printf(" [-m size] (minimum number of allocated buckets)\n");
218 printf(" [-n size] (maximum number of buckets)\n");
18ca7a5b 219printf(" [not -u nor -s] Add entries (supports redundant keys).\n");
48ed1c18
MD
220 printf(" [-u] Uniquify add (no redundant keys).\n");
221 printf(" [-s] Replace (swap) entries.\n");
222 printf(" [-i] Add only (no removal).\n");
223 printf(" [-k nr_nodes] Number of nodes to insert initially.\n");
224 printf(" [-A] Automatically resize hash table.\n");
c0b8a865 225 printf(" [-B order|chunk|mmap] Specify the memory backend.\n");
48ed1c18
MD
226 printf(" [-R offset] Lookup pool offset.\n");
227 printf(" [-S offset] Write pool offset.\n");
228 printf(" [-T offset] Init pool offset.\n");
229 printf(" [-M size] Lookup pool size.\n");
230 printf(" [-N size] Write pool size.\n");
231 printf(" [-O size] Init pool size.\n");
232 printf(" [-V] Validate lookups of init values (use with filled init pool, same lookup range, with different write range).\n");
233 printf("\n\n");
ab7d5fc6
MD
234}
235
236int main(int argc, char **argv)
237{
238 int err;
239 pthread_t *tid_reader, *tid_writer;
7ed7682f 240 pthread_t tid_count;
ab7d5fc6 241 void *tret;
b8e1907c
MD
242 unsigned long long *count_reader;
243 struct wr_count *count_writer;
244 unsigned long long tot_reads = 0, tot_writes = 0,
3c16bf4b 245 tot_add = 0, tot_add_exist = 0, tot_remove = 0;
caf3653d 246 unsigned long count;
d933dd0e 247 long approx_before, approx_after;
5e28c532 248 int i, a, ret;
3967a8a8
MD
249 struct sigaction act;
250 unsigned int remain;
ab7d5fc6
MD
251
252 if (argc < 4) {
253 show_usage(argc, argv);
254 return -1;
255 }
256
257 err = sscanf(argv[1], "%u", &nr_readers);
258 if (err != 1) {
259 show_usage(argc, argv);
260 return -1;
261 }
262
263 err = sscanf(argv[2], "%u", &nr_writers);
264 if (err != 1) {
265 show_usage(argc, argv);
266 return -1;
267 }
268
269 err = sscanf(argv[3], "%lu", &duration);
270 if (err != 1) {
271 show_usage(argc, argv);
272 return -1;
273 }
274
275 for (i = 4; i < argc; i++) {
276 if (argv[i][0] != '-')
277 continue;
278 switch (argv[i][1]) {
279#ifdef DEBUG_YIELD
280 case 'r':
281 yield_active |= YIELD_READ;
282 break;
283 case 'w':
284 yield_active |= YIELD_WRITE;
285 break;
286#endif
287 case 'a':
288 if (argc < i + 2) {
289 show_usage(argc, argv);
290 return -1;
291 }
292 a = atoi(argv[++i]);
293 cpu_affinities[next_aff++] = a;
294 use_affinity = 1;
295 printf_verbose("Adding CPU %d affinity\n", a);
296 break;
297 case 'c':
298 if (argc < i + 2) {
299 show_usage(argc, argv);
300 return -1;
301 }
302 rduration = atol(argv[++i]);
303 break;
304 case 'd':
305 if (argc < i + 2) {
306 show_usage(argc, argv);
307 return -1;
308 }
309 wdelay = atol(argv[++i]);
310 break;
311 case 'v':
312 verbose_mode = 1;
313 break;
6d5c0ca9
MD
314 case 'h':
315 if (argc < i + 2) {
316 show_usage(argc, argv);
317 return -1;
318 }
319 init_hash_size = atol(argv[++i]);
320 break;
32becef6
LJ
321 case 'm':
322 if (argc < i + 2) {
323 show_usage(argc, argv);
324 return -1;
325 }
326 min_hash_alloc_size = atol(argv[++i]);
327 break;
b99436d7
LJ
328 case 'n':
329 if (argc < i + 2) {
330 show_usage(argc, argv);
331 return -1;
332 }
333 max_hash_buckets_size = atol(argv[++i]);
334 break;
6d5c0ca9 335 case 'u':
48ed1c18
MD
336 if (add_replace) {
337 printf("Please specify at most one of -s or -u.\n");
338 exit(-1);
339 }
6d5c0ca9
MD
340 add_unique = 1;
341 break;
48ed1c18
MD
342 case 's':
343 if (add_unique) {
344 printf("Please specify at most one of -s or -u.\n");
345 exit(-1);
346 }
347 add_replace = 1;
348 break;
6d5c0ca9
MD
349 case 'i':
350 add_only = 1;
351 break;
cd1ae16a
MD
352 case 'k':
353 init_populate = atol(argv[++i]);
354 break;
151e7a93
MD
355 case 'A':
356 opt_auto_resize = 1;
357 break;
c0b8a865
LJ
358 case 'B':
359 if (argc < i + 2) {
360 show_usage(argc, argv);
361 return -1;
362 }
363 i++;
364 if (!strcmp("order", argv[i]))
365 memory_backend = &cds_lfht_mm_order;
366 else if (!strcmp("chunk", argv[i]))
367 memory_backend = &cds_lfht_mm_chunk;
368 else if (!strcmp("mmap", argv[i]))
369 memory_backend = &cds_lfht_mm_mmap;
370 else {
371 printf("Please specify memory backend with order|chunk|mmap.\n");
372 exit(-1);
373 }
374 break;
8008a032
MD
375 case 'R':
376 lookup_pool_offset = atol(argv[++i]);
377 break;
378 case 'S':
379 write_pool_offset = atol(argv[++i]);
380 break;
381 case 'T':
382 init_pool_offset = atol(argv[++i]);
383 break;
d837911d
MD
384 case 'M':
385 lookup_pool_size = atol(argv[++i]);
386 break;
387 case 'N':
388 write_pool_size = atol(argv[++i]);
389 break;
390 case 'O':
391 init_pool_size = atol(argv[++i]);
392 break;
59b10634
MD
393 case 'V':
394 validate_lookup = 1;
395 break;
8008a032 396
ab7d5fc6
MD
397 }
398 }
399
6d5c0ca9
MD
400 /* Check if hash size is power of 2 */
401 if (init_hash_size && init_hash_size & (init_hash_size - 1)) {
b99436d7 402 printf("Error: Initial number of buckets (%lu) is not a power of 2.\n",
6d5c0ca9
MD
403 init_hash_size);
404 return -1;
405 }
406
d0d8f9aa 407 if (min_hash_alloc_size && min_hash_alloc_size & (min_hash_alloc_size - 1)) {
b99436d7 408 printf("Error: Minimum number of allocated buckets (%lu) is not a power of 2.\n",
32becef6
LJ
409 min_hash_alloc_size);
410 return -1;
411 }
412
b99436d7
LJ
413 if (max_hash_buckets_size && max_hash_buckets_size & (max_hash_buckets_size - 1)) {
414 printf("Error: Maximum number of buckets (%lu) is not a power of 2.\n",
415 max_hash_buckets_size);
416 return -1;
417 }
418
3967a8a8
MD
419 memset(&act, 0, sizeof(act));
420 ret = sigemptyset(&act.sa_mask);
421 if (ret == -1) {
422 perror("sigemptyset");
423 return -1;
424 }
18ca7a5b 425 act.sa_handler = test_hash_rw_sigusr1_handler;
3967a8a8
MD
426 act.sa_flags = SA_RESTART;
427 ret = sigaction(SIGUSR1, &act, NULL);
428 if (ret == -1) {
429 perror("sigaction");
430 return -1;
431 }
7ed7682f
MD
432
433 ret = pipe(count_pipe);
434 if (ret == -1) {
435 perror("pipe");
436 return -1;
437 }
438
439 /* spawn counter thread */
440 err = pthread_create(&tid_count, NULL, thr_count,
441 NULL);
442 if (err != 0)
443 exit(1);
444
18ca7a5b 445 act.sa_handler = test_hash_rw_sigusr2_handler;
973e5e1b
MD
446 act.sa_flags = SA_RESTART;
447 ret = sigaction(SIGUSR2, &act, NULL);
448 if (ret == -1) {
449 perror("sigaction");
450 return -1;
451 }
3967a8a8 452
ab7d5fc6
MD
453 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
454 duration, nr_readers, nr_writers);
455 printf_verbose("Writer delay : %lu loops.\n", wdelay);
456 printf_verbose("Reader duration : %lu loops.\n", rduration);
6d5c0ca9
MD
457 printf_verbose("Mode:%s%s.\n",
458 add_only ? " add only" : " add/remove",
48ed1c18 459 add_unique ? " uniquify" : ( add_replace ? " replace" : " insert"));
b99436d7
LJ
460 printf_verbose("Initial number of buckets: %lu buckets.\n", init_hash_size);
461 printf_verbose("Minimum number of allocated buckets: %lu buckets.\n", min_hash_alloc_size);
462 printf_verbose("Maximum number of buckets: %lu buckets.\n", max_hash_buckets_size);
5dc00396
MD
463 printf_verbose("Init pool size offset %lu size %lu.\n",
464 init_pool_offset, init_pool_size);
465 printf_verbose("Lookup pool size offset %lu size %lu.\n",
466 lookup_pool_offset, lookup_pool_size);
467 printf_verbose("Update pool size offset %lu size %lu.\n",
468 write_pool_offset, write_pool_size);
ab7d5fc6
MD
469 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
470 "main", pthread_self(), (unsigned long)gettid());
471
ab7d5fc6
MD
472 tid_reader = malloc(sizeof(*tid_reader) * nr_readers);
473 tid_writer = malloc(sizeof(*tid_writer) * nr_writers);
474 count_reader = malloc(sizeof(*count_reader) * nr_readers);
475 count_writer = malloc(sizeof(*count_writer) * nr_writers);
48ed1c18
MD
476
477 err = create_all_cpu_call_rcu_data(0);
8bcbd94a
MD
478 if (err) {
479 printf("Per-CPU call_rcu() worker threads unavailable. Using default global worker thread.\n");
480 }
48ed1c18 481
c0b8a865
LJ
482 if (memory_backend) {
483 test_ht = _cds_lfht_new(init_hash_size, min_hash_alloc_size,
484 max_hash_buckets_size,
485 (opt_auto_resize ? CDS_LFHT_AUTO_RESIZE : 0) |
486 CDS_LFHT_ACCOUNTING, memory_backend,
487 &rcu_flavor, NULL);
488 } else {
489 test_ht = cds_lfht_new(init_hash_size, min_hash_alloc_size,
490 max_hash_buckets_size,
491 (opt_auto_resize ? CDS_LFHT_AUTO_RESIZE : 0) |
492 CDS_LFHT_ACCOUNTING, NULL);
493 }
494
48ed1c18 495 /*
c0b8a865 496 * Hash Population needs to be seen as a RCU reader
48ed1c18
MD
497 * thread from the point of view of resize.
498 */
499 rcu_register_thread();
18ca7a5b 500 ret = test_hash_rw_populate_hash();
5dc45f25 501 assert(!ret);
59e371e3
MD
502
503 rcu_thread_offline();
14756542 504
ab7d5fc6
MD
505 next_aff = 0;
506
507 for (i = 0; i < nr_readers; i++) {
18ca7a5b
MD
508 err = pthread_create(&tid_reader[i],
509 NULL, test_hash_rw_thr_reader,
ab7d5fc6
MD
510 &count_reader[i]);
511 if (err != 0)
512 exit(1);
513 }
514 for (i = 0; i < nr_writers; i++) {
18ca7a5b
MD
515 err = pthread_create(&tid_writer[i],
516 NULL, test_hash_rw_thr_writer,
ab7d5fc6
MD
517 &count_writer[i]);
518 if (err != 0)
519 exit(1);
520 }
521
abc490a1 522 cmm_smp_mb();
ab7d5fc6
MD
523
524 test_go = 1;
525
3967a8a8
MD
526 remain = duration;
527 do {
528 remain = sleep(remain);
529 } while (remain > 0);
ab7d5fc6
MD
530
531 test_stop = 1;
532
533 for (i = 0; i < nr_readers; i++) {
534 err = pthread_join(tid_reader[i], &tret);
535 if (err != 0)
536 exit(1);
537 tot_reads += count_reader[i];
538 }
539 for (i = 0; i < nr_writers; i++) {
540 err = pthread_join(tid_writer[i], &tret);
541 if (err != 0)
542 exit(1);
b8e1907c
MD
543 tot_writes += count_writer[i].update_ops;
544 tot_add += count_writer[i].add;
3c16bf4b 545 tot_add_exist += count_writer[i].add_exist;
b8e1907c 546 tot_remove += count_writer[i].remove;
ab7d5fc6 547 }
7ed7682f
MD
548
549 /* teardown counter thread */
550 act.sa_handler = SIG_IGN;
551 act.sa_flags = SA_RESTART;
552 ret = sigaction(SIGUSR2, &act, NULL);
553 if (ret == -1) {
554 perror("sigaction");
555 return -1;
556 }
557 {
558 char msg[1] = { 0x42 };
3fb1173c
MD
559 ssize_t ret;
560
561 do {
562 ret = write(count_pipe[1], msg, 1); /* wakeup thread */
563 } while (ret == -1L && errno == EINTR);
7ed7682f
MD
564 }
565 err = pthread_join(tid_count, &tret);
566 if (err != 0)
567 exit(1);
568
33c7c748 569 fflush(stdout);
59e371e3
MD
570 rcu_thread_online();
571 rcu_read_lock();
175ec0eb 572 printf("Counting nodes... ");
caf3653d 573 cds_lfht_count_nodes(test_ht, &approx_before, &count, &approx_after);
175ec0eb
MD
574 printf("done.\n");
575 test_delete_all_nodes(test_ht);
59e371e3
MD
576 rcu_read_unlock();
577 rcu_thread_offline();
caf3653d 578 if (count) {
d933dd0e 579 printf("Approximation before node accounting: %ld nodes.\n",
973e5e1b 580 approx_before);
175ec0eb 581 printf("Nodes deleted from hash table before destroy: "
caf3653d
MD
582 "%lu nodes.\n",
583 count);
d933dd0e 584 printf("Approximation after node accounting: %ld nodes.\n",
973e5e1b
MD
585 approx_after);
586 }
b7d619b0 587 ret = cds_lfht_destroy(test_ht, NULL);
33c7c748
MD
588 if (ret)
589 printf_verbose("final delete aborted\n");
590 else
591 printf_verbose("final delete success\n");
ab7d5fc6
MD
592 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
593 tot_writes);
594 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu "
595 "nr_writers %3u "
d837911d 596 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu "
cd1ae16a 597 "nr_add %12llu nr_add_fail %12llu nr_remove %12llu nr_leaked %12lld\n",
ab7d5fc6 598 argv[0], duration, nr_readers, rduration,
d837911d 599 nr_writers, wdelay, tot_reads, tot_writes,
3c16bf4b 600 tot_reads + tot_writes, tot_add, tot_add_exist, tot_remove,
cd1ae16a 601 (long long) tot_add + init_populate - tot_remove - count);
59e371e3 602 rcu_unregister_thread();
3a22f1dd 603 free_all_cpu_call_rcu_data();
ab7d5fc6
MD
604 free(tid_reader);
605 free(tid_writer);
606 free(count_reader);
607 free(count_writer);
608 return 0;
609}
This page took 0.055985 seconds and 4 git commands to generate.