Build system: use SPDX identifiers
[urcu.git] / tests / benchmark / test_urcu_hash.c
CommitLineData
ab7d5fc6 1/*
cd1ae16a 2 * test_urcu_hash.c
ab7d5fc6
MD
3 *
4 * Userspace RCU library - test program
5 *
18ca7a5b 6 * Copyright 2009-2012 - Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
ab7d5fc6
MD
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
18ca7a5b 23#include "test_urcu_hash.h"
ab7d5fc6 24
f52c1ef7
MD
25enum test_hash {
26 TEST_HASH_RW,
20adf780 27 TEST_HASH_UNIQUE,
f52c1ef7
MD
28};
29
30struct test_hash_cb {
31 void (*sigusr1)(int signo);
32 void (*sigusr2)(int signo);
33 void *(*thr_reader)(void *_count);
34 void *(*thr_writer)(void *_count);
35 int (*populate_hash)(void);
36};
37
38static
39struct test_hash_cb test_hash_cb[] = {
40 [TEST_HASH_RW] = {
41 test_hash_rw_sigusr1_handler,
42 test_hash_rw_sigusr2_handler,
43 test_hash_rw_thr_reader,
44 test_hash_rw_thr_writer,
45 test_hash_rw_populate_hash,
46 },
20adf780
MD
47 [TEST_HASH_UNIQUE] = {
48 test_hash_unique_sigusr1_handler,
49 test_hash_unique_sigusr2_handler,
50 test_hash_unique_thr_reader,
51 test_hash_unique_thr_writer,
52 test_hash_unique_populate_hash,
53 },
54
f52c1ef7
MD
55};
56
57static enum test_hash test_choice = TEST_HASH_RW;
58
61c3fb60 59static
f52c1ef7
MD
60void (*get_sigusr1_cb(void))(int)
61{
62 return test_hash_cb[test_choice].sigusr1;
63}
64
61c3fb60 65static
f52c1ef7
MD
66void (*get_sigusr2_cb(void))(int)
67{
68 return test_hash_cb[test_choice].sigusr2;
69}
70
61c3fb60 71static
f52c1ef7
MD
72void *(*get_thr_reader_cb(void))(void *)
73{
74 return test_hash_cb[test_choice].thr_reader;
75}
76
61c3fb60 77static
f52c1ef7
MD
78void *(*get_thr_writer_cb(void))(void *)
79{
80 return test_hash_cb[test_choice].thr_writer;
81}
82
61c3fb60 83static
f52c1ef7
MD
84int (*get_populate_hash_cb(void))(void)
85{
86 return test_hash_cb[test_choice].populate_hash;
87}
88
bd252a04
MD
89DEFINE_URCU_TLS(unsigned int, rand_lookup);
90DEFINE_URCU_TLS(unsigned long, nr_add);
91DEFINE_URCU_TLS(unsigned long, nr_addexist);
92DEFINE_URCU_TLS(unsigned long, nr_del);
93DEFINE_URCU_TLS(unsigned long, nr_delnoent);
94DEFINE_URCU_TLS(unsigned long, lookup_fail);
95DEFINE_URCU_TLS(unsigned long, lookup_ok);
b8e1907c 96
18ca7a5b 97struct cds_lfht *test_ht;
5e28c532 98
18ca7a5b 99volatile int test_go, test_stop;
ab7d5fc6 100
18ca7a5b 101unsigned long wdelay;
ab7d5fc6 102
18ca7a5b 103unsigned long duration;
ab7d5fc6
MD
104
105/* read-side C.S. duration, in loops */
18ca7a5b
MD
106unsigned long rduration;
107
108unsigned long init_hash_size = DEFAULT_HASH_SIZE;
109unsigned long min_hash_alloc_size = DEFAULT_MIN_ALLOC_SIZE;
110unsigned long max_hash_buckets_size = (1UL << 20);
111unsigned long init_populate;
112int opt_auto_resize;
113int add_only, add_unique, add_replace;
114const struct cds_lfht_mm_type *memory_backend;
115
116unsigned long init_pool_offset, lookup_pool_offset, write_pool_offset;
117unsigned long init_pool_size = DEFAULT_RAND_POOL,
d837911d
MD
118 lookup_pool_size = DEFAULT_RAND_POOL,
119 write_pool_size = DEFAULT_RAND_POOL;
18ca7a5b 120int validate_lookup;
495913bf 121unsigned long nr_hash_chains; /* 0: normal table, other: number of hash chains */
8008a032 122
18ca7a5b 123int count_pipe[2];
7ed7682f 124
18ca7a5b 125int verbose_mode;
ab7d5fc6 126
18ca7a5b
MD
127unsigned int cpu_affinities[NR_CPUS];
128unsigned int next_aff = 0;
129int use_affinity = 0;
ab7d5fc6 130
18ca7a5b 131pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
ab7d5fc6 132
bd252a04
MD
133DEFINE_URCU_TLS(unsigned long long, nr_writes);
134DEFINE_URCU_TLS(unsigned long long, nr_reads);
ab7d5fc6 135
18ca7a5b
MD
136unsigned int nr_readers;
137unsigned int nr_writers;
ab7d5fc6 138
18ca7a5b 139static pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
fb6d173d 140
18ca7a5b 141void set_affinity(void)
ab7d5fc6 142{
2388c075 143#ifdef HAVE_SCHED_SETAFFINITY
ab7d5fc6 144 cpu_set_t mask;
95bc7fb9
MD
145 int cpu, ret;
146#endif /* HAVE_SCHED_SETAFFINITY */
ab7d5fc6
MD
147
148 if (!use_affinity)
149 return;
150
2388c075 151#ifdef HAVE_SCHED_SETAFFINITY
ab7d5fc6
MD
152 ret = pthread_mutex_lock(&affinity_mutex);
153 if (ret) {
154 perror("Error in pthread mutex lock");
155 exit(-1);
156 }
157 cpu = cpu_affinities[next_aff++];
158 ret = pthread_mutex_unlock(&affinity_mutex);
159 if (ret) {
160 perror("Error in pthread mutex unlock");
161 exit(-1);
162 }
163 CPU_ZERO(&mask);
164 CPU_SET(cpu, &mask);
e7e6ff7f 165 sched_setaffinity(0, sizeof(mask), &mask);
fb6d173d 166#endif /* HAVE_SCHED_SETAFFINITY */
ab7d5fc6
MD
167}
168
ab7d5fc6
MD
169void rcu_copy_mutex_lock(void)
170{
171 int ret;
172 ret = pthread_mutex_lock(&rcu_copy_mutex);
173 if (ret) {
174 perror("Error in pthread mutex lock");
175 exit(-1);
176 }
177}
178
179void rcu_copy_mutex_unlock(void)
180{
181 int ret;
182
183 ret = pthread_mutex_unlock(&rcu_copy_mutex);
184 if (ret) {
185 perror("Error in pthread mutex unlock");
186 exit(-1);
187 }
188}
189
996ff57c 190unsigned long test_compare(const void *key1, size_t key1_len,
e7e6ff7f 191 const void *key2, size_t key2_len)
732ad076 192{
8ed51e04 193 if (caa_unlikely(key1_len != key2_len))
732ad076 194 return -1;
01477510 195 urcu_posix_assert(key1_len == sizeof(unsigned long));
732ad076
MD
196 if (key1 == key2)
197 return 0;
198 else
199 return 1;
abc490a1 200}
ab7d5fc6 201
61c3fb60 202static
70469b43 203void *thr_count(void *arg __attribute__((unused)))
7ed7682f 204{
94df6318
MD
205 printf_verbose("thread_begin %s, tid %lu\n",
206 "counter", urcu_get_thread_id());
7ed7682f
MD
207
208 rcu_register_thread();
209
210 for (;;) {
caf3653d 211 unsigned long count;
d933dd0e 212 long approx_before, approx_after;
7ed7682f
MD
213 ssize_t len;
214 char buf[1];
215
216 rcu_thread_offline();
217 len = read(count_pipe[0], buf, 1);
218 rcu_thread_online();
8ed51e04 219 if (caa_unlikely(!test_duration_read()))
7ed7682f
MD
220 break;
221 if (len != 1)
222 continue;
223 /* Accounting */
224 printf("Counting nodes... ");
225 fflush(stdout);
226 rcu_read_lock();
caf3653d 227 cds_lfht_count_nodes(test_ht, &approx_before, &count,
7ed7682f
MD
228 &approx_after);
229 rcu_read_unlock();
230 printf("done.\n");
d933dd0e 231 printf("Approximation before node accounting: %ld nodes.\n",
7ed7682f
MD
232 approx_before);
233 printf("Accounting of nodes in the hash table: "
caf3653d
MD
234 "%lu nodes.\n",
235 count);
d933dd0e 236 printf("Approximation after node accounting: %ld nodes.\n",
7ed7682f
MD
237 approx_after);
238 }
239 rcu_unregister_thread();
240 return NULL;
241}
242
abc490a1
MD
243void free_node_cb(struct rcu_head *head)
244{
3c692076 245 struct lfht_test_node *node =
81d91005 246 caa_container_of(head, struct lfht_test_node, head);
abc490a1
MD
247 free(node);
248}
249
175ec0eb
MD
250static
251void test_delete_all_nodes(struct cds_lfht *ht)
252{
253 struct cds_lfht_iter iter;
3c692076 254 struct lfht_test_node *node;
175ec0eb
MD
255 unsigned long count = 0;
256
6d320126 257 cds_lfht_for_each_entry(ht, &iter, node, node) {
175ec0eb
MD
258 int ret;
259
bc8c3c74 260 ret = cds_lfht_del(test_ht, cds_lfht_iter_get_node(&iter));
01477510 261 urcu_posix_assert(!ret);
81d91005 262 call_rcu(&node->head, free_node_cb);
175ec0eb
MD
263 count++;
264 }
265 printf("deleted %lu nodes.\n", count);
266}
267
61c3fb60 268static
70469b43 269void show_usage(char **argv)
ab7d5fc6 270{
06637138
MD
271 printf("Usage : %s nr_readers nr_writers duration (s) <OPTIONS>\n",
272 argv[0]);
273 printf("OPTIONS:\n");
48ed1c18 274 printf(" [-r] [-w] (yield reader and/or writer)\n");
48ed1c18
MD
275 printf(" [-d delay] (writer period (us))\n");
276 printf(" [-c duration] (reader C.S. duration (in loops))\n");
277 printf(" [-v] (verbose output)\n");
278 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
b99436d7
LJ
279 printf(" [-h size] (initial number of buckets)\n");
280 printf(" [-m size] (minimum number of allocated buckets)\n");
281 printf(" [-n size] (maximum number of buckets)\n");
18ca7a5b 282printf(" [not -u nor -s] Add entries (supports redundant keys).\n");
48ed1c18
MD
283 printf(" [-u] Uniquify add (no redundant keys).\n");
284 printf(" [-s] Replace (swap) entries.\n");
285 printf(" [-i] Add only (no removal).\n");
286 printf(" [-k nr_nodes] Number of nodes to insert initially.\n");
287 printf(" [-A] Automatically resize hash table.\n");
c0b8a865 288 printf(" [-B order|chunk|mmap] Specify the memory backend.\n");
48ed1c18
MD
289 printf(" [-R offset] Lookup pool offset.\n");
290 printf(" [-S offset] Write pool offset.\n");
291 printf(" [-T offset] Init pool offset.\n");
292 printf(" [-M size] Lookup pool size.\n");
293 printf(" [-N size] Write pool size.\n");
294 printf(" [-O size] Init pool size.\n");
06637138
MD
295 printf(" [-V] Validate lookups of init values.\n");
296 printf(" (use with filled init pool, same lookup range,\n");
297 printf(" with different write range)\n");
20adf780 298 printf(" [-U] Uniqueness test.\n");
495913bf 299 printf(" [-C] Number of hash chains.\n");
06637138 300 printf("\n");
ab7d5fc6
MD
301}
302
303int main(int argc, char **argv)
304{
ab7d5fc6 305 pthread_t *tid_reader, *tid_writer;
7ed7682f 306 pthread_t tid_count;
ab7d5fc6 307 void *tret;
b8e1907c
MD
308 unsigned long long *count_reader;
309 struct wr_count *count_writer;
310 unsigned long long tot_reads = 0, tot_writes = 0,
3c16bf4b 311 tot_add = 0, tot_add_exist = 0, tot_remove = 0;
caf3653d 312 unsigned long count;
d933dd0e 313 long approx_before, approx_after;
e7e6ff7f 314 int i, a, ret, err, mainret = 0;
83e334d0 315 unsigned int i_thr;
3967a8a8
MD
316 struct sigaction act;
317 unsigned int remain;
e7e6ff7f
MD
318 unsigned int nr_readers_created = 0, nr_writers_created = 0;
319 long long nr_leaked;
ab7d5fc6
MD
320
321 if (argc < 4) {
70469b43 322 show_usage(argv);
e7e6ff7f
MD
323 mainret = 1;
324 goto end;
ab7d5fc6
MD
325 }
326
327 err = sscanf(argv[1], "%u", &nr_readers);
328 if (err != 1) {
70469b43 329 show_usage(argv);
e7e6ff7f
MD
330 mainret = 1;
331 goto end;
ab7d5fc6
MD
332 }
333
334 err = sscanf(argv[2], "%u", &nr_writers);
335 if (err != 1) {
70469b43 336 show_usage(argv);
e7e6ff7f
MD
337 mainret = 1;
338 goto end;
ab7d5fc6 339 }
83e334d0 340
ab7d5fc6
MD
341 err = sscanf(argv[3], "%lu", &duration);
342 if (err != 1) {
70469b43 343 show_usage(argv);
e7e6ff7f
MD
344 mainret = 1;
345 goto end;
ab7d5fc6
MD
346 }
347
348 for (i = 4; i < argc; i++) {
349 if (argv[i][0] != '-')
350 continue;
351 switch (argv[i][1]) {
ab7d5fc6 352 case 'r':
2650042a 353 rcu_debug_yield_enable(RCU_YIELD_READ);
ab7d5fc6
MD
354 break;
355 case 'w':
2650042a 356 rcu_debug_yield_enable(RCU_YIELD_WRITE);
ab7d5fc6 357 break;
ab7d5fc6
MD
358 case 'a':
359 if (argc < i + 2) {
70469b43 360 show_usage(argv);
e7e6ff7f
MD
361 mainret = 1;
362 goto end;
ab7d5fc6
MD
363 }
364 a = atoi(argv[++i]);
365 cpu_affinities[next_aff++] = a;
366 use_affinity = 1;
367 printf_verbose("Adding CPU %d affinity\n", a);
368 break;
369 case 'c':
370 if (argc < i + 2) {
70469b43 371 show_usage(argv);
e7e6ff7f
MD
372 mainret = 1;
373 goto end;
ab7d5fc6
MD
374 }
375 rduration = atol(argv[++i]);
376 break;
377 case 'd':
378 if (argc < i + 2) {
70469b43 379 show_usage(argv);
e7e6ff7f
MD
380 mainret = 1;
381 goto end;
ab7d5fc6
MD
382 }
383 wdelay = atol(argv[++i]);
384 break;
385 case 'v':
386 verbose_mode = 1;
387 break;
6d5c0ca9
MD
388 case 'h':
389 if (argc < i + 2) {
70469b43 390 show_usage(argv);
e7e6ff7f
MD
391 mainret = 1;
392 goto end;
6d5c0ca9
MD
393 }
394 init_hash_size = atol(argv[++i]);
395 break;
32becef6
LJ
396 case 'm':
397 if (argc < i + 2) {
70469b43 398 show_usage(argv);
e7e6ff7f
MD
399 mainret = 1;
400 goto end;
32becef6
LJ
401 }
402 min_hash_alloc_size = atol(argv[++i]);
403 break;
b99436d7
LJ
404 case 'n':
405 if (argc < i + 2) {
70469b43 406 show_usage(argv);
e7e6ff7f
MD
407 mainret = 1;
408 goto end;
b99436d7
LJ
409 }
410 max_hash_buckets_size = atol(argv[++i]);
411 break;
6d5c0ca9 412 case 'u':
48ed1c18
MD
413 if (add_replace) {
414 printf("Please specify at most one of -s or -u.\n");
415 exit(-1);
416 }
6d5c0ca9
MD
417 add_unique = 1;
418 break;
48ed1c18
MD
419 case 's':
420 if (add_unique) {
421 printf("Please specify at most one of -s or -u.\n");
422 exit(-1);
423 }
424 add_replace = 1;
425 break;
6d5c0ca9
MD
426 case 'i':
427 add_only = 1;
428 break;
cd1ae16a
MD
429 case 'k':
430 init_populate = atol(argv[++i]);
431 break;
151e7a93
MD
432 case 'A':
433 opt_auto_resize = 1;
434 break;
c0b8a865
LJ
435 case 'B':
436 if (argc < i + 2) {
70469b43 437 show_usage(argv);
e7e6ff7f
MD
438 mainret = 1;
439 goto end;
c0b8a865
LJ
440 }
441 i++;
442 if (!strcmp("order", argv[i]))
443 memory_backend = &cds_lfht_mm_order;
444 else if (!strcmp("chunk", argv[i]))
445 memory_backend = &cds_lfht_mm_chunk;
446 else if (!strcmp("mmap", argv[i]))
447 memory_backend = &cds_lfht_mm_mmap;
e7e6ff7f 448 else {
c0b8a865 449 printf("Please specify memory backend with order|chunk|mmap.\n");
e7e6ff7f
MD
450 mainret = 1;
451 goto end;
c0b8a865
LJ
452 }
453 break;
8008a032
MD
454 case 'R':
455 lookup_pool_offset = atol(argv[++i]);
456 break;
457 case 'S':
458 write_pool_offset = atol(argv[++i]);
459 break;
460 case 'T':
461 init_pool_offset = atol(argv[++i]);
462 break;
d837911d
MD
463 case 'M':
464 lookup_pool_size = atol(argv[++i]);
465 break;
466 case 'N':
467 write_pool_size = atol(argv[++i]);
468 break;
469 case 'O':
470 init_pool_size = atol(argv[++i]);
471 break;
59b10634
MD
472 case 'V':
473 validate_lookup = 1;
474 break;
20adf780
MD
475 case 'U':
476 test_choice = TEST_HASH_UNIQUE;
477 break;
495913bf
MD
478 case 'C':
479 nr_hash_chains = atol(argv[++i]);
480 break;
ab7d5fc6
MD
481 }
482 }
483
6d5c0ca9
MD
484 /* Check if hash size is power of 2 */
485 if (init_hash_size && init_hash_size & (init_hash_size - 1)) {
b99436d7 486 printf("Error: Initial number of buckets (%lu) is not a power of 2.\n",
6d5c0ca9 487 init_hash_size);
e7e6ff7f
MD
488 mainret = 1;
489 goto end;
6d5c0ca9
MD
490 }
491
d0d8f9aa 492 if (min_hash_alloc_size && min_hash_alloc_size & (min_hash_alloc_size - 1)) {
b99436d7 493 printf("Error: Minimum number of allocated buckets (%lu) is not a power of 2.\n",
32becef6 494 min_hash_alloc_size);
e7e6ff7f
MD
495 mainret = 1;
496 goto end;
32becef6
LJ
497 }
498
b99436d7
LJ
499 if (max_hash_buckets_size && max_hash_buckets_size & (max_hash_buckets_size - 1)) {
500 printf("Error: Maximum number of buckets (%lu) is not a power of 2.\n",
501 max_hash_buckets_size);
e7e6ff7f
MD
502 mainret = 1;
503 goto end;
b99436d7
LJ
504 }
505
3967a8a8
MD
506 memset(&act, 0, sizeof(act));
507 ret = sigemptyset(&act.sa_mask);
508 if (ret == -1) {
509 perror("sigemptyset");
e7e6ff7f
MD
510 mainret = 1;
511 goto end;
3967a8a8 512 }
f52c1ef7 513 act.sa_handler = get_sigusr1_cb();
3967a8a8
MD
514 act.sa_flags = SA_RESTART;
515 ret = sigaction(SIGUSR1, &act, NULL);
516 if (ret == -1) {
517 perror("sigaction");
e7e6ff7f
MD
518 mainret = 1;
519 goto end;
7ed7682f
MD
520 }
521
f52c1ef7 522 act.sa_handler = get_sigusr2_cb();
973e5e1b
MD
523 act.sa_flags = SA_RESTART;
524 ret = sigaction(SIGUSR2, &act, NULL);
525 if (ret == -1) {
526 perror("sigaction");
e7e6ff7f
MD
527 mainret = 1;
528 goto end;
973e5e1b 529 }
3967a8a8 530
ab7d5fc6
MD
531 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
532 duration, nr_readers, nr_writers);
533 printf_verbose("Writer delay : %lu loops.\n", wdelay);
534 printf_verbose("Reader duration : %lu loops.\n", rduration);
6d5c0ca9
MD
535 printf_verbose("Mode:%s%s.\n",
536 add_only ? " add only" : " add/remove",
48ed1c18 537 add_unique ? " uniquify" : ( add_replace ? " replace" : " insert"));
b99436d7
LJ
538 printf_verbose("Initial number of buckets: %lu buckets.\n", init_hash_size);
539 printf_verbose("Minimum number of allocated buckets: %lu buckets.\n", min_hash_alloc_size);
540 printf_verbose("Maximum number of buckets: %lu buckets.\n", max_hash_buckets_size);
5dc00396
MD
541 printf_verbose("Init pool size offset %lu size %lu.\n",
542 init_pool_offset, init_pool_size);
543 printf_verbose("Lookup pool size offset %lu size %lu.\n",
544 lookup_pool_offset, lookup_pool_size);
545 printf_verbose("Update pool size offset %lu size %lu.\n",
546 write_pool_offset, write_pool_size);
495913bf
MD
547 printf_verbose("Number of hash chains: %lu.\n",
548 nr_hash_chains);
94df6318
MD
549 printf_verbose("thread %-6s, tid %lu\n",
550 "main", urcu_get_thread_id());
ab7d5fc6 551
9aa14175 552 tid_reader = calloc(nr_readers, sizeof(*tid_reader));
e7e6ff7f
MD
553 if (!tid_reader) {
554 mainret = 1;
555 goto end;
556 }
9aa14175 557 tid_writer = calloc(nr_writers, sizeof(*tid_writer));
e7e6ff7f
MD
558 if (!tid_writer) {
559 mainret = 1;
560 goto end_free_tid_reader;
561 }
9aa14175 562 count_reader = calloc(nr_readers, sizeof(*count_reader));
e7e6ff7f
MD
563 if (!count_reader) {
564 mainret = 1;
565 goto end_free_tid_writer;
566 }
9aa14175 567 count_writer = calloc(nr_writers, sizeof(*count_writer));
e7e6ff7f
MD
568 if (!count_writer) {
569 mainret = 1;
570 goto end_free_count_reader;
571 }
48ed1c18
MD
572
573 err = create_all_cpu_call_rcu_data(0);
8bcbd94a
MD
574 if (err) {
575 printf("Per-CPU call_rcu() worker threads unavailable. Using default global worker thread.\n");
576 }
48ed1c18 577
c0b8a865
LJ
578 if (memory_backend) {
579 test_ht = _cds_lfht_new(init_hash_size, min_hash_alloc_size,
580 max_hash_buckets_size,
581 (opt_auto_resize ? CDS_LFHT_AUTO_RESIZE : 0) |
582 CDS_LFHT_ACCOUNTING, memory_backend,
583 &rcu_flavor, NULL);
584 } else {
585 test_ht = cds_lfht_new(init_hash_size, min_hash_alloc_size,
586 max_hash_buckets_size,
587 (opt_auto_resize ? CDS_LFHT_AUTO_RESIZE : 0) |
588 CDS_LFHT_ACCOUNTING, NULL);
589 }
0d02f4b5
MD
590 if (!test_ht) {
591 printf("Error allocating hash table.\n");
e7e6ff7f
MD
592 mainret = 1;
593 goto end_free_call_rcu_data;
0d02f4b5 594 }
c0b8a865 595
48ed1c18 596 /*
c0b8a865 597 * Hash Population needs to be seen as a RCU reader
48ed1c18
MD
598 * thread from the point of view of resize.
599 */
600 rcu_register_thread();
e7e6ff7f 601 ret = (get_populate_hash_cb())();
01477510 602 urcu_posix_assert(!ret);
59e371e3
MD
603
604 rcu_thread_offline();
14756542 605
ab7d5fc6
MD
606 next_aff = 0;
607
e7e6ff7f
MD
608 ret = pipe(count_pipe);
609 if (ret == -1) {
610 perror("pipe");
611 mainret = 1;
612 goto end_online;
613 }
614
615 /* spawn counter thread */
616 err = pthread_create(&tid_count, NULL, thr_count,
617 NULL);
618 if (err != 0) {
619 errno = err;
620 mainret = 1;
621 perror("pthread_create");
622 goto end_close_pipe;
623 }
624
83e334d0
MJ
625 for (i_thr = 0; i_thr < nr_readers; i_thr++) {
626 err = pthread_create(&tid_reader[i_thr],
f52c1ef7 627 NULL, get_thr_reader_cb(),
83e334d0 628 &count_reader[i_thr]);
e7e6ff7f
MD
629 if (err != 0) {
630 errno = err;
631 mainret = 1;
632 perror("pthread_create");
633 goto end_pthread_join;
634 }
635 nr_readers_created++;
ab7d5fc6 636 }
83e334d0
MJ
637 for (i_thr = 0; i_thr < nr_writers; i_thr++) {
638 err = pthread_create(&tid_writer[i_thr],
f52c1ef7 639 NULL, get_thr_writer_cb(),
83e334d0 640 &count_writer[i_thr]);
e7e6ff7f
MD
641 if (err != 0) {
642 errno = err;
643 mainret = 1;
644 perror("pthread_create");
645 goto end_pthread_join;
646 }
647 nr_writers_created++;
ab7d5fc6
MD
648 }
649
abc490a1 650 cmm_smp_mb();
ab7d5fc6
MD
651
652 test_go = 1;
653
3967a8a8
MD
654 remain = duration;
655 do {
656 remain = sleep(remain);
657 } while (remain > 0);
ab7d5fc6
MD
658
659 test_stop = 1;
660
e7e6ff7f 661end_pthread_join:
83e334d0
MJ
662 for (i_thr = 0; i_thr < nr_readers_created; i_thr++) {
663 err = pthread_join(tid_reader[i_thr], &tret);
e7e6ff7f
MD
664 if (err != 0) {
665 errno = err;
666 mainret = 1;
667 perror("pthread_join");
668 }
83e334d0 669 tot_reads += count_reader[i_thr];
ab7d5fc6 670 }
83e334d0
MJ
671 for (i_thr = 0; i_thr < nr_writers_created; i_thr++) {
672 err = pthread_join(tid_writer[i_thr], &tret);
e7e6ff7f
MD
673 if (err != 0) {
674 errno = err;
675 mainret = 1;
676 perror("pthread_join");
677 }
83e334d0
MJ
678 tot_writes += count_writer[i_thr].update_ops;
679 tot_add += count_writer[i_thr].add;
680 tot_add_exist += count_writer[i_thr].add_exist;
681 tot_remove += count_writer[i_thr].remove;
ab7d5fc6 682 }
7ed7682f
MD
683
684 /* teardown counter thread */
685 act.sa_handler = SIG_IGN;
686 act.sa_flags = SA_RESTART;
687 ret = sigaction(SIGUSR2, &act, NULL);
688 if (ret == -1) {
e7e6ff7f 689 mainret = 1;
7ed7682f 690 perror("sigaction");
7ed7682f
MD
691 }
692 {
693 char msg[1] = { 0x42 };
bd23a6c6 694 ssize_t sret;
3fb1173c
MD
695
696 do {
bd23a6c6
MJ
697 sret = write(count_pipe[1], msg, 1); /* wakeup thread */
698 } while (sret == -1L && errno == EINTR);
7ed7682f
MD
699 }
700 err = pthread_join(tid_count, &tret);
e7e6ff7f
MD
701 if (err != 0) {
702 errno = err;
703 mainret = 1;
704 perror("pthread_join");
705 }
7ed7682f 706
e7e6ff7f
MD
707end_close_pipe:
708 for (i = 0; i < 2; i++) {
709 err = close(count_pipe[i]);
710 if (err) {
711 mainret = 1;
712 perror("close pipe");
713 }
714 }
33c7c748 715 fflush(stdout);
e7e6ff7f 716end_online:
59e371e3
MD
717 rcu_thread_online();
718 rcu_read_lock();
175ec0eb 719 printf("Counting nodes... ");
caf3653d 720 cds_lfht_count_nodes(test_ht, &approx_before, &count, &approx_after);
175ec0eb
MD
721 printf("done.\n");
722 test_delete_all_nodes(test_ht);
59e371e3
MD
723 rcu_read_unlock();
724 rcu_thread_offline();
caf3653d 725 if (count) {
d933dd0e 726 printf("Approximation before node accounting: %ld nodes.\n",
973e5e1b 727 approx_before);
175ec0eb 728 printf("Nodes deleted from hash table before destroy: "
caf3653d
MD
729 "%lu nodes.\n",
730 count);
d933dd0e 731 printf("Approximation after node accounting: %ld nodes.\n",
973e5e1b
MD
732 approx_after);
733 }
e7e6ff7f 734
b7d619b0 735 ret = cds_lfht_destroy(test_ht, NULL);
e7e6ff7f 736 if (ret) {
33c7c748 737 printf_verbose("final delete aborted\n");
e7e6ff7f
MD
738 mainret = 1;
739 } else {
33c7c748 740 printf_verbose("final delete success\n");
e7e6ff7f 741 }
ab7d5fc6
MD
742 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
743 tot_writes);
e7e6ff7f 744 nr_leaked = (long long) tot_add + init_populate - tot_remove - count;
ab7d5fc6
MD
745 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu "
746 "nr_writers %3u "
d837911d 747 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu "
cd1ae16a 748 "nr_add %12llu nr_add_fail %12llu nr_remove %12llu nr_leaked %12lld\n",
ab7d5fc6 749 argv[0], duration, nr_readers, rduration,
d837911d 750 nr_writers, wdelay, tot_reads, tot_writes,
3c16bf4b 751 tot_reads + tot_writes, tot_add, tot_add_exist, tot_remove,
e7e6ff7f
MD
752 nr_leaked);
753 if (nr_leaked != 0) {
754 mainret = 1;
755 printf("WARNING: %lld nodes were leaked!\n", nr_leaked);
756 }
757
59e371e3 758 rcu_unregister_thread();
e7e6ff7f 759end_free_call_rcu_data:
3a22f1dd 760 free_all_cpu_call_rcu_data();
ab7d5fc6 761 free(count_writer);
e7e6ff7f
MD
762end_free_count_reader:
763 free(count_reader);
764end_free_tid_writer:
765 free(tid_writer);
766end_free_tid_reader:
767 free(tid_reader);
768end:
769 if (!mainret)
770 exit(EXIT_SUCCESS);
771 else
772 exit(EXIT_FAILURE);
ab7d5fc6 773}
This page took 0.090769 seconds and 4 git commands to generate.