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