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