rculfhash test: print number of add fail (uniquify matches)
[urcu.git] / tests / test_urcu_hash.c
CommitLineData
ab7d5fc6
MD
1/*
2 * test_ht.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>
ab7d5fc6 33#include <sched.h>
5e28c532 34#include <errno.h>
ab7d5fc6 35
abc490a1
MD
36#ifdef __linux__
37#include <syscall.h>
38#endif
ab7d5fc6 39
6d5c0ca9
MD
40#define DEFAULT_HASH_SIZE 32
41#define DEFAULT_RAND_POOL 1000000
5e28c532 42
ab7d5fc6
MD
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)
52static inline pid_t gettid(void)
53{
54 return syscall(__NR_gettid);
55}
56#else
57#warning "use pid as tid"
58static 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
abc490a1
MD
69#include <urcu.h>
70#include <urcu/rculfhash.h>
71#include <urcu-call-rcu.h>
ab7d5fc6 72
b8e1907c
MD
73struct wr_count {
74 unsigned long update_ops;
75 unsigned long add;
3c16bf4b 76 unsigned long add_exist;
b8e1907c
MD
77 unsigned long remove;
78};
79
5e28c532
MD
80static unsigned int __thread rand_lookup;
81static unsigned long __thread nr_add;
82static unsigned long __thread nr_addexist;
83static unsigned long __thread nr_del;
84static unsigned long __thread nr_delnoent;
85static unsigned long __thread lookup_fail;
86static unsigned long __thread lookup_ok;
87
14044b37 88static struct cds_lfht *test_ht;
ab7d5fc6 89
5e28c532 90struct test_data {
ab7d5fc6 91 int a;
5e28c532 92 int b;
ab7d5fc6
MD
93};
94
95static volatile int test_go, test_stop;
96
97static unsigned long wdelay;
98
ab7d5fc6
MD
99static unsigned long duration;
100
101/* read-side C.S. duration, in loops */
102static unsigned long rduration;
103
6d5c0ca9
MD
104static unsigned long init_hash_size = DEFAULT_HASH_SIZE;
105static unsigned long rand_pool = DEFAULT_RAND_POOL;
106static int add_only, add_unique;
107
ab7d5fc6
MD
108static inline void loop_sleep(unsigned long l)
109{
110 while(l-- != 0)
abc490a1 111 caa_cpu_relax();
ab7d5fc6
MD
112}
113
114static int verbose_mode;
115
116#define printf_verbose(fmt, args...) \
117 do { \
118 if (verbose_mode) \
33c7c748 119 printf(fmt, ## args); \
ab7d5fc6
MD
120 } while (0)
121
122static unsigned int cpu_affinities[NR_CPUS];
123static unsigned int next_aff = 0;
124static int use_affinity = 0;
125
126pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
127
128static void set_affinity(void)
129{
130 cpu_set_t mask;
131 int cpu;
132 int ret;
133
134 if (!use_affinity)
135 return;
136
137 ret = pthread_mutex_lock(&affinity_mutex);
138 if (ret) {
139 perror("Error in pthread mutex lock");
140 exit(-1);
141 }
142 cpu = cpu_affinities[next_aff++];
143 ret = pthread_mutex_unlock(&affinity_mutex);
144 if (ret) {
145 perror("Error in pthread mutex unlock");
146 exit(-1);
147 }
148 CPU_ZERO(&mask);
149 CPU_SET(cpu, &mask);
150 sched_setaffinity(0, sizeof(mask), &mask);
151}
152
153/*
154 * returns 0 if test should end.
155 */
156static int test_duration_write(void)
157{
158 return !test_stop;
159}
160
161static int test_duration_read(void)
162{
163 return !test_stop;
164}
165
166static unsigned long long __thread nr_writes;
167static unsigned long long __thread nr_reads;
168
169static unsigned int nr_readers;
170static unsigned int nr_writers;
171
172pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
173
174void rcu_copy_mutex_lock(void)
175{
176 int ret;
177 ret = pthread_mutex_lock(&rcu_copy_mutex);
178 if (ret) {
179 perror("Error in pthread mutex lock");
180 exit(-1);
181 }
182}
183
184void rcu_copy_mutex_unlock(void)
185{
186 int ret;
187
188 ret = pthread_mutex_unlock(&rcu_copy_mutex);
189 if (ret) {
190 perror("Error in pthread mutex unlock");
191 exit(-1);
192 }
193}
194
732ad076
MD
195/*
196 * Hash function
197 * Source: http://burtleburtle.net/bob/c/lookup3.c
198 * Originally Public Domain
199 */
200
201#define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
202
203#define mix(a, b, c) \
204do { \
205 a -= c; a ^= rot(c, 4); c += b; \
206 b -= a; b ^= rot(a, 6); a += c; \
207 c -= b; c ^= rot(b, 8); b += a; \
208 a -= c; a ^= rot(c, 16); c += b; \
209 b -= a; b ^= rot(a, 19); a += c; \
210 c -= b; c ^= rot(b, 4); b += a; \
211} while (0)
212
213#define final(a, b, c) \
214{ \
215 c ^= b; c -= rot(b, 14); \
216 a ^= c; a -= rot(c, 11); \
217 b ^= a; b -= rot(a, 25); \
218 c ^= b; c -= rot(b, 16); \
219 a ^= c; a -= rot(c, 4);\
220 b ^= a; b -= rot(a, 14); \
221 c ^= b; c -= rot(b, 24); \
222}
223
224static __attribute__((unused))
225uint32_t hash_u32(
226 const uint32_t *k, /* the key, an array of uint32_t values */
227 size_t length, /* the length of the key, in uint32_ts */
228 uint32_t initval) /* the previous hash, or an arbitrary value */
229{
230 uint32_t a, b, c;
231
232 /* Set up the internal state */
233 a = b = c = 0xdeadbeef + (((uint32_t) length) << 2) + initval;
234
235 /*----------------------------------------- handle most of the key */
236 while (length > 3) {
237 a += k[0];
238 b += k[1];
239 c += k[2];
240 mix(a, b, c);
241 length -= 3;
242 k += 3;
243 }
244
245 /*----------------------------------- handle the last 3 uint32_t's */
246 switch (length) { /* all the case statements fall through */
247 case 3: c += k[2];
248 case 2: b += k[1];
249 case 1: a += k[0];
250 final(a, b, c);
251 case 0: /* case 0: nothing left to add */
252 break;
253 }
254 /*---------------------------------------------- report the result */
255 return c;
256}
257
258static
259void hashword2(
260 const uint32_t *k, /* the key, an array of uint32_t values */
261 size_t length, /* the length of the key, in uint32_ts */
262 uint32_t *pc, /* IN: seed OUT: primary hash value */
263 uint32_t *pb) /* IN: more seed OUT: secondary hash value */
264{
265 uint32_t a, b, c;
266
267 /* Set up the internal state */
268 a = b = c = 0xdeadbeef + ((uint32_t) (length << 2)) + *pc;
269 c += *pb;
270
271 /*----------------------------------------- handle most of the key */
272 while (length > 3) {
273 a += k[0];
274 b += k[1];
275 c += k[2];
276 mix(a, b, c);
277 length -= 3;
278 k += 3;
279 }
280
281 /*----------------------------------- handle the last 3 uint32_t's */
282 switch (length) { /* all the case statements fall through */
283 case 3: c += k[2];
284 case 2: b += k[1];
285 case 1: a += k[0];
286 final(a, b, c);
287 case 0: /* case 0: nothing left to add */
288 break;
289 }
290 /*---------------------------------------------- report the result */
291 *pc = c;
292 *pb = b;
293}
294
295#if (CAA_BITS_PER_LONG == 32)
abc490a1 296static
732ad076 297unsigned long test_hash(void *_key, size_t length, unsigned long seed)
abc490a1 298{
abc490a1
MD
299 unsigned long key = (unsigned long) _key;
300 unsigned long v;
301
732ad076
MD
302 assert(length == sizeof(unsigned long));
303 return hash_u32(&v, 1, seed);
304}
305#else
306static
307unsigned long test_hash(void *_key, size_t length, unsigned long seed)
308{
309 union {
310 uint64_t v64;
311 uint32_t v32[2];
312 } v;
313 union {
314 uint64_t v64;
315 uint32_t v32[2];
316 } key;
317
318 assert(length == sizeof(unsigned long));
319 v.v64 = (uint64_t) seed;
320 key.v64 = (uint64_t) _key;
321 hashword2(key.v32, 2, &v.v32[0], &v.v32[1]);
322 return v.v64;
323}
324#endif
abc490a1 325
732ad076
MD
326static
327unsigned long test_compare(void *key1, size_t key1_len,
328 void *key2, size_t key2_len)
329{
330 if (unlikely(key1_len != key2_len))
331 return -1;
332 assert(key1_len == sizeof(unsigned long));
333 if (key1 == key2)
334 return 0;
335 else
336 return 1;
abc490a1 337}
ab7d5fc6
MD
338
339void *thr_reader(void *_count)
340{
341 unsigned long long *count = _count;
14044b37 342 struct cds_lfht_node *node;
ab7d5fc6
MD
343
344 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
345 "reader", pthread_self(), (unsigned long)gettid());
346
347 set_affinity();
348
349 rcu_register_thread();
350
351 while (!test_go)
352 {
353 }
abc490a1 354 cmm_smp_mb();
ab7d5fc6
MD
355
356 for (;;) {
357 rcu_read_lock();
14044b37 358 node = cds_lfht_lookup(test_ht,
6d5c0ca9 359 (void *)(unsigned long)(rand_r(&rand_lookup) % rand_pool),
732ad076 360 sizeof(void *));
abc490a1 361 if (node == NULL)
5e28c532
MD
362 lookup_fail++;
363 else
364 lookup_ok++;
ab7d5fc6 365 debug_yield_read();
ab7d5fc6
MD
366 if (unlikely(rduration))
367 loop_sleep(rduration);
368 rcu_read_unlock();
369 nr_reads++;
370 if (unlikely(!test_duration_read()))
371 break;
372 }
373
374 rcu_unregister_thread();
375
376 *count = nr_reads;
377 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
378 "reader", pthread_self(), (unsigned long)gettid());
5e28c532
MD
379 printf_verbose("readid : %lx, lookupfail %lu, lookupok %lu\n",
380 pthread_self(), lookup_fail, lookup_ok);
ab7d5fc6
MD
381 return ((void*)1);
382
383}
384
abc490a1
MD
385static
386void free_node_cb(struct rcu_head *head)
387{
14044b37
MD
388 struct cds_lfht_node *node =
389 caa_container_of(head, struct cds_lfht_node, head);
abc490a1
MD
390 free(node);
391}
392
ab7d5fc6
MD
393void *thr_writer(void *_count)
394{
14044b37 395 struct cds_lfht_node *node, *ret_node;
b8e1907c 396 struct wr_count *count = _count;
5e28c532 397 int ret;
ab7d5fc6
MD
398
399 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
400 "writer", pthread_self(), (unsigned long)gettid());
401
402 set_affinity();
403
5e28c532 404 rcu_register_thread();
5e28c532 405
ab7d5fc6
MD
406 while (!test_go)
407 {
408 }
abc490a1 409 cmm_smp_mb();
ab7d5fc6
MD
410
411 for (;;) {
6d5c0ca9 412 if (add_only || rand_r(&rand_lookup) & 1) {
14044b37 413 node = malloc(sizeof(struct cds_lfht_node));
abc490a1 414 rcu_read_lock();
14044b37 415 cds_lfht_node_init(node,
6d5c0ca9 416 (void *)(unsigned long)(rand_r(&rand_lookup) % rand_pool),
cb233752 417 sizeof(void *));
6d5c0ca9 418 if (add_unique)
14044b37 419 ret_node = cds_lfht_add_unique(test_ht, node);
6d5c0ca9 420 else
14044b37 421 cds_lfht_add(test_ht, node);
abc490a1 422 rcu_read_unlock();
6d5c0ca9 423 if (add_unique && ret_node != node) {
742aa1db 424 free(node);
3eca1b8c 425 nr_addexist++;
742aa1db 426 } else
3eca1b8c 427 nr_add++;
5e28c532
MD
428 } else {
429 /* May delete */
abc490a1 430 rcu_read_lock();
14044b37 431 node = cds_lfht_lookup(test_ht,
6d5c0ca9 432 (void *)(unsigned long)(rand_r(&rand_lookup) % rand_pool),
732ad076 433 sizeof(void *));
abc490a1 434 if (node)
14044b37 435 ret = cds_lfht_remove(test_ht, node);
5e28c532 436 else
abc490a1
MD
437 ret = -ENOENT;
438 rcu_read_unlock();
439 if (ret == 0) {
440 call_rcu(&node->head, free_node_cb);
5e28c532 441 nr_del++;
abc490a1
MD
442 } else
443 nr_delnoent++;
44395fb7 444 }
abc490a1 445#if 0
44395fb7
MD
446 //if (nr_writes % 100000 == 0) {
447 if (nr_writes % 1000 == 0) {
abc490a1 448 rcu_read_lock();
44395fb7
MD
449 if (rand_r(&rand_lookup) & 1) {
450 ht_resize(test_ht, 1);
451 } else {
452 ht_resize(test_ht, -1);
453 }
abc490a1 454 rcu_read_unlock();
5e28c532 455 }
abc490a1 456#endif //0
ab7d5fc6
MD
457 nr_writes++;
458 if (unlikely(!test_duration_write()))
459 break;
460 if (unlikely(wdelay))
461 loop_sleep(wdelay);
462 }
463
5e28c532
MD
464 rcu_unregister_thread();
465
ab7d5fc6
MD
466 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
467 "writer", pthread_self(), (unsigned long)gettid());
5e28c532
MD
468 printf_verbose("info id %lx: nr_add %lu, nr_addexist %lu, nr_del %lu, "
469 "nr_delnoent %lu\n", pthread_self(), nr_add,
470 nr_addexist, nr_del, nr_delnoent);
b8e1907c
MD
471 count->update_ops = nr_writes;
472 count->add = nr_add;
3c16bf4b 473 count->add_exist = nr_addexist;
b8e1907c 474 count->remove = nr_del;
ab7d5fc6
MD
475 return ((void*)2);
476}
477
478void show_usage(int argc, char **argv)
479{
480 printf("Usage : %s nr_readers nr_writers duration (s)", argv[0]);
481#ifdef DEBUG_YIELD
482 printf(" [-r] [-w] (yield reader and/or writer)");
483#endif
484 printf(" [-d delay] (writer period (us))");
485 printf(" [-c duration] (reader C.S. duration (in loops))");
486 printf(" [-v] (verbose output)");
487 printf(" [-a cpu#] [-a cpu#]... (affinity)");
6d5c0ca9
MD
488 printf(" [-p size] (random key value pool size)");
489 printf(" [-h size] (initial hash table size)");
490 printf(" [-u] Uniquify add.");
491 printf(" [-i] Add only (no removal).");
ab7d5fc6
MD
492 printf("\n");
493}
494
495int main(int argc, char **argv)
496{
497 int err;
498 pthread_t *tid_reader, *tid_writer;
499 void *tret;
b8e1907c
MD
500 unsigned long long *count_reader;
501 struct wr_count *count_writer;
502 unsigned long long tot_reads = 0, tot_writes = 0,
3c16bf4b 503 tot_add = 0, tot_add_exist = 0, tot_remove = 0;
b8e1907c 504 unsigned long count, removed;
5e28c532 505 int i, a, ret;
ab7d5fc6
MD
506
507 if (argc < 4) {
508 show_usage(argc, argv);
509 return -1;
510 }
511
512 err = sscanf(argv[1], "%u", &nr_readers);
513 if (err != 1) {
514 show_usage(argc, argv);
515 return -1;
516 }
517
518 err = sscanf(argv[2], "%u", &nr_writers);
519 if (err != 1) {
520 show_usage(argc, argv);
521 return -1;
522 }
523
524 err = sscanf(argv[3], "%lu", &duration);
525 if (err != 1) {
526 show_usage(argc, argv);
527 return -1;
528 }
529
530 for (i = 4; i < argc; i++) {
531 if (argv[i][0] != '-')
532 continue;
533 switch (argv[i][1]) {
534#ifdef DEBUG_YIELD
535 case 'r':
536 yield_active |= YIELD_READ;
537 break;
538 case 'w':
539 yield_active |= YIELD_WRITE;
540 break;
541#endif
542 case 'a':
543 if (argc < i + 2) {
544 show_usage(argc, argv);
545 return -1;
546 }
547 a = atoi(argv[++i]);
548 cpu_affinities[next_aff++] = a;
549 use_affinity = 1;
550 printf_verbose("Adding CPU %d affinity\n", a);
551 break;
552 case 'c':
553 if (argc < i + 2) {
554 show_usage(argc, argv);
555 return -1;
556 }
557 rduration = atol(argv[++i]);
558 break;
559 case 'd':
560 if (argc < i + 2) {
561 show_usage(argc, argv);
562 return -1;
563 }
564 wdelay = atol(argv[++i]);
565 break;
566 case 'v':
567 verbose_mode = 1;
568 break;
6d5c0ca9
MD
569 case 'p':
570 if (argc < i + 2) {
571 show_usage(argc, argv);
572 return -1;
573 }
574 rand_pool = atol(argv[++i]);
575 break;
576 case 'h':
577 if (argc < i + 2) {
578 show_usage(argc, argv);
579 return -1;
580 }
581 init_hash_size = atol(argv[++i]);
582 break;
583 case 'u':
584 add_unique = 1;
585 break;
586 case 'i':
587 add_only = 1;
588 break;
ab7d5fc6
MD
589 }
590 }
591
6d5c0ca9
MD
592 /* Check if hash size is power of 2 */
593 if (init_hash_size && init_hash_size & (init_hash_size - 1)) {
594 printf("Error: Hash table size %lu is not a power of 2.\n",
595 init_hash_size);
596 return -1;
597 }
598
ab7d5fc6
MD
599 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
600 duration, nr_readers, nr_writers);
601 printf_verbose("Writer delay : %lu loops.\n", wdelay);
602 printf_verbose("Reader duration : %lu loops.\n", rduration);
6d5c0ca9
MD
603 printf_verbose("Random pool size : %lu.\n", rand_pool);
604 printf_verbose("Mode:%s%s.\n",
605 add_only ? " add only" : " add/remove",
606 add_unique ? " uniquify" : "");
607 printf_verbose("Initial hash table size: %lu buckets.\n", init_hash_size);
ab7d5fc6
MD
608 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
609 "main", pthread_self(), (unsigned long)gettid());
610
ab7d5fc6
MD
611 tid_reader = malloc(sizeof(*tid_reader) * nr_readers);
612 tid_writer = malloc(sizeof(*tid_writer) * nr_writers);
613 count_reader = malloc(sizeof(*count_reader) * nr_readers);
614 count_writer = malloc(sizeof(*count_writer) * nr_writers);
14044b37 615 test_ht = cds_lfht_new(test_hash, test_compare, 0x42UL,
6d5c0ca9 616 init_hash_size, call_rcu);
18117871 617
14756542
MD
618 err = create_all_cpu_call_rcu_data(0);
619 assert(!err);
620
ab7d5fc6
MD
621 next_aff = 0;
622
623 for (i = 0; i < nr_readers; i++) {
624 err = pthread_create(&tid_reader[i], NULL, thr_reader,
625 &count_reader[i]);
626 if (err != 0)
627 exit(1);
628 }
629 for (i = 0; i < nr_writers; i++) {
630 err = pthread_create(&tid_writer[i], NULL, thr_writer,
631 &count_writer[i]);
632 if (err != 0)
633 exit(1);
634 }
635
abc490a1 636 cmm_smp_mb();
ab7d5fc6
MD
637
638 test_go = 1;
639
640 sleep(duration);
641
642 test_stop = 1;
643
644 for (i = 0; i < nr_readers; i++) {
645 err = pthread_join(tid_reader[i], &tret);
646 if (err != 0)
647 exit(1);
648 tot_reads += count_reader[i];
649 }
650 for (i = 0; i < nr_writers; i++) {
651 err = pthread_join(tid_writer[i], &tret);
652 if (err != 0)
653 exit(1);
b8e1907c
MD
654 tot_writes += count_writer[i].update_ops;
655 tot_add += count_writer[i].add;
3c16bf4b 656 tot_add_exist += count_writer[i].add_exist;
b8e1907c 657 tot_remove += count_writer[i].remove;
ab7d5fc6 658 }
33c7c748
MD
659 printf("Counting nodes... ");
660 fflush(stdout);
14044b37 661 cds_lfht_count_nodes(test_ht, &count, &removed);
33c7c748 662 printf("done.\n");
b8e1907c 663 if (count || removed)
273399de
MD
664 printf("WARNING: nodes left in the hash table upon destroy: "
665 "%lu nodes + %lu logically removed.\n", count, removed);
14044b37 666 ret = cds_lfht_destroy(test_ht);
f000907d 667
33c7c748
MD
668 if (ret)
669 printf_verbose("final delete aborted\n");
670 else
671 printf_verbose("final delete success\n");
ab7d5fc6
MD
672 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
673 tot_writes);
674 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu "
675 "nr_writers %3u "
6d5c0ca9 676 "wdelay %6lu rand_pool %12llu nr_reads %12llu nr_writes %12llu nr_ops %12llu "
3c16bf4b 677 "nr_add %12llu nr_add_fail %12llu nr_remove %12llu nr_leaked %12llu\n",
ab7d5fc6 678 argv[0], duration, nr_readers, rduration,
6d5c0ca9 679 nr_writers, wdelay, rand_pool, tot_reads, tot_writes,
3c16bf4b 680 tot_reads + tot_writes, tot_add, tot_add_exist, tot_remove,
b8e1907c 681 tot_add - tot_remove - count);
ab7d5fc6
MD
682 free(tid_reader);
683 free(tid_writer);
684 free(count_reader);
685 free(count_writer);
686 return 0;
687}
This page took 0.050585 seconds and 4 git commands to generate.