4 * Userspace RCU library - test program
6 * Copyright 2009-2012 - Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
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.
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.
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.
24 #include "test_urcu_ja_range.h"
25 #include "../common/debug-yield.h"
29 DEFINE_URCU_TLS(unsigned int, rand_lookup
);
30 DEFINE_URCU_TLS(unsigned long, nr_add
);
31 DEFINE_URCU_TLS(unsigned long, nr_addexist
);
32 DEFINE_URCU_TLS(unsigned long, nr_del
);
33 DEFINE_URCU_TLS(unsigned long, nr_delnoent
);
34 DEFINE_URCU_TLS(unsigned long, lookup_fail
);
35 DEFINE_URCU_TLS(unsigned long, lookup_ok
);
37 struct cds_ja
*test_ja
;
39 volatile int test_go
, test_stop
;
43 unsigned long duration
;
45 /* read-side C.S. duration, in loops */
46 unsigned long rduration
;
48 unsigned long init_populate
;
51 unsigned long init_pool_offset
, lookup_pool_offset
, write_pool_offset
;
52 unsigned long init_pool_size
= DEFAULT_RAND_POOL
,
53 lookup_pool_size
= DEFAULT_RAND_POOL
,
54 write_pool_size
= DEFAULT_RAND_POOL
;
57 unsigned int key_bits
= 32;
63 unsigned int cpu_affinities
[NR_CPUS
];
64 unsigned int next_aff
= 0;
67 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
69 DEFINE_URCU_TLS(unsigned long long, nr_writes
);
70 DEFINE_URCU_TLS(unsigned long long, nr_reads
);
72 unsigned int nr_readers
;
73 unsigned int nr_writers
;
75 static unsigned int add_ratio
= 50, range_max_len
= 0;
76 static uint64_t key_mul
= 1ULL;
78 static int add_unique
, add_replace
;
80 static pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
82 static int leak_detection
;
83 static unsigned long test_nodes_allocated
, test_nodes_freed
;
85 void set_affinity(void)
94 #if HAVE_SCHED_SETAFFINITY
95 ret
= pthread_mutex_lock(&affinity_mutex
);
97 perror("Error in pthread mutex lock");
100 cpu
= cpu_affinities
[next_aff
++];
101 ret
= pthread_mutex_unlock(&affinity_mutex
);
103 perror("Error in pthread mutex unlock");
108 #if SCHED_SETAFFINITY_ARGS == 2
109 sched_setaffinity(0, &mask
);
111 sched_setaffinity(0, sizeof(mask
), &mask
);
113 #endif /* HAVE_SCHED_SETAFFINITY */
116 void rcu_copy_mutex_lock(void)
119 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
121 perror("Error in pthread mutex lock");
126 void rcu_copy_mutex_unlock(void)
130 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
132 perror("Error in pthread mutex unlock");
137 void show_usage(int argc
, char **argv
)
139 printf("Usage : %s nr_readers nr_writers duration (s)\n", argv
[0]);
141 printf(" [-r] [-w] (yield reader and/or writer)\n");
143 printf(" [-d delay] (writer period (us))\n");
144 printf(" [-c duration] (reader C.S. duration (in loops))\n");
145 printf(" [-v] (verbose output)\n");
146 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
147 printf(" [-u] Add unique keys.\n");
148 printf(" [-s] Replace existing keys.\n");
149 printf(" [not -u nor -s] Add entries (supports redundant keys).\n");
150 printf(" [-r ratio] Add ratio (in %% of add+removal).\n");
151 printf(" [-k] Populate init nodes.\n");
152 printf(" [-R offset] Lookup pool offset.\n");
153 printf(" [-S offset] Write pool offset.\n");
154 printf(" [-T offset] Init pool offset.\n");
155 printf(" [-M size] Lookup pool size.\n");
156 printf(" [-N size] Write pool size.\n");
157 printf(" [-O size] Init pool size.\n");
158 printf(" [-V] Validate lookups of init values (use with filled init pool, same lookup range, with different write range).\n");
159 printf(" [-t] Do sanity test.\n");
160 printf(" [-B] Key bits for multithread test (default: 32).\n");
161 printf(" [-m factor] Key multiplication factor.\n");
162 printf(" [-l] Memory leak detection.\n");
163 printf(" [-L len] Range max len.\n");
167 enum urcu_ja_addremove
{
171 }; /* 1: add, -1 remove, 0: random */
173 static enum urcu_ja_addremove addremove
; /* 1: add, -1 remove, 0: random */
176 void test_ja_rw_sigusr1_handler(int signo
)
180 printf("Add/Remove: random.\n");
181 addremove
= AR_RANDOM
;
184 printf("Add/Remove: remove only.\n");
185 addremove
= AR_REMOVE
;
188 printf("Add/Remove: add only.\n");
195 void *test_ja_rw_thr_reader(void *_count
)
197 unsigned long long *count
= _count
;
198 struct cds_ja_range
*range
;
201 printf_verbose("thread_begin %s, tid %lu\n",
202 "reader", urcu_get_thread_id());
204 URCU_TLS(rand_lookup
) = (unsigned int) urcu_get_thread_id() ^ time(NULL
);
208 rcu_register_thread();
218 key
= (uint64_t) rand_r(&URCU_TLS(rand_lookup
));
219 key
+= (uint64_t) rand_r(&URCU_TLS(rand_lookup
)) << 32ULL;
220 key
= (key
% lookup_pool_size
) + lookup_pool_offset
;
223 range
= cds_ja_range_lookup(test_ja
, key
);
225 if (validate_lookup
) {
226 printf("[ERROR] Lookup cannot find initial node.\n");
229 URCU_TLS(lookup_fail
)++;
231 range
= cds_ja_range_lock(range
);
233 if (validate_lookup
) {
234 printf("[ERROR] Lookup cannot find initial node.\n");
238 URCU_TLS(lookup_ok
)++;
239 cds_ja_range_unlock(range
);
242 rcu_debug_yield_read();
243 if (caa_unlikely(rduration
))
244 loop_sleep(rduration
);
246 URCU_TLS(nr_reads
)++;
247 if (caa_unlikely(!test_duration_read()))
249 if (caa_unlikely((URCU_TLS(nr_reads
) & ((1 << 10) - 1)) == 0))
250 rcu_quiescent_state();
253 rcu_unregister_thread();
255 *count
= URCU_TLS(nr_reads
);
256 printf_verbose("thread_end %s, tid %lu\n",
257 "reader", urcu_get_thread_id());
258 printf_verbose("read tid : %lu, lookupfail %lu, lookupok %lu\n",
259 urcu_get_thread_id(), URCU_TLS(lookup_fail
),
260 URCU_TLS(lookup_ok
));
267 return ((unsigned int) rand_r(&URCU_TLS(rand_lookup
)) % 100) < add_ratio
;
271 void *test_ja_rw_thr_writer(void *_count
)
273 struct wr_count
*count
= _count
;
276 printf_verbose("thread_begin %s, tid %lu\n",
277 "writer", urcu_get_thread_id());
279 URCU_TLS(rand_lookup
) = (unsigned int) urcu_get_thread_id() ^ time(NULL
);
283 rcu_register_thread();
291 if ((addremove
== AR_ADD
)
292 || (addremove
== AR_RANDOM
&& is_add())) {
293 struct cds_ja_range
*range
;
294 uint64_t start
, end
, tmp
;
296 start
= (uint64_t) rand_r(&URCU_TLS(rand_lookup
));
297 start
+= (uint64_t) rand_r(&URCU_TLS(rand_lookup
)) << 32ULL;
298 start
= (start
% write_pool_size
) + write_pool_offset
;
300 end
= (uint64_t) rand_r(&URCU_TLS(rand_lookup
));
301 end
+= (uint64_t) rand_r(&URCU_TLS(rand_lookup
)) << 32ULL;
302 end
= (end
% write_pool_size
) + write_pool_offset
;
311 if (end
- start
> range_max_len
) {
312 end
= start
+ range_max_len
;
315 ret
= cds_ja_range_add(test_ja
, start
, end
, NULL
);
317 if (ret
== -EEXIST
) {
318 URCU_TLS(nr_addexist
)++;
327 struct cds_ja_range
*range
;
331 key
= (uint64_t) rand_r(&URCU_TLS(rand_lookup
));
332 key
+= (uint64_t) rand_r(&URCU_TLS(rand_lookup
)) << 32ULL;
333 key
= (key
% write_pool_size
) + write_pool_offset
;
338 range
= cds_ja_range_lookup(test_ja
, key
);
340 ret
= cds_ja_range_del(test_ja
, range
);
344 URCU_TLS(nr_delnoent
)++;
347 URCU_TLS(nr_delnoent
)++;
352 URCU_TLS(nr_writes
)++;
353 if (caa_unlikely(!test_duration_write()))
355 if (caa_unlikely(wdelay
))
357 if (caa_unlikely((URCU_TLS(nr_writes
) & ((1 << 10) - 1)) == 0))
358 rcu_quiescent_state();
361 rcu_unregister_thread();
363 printf_verbose("thread_end %s, tid %lu\n",
364 "writer", urcu_get_thread_id());
365 printf_verbose("info tid %lu: nr_add %lu, nr_addexist %lu, nr_del %lu, "
366 "nr_delnoent %lu\n", urcu_get_thread_id(),
368 URCU_TLS(nr_addexist
), URCU_TLS(nr_del
),
369 URCU_TLS(nr_delnoent
));
370 count
->update_ops
= URCU_TLS(nr_writes
);
371 count
->add
= URCU_TLS(nr_add
);
372 count
->add_exist
= URCU_TLS(nr_addexist
);
373 count
->remove
= URCU_TLS(nr_del
);
378 int do_mt_populate_ja(void)
386 printf("Starting rw test\n");
388 for (iter
= init_pool_offset
; iter
< init_pool_offset
+ init_pool_size
; iter
++) {
389 struct cds_ja_range
*range
;
392 /* note: only inserting ulong keys */
393 key
= (unsigned long) iter
;
396 ret
= cds_ja_range_add(test_ja
, key
, key
, NULL
);
398 URCU_TLS(nr_writes
)++;
400 /* Hash table resize only occurs in call_rcu thread */
402 rcu_quiescent_state();
404 fprintf(stderr
, "Error (%d) adding range %" PRIu64
"\n",
415 pthread_t
*tid_reader
, *tid_writer
;
418 unsigned long long *count_reader
;
419 struct wr_count
*count_writer
;
420 unsigned long long tot_reads
= 0, tot_writes
= 0,
421 tot_add
= 0, tot_add_exist
= 0, tot_remove
= 0;
424 tid_reader
= malloc(sizeof(*tid_reader
) * nr_readers
);
425 tid_writer
= malloc(sizeof(*tid_writer
) * nr_writers
);
426 count_reader
= malloc(sizeof(*count_reader
) * nr_readers
);
427 count_writer
= malloc(sizeof(*count_writer
) * nr_writers
);
429 printf("Allocating %u-bit Judy Array for ranges\n",
431 test_ja
= cds_ja_range_new(key_bits
);
433 printf("Error allocating judy array.\n");
442 for (i
= 0; i
< nr_readers
; i
++) {
443 err
= pthread_create(&tid_reader
[i
],
444 NULL
, test_ja_rw_thr_reader
,
449 for (i
= 0; i
< nr_writers
; i
++) {
450 err
= pthread_create(&tid_writer
[i
],
451 NULL
, test_ja_rw_thr_writer
,
461 rcu_thread_offline_qsbr();
465 remain
= sleep(remain
);
466 } while (remain
> 0);
470 for (i
= 0; i
< nr_readers
; i
++) {
471 err
= pthread_join(tid_reader
[i
], &tret
);
474 tot_reads
+= count_reader
[i
];
476 for (i
= 0; i
< nr_writers
; i
++) {
477 err
= pthread_join(tid_writer
[i
], &tret
);
480 tot_writes
+= count_writer
[i
].update_ops
;
481 tot_add
+= count_writer
[i
].add
;
482 tot_add_exist
+= count_writer
[i
].add_exist
;
483 tot_remove
+= count_writer
[i
].remove
;
485 rcu_thread_online_qsbr();
487 ret
= cds_ja_range_validate(test_ja
);
490 ret
= cds_ja_range_destroy(test_ja
, NULL
);
492 fprintf(stderr
, "Error destroying judy array\n");
505 int main(int argc
, char **argv
)
507 int i
, j
, a
, ret
, err
;
509 struct sigaction act
;
512 show_usage(argc
, argv
);
516 err
= sscanf(argv
[1], "%u", &nr_readers
);
518 show_usage(argc
, argv
);
522 err
= sscanf(argv
[2], "%u", &nr_writers
);
524 show_usage(argc
, argv
);
528 err
= sscanf(argv
[3], "%lu", &duration
);
530 show_usage(argc
, argv
);
534 for (i
= 4; i
< argc
; i
++) {
535 if (argv
[i
][0] != '-')
537 switch (argv
[i
][1]) {
540 yield_active
|= YIELD_READ
;
543 yield_active
|= YIELD_WRITE
;
548 show_usage(argc
, argv
);
552 cpu_affinities
[next_aff
++] = a
;
554 printf_verbose("Adding CPU %d affinity\n", a
);
558 show_usage(argc
, argv
);
561 rduration
= atol(argv
[++i
]);
565 show_usage(argc
, argv
);
568 wdelay
= atol(argv
[++i
]);
574 add_ratio
= atoi(argv
[++i
]);
580 lookup_pool_offset
= atol(argv
[++i
]);
583 write_pool_offset
= atol(argv
[++i
]);
586 init_pool_offset
= atol(argv
[++i
]);
589 lookup_pool_size
= atol(argv
[++i
]);
592 write_pool_size
= atol(argv
[++i
]);
595 init_pool_size
= atol(argv
[++i
]);
604 key_bits
= atol(argv
[++i
]);
607 key_mul
= atoll(argv
[++i
]);
619 range_max_len
= atol(argv
[++i
]);
624 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
625 duration
, nr_readers
, nr_writers
);
626 printf_verbose("Writer delay : %lu loops.\n", wdelay
);
627 printf_verbose("Reader duration : %lu loops.\n", rduration
);
628 printf_verbose("Add ratio: %u%%.\n", add_ratio
);
629 printf_verbose("Mode:%s%s.\n",
631 add_unique
? " uniquify" : ( add_replace
? " replace" : " insert"));
632 printf_verbose("Key multiplication factor: %" PRIu64
".\n", key_mul
);
633 printf_verbose("Init pool size offset %lu size %lu.\n",
634 init_pool_offset
, init_pool_size
);
635 printf_verbose("Lookup pool size offset %lu size %lu.\n",
636 lookup_pool_offset
, lookup_pool_size
);
637 printf_verbose("Update pool size offset %lu size %lu.\n",
638 write_pool_offset
, write_pool_size
);
639 printf_verbose("Range max len: %u.\n",
642 printf_verbose("Validating lookups.\n");
644 printf_verbose("Memory leak dection activated.\n");
645 printf_verbose("thread %-6s, tid %lu\n",
646 "main", urcu_get_thread_id());
648 memset(&act
, 0, sizeof(act
));
649 ret
= sigemptyset(&act
.sa_mask
);
651 perror("sigemptyset");
654 act
.sa_handler
= test_ja_rw_sigusr1_handler
;
655 act
.sa_flags
= SA_RESTART
;
656 ret
= sigaction(SIGUSR1
, &act
, NULL
);
662 err
= create_all_cpu_call_rcu_data(0);
664 printf("Per-CPU call_rcu() worker threads unavailable. Using default global worker thread.\n");
667 rcu_register_thread();
671 /* Wait for in-flight call_rcu free to complete for leak detection */
674 rcu_unregister_thread();
675 free_all_cpu_call_rcu_data();
678 printf("Test ended with error: %d\n", ret
);