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