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