rculfhash: add CDS_LFHT_ACCOUNTING flag
[userspace-rcu.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 202 char msg[1] = { 0x42 };
3fb1173c
MD
203 ssize_t ret;
204
205 do {
206 ret = write(count_pipe[1], msg, 1); /* wakeup thread */
207 } while (ret == -1L && errno == EINTR);
973e5e1b
MD
208}
209
ab7d5fc6
MD
210/*
211 * returns 0 if test should end.
212 */
213static int test_duration_write(void)
214{
215 return !test_stop;
216}
217
218static int test_duration_read(void)
219{
220 return !test_stop;
221}
222
223static unsigned long long __thread nr_writes;
224static unsigned long long __thread nr_reads;
225
226static unsigned int nr_readers;
227static unsigned int nr_writers;
228
229pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
230
231void rcu_copy_mutex_lock(void)
232{
233 int ret;
234 ret = pthread_mutex_lock(&rcu_copy_mutex);
235 if (ret) {
236 perror("Error in pthread mutex lock");
237 exit(-1);
238 }
239}
240
241void rcu_copy_mutex_unlock(void)
242{
243 int ret;
244
245 ret = pthread_mutex_unlock(&rcu_copy_mutex);
246 if (ret) {
247 perror("Error in pthread mutex unlock");
248 exit(-1);
249 }
250}
251
732ad076
MD
252/*
253 * Hash function
254 * Source: http://burtleburtle.net/bob/c/lookup3.c
255 * Originally Public Domain
256 */
257
258#define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
259
260#define mix(a, b, c) \
261do { \
262 a -= c; a ^= rot(c, 4); c += b; \
263 b -= a; b ^= rot(a, 6); a += c; \
264 c -= b; c ^= rot(b, 8); b += a; \
265 a -= c; a ^= rot(c, 16); c += b; \
266 b -= a; b ^= rot(a, 19); a += c; \
267 c -= b; c ^= rot(b, 4); b += a; \
268} while (0)
269
270#define final(a, b, c) \
271{ \
272 c ^= b; c -= rot(b, 14); \
273 a ^= c; a -= rot(c, 11); \
274 b ^= a; b -= rot(a, 25); \
275 c ^= b; c -= rot(b, 16); \
276 a ^= c; a -= rot(c, 4);\
277 b ^= a; b -= rot(a, 14); \
278 c ^= b; c -= rot(b, 24); \
279}
280
281static __attribute__((unused))
282uint32_t hash_u32(
283 const uint32_t *k, /* the key, an array of uint32_t values */
284 size_t length, /* the length of the key, in uint32_ts */
285 uint32_t initval) /* the previous hash, or an arbitrary value */
286{
287 uint32_t a, b, c;
288
289 /* Set up the internal state */
290 a = b = c = 0xdeadbeef + (((uint32_t) length) << 2) + initval;
291
292 /*----------------------------------------- handle most of the key */
293 while (length > 3) {
294 a += k[0];
295 b += k[1];
296 c += k[2];
297 mix(a, b, c);
298 length -= 3;
299 k += 3;
300 }
301
302 /*----------------------------------- handle the last 3 uint32_t's */
303 switch (length) { /* all the case statements fall through */
304 case 3: c += k[2];
305 case 2: b += k[1];
306 case 1: a += k[0];
307 final(a, b, c);
308 case 0: /* case 0: nothing left to add */
309 break;
310 }
311 /*---------------------------------------------- report the result */
312 return c;
313}
314
315static
316void hashword2(
317 const uint32_t *k, /* the key, an array of uint32_t values */
318 size_t length, /* the length of the key, in uint32_ts */
319 uint32_t *pc, /* IN: seed OUT: primary hash value */
320 uint32_t *pb) /* IN: more seed OUT: secondary hash value */
321{
322 uint32_t a, b, c;
323
324 /* Set up the internal state */
325 a = b = c = 0xdeadbeef + ((uint32_t) (length << 2)) + *pc;
326 c += *pb;
327
328 /*----------------------------------------- handle most of the key */
329 while (length > 3) {
330 a += k[0];
331 b += k[1];
332 c += k[2];
333 mix(a, b, c);
334 length -= 3;
335 k += 3;
336 }
337
338 /*----------------------------------- handle the last 3 uint32_t's */
339 switch (length) { /* all the case statements fall through */
340 case 3: c += k[2];
341 case 2: b += k[1];
342 case 1: a += k[0];
343 final(a, b, c);
344 case 0: /* case 0: nothing left to add */
345 break;
346 }
347 /*---------------------------------------------- report the result */
348 *pc = c;
349 *pb = b;
350}
351
352#if (CAA_BITS_PER_LONG == 32)
abc490a1 353static
732ad076 354unsigned long test_hash(void *_key, size_t length, unsigned long seed)
abc490a1 355{
f542a7ee 356 unsigned int key = (unsigned int) _key;
abc490a1 357
f542a7ee 358 assert(length == sizeof(unsigned int));
bda21126 359 return hash_u32(&key, 1, seed);
732ad076
MD
360}
361#else
362static
363unsigned long test_hash(void *_key, size_t length, unsigned long seed)
364{
365 union {
366 uint64_t v64;
367 uint32_t v32[2];
368 } v;
369 union {
370 uint64_t v64;
371 uint32_t v32[2];
372 } key;
373
374 assert(length == sizeof(unsigned long));
375 v.v64 = (uint64_t) seed;
376 key.v64 = (uint64_t) _key;
377 hashword2(key.v32, 2, &v.v32[0], &v.v32[1]);
378 return v.v64;
379}
380#endif
abc490a1 381
732ad076
MD
382static
383unsigned long test_compare(void *key1, size_t key1_len,
384 void *key2, size_t key2_len)
385{
386 if (unlikely(key1_len != key2_len))
387 return -1;
388 assert(key1_len == sizeof(unsigned long));
389 if (key1 == key2)
390 return 0;
391 else
392 return 1;
abc490a1 393}
ab7d5fc6 394
7ed7682f
MD
395void *thr_count(void *arg)
396{
397 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
398 "counter", pthread_self(), (unsigned long)gettid());
399
400 rcu_register_thread();
401
402 for (;;) {
d933dd0e
MD
403 unsigned long count, removed;
404 long approx_before, approx_after;
7ed7682f
MD
405 ssize_t len;
406 char buf[1];
407
408 rcu_thread_offline();
409 len = read(count_pipe[0], buf, 1);
410 rcu_thread_online();
411 if (unlikely(!test_duration_read()))
412 break;
413 if (len != 1)
414 continue;
415 /* Accounting */
416 printf("Counting nodes... ");
417 fflush(stdout);
418 rcu_read_lock();
419 cds_lfht_count_nodes(test_ht, &approx_before, &count, &removed,
420 &approx_after);
421 rcu_read_unlock();
422 printf("done.\n");
d933dd0e 423 printf("Approximation before node accounting: %ld nodes.\n",
7ed7682f
MD
424 approx_before);
425 printf("Accounting of nodes in the hash table: "
426 "%lu nodes + %lu logically removed.\n",
427 count, removed);
d933dd0e 428 printf("Approximation after node accounting: %ld nodes.\n",
7ed7682f
MD
429 approx_after);
430 }
431 rcu_unregister_thread();
432 return NULL;
433}
434
ab7d5fc6
MD
435void *thr_reader(void *_count)
436{
437 unsigned long long *count = _count;
14044b37 438 struct cds_lfht_node *node;
adc0de68 439 struct cds_lfht_iter iter;
ab7d5fc6
MD
440
441 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
442 "reader", pthread_self(), (unsigned long)gettid());
443
444 set_affinity();
445
446 rcu_register_thread();
447
448 while (!test_go)
449 {
450 }
abc490a1 451 cmm_smp_mb();
ab7d5fc6
MD
452
453 for (;;) {
454 rcu_read_lock();
adc0de68 455 cds_lfht_lookup(test_ht,
c8f5b384 456 (void *)(((unsigned long) rand_r(&rand_lookup) % lookup_pool_size) + lookup_pool_offset),
adc0de68
MD
457 sizeof(void *), &iter);
458 node = cds_lfht_iter_get_node(&iter);
59b10634
MD
459 if (node == NULL) {
460 if (validate_lookup) {
461 printf("[ERROR] Lookup cannot find initial node.\n");
46834ba6 462 exit(-1);
59b10634 463 }
5e28c532 464 lookup_fail++;
59b10634 465 } else {
5e28c532 466 lookup_ok++;
59b10634 467 }
ab7d5fc6 468 debug_yield_read();
ab7d5fc6
MD
469 if (unlikely(rduration))
470 loop_sleep(rduration);
471 rcu_read_unlock();
472 nr_reads++;
473 if (unlikely(!test_duration_read()))
474 break;
5567839e
MD
475 if (unlikely((nr_reads & ((1 << 10) - 1)) == 0))
476 rcu_quiescent_state();
ab7d5fc6
MD
477 }
478
479 rcu_unregister_thread();
480
481 *count = nr_reads;
482 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
483 "reader", pthread_self(), (unsigned long)gettid());
5e28c532
MD
484 printf_verbose("readid : %lx, lookupfail %lu, lookupok %lu\n",
485 pthread_self(), lookup_fail, lookup_ok);
ab7d5fc6
MD
486 return ((void*)1);
487
488}
489
abc490a1
MD
490static
491void free_node_cb(struct rcu_head *head)
492{
14044b37
MD
493 struct cds_lfht_node *node =
494 caa_container_of(head, struct cds_lfht_node, head);
abc490a1
MD
495 free(node);
496}
497
ab7d5fc6
MD
498void *thr_writer(void *_count)
499{
14044b37 500 struct cds_lfht_node *node, *ret_node;
adc0de68 501 struct cds_lfht_iter iter;
b8e1907c 502 struct wr_count *count = _count;
5e28c532 503 int ret;
ab7d5fc6
MD
504
505 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
506 "writer", pthread_self(), (unsigned long)gettid());
507
508 set_affinity();
509
5e28c532 510 rcu_register_thread();
5e28c532 511
ab7d5fc6
MD
512 while (!test_go)
513 {
514 }
abc490a1 515 cmm_smp_mb();
ab7d5fc6
MD
516
517 for (;;) {
3967a8a8
MD
518 if ((addremove == AR_ADD || add_only)
519 || (addremove == AR_RANDOM && rand_r(&rand_lookup) & 1)) {
14044b37 520 node = malloc(sizeof(struct cds_lfht_node));
14044b37 521 cds_lfht_node_init(node,
c8f5b384 522 (void *)(((unsigned long) rand_r(&rand_lookup) % write_pool_size) + write_pool_offset),
cb233752 523 sizeof(void *));
6b262fd7 524 rcu_read_lock();
48ed1c18 525 if (add_unique) {
14044b37 526 ret_node = cds_lfht_add_unique(test_ht, node);
48ed1c18
MD
527 } else {
528 if (add_replace)
9357c415 529 ret_node = cds_lfht_add_replace(test_ht, node);
48ed1c18
MD
530 else
531 cds_lfht_add(test_ht, node);
532 }
abc490a1 533 rcu_read_unlock();
6d5c0ca9 534 if (add_unique && ret_node != node) {
742aa1db 535 free(node);
3eca1b8c 536 nr_addexist++;
48ed1c18
MD
537 } else {
538 if (add_replace && ret_node) {
539 call_rcu(&ret_node->head, free_node_cb);
540 nr_addexist++;
541 } else {
542 nr_add++;
543 }
544 }
5e28c532
MD
545 } else {
546 /* May delete */
abc490a1 547 rcu_read_lock();
adc0de68 548 cds_lfht_lookup(test_ht,
c8f5b384 549 (void *)(((unsigned long) rand_r(&rand_lookup) % write_pool_size) + write_pool_offset),
adc0de68 550 sizeof(void *), &iter);
9357c415 551 ret = cds_lfht_del(test_ht, &iter);
abc490a1
MD
552 rcu_read_unlock();
553 if (ret == 0) {
9357c415 554 node = cds_lfht_iter_get_node(&iter);
abc490a1 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)
9357c415 618 ret_node = cds_lfht_add_replace(test_ht, node);
48ed1c18
MD
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
175ec0eb
MD
639static
640void test_delete_all_nodes(struct cds_lfht *ht)
641{
642 struct cds_lfht_iter iter;
643 struct cds_lfht_node *node;
644 unsigned long count = 0;
645
646 cds_lfht_first(ht, &iter);
647 while ((node = cds_lfht_iter_get_node(&iter)) != NULL) {
648 int ret;
649
650 ret = cds_lfht_del(test_ht, &iter);
651 assert(!ret);
652 call_rcu(&node->head, free_node_cb);
653 cds_lfht_next(ht, &iter);
654 count++;
655 }
656 printf("deleted %lu nodes.\n", count);
657}
658
ab7d5fc6
MD
659void show_usage(int argc, char **argv)
660{
48ed1c18 661 printf("Usage : %s nr_readers nr_writers duration (s)\n", argv[0]);
ab7d5fc6 662#ifdef DEBUG_YIELD
48ed1c18 663 printf(" [-r] [-w] (yield reader and/or writer)\n");
ab7d5fc6 664#endif
48ed1c18
MD
665 printf(" [-d delay] (writer period (us))\n");
666 printf(" [-c duration] (reader C.S. duration (in loops))\n");
667 printf(" [-v] (verbose output)\n");
668 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
669 printf(" [-h size] (initial hash table size)\n");
670 printf(" [not -u nor -s] Add entries (supports redundant keys).\n");
671 printf(" [-u] Uniquify add (no redundant keys).\n");
672 printf(" [-s] Replace (swap) entries.\n");
673 printf(" [-i] Add only (no removal).\n");
674 printf(" [-k nr_nodes] Number of nodes to insert initially.\n");
675 printf(" [-A] Automatically resize hash table.\n");
676 printf(" [-R offset] Lookup pool offset.\n");
677 printf(" [-S offset] Write pool offset.\n");
678 printf(" [-T offset] Init pool offset.\n");
679 printf(" [-M size] Lookup pool size.\n");
680 printf(" [-N size] Write pool size.\n");
681 printf(" [-O size] Init pool size.\n");
682 printf(" [-V] Validate lookups of init values (use with filled init pool, same lookup range, with different write range).\n");
683 printf("\n\n");
ab7d5fc6
MD
684}
685
686int main(int argc, char **argv)
687{
688 int err;
689 pthread_t *tid_reader, *tid_writer;
7ed7682f 690 pthread_t tid_count;
ab7d5fc6 691 void *tret;
b8e1907c
MD
692 unsigned long long *count_reader;
693 struct wr_count *count_writer;
694 unsigned long long tot_reads = 0, tot_writes = 0,
3c16bf4b 695 tot_add = 0, tot_add_exist = 0, tot_remove = 0;
d933dd0e
MD
696 unsigned long count, removed;
697 long approx_before, approx_after;
5e28c532 698 int i, a, ret;
3967a8a8
MD
699 struct sigaction act;
700 unsigned int remain;
ab7d5fc6
MD
701
702 if (argc < 4) {
703 show_usage(argc, argv);
704 return -1;
705 }
706
707 err = sscanf(argv[1], "%u", &nr_readers);
708 if (err != 1) {
709 show_usage(argc, argv);
710 return -1;
711 }
712
713 err = sscanf(argv[2], "%u", &nr_writers);
714 if (err != 1) {
715 show_usage(argc, argv);
716 return -1;
717 }
718
719 err = sscanf(argv[3], "%lu", &duration);
720 if (err != 1) {
721 show_usage(argc, argv);
722 return -1;
723 }
724
725 for (i = 4; i < argc; i++) {
726 if (argv[i][0] != '-')
727 continue;
728 switch (argv[i][1]) {
729#ifdef DEBUG_YIELD
730 case 'r':
731 yield_active |= YIELD_READ;
732 break;
733 case 'w':
734 yield_active |= YIELD_WRITE;
735 break;
736#endif
737 case 'a':
738 if (argc < i + 2) {
739 show_usage(argc, argv);
740 return -1;
741 }
742 a = atoi(argv[++i]);
743 cpu_affinities[next_aff++] = a;
744 use_affinity = 1;
745 printf_verbose("Adding CPU %d affinity\n", a);
746 break;
747 case 'c':
748 if (argc < i + 2) {
749 show_usage(argc, argv);
750 return -1;
751 }
752 rduration = atol(argv[++i]);
753 break;
754 case 'd':
755 if (argc < i + 2) {
756 show_usage(argc, argv);
757 return -1;
758 }
759 wdelay = atol(argv[++i]);
760 break;
761 case 'v':
762 verbose_mode = 1;
763 break;
6d5c0ca9
MD
764 case 'h':
765 if (argc < i + 2) {
766 show_usage(argc, argv);
767 return -1;
768 }
769 init_hash_size = atol(argv[++i]);
770 break;
771 case 'u':
48ed1c18
MD
772 if (add_replace) {
773 printf("Please specify at most one of -s or -u.\n");
774 exit(-1);
775 }
6d5c0ca9
MD
776 add_unique = 1;
777 break;
48ed1c18
MD
778 case 's':
779 if (add_unique) {
780 printf("Please specify at most one of -s or -u.\n");
781 exit(-1);
782 }
783 add_replace = 1;
784 break;
6d5c0ca9
MD
785 case 'i':
786 add_only = 1;
787 break;
cd1ae16a
MD
788 case 'k':
789 init_populate = atol(argv[++i]);
790 break;
151e7a93
MD
791 case 'A':
792 opt_auto_resize = 1;
793 break;
8008a032
MD
794 case 'R':
795 lookup_pool_offset = atol(argv[++i]);
796 break;
797 case 'S':
798 write_pool_offset = atol(argv[++i]);
799 break;
800 case 'T':
801 init_pool_offset = atol(argv[++i]);
802 break;
d837911d
MD
803 case 'M':
804 lookup_pool_size = atol(argv[++i]);
805 break;
806 case 'N':
807 write_pool_size = atol(argv[++i]);
808 break;
809 case 'O':
810 init_pool_size = atol(argv[++i]);
811 break;
59b10634
MD
812 case 'V':
813 validate_lookup = 1;
814 break;
8008a032 815
ab7d5fc6
MD
816 }
817 }
818
6d5c0ca9
MD
819 /* Check if hash size is power of 2 */
820 if (init_hash_size && init_hash_size & (init_hash_size - 1)) {
821 printf("Error: Hash table size %lu is not a power of 2.\n",
822 init_hash_size);
823 return -1;
824 }
825
3967a8a8
MD
826 memset(&act, 0, sizeof(act));
827 ret = sigemptyset(&act.sa_mask);
828 if (ret == -1) {
829 perror("sigemptyset");
830 return -1;
831 }
832 act.sa_handler = sigusr1_handler;
833 act.sa_flags = SA_RESTART;
834 ret = sigaction(SIGUSR1, &act, NULL);
835 if (ret == -1) {
836 perror("sigaction");
837 return -1;
838 }
7ed7682f
MD
839
840 ret = pipe(count_pipe);
841 if (ret == -1) {
842 perror("pipe");
843 return -1;
844 }
845
846 /* spawn counter thread */
847 err = pthread_create(&tid_count, NULL, thr_count,
848 NULL);
849 if (err != 0)
850 exit(1);
851
973e5e1b
MD
852 act.sa_handler = sigusr2_handler;
853 act.sa_flags = SA_RESTART;
854 ret = sigaction(SIGUSR2, &act, NULL);
855 if (ret == -1) {
856 perror("sigaction");
857 return -1;
858 }
3967a8a8 859
ab7d5fc6
MD
860 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
861 duration, nr_readers, nr_writers);
862 printf_verbose("Writer delay : %lu loops.\n", wdelay);
863 printf_verbose("Reader duration : %lu loops.\n", rduration);
6d5c0ca9
MD
864 printf_verbose("Mode:%s%s.\n",
865 add_only ? " add only" : " add/remove",
48ed1c18 866 add_unique ? " uniquify" : ( add_replace ? " replace" : " insert"));
6d5c0ca9 867 printf_verbose("Initial hash table size: %lu buckets.\n", init_hash_size);
5dc00396
MD
868 printf_verbose("Init pool size offset %lu size %lu.\n",
869 init_pool_offset, init_pool_size);
870 printf_verbose("Lookup pool size offset %lu size %lu.\n",
871 lookup_pool_offset, lookup_pool_size);
872 printf_verbose("Update pool size offset %lu size %lu.\n",
873 write_pool_offset, write_pool_size);
ab7d5fc6
MD
874 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
875 "main", pthread_self(), (unsigned long)gettid());
876
ab7d5fc6
MD
877 tid_reader = malloc(sizeof(*tid_reader) * nr_readers);
878 tid_writer = malloc(sizeof(*tid_writer) * nr_writers);
879 count_reader = malloc(sizeof(*count_reader) * nr_readers);
880 count_writer = malloc(sizeof(*count_writer) * nr_writers);
48ed1c18
MD
881
882 err = create_all_cpu_call_rcu_data(0);
883 assert(!err);
884
885 /*
886 * Hash creation and population needs to be seen as a RCU reader
887 * thread from the point of view of resize.
888 */
889 rcu_register_thread();
14044b37 890 test_ht = cds_lfht_new(test_hash, test_compare, 0x42UL,
5488222b 891 init_hash_size, 1,
5afadd12
LJ
892 (opt_auto_resize ? CDS_LFHT_AUTO_RESIZE : 0) |
893 CDS_LFHT_ACCOUNTING, NULL);
48ed1c18 894 ret = populate_hash();
5dc45f25 895 assert(!ret);
59e371e3
MD
896
897 rcu_thread_offline();
14756542 898
ab7d5fc6
MD
899 next_aff = 0;
900
901 for (i = 0; i < nr_readers; i++) {
902 err = pthread_create(&tid_reader[i], NULL, thr_reader,
903 &count_reader[i]);
904 if (err != 0)
905 exit(1);
906 }
907 for (i = 0; i < nr_writers; i++) {
908 err = pthread_create(&tid_writer[i], NULL, thr_writer,
909 &count_writer[i]);
910 if (err != 0)
911 exit(1);
912 }
913
abc490a1 914 cmm_smp_mb();
ab7d5fc6
MD
915
916 test_go = 1;
917
3967a8a8
MD
918 remain = duration;
919 do {
920 remain = sleep(remain);
921 } while (remain > 0);
ab7d5fc6
MD
922
923 test_stop = 1;
924
925 for (i = 0; i < nr_readers; i++) {
926 err = pthread_join(tid_reader[i], &tret);
927 if (err != 0)
928 exit(1);
929 tot_reads += count_reader[i];
930 }
931 for (i = 0; i < nr_writers; i++) {
932 err = pthread_join(tid_writer[i], &tret);
933 if (err != 0)
934 exit(1);
b8e1907c
MD
935 tot_writes += count_writer[i].update_ops;
936 tot_add += count_writer[i].add;
3c16bf4b 937 tot_add_exist += count_writer[i].add_exist;
b8e1907c 938 tot_remove += count_writer[i].remove;
ab7d5fc6 939 }
7ed7682f
MD
940
941 /* teardown counter thread */
942 act.sa_handler = SIG_IGN;
943 act.sa_flags = SA_RESTART;
944 ret = sigaction(SIGUSR2, &act, NULL);
945 if (ret == -1) {
946 perror("sigaction");
947 return -1;
948 }
949 {
950 char msg[1] = { 0x42 };
3fb1173c
MD
951 ssize_t ret;
952
953 do {
954 ret = write(count_pipe[1], msg, 1); /* wakeup thread */
955 } while (ret == -1L && errno == EINTR);
7ed7682f
MD
956 }
957 err = pthread_join(tid_count, &tret);
958 if (err != 0)
959 exit(1);
960
33c7c748 961 fflush(stdout);
59e371e3
MD
962 rcu_thread_online();
963 rcu_read_lock();
175ec0eb 964 printf("Counting nodes... ");
973e5e1b
MD
965 cds_lfht_count_nodes(test_ht, &approx_before, &count, &removed,
966 &approx_after);
175ec0eb
MD
967 printf("done.\n");
968 test_delete_all_nodes(test_ht);
59e371e3
MD
969 rcu_read_unlock();
970 rcu_thread_offline();
973e5e1b 971 if (count || removed) {
d933dd0e 972 printf("Approximation before node accounting: %ld nodes.\n",
973e5e1b 973 approx_before);
175ec0eb 974 printf("Nodes deleted from hash table before destroy: "
973e5e1b
MD
975 "%lu nodes + %lu logically removed.\n",
976 count, removed);
d933dd0e 977 printf("Approximation after node accounting: %ld nodes.\n",
973e5e1b
MD
978 approx_after);
979 }
b7d619b0 980 ret = cds_lfht_destroy(test_ht, NULL);
33c7c748
MD
981 if (ret)
982 printf_verbose("final delete aborted\n");
983 else
984 printf_verbose("final delete success\n");
ab7d5fc6
MD
985 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
986 tot_writes);
987 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu "
988 "nr_writers %3u "
d837911d 989 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu "
cd1ae16a 990 "nr_add %12llu nr_add_fail %12llu nr_remove %12llu nr_leaked %12lld\n",
ab7d5fc6 991 argv[0], duration, nr_readers, rduration,
d837911d 992 nr_writers, wdelay, tot_reads, tot_writes,
3c16bf4b 993 tot_reads + tot_writes, tot_add, tot_add_exist, tot_remove,
cd1ae16a 994 (long long) tot_add + init_populate - tot_remove - count);
59e371e3 995 rcu_unregister_thread();
3a22f1dd 996 free_all_cpu_call_rcu_data();
ab7d5fc6
MD
997 free(tid_reader);
998 free(tid_writer);
999 free(count_reader);
1000 free(count_writer);
1001 return 0;
1002}
This page took 0.081232 seconds and 4 git commands to generate.