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