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; | |
1a0c0717 MD |
55 | int sanity_test; |
56 | unsigned int key_bits = 32; | |
44151f89 MD |
57 | |
58 | int count_pipe[2]; | |
59 | ||
60 | int verbose_mode; | |
61 | ||
62 | unsigned int cpu_affinities[NR_CPUS]; | |
63 | unsigned int next_aff = 0; | |
64 | int use_affinity = 0; | |
65 | ||
66 | pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER; | |
67 | ||
68 | DEFINE_URCU_TLS(unsigned long long, nr_writes); | |
69 | DEFINE_URCU_TLS(unsigned long long, nr_reads); | |
70 | ||
71 | unsigned int nr_readers; | |
72 | unsigned int nr_writers; | |
73 | ||
34ffee3e | 74 | static unsigned int add_ratio = 50; |
4df4328c | 75 | static uint64_t key_mul = 1ULL; |
34ffee3e | 76 | |
04c48ddf MD |
77 | static int add_unique, add_replace; |
78 | ||
44151f89 MD |
79 | static pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER; |
80 | ||
026545d3 MD |
81 | static int leak_detection; |
82 | static unsigned long test_nodes_allocated, test_nodes_freed; | |
83 | ||
44151f89 MD |
84 | void set_affinity(void) |
85 | { | |
86 | cpu_set_t mask; | |
87 | int cpu; | |
88 | int ret; | |
89 | ||
90 | if (!use_affinity) | |
91 | return; | |
92 | ||
93 | #if HAVE_SCHED_SETAFFINITY | |
94 | ret = pthread_mutex_lock(&affinity_mutex); | |
95 | if (ret) { | |
96 | perror("Error in pthread mutex lock"); | |
97 | exit(-1); | |
98 | } | |
99 | cpu = cpu_affinities[next_aff++]; | |
100 | ret = pthread_mutex_unlock(&affinity_mutex); | |
101 | if (ret) { | |
102 | perror("Error in pthread mutex unlock"); | |
103 | exit(-1); | |
104 | } | |
105 | CPU_ZERO(&mask); | |
106 | CPU_SET(cpu, &mask); | |
107 | #if SCHED_SETAFFINITY_ARGS == 2 | |
108 | sched_setaffinity(0, &mask); | |
109 | #else | |
110 | sched_setaffinity(0, sizeof(mask), &mask); | |
111 | #endif | |
112 | #endif /* HAVE_SCHED_SETAFFINITY */ | |
113 | } | |
114 | ||
115 | void rcu_copy_mutex_lock(void) | |
116 | { | |
117 | int ret; | |
118 | ret = pthread_mutex_lock(&rcu_copy_mutex); | |
119 | if (ret) { | |
120 | perror("Error in pthread mutex lock"); | |
121 | exit(-1); | |
122 | } | |
123 | } | |
124 | ||
125 | void rcu_copy_mutex_unlock(void) | |
126 | { | |
127 | int ret; | |
128 | ||
129 | ret = pthread_mutex_unlock(&rcu_copy_mutex); | |
130 | if (ret) { | |
131 | perror("Error in pthread mutex unlock"); | |
132 | exit(-1); | |
133 | } | |
134 | } | |
135 | ||
026545d3 MD |
136 | static |
137 | struct ja_test_node *node_alloc(void) | |
138 | { | |
139 | struct ja_test_node *node; | |
140 | ||
141 | node = calloc(sizeof(*node), 1); | |
142 | if (leak_detection && node) | |
143 | uatomic_inc(&test_nodes_allocated); | |
144 | return node; | |
145 | } | |
146 | ||
147 | static | |
9d5c85c8 | 148 | void free_node(struct ja_test_node *node) |
44151f89 | 149 | { |
a7542b7f | 150 | poison_free(node); |
026545d3 MD |
151 | if (leak_detection) |
152 | uatomic_inc(&test_nodes_freed); | |
44151f89 MD |
153 | } |
154 | ||
9d5c85c8 MD |
155 | static |
156 | void free_node_cb(struct rcu_head *head) | |
157 | { | |
158 | struct ja_test_node *node = | |
159 | caa_container_of(head, struct ja_test_node, node.head); | |
160 | free_node(node); | |
161 | } | |
162 | ||
3d8fe307 | 163 | #if 0 |
44151f89 MD |
164 | static |
165 | void test_delete_all_nodes(struct cds_lfht *ht) | |
166 | { | |
167 | struct cds_lfht_iter iter; | |
168 | struct lfht_test_node *node; | |
169 | unsigned long count = 0; | |
170 | ||
171 | cds_lfht_for_each_entry(ht, &iter, node, node) { | |
172 | int ret; | |
173 | ||
174 | ret = cds_lfht_del(test_ht, cds_lfht_iter_get_node(&iter)); | |
175 | assert(!ret); | |
176 | call_rcu(&node->head, free_node_cb); | |
177 | count++; | |
178 | } | |
179 | printf("deleted %lu nodes.\n", count); | |
180 | } | |
181 | #endif | |
182 | ||
183 | void show_usage(int argc, char **argv) | |
184 | { | |
185 | printf("Usage : %s nr_readers nr_writers duration (s)\n", argv[0]); | |
186 | #ifdef DEBUG_YIELD | |
187 | printf(" [-r] [-w] (yield reader and/or writer)\n"); | |
188 | #endif | |
189 | printf(" [-d delay] (writer period (us))\n"); | |
190 | printf(" [-c duration] (reader C.S. duration (in loops))\n"); | |
191 | printf(" [-v] (verbose output)\n"); | |
192 | printf(" [-a cpu#] [-a cpu#]... (affinity)\n"); | |
04c48ddf MD |
193 | printf(" [-u] Add unique keys.\n"); |
194 | printf(" [-s] Replace existing keys.\n"); | |
44151f89 | 195 | printf(" [not -u nor -s] Add entries (supports redundant keys).\n"); |
4df4328c | 196 | printf(" [-r ratio] Add ratio (in %% of add+removal).\n"); |
46f373a6 | 197 | printf(" [-k] Populate init nodes.\n"); |
44151f89 MD |
198 | printf(" [-R offset] Lookup pool offset.\n"); |
199 | printf(" [-S offset] Write pool offset.\n"); | |
200 | printf(" [-T offset] Init pool offset.\n"); | |
201 | printf(" [-M size] Lookup pool size.\n"); | |
202 | printf(" [-N size] Write pool size.\n"); | |
203 | printf(" [-O size] Init pool size.\n"); | |
204 | printf(" [-V] Validate lookups of init values (use with filled init pool, same lookup range, with different write range).\n"); | |
04c48ddf | 205 | printf(" [-t] Do sanity test.\n"); |
1a0c0717 | 206 | printf(" [-B] Key bits for multithread test (default: 32).\n"); |
4df4328c | 207 | printf(" [-m factor] Key multiplication factor.\n"); |
026545d3 | 208 | printf(" [-l] Memory leak detection.\n"); |
44151f89 MD |
209 | printf("\n\n"); |
210 | } | |
211 | ||
582a6ade MD |
212 | |
213 | static | |
214 | int test_8bit_key(void) | |
215 | { | |
216 | int ret; | |
217 | uint64_t key; | |
218 | ||
219 | /* Test with 8-bit key */ | |
220 | test_ja = cds_ja_new(8); | |
221 | if (!test_ja) { | |
222 | printf("Error allocating judy array.\n"); | |
223 | return -1; | |
224 | } | |
225 | ||
226 | /* Add keys */ | |
227 | printf("Test #1: add keys (8-bit).\n"); | |
228 | for (key = 0; key < 200; key++) { | |
026545d3 | 229 | struct ja_test_node *node = node_alloc(); |
582a6ade MD |
230 | |
231 | ja_test_node_init(node, key); | |
232 | rcu_read_lock(); | |
233 | ret = cds_ja_add(test_ja, key, &node->node); | |
234 | rcu_read_unlock(); | |
235 | if (ret) { | |
236 | fprintf(stderr, "Error (%d) adding node %" PRIu64 "\n", | |
237 | ret, key); | |
238 | assert(0); | |
239 | } | |
240 | } | |
241 | printf("OK\n"); | |
242 | ||
243 | printf("Test #2: successful key lookup (8-bit).\n"); | |
244 | for (key = 0; key < 200; key++) { | |
af3cbd45 | 245 | struct cds_hlist_head head; |
582a6ade MD |
246 | |
247 | rcu_read_lock(); | |
248 | head = cds_ja_lookup(test_ja, key); | |
af3cbd45 | 249 | if (cds_hlist_empty(&head)) { |
582a6ade MD |
250 | fprintf(stderr, "Error lookup node %" PRIu64 "\n", key); |
251 | assert(0); | |
252 | } | |
253 | rcu_read_unlock(); | |
254 | } | |
255 | printf("OK\n"); | |
256 | printf("Test #3: unsuccessful key lookup (8-bit).\n"); | |
257 | for (key = 200; key < 240; key++) { | |
af3cbd45 | 258 | struct cds_hlist_head head; |
582a6ade MD |
259 | |
260 | rcu_read_lock(); | |
261 | head = cds_ja_lookup(test_ja, key); | |
af3cbd45 | 262 | if (!cds_hlist_empty(&head)) { |
582a6ade MD |
263 | fprintf(stderr, |
264 | "Error unexpected lookup node %" PRIu64 "\n", | |
265 | key); | |
266 | assert(0); | |
267 | } | |
268 | rcu_read_unlock(); | |
269 | } | |
270 | printf("OK\n"); | |
3d8fe307 MD |
271 | printf("Test #4: remove keys (8-bit).\n"); |
272 | for (key = 0; key < 200; key++) { | |
273 | struct cds_hlist_head head; | |
274 | struct ja_test_node *node; | |
275 | ||
276 | rcu_read_lock(); | |
277 | head = cds_ja_lookup(test_ja, key); | |
278 | node = cds_hlist_first_entry_rcu(&head, struct ja_test_node, node.list); | |
279 | if (!node) { | |
280 | fprintf(stderr, "Error lookup node %" PRIu64 "\n", key); | |
281 | assert(0); | |
282 | } | |
283 | ret = cds_ja_del(test_ja, key, &node->node); | |
284 | if (ret) { | |
285 | fprintf(stderr, "Error (%d) removing node %" PRIu64 "\n", ret, key); | |
286 | assert(0); | |
287 | } | |
288 | call_rcu(&node->node.head, free_node_cb); | |
4d6ef45e MD |
289 | head = cds_ja_lookup(test_ja, key); |
290 | if (!cds_hlist_empty(&head)) { | |
291 | fprintf(stderr, "Error lookup %" PRIu64 ": %p (after delete) failed. Node is not expected.\n", key, head.next); | |
292 | assert(0); | |
293 | } | |
3d8fe307 MD |
294 | rcu_read_unlock(); |
295 | } | |
296 | printf("OK\n"); | |
582a6ade | 297 | |
3d8fe307 | 298 | ret = cds_ja_destroy(test_ja, free_node_cb); |
582a6ade MD |
299 | if (ret) { |
300 | fprintf(stderr, "Error destroying judy array\n"); | |
301 | return -1; | |
302 | } | |
303 | return 0; | |
304 | } | |
305 | ||
306 | static | |
307 | int test_16bit_key(void) | |
308 | { | |
309 | int ret; | |
310 | uint64_t key; | |
311 | ||
312 | /* Test with 16-bit key */ | |
313 | test_ja = cds_ja_new(16); | |
314 | if (!test_ja) { | |
315 | printf("Error allocating judy array.\n"); | |
316 | return -1; | |
317 | } | |
318 | ||
319 | /* Add keys */ | |
320 | printf("Test #1: add keys (16-bit).\n"); | |
4d6ef45e MD |
321 | for (key = 0; key < 10000; key++) { |
322 | //for (key = 0; key < 65536; key+=256) { | |
026545d3 | 323 | struct ja_test_node *node = node_alloc(); |
582a6ade MD |
324 | |
325 | ja_test_node_init(node, key); | |
326 | rcu_read_lock(); | |
327 | ret = cds_ja_add(test_ja, key, &node->node); | |
328 | rcu_read_unlock(); | |
329 | if (ret) { | |
330 | fprintf(stderr, "Error (%d) adding node %" PRIu64 "\n", | |
331 | ret, key); | |
332 | assert(0); | |
333 | } | |
334 | } | |
335 | printf("OK\n"); | |
336 | ||
337 | printf("Test #2: successful key lookup (16-bit).\n"); | |
4d6ef45e MD |
338 | for (key = 0; key < 10000; key++) { |
339 | //for (key = 0; key < 65536; key+=256) { | |
af3cbd45 | 340 | struct cds_hlist_head head; |
582a6ade MD |
341 | |
342 | rcu_read_lock(); | |
343 | head = cds_ja_lookup(test_ja, key); | |
af3cbd45 | 344 | if (cds_hlist_empty(&head)) { |
582a6ade MD |
345 | fprintf(stderr, "Error lookup node %" PRIu64 "\n", key); |
346 | assert(0); | |
347 | } | |
348 | rcu_read_unlock(); | |
349 | } | |
350 | printf("OK\n"); | |
351 | printf("Test #3: unsuccessful key lookup (16-bit).\n"); | |
352 | for (key = 11000; key <= 11002; key++) { | |
af3cbd45 | 353 | struct cds_hlist_head head; |
582a6ade MD |
354 | |
355 | rcu_read_lock(); | |
356 | head = cds_ja_lookup(test_ja, key); | |
af3cbd45 | 357 | if (!cds_hlist_empty(&head)) { |
582a6ade MD |
358 | fprintf(stderr, |
359 | "Error unexpected lookup node %" PRIu64 "\n", | |
360 | key); | |
361 | assert(0); | |
362 | } | |
363 | rcu_read_unlock(); | |
364 | } | |
365 | printf("OK\n"); | |
4d6ef45e MD |
366 | printf("Test #4: remove keys (16-bit).\n"); |
367 | for (key = 0; key < 10000; key++) { | |
368 | //for (key = 0; key < 65536; key+=256) { | |
369 | struct cds_hlist_head head; | |
370 | struct ja_test_node *node; | |
371 | ||
372 | rcu_read_lock(); | |
373 | head = cds_ja_lookup(test_ja, key); | |
374 | node = cds_hlist_first_entry_rcu(&head, struct ja_test_node, node.list); | |
375 | if (!node) { | |
376 | fprintf(stderr, "Error lookup node %" PRIu64 "\n", key); | |
377 | assert(0); | |
378 | } | |
379 | ret = cds_ja_del(test_ja, key, &node->node); | |
380 | if (ret) { | |
381 | fprintf(stderr, "Error (%d) removing node %" PRIu64 "\n", ret, key); | |
382 | assert(0); | |
383 | } | |
384 | call_rcu(&node->node.head, free_node_cb); | |
385 | head = cds_ja_lookup(test_ja, key); | |
386 | if (!cds_hlist_empty(&head)) { | |
387 | fprintf(stderr, "Error lookup %" PRIu64 ": %p (after delete) failed. Node is not expected.\n", key, head.next); | |
388 | assert(0); | |
389 | } | |
390 | rcu_read_unlock(); | |
391 | } | |
392 | printf("OK\n"); | |
582a6ade | 393 | |
3d8fe307 | 394 | ret = cds_ja_destroy(test_ja, free_node_cb); |
582a6ade MD |
395 | if (ret) { |
396 | fprintf(stderr, "Error destroying judy array\n"); | |
397 | return -1; | |
398 | } | |
399 | return 0; | |
400 | } | |
401 | ||
6fdf58e8 MD |
402 | /* |
403 | * nr_dup is number of nodes per key. | |
404 | */ | |
582a6ade | 405 | static |
6fdf58e8 | 406 | int test_sparse_key(unsigned int bits, int nr_dup) |
582a6ade | 407 | { |
582a6ade | 408 | uint64_t key, max_key; |
6fdf58e8 | 409 | int zerocount, i, ret; |
582a6ade MD |
410 | |
411 | if (bits == 64) | |
412 | max_key = UINT64_MAX; | |
413 | else | |
414 | max_key = (1ULL << bits) - 1; | |
415 | ||
416 | printf("Sparse key test begins for %u-bit keys\n", bits); | |
417 | /* Test with 16-bit key */ | |
418 | test_ja = cds_ja_new(bits); | |
419 | if (!test_ja) { | |
420 | printf("Error allocating judy array.\n"); | |
421 | return -1; | |
422 | } | |
423 | ||
424 | /* Add keys */ | |
425 | printf("Test #1: add keys (%u-bit).\n", bits); | |
6fdf58e8 MD |
426 | for (i = 0; i < nr_dup; i++) { |
427 | zerocount = 0; | |
428 | for (key = 0; key <= max_key && (key != 0 || zerocount < 1); key += 1ULL << (bits - 8)) { | |
026545d3 | 429 | struct ja_test_node *node = node_alloc(); |
582a6ade | 430 | |
6fdf58e8 MD |
431 | ja_test_node_init(node, key); |
432 | rcu_read_lock(); | |
433 | ret = cds_ja_add(test_ja, key, &node->node); | |
434 | rcu_read_unlock(); | |
435 | if (ret) { | |
436 | fprintf(stderr, "Error (%d) adding node %" PRIu64 "\n", | |
437 | ret, key); | |
438 | assert(0); | |
439 | } | |
440 | if (key == 0) | |
441 | zerocount++; | |
582a6ade | 442 | } |
582a6ade MD |
443 | } |
444 | printf("OK\n"); | |
445 | ||
446 | printf("Test #2: successful key lookup (%u-bit).\n", bits); | |
447 | zerocount = 0; | |
448 | for (key = 0; key <= max_key && (key != 0 || zerocount < 1); key += 1ULL << (bits - 8)) { | |
af3cbd45 | 449 | struct cds_hlist_head head; |
6fdf58e8 MD |
450 | struct ja_test_node *node; |
451 | struct cds_hlist_node *pos; | |
452 | int count = 0; | |
582a6ade MD |
453 | |
454 | rcu_read_lock(); | |
455 | head = cds_ja_lookup(test_ja, key); | |
af3cbd45 | 456 | if (cds_hlist_empty(&head)) { |
582a6ade MD |
457 | fprintf(stderr, "Error lookup node %" PRIu64 "\n", key); |
458 | assert(0); | |
459 | } | |
6fdf58e8 MD |
460 | cds_hlist_for_each_entry_rcu(node, pos, &head, node.list) { |
461 | count++; | |
462 | } | |
463 | if (count != nr_dup) { | |
464 | fprintf(stderr, "Unexpected number of match for key %" PRIu64 ", expected %d, got %d.\n", key, nr_dup, count); | |
465 | } | |
582a6ade MD |
466 | rcu_read_unlock(); |
467 | if (key == 0) | |
468 | zerocount++; | |
469 | } | |
470 | printf("OK\n"); | |
471 | if (bits > 8) { | |
472 | printf("Test #3: unsuccessful key lookup (%u-bit).\n", bits); | |
473 | zerocount = 0; | |
474 | for (key = 0; key <= max_key && (key != 0 || zerocount < 1); key += 1ULL << (bits - 8)) { | |
af3cbd45 | 475 | struct cds_hlist_head head; |
582a6ade MD |
476 | |
477 | rcu_read_lock(); | |
478 | head = cds_ja_lookup(test_ja, key + 42); | |
af3cbd45 | 479 | if (!cds_hlist_empty(&head)) { |
582a6ade MD |
480 | fprintf(stderr, |
481 | "Error unexpected lookup node %" PRIu64 "\n", | |
482 | key + 42); | |
483 | assert(0); | |
484 | } | |
485 | rcu_read_unlock(); | |
486 | if (key == 0) | |
487 | zerocount++; | |
488 | } | |
489 | printf("OK\n"); | |
490 | } | |
81c18eb9 | 491 | printf("Test #4: remove keys (%u-bit).\n", bits); |
4d6ef45e MD |
492 | zerocount = 0; |
493 | for (key = 0; key <= max_key && (key != 0 || zerocount < 1); key += 1ULL << (bits - 8)) { | |
494 | struct cds_hlist_head head; | |
495 | struct ja_test_node *node; | |
6fdf58e8 MD |
496 | struct cds_hlist_node *pos; |
497 | int count = 0; | |
4d6ef45e MD |
498 | |
499 | rcu_read_lock(); | |
500 | head = cds_ja_lookup(test_ja, key); | |
6fdf58e8 MD |
501 | |
502 | ||
503 | cds_hlist_for_each_entry_rcu(node, pos, &head, node.list) { | |
504 | struct cds_hlist_head testhead; | |
505 | ||
506 | count++; | |
507 | if (!node) { | |
508 | fprintf(stderr, "Error lookup node %" PRIu64 "\n", key); | |
509 | assert(0); | |
510 | } | |
511 | ret = cds_ja_del(test_ja, key, &node->node); | |
512 | if (ret) { | |
513 | fprintf(stderr, "Error (%d) removing node %" PRIu64 "\n", ret, key); | |
514 | assert(0); | |
515 | } | |
516 | call_rcu(&node->node.head, free_node_cb); | |
517 | testhead = cds_ja_lookup(test_ja, key); | |
518 | if (count < nr_dup && cds_hlist_empty(&testhead)) { | |
519 | fprintf(stderr, "Error: no node found after deletion of some nodes of a key\n"); | |
520 | assert(0); | |
521 | } | |
4d6ef45e | 522 | } |
4d6ef45e MD |
523 | head = cds_ja_lookup(test_ja, key); |
524 | if (!cds_hlist_empty(&head)) { | |
525 | fprintf(stderr, "Error lookup %" PRIu64 ": %p (after delete) failed. Node is not expected.\n", key, head.next); | |
526 | assert(0); | |
527 | } | |
528 | rcu_read_unlock(); | |
529 | if (key == 0) | |
530 | zerocount++; | |
531 | } | |
532 | printf("OK\n"); | |
582a6ade | 533 | |
3d8fe307 | 534 | ret = cds_ja_destroy(test_ja, free_node_cb); |
582a6ade MD |
535 | if (ret) { |
536 | fprintf(stderr, "Error destroying judy array\n"); | |
537 | return -1; | |
538 | } | |
539 | printf("Test ends\n"); | |
540 | ||
541 | return 0; | |
542 | } | |
543 | ||
1a0c0717 MD |
544 | static |
545 | int do_sanity_test(void) | |
546 | { | |
547 | int i, j, ret; | |
582a6ade | 548 | |
1a0c0717 MD |
549 | printf("Sanity test start.\n"); |
550 | ||
551 | for (i = 0; i < 3; i++) { | |
552 | ret = test_8bit_key(); | |
553 | if (ret) { | |
554 | return ret; | |
555 | } | |
556 | rcu_quiescent_state(); | |
557 | } | |
558 | ret = test_16bit_key(); | |
559 | if (ret) { | |
560 | return ret; | |
561 | } | |
562 | rcu_quiescent_state(); | |
563 | ||
564 | /* key bits */ | |
565 | for (i = 8; i <= 64; i *= 2) { | |
566 | /* nr of nodes per key */ | |
567 | for (j = 1; j < 4; j++) { | |
568 | ret = test_sparse_key(i, j); | |
569 | if (ret) { | |
570 | return ret; | |
571 | } | |
572 | rcu_quiescent_state(); | |
573 | } | |
574 | } | |
575 | printf("Sanity test end.\n"); | |
576 | ||
577 | return 0; | |
578 | } | |
579 | ||
580 | enum urcu_ja_addremove { | |
581 | AR_RANDOM = 0, | |
582 | AR_ADD = 1, | |
583 | AR_REMOVE = -1, | |
584 | }; /* 1: add, -1 remove, 0: random */ | |
585 | ||
586 | static enum urcu_ja_addremove addremove; /* 1: add, -1 remove, 0: random */ | |
587 | ||
588 | static | |
589 | void test_ja_rw_sigusr1_handler(int signo) | |
590 | { | |
591 | switch (addremove) { | |
592 | case AR_ADD: | |
593 | printf("Add/Remove: random.\n"); | |
594 | addremove = AR_RANDOM; | |
595 | break; | |
596 | case AR_RANDOM: | |
597 | printf("Add/Remove: remove only.\n"); | |
598 | addremove = AR_REMOVE; | |
599 | break; | |
600 | case AR_REMOVE: | |
601 | printf("Add/Remove: add only.\n"); | |
602 | addremove = AR_ADD; | |
603 | break; | |
604 | } | |
605 | } | |
606 | ||
607 | static | |
608 | void *test_ja_rw_thr_reader(void *_count) | |
609 | { | |
610 | unsigned long long *count = _count; | |
611 | struct cds_hlist_head head; | |
612 | uint64_t key; | |
613 | ||
614 | printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n", | |
615 | "reader", pthread_self(), (unsigned long) gettid()); | |
616 | ||
617 | set_affinity(); | |
618 | ||
619 | rcu_register_thread(); | |
620 | ||
621 | while (!test_go) | |
622 | { | |
623 | } | |
624 | cmm_smp_mb(); | |
625 | ||
626 | for (;;) { | |
627 | rcu_read_lock(); | |
628 | ||
629 | /* note: only looking up ulong keys */ | |
630 | key = ((unsigned long) rand_r(&URCU_TLS(rand_lookup)) % lookup_pool_size) + lookup_pool_offset; | |
4df4328c | 631 | key *= key_mul; |
1a0c0717 MD |
632 | head = cds_ja_lookup(test_ja, key); |
633 | if (cds_hlist_empty(&head)) { | |
634 | if (validate_lookup) { | |
635 | printf("[ERROR] Lookup cannot find initial node.\n"); | |
636 | exit(-1); | |
637 | } | |
638 | URCU_TLS(lookup_fail)++; | |
639 | } else { | |
640 | URCU_TLS(lookup_ok)++; | |
641 | } | |
756c692d | 642 | rcu_debug_yield_read(); |
1a0c0717 MD |
643 | if (caa_unlikely(rduration)) |
644 | loop_sleep(rduration); | |
645 | rcu_read_unlock(); | |
646 | URCU_TLS(nr_reads)++; | |
647 | if (caa_unlikely(!test_duration_read())) | |
648 | break; | |
649 | if (caa_unlikely((URCU_TLS(nr_reads) & ((1 << 10) - 1)) == 0)) | |
650 | rcu_quiescent_state(); | |
651 | } | |
652 | ||
653 | rcu_unregister_thread(); | |
654 | ||
655 | *count = URCU_TLS(nr_reads); | |
656 | printf_verbose("thread_end %s, thread id : %lx, tid %lu\n", | |
657 | "reader", pthread_self(), (unsigned long) gettid()); | |
658 | printf_verbose("readid : %lx, lookupfail %lu, lookupok %lu\n", | |
659 | pthread_self(), URCU_TLS(lookup_fail), | |
660 | URCU_TLS(lookup_ok)); | |
661 | return ((void*)1); | |
662 | } | |
663 | ||
34ffee3e MD |
664 | static |
665 | int is_add(void) | |
666 | { | |
667 | return ((unsigned int) rand_r(&URCU_TLS(rand_lookup)) % 100) < add_ratio; | |
668 | } | |
669 | ||
1a0c0717 MD |
670 | static |
671 | void *test_ja_rw_thr_writer(void *_count) | |
672 | { | |
673 | struct wr_count *count = _count; | |
674 | struct cds_hlist_head head; | |
675 | uint64_t key; | |
676 | int ret; | |
677 | ||
678 | printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n", | |
679 | "writer", pthread_self(), (unsigned long) gettid()); | |
680 | ||
681 | set_affinity(); | |
682 | ||
683 | rcu_register_thread(); | |
684 | ||
685 | while (!test_go) | |
686 | { | |
687 | } | |
688 | cmm_smp_mb(); | |
689 | ||
690 | for (;;) { | |
34ffee3e MD |
691 | if ((addremove == AR_ADD) |
692 | || (addremove == AR_RANDOM && is_add())) { | |
026545d3 | 693 | struct ja_test_node *node = node_alloc(); |
75d573aa | 694 | struct cds_ja_node *ret_node; |
1a0c0717 MD |
695 | |
696 | /* note: only inserting ulong keys */ | |
697 | key = ((unsigned long) rand_r(&URCU_TLS(rand_lookup)) % write_pool_size) + write_pool_offset; | |
4df4328c | 698 | key *= key_mul; |
1a0c0717 MD |
699 | ja_test_node_init(node, key); |
700 | rcu_read_lock(); | |
04c48ddf MD |
701 | if (add_unique) { |
702 | ret_node = cds_ja_add_unique(test_ja, key, &node->node); | |
703 | if (ret_node != &node->node) { | |
9d5c85c8 | 704 | free_node(node); |
04c48ddf MD |
705 | URCU_TLS(nr_addexist)++; |
706 | } else { | |
707 | URCU_TLS(nr_add)++; | |
708 | } | |
709 | } else if (add_replace) { | |
710 | assert(0); /* not implemented yet. */ | |
75d573aa | 711 | } else { |
04c48ddf MD |
712 | ret = cds_ja_add(test_ja, key, &node->node); |
713 | if (ret) { | |
714 | fprintf(stderr, "Error in cds_ja_add: %d\n", ret); | |
9d5c85c8 | 715 | free_node(node); |
04c48ddf MD |
716 | } else { |
717 | URCU_TLS(nr_add)++; | |
718 | } | |
1a0c0717 | 719 | } |
04c48ddf | 720 | rcu_read_unlock(); |
1a0c0717 MD |
721 | } else { |
722 | struct ja_test_node *node; | |
723 | ||
724 | /* May delete */ | |
725 | /* note: only deleting ulong keys */ | |
726 | key = ((unsigned long) rand_r(&URCU_TLS(rand_lookup)) % write_pool_size) + write_pool_offset; | |
4df4328c | 727 | key *= key_mul; |
1a0c0717 MD |
728 | |
729 | rcu_read_lock(); | |
730 | ||
731 | head = cds_ja_lookup(test_ja, key); | |
732 | node = cds_hlist_first_entry_rcu(&head, struct ja_test_node, node.list); | |
733 | if (node) { | |
734 | ret = cds_ja_del(test_ja, key, &node->node); | |
d7069878 | 735 | if (!ret) { |
1a0c0717 | 736 | call_rcu(&node->node.head, free_node_cb); |
1a0c0717 | 737 | URCU_TLS(nr_del)++; |
d7069878 MD |
738 | } else { |
739 | URCU_TLS(nr_delnoent)++; | |
1a0c0717 MD |
740 | } |
741 | } else { | |
742 | URCU_TLS(nr_delnoent)++; | |
743 | } | |
6da38aec | 744 | rcu_read_unlock(); |
1a0c0717 MD |
745 | } |
746 | ||
747 | URCU_TLS(nr_writes)++; | |
748 | if (caa_unlikely(!test_duration_write())) | |
749 | break; | |
750 | if (caa_unlikely(wdelay)) | |
751 | loop_sleep(wdelay); | |
752 | if (caa_unlikely((URCU_TLS(nr_writes) & ((1 << 10) - 1)) == 0)) | |
753 | rcu_quiescent_state(); | |
754 | } | |
755 | ||
756 | rcu_unregister_thread(); | |
757 | ||
758 | printf_verbose("thread_end %s, thread id : %lx, tid %lu\n", | |
759 | "writer", pthread_self(), (unsigned long) gettid()); | |
760 | printf_verbose("info id %lx: nr_add %lu, nr_addexist %lu, nr_del %lu, " | |
761 | "nr_delnoent %lu\n", pthread_self(), URCU_TLS(nr_add), | |
762 | URCU_TLS(nr_addexist), URCU_TLS(nr_del), | |
763 | URCU_TLS(nr_delnoent)); | |
764 | count->update_ops = URCU_TLS(nr_writes); | |
765 | count->add = URCU_TLS(nr_add); | |
766 | count->add_exist = URCU_TLS(nr_addexist); | |
767 | count->remove = URCU_TLS(nr_del); | |
768 | return ((void*)2); | |
769 | } | |
770 | ||
771 | static | |
772 | int do_mt_populate_ja(void) | |
773 | { | |
774 | struct cds_hlist_head head; | |
4df4328c | 775 | uint64_t iter; |
1a0c0717 MD |
776 | int ret; |
777 | ||
778 | if (!init_populate) | |
779 | return 0; | |
780 | ||
781 | printf("Starting rw test\n"); | |
782 | ||
4df4328c | 783 | for (iter = init_pool_offset; iter < init_pool_offset + init_pool_size; iter++) { |
026545d3 | 784 | struct ja_test_node *node = node_alloc(); |
4df4328c | 785 | uint64_t key; |
1a0c0717 MD |
786 | |
787 | /* note: only inserting ulong keys */ | |
4df4328c MD |
788 | key = (unsigned long) iter; |
789 | key *= key_mul; | |
1a0c0717 MD |
790 | ja_test_node_init(node, key); |
791 | rcu_read_lock(); | |
792 | ret = cds_ja_add(test_ja, key, &node->node); | |
793 | URCU_TLS(nr_add)++; | |
794 | URCU_TLS(nr_writes)++; | |
795 | rcu_read_unlock(); | |
796 | if (ret) { | |
797 | fprintf(stderr, "Error (%d) adding node %" PRIu64 "\n", | |
798 | ret, key); | |
799 | assert(0); | |
800 | } | |
801 | } | |
802 | return 0; | |
803 | } | |
804 | ||
805 | static | |
806 | int do_mt_test(void) | |
44151f89 | 807 | { |
44151f89 MD |
808 | pthread_t *tid_reader, *tid_writer; |
809 | void *tret; | |
1a0c0717 | 810 | int ret, i, err; |
44151f89 MD |
811 | unsigned long long *count_reader; |
812 | struct wr_count *count_writer; | |
813 | unsigned long long tot_reads = 0, tot_writes = 0, | |
814 | tot_add = 0, tot_add_exist = 0, tot_remove = 0; | |
44151f89 | 815 | unsigned int remain; |
1a0c0717 MD |
816 | |
817 | tid_reader = malloc(sizeof(*tid_reader) * nr_readers); | |
818 | tid_writer = malloc(sizeof(*tid_writer) * nr_writers); | |
819 | count_reader = malloc(sizeof(*count_reader) * nr_readers); | |
820 | count_writer = malloc(sizeof(*count_writer) * nr_writers); | |
821 | ||
822 | printf("Allocating Judy Array for %u-bit keys\n", key_bits); | |
823 | test_ja = cds_ja_new(key_bits); | |
824 | if (!test_ja) { | |
825 | printf("Error allocating judy array.\n"); | |
826 | ret = -1; | |
827 | goto end; | |
828 | } | |
829 | ||
830 | do_mt_populate_ja(); | |
831 | ||
832 | next_aff = 0; | |
833 | ||
834 | for (i = 0; i < nr_readers; i++) { | |
835 | err = pthread_create(&tid_reader[i], | |
836 | NULL, test_ja_rw_thr_reader, | |
837 | &count_reader[i]); | |
838 | if (err != 0) | |
839 | exit(1); | |
840 | } | |
841 | for (i = 0; i < nr_writers; i++) { | |
842 | err = pthread_create(&tid_writer[i], | |
843 | NULL, test_ja_rw_thr_writer, | |
844 | &count_writer[i]); | |
845 | if (err != 0) | |
846 | exit(1); | |
847 | } | |
848 | ||
849 | cmm_smp_mb(); | |
850 | ||
851 | test_go = 1; | |
852 | ||
853 | rcu_thread_offline_qsbr(); | |
854 | ||
855 | remain = duration; | |
856 | do { | |
857 | remain = sleep(remain); | |
858 | } while (remain > 0); | |
859 | ||
860 | test_stop = 1; | |
861 | ||
862 | for (i = 0; i < nr_readers; i++) { | |
863 | err = pthread_join(tid_reader[i], &tret); | |
864 | if (err != 0) | |
865 | exit(1); | |
866 | tot_reads += count_reader[i]; | |
867 | } | |
868 | for (i = 0; i < nr_writers; i++) { | |
869 | err = pthread_join(tid_writer[i], &tret); | |
870 | if (err != 0) | |
871 | exit(1); | |
872 | tot_writes += count_writer[i].update_ops; | |
873 | tot_add += count_writer[i].add; | |
874 | tot_add_exist += count_writer[i].add_exist; | |
875 | tot_remove += count_writer[i].remove; | |
876 | } | |
88e115c3 | 877 | rcu_thread_online_qsbr(); |
1a0c0717 MD |
878 | |
879 | ret = cds_ja_destroy(test_ja, free_node_cb); | |
880 | if (ret) { | |
881 | fprintf(stderr, "Error destroying judy array\n"); | |
882 | goto end; | |
883 | } | |
1a0c0717 MD |
884 | |
885 | free(tid_reader); | |
886 | free(tid_writer); | |
887 | free(count_reader); | |
888 | free(count_writer); | |
889 | ret = 0; | |
890 | end: | |
891 | return ret; | |
892 | } | |
893 | ||
026545d3 MD |
894 | static |
895 | int check_memory_leaks(void) | |
896 | { | |
897 | unsigned long na, nf; | |
898 | ||
899 | na = uatomic_read(&test_nodes_allocated); | |
900 | nf = uatomic_read(&test_nodes_freed); | |
901 | if (na != nf) { | |
902 | fprintf(stderr, "Memory leak of %ld test nodes detected. Allocated: %lu, freed: %lu\n", | |
903 | na - nf, na, nf); | |
904 | return -1; | |
905 | } | |
906 | return 0; | |
907 | } | |
908 | ||
1a0c0717 MD |
909 | int main(int argc, char **argv) |
910 | { | |
911 | int i, j, a, ret, err; | |
a2a7ff59 | 912 | uint64_t key; |
1a0c0717 | 913 | struct sigaction act; |
44151f89 MD |
914 | |
915 | if (argc < 4) { | |
916 | show_usage(argc, argv); | |
917 | return -1; | |
918 | } | |
919 | ||
920 | err = sscanf(argv[1], "%u", &nr_readers); | |
921 | if (err != 1) { | |
922 | show_usage(argc, argv); | |
923 | return -1; | |
924 | } | |
925 | ||
926 | err = sscanf(argv[2], "%u", &nr_writers); | |
927 | if (err != 1) { | |
928 | show_usage(argc, argv); | |
929 | return -1; | |
930 | } | |
931 | ||
932 | err = sscanf(argv[3], "%lu", &duration); | |
933 | if (err != 1) { | |
934 | show_usage(argc, argv); | |
935 | return -1; | |
936 | } | |
937 | ||
938 | for (i = 4; i < argc; i++) { | |
939 | if (argv[i][0] != '-') | |
940 | continue; | |
941 | switch (argv[i][1]) { | |
942 | #ifdef DEBUG_YIELD | |
943 | case 'r': | |
944 | yield_active |= YIELD_READ; | |
945 | break; | |
946 | case 'w': | |
947 | yield_active |= YIELD_WRITE; | |
948 | break; | |
949 | #endif | |
950 | case 'a': | |
951 | if (argc < i + 2) { | |
952 | show_usage(argc, argv); | |
953 | return -1; | |
954 | } | |
955 | a = atoi(argv[++i]); | |
956 | cpu_affinities[next_aff++] = a; | |
957 | use_affinity = 1; | |
958 | printf_verbose("Adding CPU %d affinity\n", a); | |
959 | break; | |
960 | case 'c': | |
961 | if (argc < i + 2) { | |
962 | show_usage(argc, argv); | |
963 | return -1; | |
964 | } | |
965 | rduration = atol(argv[++i]); | |
966 | break; | |
967 | case 'd': | |
968 | if (argc < i + 2) { | |
969 | show_usage(argc, argv); | |
970 | return -1; | |
971 | } | |
972 | wdelay = atol(argv[++i]); | |
973 | break; | |
974 | case 'v': | |
975 | verbose_mode = 1; | |
976 | break; | |
34ffee3e MD |
977 | case 'r': |
978 | add_ratio = atoi(argv[++i]); | |
44151f89 MD |
979 | break; |
980 | case 'k': | |
46f373a6 | 981 | init_populate = 1; |
44151f89 MD |
982 | break; |
983 | case 'R': | |
984 | lookup_pool_offset = atol(argv[++i]); | |
985 | break; | |
986 | case 'S': | |
987 | write_pool_offset = atol(argv[++i]); | |
988 | break; | |
989 | case 'T': | |
990 | init_pool_offset = atol(argv[++i]); | |
991 | break; | |
992 | case 'M': | |
993 | lookup_pool_size = atol(argv[++i]); | |
994 | break; | |
995 | case 'N': | |
996 | write_pool_size = atol(argv[++i]); | |
997 | break; | |
998 | case 'O': | |
999 | init_pool_size = atol(argv[++i]); | |
1000 | break; | |
1001 | case 'V': | |
1002 | validate_lookup = 1; | |
1003 | break; | |
04c48ddf | 1004 | case 't': |
1a0c0717 MD |
1005 | sanity_test = 1; |
1006 | break; | |
1007 | case 'B': | |
1008 | key_bits = atol(argv[++i]); | |
1009 | break; | |
4df4328c MD |
1010 | case 'm': |
1011 | key_mul = atoll(argv[++i]); | |
1012 | break; | |
04c48ddf MD |
1013 | case 'u': |
1014 | add_unique = 1; | |
1015 | break; | |
1016 | case 's': | |
1017 | add_replace = 1; | |
1018 | break; | |
026545d3 MD |
1019 | case 'l': |
1020 | leak_detection = 1; | |
1021 | break; | |
44151f89 MD |
1022 | } |
1023 | } | |
1024 | ||
1025 | printf_verbose("running test for %lu seconds, %u readers, %u writers.\n", | |
1026 | duration, nr_readers, nr_writers); | |
1027 | printf_verbose("Writer delay : %lu loops.\n", wdelay); | |
1028 | printf_verbose("Reader duration : %lu loops.\n", rduration); | |
34ffee3e | 1029 | printf_verbose("Add ratio: %u%%.\n", add_ratio); |
04c48ddf MD |
1030 | printf_verbose("Mode:%s%s.\n", |
1031 | " add/remove", | |
1032 | add_unique ? " uniquify" : ( add_replace ? " replace" : " insert")); | |
b31cb285 | 1033 | printf_verbose("Key multiplication factor: %" PRIu64 ".\n", key_mul); |
44151f89 MD |
1034 | printf_verbose("Init pool size offset %lu size %lu.\n", |
1035 | init_pool_offset, init_pool_size); | |
1036 | printf_verbose("Lookup pool size offset %lu size %lu.\n", | |
1037 | lookup_pool_offset, lookup_pool_size); | |
1038 | printf_verbose("Update pool size offset %lu size %lu.\n", | |
1039 | write_pool_offset, write_pool_size); | |
46fc422b MD |
1040 | if (validate_lookup) |
1041 | printf_verbose("Validating lookups.\n"); | |
026545d3 MD |
1042 | if (leak_detection) |
1043 | printf_verbose("Memory leak dection activated.\n"); | |
44151f89 MD |
1044 | printf_verbose("thread %-6s, thread id : %lx, tid %lu\n", |
1045 | "main", pthread_self(), (unsigned long)gettid()); | |
1046 | ||
1a0c0717 MD |
1047 | memset(&act, 0, sizeof(act)); |
1048 | ret = sigemptyset(&act.sa_mask); | |
1049 | if (ret == -1) { | |
1050 | perror("sigemptyset"); | |
1051 | return -1; | |
1052 | } | |
1053 | act.sa_handler = test_ja_rw_sigusr1_handler; | |
1054 | act.sa_flags = SA_RESTART; | |
1055 | ret = sigaction(SIGUSR1, &act, NULL); | |
1056 | if (ret == -1) { | |
1057 | perror("sigaction"); | |
1058 | return -1; | |
1059 | } | |
44151f89 MD |
1060 | |
1061 | err = create_all_cpu_call_rcu_data(0); | |
1062 | if (err) { | |
1063 | printf("Per-CPU call_rcu() worker threads unavailable. Using default global worker thread.\n"); | |
1064 | } | |
1065 | ||
582a6ade | 1066 | rcu_register_thread(); |
44151f89 | 1067 | |
1a0c0717 MD |
1068 | if (sanity_test) { |
1069 | ret = do_sanity_test(); | |
1a0c0717 | 1070 | } else { |
4bea9b8d | 1071 | ret = do_mt_test(); |
44151f89 | 1072 | } |
a2a7ff59 | 1073 | |
026545d3 MD |
1074 | /* Wait for in-flight call_rcu free to complete for leak detection */ |
1075 | rcu_barrier(); | |
1076 | ||
1077 | ret |= check_memory_leaks(); | |
1078 | ||
582a6ade | 1079 | rcu_unregister_thread(); |
1a0c0717 | 1080 | free_all_cpu_call_rcu_data(); |
4bea9b8d MD |
1081 | |
1082 | if (ret) { | |
1083 | printf("Test ended with error: %d\n", ret); | |
1084 | } | |
1085 | return ret; | |
44151f89 | 1086 | } |