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