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