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