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