Commit | Line | Data |
---|---|---|
44151f89 MD |
1 | /* |
2 | * test_urcu_ja.c | |
3 | * | |
4 | * Userspace RCU library - test program | |
5 | * | |
6 | * Copyright 2009-2012 - Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca> | |
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 | |
24 | #include "test_urcu_ja.h" | |
a2a7ff59 | 25 | #include <inttypes.h> |
582a6ade | 26 | #include <stdint.h> |
44151f89 MD |
27 | |
28 | DEFINE_URCU_TLS(unsigned int, rand_lookup); | |
29 | DEFINE_URCU_TLS(unsigned long, nr_add); | |
30 | DEFINE_URCU_TLS(unsigned long, nr_addexist); | |
31 | DEFINE_URCU_TLS(unsigned long, nr_del); | |
32 | DEFINE_URCU_TLS(unsigned long, nr_delnoent); | |
33 | DEFINE_URCU_TLS(unsigned long, lookup_fail); | |
34 | DEFINE_URCU_TLS(unsigned long, lookup_ok); | |
35 | ||
36 | struct cds_ja *test_ja; | |
37 | ||
38 | volatile int test_go, test_stop; | |
39 | ||
40 | unsigned long wdelay; | |
41 | ||
42 | unsigned long duration; | |
43 | ||
44 | /* read-side C.S. duration, in loops */ | |
45 | unsigned long rduration; | |
46 | ||
47 | unsigned long init_populate; | |
48 | int add_only; | |
49 | ||
50 | unsigned long init_pool_offset, lookup_pool_offset, write_pool_offset; | |
51 | unsigned long init_pool_size = DEFAULT_RAND_POOL, | |
52 | lookup_pool_size = DEFAULT_RAND_POOL, | |
53 | write_pool_size = DEFAULT_RAND_POOL; | |
54 | int validate_lookup; | |
55 | ||
56 | int count_pipe[2]; | |
57 | ||
58 | int verbose_mode; | |
59 | ||
60 | unsigned int cpu_affinities[NR_CPUS]; | |
61 | unsigned int next_aff = 0; | |
62 | int use_affinity = 0; | |
63 | ||
64 | pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER; | |
65 | ||
66 | DEFINE_URCU_TLS(unsigned long long, nr_writes); | |
67 | DEFINE_URCU_TLS(unsigned long long, nr_reads); | |
68 | ||
69 | unsigned int nr_readers; | |
70 | unsigned int nr_writers; | |
71 | ||
72 | static pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER; | |
73 | ||
74 | void set_affinity(void) | |
75 | { | |
76 | cpu_set_t mask; | |
77 | int cpu; | |
78 | int ret; | |
79 | ||
80 | if (!use_affinity) | |
81 | return; | |
82 | ||
83 | #if HAVE_SCHED_SETAFFINITY | |
84 | ret = pthread_mutex_lock(&affinity_mutex); | |
85 | if (ret) { | |
86 | perror("Error in pthread mutex lock"); | |
87 | exit(-1); | |
88 | } | |
89 | cpu = cpu_affinities[next_aff++]; | |
90 | ret = pthread_mutex_unlock(&affinity_mutex); | |
91 | if (ret) { | |
92 | perror("Error in pthread mutex unlock"); | |
93 | exit(-1); | |
94 | } | |
95 | CPU_ZERO(&mask); | |
96 | CPU_SET(cpu, &mask); | |
97 | #if SCHED_SETAFFINITY_ARGS == 2 | |
98 | sched_setaffinity(0, &mask); | |
99 | #else | |
100 | sched_setaffinity(0, sizeof(mask), &mask); | |
101 | #endif | |
102 | #endif /* HAVE_SCHED_SETAFFINITY */ | |
103 | } | |
104 | ||
105 | void rcu_copy_mutex_lock(void) | |
106 | { | |
107 | int ret; | |
108 | ret = pthread_mutex_lock(&rcu_copy_mutex); | |
109 | if (ret) { | |
110 | perror("Error in pthread mutex lock"); | |
111 | exit(-1); | |
112 | } | |
113 | } | |
114 | ||
115 | void rcu_copy_mutex_unlock(void) | |
116 | { | |
117 | int ret; | |
118 | ||
119 | ret = pthread_mutex_unlock(&rcu_copy_mutex); | |
120 | if (ret) { | |
121 | perror("Error in pthread mutex unlock"); | |
122 | exit(-1); | |
123 | } | |
124 | } | |
125 | ||
44151f89 MD |
126 | void free_node_cb(struct rcu_head *head) |
127 | { | |
128 | struct ja_test_node *node = | |
3d8fe307 | 129 | caa_container_of(head, struct ja_test_node, node.head); |
44151f89 MD |
130 | free(node); |
131 | } | |
132 | ||
3d8fe307 | 133 | #if 0 |
44151f89 MD |
134 | static |
135 | void test_delete_all_nodes(struct cds_lfht *ht) | |
136 | { | |
137 | struct cds_lfht_iter iter; | |
138 | struct lfht_test_node *node; | |
139 | unsigned long count = 0; | |
140 | ||
141 | cds_lfht_for_each_entry(ht, &iter, node, node) { | |
142 | int ret; | |
143 | ||
144 | ret = cds_lfht_del(test_ht, cds_lfht_iter_get_node(&iter)); | |
145 | assert(!ret); | |
146 | call_rcu(&node->head, free_node_cb); | |
147 | count++; | |
148 | } | |
149 | printf("deleted %lu nodes.\n", count); | |
150 | } | |
151 | #endif | |
152 | ||
153 | void show_usage(int argc, char **argv) | |
154 | { | |
155 | printf("Usage : %s nr_readers nr_writers duration (s)\n", argv[0]); | |
156 | #ifdef DEBUG_YIELD | |
157 | printf(" [-r] [-w] (yield reader and/or writer)\n"); | |
158 | #endif | |
159 | printf(" [-d delay] (writer period (us))\n"); | |
160 | printf(" [-c duration] (reader C.S. duration (in loops))\n"); | |
161 | printf(" [-v] (verbose output)\n"); | |
162 | printf(" [-a cpu#] [-a cpu#]... (affinity)\n"); | |
163 | printf(" [not -u nor -s] Add entries (supports redundant keys).\n"); | |
164 | printf(" [-i] Add only (no removal).\n"); | |
165 | printf(" [-k nr_nodes] Number of nodes to insert initially.\n"); | |
166 | printf(" [-R offset] Lookup pool offset.\n"); | |
167 | printf(" [-S offset] Write pool offset.\n"); | |
168 | printf(" [-T offset] Init pool offset.\n"); | |
169 | printf(" [-M size] Lookup pool size.\n"); | |
170 | printf(" [-N size] Write pool size.\n"); | |
171 | printf(" [-O size] Init pool size.\n"); | |
172 | printf(" [-V] Validate lookups of init values (use with filled init pool, same lookup range, with different write range).\n"); | |
173 | printf("\n\n"); | |
174 | } | |
175 | ||
582a6ade MD |
176 | |
177 | static | |
178 | int test_8bit_key(void) | |
179 | { | |
180 | int ret; | |
181 | uint64_t key; | |
182 | ||
183 | /* Test with 8-bit key */ | |
184 | test_ja = cds_ja_new(8); | |
185 | if (!test_ja) { | |
186 | printf("Error allocating judy array.\n"); | |
187 | return -1; | |
188 | } | |
189 | ||
190 | /* Add keys */ | |
191 | printf("Test #1: add keys (8-bit).\n"); | |
192 | for (key = 0; key < 200; key++) { | |
193 | struct ja_test_node *node = | |
194 | calloc(sizeof(*node), 1); | |
195 | ||
196 | ja_test_node_init(node, key); | |
197 | rcu_read_lock(); | |
198 | ret = cds_ja_add(test_ja, key, &node->node); | |
199 | rcu_read_unlock(); | |
200 | if (ret) { | |
201 | fprintf(stderr, "Error (%d) adding node %" PRIu64 "\n", | |
202 | ret, key); | |
203 | assert(0); | |
204 | } | |
205 | } | |
206 | printf("OK\n"); | |
207 | ||
208 | printf("Test #2: successful key lookup (8-bit).\n"); | |
209 | for (key = 0; key < 200; key++) { | |
af3cbd45 | 210 | struct cds_hlist_head head; |
582a6ade MD |
211 | |
212 | rcu_read_lock(); | |
213 | head = cds_ja_lookup(test_ja, key); | |
af3cbd45 | 214 | if (cds_hlist_empty(&head)) { |
582a6ade MD |
215 | fprintf(stderr, "Error lookup node %" PRIu64 "\n", key); |
216 | assert(0); | |
217 | } | |
218 | rcu_read_unlock(); | |
219 | } | |
220 | printf("OK\n"); | |
221 | printf("Test #3: unsuccessful key lookup (8-bit).\n"); | |
222 | for (key = 200; key < 240; key++) { | |
af3cbd45 | 223 | struct cds_hlist_head head; |
582a6ade MD |
224 | |
225 | rcu_read_lock(); | |
226 | head = cds_ja_lookup(test_ja, key); | |
af3cbd45 | 227 | if (!cds_hlist_empty(&head)) { |
582a6ade MD |
228 | fprintf(stderr, |
229 | "Error unexpected lookup node %" PRIu64 "\n", | |
230 | key); | |
231 | assert(0); | |
232 | } | |
233 | rcu_read_unlock(); | |
234 | } | |
235 | printf("OK\n"); | |
3d8fe307 MD |
236 | printf("Test #4: remove keys (8-bit).\n"); |
237 | for (key = 0; key < 200; key++) { | |
238 | struct cds_hlist_head head; | |
239 | struct ja_test_node *node; | |
240 | ||
241 | rcu_read_lock(); | |
242 | head = cds_ja_lookup(test_ja, key); | |
243 | node = cds_hlist_first_entry_rcu(&head, struct ja_test_node, node.list); | |
244 | if (!node) { | |
245 | fprintf(stderr, "Error lookup node %" PRIu64 "\n", key); | |
246 | assert(0); | |
247 | } | |
248 | ret = cds_ja_del(test_ja, key, &node->node); | |
249 | if (ret) { | |
250 | fprintf(stderr, "Error (%d) removing node %" PRIu64 "\n", ret, key); | |
251 | assert(0); | |
252 | } | |
253 | call_rcu(&node->node.head, free_node_cb); | |
4d6ef45e MD |
254 | head = cds_ja_lookup(test_ja, key); |
255 | if (!cds_hlist_empty(&head)) { | |
256 | fprintf(stderr, "Error lookup %" PRIu64 ": %p (after delete) failed. Node is not expected.\n", key, head.next); | |
257 | assert(0); | |
258 | } | |
3d8fe307 MD |
259 | rcu_read_unlock(); |
260 | } | |
261 | printf("OK\n"); | |
582a6ade | 262 | |
3d8fe307 | 263 | ret = cds_ja_destroy(test_ja, free_node_cb); |
582a6ade MD |
264 | if (ret) { |
265 | fprintf(stderr, "Error destroying judy array\n"); | |
266 | return -1; | |
267 | } | |
268 | return 0; | |
269 | } | |
270 | ||
271 | static | |
272 | int test_16bit_key(void) | |
273 | { | |
274 | int ret; | |
275 | uint64_t key; | |
276 | ||
277 | /* Test with 16-bit key */ | |
278 | test_ja = cds_ja_new(16); | |
279 | if (!test_ja) { | |
280 | printf("Error allocating judy array.\n"); | |
281 | return -1; | |
282 | } | |
283 | ||
284 | /* Add keys */ | |
285 | printf("Test #1: add keys (16-bit).\n"); | |
4d6ef45e MD |
286 | for (key = 0; key < 10000; key++) { |
287 | //for (key = 0; key < 65536; key+=256) { | |
582a6ade MD |
288 | struct ja_test_node *node = |
289 | calloc(sizeof(*node), 1); | |
290 | ||
291 | ja_test_node_init(node, key); | |
292 | rcu_read_lock(); | |
293 | ret = cds_ja_add(test_ja, key, &node->node); | |
294 | rcu_read_unlock(); | |
295 | if (ret) { | |
296 | fprintf(stderr, "Error (%d) adding node %" PRIu64 "\n", | |
297 | ret, key); | |
298 | assert(0); | |
299 | } | |
300 | } | |
301 | printf("OK\n"); | |
302 | ||
303 | printf("Test #2: successful key lookup (16-bit).\n"); | |
4d6ef45e MD |
304 | for (key = 0; key < 10000; key++) { |
305 | //for (key = 0; key < 65536; key+=256) { | |
af3cbd45 | 306 | struct cds_hlist_head head; |
582a6ade MD |
307 | |
308 | rcu_read_lock(); | |
309 | head = cds_ja_lookup(test_ja, key); | |
af3cbd45 | 310 | if (cds_hlist_empty(&head)) { |
582a6ade MD |
311 | fprintf(stderr, "Error lookup node %" PRIu64 "\n", key); |
312 | assert(0); | |
313 | } | |
314 | rcu_read_unlock(); | |
315 | } | |
316 | printf("OK\n"); | |
317 | printf("Test #3: unsuccessful key lookup (16-bit).\n"); | |
318 | for (key = 11000; key <= 11002; key++) { | |
af3cbd45 | 319 | struct cds_hlist_head head; |
582a6ade MD |
320 | |
321 | rcu_read_lock(); | |
322 | head = cds_ja_lookup(test_ja, key); | |
af3cbd45 | 323 | if (!cds_hlist_empty(&head)) { |
582a6ade MD |
324 | fprintf(stderr, |
325 | "Error unexpected lookup node %" PRIu64 "\n", | |
326 | key); | |
327 | assert(0); | |
328 | } | |
329 | rcu_read_unlock(); | |
330 | } | |
331 | printf("OK\n"); | |
4d6ef45e MD |
332 | printf("Test #4: remove keys (16-bit).\n"); |
333 | for (key = 0; key < 10000; key++) { | |
334 | //for (key = 0; key < 65536; key+=256) { | |
335 | struct cds_hlist_head head; | |
336 | struct ja_test_node *node; | |
337 | ||
338 | rcu_read_lock(); | |
339 | head = cds_ja_lookup(test_ja, key); | |
340 | node = cds_hlist_first_entry_rcu(&head, struct ja_test_node, node.list); | |
341 | if (!node) { | |
342 | fprintf(stderr, "Error lookup node %" PRIu64 "\n", key); | |
343 | assert(0); | |
344 | } | |
345 | ret = cds_ja_del(test_ja, key, &node->node); | |
346 | if (ret) { | |
347 | fprintf(stderr, "Error (%d) removing node %" PRIu64 "\n", ret, key); | |
348 | assert(0); | |
349 | } | |
350 | call_rcu(&node->node.head, free_node_cb); | |
351 | head = cds_ja_lookup(test_ja, key); | |
352 | if (!cds_hlist_empty(&head)) { | |
353 | fprintf(stderr, "Error lookup %" PRIu64 ": %p (after delete) failed. Node is not expected.\n", key, head.next); | |
354 | assert(0); | |
355 | } | |
356 | rcu_read_unlock(); | |
357 | } | |
358 | printf("OK\n"); | |
582a6ade | 359 | |
3d8fe307 | 360 | ret = cds_ja_destroy(test_ja, free_node_cb); |
582a6ade MD |
361 | if (ret) { |
362 | fprintf(stderr, "Error destroying judy array\n"); | |
363 | return -1; | |
364 | } | |
365 | return 0; | |
366 | } | |
367 | ||
6fdf58e8 MD |
368 | /* |
369 | * nr_dup is number of nodes per key. | |
370 | */ | |
582a6ade | 371 | static |
6fdf58e8 | 372 | int test_sparse_key(unsigned int bits, int nr_dup) |
582a6ade | 373 | { |
582a6ade | 374 | uint64_t key, max_key; |
6fdf58e8 | 375 | int zerocount, i, ret; |
582a6ade MD |
376 | |
377 | if (bits == 64) | |
378 | max_key = UINT64_MAX; | |
379 | else | |
380 | max_key = (1ULL << bits) - 1; | |
381 | ||
382 | printf("Sparse key test begins for %u-bit keys\n", bits); | |
383 | /* Test with 16-bit key */ | |
384 | test_ja = cds_ja_new(bits); | |
385 | if (!test_ja) { | |
386 | printf("Error allocating judy array.\n"); | |
387 | return -1; | |
388 | } | |
389 | ||
390 | /* Add keys */ | |
391 | printf("Test #1: add keys (%u-bit).\n", bits); | |
6fdf58e8 MD |
392 | for (i = 0; i < nr_dup; i++) { |
393 | zerocount = 0; | |
394 | for (key = 0; key <= max_key && (key != 0 || zerocount < 1); key += 1ULL << (bits - 8)) { | |
395 | struct ja_test_node *node = | |
396 | calloc(sizeof(*node), 1); | |
582a6ade | 397 | |
6fdf58e8 MD |
398 | ja_test_node_init(node, key); |
399 | rcu_read_lock(); | |
400 | ret = cds_ja_add(test_ja, key, &node->node); | |
401 | rcu_read_unlock(); | |
402 | if (ret) { | |
403 | fprintf(stderr, "Error (%d) adding node %" PRIu64 "\n", | |
404 | ret, key); | |
405 | assert(0); | |
406 | } | |
407 | if (key == 0) | |
408 | zerocount++; | |
582a6ade | 409 | } |
582a6ade MD |
410 | } |
411 | printf("OK\n"); | |
412 | ||
413 | printf("Test #2: successful key lookup (%u-bit).\n", bits); | |
414 | zerocount = 0; | |
415 | for (key = 0; key <= max_key && (key != 0 || zerocount < 1); key += 1ULL << (bits - 8)) { | |
af3cbd45 | 416 | struct cds_hlist_head head; |
6fdf58e8 MD |
417 | struct ja_test_node *node; |
418 | struct cds_hlist_node *pos; | |
419 | int count = 0; | |
582a6ade MD |
420 | |
421 | rcu_read_lock(); | |
422 | head = cds_ja_lookup(test_ja, key); | |
af3cbd45 | 423 | if (cds_hlist_empty(&head)) { |
582a6ade MD |
424 | fprintf(stderr, "Error lookup node %" PRIu64 "\n", key); |
425 | assert(0); | |
426 | } | |
6fdf58e8 MD |
427 | cds_hlist_for_each_entry_rcu(node, pos, &head, node.list) { |
428 | count++; | |
429 | } | |
430 | if (count != nr_dup) { | |
431 | fprintf(stderr, "Unexpected number of match for key %" PRIu64 ", expected %d, got %d.\n", key, nr_dup, count); | |
432 | } | |
582a6ade MD |
433 | rcu_read_unlock(); |
434 | if (key == 0) | |
435 | zerocount++; | |
436 | } | |
437 | printf("OK\n"); | |
438 | if (bits > 8) { | |
439 | printf("Test #3: unsuccessful key lookup (%u-bit).\n", bits); | |
440 | zerocount = 0; | |
441 | for (key = 0; key <= max_key && (key != 0 || zerocount < 1); key += 1ULL << (bits - 8)) { | |
af3cbd45 | 442 | struct cds_hlist_head head; |
582a6ade MD |
443 | |
444 | rcu_read_lock(); | |
445 | head = cds_ja_lookup(test_ja, key + 42); | |
af3cbd45 | 446 | if (!cds_hlist_empty(&head)) { |
582a6ade MD |
447 | fprintf(stderr, |
448 | "Error unexpected lookup node %" PRIu64 "\n", | |
449 | key + 42); | |
450 | assert(0); | |
451 | } | |
452 | rcu_read_unlock(); | |
453 | if (key == 0) | |
454 | zerocount++; | |
455 | } | |
456 | printf("OK\n"); | |
457 | } | |
4d6ef45e MD |
458 | printf("Test #4: remove keys (16-bit).\n"); |
459 | zerocount = 0; | |
460 | for (key = 0; key <= max_key && (key != 0 || zerocount < 1); key += 1ULL << (bits - 8)) { | |
461 | struct cds_hlist_head head; | |
462 | struct ja_test_node *node; | |
6fdf58e8 MD |
463 | struct cds_hlist_node *pos; |
464 | int count = 0; | |
4d6ef45e MD |
465 | |
466 | rcu_read_lock(); | |
467 | head = cds_ja_lookup(test_ja, key); | |
6fdf58e8 MD |
468 | |
469 | ||
470 | cds_hlist_for_each_entry_rcu(node, pos, &head, node.list) { | |
471 | struct cds_hlist_head testhead; | |
472 | ||
473 | count++; | |
474 | if (!node) { | |
475 | fprintf(stderr, "Error lookup node %" PRIu64 "\n", key); | |
476 | assert(0); | |
477 | } | |
478 | ret = cds_ja_del(test_ja, key, &node->node); | |
479 | if (ret) { | |
480 | fprintf(stderr, "Error (%d) removing node %" PRIu64 "\n", ret, key); | |
481 | assert(0); | |
482 | } | |
483 | call_rcu(&node->node.head, free_node_cb); | |
484 | testhead = cds_ja_lookup(test_ja, key); | |
485 | if (count < nr_dup && cds_hlist_empty(&testhead)) { | |
486 | fprintf(stderr, "Error: no node found after deletion of some nodes of a key\n"); | |
487 | assert(0); | |
488 | } | |
4d6ef45e | 489 | } |
4d6ef45e MD |
490 | head = cds_ja_lookup(test_ja, key); |
491 | if (!cds_hlist_empty(&head)) { | |
492 | fprintf(stderr, "Error lookup %" PRIu64 ": %p (after delete) failed. Node is not expected.\n", key, head.next); | |
493 | assert(0); | |
494 | } | |
495 | rcu_read_unlock(); | |
496 | if (key == 0) | |
497 | zerocount++; | |
498 | } | |
499 | printf("OK\n"); | |
582a6ade | 500 | |
3d8fe307 | 501 | ret = cds_ja_destroy(test_ja, free_node_cb); |
582a6ade MD |
502 | if (ret) { |
503 | fprintf(stderr, "Error destroying judy array\n"); | |
504 | return -1; | |
505 | } | |
506 | printf("Test ends\n"); | |
507 | ||
508 | return 0; | |
509 | } | |
510 | ||
511 | ||
44151f89 MD |
512 | int main(int argc, char **argv) |
513 | { | |
514 | int err; | |
515 | pthread_t *tid_reader, *tid_writer; | |
516 | void *tret; | |
517 | unsigned long long *count_reader; | |
518 | struct wr_count *count_writer; | |
519 | unsigned long long tot_reads = 0, tot_writes = 0, | |
520 | tot_add = 0, tot_add_exist = 0, tot_remove = 0; | |
6fdf58e8 | 521 | int i, j, a, ret; |
44151f89 | 522 | unsigned int remain; |
a2a7ff59 | 523 | uint64_t key; |
44151f89 MD |
524 | |
525 | if (argc < 4) { | |
526 | show_usage(argc, argv); | |
527 | return -1; | |
528 | } | |
529 | ||
530 | err = sscanf(argv[1], "%u", &nr_readers); | |
531 | if (err != 1) { | |
532 | show_usage(argc, argv); | |
533 | return -1; | |
534 | } | |
535 | ||
536 | err = sscanf(argv[2], "%u", &nr_writers); | |
537 | if (err != 1) { | |
538 | show_usage(argc, argv); | |
539 | return -1; | |
540 | } | |
541 | ||
542 | err = sscanf(argv[3], "%lu", &duration); | |
543 | if (err != 1) { | |
544 | show_usage(argc, argv); | |
545 | return -1; | |
546 | } | |
547 | ||
548 | for (i = 4; i < argc; i++) { | |
549 | if (argv[i][0] != '-') | |
550 | continue; | |
551 | switch (argv[i][1]) { | |
552 | #ifdef DEBUG_YIELD | |
553 | case 'r': | |
554 | yield_active |= YIELD_READ; | |
555 | break; | |
556 | case 'w': | |
557 | yield_active |= YIELD_WRITE; | |
558 | break; | |
559 | #endif | |
560 | case 'a': | |
561 | if (argc < i + 2) { | |
562 | show_usage(argc, argv); | |
563 | return -1; | |
564 | } | |
565 | a = atoi(argv[++i]); | |
566 | cpu_affinities[next_aff++] = a; | |
567 | use_affinity = 1; | |
568 | printf_verbose("Adding CPU %d affinity\n", a); | |
569 | break; | |
570 | case 'c': | |
571 | if (argc < i + 2) { | |
572 | show_usage(argc, argv); | |
573 | return -1; | |
574 | } | |
575 | rduration = atol(argv[++i]); | |
576 | break; | |
577 | case 'd': | |
578 | if (argc < i + 2) { | |
579 | show_usage(argc, argv); | |
580 | return -1; | |
581 | } | |
582 | wdelay = atol(argv[++i]); | |
583 | break; | |
584 | case 'v': | |
585 | verbose_mode = 1; | |
586 | break; | |
587 | case 'i': | |
588 | add_only = 1; | |
589 | break; | |
590 | case 'k': | |
591 | init_populate = atol(argv[++i]); | |
592 | break; | |
593 | case 'R': | |
594 | lookup_pool_offset = atol(argv[++i]); | |
595 | break; | |
596 | case 'S': | |
597 | write_pool_offset = atol(argv[++i]); | |
598 | break; | |
599 | case 'T': | |
600 | init_pool_offset = atol(argv[++i]); | |
601 | break; | |
602 | case 'M': | |
603 | lookup_pool_size = atol(argv[++i]); | |
604 | break; | |
605 | case 'N': | |
606 | write_pool_size = atol(argv[++i]); | |
607 | break; | |
608 | case 'O': | |
609 | init_pool_size = atol(argv[++i]); | |
610 | break; | |
611 | case 'V': | |
612 | validate_lookup = 1; | |
613 | break; | |
614 | } | |
615 | } | |
616 | ||
617 | printf_verbose("running test for %lu seconds, %u readers, %u writers.\n", | |
618 | duration, nr_readers, nr_writers); | |
619 | printf_verbose("Writer delay : %lu loops.\n", wdelay); | |
620 | printf_verbose("Reader duration : %lu loops.\n", rduration); | |
621 | printf_verbose("Mode:%s.\n", | |
622 | add_only ? " add only" : " add/delete"); | |
623 | printf_verbose("Init pool size offset %lu size %lu.\n", | |
624 | init_pool_offset, init_pool_size); | |
625 | printf_verbose("Lookup pool size offset %lu size %lu.\n", | |
626 | lookup_pool_offset, lookup_pool_size); | |
627 | printf_verbose("Update pool size offset %lu size %lu.\n", | |
628 | write_pool_offset, write_pool_size); | |
629 | printf_verbose("thread %-6s, thread id : %lx, tid %lu\n", | |
630 | "main", pthread_self(), (unsigned long)gettid()); | |
631 | ||
632 | tid_reader = malloc(sizeof(*tid_reader) * nr_readers); | |
633 | tid_writer = malloc(sizeof(*tid_writer) * nr_writers); | |
634 | count_reader = malloc(sizeof(*count_reader) * nr_readers); | |
635 | count_writer = malloc(sizeof(*count_writer) * nr_writers); | |
636 | ||
637 | err = create_all_cpu_call_rcu_data(0); | |
638 | if (err) { | |
639 | printf("Per-CPU call_rcu() worker threads unavailable. Using default global worker thread.\n"); | |
640 | } | |
641 | ||
582a6ade | 642 | rcu_register_thread(); |
44151f89 | 643 | |
582a6ade | 644 | printf("Test start.\n"); |
44151f89 | 645 | |
582a6ade MD |
646 | for (i = 0; i < 3; i++) { |
647 | ret = test_8bit_key(); | |
44151f89 | 648 | if (ret) { |
582a6ade | 649 | return ret; |
44151f89 | 650 | } |
582a6ade | 651 | rcu_quiescent_state(); |
44151f89 | 652 | } |
582a6ade MD |
653 | ret = test_16bit_key(); |
654 | if (ret) { | |
655 | return ret; | |
656 | } | |
657 | rcu_quiescent_state(); | |
a2a7ff59 | 658 | |
6fdf58e8 MD |
659 | /* key bits */ |
660 | for (i = 8; i <= 64; i *= 2) { | |
661 | /* nr of nodes per key */ | |
662 | for (j = 1; j < 4; j++) { | |
663 | ret = test_sparse_key(i, j); | |
664 | if (ret) { | |
665 | return ret; | |
666 | } | |
667 | rcu_quiescent_state(); | |
668 | } | |
44151f89 | 669 | } |
582a6ade | 670 | |
44151f89 | 671 | printf("Test end.\n"); |
582a6ade | 672 | rcu_unregister_thread(); |
44151f89 MD |
673 | return 0; |
674 | ||
675 | #if 0 | |
676 | /* | |
677 | * Hash Population needs to be seen as a RCU reader | |
678 | * thread from the point of view of resize. | |
679 | */ | |
680 | rcu_register_thread(); | |
681 | ret = (get_populate_hash_cb())(); | |
682 | assert(!ret); | |
683 | ||
684 | rcu_thread_offline(); | |
685 | ||
686 | next_aff = 0; | |
687 | ||
688 | for (i = 0; i < nr_readers; i++) { | |
689 | err = pthread_create(&tid_reader[i], | |
690 | NULL, get_thr_reader_cb(), | |
691 | &count_reader[i]); | |
692 | if (err != 0) | |
693 | exit(1); | |
694 | } | |
695 | for (i = 0; i < nr_writers; i++) { | |
696 | err = pthread_create(&tid_writer[i], | |
697 | NULL, get_thr_writer_cb(), | |
698 | &count_writer[i]); | |
699 | if (err != 0) | |
700 | exit(1); | |
701 | } | |
702 | ||
703 | cmm_smp_mb(); | |
704 | ||
705 | test_go = 1; | |
706 | ||
707 | remain = duration; | |
708 | do { | |
709 | remain = sleep(remain); | |
710 | } while (remain > 0); | |
711 | ||
712 | test_stop = 1; | |
713 | ||
714 | for (i = 0; i < nr_readers; i++) { | |
715 | err = pthread_join(tid_reader[i], &tret); | |
716 | if (err != 0) | |
717 | exit(1); | |
718 | tot_reads += count_reader[i]; | |
719 | } | |
720 | for (i = 0; i < nr_writers; i++) { | |
721 | err = pthread_join(tid_writer[i], &tret); | |
722 | if (err != 0) | |
723 | exit(1); | |
724 | tot_writes += count_writer[i].update_ops; | |
725 | tot_add += count_writer[i].add; | |
726 | tot_add_exist += count_writer[i].add_exist; | |
727 | tot_remove += count_writer[i].remove; | |
728 | } | |
729 | ||
730 | /* teardown counter thread */ | |
731 | act.sa_handler = SIG_IGN; | |
732 | act.sa_flags = SA_RESTART; | |
733 | ret = sigaction(SIGUSR2, &act, NULL); | |
734 | if (ret == -1) { | |
735 | perror("sigaction"); | |
736 | return -1; | |
737 | } | |
738 | { | |
739 | char msg[1] = { 0x42 }; | |
740 | ssize_t ret; | |
741 | ||
742 | do { | |
743 | ret = write(count_pipe[1], msg, 1); /* wakeup thread */ | |
744 | } while (ret == -1L && errno == EINTR); | |
745 | } | |
746 | ||
747 | fflush(stdout); | |
748 | rcu_thread_online(); | |
749 | rcu_read_lock(); | |
750 | printf("Counting nodes... "); | |
751 | cds_lfht_count_nodes(test_ht, &approx_before, &count, &approx_after); | |
752 | printf("done.\n"); | |
753 | test_delete_all_nodes(test_ht); | |
754 | rcu_read_unlock(); | |
755 | rcu_thread_offline(); | |
756 | if (count) { | |
757 | printf("Approximation before node accounting: %ld nodes.\n", | |
758 | approx_before); | |
759 | printf("Nodes deleted from hash table before destroy: " | |
760 | "%lu nodes.\n", | |
761 | count); | |
762 | printf("Approximation after node accounting: %ld nodes.\n", | |
763 | approx_after); | |
764 | } | |
765 | ret = cds_lfht_destroy(test_ht, NULL); | |
766 | if (ret) | |
767 | printf_verbose("final delete aborted\n"); | |
768 | else | |
769 | printf_verbose("final delete success\n"); | |
770 | printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads, | |
771 | tot_writes); | |
772 | printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu " | |
773 | "nr_writers %3u " | |
774 | "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu " | |
775 | "nr_add %12llu nr_add_fail %12llu nr_remove %12llu nr_leaked %12lld\n", | |
776 | argv[0], duration, nr_readers, rduration, | |
777 | nr_writers, wdelay, tot_reads, tot_writes, | |
778 | tot_reads + tot_writes, tot_add, tot_add_exist, tot_remove, | |
779 | (long long) tot_add + init_populate - tot_remove - count); | |
780 | rcu_unregister_thread(); | |
781 | free_all_cpu_call_rcu_data(); | |
782 | free(tid_reader); | |
783 | free(tid_writer); | |
784 | free(count_reader); | |
785 | free(count_writer); | |
786 | #endif | |
787 | return 0; | |
788 | } |