qsbr vs call_rcu : remove exit assertion
[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 *
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
24#include <stdio.h>
25#include <pthread.h>
26#include <stdlib.h>
27#include <string.h>
28#include <sys/types.h>
29#include <sys/wait.h>
30#include <unistd.h>
31#include <stdio.h>
32#include <assert.h>
ab7d5fc6 33#include <sched.h>
5e28c532 34#include <errno.h>
ab7d5fc6 35
abc490a1
MD
36#ifdef __linux__
37#include <syscall.h>
38#endif
ab7d5fc6 39
6d5c0ca9
MD
40#define DEFAULT_HASH_SIZE 32
41#define DEFAULT_RAND_POOL 1000000
5e28c532 42
ab7d5fc6
MD
43/* Make this big enough to include the POWER5+ L3 cacheline size of 256B */
44#define CACHE_LINE_SIZE 4096
45
46/* hardcoded number of CPUs */
47#define NR_CPUS 16384
48
98808fb1
MD
49#ifdef POISON_FREE
50#define poison_free(ptr) \
51 do { \
52 memset(ptr, 0x42, sizeof(*(ptr))); \
53 free(ptr); \
54 } while (0)
55#else
56#define poison_free(ptr) free(ptr)
57#endif
58
59
60
ab7d5fc6
MD
61#if defined(_syscall0)
62_syscall0(pid_t, gettid)
63#elif defined(__NR_gettid)
64static inline pid_t gettid(void)
65{
66 return syscall(__NR_gettid);
67}
68#else
69#warning "use pid as tid"
70static inline pid_t gettid(void)
71{
72 return getpid();
73}
74#endif
75
76#ifndef DYNAMIC_LINK_TEST
77#define _LGPL_SOURCE
78#else
79#define debug_yield_read()
80#endif
5567839e 81#include <urcu-qsbr.h>
abc490a1
MD
82#include <urcu/rculfhash.h>
83#include <urcu-call-rcu.h>
ab7d5fc6 84
b8e1907c
MD
85struct wr_count {
86 unsigned long update_ops;
87 unsigned long add;
3c16bf4b 88 unsigned long add_exist;
b8e1907c
MD
89 unsigned long remove;
90};
91
5e28c532
MD
92static unsigned int __thread rand_lookup;
93static unsigned long __thread nr_add;
94static unsigned long __thread nr_addexist;
95static unsigned long __thread nr_del;
96static unsigned long __thread nr_delnoent;
97static unsigned long __thread lookup_fail;
98static unsigned long __thread lookup_ok;
99
14044b37 100static struct cds_lfht *test_ht;
ab7d5fc6 101
5e28c532 102struct test_data {
ab7d5fc6 103 int a;
5e28c532 104 int b;
ab7d5fc6
MD
105};
106
107static volatile int test_go, test_stop;
108
109static unsigned long wdelay;
110
ab7d5fc6
MD
111static unsigned long duration;
112
113/* read-side C.S. duration, in loops */
114static unsigned long rduration;
115
6d5c0ca9 116static unsigned long init_hash_size = DEFAULT_HASH_SIZE;
cd1ae16a 117static unsigned long init_populate;
6d5c0ca9 118static unsigned long rand_pool = DEFAULT_RAND_POOL;
151e7a93 119static int opt_auto_resize;
6d5c0ca9
MD
120static int add_only, add_unique;
121
8008a032 122static unsigned long init_pool_offset, lookup_pool_offset, write_pool_offset;
59b10634 123static int validate_lookup;
8008a032 124
ab7d5fc6
MD
125static inline void loop_sleep(unsigned long l)
126{
127 while(l-- != 0)
abc490a1 128 caa_cpu_relax();
ab7d5fc6
MD
129}
130
131static int verbose_mode;
132
133#define printf_verbose(fmt, args...) \
134 do { \
135 if (verbose_mode) \
33c7c748 136 printf(fmt, ## args); \
ab7d5fc6
MD
137 } while (0)
138
139static unsigned int cpu_affinities[NR_CPUS];
140static unsigned int next_aff = 0;
141static int use_affinity = 0;
142
143pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
144
145static void set_affinity(void)
146{
147 cpu_set_t mask;
148 int cpu;
149 int ret;
150
151 if (!use_affinity)
152 return;
153
154 ret = pthread_mutex_lock(&affinity_mutex);
155 if (ret) {
156 perror("Error in pthread mutex lock");
157 exit(-1);
158 }
159 cpu = cpu_affinities[next_aff++];
160 ret = pthread_mutex_unlock(&affinity_mutex);
161 if (ret) {
162 perror("Error in pthread mutex unlock");
163 exit(-1);
164 }
165 CPU_ZERO(&mask);
166 CPU_SET(cpu, &mask);
167 sched_setaffinity(0, sizeof(mask), &mask);
168}
169
3967a8a8
MD
170static enum {
171 AR_RANDOM = 0,
172 AR_ADD = 1,
173 AR_REMOVE = -1,
174} addremove; /* 1: add, -1 remove, 0: random */
175
176static
177void sigusr1_handler(int signo)
178{
179 switch (addremove) {
180 case AR_ADD:
181 printf("Add/Remove: random.\n");
182 addremove = AR_RANDOM;
183 break;
184 case AR_RANDOM:
185 printf("Add/Remove: remove only.\n");
186 addremove = AR_REMOVE;
187 break;
188 case AR_REMOVE:
189 printf("Add/Remove: add only.\n");
190 addremove = AR_ADD;
191 break;
192 }
193}
194
ab7d5fc6
MD
195/*
196 * returns 0 if test should end.
197 */
198static int test_duration_write(void)
199{
200 return !test_stop;
201}
202
203static int test_duration_read(void)
204{
205 return !test_stop;
206}
207
208static unsigned long long __thread nr_writes;
209static unsigned long long __thread nr_reads;
210
211static unsigned int nr_readers;
212static unsigned int nr_writers;
213
214pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
215
216void rcu_copy_mutex_lock(void)
217{
218 int ret;
219 ret = pthread_mutex_lock(&rcu_copy_mutex);
220 if (ret) {
221 perror("Error in pthread mutex lock");
222 exit(-1);
223 }
224}
225
226void rcu_copy_mutex_unlock(void)
227{
228 int ret;
229
230 ret = pthread_mutex_unlock(&rcu_copy_mutex);
231 if (ret) {
232 perror("Error in pthread mutex unlock");
233 exit(-1);
234 }
235}
236
732ad076
MD
237/*
238 * Hash function
239 * Source: http://burtleburtle.net/bob/c/lookup3.c
240 * Originally Public Domain
241 */
242
243#define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
244
245#define mix(a, b, c) \
246do { \
247 a -= c; a ^= rot(c, 4); c += b; \
248 b -= a; b ^= rot(a, 6); a += c; \
249 c -= b; c ^= rot(b, 8); b += a; \
250 a -= c; a ^= rot(c, 16); c += b; \
251 b -= a; b ^= rot(a, 19); a += c; \
252 c -= b; c ^= rot(b, 4); b += a; \
253} while (0)
254
255#define final(a, b, c) \
256{ \
257 c ^= b; c -= rot(b, 14); \
258 a ^= c; a -= rot(c, 11); \
259 b ^= a; b -= rot(a, 25); \
260 c ^= b; c -= rot(b, 16); \
261 a ^= c; a -= rot(c, 4);\
262 b ^= a; b -= rot(a, 14); \
263 c ^= b; c -= rot(b, 24); \
264}
265
266static __attribute__((unused))
267uint32_t hash_u32(
268 const uint32_t *k, /* the key, an array of uint32_t values */
269 size_t length, /* the length of the key, in uint32_ts */
270 uint32_t initval) /* the previous hash, or an arbitrary value */
271{
272 uint32_t a, b, c;
273
274 /* Set up the internal state */
275 a = b = c = 0xdeadbeef + (((uint32_t) length) << 2) + initval;
276
277 /*----------------------------------------- handle most of the key */
278 while (length > 3) {
279 a += k[0];
280 b += k[1];
281 c += k[2];
282 mix(a, b, c);
283 length -= 3;
284 k += 3;
285 }
286
287 /*----------------------------------- handle the last 3 uint32_t's */
288 switch (length) { /* all the case statements fall through */
289 case 3: c += k[2];
290 case 2: b += k[1];
291 case 1: a += k[0];
292 final(a, b, c);
293 case 0: /* case 0: nothing left to add */
294 break;
295 }
296 /*---------------------------------------------- report the result */
297 return c;
298}
299
300static
301void hashword2(
302 const uint32_t *k, /* the key, an array of uint32_t values */
303 size_t length, /* the length of the key, in uint32_ts */
304 uint32_t *pc, /* IN: seed OUT: primary hash value */
305 uint32_t *pb) /* IN: more seed OUT: secondary hash value */
306{
307 uint32_t a, b, c;
308
309 /* Set up the internal state */
310 a = b = c = 0xdeadbeef + ((uint32_t) (length << 2)) + *pc;
311 c += *pb;
312
313 /*----------------------------------------- handle most of the key */
314 while (length > 3) {
315 a += k[0];
316 b += k[1];
317 c += k[2];
318 mix(a, b, c);
319 length -= 3;
320 k += 3;
321 }
322
323 /*----------------------------------- handle the last 3 uint32_t's */
324 switch (length) { /* all the case statements fall through */
325 case 3: c += k[2];
326 case 2: b += k[1];
327 case 1: a += k[0];
328 final(a, b, c);
329 case 0: /* case 0: nothing left to add */
330 break;
331 }
332 /*---------------------------------------------- report the result */
333 *pc = c;
334 *pb = b;
335}
336
337#if (CAA_BITS_PER_LONG == 32)
abc490a1 338static
732ad076 339unsigned long test_hash(void *_key, size_t length, unsigned long seed)
abc490a1 340{
abc490a1
MD
341 unsigned long key = (unsigned long) _key;
342 unsigned long v;
343
732ad076
MD
344 assert(length == sizeof(unsigned long));
345 return hash_u32(&v, 1, seed);
346}
347#else
348static
349unsigned long test_hash(void *_key, size_t length, unsigned long seed)
350{
351 union {
352 uint64_t v64;
353 uint32_t v32[2];
354 } v;
355 union {
356 uint64_t v64;
357 uint32_t v32[2];
358 } key;
359
360 assert(length == sizeof(unsigned long));
361 v.v64 = (uint64_t) seed;
362 key.v64 = (uint64_t) _key;
363 hashword2(key.v32, 2, &v.v32[0], &v.v32[1]);
364 return v.v64;
365}
366#endif
abc490a1 367
732ad076
MD
368static
369unsigned long test_compare(void *key1, size_t key1_len,
370 void *key2, size_t key2_len)
371{
372 if (unlikely(key1_len != key2_len))
373 return -1;
374 assert(key1_len == sizeof(unsigned long));
375 if (key1 == key2)
376 return 0;
377 else
378 return 1;
abc490a1 379}
ab7d5fc6
MD
380
381void *thr_reader(void *_count)
382{
383 unsigned long long *count = _count;
14044b37 384 struct cds_lfht_node *node;
ab7d5fc6
MD
385
386 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
387 "reader", pthread_self(), (unsigned long)gettid());
388
389 set_affinity();
390
391 rcu_register_thread();
392
393 while (!test_go)
394 {
395 }
abc490a1 396 cmm_smp_mb();
ab7d5fc6
MD
397
398 for (;;) {
399 rcu_read_lock();
14044b37 400 node = cds_lfht_lookup(test_ht,
8008a032 401 (void *)(unsigned long)((rand_r(&rand_lookup) % rand_pool) + lookup_pool_offset),
732ad076 402 sizeof(void *));
59b10634
MD
403 if (node == NULL) {
404 if (validate_lookup) {
405 printf("[ERROR] Lookup cannot find initial node.\n");
406 }
5e28c532 407 lookup_fail++;
59b10634 408 } else {
5e28c532 409 lookup_ok++;
59b10634 410 }
ab7d5fc6 411 debug_yield_read();
ab7d5fc6
MD
412 if (unlikely(rduration))
413 loop_sleep(rduration);
414 rcu_read_unlock();
415 nr_reads++;
416 if (unlikely(!test_duration_read()))
417 break;
5567839e
MD
418 if (unlikely((nr_reads & ((1 << 10) - 1)) == 0))
419 rcu_quiescent_state();
ab7d5fc6
MD
420 }
421
422 rcu_unregister_thread();
423
424 *count = nr_reads;
425 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
426 "reader", pthread_self(), (unsigned long)gettid());
5e28c532
MD
427 printf_verbose("readid : %lx, lookupfail %lu, lookupok %lu\n",
428 pthread_self(), lookup_fail, lookup_ok);
ab7d5fc6
MD
429 return ((void*)1);
430
431}
432
abc490a1
MD
433static
434void free_node_cb(struct rcu_head *head)
435{
14044b37
MD
436 struct cds_lfht_node *node =
437 caa_container_of(head, struct cds_lfht_node, head);
abc490a1
MD
438 free(node);
439}
440
ab7d5fc6
MD
441void *thr_writer(void *_count)
442{
14044b37 443 struct cds_lfht_node *node, *ret_node;
b8e1907c 444 struct wr_count *count = _count;
5e28c532 445 int ret;
ab7d5fc6
MD
446
447 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
448 "writer", pthread_self(), (unsigned long)gettid());
449
450 set_affinity();
451
5e28c532 452 rcu_register_thread();
5e28c532 453
ab7d5fc6
MD
454 while (!test_go)
455 {
456 }
abc490a1 457 cmm_smp_mb();
ab7d5fc6
MD
458
459 for (;;) {
3967a8a8
MD
460 if ((addremove == AR_ADD || add_only)
461 || (addremove == AR_RANDOM && rand_r(&rand_lookup) & 1)) {
14044b37 462 node = malloc(sizeof(struct cds_lfht_node));
abc490a1 463 rcu_read_lock();
14044b37 464 cds_lfht_node_init(node,
8008a032 465 (void *)(unsigned long)((rand_r(&rand_lookup) % rand_pool) + write_pool_offset),
cb233752 466 sizeof(void *));
6d5c0ca9 467 if (add_unique)
14044b37 468 ret_node = cds_lfht_add_unique(test_ht, node);
6d5c0ca9 469 else
14044b37 470 cds_lfht_add(test_ht, node);
abc490a1 471 rcu_read_unlock();
6d5c0ca9 472 if (add_unique && ret_node != node) {
742aa1db 473 free(node);
3eca1b8c 474 nr_addexist++;
742aa1db 475 } else
3eca1b8c 476 nr_add++;
5e28c532
MD
477 } else {
478 /* May delete */
abc490a1 479 rcu_read_lock();
14044b37 480 node = cds_lfht_lookup(test_ht,
8008a032 481 (void *)(unsigned long)((rand_r(&rand_lookup) % rand_pool) + write_pool_offset),
732ad076 482 sizeof(void *));
abc490a1 483 if (node)
14044b37 484 ret = cds_lfht_remove(test_ht, node);
5e28c532 485 else
abc490a1
MD
486 ret = -ENOENT;
487 rcu_read_unlock();
488 if (ret == 0) {
489 call_rcu(&node->head, free_node_cb);
5e28c532 490 nr_del++;
abc490a1
MD
491 } else
492 nr_delnoent++;
44395fb7 493 }
abc490a1 494#if 0
44395fb7
MD
495 //if (nr_writes % 100000 == 0) {
496 if (nr_writes % 1000 == 0) {
abc490a1 497 rcu_read_lock();
44395fb7
MD
498 if (rand_r(&rand_lookup) & 1) {
499 ht_resize(test_ht, 1);
500 } else {
501 ht_resize(test_ht, -1);
502 }
abc490a1 503 rcu_read_unlock();
5e28c532 504 }
abc490a1 505#endif //0
ab7d5fc6
MD
506 nr_writes++;
507 if (unlikely(!test_duration_write()))
508 break;
509 if (unlikely(wdelay))
510 loop_sleep(wdelay);
5567839e
MD
511 if (unlikely((nr_writes & ((1 << 10) - 1)) == 0))
512 rcu_quiescent_state();
ab7d5fc6
MD
513 }
514
5e28c532
MD
515 rcu_unregister_thread();
516
ab7d5fc6
MD
517 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
518 "writer", pthread_self(), (unsigned long)gettid());
5e28c532
MD
519 printf_verbose("info id %lx: nr_add %lu, nr_addexist %lu, nr_del %lu, "
520 "nr_delnoent %lu\n", pthread_self(), nr_add,
521 nr_addexist, nr_del, nr_delnoent);
b8e1907c
MD
522 count->update_ops = nr_writes;
523 count->add = nr_add;
3c16bf4b 524 count->add_exist = nr_addexist;
b8e1907c 525 count->remove = nr_del;
ab7d5fc6
MD
526 return ((void*)2);
527}
528
5dc45f25 529static int populate_hash(void)
cd1ae16a
MD
530{
531 struct cds_lfht_node *node, *ret_node;
532
533 if (!init_populate)
5dc45f25
MD
534 return 0;
535
536 if (add_unique && init_populate * 10 > rand_pool) {
537 printf("WARNING: required to populate %lu nodes (-k), but random "
538"pool is quite small (%lu values) and we are in add_unique (-u) mode. Try with a "
59b10634 539"larger random pool (-p option). This may take a while...\n", init_populate, rand_pool);
5dc45f25 540 }
cd1ae16a
MD
541
542 while (nr_add < init_populate) {
543 node = malloc(sizeof(struct cds_lfht_node));
544 cds_lfht_node_init(node,
8008a032 545 (void *)(unsigned long)((rand_r(&rand_lookup) % rand_pool) + init_pool_offset),
cd1ae16a
MD
546 sizeof(void *));
547 if (add_unique)
548 ret_node = cds_lfht_add_unique(test_ht, node);
549 else
550 cds_lfht_add(test_ht, node);
551 if (add_unique && ret_node != node) {
552 free(node);
553 nr_addexist++;
554 } else
555 nr_add++;
556 nr_writes++;
557 }
5dc45f25 558 return 0;
cd1ae16a
MD
559}
560
ab7d5fc6
MD
561void show_usage(int argc, char **argv)
562{
563 printf("Usage : %s nr_readers nr_writers duration (s)", argv[0]);
564#ifdef DEBUG_YIELD
565 printf(" [-r] [-w] (yield reader and/or writer)");
566#endif
567 printf(" [-d delay] (writer period (us))");
568 printf(" [-c duration] (reader C.S. duration (in loops))");
569 printf(" [-v] (verbose output)");
570 printf(" [-a cpu#] [-a cpu#]... (affinity)");
6d5c0ca9
MD
571 printf(" [-p size] (random key value pool size)");
572 printf(" [-h size] (initial hash table size)");
573 printf(" [-u] Uniquify add.");
574 printf(" [-i] Add only (no removal).");
cd1ae16a 575 printf(" [-k nr_nodes] Number of nodes to insert initially.");
151e7a93 576 printf(" [-A] Automatically resize hash table.");
59b10634
MD
577 printf(" [-R offset] Lookup pool offset.");
578 printf(" [-S offset] Write pool offset.");
579 printf(" [-T offset] Init pool offset.");
580 printf(" [-V] Validate lookups of init values (use with filled init pool, same lookup range, with different write range).");
ab7d5fc6
MD
581 printf("\n");
582}
583
584int main(int argc, char **argv)
585{
586 int err;
587 pthread_t *tid_reader, *tid_writer;
588 void *tret;
b8e1907c
MD
589 unsigned long long *count_reader;
590 struct wr_count *count_writer;
591 unsigned long long tot_reads = 0, tot_writes = 0,
3c16bf4b 592 tot_add = 0, tot_add_exist = 0, tot_remove = 0;
b8e1907c 593 unsigned long count, removed;
5e28c532 594 int i, a, ret;
3967a8a8
MD
595 struct sigaction act;
596 unsigned int remain;
ab7d5fc6
MD
597
598 if (argc < 4) {
599 show_usage(argc, argv);
600 return -1;
601 }
602
603 err = sscanf(argv[1], "%u", &nr_readers);
604 if (err != 1) {
605 show_usage(argc, argv);
606 return -1;
607 }
608
609 err = sscanf(argv[2], "%u", &nr_writers);
610 if (err != 1) {
611 show_usage(argc, argv);
612 return -1;
613 }
614
615 err = sscanf(argv[3], "%lu", &duration);
616 if (err != 1) {
617 show_usage(argc, argv);
618 return -1;
619 }
620
621 for (i = 4; i < argc; i++) {
622 if (argv[i][0] != '-')
623 continue;
624 switch (argv[i][1]) {
625#ifdef DEBUG_YIELD
626 case 'r':
627 yield_active |= YIELD_READ;
628 break;
629 case 'w':
630 yield_active |= YIELD_WRITE;
631 break;
632#endif
633 case 'a':
634 if (argc < i + 2) {
635 show_usage(argc, argv);
636 return -1;
637 }
638 a = atoi(argv[++i]);
639 cpu_affinities[next_aff++] = a;
640 use_affinity = 1;
641 printf_verbose("Adding CPU %d affinity\n", a);
642 break;
643 case 'c':
644 if (argc < i + 2) {
645 show_usage(argc, argv);
646 return -1;
647 }
648 rduration = atol(argv[++i]);
649 break;
650 case 'd':
651 if (argc < i + 2) {
652 show_usage(argc, argv);
653 return -1;
654 }
655 wdelay = atol(argv[++i]);
656 break;
657 case 'v':
658 verbose_mode = 1;
659 break;
6d5c0ca9
MD
660 case 'p':
661 if (argc < i + 2) {
662 show_usage(argc, argv);
663 return -1;
664 }
665 rand_pool = atol(argv[++i]);
666 break;
667 case 'h':
668 if (argc < i + 2) {
669 show_usage(argc, argv);
670 return -1;
671 }
672 init_hash_size = atol(argv[++i]);
673 break;
674 case 'u':
675 add_unique = 1;
676 break;
677 case 'i':
678 add_only = 1;
679 break;
cd1ae16a
MD
680 case 'k':
681 init_populate = atol(argv[++i]);
682 break;
151e7a93
MD
683 case 'A':
684 opt_auto_resize = 1;
685 break;
8008a032
MD
686 case 'R':
687 lookup_pool_offset = atol(argv[++i]);
688 break;
689 case 'S':
690 write_pool_offset = atol(argv[++i]);
691 break;
692 case 'T':
693 init_pool_offset = atol(argv[++i]);
694 break;
59b10634
MD
695 case 'V':
696 validate_lookup = 1;
697 break;
8008a032 698
ab7d5fc6
MD
699 }
700 }
701
6d5c0ca9
MD
702 /* Check if hash size is power of 2 */
703 if (init_hash_size && init_hash_size & (init_hash_size - 1)) {
704 printf("Error: Hash table size %lu is not a power of 2.\n",
705 init_hash_size);
706 return -1;
707 }
708
3967a8a8
MD
709 memset(&act, 0, sizeof(act));
710 ret = sigemptyset(&act.sa_mask);
711 if (ret == -1) {
712 perror("sigemptyset");
713 return -1;
714 }
715 act.sa_handler = sigusr1_handler;
716 act.sa_flags = SA_RESTART;
717 ret = sigaction(SIGUSR1, &act, NULL);
718 if (ret == -1) {
719 perror("sigaction");
720 return -1;
721 }
722
ab7d5fc6
MD
723 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
724 duration, nr_readers, nr_writers);
725 printf_verbose("Writer delay : %lu loops.\n", wdelay);
726 printf_verbose("Reader duration : %lu loops.\n", rduration);
6d5c0ca9
MD
727 printf_verbose("Random pool size : %lu.\n", rand_pool);
728 printf_verbose("Mode:%s%s.\n",
729 add_only ? " add only" : " add/remove",
730 add_unique ? " uniquify" : "");
731 printf_verbose("Initial hash table size: %lu buckets.\n", init_hash_size);
ab7d5fc6
MD
732 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
733 "main", pthread_self(), (unsigned long)gettid());
734
ab7d5fc6
MD
735 tid_reader = malloc(sizeof(*tid_reader) * nr_readers);
736 tid_writer = malloc(sizeof(*tid_writer) * nr_writers);
737 count_reader = malloc(sizeof(*count_reader) * nr_readers);
738 count_writer = malloc(sizeof(*count_writer) * nr_writers);
14044b37 739 test_ht = cds_lfht_new(test_hash, test_compare, 0x42UL,
151e7a93
MD
740 init_hash_size,
741 opt_auto_resize ? CDS_LFHT_AUTO_RESIZE : 0,
01dbfa62 742 call_rcu, synchronize_rcu, rcu_read_lock,
5f511391
MD
743 rcu_read_unlock, rcu_thread_offline,
744 rcu_thread_online);
5dc45f25
MD
745 ret = populate_hash();
746 assert(!ret);
14756542
MD
747 err = create_all_cpu_call_rcu_data(0);
748 assert(!err);
749
ab7d5fc6
MD
750 next_aff = 0;
751
752 for (i = 0; i < nr_readers; i++) {
753 err = pthread_create(&tid_reader[i], NULL, thr_reader,
754 &count_reader[i]);
755 if (err != 0)
756 exit(1);
757 }
758 for (i = 0; i < nr_writers; i++) {
759 err = pthread_create(&tid_writer[i], NULL, thr_writer,
760 &count_writer[i]);
761 if (err != 0)
762 exit(1);
763 }
764
abc490a1 765 cmm_smp_mb();
ab7d5fc6
MD
766
767 test_go = 1;
768
3967a8a8
MD
769 remain = duration;
770 do {
771 remain = sleep(remain);
772 } while (remain > 0);
ab7d5fc6
MD
773
774 test_stop = 1;
775
776 for (i = 0; i < nr_readers; i++) {
777 err = pthread_join(tid_reader[i], &tret);
778 if (err != 0)
779 exit(1);
780 tot_reads += count_reader[i];
781 }
782 for (i = 0; i < nr_writers; i++) {
783 err = pthread_join(tid_writer[i], &tret);
784 if (err != 0)
785 exit(1);
b8e1907c
MD
786 tot_writes += count_writer[i].update_ops;
787 tot_add += count_writer[i].add;
3c16bf4b 788 tot_add_exist += count_writer[i].add_exist;
b8e1907c 789 tot_remove += count_writer[i].remove;
ab7d5fc6 790 }
33c7c748
MD
791 printf("Counting nodes... ");
792 fflush(stdout);
14044b37 793 cds_lfht_count_nodes(test_ht, &count, &removed);
33c7c748 794 printf("done.\n");
b8e1907c 795 if (count || removed)
273399de
MD
796 printf("WARNING: nodes left in the hash table upon destroy: "
797 "%lu nodes + %lu logically removed.\n", count, removed);
14044b37 798 ret = cds_lfht_destroy(test_ht);
f000907d 799
33c7c748
MD
800 if (ret)
801 printf_verbose("final delete aborted\n");
802 else
803 printf_verbose("final delete success\n");
ab7d5fc6
MD
804 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
805 tot_writes);
806 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu "
807 "nr_writers %3u "
6d5c0ca9 808 "wdelay %6lu rand_pool %12llu nr_reads %12llu nr_writes %12llu nr_ops %12llu "
cd1ae16a 809 "nr_add %12llu nr_add_fail %12llu nr_remove %12llu nr_leaked %12lld\n",
ab7d5fc6 810 argv[0], duration, nr_readers, rduration,
6d5c0ca9 811 nr_writers, wdelay, rand_pool, tot_reads, tot_writes,
3c16bf4b 812 tot_reads + tot_writes, tot_add, tot_add_exist, tot_remove,
cd1ae16a 813 (long long) tot_add + init_populate - tot_remove - count);
3a22f1dd 814 free_all_cpu_call_rcu_data();
ab7d5fc6
MD
815 free(tid_reader);
816 free(tid_writer);
817 free(count_reader);
818 free(count_writer);
819 return 0;
820}
This page took 0.059057 seconds and 4 git commands to generate.