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