tests urcu bp: use lib rather than recompile source
[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
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{
94df6318
MD
204 printf_verbose("thread_begin %s, tid %lu\n",
205 "counter", urcu_get_thread_id());
7ed7682f
MD
206
207 rcu_register_thread();
208
209 for (;;) {
caf3653d 210 unsigned long count;
d933dd0e 211 long approx_before, approx_after;
7ed7682f
MD
212 ssize_t len;
213 char buf[1];
214
215 rcu_thread_offline();
216 len = read(count_pipe[0], buf, 1);
217 rcu_thread_online();
8ed51e04 218 if (caa_unlikely(!test_duration_read()))
7ed7682f
MD
219 break;
220 if (len != 1)
221 continue;
222 /* Accounting */
223 printf("Counting nodes... ");
224 fflush(stdout);
225 rcu_read_lock();
caf3653d 226 cds_lfht_count_nodes(test_ht, &approx_before, &count,
7ed7682f
MD
227 &approx_after);
228 rcu_read_unlock();
229 printf("done.\n");
d933dd0e 230 printf("Approximation before node accounting: %ld nodes.\n",
7ed7682f
MD
231 approx_before);
232 printf("Accounting of nodes in the hash table: "
caf3653d
MD
233 "%lu nodes.\n",
234 count);
d933dd0e 235 printf("Approximation after node accounting: %ld nodes.\n",
7ed7682f
MD
236 approx_after);
237 }
238 rcu_unregister_thread();
239 return NULL;
240}
241
abc490a1
MD
242void free_node_cb(struct rcu_head *head)
243{
3c692076 244 struct lfht_test_node *node =
81d91005 245 caa_container_of(head, struct lfht_test_node, head);
abc490a1
MD
246 free(node);
247}
248
175ec0eb
MD
249static
250void test_delete_all_nodes(struct cds_lfht *ht)
251{
252 struct cds_lfht_iter iter;
3c692076 253 struct lfht_test_node *node;
175ec0eb
MD
254 unsigned long count = 0;
255
6d320126 256 cds_lfht_for_each_entry(ht, &iter, node, node) {
175ec0eb
MD
257 int ret;
258
bc8c3c74 259 ret = cds_lfht_del(test_ht, cds_lfht_iter_get_node(&iter));
175ec0eb 260 assert(!ret);
81d91005 261 call_rcu(&node->head, free_node_cb);
175ec0eb
MD
262 count++;
263 }
264 printf("deleted %lu nodes.\n", count);
265}
266
ab7d5fc6
MD
267void show_usage(int argc, char **argv)
268{
06637138
MD
269 printf("Usage : %s nr_readers nr_writers duration (s) <OPTIONS>\n",
270 argv[0]);
271 printf("OPTIONS:\n");
ab7d5fc6 272#ifdef DEBUG_YIELD
48ed1c18 273 printf(" [-r] [-w] (yield reader and/or writer)\n");
ab7d5fc6 274#endif
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;
3967a8a8
MD
315 struct sigaction act;
316 unsigned int remain;
e7e6ff7f
MD
317 unsigned int nr_readers_created = 0, nr_writers_created = 0;
318 long long nr_leaked;
ab7d5fc6
MD
319
320 if (argc < 4) {
321 show_usage(argc, argv);
e7e6ff7f
MD
322 mainret = 1;
323 goto end;
ab7d5fc6
MD
324 }
325
326 err = sscanf(argv[1], "%u", &nr_readers);
327 if (err != 1) {
328 show_usage(argc, argv);
e7e6ff7f
MD
329 mainret = 1;
330 goto end;
ab7d5fc6
MD
331 }
332
333 err = sscanf(argv[2], "%u", &nr_writers);
334 if (err != 1) {
335 show_usage(argc, argv);
e7e6ff7f
MD
336 mainret = 1;
337 goto end;
ab7d5fc6
MD
338 }
339
340 err = sscanf(argv[3], "%lu", &duration);
341 if (err != 1) {
342 show_usage(argc, argv);
e7e6ff7f
MD
343 mainret = 1;
344 goto end;
ab7d5fc6
MD
345 }
346
347 for (i = 4; i < argc; i++) {
348 if (argv[i][0] != '-')
349 continue;
350 switch (argv[i][1]) {
351#ifdef DEBUG_YIELD
352 case 'r':
353 yield_active |= YIELD_READ;
354 break;
355 case 'w':
356 yield_active |= YIELD_WRITE;
357 break;
358#endif
359 case 'a':
360 if (argc < i + 2) {
361 show_usage(argc, argv);
e7e6ff7f
MD
362 mainret = 1;
363 goto end;
ab7d5fc6
MD
364 }
365 a = atoi(argv[++i]);
366 cpu_affinities[next_aff++] = a;
367 use_affinity = 1;
368 printf_verbose("Adding CPU %d affinity\n", a);
369 break;
370 case 'c':
371 if (argc < i + 2) {
372 show_usage(argc, argv);
e7e6ff7f
MD
373 mainret = 1;
374 goto end;
ab7d5fc6
MD
375 }
376 rduration = atol(argv[++i]);
377 break;
378 case 'd':
379 if (argc < i + 2) {
380 show_usage(argc, argv);
e7e6ff7f
MD
381 mainret = 1;
382 goto end;
ab7d5fc6
MD
383 }
384 wdelay = atol(argv[++i]);
385 break;
386 case 'v':
387 verbose_mode = 1;
388 break;
6d5c0ca9
MD
389 case 'h':
390 if (argc < i + 2) {
391 show_usage(argc, argv);
e7e6ff7f
MD
392 mainret = 1;
393 goto end;
6d5c0ca9
MD
394 }
395 init_hash_size = atol(argv[++i]);
396 break;
32becef6
LJ
397 case 'm':
398 if (argc < i + 2) {
399 show_usage(argc, argv);
e7e6ff7f
MD
400 mainret = 1;
401 goto end;
32becef6
LJ
402 }
403 min_hash_alloc_size = atol(argv[++i]);
404 break;
b99436d7
LJ
405 case 'n':
406 if (argc < i + 2) {
407 show_usage(argc, argv);
e7e6ff7f
MD
408 mainret = 1;
409 goto end;
b99436d7
LJ
410 }
411 max_hash_buckets_size = atol(argv[++i]);
412 break;
6d5c0ca9 413 case 'u':
48ed1c18
MD
414 if (add_replace) {
415 printf("Please specify at most one of -s or -u.\n");
416 exit(-1);
417 }
6d5c0ca9
MD
418 add_unique = 1;
419 break;
48ed1c18
MD
420 case 's':
421 if (add_unique) {
422 printf("Please specify at most one of -s or -u.\n");
423 exit(-1);
424 }
425 add_replace = 1;
426 break;
6d5c0ca9
MD
427 case 'i':
428 add_only = 1;
429 break;
cd1ae16a
MD
430 case 'k':
431 init_populate = atol(argv[++i]);
432 break;
151e7a93
MD
433 case 'A':
434 opt_auto_resize = 1;
435 break;
c0b8a865
LJ
436 case 'B':
437 if (argc < i + 2) {
438 show_usage(argc, argv);
e7e6ff7f
MD
439 mainret = 1;
440 goto end;
c0b8a865
LJ
441 }
442 i++;
443 if (!strcmp("order", argv[i]))
444 memory_backend = &cds_lfht_mm_order;
445 else if (!strcmp("chunk", argv[i]))
446 memory_backend = &cds_lfht_mm_chunk;
447 else if (!strcmp("mmap", argv[i]))
448 memory_backend = &cds_lfht_mm_mmap;
e7e6ff7f 449 else {
c0b8a865 450 printf("Please specify memory backend with order|chunk|mmap.\n");
e7e6ff7f
MD
451 mainret = 1;
452 goto end;
c0b8a865
LJ
453 }
454 break;
8008a032
MD
455 case 'R':
456 lookup_pool_offset = atol(argv[++i]);
457 break;
458 case 'S':
459 write_pool_offset = atol(argv[++i]);
460 break;
461 case 'T':
462 init_pool_offset = atol(argv[++i]);
463 break;
d837911d
MD
464 case 'M':
465 lookup_pool_size = atol(argv[++i]);
466 break;
467 case 'N':
468 write_pool_size = atol(argv[++i]);
469 break;
470 case 'O':
471 init_pool_size = atol(argv[++i]);
472 break;
59b10634
MD
473 case 'V':
474 validate_lookup = 1;
475 break;
20adf780
MD
476 case 'U':
477 test_choice = TEST_HASH_UNIQUE;
478 break;
495913bf
MD
479 case 'C':
480 nr_hash_chains = atol(argv[++i]);
481 break;
ab7d5fc6
MD
482 }
483 }
484
6d5c0ca9
MD
485 /* Check if hash size is power of 2 */
486 if (init_hash_size && init_hash_size & (init_hash_size - 1)) {
b99436d7 487 printf("Error: Initial number of buckets (%lu) is not a power of 2.\n",
6d5c0ca9 488 init_hash_size);
e7e6ff7f
MD
489 mainret = 1;
490 goto end;
6d5c0ca9
MD
491 }
492
d0d8f9aa 493 if (min_hash_alloc_size && min_hash_alloc_size & (min_hash_alloc_size - 1)) {
b99436d7 494 printf("Error: Minimum number of allocated buckets (%lu) is not a power of 2.\n",
32becef6 495 min_hash_alloc_size);
e7e6ff7f
MD
496 mainret = 1;
497 goto end;
32becef6
LJ
498 }
499
b99436d7
LJ
500 if (max_hash_buckets_size && max_hash_buckets_size & (max_hash_buckets_size - 1)) {
501 printf("Error: Maximum number of buckets (%lu) is not a power of 2.\n",
502 max_hash_buckets_size);
e7e6ff7f
MD
503 mainret = 1;
504 goto end;
b99436d7
LJ
505 }
506
3967a8a8
MD
507 memset(&act, 0, sizeof(act));
508 ret = sigemptyset(&act.sa_mask);
509 if (ret == -1) {
510 perror("sigemptyset");
e7e6ff7f
MD
511 mainret = 1;
512 goto end;
3967a8a8 513 }
f52c1ef7 514 act.sa_handler = get_sigusr1_cb();
3967a8a8
MD
515 act.sa_flags = SA_RESTART;
516 ret = sigaction(SIGUSR1, &act, NULL);
517 if (ret == -1) {
518 perror("sigaction");
e7e6ff7f
MD
519 mainret = 1;
520 goto end;
7ed7682f
MD
521 }
522
f52c1ef7 523 act.sa_handler = get_sigusr2_cb();
973e5e1b
MD
524 act.sa_flags = SA_RESTART;
525 ret = sigaction(SIGUSR2, &act, NULL);
526 if (ret == -1) {
527 perror("sigaction");
e7e6ff7f
MD
528 mainret = 1;
529 goto end;
973e5e1b 530 }
3967a8a8 531
ab7d5fc6
MD
532 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
533 duration, nr_readers, nr_writers);
534 printf_verbose("Writer delay : %lu loops.\n", wdelay);
535 printf_verbose("Reader duration : %lu loops.\n", rduration);
6d5c0ca9
MD
536 printf_verbose("Mode:%s%s.\n",
537 add_only ? " add only" : " add/remove",
48ed1c18 538 add_unique ? " uniquify" : ( add_replace ? " replace" : " insert"));
b99436d7
LJ
539 printf_verbose("Initial number of buckets: %lu buckets.\n", init_hash_size);
540 printf_verbose("Minimum number of allocated buckets: %lu buckets.\n", min_hash_alloc_size);
541 printf_verbose("Maximum number of buckets: %lu buckets.\n", max_hash_buckets_size);
5dc00396
MD
542 printf_verbose("Init pool size offset %lu size %lu.\n",
543 init_pool_offset, init_pool_size);
544 printf_verbose("Lookup pool size offset %lu size %lu.\n",
545 lookup_pool_offset, lookup_pool_size);
546 printf_verbose("Update pool size offset %lu size %lu.\n",
547 write_pool_offset, write_pool_size);
495913bf
MD
548 printf_verbose("Number of hash chains: %lu.\n",
549 nr_hash_chains);
94df6318
MD
550 printf_verbose("thread %-6s, tid %lu\n",
551 "main", urcu_get_thread_id());
ab7d5fc6 552
9aa14175 553 tid_reader = calloc(nr_readers, sizeof(*tid_reader));
e7e6ff7f
MD
554 if (!tid_reader) {
555 mainret = 1;
556 goto end;
557 }
9aa14175 558 tid_writer = calloc(nr_writers, sizeof(*tid_writer));
e7e6ff7f
MD
559 if (!tid_writer) {
560 mainret = 1;
561 goto end_free_tid_reader;
562 }
9aa14175 563 count_reader = calloc(nr_readers, sizeof(*count_reader));
e7e6ff7f
MD
564 if (!count_reader) {
565 mainret = 1;
566 goto end_free_tid_writer;
567 }
9aa14175 568 count_writer = calloc(nr_writers, sizeof(*count_writer));
e7e6ff7f
MD
569 if (!count_writer) {
570 mainret = 1;
571 goto end_free_count_reader;
572 }
48ed1c18
MD
573
574 err = create_all_cpu_call_rcu_data(0);
8bcbd94a
MD
575 if (err) {
576 printf("Per-CPU call_rcu() worker threads unavailable. Using default global worker thread.\n");
577 }
48ed1c18 578
c0b8a865
LJ
579 if (memory_backend) {
580 test_ht = _cds_lfht_new(init_hash_size, min_hash_alloc_size,
581 max_hash_buckets_size,
582 (opt_auto_resize ? CDS_LFHT_AUTO_RESIZE : 0) |
583 CDS_LFHT_ACCOUNTING, memory_backend,
584 &rcu_flavor, NULL);
585 } else {
586 test_ht = cds_lfht_new(init_hash_size, min_hash_alloc_size,
587 max_hash_buckets_size,
588 (opt_auto_resize ? CDS_LFHT_AUTO_RESIZE : 0) |
589 CDS_LFHT_ACCOUNTING, NULL);
590 }
0d02f4b5
MD
591 if (!test_ht) {
592 printf("Error allocating hash table.\n");
e7e6ff7f
MD
593 mainret = 1;
594 goto end_free_call_rcu_data;
0d02f4b5 595 }
c0b8a865 596
48ed1c18 597 /*
c0b8a865 598 * Hash Population needs to be seen as a RCU reader
48ed1c18
MD
599 * thread from the point of view of resize.
600 */
601 rcu_register_thread();
e7e6ff7f 602 ret = (get_populate_hash_cb())();
5dc45f25 603 assert(!ret);
59e371e3
MD
604
605 rcu_thread_offline();
14756542 606
ab7d5fc6
MD
607 next_aff = 0;
608
e7e6ff7f
MD
609 ret = pipe(count_pipe);
610 if (ret == -1) {
611 perror("pipe");
612 mainret = 1;
613 goto end_online;
614 }
615
616 /* spawn counter thread */
617 err = pthread_create(&tid_count, NULL, thr_count,
618 NULL);
619 if (err != 0) {
620 errno = err;
621 mainret = 1;
622 perror("pthread_create");
623 goto end_close_pipe;
624 }
625
ab7d5fc6 626 for (i = 0; i < nr_readers; i++) {
18ca7a5b 627 err = pthread_create(&tid_reader[i],
f52c1ef7 628 NULL, get_thr_reader_cb(),
ab7d5fc6 629 &count_reader[i]);
e7e6ff7f
MD
630 if (err != 0) {
631 errno = err;
632 mainret = 1;
633 perror("pthread_create");
634 goto end_pthread_join;
635 }
636 nr_readers_created++;
ab7d5fc6
MD
637 }
638 for (i = 0; i < nr_writers; i++) {
18ca7a5b 639 err = pthread_create(&tid_writer[i],
f52c1ef7 640 NULL, get_thr_writer_cb(),
ab7d5fc6 641 &count_writer[i]);
e7e6ff7f
MD
642 if (err != 0) {
643 errno = err;
644 mainret = 1;
645 perror("pthread_create");
646 goto end_pthread_join;
647 }
648 nr_writers_created++;
ab7d5fc6
MD
649 }
650
abc490a1 651 cmm_smp_mb();
ab7d5fc6
MD
652
653 test_go = 1;
654
3967a8a8
MD
655 remain = duration;
656 do {
657 remain = sleep(remain);
658 } while (remain > 0);
ab7d5fc6
MD
659
660 test_stop = 1;
661
e7e6ff7f
MD
662end_pthread_join:
663 for (i = 0; i < nr_readers_created; i++) {
ab7d5fc6 664 err = pthread_join(tid_reader[i], &tret);
e7e6ff7f
MD
665 if (err != 0) {
666 errno = err;
667 mainret = 1;
668 perror("pthread_join");
669 }
ab7d5fc6
MD
670 tot_reads += count_reader[i];
671 }
e7e6ff7f 672 for (i = 0; i < nr_writers_created; i++) {
ab7d5fc6 673 err = pthread_join(tid_writer[i], &tret);
e7e6ff7f
MD
674 if (err != 0) {
675 errno = err;
676 mainret = 1;
677 perror("pthread_join");
678 }
b8e1907c
MD
679 tot_writes += count_writer[i].update_ops;
680 tot_add += count_writer[i].add;
3c16bf4b 681 tot_add_exist += count_writer[i].add_exist;
b8e1907c 682 tot_remove += count_writer[i].remove;
ab7d5fc6 683 }
7ed7682f
MD
684
685 /* teardown counter thread */
686 act.sa_handler = SIG_IGN;
687 act.sa_flags = SA_RESTART;
688 ret = sigaction(SIGUSR2, &act, NULL);
689 if (ret == -1) {
e7e6ff7f 690 mainret = 1;
7ed7682f 691 perror("sigaction");
7ed7682f
MD
692 }
693 {
694 char msg[1] = { 0x42 };
3fb1173c
MD
695 ssize_t ret;
696
697 do {
698 ret = write(count_pipe[1], msg, 1); /* wakeup thread */
699 } while (ret == -1L && errno == EINTR);
7ed7682f
MD
700 }
701 err = pthread_join(tid_count, &tret);
e7e6ff7f
MD
702 if (err != 0) {
703 errno = err;
704 mainret = 1;
705 perror("pthread_join");
706 }
7ed7682f 707
e7e6ff7f
MD
708end_close_pipe:
709 for (i = 0; i < 2; i++) {
710 err = close(count_pipe[i]);
711 if (err) {
712 mainret = 1;
713 perror("close pipe");
714 }
715 }
33c7c748 716 fflush(stdout);
e7e6ff7f 717end_online:
59e371e3
MD
718 rcu_thread_online();
719 rcu_read_lock();
175ec0eb 720 printf("Counting nodes... ");
caf3653d 721 cds_lfht_count_nodes(test_ht, &approx_before, &count, &approx_after);
175ec0eb
MD
722 printf("done.\n");
723 test_delete_all_nodes(test_ht);
59e371e3
MD
724 rcu_read_unlock();
725 rcu_thread_offline();
caf3653d 726 if (count) {
d933dd0e 727 printf("Approximation before node accounting: %ld nodes.\n",
973e5e1b 728 approx_before);
175ec0eb 729 printf("Nodes deleted from hash table before destroy: "
caf3653d
MD
730 "%lu nodes.\n",
731 count);
d933dd0e 732 printf("Approximation after node accounting: %ld nodes.\n",
973e5e1b
MD
733 approx_after);
734 }
e7e6ff7f 735
b7d619b0 736 ret = cds_lfht_destroy(test_ht, NULL);
e7e6ff7f 737 if (ret) {
33c7c748 738 printf_verbose("final delete aborted\n");
e7e6ff7f
MD
739 mainret = 1;
740 } else {
33c7c748 741 printf_verbose("final delete success\n");
e7e6ff7f 742 }
ab7d5fc6
MD
743 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
744 tot_writes);
e7e6ff7f 745 nr_leaked = (long long) tot_add + init_populate - tot_remove - count;
ab7d5fc6
MD
746 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu "
747 "nr_writers %3u "
d837911d 748 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu "
cd1ae16a 749 "nr_add %12llu nr_add_fail %12llu nr_remove %12llu nr_leaked %12lld\n",
ab7d5fc6 750 argv[0], duration, nr_readers, rduration,
d837911d 751 nr_writers, wdelay, tot_reads, tot_writes,
3c16bf4b 752 tot_reads + tot_writes, tot_add, tot_add_exist, tot_remove,
e7e6ff7f
MD
753 nr_leaked);
754 if (nr_leaked != 0) {
755 mainret = 1;
756 printf("WARNING: %lld nodes were leaked!\n", nr_leaked);
757 }
758
59e371e3 759 rcu_unregister_thread();
e7e6ff7f 760end_free_call_rcu_data:
3a22f1dd 761 free_all_cpu_call_rcu_data();
ab7d5fc6 762 free(count_writer);
e7e6ff7f
MD
763end_free_count_reader:
764 free(count_reader);
765end_free_tid_writer:
766 free(tid_writer);
767end_free_tid_reader:
768 free(tid_reader);
769end:
770 if (!mainret)
771 exit(EXIT_SUCCESS);
772 else
773 exit(EXIT_FAILURE);
ab7d5fc6 774}
This page took 0.067999 seconds and 4 git commands to generate.