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