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