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 | |
5b62a42a | 148 | void free_test_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 | 155 | static |
5b62a42a | 156 | void free_test_node_cb(struct rcu_head *head) |
9d5c85c8 MD |
157 | { |
158 | struct ja_test_node *node = | |
21ac4c56 | 159 | caa_container_of(head, struct ja_test_node, head); |
5b62a42a | 160 | free_test_node(node); |
9d5c85c8 MD |
161 | } |
162 | ||
21ac4c56 MD |
163 | static |
164 | void rcu_free_test_node(struct ja_test_node *test_node) | |
165 | { | |
5b62a42a | 166 | call_rcu(&test_node->head, free_test_node_cb); |
21ac4c56 MD |
167 | } |
168 | ||
169 | static | |
5b62a42a | 170 | void free_node(struct cds_ja_node *node) |
21ac4c56 MD |
171 | { |
172 | struct ja_test_node *test_node = to_test_node(node); | |
173 | ||
5b62a42a | 174 | free_test_node(test_node); |
21ac4c56 MD |
175 | } |
176 | ||
3d8fe307 | 177 | #if 0 |
44151f89 MD |
178 | static |
179 | void test_delete_all_nodes(struct cds_lfht *ht) | |
180 | { | |
181 | struct cds_lfht_iter iter; | |
182 | struct lfht_test_node *node; | |
183 | unsigned long count = 0; | |
184 | ||
185 | cds_lfht_for_each_entry(ht, &iter, node, node) { | |
186 | int ret; | |
187 | ||
188 | ret = cds_lfht_del(test_ht, cds_lfht_iter_get_node(&iter)); | |
189 | assert(!ret); | |
190 | call_rcu(&node->head, free_node_cb); | |
191 | count++; | |
192 | } | |
193 | printf("deleted %lu nodes.\n", count); | |
194 | } | |
195 | #endif | |
196 | ||
197 | void show_usage(int argc, char **argv) | |
198 | { | |
199 | printf("Usage : %s nr_readers nr_writers duration (s)\n", argv[0]); | |
200 | #ifdef DEBUG_YIELD | |
201 | printf(" [-r] [-w] (yield reader and/or writer)\n"); | |
202 | #endif | |
203 | printf(" [-d delay] (writer period (us))\n"); | |
204 | printf(" [-c duration] (reader C.S. duration (in loops))\n"); | |
205 | printf(" [-v] (verbose output)\n"); | |
206 | printf(" [-a cpu#] [-a cpu#]... (affinity)\n"); | |
04c48ddf MD |
207 | printf(" [-u] Add unique keys.\n"); |
208 | printf(" [-s] Replace existing keys.\n"); | |
44151f89 | 209 | printf(" [not -u nor -s] Add entries (supports redundant keys).\n"); |
4df4328c | 210 | printf(" [-r ratio] Add ratio (in %% of add+removal).\n"); |
46f373a6 | 211 | printf(" [-k] Populate init nodes.\n"); |
44151f89 MD |
212 | printf(" [-R offset] Lookup pool offset.\n"); |
213 | printf(" [-S offset] Write pool offset.\n"); | |
214 | printf(" [-T offset] Init pool offset.\n"); | |
215 | printf(" [-M size] Lookup pool size.\n"); | |
216 | printf(" [-N size] Write pool size.\n"); | |
217 | printf(" [-O size] Init pool size.\n"); | |
218 | printf(" [-V] Validate lookups of init values (use with filled init pool, same lookup range, with different write range).\n"); | |
04c48ddf | 219 | printf(" [-t] Do sanity test.\n"); |
1a0c0717 | 220 | printf(" [-B] Key bits for multithread test (default: 32).\n"); |
4df4328c | 221 | printf(" [-m factor] Key multiplication factor.\n"); |
026545d3 | 222 | printf(" [-l] Memory leak detection.\n"); |
44151f89 MD |
223 | printf("\n\n"); |
224 | } | |
225 | ||
c01cfe73 MD |
226 | static |
227 | int test_free_all_nodes(struct cds_ja *ja) | |
228 | { | |
229 | uint64_t key; | |
230 | struct cds_ja_node *ja_node; | |
231 | int ret = 0; | |
232 | ||
233 | rcu_read_lock(); | |
234 | cds_ja_for_each_key_rcu(test_ja, key, ja_node) { | |
235 | struct cds_ja_node *tmp_node; | |
236 | ||
237 | cds_ja_for_each_duplicate_safe_rcu(ja_node, tmp_node) { | |
238 | ret = cds_ja_del(test_ja, key, ja_node); | |
239 | if (ret) { | |
240 | fprintf(stderr, "Error (%d) removing node %" PRIu64 "\n", ret, key); | |
241 | goto end; | |
242 | } | |
243 | /* Alone using Judy array, OK to free now */ | |
244 | free_node(ja_node); | |
245 | } | |
246 | } | |
247 | end: | |
248 | rcu_read_unlock(); | |
249 | return ret; | |
250 | } | |
251 | ||
582a6ade MD |
252 | static |
253 | int test_8bit_key(void) | |
254 | { | |
c6cd5af3 | 255 | int ret, i; |
582a6ade | 256 | uint64_t key; |
5277c70e | 257 | uint64_t ka[] = { 5, 17, 100, 222 }; |
c6cd5af3 | 258 | uint64_t ka_test_offset = 5; |
c01cfe73 | 259 | struct cds_ja_node *ja_node; |
582a6ade MD |
260 | |
261 | /* Test with 8-bit key */ | |
262 | test_ja = cds_ja_new(8); | |
263 | if (!test_ja) { | |
264 | printf("Error allocating judy array.\n"); | |
265 | return -1; | |
266 | } | |
267 | ||
268 | /* Add keys */ | |
269 | printf("Test #1: add keys (8-bit).\n"); | |
270 | for (key = 0; key < 200; key++) { | |
026545d3 | 271 | struct ja_test_node *node = node_alloc(); |
582a6ade MD |
272 | |
273 | ja_test_node_init(node, key); | |
274 | rcu_read_lock(); | |
275 | ret = cds_ja_add(test_ja, key, &node->node); | |
276 | rcu_read_unlock(); | |
277 | if (ret) { | |
278 | fprintf(stderr, "Error (%d) adding node %" PRIu64 "\n", | |
279 | ret, key); | |
280 | assert(0); | |
281 | } | |
282 | } | |
283 | printf("OK\n"); | |
284 | ||
285 | printf("Test #2: successful key lookup (8-bit).\n"); | |
286 | for (key = 0; key < 200; key++) { | |
582a6ade | 287 | rcu_read_lock(); |
c01cfe73 MD |
288 | ja_node = cds_ja_lookup(test_ja, key); |
289 | if (!ja_node) { | |
582a6ade MD |
290 | fprintf(stderr, "Error lookup node %" PRIu64 "\n", key); |
291 | assert(0); | |
292 | } | |
293 | rcu_read_unlock(); | |
294 | } | |
295 | printf("OK\n"); | |
296 | printf("Test #3: unsuccessful key lookup (8-bit).\n"); | |
297 | for (key = 200; key < 240; key++) { | |
582a6ade | 298 | rcu_read_lock(); |
c01cfe73 MD |
299 | ja_node = cds_ja_lookup(test_ja, key); |
300 | if (ja_node) { | |
582a6ade MD |
301 | fprintf(stderr, |
302 | "Error unexpected lookup node %" PRIu64 "\n", | |
303 | key); | |
304 | assert(0); | |
305 | } | |
306 | rcu_read_unlock(); | |
307 | } | |
308 | printf("OK\n"); | |
3d8fe307 MD |
309 | printf("Test #4: remove keys (8-bit).\n"); |
310 | for (key = 0; key < 200; key++) { | |
3d8fe307 MD |
311 | struct ja_test_node *node; |
312 | ||
313 | rcu_read_lock(); | |
03ec1aeb MD |
314 | ja_node = cds_ja_lookup(test_ja, key); |
315 | if (!ja_node) { | |
3d8fe307 MD |
316 | fprintf(stderr, "Error lookup node %" PRIu64 "\n", key); |
317 | assert(0); | |
318 | } | |
03ec1aeb | 319 | node = caa_container_of(ja_node, struct ja_test_node, node); |
3d8fe307 MD |
320 | ret = cds_ja_del(test_ja, key, &node->node); |
321 | if (ret) { | |
322 | fprintf(stderr, "Error (%d) removing node %" PRIu64 "\n", ret, key); | |
323 | assert(0); | |
324 | } | |
21ac4c56 | 325 | rcu_free_test_node(node); |
03ec1aeb MD |
326 | ja_node = cds_ja_lookup(test_ja, key); |
327 | if (ja_node) { | |
328 | fprintf(stderr, "Error lookup %" PRIu64 ": %p (after delete) failed. Node is not expected.\n", key, ja_node); | |
4d6ef45e MD |
329 | assert(0); |
330 | } | |
3d8fe307 MD |
331 | rcu_read_unlock(); |
332 | } | |
333 | printf("OK\n"); | |
582a6ade | 334 | |
5277c70e | 335 | printf("Test #5: lookup below/above equal (8-bit).\n"); |
c6cd5af3 MD |
336 | |
337 | for (i = 0; i < CAA_ARRAY_SIZE(ka); i++) { | |
338 | struct ja_test_node *node = node_alloc(); | |
339 | ||
340 | key = ka[i]; | |
341 | ja_test_node_init(node, key); | |
342 | rcu_read_lock(); | |
343 | ret = cds_ja_add(test_ja, key, &node->node); | |
344 | rcu_read_unlock(); | |
345 | if (ret) { | |
346 | fprintf(stderr, "Error (%d) adding node %" PRIu64 "\n", | |
347 | ret, key); | |
348 | assert(0); | |
349 | } | |
350 | } | |
351 | ||
352 | for (i = 0; i < CAA_ARRAY_SIZE(ka); i++) { | |
c6cd5af3 | 353 | struct ja_test_node *node; |
36305a3d | 354 | uint64_t result_key; |
c6cd5af3 MD |
355 | |
356 | key = ka[i] + ka_test_offset; | |
357 | rcu_read_lock(); | |
36305a3d | 358 | ja_node = cds_ja_lookup_below_equal(test_ja, key, &result_key); |
03ec1aeb | 359 | if (!ja_node) { |
5277c70e | 360 | fprintf(stderr, "Error lookup below equal. Cannot find expected key %" PRIu64" below or equal to %" PRIu64 ".\n", |
c6cd5af3 MD |
361 | ka[i], key); |
362 | assert(0); | |
363 | } | |
03ec1aeb | 364 | node = caa_container_of(ja_node, struct ja_test_node, node); |
36305a3d MD |
365 | if (node->key != ka[i] || result_key != ka[i]) { |
366 | fprintf(stderr, "Error lookup below equal. Expecting key %" PRIu64 " below or equal to %" PRIu64 ", but found %" PRIu64 "/%" PRIu64" instead.\n", | |
367 | ka[i], key, node->key, result_key); | |
5277c70e MD |
368 | assert(0); |
369 | } | |
370 | rcu_read_unlock(); | |
371 | } | |
372 | ||
373 | for (i = 0; i < CAA_ARRAY_SIZE(ka); i++) { | |
5277c70e | 374 | struct ja_test_node *node; |
36305a3d | 375 | uint64_t result_key; |
5277c70e MD |
376 | |
377 | key = ka[i] - ka_test_offset; | |
378 | rcu_read_lock(); | |
36305a3d | 379 | ja_node = cds_ja_lookup_above_equal(test_ja, key, &result_key); |
5277c70e MD |
380 | if (!ja_node) { |
381 | fprintf(stderr, "Error lookup above equal. Cannot find expected key %" PRIu64" below or equal to %" PRIu64 ".\n", | |
382 | ka[i], key); | |
383 | assert(0); | |
384 | } | |
385 | node = caa_container_of(ja_node, struct ja_test_node, node); | |
36305a3d MD |
386 | if (node->key != ka[i] || result_key != ka[i]) { |
387 | fprintf(stderr, "Error lookup above equal. Expecting key %" PRIu64 " below or equal to %" PRIu64 ", but found %" PRIu64 "/%" PRIu64" instead.\n", | |
388 | ka[i], key, node->key, result_key); | |
c6cd5af3 MD |
389 | assert(0); |
390 | } | |
391 | rcu_read_unlock(); | |
392 | } | |
393 | ||
394 | for (i = 0; i < CAA_ARRAY_SIZE(ka); i++) { | |
c6cd5af3 | 395 | struct ja_test_node *node; |
36305a3d | 396 | uint64_t result_key; |
c6cd5af3 MD |
397 | |
398 | key = ka[i]; /* without offset */ | |
399 | rcu_read_lock(); | |
36305a3d | 400 | ja_node = cds_ja_lookup_below_equal(test_ja, key, &result_key); |
03ec1aeb | 401 | if (!ja_node) { |
5277c70e MD |
402 | fprintf(stderr, "Error lookup below equal. Cannot find expected key %" PRIu64" below or equal to %" PRIu64 ".\n", |
403 | ka[i], key); | |
404 | assert(0); | |
405 | } | |
406 | node = caa_container_of(ja_node, struct ja_test_node, node); | |
36305a3d MD |
407 | if (node->key != ka[i] || result_key != ka[i]) { |
408 | fprintf(stderr, "Error lookup below equal. Expecting key %" PRIu64 " below or equal to %" PRIu64 ", but found %" PRIu64 "/%" PRIu64" instead.\n", | |
409 | ka[i], key, node->key, result_key); | |
5277c70e MD |
410 | assert(0); |
411 | } | |
412 | ||
36305a3d | 413 | ja_node = cds_ja_lookup_above_equal(test_ja, key, &result_key); |
5277c70e MD |
414 | if (!ja_node) { |
415 | fprintf(stderr, "Error lookup above equal. Cannot find expected key %" PRIu64" below or equal to %" PRIu64 ".\n", | |
c6cd5af3 MD |
416 | ka[i], key); |
417 | assert(0); | |
418 | } | |
03ec1aeb | 419 | node = caa_container_of(ja_node, struct ja_test_node, node); |
36305a3d MD |
420 | if (node->key != ka[i] || result_key != ka[i]) { |
421 | fprintf(stderr, "Error lookup above equal. Expecting key %" PRIu64 " below or equal to %" PRIu64 ", but found %" PRIu64 "/%" PRIu64" instead.\n", | |
422 | ka[i], key, node->key, result_key); | |
c6cd5af3 MD |
423 | assert(0); |
424 | } | |
425 | rcu_read_unlock(); | |
426 | } | |
427 | ||
428 | printf("OK\n"); | |
429 | ||
c01cfe73 MD |
430 | ret = test_free_all_nodes(test_ja); |
431 | if (ret) { | |
432 | fprintf(stderr, "Error freeing all nodes\n"); | |
433 | return -1; | |
434 | } | |
435 | ||
99e6e3dc | 436 | ret = cds_ja_destroy(test_ja); |
582a6ade MD |
437 | if (ret) { |
438 | fprintf(stderr, "Error destroying judy array\n"); | |
439 | return -1; | |
440 | } | |
441 | return 0; | |
442 | } | |
443 | ||
444 | static | |
445 | int test_16bit_key(void) | |
446 | { | |
c6cd5af3 | 447 | int ret, i; |
582a6ade | 448 | uint64_t key; |
5277c70e | 449 | uint64_t ka[] = { 105, 206, 4000, 4111, 59990, 65435 }; |
c6cd5af3 | 450 | uint64_t ka_test_offset = 100; |
c01cfe73 | 451 | struct cds_ja_node *ja_node; |
582a6ade MD |
452 | |
453 | /* Test with 16-bit key */ | |
454 | test_ja = cds_ja_new(16); | |
455 | if (!test_ja) { | |
456 | printf("Error allocating judy array.\n"); | |
457 | return -1; | |
458 | } | |
459 | ||
460 | /* Add keys */ | |
461 | printf("Test #1: add keys (16-bit).\n"); | |
4d6ef45e MD |
462 | for (key = 0; key < 10000; key++) { |
463 | //for (key = 0; key < 65536; key+=256) { | |
026545d3 | 464 | struct ja_test_node *node = node_alloc(); |
582a6ade MD |
465 | |
466 | ja_test_node_init(node, key); | |
467 | rcu_read_lock(); | |
468 | ret = cds_ja_add(test_ja, key, &node->node); | |
469 | rcu_read_unlock(); | |
470 | if (ret) { | |
471 | fprintf(stderr, "Error (%d) adding node %" PRIu64 "\n", | |
472 | ret, key); | |
473 | assert(0); | |
474 | } | |
475 | } | |
476 | printf("OK\n"); | |
477 | ||
478 | printf("Test #2: successful key lookup (16-bit).\n"); | |
4d6ef45e MD |
479 | for (key = 0; key < 10000; key++) { |
480 | //for (key = 0; key < 65536; key+=256) { | |
c01cfe73 | 481 | struct cds_ja_node *ja_node; |
582a6ade MD |
482 | |
483 | rcu_read_lock(); | |
c01cfe73 MD |
484 | ja_node = cds_ja_lookup(test_ja, key); |
485 | if (!ja_node) { | |
582a6ade MD |
486 | fprintf(stderr, "Error lookup node %" PRIu64 "\n", key); |
487 | assert(0); | |
488 | } | |
489 | rcu_read_unlock(); | |
490 | } | |
491 | printf("OK\n"); | |
492 | printf("Test #3: unsuccessful key lookup (16-bit).\n"); | |
493 | for (key = 11000; key <= 11002; key++) { | |
c01cfe73 | 494 | struct cds_ja_node *ja_node; |
582a6ade MD |
495 | |
496 | rcu_read_lock(); | |
c01cfe73 MD |
497 | ja_node = cds_ja_lookup(test_ja, key); |
498 | if (ja_node) { | |
582a6ade MD |
499 | fprintf(stderr, |
500 | "Error unexpected lookup node %" PRIu64 "\n", | |
501 | key); | |
502 | assert(0); | |
503 | } | |
504 | rcu_read_unlock(); | |
505 | } | |
506 | printf("OK\n"); | |
4d6ef45e MD |
507 | printf("Test #4: remove keys (16-bit).\n"); |
508 | for (key = 0; key < 10000; key++) { | |
509 | //for (key = 0; key < 65536; key+=256) { | |
4d6ef45e MD |
510 | struct ja_test_node *node; |
511 | ||
512 | rcu_read_lock(); | |
03ec1aeb MD |
513 | ja_node = cds_ja_lookup(test_ja, key); |
514 | if (!ja_node) { | |
4d6ef45e MD |
515 | fprintf(stderr, "Error lookup node %" PRIu64 "\n", key); |
516 | assert(0); | |
517 | } | |
03ec1aeb | 518 | node = caa_container_of(ja_node, struct ja_test_node, node); |
4d6ef45e MD |
519 | ret = cds_ja_del(test_ja, key, &node->node); |
520 | if (ret) { | |
521 | fprintf(stderr, "Error (%d) removing node %" PRIu64 "\n", ret, key); | |
522 | assert(0); | |
523 | } | |
21ac4c56 | 524 | rcu_free_test_node(node); |
03ec1aeb MD |
525 | ja_node = cds_ja_lookup(test_ja, key); |
526 | if (ja_node) { | |
527 | fprintf(stderr, "Error lookup %" PRIu64 ": %p (after delete) failed. Node is not expected.\n", key, ja_node); | |
4d6ef45e MD |
528 | assert(0); |
529 | } | |
530 | rcu_read_unlock(); | |
531 | } | |
532 | printf("OK\n"); | |
582a6ade | 533 | |
36305a3d | 534 | printf("Test #5: lookup below/above equal (16-bit).\n"); |
c6cd5af3 MD |
535 | |
536 | for (i = 0; i < CAA_ARRAY_SIZE(ka); i++) { | |
537 | struct ja_test_node *node = node_alloc(); | |
538 | ||
539 | key = ka[i]; | |
540 | ja_test_node_init(node, key); | |
541 | rcu_read_lock(); | |
542 | ret = cds_ja_add(test_ja, key, &node->node); | |
543 | rcu_read_unlock(); | |
544 | if (ret) { | |
545 | fprintf(stderr, "Error (%d) adding node %" PRIu64 "\n", | |
546 | ret, key); | |
547 | assert(0); | |
548 | } | |
549 | } | |
550 | ||
551 | for (i = 0; i < CAA_ARRAY_SIZE(ka); i++) { | |
c6cd5af3 | 552 | struct ja_test_node *node; |
36305a3d | 553 | uint64_t result_key; |
c6cd5af3 MD |
554 | |
555 | key = ka[i] + ka_test_offset; | |
556 | rcu_read_lock(); | |
36305a3d | 557 | ja_node = cds_ja_lookup_below_equal(test_ja, key, &result_key); |
03ec1aeb | 558 | if (!ja_node) { |
5277c70e | 559 | fprintf(stderr, "Error lookup below equal. Cannot find expected key %" PRIu64" below or equal to %" PRIu64 ".\n", |
c6cd5af3 MD |
560 | ka[i], key); |
561 | assert(0); | |
562 | } | |
03ec1aeb | 563 | node = caa_container_of(ja_node, struct ja_test_node, node); |
36305a3d MD |
564 | if (node->key != ka[i] || result_key != ka[i]) { |
565 | fprintf(stderr, "Error lookup below equal. Expecting key %" PRIu64 " below or equal to %" PRIu64 ", but found %" PRIu64 "/%" PRIu64" instead.\n", | |
566 | ka[i], key, node->key, result_key); | |
5277c70e MD |
567 | assert(0); |
568 | } | |
569 | rcu_read_unlock(); | |
570 | } | |
571 | ||
572 | for (i = 0; i < CAA_ARRAY_SIZE(ka); i++) { | |
5277c70e | 573 | struct ja_test_node *node; |
36305a3d | 574 | uint64_t result_key; |
5277c70e MD |
575 | |
576 | key = ka[i] - ka_test_offset; | |
577 | rcu_read_lock(); | |
36305a3d | 578 | ja_node = cds_ja_lookup_above_equal(test_ja, key, &result_key); |
5277c70e MD |
579 | if (!ja_node) { |
580 | fprintf(stderr, "Error lookup above equal. Cannot find expected key %" PRIu64" above or equal to %" PRIu64 ".\n", | |
581 | ka[i], key); | |
582 | assert(0); | |
583 | } | |
584 | node = caa_container_of(ja_node, struct ja_test_node, node); | |
36305a3d MD |
585 | if (node->key != ka[i] || result_key != ka[i]) { |
586 | fprintf(stderr, "Error lookup above equal. Expecting key %" PRIu64 " above or equal to %" PRIu64 ", but found %" PRIu64 "/%" PRIu64" instead.\n", | |
587 | ka[i], key, node->key, result_key); | |
c6cd5af3 MD |
588 | assert(0); |
589 | } | |
590 | rcu_read_unlock(); | |
591 | } | |
592 | ||
593 | for (i = 0; i < CAA_ARRAY_SIZE(ka); i++) { | |
c6cd5af3 | 594 | struct ja_test_node *node; |
36305a3d | 595 | uint64_t result_key; |
c6cd5af3 MD |
596 | |
597 | key = ka[i]; /* without offset */ | |
598 | rcu_read_lock(); | |
36305a3d | 599 | ja_node = cds_ja_lookup_below_equal(test_ja, key, &result_key); |
03ec1aeb | 600 | if (!ja_node) { |
5277c70e MD |
601 | fprintf(stderr, "Error lookup below equal. Cannot find expected key %" PRIu64" below or equal to %" PRIu64 ".\n", |
602 | ka[i], key); | |
603 | assert(0); | |
604 | } | |
605 | node = caa_container_of(ja_node, struct ja_test_node, node); | |
36305a3d MD |
606 | if (node->key != ka[i] || result_key != ka[i]) { |
607 | fprintf(stderr, "Error lookup below equal. Expecting key %" PRIu64 " below or equal to %" PRIu64 ", but found %" PRIu64 "/%" PRIu64" instead.\n", | |
608 | ka[i], key, node->key, result_key); | |
5277c70e MD |
609 | assert(0); |
610 | } | |
611 | ||
36305a3d | 612 | ja_node = cds_ja_lookup_above_equal(test_ja, key, &result_key); |
5277c70e MD |
613 | if (!ja_node) { |
614 | fprintf(stderr, "Error lookup above equal. Cannot find expected key %" PRIu64" above or equal to %" PRIu64 ".\n", | |
c6cd5af3 MD |
615 | ka[i], key); |
616 | assert(0); | |
617 | } | |
03ec1aeb | 618 | node = caa_container_of(ja_node, struct ja_test_node, node); |
36305a3d MD |
619 | if (node->key != ka[i] || result_key != ka[i]) { |
620 | fprintf(stderr, "Error lookup above equal. Expecting key %" PRIu64 " above or equal to %" PRIu64 ", but found %" PRIu64 "/%" PRIu64" instead.\n", | |
621 | ka[i], key, node->key, result_key); | |
c6cd5af3 MD |
622 | assert(0); |
623 | } | |
624 | rcu_read_unlock(); | |
625 | } | |
626 | ||
627 | printf("OK\n"); | |
628 | ||
c01cfe73 MD |
629 | ret = test_free_all_nodes(test_ja); |
630 | if (ret) { | |
631 | fprintf(stderr, "Error freeing all nodes\n"); | |
632 | return -1; | |
633 | } | |
634 | ||
99e6e3dc | 635 | ret = cds_ja_destroy(test_ja); |
582a6ade MD |
636 | if (ret) { |
637 | fprintf(stderr, "Error destroying judy array\n"); | |
638 | return -1; | |
639 | } | |
640 | return 0; | |
641 | } | |
642 | ||
6fdf58e8 MD |
643 | /* |
644 | * nr_dup is number of nodes per key. | |
645 | */ | |
582a6ade | 646 | static |
6fdf58e8 | 647 | int test_sparse_key(unsigned int bits, int nr_dup) |
582a6ade | 648 | { |
582a6ade | 649 | uint64_t key, max_key; |
6fdf58e8 | 650 | int zerocount, i, ret; |
c01cfe73 | 651 | struct cds_ja_node *ja_node; |
582a6ade MD |
652 | |
653 | if (bits == 64) | |
654 | max_key = UINT64_MAX; | |
655 | else | |
656 | max_key = (1ULL << bits) - 1; | |
657 | ||
658 | printf("Sparse key test begins for %u-bit keys\n", bits); | |
659 | /* Test with 16-bit key */ | |
660 | test_ja = cds_ja_new(bits); | |
661 | if (!test_ja) { | |
662 | printf("Error allocating judy array.\n"); | |
663 | return -1; | |
664 | } | |
665 | ||
666 | /* Add keys */ | |
667 | printf("Test #1: add keys (%u-bit).\n", bits); | |
6fdf58e8 MD |
668 | for (i = 0; i < nr_dup; i++) { |
669 | zerocount = 0; | |
670 | for (key = 0; key <= max_key && (key != 0 || zerocount < 1); key += 1ULL << (bits - 8)) { | |
026545d3 | 671 | struct ja_test_node *node = node_alloc(); |
582a6ade | 672 | |
6fdf58e8 MD |
673 | ja_test_node_init(node, key); |
674 | rcu_read_lock(); | |
675 | ret = cds_ja_add(test_ja, key, &node->node); | |
676 | rcu_read_unlock(); | |
677 | if (ret) { | |
678 | fprintf(stderr, "Error (%d) adding node %" PRIu64 "\n", | |
679 | ret, key); | |
680 | assert(0); | |
681 | } | |
682 | if (key == 0) | |
683 | zerocount++; | |
582a6ade | 684 | } |
582a6ade MD |
685 | } |
686 | printf("OK\n"); | |
687 | ||
688 | printf("Test #2: successful key lookup (%u-bit).\n", bits); | |
689 | zerocount = 0; | |
690 | for (key = 0; key <= max_key && (key != 0 || zerocount < 1); key += 1ULL << (bits - 8)) { | |
6fdf58e8 | 691 | struct ja_test_node *node; |
6fdf58e8 | 692 | int count = 0; |
582a6ade MD |
693 | |
694 | rcu_read_lock(); | |
03ec1aeb MD |
695 | ja_node = cds_ja_lookup(test_ja, key); |
696 | if (!ja_node) { | |
582a6ade MD |
697 | fprintf(stderr, "Error lookup node %" PRIu64 "\n", key); |
698 | assert(0); | |
699 | } | |
03ec1aeb | 700 | cds_ja_for_each_duplicate_rcu(ja_node) { |
6fdf58e8 MD |
701 | count++; |
702 | } | |
703 | if (count != nr_dup) { | |
704 | fprintf(stderr, "Unexpected number of match for key %" PRIu64 ", expected %d, got %d.\n", key, nr_dup, count); | |
705 | } | |
582a6ade MD |
706 | rcu_read_unlock(); |
707 | if (key == 0) | |
708 | zerocount++; | |
709 | } | |
710 | printf("OK\n"); | |
711 | if (bits > 8) { | |
712 | printf("Test #3: unsuccessful key lookup (%u-bit).\n", bits); | |
713 | zerocount = 0; | |
714 | for (key = 0; key <= max_key && (key != 0 || zerocount < 1); key += 1ULL << (bits - 8)) { | |
582a6ade | 715 | rcu_read_lock(); |
03ec1aeb MD |
716 | ja_node = cds_ja_lookup(test_ja, key + 42); |
717 | if (ja_node) { | |
582a6ade MD |
718 | fprintf(stderr, |
719 | "Error unexpected lookup node %" PRIu64 "\n", | |
720 | key + 42); | |
721 | assert(0); | |
722 | } | |
723 | rcu_read_unlock(); | |
724 | if (key == 0) | |
725 | zerocount++; | |
726 | } | |
727 | printf("OK\n"); | |
728 | } | |
81c18eb9 | 729 | printf("Test #4: remove keys (%u-bit).\n", bits); |
4d6ef45e MD |
730 | zerocount = 0; |
731 | for (key = 0; key <= max_key && (key != 0 || zerocount < 1); key += 1ULL << (bits - 8)) { | |
6fdf58e8 | 732 | int count = 0; |
4d6ef45e MD |
733 | |
734 | rcu_read_lock(); | |
03ec1aeb | 735 | ja_node = cds_ja_lookup(test_ja, key); |
6fdf58e8 | 736 | |
03ec1aeb MD |
737 | cds_ja_for_each_duplicate_rcu(ja_node) { |
738 | struct cds_ja_node *test_ja_node; | |
739 | struct ja_test_node *node; | |
6fdf58e8 MD |
740 | |
741 | count++; | |
03ec1aeb MD |
742 | node = caa_container_of(ja_node, |
743 | struct ja_test_node, node); | |
6fdf58e8 MD |
744 | ret = cds_ja_del(test_ja, key, &node->node); |
745 | if (ret) { | |
746 | fprintf(stderr, "Error (%d) removing node %" PRIu64 "\n", ret, key); | |
747 | assert(0); | |
748 | } | |
21ac4c56 | 749 | rcu_free_test_node(node); |
03ec1aeb MD |
750 | test_ja_node = cds_ja_lookup(test_ja, key); |
751 | if (count < nr_dup && !test_ja_node) { | |
6fdf58e8 MD |
752 | fprintf(stderr, "Error: no node found after deletion of some nodes of a key\n"); |
753 | assert(0); | |
754 | } | |
4d6ef45e | 755 | } |
03ec1aeb MD |
756 | ja_node = cds_ja_lookup(test_ja, key); |
757 | if (ja_node) { | |
758 | fprintf(stderr, "Error lookup %" PRIu64 ": %p (after delete) failed. Node is not expected.\n", key, ja_node); | |
4d6ef45e MD |
759 | assert(0); |
760 | } | |
761 | rcu_read_unlock(); | |
762 | if (key == 0) | |
763 | zerocount++; | |
764 | } | |
765 | printf("OK\n"); | |
582a6ade | 766 | |
c01cfe73 MD |
767 | ret = test_free_all_nodes(test_ja); |
768 | if (ret) { | |
769 | fprintf(stderr, "Error freeing all nodes\n"); | |
770 | return -1; | |
771 | } | |
772 | ||
99e6e3dc | 773 | ret = cds_ja_destroy(test_ja); |
582a6ade MD |
774 | if (ret) { |
775 | fprintf(stderr, "Error destroying judy array\n"); | |
776 | return -1; | |
777 | } | |
778 | printf("Test ends\n"); | |
779 | ||
780 | return 0; | |
781 | } | |
782 | ||
1a0c0717 MD |
783 | static |
784 | int do_sanity_test(void) | |
785 | { | |
786 | int i, j, ret; | |
582a6ade | 787 | |
1a0c0717 MD |
788 | printf("Sanity test start.\n"); |
789 | ||
790 | for (i = 0; i < 3; i++) { | |
791 | ret = test_8bit_key(); | |
792 | if (ret) { | |
793 | return ret; | |
794 | } | |
795 | rcu_quiescent_state(); | |
796 | } | |
797 | ret = test_16bit_key(); | |
798 | if (ret) { | |
799 | return ret; | |
800 | } | |
801 | rcu_quiescent_state(); | |
802 | ||
803 | /* key bits */ | |
804 | for (i = 8; i <= 64; i *= 2) { | |
805 | /* nr of nodes per key */ | |
806 | for (j = 1; j < 4; j++) { | |
807 | ret = test_sparse_key(i, j); | |
808 | if (ret) { | |
809 | return ret; | |
810 | } | |
811 | rcu_quiescent_state(); | |
812 | } | |
813 | } | |
814 | printf("Sanity test end.\n"); | |
815 | ||
816 | return 0; | |
817 | } | |
818 | ||
819 | enum urcu_ja_addremove { | |
820 | AR_RANDOM = 0, | |
821 | AR_ADD = 1, | |
822 | AR_REMOVE = -1, | |
823 | }; /* 1: add, -1 remove, 0: random */ | |
824 | ||
825 | static enum urcu_ja_addremove addremove; /* 1: add, -1 remove, 0: random */ | |
826 | ||
827 | static | |
828 | void test_ja_rw_sigusr1_handler(int signo) | |
829 | { | |
830 | switch (addremove) { | |
831 | case AR_ADD: | |
832 | printf("Add/Remove: random.\n"); | |
833 | addremove = AR_RANDOM; | |
834 | break; | |
835 | case AR_RANDOM: | |
836 | printf("Add/Remove: remove only.\n"); | |
837 | addremove = AR_REMOVE; | |
838 | break; | |
839 | case AR_REMOVE: | |
840 | printf("Add/Remove: add only.\n"); | |
841 | addremove = AR_ADD; | |
842 | break; | |
843 | } | |
844 | } | |
845 | ||
846 | static | |
847 | void *test_ja_rw_thr_reader(void *_count) | |
848 | { | |
849 | unsigned long long *count = _count; | |
03ec1aeb | 850 | struct cds_ja_node *ja_node; |
1a0c0717 MD |
851 | uint64_t key; |
852 | ||
853 | printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n", | |
854 | "reader", pthread_self(), (unsigned long) gettid()); | |
855 | ||
856 | set_affinity(); | |
857 | ||
858 | rcu_register_thread(); | |
859 | ||
860 | while (!test_go) | |
861 | { | |
862 | } | |
863 | cmm_smp_mb(); | |
864 | ||
865 | for (;;) { | |
866 | rcu_read_lock(); | |
867 | ||
868 | /* note: only looking up ulong keys */ | |
869 | key = ((unsigned long) rand_r(&URCU_TLS(rand_lookup)) % lookup_pool_size) + lookup_pool_offset; | |
4df4328c | 870 | key *= key_mul; |
03ec1aeb MD |
871 | ja_node = cds_ja_lookup(test_ja, key); |
872 | if (!ja_node) { | |
1a0c0717 MD |
873 | if (validate_lookup) { |
874 | printf("[ERROR] Lookup cannot find initial node.\n"); | |
875 | exit(-1); | |
876 | } | |
877 | URCU_TLS(lookup_fail)++; | |
878 | } else { | |
879 | URCU_TLS(lookup_ok)++; | |
880 | } | |
756c692d | 881 | rcu_debug_yield_read(); |
1a0c0717 MD |
882 | if (caa_unlikely(rduration)) |
883 | loop_sleep(rduration); | |
884 | rcu_read_unlock(); | |
885 | URCU_TLS(nr_reads)++; | |
886 | if (caa_unlikely(!test_duration_read())) | |
887 | break; | |
888 | if (caa_unlikely((URCU_TLS(nr_reads) & ((1 << 10) - 1)) == 0)) | |
889 | rcu_quiescent_state(); | |
890 | } | |
891 | ||
892 | rcu_unregister_thread(); | |
893 | ||
894 | *count = URCU_TLS(nr_reads); | |
895 | printf_verbose("thread_end %s, thread id : %lx, tid %lu\n", | |
896 | "reader", pthread_self(), (unsigned long) gettid()); | |
897 | printf_verbose("readid : %lx, lookupfail %lu, lookupok %lu\n", | |
898 | pthread_self(), URCU_TLS(lookup_fail), | |
899 | URCU_TLS(lookup_ok)); | |
900 | return ((void*)1); | |
901 | } | |
902 | ||
34ffee3e MD |
903 | static |
904 | int is_add(void) | |
905 | { | |
906 | return ((unsigned int) rand_r(&URCU_TLS(rand_lookup)) % 100) < add_ratio; | |
907 | } | |
908 | ||
1a0c0717 MD |
909 | static |
910 | void *test_ja_rw_thr_writer(void *_count) | |
911 | { | |
912 | struct wr_count *count = _count; | |
1a0c0717 MD |
913 | uint64_t key; |
914 | int ret; | |
915 | ||
916 | printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n", | |
917 | "writer", pthread_self(), (unsigned long) gettid()); | |
918 | ||
919 | set_affinity(); | |
920 | ||
921 | rcu_register_thread(); | |
922 | ||
923 | while (!test_go) | |
924 | { | |
925 | } | |
926 | cmm_smp_mb(); | |
927 | ||
928 | for (;;) { | |
34ffee3e MD |
929 | if ((addremove == AR_ADD) |
930 | || (addremove == AR_RANDOM && is_add())) { | |
026545d3 | 931 | struct ja_test_node *node = node_alloc(); |
75d573aa | 932 | struct cds_ja_node *ret_node; |
1a0c0717 MD |
933 | |
934 | /* note: only inserting ulong keys */ | |
935 | key = ((unsigned long) rand_r(&URCU_TLS(rand_lookup)) % write_pool_size) + write_pool_offset; | |
4df4328c | 936 | key *= key_mul; |
1a0c0717 MD |
937 | ja_test_node_init(node, key); |
938 | rcu_read_lock(); | |
04c48ddf MD |
939 | if (add_unique) { |
940 | ret_node = cds_ja_add_unique(test_ja, key, &node->node); | |
941 | if (ret_node != &node->node) { | |
5b62a42a | 942 | free_test_node(node); |
04c48ddf MD |
943 | URCU_TLS(nr_addexist)++; |
944 | } else { | |
945 | URCU_TLS(nr_add)++; | |
946 | } | |
947 | } else if (add_replace) { | |
948 | assert(0); /* not implemented yet. */ | |
75d573aa | 949 | } else { |
04c48ddf MD |
950 | ret = cds_ja_add(test_ja, key, &node->node); |
951 | if (ret) { | |
952 | fprintf(stderr, "Error in cds_ja_add: %d\n", ret); | |
5b62a42a | 953 | free_test_node(node); |
04c48ddf MD |
954 | } else { |
955 | URCU_TLS(nr_add)++; | |
956 | } | |
1a0c0717 | 957 | } |
04c48ddf | 958 | rcu_read_unlock(); |
1a0c0717 | 959 | } else { |
03ec1aeb | 960 | struct cds_ja_node *ja_node; |
1a0c0717 MD |
961 | struct ja_test_node *node; |
962 | ||
963 | /* May delete */ | |
964 | /* note: only deleting ulong keys */ | |
965 | key = ((unsigned long) rand_r(&URCU_TLS(rand_lookup)) % write_pool_size) + write_pool_offset; | |
4df4328c | 966 | key *= key_mul; |
1a0c0717 MD |
967 | |
968 | rcu_read_lock(); | |
969 | ||
03ec1aeb MD |
970 | ja_node = cds_ja_lookup(test_ja, key); |
971 | /* Remove first entry */ | |
972 | if (ja_node) { | |
973 | node = caa_container_of(ja_node, | |
974 | struct ja_test_node, node); | |
1a0c0717 | 975 | ret = cds_ja_del(test_ja, key, &node->node); |
d7069878 | 976 | if (!ret) { |
21ac4c56 | 977 | rcu_free_test_node(node); |
1a0c0717 | 978 | URCU_TLS(nr_del)++; |
d7069878 MD |
979 | } else { |
980 | URCU_TLS(nr_delnoent)++; | |
1a0c0717 MD |
981 | } |
982 | } else { | |
983 | URCU_TLS(nr_delnoent)++; | |
984 | } | |
6da38aec | 985 | rcu_read_unlock(); |
1a0c0717 MD |
986 | } |
987 | ||
988 | URCU_TLS(nr_writes)++; | |
989 | if (caa_unlikely(!test_duration_write())) | |
990 | break; | |
991 | if (caa_unlikely(wdelay)) | |
992 | loop_sleep(wdelay); | |
993 | if (caa_unlikely((URCU_TLS(nr_writes) & ((1 << 10) - 1)) == 0)) | |
994 | rcu_quiescent_state(); | |
995 | } | |
996 | ||
997 | rcu_unregister_thread(); | |
998 | ||
999 | printf_verbose("thread_end %s, thread id : %lx, tid %lu\n", | |
1000 | "writer", pthread_self(), (unsigned long) gettid()); | |
1001 | printf_verbose("info id %lx: nr_add %lu, nr_addexist %lu, nr_del %lu, " | |
1002 | "nr_delnoent %lu\n", pthread_self(), URCU_TLS(nr_add), | |
1003 | URCU_TLS(nr_addexist), URCU_TLS(nr_del), | |
1004 | URCU_TLS(nr_delnoent)); | |
1005 | count->update_ops = URCU_TLS(nr_writes); | |
1006 | count->add = URCU_TLS(nr_add); | |
1007 | count->add_exist = URCU_TLS(nr_addexist); | |
1008 | count->remove = URCU_TLS(nr_del); | |
1009 | return ((void*)2); | |
1010 | } | |
1011 | ||
1012 | static | |
1013 | int do_mt_populate_ja(void) | |
1014 | { | |
4df4328c | 1015 | uint64_t iter; |
1a0c0717 MD |
1016 | int ret; |
1017 | ||
1018 | if (!init_populate) | |
1019 | return 0; | |
1020 | ||
1021 | printf("Starting rw test\n"); | |
1022 | ||
4df4328c | 1023 | for (iter = init_pool_offset; iter < init_pool_offset + init_pool_size; iter++) { |
026545d3 | 1024 | struct ja_test_node *node = node_alloc(); |
4df4328c | 1025 | uint64_t key; |
1a0c0717 MD |
1026 | |
1027 | /* note: only inserting ulong keys */ | |
4df4328c MD |
1028 | key = (unsigned long) iter; |
1029 | key *= key_mul; | |
1a0c0717 MD |
1030 | ja_test_node_init(node, key); |
1031 | rcu_read_lock(); | |
1032 | ret = cds_ja_add(test_ja, key, &node->node); | |
1033 | URCU_TLS(nr_add)++; | |
1034 | URCU_TLS(nr_writes)++; | |
1035 | rcu_read_unlock(); | |
1036 | if (ret) { | |
1037 | fprintf(stderr, "Error (%d) adding node %" PRIu64 "\n", | |
1038 | ret, key); | |
1039 | assert(0); | |
1040 | } | |
1041 | } | |
1042 | return 0; | |
1043 | } | |
1044 | ||
1045 | static | |
1046 | int do_mt_test(void) | |
44151f89 | 1047 | { |
44151f89 MD |
1048 | pthread_t *tid_reader, *tid_writer; |
1049 | void *tret; | |
1a0c0717 | 1050 | int ret, i, err; |
44151f89 MD |
1051 | unsigned long long *count_reader; |
1052 | struct wr_count *count_writer; | |
1053 | unsigned long long tot_reads = 0, tot_writes = 0, | |
1054 | tot_add = 0, tot_add_exist = 0, tot_remove = 0; | |
44151f89 | 1055 | unsigned int remain; |
1a0c0717 MD |
1056 | |
1057 | tid_reader = malloc(sizeof(*tid_reader) * nr_readers); | |
1058 | tid_writer = malloc(sizeof(*tid_writer) * nr_writers); | |
1059 | count_reader = malloc(sizeof(*count_reader) * nr_readers); | |
1060 | count_writer = malloc(sizeof(*count_writer) * nr_writers); | |
1061 | ||
1062 | printf("Allocating Judy Array for %u-bit keys\n", key_bits); | |
1063 | test_ja = cds_ja_new(key_bits); | |
1064 | if (!test_ja) { | |
1065 | printf("Error allocating judy array.\n"); | |
1066 | ret = -1; | |
1067 | goto end; | |
1068 | } | |
1069 | ||
1070 | do_mt_populate_ja(); | |
1071 | ||
1072 | next_aff = 0; | |
1073 | ||
1074 | for (i = 0; i < nr_readers; i++) { | |
1075 | err = pthread_create(&tid_reader[i], | |
1076 | NULL, test_ja_rw_thr_reader, | |
1077 | &count_reader[i]); | |
1078 | if (err != 0) | |
1079 | exit(1); | |
1080 | } | |
1081 | for (i = 0; i < nr_writers; i++) { | |
1082 | err = pthread_create(&tid_writer[i], | |
1083 | NULL, test_ja_rw_thr_writer, | |
1084 | &count_writer[i]); | |
1085 | if (err != 0) | |
1086 | exit(1); | |
1087 | } | |
1088 | ||
1089 | cmm_smp_mb(); | |
1090 | ||
1091 | test_go = 1; | |
1092 | ||
1093 | rcu_thread_offline_qsbr(); | |
1094 | ||
1095 | remain = duration; | |
1096 | do { | |
1097 | remain = sleep(remain); | |
1098 | } while (remain > 0); | |
1099 | ||
1100 | test_stop = 1; | |
1101 | ||
1102 | for (i = 0; i < nr_readers; i++) { | |
1103 | err = pthread_join(tid_reader[i], &tret); | |
1104 | if (err != 0) | |
1105 | exit(1); | |
1106 | tot_reads += count_reader[i]; | |
1107 | } | |
1108 | for (i = 0; i < nr_writers; i++) { | |
1109 | err = pthread_join(tid_writer[i], &tret); | |
1110 | if (err != 0) | |
1111 | exit(1); | |
1112 | tot_writes += count_writer[i].update_ops; | |
1113 | tot_add += count_writer[i].add; | |
1114 | tot_add_exist += count_writer[i].add_exist; | |
1115 | tot_remove += count_writer[i].remove; | |
1116 | } | |
88e115c3 | 1117 | rcu_thread_online_qsbr(); |
1a0c0717 | 1118 | |
c01cfe73 MD |
1119 | ret = test_free_all_nodes(test_ja); |
1120 | if (ret) { | |
1121 | fprintf(stderr, "Error freeing all nodes\n"); | |
1122 | return -1; | |
1123 | } | |
1124 | ||
99e6e3dc | 1125 | ret = cds_ja_destroy(test_ja); |
1a0c0717 MD |
1126 | if (ret) { |
1127 | fprintf(stderr, "Error destroying judy array\n"); | |
1128 | goto end; | |
1129 | } | |
1a0c0717 MD |
1130 | |
1131 | free(tid_reader); | |
1132 | free(tid_writer); | |
1133 | free(count_reader); | |
1134 | free(count_writer); | |
1135 | ret = 0; | |
1136 | end: | |
1137 | return ret; | |
1138 | } | |
1139 | ||
026545d3 MD |
1140 | static |
1141 | int check_memory_leaks(void) | |
1142 | { | |
1143 | unsigned long na, nf; | |
1144 | ||
1145 | na = uatomic_read(&test_nodes_allocated); | |
1146 | nf = uatomic_read(&test_nodes_freed); | |
1147 | if (na != nf) { | |
1148 | fprintf(stderr, "Memory leak of %ld test nodes detected. Allocated: %lu, freed: %lu\n", | |
1149 | na - nf, na, nf); | |
1150 | return -1; | |
1151 | } | |
1152 | return 0; | |
1153 | } | |
1154 | ||
1a0c0717 MD |
1155 | int main(int argc, char **argv) |
1156 | { | |
1157 | int i, j, a, ret, err; | |
a2a7ff59 | 1158 | uint64_t key; |
1a0c0717 | 1159 | struct sigaction act; |
44151f89 MD |
1160 | |
1161 | if (argc < 4) { | |
1162 | show_usage(argc, argv); | |
1163 | return -1; | |
1164 | } | |
1165 | ||
1166 | err = sscanf(argv[1], "%u", &nr_readers); | |
1167 | if (err != 1) { | |
1168 | show_usage(argc, argv); | |
1169 | return -1; | |
1170 | } | |
1171 | ||
1172 | err = sscanf(argv[2], "%u", &nr_writers); | |
1173 | if (err != 1) { | |
1174 | show_usage(argc, argv); | |
1175 | return -1; | |
1176 | } | |
1177 | ||
1178 | err = sscanf(argv[3], "%lu", &duration); | |
1179 | if (err != 1) { | |
1180 | show_usage(argc, argv); | |
1181 | return -1; | |
1182 | } | |
1183 | ||
1184 | for (i = 4; i < argc; i++) { | |
1185 | if (argv[i][0] != '-') | |
1186 | continue; | |
1187 | switch (argv[i][1]) { | |
1188 | #ifdef DEBUG_YIELD | |
1189 | case 'r': | |
1190 | yield_active |= YIELD_READ; | |
1191 | break; | |
1192 | case 'w': | |
1193 | yield_active |= YIELD_WRITE; | |
1194 | break; | |
1195 | #endif | |
1196 | case 'a': | |
1197 | if (argc < i + 2) { | |
1198 | show_usage(argc, argv); | |
1199 | return -1; | |
1200 | } | |
1201 | a = atoi(argv[++i]); | |
1202 | cpu_affinities[next_aff++] = a; | |
1203 | use_affinity = 1; | |
1204 | printf_verbose("Adding CPU %d affinity\n", a); | |
1205 | break; | |
1206 | case 'c': | |
1207 | if (argc < i + 2) { | |
1208 | show_usage(argc, argv); | |
1209 | return -1; | |
1210 | } | |
1211 | rduration = atol(argv[++i]); | |
1212 | break; | |
1213 | case 'd': | |
1214 | if (argc < i + 2) { | |
1215 | show_usage(argc, argv); | |
1216 | return -1; | |
1217 | } | |
1218 | wdelay = atol(argv[++i]); | |
1219 | break; | |
1220 | case 'v': | |
1221 | verbose_mode = 1; | |
1222 | break; | |
34ffee3e MD |
1223 | case 'r': |
1224 | add_ratio = atoi(argv[++i]); | |
44151f89 MD |
1225 | break; |
1226 | case 'k': | |
46f373a6 | 1227 | init_populate = 1; |
44151f89 MD |
1228 | break; |
1229 | case 'R': | |
1230 | lookup_pool_offset = atol(argv[++i]); | |
1231 | break; | |
1232 | case 'S': | |
1233 | write_pool_offset = atol(argv[++i]); | |
1234 | break; | |
1235 | case 'T': | |
1236 | init_pool_offset = atol(argv[++i]); | |
1237 | break; | |
1238 | case 'M': | |
1239 | lookup_pool_size = atol(argv[++i]); | |
1240 | break; | |
1241 | case 'N': | |
1242 | write_pool_size = atol(argv[++i]); | |
1243 | break; | |
1244 | case 'O': | |
1245 | init_pool_size = atol(argv[++i]); | |
1246 | break; | |
1247 | case 'V': | |
1248 | validate_lookup = 1; | |
1249 | break; | |
04c48ddf | 1250 | case 't': |
1a0c0717 MD |
1251 | sanity_test = 1; |
1252 | break; | |
1253 | case 'B': | |
1254 | key_bits = atol(argv[++i]); | |
1255 | break; | |
4df4328c MD |
1256 | case 'm': |
1257 | key_mul = atoll(argv[++i]); | |
1258 | break; | |
04c48ddf MD |
1259 | case 'u': |
1260 | add_unique = 1; | |
1261 | break; | |
1262 | case 's': | |
1263 | add_replace = 1; | |
1264 | break; | |
026545d3 MD |
1265 | case 'l': |
1266 | leak_detection = 1; | |
1267 | break; | |
44151f89 MD |
1268 | } |
1269 | } | |
1270 | ||
1271 | printf_verbose("running test for %lu seconds, %u readers, %u writers.\n", | |
1272 | duration, nr_readers, nr_writers); | |
1273 | printf_verbose("Writer delay : %lu loops.\n", wdelay); | |
1274 | printf_verbose("Reader duration : %lu loops.\n", rduration); | |
34ffee3e | 1275 | printf_verbose("Add ratio: %u%%.\n", add_ratio); |
04c48ddf MD |
1276 | printf_verbose("Mode:%s%s.\n", |
1277 | " add/remove", | |
1278 | add_unique ? " uniquify" : ( add_replace ? " replace" : " insert")); | |
b31cb285 | 1279 | printf_verbose("Key multiplication factor: %" PRIu64 ".\n", key_mul); |
44151f89 MD |
1280 | printf_verbose("Init pool size offset %lu size %lu.\n", |
1281 | init_pool_offset, init_pool_size); | |
1282 | printf_verbose("Lookup pool size offset %lu size %lu.\n", | |
1283 | lookup_pool_offset, lookup_pool_size); | |
1284 | printf_verbose("Update pool size offset %lu size %lu.\n", | |
1285 | write_pool_offset, write_pool_size); | |
46fc422b MD |
1286 | if (validate_lookup) |
1287 | printf_verbose("Validating lookups.\n"); | |
026545d3 MD |
1288 | if (leak_detection) |
1289 | printf_verbose("Memory leak dection activated.\n"); | |
44151f89 MD |
1290 | printf_verbose("thread %-6s, thread id : %lx, tid %lu\n", |
1291 | "main", pthread_self(), (unsigned long)gettid()); | |
1292 | ||
1a0c0717 MD |
1293 | memset(&act, 0, sizeof(act)); |
1294 | ret = sigemptyset(&act.sa_mask); | |
1295 | if (ret == -1) { | |
1296 | perror("sigemptyset"); | |
1297 | return -1; | |
1298 | } | |
1299 | act.sa_handler = test_ja_rw_sigusr1_handler; | |
1300 | act.sa_flags = SA_RESTART; | |
1301 | ret = sigaction(SIGUSR1, &act, NULL); | |
1302 | if (ret == -1) { | |
1303 | perror("sigaction"); | |
1304 | return -1; | |
1305 | } | |
44151f89 MD |
1306 | |
1307 | err = create_all_cpu_call_rcu_data(0); | |
1308 | if (err) { | |
1309 | printf("Per-CPU call_rcu() worker threads unavailable. Using default global worker thread.\n"); | |
1310 | } | |
1311 | ||
582a6ade | 1312 | rcu_register_thread(); |
44151f89 | 1313 | |
1a0c0717 MD |
1314 | if (sanity_test) { |
1315 | ret = do_sanity_test(); | |
1a0c0717 | 1316 | } else { |
4bea9b8d | 1317 | ret = do_mt_test(); |
44151f89 | 1318 | } |
a2a7ff59 | 1319 | |
026545d3 MD |
1320 | /* Wait for in-flight call_rcu free to complete for leak detection */ |
1321 | rcu_barrier(); | |
1322 | ||
1323 | ret |= check_memory_leaks(); | |
1324 | ||
582a6ade | 1325 | rcu_unregister_thread(); |
1a0c0717 | 1326 | free_all_cpu_call_rcu_data(); |
4bea9b8d MD |
1327 | |
1328 | if (ret) { | |
1329 | printf("Test ended with error: %d\n", ret); | |
1330 | } | |
1331 | return ret; | |
44151f89 | 1332 | } |