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