Hash table test: FreeBSD compatibility fix
[urcu.git] / tests / test_urcu_hash.c
CommitLineData
ab7d5fc6 1/*
cd1ae16a 2 * test_urcu_hash.c
ab7d5fc6
MD
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
fb6d173d 24#include "../config.h"
ab7d5fc6
MD
25#include <stdio.h>
26#include <pthread.h>
27#include <stdlib.h>
28#include <string.h>
29#include <sys/types.h>
30#include <sys/wait.h>
31#include <unistd.h>
32#include <stdio.h>
33#include <assert.h>
ab7d5fc6 34#include <sched.h>
5e28c532 35#include <errno.h>
fb6d173d 36#include <signal.h>
ab7d5fc6 37
abc490a1
MD
38#ifdef __linux__
39#include <syscall.h>
40#endif
ab7d5fc6 41
6d5c0ca9 42#define DEFAULT_HASH_SIZE 32
32becef6 43#define DEFAULT_MIN_ALLOC_SIZE 1
6d5c0ca9 44#define DEFAULT_RAND_POOL 1000000
5e28c532 45
c9582f15
MD
46/*
47 * Note: the hash seed should be a random value for hash tables
48 * targeting production environments to provide protection against
49 * denial of service attacks. We keep it a static value within this test
50 * program to compare identical benchmark runs.
51 */
0422d92c
MD
52#define TEST_HASH_SEED 0x42UL
53
ab7d5fc6
MD
54/* Make this big enough to include the POWER5+ L3 cacheline size of 256B */
55#define CACHE_LINE_SIZE 4096
56
57/* hardcoded number of CPUs */
58#define NR_CPUS 16384
59
98808fb1
MD
60#ifdef POISON_FREE
61#define poison_free(ptr) \
62 do { \
63 memset(ptr, 0x42, sizeof(*(ptr))); \
64 free(ptr); \
65 } while (0)
66#else
67#define poison_free(ptr) free(ptr)
68#endif
69
70
71
ab7d5fc6
MD
72#if defined(_syscall0)
73_syscall0(pid_t, gettid)
74#elif defined(__NR_gettid)
75static inline pid_t gettid(void)
76{
77 return syscall(__NR_gettid);
78}
79#else
80#warning "use pid as tid"
81static inline pid_t gettid(void)
82{
83 return getpid();
84}
85#endif
86
87#ifndef DYNAMIC_LINK_TEST
88#define _LGPL_SOURCE
89#else
90#define debug_yield_read()
91#endif
5567839e 92#include <urcu-qsbr.h>
abc490a1
MD
93#include <urcu/rculfhash.h>
94#include <urcu-call-rcu.h>
ab7d5fc6 95
b8e1907c
MD
96struct wr_count {
97 unsigned long update_ops;
98 unsigned long add;
3c16bf4b 99 unsigned long add_exist;
b8e1907c
MD
100 unsigned long remove;
101};
102
5e28c532
MD
103static unsigned int __thread rand_lookup;
104static unsigned long __thread nr_add;
105static unsigned long __thread nr_addexist;
106static unsigned long __thread nr_del;
107static unsigned long __thread nr_delnoent;
108static unsigned long __thread lookup_fail;
109static unsigned long __thread lookup_ok;
110
14044b37 111static struct cds_lfht *test_ht;
ab7d5fc6 112
5e28c532 113struct test_data {
ab7d5fc6 114 int a;
5e28c532 115 int b;
ab7d5fc6
MD
116};
117
3c692076
LJ
118struct lfht_test_node {
119 struct cds_lfht_node node;
04db56f8
LJ
120 void *key;
121 unsigned int key_len;
81d91005
LJ
122 /* cache-cold for iteration */
123 struct rcu_head head;
3c692076
LJ
124};
125
126static inline struct lfht_test_node *
127to_test_node(struct cds_lfht_node *node)
128{
129 return caa_container_of(node, struct lfht_test_node, node);
130}
131
132static inline
133void lfht_test_node_init(struct lfht_test_node *node, void *key,
134 size_t key_len)
135{
04db56f8
LJ
136 cds_lfht_node_init(&node->node);
137 node->key = key;
138 node->key_len = key_len;
3c692076
LJ
139}
140
141static inline struct lfht_test_node *
142cds_lfht_iter_get_test_node(struct cds_lfht_iter *iter)
143{
144 return to_test_node(cds_lfht_iter_get_node(iter));
145}
146
ab7d5fc6
MD
147static volatile int test_go, test_stop;
148
149static unsigned long wdelay;
150
ab7d5fc6
MD
151static unsigned long duration;
152
153/* read-side C.S. duration, in loops */
154static unsigned long rduration;
155
6d5c0ca9 156static unsigned long init_hash_size = DEFAULT_HASH_SIZE;
32becef6 157static unsigned long min_hash_alloc_size = DEFAULT_MIN_ALLOC_SIZE;
b99436d7 158static unsigned long max_hash_buckets_size = (1UL << 20);
cd1ae16a 159static unsigned long init_populate;
151e7a93 160static int opt_auto_resize;
48ed1c18 161static int add_only, add_unique, add_replace;
c0b8a865 162static const struct cds_lfht_mm_type *memory_backend;
6d5c0ca9 163
8008a032 164static unsigned long init_pool_offset, lookup_pool_offset, write_pool_offset;
d837911d
MD
165static unsigned long init_pool_size = DEFAULT_RAND_POOL,
166 lookup_pool_size = DEFAULT_RAND_POOL,
167 write_pool_size = DEFAULT_RAND_POOL;
59b10634 168static int validate_lookup;
8008a032 169
7ed7682f
MD
170static int count_pipe[2];
171
ab7d5fc6
MD
172static inline void loop_sleep(unsigned long l)
173{
174 while(l-- != 0)
abc490a1 175 caa_cpu_relax();
ab7d5fc6
MD
176}
177
178static int verbose_mode;
179
180#define printf_verbose(fmt, args...) \
181 do { \
182 if (verbose_mode) \
33c7c748 183 printf(fmt, ## args); \
ab7d5fc6
MD
184 } while (0)
185
186static unsigned int cpu_affinities[NR_CPUS];
187static unsigned int next_aff = 0;
188static int use_affinity = 0;
189
190pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
191
fb6d173d
MD
192#ifndef HAVE_CPU_SET_T
193typedef unsigned long cpu_set_t;
194# define CPU_ZERO(cpuset) do { *(cpuset) = 0; } while(0)
195# define CPU_SET(cpu, cpuset) do { *(cpuset) |= (1UL << (cpu)); } while(0)
196#endif
197
ab7d5fc6
MD
198static void set_affinity(void)
199{
200 cpu_set_t mask;
201 int cpu;
202 int ret;
203
204 if (!use_affinity)
205 return;
206
fb6d173d 207#if HAVE_SCHED_SETAFFINITY
ab7d5fc6
MD
208 ret = pthread_mutex_lock(&affinity_mutex);
209 if (ret) {
210 perror("Error in pthread mutex lock");
211 exit(-1);
212 }
213 cpu = cpu_affinities[next_aff++];
214 ret = pthread_mutex_unlock(&affinity_mutex);
215 if (ret) {
216 perror("Error in pthread mutex unlock");
217 exit(-1);
218 }
219 CPU_ZERO(&mask);
220 CPU_SET(cpu, &mask);
fb6d173d
MD
221#if SCHED_SETAFFINITY_ARGS == 2
222 sched_setaffinity(0, &mask);
223#else
224 sched_setaffinity(0, sizeof(mask), &mask);
225#endif
226#endif /* HAVE_SCHED_SETAFFINITY */
ab7d5fc6
MD
227}
228
3967a8a8
MD
229static enum {
230 AR_RANDOM = 0,
231 AR_ADD = 1,
232 AR_REMOVE = -1,
233} addremove; /* 1: add, -1 remove, 0: random */
234
235static
236void sigusr1_handler(int signo)
237{
238 switch (addremove) {
239 case AR_ADD:
240 printf("Add/Remove: random.\n");
241 addremove = AR_RANDOM;
242 break;
243 case AR_RANDOM:
244 printf("Add/Remove: remove only.\n");
245 addremove = AR_REMOVE;
246 break;
247 case AR_REMOVE:
248 printf("Add/Remove: add only.\n");
249 addremove = AR_ADD;
250 break;
251 }
252}
253
973e5e1b
MD
254static
255void sigusr2_handler(int signo)
256{
7ed7682f 257 char msg[1] = { 0x42 };
3fb1173c
MD
258 ssize_t ret;
259
260 do {
261 ret = write(count_pipe[1], msg, 1); /* wakeup thread */
262 } while (ret == -1L && errno == EINTR);
973e5e1b
MD
263}
264
ab7d5fc6
MD
265/*
266 * returns 0 if test should end.
267 */
268static int test_duration_write(void)
269{
270 return !test_stop;
271}
272
273static int test_duration_read(void)
274{
275 return !test_stop;
276}
277
278static unsigned long long __thread nr_writes;
279static unsigned long long __thread nr_reads;
280
281static unsigned int nr_readers;
282static unsigned int nr_writers;
283
284pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
285
286void rcu_copy_mutex_lock(void)
287{
288 int ret;
289 ret = pthread_mutex_lock(&rcu_copy_mutex);
290 if (ret) {
291 perror("Error in pthread mutex lock");
292 exit(-1);
293 }
294}
295
296void rcu_copy_mutex_unlock(void)
297{
298 int ret;
299
300 ret = pthread_mutex_unlock(&rcu_copy_mutex);
301 if (ret) {
302 perror("Error in pthread mutex unlock");
303 exit(-1);
304 }
305}
306
732ad076
MD
307/*
308 * Hash function
309 * Source: http://burtleburtle.net/bob/c/lookup3.c
310 * Originally Public Domain
311 */
312
313#define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
314
315#define mix(a, b, c) \
316do { \
317 a -= c; a ^= rot(c, 4); c += b; \
318 b -= a; b ^= rot(a, 6); a += c; \
319 c -= b; c ^= rot(b, 8); b += a; \
320 a -= c; a ^= rot(c, 16); c += b; \
321 b -= a; b ^= rot(a, 19); a += c; \
322 c -= b; c ^= rot(b, 4); b += a; \
323} while (0)
324
325#define final(a, b, c) \
326{ \
327 c ^= b; c -= rot(b, 14); \
328 a ^= c; a -= rot(c, 11); \
329 b ^= a; b -= rot(a, 25); \
330 c ^= b; c -= rot(b, 16); \
331 a ^= c; a -= rot(c, 4);\
332 b ^= a; b -= rot(a, 14); \
333 c ^= b; c -= rot(b, 24); \
334}
335
336static __attribute__((unused))
337uint32_t hash_u32(
338 const uint32_t *k, /* the key, an array of uint32_t values */
339 size_t length, /* the length of the key, in uint32_ts */
340 uint32_t initval) /* the previous hash, or an arbitrary value */
341{
342 uint32_t a, b, c;
343
344 /* Set up the internal state */
345 a = b = c = 0xdeadbeef + (((uint32_t) length) << 2) + initval;
346
347 /*----------------------------------------- handle most of the key */
348 while (length > 3) {
349 a += k[0];
350 b += k[1];
351 c += k[2];
352 mix(a, b, c);
353 length -= 3;
354 k += 3;
355 }
356
357 /*----------------------------------- handle the last 3 uint32_t's */
358 switch (length) { /* all the case statements fall through */
359 case 3: c += k[2];
360 case 2: b += k[1];
361 case 1: a += k[0];
362 final(a, b, c);
363 case 0: /* case 0: nothing left to add */
364 break;
365 }
366 /*---------------------------------------------- report the result */
367 return c;
368}
369
370static
371void hashword2(
372 const uint32_t *k, /* the key, an array of uint32_t values */
373 size_t length, /* the length of the key, in uint32_ts */
374 uint32_t *pc, /* IN: seed OUT: primary hash value */
375 uint32_t *pb) /* IN: more seed OUT: secondary hash value */
376{
377 uint32_t a, b, c;
378
379 /* Set up the internal state */
380 a = b = c = 0xdeadbeef + ((uint32_t) (length << 2)) + *pc;
381 c += *pb;
382
383 /*----------------------------------------- handle most of the key */
384 while (length > 3) {
385 a += k[0];
386 b += k[1];
387 c += k[2];
388 mix(a, b, c);
389 length -= 3;
390 k += 3;
391 }
392
393 /*----------------------------------- handle the last 3 uint32_t's */
394 switch (length) { /* all the case statements fall through */
395 case 3: c += k[2];
396 case 2: b += k[1];
397 case 1: a += k[0];
398 final(a, b, c);
399 case 0: /* case 0: nothing left to add */
400 break;
401 }
402 /*---------------------------------------------- report the result */
403 *pc = c;
404 *pb = b;
405}
406
407#if (CAA_BITS_PER_LONG == 32)
abc490a1 408static
996ff57c 409unsigned long test_hash(const void *_key, size_t length, unsigned long seed)
abc490a1 410{
f542a7ee 411 unsigned int key = (unsigned int) _key;
abc490a1 412
f542a7ee 413 assert(length == sizeof(unsigned int));
bda21126 414 return hash_u32(&key, 1, seed);
732ad076
MD
415}
416#else
417static
996ff57c 418unsigned long test_hash(const void *_key, size_t length, unsigned long seed)
732ad076
MD
419{
420 union {
421 uint64_t v64;
422 uint32_t v32[2];
423 } v;
424 union {
425 uint64_t v64;
426 uint32_t v32[2];
427 } key;
428
429 assert(length == sizeof(unsigned long));
430 v.v64 = (uint64_t) seed;
431 key.v64 = (uint64_t) _key;
432 hashword2(key.v32, 2, &v.v32[0], &v.v32[1]);
433 return v.v64;
434}
435#endif
abc490a1 436
732ad076 437static
996ff57c
MD
438unsigned long test_compare(const void *key1, size_t key1_len,
439 const void *key2, size_t key2_len)
732ad076 440{
8ed51e04 441 if (caa_unlikely(key1_len != key2_len))
732ad076
MD
442 return -1;
443 assert(key1_len == sizeof(unsigned long));
444 if (key1 == key2)
445 return 0;
446 else
447 return 1;
abc490a1 448}
ab7d5fc6 449
0422d92c 450static
996ff57c 451int test_match(struct cds_lfht_node *node, const void *key)
0422d92c 452{
04db56f8
LJ
453 struct lfht_test_node *test_node = to_test_node(node);
454
455 return !test_compare(test_node->key, test_node->key_len,
456 key, sizeof(unsigned long));
0422d92c
MD
457}
458
b99436d7 459static inline
0422d92c
MD
460void cds_lfht_test_lookup(struct cds_lfht *ht, void *key, size_t key_len,
461 struct cds_lfht_iter *iter)
462{
463 assert(key_len == sizeof(unsigned long));
464
6f554439
LJ
465 cds_lfht_lookup(ht, test_hash(key, key_len, TEST_HASH_SEED),
466 test_match, key, iter);
0422d92c
MD
467}
468
7ed7682f
MD
469void *thr_count(void *arg)
470{
471 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
472 "counter", pthread_self(), (unsigned long)gettid());
473
474 rcu_register_thread();
475
476 for (;;) {
caf3653d 477 unsigned long count;
d933dd0e 478 long approx_before, approx_after;
7ed7682f
MD
479 ssize_t len;
480 char buf[1];
481
482 rcu_thread_offline();
483 len = read(count_pipe[0], buf, 1);
484 rcu_thread_online();
8ed51e04 485 if (caa_unlikely(!test_duration_read()))
7ed7682f
MD
486 break;
487 if (len != 1)
488 continue;
489 /* Accounting */
490 printf("Counting nodes... ");
491 fflush(stdout);
492 rcu_read_lock();
caf3653d 493 cds_lfht_count_nodes(test_ht, &approx_before, &count,
7ed7682f
MD
494 &approx_after);
495 rcu_read_unlock();
496 printf("done.\n");
d933dd0e 497 printf("Approximation before node accounting: %ld nodes.\n",
7ed7682f
MD
498 approx_before);
499 printf("Accounting of nodes in the hash table: "
caf3653d
MD
500 "%lu nodes.\n",
501 count);
d933dd0e 502 printf("Approximation after node accounting: %ld nodes.\n",
7ed7682f
MD
503 approx_after);
504 }
505 rcu_unregister_thread();
506 return NULL;
507}
508
ab7d5fc6
MD
509void *thr_reader(void *_count)
510{
511 unsigned long long *count = _count;
3c692076 512 struct lfht_test_node *node;
adc0de68 513 struct cds_lfht_iter iter;
ab7d5fc6
MD
514
515 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
516 "reader", pthread_self(), (unsigned long)gettid());
517
518 set_affinity();
519
520 rcu_register_thread();
521
522 while (!test_go)
523 {
524 }
abc490a1 525 cmm_smp_mb();
ab7d5fc6
MD
526
527 for (;;) {
528 rcu_read_lock();
0422d92c 529 cds_lfht_test_lookup(test_ht,
c8f5b384 530 (void *)(((unsigned long) rand_r(&rand_lookup) % lookup_pool_size) + lookup_pool_offset),
adc0de68 531 sizeof(void *), &iter);
3c692076 532 node = cds_lfht_iter_get_test_node(&iter);
59b10634
MD
533 if (node == NULL) {
534 if (validate_lookup) {
535 printf("[ERROR] Lookup cannot find initial node.\n");
46834ba6 536 exit(-1);
59b10634 537 }
5e28c532 538 lookup_fail++;
59b10634 539 } else {
5e28c532 540 lookup_ok++;
59b10634 541 }
ab7d5fc6 542 debug_yield_read();
8ed51e04 543 if (caa_unlikely(rduration))
ab7d5fc6
MD
544 loop_sleep(rduration);
545 rcu_read_unlock();
546 nr_reads++;
8ed51e04 547 if (caa_unlikely(!test_duration_read()))
ab7d5fc6 548 break;
8ed51e04 549 if (caa_unlikely((nr_reads & ((1 << 10) - 1)) == 0))
5567839e 550 rcu_quiescent_state();
ab7d5fc6
MD
551 }
552
553 rcu_unregister_thread();
554
555 *count = nr_reads;
556 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
557 "reader", pthread_self(), (unsigned long)gettid());
5e28c532
MD
558 printf_verbose("readid : %lx, lookupfail %lu, lookupok %lu\n",
559 pthread_self(), lookup_fail, lookup_ok);
ab7d5fc6
MD
560 return ((void*)1);
561
562}
563
abc490a1
MD
564static
565void free_node_cb(struct rcu_head *head)
566{
3c692076 567 struct lfht_test_node *node =
81d91005 568 caa_container_of(head, struct lfht_test_node, head);
abc490a1
MD
569 free(node);
570}
571
ab7d5fc6
MD
572void *thr_writer(void *_count)
573{
3c692076
LJ
574 struct lfht_test_node *node;
575 struct cds_lfht_node *ret_node;
adc0de68 576 struct cds_lfht_iter iter;
b8e1907c 577 struct wr_count *count = _count;
5e28c532 578 int ret;
ab7d5fc6
MD
579
580 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
581 "writer", pthread_self(), (unsigned long)gettid());
582
583 set_affinity();
584
5e28c532 585 rcu_register_thread();
5e28c532 586
ab7d5fc6
MD
587 while (!test_go)
588 {
589 }
abc490a1 590 cmm_smp_mb();
ab7d5fc6
MD
591
592 for (;;) {
3967a8a8
MD
593 if ((addremove == AR_ADD || add_only)
594 || (addremove == AR_RANDOM && rand_r(&rand_lookup) & 1)) {
3c692076
LJ
595 node = malloc(sizeof(struct lfht_test_node));
596 lfht_test_node_init(node,
c8f5b384 597 (void *)(((unsigned long) rand_r(&rand_lookup) % write_pool_size) + write_pool_offset),
cb233752 598 sizeof(void *));
6b262fd7 599 rcu_read_lock();
48ed1c18 600 if (add_unique) {
6f554439 601 ret_node = cds_lfht_add_unique(test_ht,
04db56f8 602 test_hash(node->key, node->key_len, TEST_HASH_SEED),
6f554439 603 test_match, node->key, &node->node);
48ed1c18
MD
604 } else {
605 if (add_replace)
6f554439 606 ret_node = cds_lfht_add_replace(test_ht,
04db56f8 607 test_hash(node->key, node->key_len, TEST_HASH_SEED),
6f554439 608 test_match, node->key, &node->node);
48ed1c18 609 else
0422d92c 610 cds_lfht_add(test_ht,
04db56f8 611 test_hash(node->key, node->key_len, TEST_HASH_SEED),
0422d92c 612 &node->node);
48ed1c18 613 }
abc490a1 614 rcu_read_unlock();
3c692076 615 if (add_unique && ret_node != &node->node) {
742aa1db 616 free(node);
3eca1b8c 617 nr_addexist++;
48ed1c18
MD
618 } else {
619 if (add_replace && ret_node) {
81d91005 620 call_rcu(&to_test_node(ret_node)->head,
3c692076 621 free_node_cb);
48ed1c18
MD
622 nr_addexist++;
623 } else {
624 nr_add++;
625 }
626 }
5e28c532
MD
627 } else {
628 /* May delete */
abc490a1 629 rcu_read_lock();
0422d92c 630 cds_lfht_test_lookup(test_ht,
c8f5b384 631 (void *)(((unsigned long) rand_r(&rand_lookup) % write_pool_size) + write_pool_offset),
adc0de68 632 sizeof(void *), &iter);
bc8c3c74 633 ret = cds_lfht_del(test_ht, cds_lfht_iter_get_node(&iter));
abc490a1
MD
634 rcu_read_unlock();
635 if (ret == 0) {
3c692076 636 node = cds_lfht_iter_get_test_node(&iter);
81d91005 637 call_rcu(&node->head, free_node_cb);
5e28c532 638 nr_del++;
abc490a1
MD
639 } else
640 nr_delnoent++;
44395fb7 641 }
abc490a1 642#if 0
44395fb7
MD
643 //if (nr_writes % 100000 == 0) {
644 if (nr_writes % 1000 == 0) {
abc490a1 645 rcu_read_lock();
44395fb7
MD
646 if (rand_r(&rand_lookup) & 1) {
647 ht_resize(test_ht, 1);
648 } else {
649 ht_resize(test_ht, -1);
650 }
abc490a1 651 rcu_read_unlock();
5e28c532 652 }
abc490a1 653#endif //0
ab7d5fc6 654 nr_writes++;
8ed51e04 655 if (caa_unlikely(!test_duration_write()))
ab7d5fc6 656 break;
8ed51e04 657 if (caa_unlikely(wdelay))
ab7d5fc6 658 loop_sleep(wdelay);
8ed51e04 659 if (caa_unlikely((nr_writes & ((1 << 10) - 1)) == 0))
5567839e 660 rcu_quiescent_state();
ab7d5fc6
MD
661 }
662
5e28c532
MD
663 rcu_unregister_thread();
664
ab7d5fc6
MD
665 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
666 "writer", pthread_self(), (unsigned long)gettid());
5e28c532
MD
667 printf_verbose("info id %lx: nr_add %lu, nr_addexist %lu, nr_del %lu, "
668 "nr_delnoent %lu\n", pthread_self(), nr_add,
669 nr_addexist, nr_del, nr_delnoent);
b8e1907c
MD
670 count->update_ops = nr_writes;
671 count->add = nr_add;
3c16bf4b 672 count->add_exist = nr_addexist;
b8e1907c 673 count->remove = nr_del;
ab7d5fc6
MD
674 return ((void*)2);
675}
676
5dc45f25 677static int populate_hash(void)
cd1ae16a 678{
3c692076
LJ
679 struct lfht_test_node *node;
680 struct cds_lfht_node *ret_node;
cd1ae16a
MD
681
682 if (!init_populate)
5dc45f25
MD
683 return 0;
684
48ed1c18 685 if ((add_unique || add_replace) && init_populate * 10 > init_pool_size) {
5dc45f25 686 printf("WARNING: required to populate %lu nodes (-k), but random "
48ed1c18 687"pool is quite small (%lu values) and we are in add_unique (-u) or add_replace (-s) mode. Try with a "
d837911d 688"larger random pool (-p option). This may take a while...\n", init_populate, init_pool_size);
5dc45f25 689 }
cd1ae16a
MD
690
691 while (nr_add < init_populate) {
3c692076
LJ
692 node = malloc(sizeof(struct lfht_test_node));
693 lfht_test_node_init(node,
c8f5b384 694 (void *)(((unsigned long) rand_r(&rand_lookup) % init_pool_size) + init_pool_offset),
cd1ae16a 695 sizeof(void *));
48ed1c18
MD
696 rcu_read_lock();
697 if (add_unique) {
6f554439 698 ret_node = cds_lfht_add_unique(test_ht,
04db56f8 699 test_hash(node->key, node->key_len, TEST_HASH_SEED),
6f554439 700 test_match, node->key, &node->node);
48ed1c18
MD
701 } else {
702 if (add_replace)
6f554439 703 ret_node = cds_lfht_add_replace(test_ht,
04db56f8 704 test_hash(node->key, node->key_len, TEST_HASH_SEED),
6f554439 705 test_match, node->key, &node->node);
48ed1c18 706 else
0422d92c 707 cds_lfht_add(test_ht,
04db56f8 708 test_hash(node->key, node->key_len, TEST_HASH_SEED),
0422d92c 709 &node->node);
48ed1c18
MD
710 }
711 rcu_read_unlock();
3c692076 712 if (add_unique && ret_node != &node->node) {
cd1ae16a
MD
713 free(node);
714 nr_addexist++;
48ed1c18
MD
715 } else {
716 if (add_replace && ret_node) {
81d91005 717 call_rcu(&to_test_node(ret_node)->head, free_node_cb);
48ed1c18
MD
718 nr_addexist++;
719 } else {
720 nr_add++;
721 }
722 }
cd1ae16a
MD
723 nr_writes++;
724 }
5dc45f25 725 return 0;
cd1ae16a
MD
726}
727
175ec0eb
MD
728static
729void test_delete_all_nodes(struct cds_lfht *ht)
730{
731 struct cds_lfht_iter iter;
3c692076 732 struct lfht_test_node *node;
175ec0eb
MD
733 unsigned long count = 0;
734
6d320126 735 cds_lfht_for_each_entry(ht, &iter, node, node) {
175ec0eb
MD
736 int ret;
737
bc8c3c74 738 ret = cds_lfht_del(test_ht, cds_lfht_iter_get_node(&iter));
175ec0eb 739 assert(!ret);
81d91005 740 call_rcu(&node->head, free_node_cb);
175ec0eb
MD
741 count++;
742 }
743 printf("deleted %lu nodes.\n", count);
744}
745
ab7d5fc6
MD
746void show_usage(int argc, char **argv)
747{
48ed1c18 748 printf("Usage : %s nr_readers nr_writers duration (s)\n", argv[0]);
ab7d5fc6 749#ifdef DEBUG_YIELD
48ed1c18 750 printf(" [-r] [-w] (yield reader and/or writer)\n");
ab7d5fc6 751#endif
48ed1c18
MD
752 printf(" [-d delay] (writer period (us))\n");
753 printf(" [-c duration] (reader C.S. duration (in loops))\n");
754 printf(" [-v] (verbose output)\n");
755 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
b99436d7
LJ
756 printf(" [-h size] (initial number of buckets)\n");
757 printf(" [-m size] (minimum number of allocated buckets)\n");
758 printf(" [-n size] (maximum number of buckets)\n");
48ed1c18
MD
759 printf(" [not -u nor -s] Add entries (supports redundant keys).\n");
760 printf(" [-u] Uniquify add (no redundant keys).\n");
761 printf(" [-s] Replace (swap) entries.\n");
762 printf(" [-i] Add only (no removal).\n");
763 printf(" [-k nr_nodes] Number of nodes to insert initially.\n");
764 printf(" [-A] Automatically resize hash table.\n");
c0b8a865 765 printf(" [-B order|chunk|mmap] Specify the memory backend.\n");
48ed1c18
MD
766 printf(" [-R offset] Lookup pool offset.\n");
767 printf(" [-S offset] Write pool offset.\n");
768 printf(" [-T offset] Init pool offset.\n");
769 printf(" [-M size] Lookup pool size.\n");
770 printf(" [-N size] Write pool size.\n");
771 printf(" [-O size] Init pool size.\n");
772 printf(" [-V] Validate lookups of init values (use with filled init pool, same lookup range, with different write range).\n");
773 printf("\n\n");
ab7d5fc6
MD
774}
775
776int main(int argc, char **argv)
777{
778 int err;
779 pthread_t *tid_reader, *tid_writer;
7ed7682f 780 pthread_t tid_count;
ab7d5fc6 781 void *tret;
b8e1907c
MD
782 unsigned long long *count_reader;
783 struct wr_count *count_writer;
784 unsigned long long tot_reads = 0, tot_writes = 0,
3c16bf4b 785 tot_add = 0, tot_add_exist = 0, tot_remove = 0;
caf3653d 786 unsigned long count;
d933dd0e 787 long approx_before, approx_after;
5e28c532 788 int i, a, ret;
3967a8a8
MD
789 struct sigaction act;
790 unsigned int remain;
ab7d5fc6
MD
791
792 if (argc < 4) {
793 show_usage(argc, argv);
794 return -1;
795 }
796
797 err = sscanf(argv[1], "%u", &nr_readers);
798 if (err != 1) {
799 show_usage(argc, argv);
800 return -1;
801 }
802
803 err = sscanf(argv[2], "%u", &nr_writers);
804 if (err != 1) {
805 show_usage(argc, argv);
806 return -1;
807 }
808
809 err = sscanf(argv[3], "%lu", &duration);
810 if (err != 1) {
811 show_usage(argc, argv);
812 return -1;
813 }
814
815 for (i = 4; i < argc; i++) {
816 if (argv[i][0] != '-')
817 continue;
818 switch (argv[i][1]) {
819#ifdef DEBUG_YIELD
820 case 'r':
821 yield_active |= YIELD_READ;
822 break;
823 case 'w':
824 yield_active |= YIELD_WRITE;
825 break;
826#endif
827 case 'a':
828 if (argc < i + 2) {
829 show_usage(argc, argv);
830 return -1;
831 }
832 a = atoi(argv[++i]);
833 cpu_affinities[next_aff++] = a;
834 use_affinity = 1;
835 printf_verbose("Adding CPU %d affinity\n", a);
836 break;
837 case 'c':
838 if (argc < i + 2) {
839 show_usage(argc, argv);
840 return -1;
841 }
842 rduration = atol(argv[++i]);
843 break;
844 case 'd':
845 if (argc < i + 2) {
846 show_usage(argc, argv);
847 return -1;
848 }
849 wdelay = atol(argv[++i]);
850 break;
851 case 'v':
852 verbose_mode = 1;
853 break;
6d5c0ca9
MD
854 case 'h':
855 if (argc < i + 2) {
856 show_usage(argc, argv);
857 return -1;
858 }
859 init_hash_size = atol(argv[++i]);
860 break;
32becef6
LJ
861 case 'm':
862 if (argc < i + 2) {
863 show_usage(argc, argv);
864 return -1;
865 }
866 min_hash_alloc_size = atol(argv[++i]);
867 break;
b99436d7
LJ
868 case 'n':
869 if (argc < i + 2) {
870 show_usage(argc, argv);
871 return -1;
872 }
873 max_hash_buckets_size = atol(argv[++i]);
874 break;
6d5c0ca9 875 case 'u':
48ed1c18
MD
876 if (add_replace) {
877 printf("Please specify at most one of -s or -u.\n");
878 exit(-1);
879 }
6d5c0ca9
MD
880 add_unique = 1;
881 break;
48ed1c18
MD
882 case 's':
883 if (add_unique) {
884 printf("Please specify at most one of -s or -u.\n");
885 exit(-1);
886 }
887 add_replace = 1;
888 break;
6d5c0ca9
MD
889 case 'i':
890 add_only = 1;
891 break;
cd1ae16a
MD
892 case 'k':
893 init_populate = atol(argv[++i]);
894 break;
151e7a93
MD
895 case 'A':
896 opt_auto_resize = 1;
897 break;
c0b8a865
LJ
898 case 'B':
899 if (argc < i + 2) {
900 show_usage(argc, argv);
901 return -1;
902 }
903 i++;
904 if (!strcmp("order", argv[i]))
905 memory_backend = &cds_lfht_mm_order;
906 else if (!strcmp("chunk", argv[i]))
907 memory_backend = &cds_lfht_mm_chunk;
908 else if (!strcmp("mmap", argv[i]))
909 memory_backend = &cds_lfht_mm_mmap;
910 else {
911 printf("Please specify memory backend with order|chunk|mmap.\n");
912 exit(-1);
913 }
914 break;
8008a032
MD
915 case 'R':
916 lookup_pool_offset = atol(argv[++i]);
917 break;
918 case 'S':
919 write_pool_offset = atol(argv[++i]);
920 break;
921 case 'T':
922 init_pool_offset = atol(argv[++i]);
923 break;
d837911d
MD
924 case 'M':
925 lookup_pool_size = atol(argv[++i]);
926 break;
927 case 'N':
928 write_pool_size = atol(argv[++i]);
929 break;
930 case 'O':
931 init_pool_size = atol(argv[++i]);
932 break;
59b10634
MD
933 case 'V':
934 validate_lookup = 1;
935 break;
8008a032 936
ab7d5fc6
MD
937 }
938 }
939
6d5c0ca9
MD
940 /* Check if hash size is power of 2 */
941 if (init_hash_size && init_hash_size & (init_hash_size - 1)) {
b99436d7 942 printf("Error: Initial number of buckets (%lu) is not a power of 2.\n",
6d5c0ca9
MD
943 init_hash_size);
944 return -1;
945 }
946
d0d8f9aa 947 if (min_hash_alloc_size && min_hash_alloc_size & (min_hash_alloc_size - 1)) {
b99436d7 948 printf("Error: Minimum number of allocated buckets (%lu) is not a power of 2.\n",
32becef6
LJ
949 min_hash_alloc_size);
950 return -1;
951 }
952
b99436d7
LJ
953 if (max_hash_buckets_size && max_hash_buckets_size & (max_hash_buckets_size - 1)) {
954 printf("Error: Maximum number of buckets (%lu) is not a power of 2.\n",
955 max_hash_buckets_size);
956 return -1;
957 }
958
3967a8a8
MD
959 memset(&act, 0, sizeof(act));
960 ret = sigemptyset(&act.sa_mask);
961 if (ret == -1) {
962 perror("sigemptyset");
963 return -1;
964 }
965 act.sa_handler = sigusr1_handler;
966 act.sa_flags = SA_RESTART;
967 ret = sigaction(SIGUSR1, &act, NULL);
968 if (ret == -1) {
969 perror("sigaction");
970 return -1;
971 }
7ed7682f
MD
972
973 ret = pipe(count_pipe);
974 if (ret == -1) {
975 perror("pipe");
976 return -1;
977 }
978
979 /* spawn counter thread */
980 err = pthread_create(&tid_count, NULL, thr_count,
981 NULL);
982 if (err != 0)
983 exit(1);
984
973e5e1b
MD
985 act.sa_handler = sigusr2_handler;
986 act.sa_flags = SA_RESTART;
987 ret = sigaction(SIGUSR2, &act, NULL);
988 if (ret == -1) {
989 perror("sigaction");
990 return -1;
991 }
3967a8a8 992
ab7d5fc6
MD
993 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
994 duration, nr_readers, nr_writers);
995 printf_verbose("Writer delay : %lu loops.\n", wdelay);
996 printf_verbose("Reader duration : %lu loops.\n", rduration);
6d5c0ca9
MD
997 printf_verbose("Mode:%s%s.\n",
998 add_only ? " add only" : " add/remove",
48ed1c18 999 add_unique ? " uniquify" : ( add_replace ? " replace" : " insert"));
b99436d7
LJ
1000 printf_verbose("Initial number of buckets: %lu buckets.\n", init_hash_size);
1001 printf_verbose("Minimum number of allocated buckets: %lu buckets.\n", min_hash_alloc_size);
1002 printf_verbose("Maximum number of buckets: %lu buckets.\n", max_hash_buckets_size);
5dc00396
MD
1003 printf_verbose("Init pool size offset %lu size %lu.\n",
1004 init_pool_offset, init_pool_size);
1005 printf_verbose("Lookup pool size offset %lu size %lu.\n",
1006 lookup_pool_offset, lookup_pool_size);
1007 printf_verbose("Update pool size offset %lu size %lu.\n",
1008 write_pool_offset, write_pool_size);
ab7d5fc6
MD
1009 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
1010 "main", pthread_self(), (unsigned long)gettid());
1011
ab7d5fc6
MD
1012 tid_reader = malloc(sizeof(*tid_reader) * nr_readers);
1013 tid_writer = malloc(sizeof(*tid_writer) * nr_writers);
1014 count_reader = malloc(sizeof(*count_reader) * nr_readers);
1015 count_writer = malloc(sizeof(*count_writer) * nr_writers);
48ed1c18
MD
1016
1017 err = create_all_cpu_call_rcu_data(0);
1018 assert(!err);
1019
c0b8a865
LJ
1020 if (memory_backend) {
1021 test_ht = _cds_lfht_new(init_hash_size, min_hash_alloc_size,
1022 max_hash_buckets_size,
1023 (opt_auto_resize ? CDS_LFHT_AUTO_RESIZE : 0) |
1024 CDS_LFHT_ACCOUNTING, memory_backend,
1025 &rcu_flavor, NULL);
1026 } else {
1027 test_ht = cds_lfht_new(init_hash_size, min_hash_alloc_size,
1028 max_hash_buckets_size,
1029 (opt_auto_resize ? CDS_LFHT_AUTO_RESIZE : 0) |
1030 CDS_LFHT_ACCOUNTING, NULL);
1031 }
1032
48ed1c18 1033 /*
c0b8a865 1034 * Hash Population needs to be seen as a RCU reader
48ed1c18
MD
1035 * thread from the point of view of resize.
1036 */
1037 rcu_register_thread();
48ed1c18 1038 ret = populate_hash();
5dc45f25 1039 assert(!ret);
59e371e3
MD
1040
1041 rcu_thread_offline();
14756542 1042
ab7d5fc6
MD
1043 next_aff = 0;
1044
1045 for (i = 0; i < nr_readers; i++) {
1046 err = pthread_create(&tid_reader[i], NULL, thr_reader,
1047 &count_reader[i]);
1048 if (err != 0)
1049 exit(1);
1050 }
1051 for (i = 0; i < nr_writers; i++) {
1052 err = pthread_create(&tid_writer[i], NULL, thr_writer,
1053 &count_writer[i]);
1054 if (err != 0)
1055 exit(1);
1056 }
1057
abc490a1 1058 cmm_smp_mb();
ab7d5fc6
MD
1059
1060 test_go = 1;
1061
3967a8a8
MD
1062 remain = duration;
1063 do {
1064 remain = sleep(remain);
1065 } while (remain > 0);
ab7d5fc6
MD
1066
1067 test_stop = 1;
1068
1069 for (i = 0; i < nr_readers; i++) {
1070 err = pthread_join(tid_reader[i], &tret);
1071 if (err != 0)
1072 exit(1);
1073 tot_reads += count_reader[i];
1074 }
1075 for (i = 0; i < nr_writers; i++) {
1076 err = pthread_join(tid_writer[i], &tret);
1077 if (err != 0)
1078 exit(1);
b8e1907c
MD
1079 tot_writes += count_writer[i].update_ops;
1080 tot_add += count_writer[i].add;
3c16bf4b 1081 tot_add_exist += count_writer[i].add_exist;
b8e1907c 1082 tot_remove += count_writer[i].remove;
ab7d5fc6 1083 }
7ed7682f
MD
1084
1085 /* teardown counter thread */
1086 act.sa_handler = SIG_IGN;
1087 act.sa_flags = SA_RESTART;
1088 ret = sigaction(SIGUSR2, &act, NULL);
1089 if (ret == -1) {
1090 perror("sigaction");
1091 return -1;
1092 }
1093 {
1094 char msg[1] = { 0x42 };
3fb1173c
MD
1095 ssize_t ret;
1096
1097 do {
1098 ret = write(count_pipe[1], msg, 1); /* wakeup thread */
1099 } while (ret == -1L && errno == EINTR);
7ed7682f
MD
1100 }
1101 err = pthread_join(tid_count, &tret);
1102 if (err != 0)
1103 exit(1);
1104
33c7c748 1105 fflush(stdout);
59e371e3
MD
1106 rcu_thread_online();
1107 rcu_read_lock();
175ec0eb 1108 printf("Counting nodes... ");
caf3653d 1109 cds_lfht_count_nodes(test_ht, &approx_before, &count, &approx_after);
175ec0eb
MD
1110 printf("done.\n");
1111 test_delete_all_nodes(test_ht);
59e371e3
MD
1112 rcu_read_unlock();
1113 rcu_thread_offline();
caf3653d 1114 if (count) {
d933dd0e 1115 printf("Approximation before node accounting: %ld nodes.\n",
973e5e1b 1116 approx_before);
175ec0eb 1117 printf("Nodes deleted from hash table before destroy: "
caf3653d
MD
1118 "%lu nodes.\n",
1119 count);
d933dd0e 1120 printf("Approximation after node accounting: %ld nodes.\n",
973e5e1b
MD
1121 approx_after);
1122 }
b7d619b0 1123 ret = cds_lfht_destroy(test_ht, NULL);
33c7c748
MD
1124 if (ret)
1125 printf_verbose("final delete aborted\n");
1126 else
1127 printf_verbose("final delete success\n");
ab7d5fc6
MD
1128 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
1129 tot_writes);
1130 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu "
1131 "nr_writers %3u "
d837911d 1132 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu "
cd1ae16a 1133 "nr_add %12llu nr_add_fail %12llu nr_remove %12llu nr_leaked %12lld\n",
ab7d5fc6 1134 argv[0], duration, nr_readers, rduration,
d837911d 1135 nr_writers, wdelay, tot_reads, tot_writes,
3c16bf4b 1136 tot_reads + tot_writes, tot_add, tot_add_exist, tot_remove,
cd1ae16a 1137 (long long) tot_add + init_populate - tot_remove - count);
59e371e3 1138 rcu_unregister_thread();
3a22f1dd 1139 free_all_cpu_call_rcu_data();
ab7d5fc6
MD
1140 free(tid_reader);
1141 free(tid_writer);
1142 free(count_reader);
1143 free(count_writer);
1144 return 0;
1145}
This page took 0.081784 seconds and 4 git commands to generate.