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