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