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