test rculfhash: add/remove/random support (with SIGUSR1)
[urcu.git] / tests / test_urcu_hash.c
1 /*
2 * test_urcu_hash.c
3 *
4 * Userspace RCU library - test program
5 *
6 * Copyright February 2009 - Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23 #define _GNU_SOURCE
24 #include <stdio.h>
25 #include <pthread.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/types.h>
29 #include <sys/wait.h>
30 #include <unistd.h>
31 #include <stdio.h>
32 #include <assert.h>
33 #include <sched.h>
34 #include <errno.h>
35
36 #ifdef __linux__
37 #include <syscall.h>
38 #endif
39
40 #define DEFAULT_HASH_SIZE 32
41 #define DEFAULT_RAND_POOL 1000000
42
43 /* Make this big enough to include the POWER5+ L3 cacheline size of 256B */
44 #define CACHE_LINE_SIZE 4096
45
46 /* hardcoded number of CPUs */
47 #define NR_CPUS 16384
48
49 #if defined(_syscall0)
50 _syscall0(pid_t, gettid)
51 #elif defined(__NR_gettid)
52 static inline pid_t gettid(void)
53 {
54 return syscall(__NR_gettid);
55 }
56 #else
57 #warning "use pid as tid"
58 static inline pid_t gettid(void)
59 {
60 return getpid();
61 }
62 #endif
63
64 #ifndef DYNAMIC_LINK_TEST
65 #define _LGPL_SOURCE
66 #else
67 #define debug_yield_read()
68 #endif
69 #include <urcu-qsbr.h>
70 #include <urcu/rculfhash.h>
71 #include <urcu-call-rcu.h>
72
73 struct wr_count {
74 unsigned long update_ops;
75 unsigned long add;
76 unsigned long add_exist;
77 unsigned long remove;
78 };
79
80 static unsigned int __thread rand_lookup;
81 static unsigned long __thread nr_add;
82 static unsigned long __thread nr_addexist;
83 static unsigned long __thread nr_del;
84 static unsigned long __thread nr_delnoent;
85 static unsigned long __thread lookup_fail;
86 static unsigned long __thread lookup_ok;
87
88 static struct cds_lfht *test_ht;
89
90 struct test_data {
91 int a;
92 int b;
93 };
94
95 static volatile int test_go, test_stop;
96
97 static unsigned long wdelay;
98
99 static unsigned long duration;
100
101 /* read-side C.S. duration, in loops */
102 static unsigned long rduration;
103
104 static unsigned long init_hash_size = DEFAULT_HASH_SIZE;
105 static unsigned long init_populate;
106 static unsigned long rand_pool = DEFAULT_RAND_POOL;
107 static int add_only, add_unique;
108
109 static inline void loop_sleep(unsigned long l)
110 {
111 while(l-- != 0)
112 caa_cpu_relax();
113 }
114
115 static int verbose_mode;
116
117 #define printf_verbose(fmt, args...) \
118 do { \
119 if (verbose_mode) \
120 printf(fmt, ## args); \
121 } while (0)
122
123 static unsigned int cpu_affinities[NR_CPUS];
124 static unsigned int next_aff = 0;
125 static int use_affinity = 0;
126
127 pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
128
129 static void set_affinity(void)
130 {
131 cpu_set_t mask;
132 int cpu;
133 int ret;
134
135 if (!use_affinity)
136 return;
137
138 ret = pthread_mutex_lock(&affinity_mutex);
139 if (ret) {
140 perror("Error in pthread mutex lock");
141 exit(-1);
142 }
143 cpu = cpu_affinities[next_aff++];
144 ret = pthread_mutex_unlock(&affinity_mutex);
145 if (ret) {
146 perror("Error in pthread mutex unlock");
147 exit(-1);
148 }
149 CPU_ZERO(&mask);
150 CPU_SET(cpu, &mask);
151 sched_setaffinity(0, sizeof(mask), &mask);
152 }
153
154 static enum {
155 AR_RANDOM = 0,
156 AR_ADD = 1,
157 AR_REMOVE = -1,
158 } addremove; /* 1: add, -1 remove, 0: random */
159
160 static
161 void sigusr1_handler(int signo)
162 {
163 switch (addremove) {
164 case AR_ADD:
165 printf("Add/Remove: random.\n");
166 addremove = AR_RANDOM;
167 break;
168 case AR_RANDOM:
169 printf("Add/Remove: remove only.\n");
170 addremove = AR_REMOVE;
171 break;
172 case AR_REMOVE:
173 printf("Add/Remove: add only.\n");
174 addremove = AR_ADD;
175 break;
176 }
177 }
178
179 /*
180 * returns 0 if test should end.
181 */
182 static int test_duration_write(void)
183 {
184 return !test_stop;
185 }
186
187 static int test_duration_read(void)
188 {
189 return !test_stop;
190 }
191
192 static unsigned long long __thread nr_writes;
193 static unsigned long long __thread nr_reads;
194
195 static unsigned int nr_readers;
196 static unsigned int nr_writers;
197
198 pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
199
200 void rcu_copy_mutex_lock(void)
201 {
202 int ret;
203 ret = pthread_mutex_lock(&rcu_copy_mutex);
204 if (ret) {
205 perror("Error in pthread mutex lock");
206 exit(-1);
207 }
208 }
209
210 void rcu_copy_mutex_unlock(void)
211 {
212 int ret;
213
214 ret = pthread_mutex_unlock(&rcu_copy_mutex);
215 if (ret) {
216 perror("Error in pthread mutex unlock");
217 exit(-1);
218 }
219 }
220
221 /*
222 * Hash function
223 * Source: http://burtleburtle.net/bob/c/lookup3.c
224 * Originally Public Domain
225 */
226
227 #define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
228
229 #define mix(a, b, c) \
230 do { \
231 a -= c; a ^= rot(c, 4); c += b; \
232 b -= a; b ^= rot(a, 6); a += c; \
233 c -= b; c ^= rot(b, 8); b += a; \
234 a -= c; a ^= rot(c, 16); c += b; \
235 b -= a; b ^= rot(a, 19); a += c; \
236 c -= b; c ^= rot(b, 4); b += a; \
237 } while (0)
238
239 #define final(a, b, c) \
240 { \
241 c ^= b; c -= rot(b, 14); \
242 a ^= c; a -= rot(c, 11); \
243 b ^= a; b -= rot(a, 25); \
244 c ^= b; c -= rot(b, 16); \
245 a ^= c; a -= rot(c, 4);\
246 b ^= a; b -= rot(a, 14); \
247 c ^= b; c -= rot(b, 24); \
248 }
249
250 static __attribute__((unused))
251 uint32_t hash_u32(
252 const uint32_t *k, /* the key, an array of uint32_t values */
253 size_t length, /* the length of the key, in uint32_ts */
254 uint32_t initval) /* the previous hash, or an arbitrary value */
255 {
256 uint32_t a, b, c;
257
258 /* Set up the internal state */
259 a = b = c = 0xdeadbeef + (((uint32_t) length) << 2) + initval;
260
261 /*----------------------------------------- handle most of the key */
262 while (length > 3) {
263 a += k[0];
264 b += k[1];
265 c += k[2];
266 mix(a, b, c);
267 length -= 3;
268 k += 3;
269 }
270
271 /*----------------------------------- handle the last 3 uint32_t's */
272 switch (length) { /* all the case statements fall through */
273 case 3: c += k[2];
274 case 2: b += k[1];
275 case 1: a += k[0];
276 final(a, b, c);
277 case 0: /* case 0: nothing left to add */
278 break;
279 }
280 /*---------------------------------------------- report the result */
281 return c;
282 }
283
284 static
285 void hashword2(
286 const uint32_t *k, /* the key, an array of uint32_t values */
287 size_t length, /* the length of the key, in uint32_ts */
288 uint32_t *pc, /* IN: seed OUT: primary hash value */
289 uint32_t *pb) /* IN: more seed OUT: secondary hash value */
290 {
291 uint32_t a, b, c;
292
293 /* Set up the internal state */
294 a = b = c = 0xdeadbeef + ((uint32_t) (length << 2)) + *pc;
295 c += *pb;
296
297 /*----------------------------------------- handle most of the key */
298 while (length > 3) {
299 a += k[0];
300 b += k[1];
301 c += k[2];
302 mix(a, b, c);
303 length -= 3;
304 k += 3;
305 }
306
307 /*----------------------------------- handle the last 3 uint32_t's */
308 switch (length) { /* all the case statements fall through */
309 case 3: c += k[2];
310 case 2: b += k[1];
311 case 1: a += k[0];
312 final(a, b, c);
313 case 0: /* case 0: nothing left to add */
314 break;
315 }
316 /*---------------------------------------------- report the result */
317 *pc = c;
318 *pb = b;
319 }
320
321 #if (CAA_BITS_PER_LONG == 32)
322 static
323 unsigned long test_hash(void *_key, size_t length, unsigned long seed)
324 {
325 unsigned long key = (unsigned long) _key;
326 unsigned long v;
327
328 assert(length == sizeof(unsigned long));
329 return hash_u32(&v, 1, seed);
330 }
331 #else
332 static
333 unsigned long test_hash(void *_key, size_t length, unsigned long seed)
334 {
335 union {
336 uint64_t v64;
337 uint32_t v32[2];
338 } v;
339 union {
340 uint64_t v64;
341 uint32_t v32[2];
342 } key;
343
344 assert(length == sizeof(unsigned long));
345 v.v64 = (uint64_t) seed;
346 key.v64 = (uint64_t) _key;
347 hashword2(key.v32, 2, &v.v32[0], &v.v32[1]);
348 return v.v64;
349 }
350 #endif
351
352 static
353 unsigned long test_compare(void *key1, size_t key1_len,
354 void *key2, size_t key2_len)
355 {
356 if (unlikely(key1_len != key2_len))
357 return -1;
358 assert(key1_len == sizeof(unsigned long));
359 if (key1 == key2)
360 return 0;
361 else
362 return 1;
363 }
364
365 void *thr_reader(void *_count)
366 {
367 unsigned long long *count = _count;
368 struct cds_lfht_node *node;
369
370 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
371 "reader", pthread_self(), (unsigned long)gettid());
372
373 set_affinity();
374
375 rcu_register_thread();
376
377 while (!test_go)
378 {
379 }
380 cmm_smp_mb();
381
382 for (;;) {
383 rcu_read_lock();
384 node = cds_lfht_lookup(test_ht,
385 (void *)(unsigned long)(rand_r(&rand_lookup) % rand_pool),
386 sizeof(void *));
387 if (node == NULL)
388 lookup_fail++;
389 else
390 lookup_ok++;
391 debug_yield_read();
392 if (unlikely(rduration))
393 loop_sleep(rduration);
394 rcu_read_unlock();
395 nr_reads++;
396 if (unlikely(!test_duration_read()))
397 break;
398 if (unlikely((nr_reads & ((1 << 10) - 1)) == 0))
399 rcu_quiescent_state();
400 }
401
402 rcu_unregister_thread();
403
404 *count = nr_reads;
405 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
406 "reader", pthread_self(), (unsigned long)gettid());
407 printf_verbose("readid : %lx, lookupfail %lu, lookupok %lu\n",
408 pthread_self(), lookup_fail, lookup_ok);
409 return ((void*)1);
410
411 }
412
413 static
414 void free_node_cb(struct rcu_head *head)
415 {
416 struct cds_lfht_node *node =
417 caa_container_of(head, struct cds_lfht_node, head);
418 free(node);
419 }
420
421 void *thr_writer(void *_count)
422 {
423 struct cds_lfht_node *node, *ret_node;
424 struct wr_count *count = _count;
425 int ret;
426
427 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
428 "writer", pthread_self(), (unsigned long)gettid());
429
430 set_affinity();
431
432 rcu_register_thread();
433
434 while (!test_go)
435 {
436 }
437 cmm_smp_mb();
438
439 for (;;) {
440 if ((addremove == AR_ADD || add_only)
441 || (addremove == AR_RANDOM && rand_r(&rand_lookup) & 1)) {
442 node = malloc(sizeof(struct cds_lfht_node));
443 rcu_read_lock();
444 cds_lfht_node_init(node,
445 (void *)(unsigned long)(rand_r(&rand_lookup) % rand_pool),
446 sizeof(void *));
447 if (add_unique)
448 ret_node = cds_lfht_add_unique(test_ht, node);
449 else
450 cds_lfht_add(test_ht, node);
451 rcu_read_unlock();
452 if (add_unique && ret_node != node) {
453 free(node);
454 nr_addexist++;
455 } else
456 nr_add++;
457 } else {
458 /* May delete */
459 rcu_read_lock();
460 node = cds_lfht_lookup(test_ht,
461 (void *)(unsigned long)(rand_r(&rand_lookup) % rand_pool),
462 sizeof(void *));
463 if (node)
464 ret = cds_lfht_remove(test_ht, node);
465 else
466 ret = -ENOENT;
467 rcu_read_unlock();
468 if (ret == 0) {
469 call_rcu(&node->head, free_node_cb);
470 nr_del++;
471 } else
472 nr_delnoent++;
473 }
474 #if 0
475 //if (nr_writes % 100000 == 0) {
476 if (nr_writes % 1000 == 0) {
477 rcu_read_lock();
478 if (rand_r(&rand_lookup) & 1) {
479 ht_resize(test_ht, 1);
480 } else {
481 ht_resize(test_ht, -1);
482 }
483 rcu_read_unlock();
484 }
485 #endif //0
486 nr_writes++;
487 if (unlikely(!test_duration_write()))
488 break;
489 if (unlikely(wdelay))
490 loop_sleep(wdelay);
491 if (unlikely((nr_writes & ((1 << 10) - 1)) == 0))
492 rcu_quiescent_state();
493 }
494
495 rcu_unregister_thread();
496
497 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
498 "writer", pthread_self(), (unsigned long)gettid());
499 printf_verbose("info id %lx: nr_add %lu, nr_addexist %lu, nr_del %lu, "
500 "nr_delnoent %lu\n", pthread_self(), nr_add,
501 nr_addexist, nr_del, nr_delnoent);
502 count->update_ops = nr_writes;
503 count->add = nr_add;
504 count->add_exist = nr_addexist;
505 count->remove = nr_del;
506 return ((void*)2);
507 }
508
509 static int populate_hash(void)
510 {
511 struct cds_lfht_node *node, *ret_node;
512
513 if (!init_populate)
514 return 0;
515
516 if (add_unique && init_populate * 10 > rand_pool) {
517 printf("WARNING: required to populate %lu nodes (-k), but random "
518 "pool is quite small (%lu values) and we are in add_unique (-u) mode. Try with a "
519 "larger random pool (-p option).\n", init_populate, rand_pool);
520 return -1;
521 }
522
523 while (nr_add < init_populate) {
524 node = malloc(sizeof(struct cds_lfht_node));
525 cds_lfht_node_init(node,
526 (void *)(unsigned long)(rand_r(&rand_lookup) % rand_pool),
527 sizeof(void *));
528 if (add_unique)
529 ret_node = cds_lfht_add_unique(test_ht, node);
530 else
531 cds_lfht_add(test_ht, node);
532 if (add_unique && ret_node != node) {
533 free(node);
534 nr_addexist++;
535 } else
536 nr_add++;
537 nr_writes++;
538 }
539 return 0;
540 }
541
542 void show_usage(int argc, char **argv)
543 {
544 printf("Usage : %s nr_readers nr_writers duration (s)", argv[0]);
545 #ifdef DEBUG_YIELD
546 printf(" [-r] [-w] (yield reader and/or writer)");
547 #endif
548 printf(" [-d delay] (writer period (us))");
549 printf(" [-c duration] (reader C.S. duration (in loops))");
550 printf(" [-v] (verbose output)");
551 printf(" [-a cpu#] [-a cpu#]... (affinity)");
552 printf(" [-p size] (random key value pool size)");
553 printf(" [-h size] (initial hash table size)");
554 printf(" [-u] Uniquify add.");
555 printf(" [-i] Add only (no removal).");
556 printf(" [-k nr_nodes] Number of nodes to insert initially.");
557 printf("\n");
558 }
559
560 int main(int argc, char **argv)
561 {
562 int err;
563 pthread_t *tid_reader, *tid_writer;
564 void *tret;
565 unsigned long long *count_reader;
566 struct wr_count *count_writer;
567 unsigned long long tot_reads = 0, tot_writes = 0,
568 tot_add = 0, tot_add_exist = 0, tot_remove = 0;
569 unsigned long count, removed;
570 int i, a, ret;
571 struct sigaction act;
572 unsigned int remain;
573
574 if (argc < 4) {
575 show_usage(argc, argv);
576 return -1;
577 }
578
579 err = sscanf(argv[1], "%u", &nr_readers);
580 if (err != 1) {
581 show_usage(argc, argv);
582 return -1;
583 }
584
585 err = sscanf(argv[2], "%u", &nr_writers);
586 if (err != 1) {
587 show_usage(argc, argv);
588 return -1;
589 }
590
591 err = sscanf(argv[3], "%lu", &duration);
592 if (err != 1) {
593 show_usage(argc, argv);
594 return -1;
595 }
596
597 for (i = 4; i < argc; i++) {
598 if (argv[i][0] != '-')
599 continue;
600 switch (argv[i][1]) {
601 #ifdef DEBUG_YIELD
602 case 'r':
603 yield_active |= YIELD_READ;
604 break;
605 case 'w':
606 yield_active |= YIELD_WRITE;
607 break;
608 #endif
609 case 'a':
610 if (argc < i + 2) {
611 show_usage(argc, argv);
612 return -1;
613 }
614 a = atoi(argv[++i]);
615 cpu_affinities[next_aff++] = a;
616 use_affinity = 1;
617 printf_verbose("Adding CPU %d affinity\n", a);
618 break;
619 case 'c':
620 if (argc < i + 2) {
621 show_usage(argc, argv);
622 return -1;
623 }
624 rduration = atol(argv[++i]);
625 break;
626 case 'd':
627 if (argc < i + 2) {
628 show_usage(argc, argv);
629 return -1;
630 }
631 wdelay = atol(argv[++i]);
632 break;
633 case 'v':
634 verbose_mode = 1;
635 break;
636 case 'p':
637 if (argc < i + 2) {
638 show_usage(argc, argv);
639 return -1;
640 }
641 rand_pool = atol(argv[++i]);
642 break;
643 case 'h':
644 if (argc < i + 2) {
645 show_usage(argc, argv);
646 return -1;
647 }
648 init_hash_size = atol(argv[++i]);
649 break;
650 case 'u':
651 add_unique = 1;
652 break;
653 case 'i':
654 add_only = 1;
655 break;
656 case 'k':
657 init_populate = atol(argv[++i]);
658 break;
659 }
660 }
661
662 /* Check if hash size is power of 2 */
663 if (init_hash_size && init_hash_size & (init_hash_size - 1)) {
664 printf("Error: Hash table size %lu is not a power of 2.\n",
665 init_hash_size);
666 return -1;
667 }
668
669 memset(&act, 0, sizeof(act));
670 ret = sigemptyset(&act.sa_mask);
671 if (ret == -1) {
672 perror("sigemptyset");
673 return -1;
674 }
675 act.sa_handler = sigusr1_handler;
676 act.sa_flags = SA_RESTART;
677 ret = sigaction(SIGUSR1, &act, NULL);
678 if (ret == -1) {
679 perror("sigaction");
680 return -1;
681 }
682
683 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
684 duration, nr_readers, nr_writers);
685 printf_verbose("Writer delay : %lu loops.\n", wdelay);
686 printf_verbose("Reader duration : %lu loops.\n", rduration);
687 printf_verbose("Random pool size : %lu.\n", rand_pool);
688 printf_verbose("Mode:%s%s.\n",
689 add_only ? " add only" : " add/remove",
690 add_unique ? " uniquify" : "");
691 printf_verbose("Initial hash table size: %lu buckets.\n", init_hash_size);
692 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
693 "main", pthread_self(), (unsigned long)gettid());
694
695 tid_reader = malloc(sizeof(*tid_reader) * nr_readers);
696 tid_writer = malloc(sizeof(*tid_writer) * nr_writers);
697 count_reader = malloc(sizeof(*count_reader) * nr_readers);
698 count_writer = malloc(sizeof(*count_writer) * nr_writers);
699 test_ht = cds_lfht_new(test_hash, test_compare, 0x42UL,
700 init_hash_size, call_rcu);
701 ret = populate_hash();
702 assert(!ret);
703 err = create_all_cpu_call_rcu_data(0);
704 assert(!err);
705
706 next_aff = 0;
707
708 for (i = 0; i < nr_readers; i++) {
709 err = pthread_create(&tid_reader[i], NULL, thr_reader,
710 &count_reader[i]);
711 if (err != 0)
712 exit(1);
713 }
714 for (i = 0; i < nr_writers; i++) {
715 err = pthread_create(&tid_writer[i], NULL, thr_writer,
716 &count_writer[i]);
717 if (err != 0)
718 exit(1);
719 }
720
721 cmm_smp_mb();
722
723 test_go = 1;
724
725 remain = duration;
726 do {
727 remain = sleep(remain);
728 } while (remain > 0);
729
730 test_stop = 1;
731
732 for (i = 0; i < nr_readers; i++) {
733 err = pthread_join(tid_reader[i], &tret);
734 if (err != 0)
735 exit(1);
736 tot_reads += count_reader[i];
737 }
738 for (i = 0; i < nr_writers; i++) {
739 err = pthread_join(tid_writer[i], &tret);
740 if (err != 0)
741 exit(1);
742 tot_writes += count_writer[i].update_ops;
743 tot_add += count_writer[i].add;
744 tot_add_exist += count_writer[i].add_exist;
745 tot_remove += count_writer[i].remove;
746 }
747 printf("Counting nodes... ");
748 fflush(stdout);
749 cds_lfht_count_nodes(test_ht, &count, &removed);
750 printf("done.\n");
751 if (count || removed)
752 printf("WARNING: nodes left in the hash table upon destroy: "
753 "%lu nodes + %lu logically removed.\n", count, removed);
754 ret = cds_lfht_destroy(test_ht);
755
756 if (ret)
757 printf_verbose("final delete aborted\n");
758 else
759 printf_verbose("final delete success\n");
760 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
761 tot_writes);
762 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu "
763 "nr_writers %3u "
764 "wdelay %6lu rand_pool %12llu nr_reads %12llu nr_writes %12llu nr_ops %12llu "
765 "nr_add %12llu nr_add_fail %12llu nr_remove %12llu nr_leaked %12lld\n",
766 argv[0], duration, nr_readers, rduration,
767 nr_writers, wdelay, rand_pool, tot_reads, tot_writes,
768 tot_reads + tot_writes, tot_add, tot_add_exist, tot_remove,
769 (long long) tot_add + init_populate - tot_remove - count);
770 free(tid_reader);
771 free(tid_writer);
772 free(count_reader);
773 free(count_writer);
774 return 0;
775 }
This page took 0.044943 seconds and 5 git commands to generate.