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