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