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