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