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