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