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