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