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"
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
);
36 struct cds_ja
*test_ja
;
38 volatile int test_go
, test_stop
;
42 unsigned long duration
;
44 /* read-side C.S. duration, in loops */
45 unsigned long rduration
;
47 unsigned long init_populate
;
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
;
56 unsigned int key_bits
= 32;
62 unsigned int cpu_affinities
[NR_CPUS
];
63 unsigned int next_aff
= 0;
66 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
68 DEFINE_URCU_TLS(unsigned long long, nr_writes
);
69 DEFINE_URCU_TLS(unsigned long long, nr_reads
);
71 unsigned int nr_readers
;
72 unsigned int nr_writers
;
74 static unsigned int add_ratio
= 50, range_max_len
= 0;
75 static uint64_t key_mul
= 1ULL;
77 static int add_unique
, add_replace
;
79 static pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
81 static int leak_detection
;
82 static unsigned long test_nodes_allocated
, test_nodes_freed
;
84 void set_affinity(void)
93 #if HAVE_SCHED_SETAFFINITY
94 ret
= pthread_mutex_lock(&affinity_mutex
);
96 perror("Error in pthread mutex lock");
99 cpu
= cpu_affinities
[next_aff
++];
100 ret
= pthread_mutex_unlock(&affinity_mutex
);
102 perror("Error in pthread mutex unlock");
107 #if SCHED_SETAFFINITY_ARGS == 2
108 sched_setaffinity(0, &mask
);
110 sched_setaffinity(0, sizeof(mask
), &mask
);
112 #endif /* HAVE_SCHED_SETAFFINITY */
115 void rcu_copy_mutex_lock(void)
118 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
120 perror("Error in pthread mutex lock");
125 void rcu_copy_mutex_unlock(void)
129 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
131 perror("Error in pthread mutex unlock");
136 void show_usage(int argc
, char **argv
)
138 printf("Usage : %s nr_readers nr_writers duration (s)\n", argv
[0]);
140 printf(" [-r] [-w] (yield reader and/or writer)\n");
142 printf(" [-d delay] (writer period (us))\n");
143 printf(" [-c duration] (reader C.S. duration (in loops))\n");
144 printf(" [-v] (verbose output)\n");
145 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
146 printf(" [-u] Add unique keys.\n");
147 printf(" [-s] Replace existing keys.\n");
148 printf(" [not -u nor -s] Add entries (supports redundant keys).\n");
149 printf(" [-r ratio] Add ratio (in %% of add+removal).\n");
150 printf(" [-k] Populate init nodes.\n");
151 printf(" [-R offset] Lookup pool offset.\n");
152 printf(" [-S offset] Write pool offset.\n");
153 printf(" [-T offset] Init pool offset.\n");
154 printf(" [-M size] Lookup pool size.\n");
155 printf(" [-N size] Write pool size.\n");
156 printf(" [-O size] Init pool size.\n");
157 printf(" [-V] Validate lookups of init values (use with filled init pool, same lookup range, with different write range).\n");
158 printf(" [-t] Do sanity test.\n");
159 printf(" [-B] Key bits for multithread test (default: 32).\n");
160 printf(" [-m factor] Key multiplication factor.\n");
161 printf(" [-l] Memory leak detection.\n");
162 printf(" [-L len] Range max len.\n");
166 enum urcu_ja_addremove
{
170 }; /* 1: add, -1 remove, 0: random */
172 static enum urcu_ja_addremove addremove
; /* 1: add, -1 remove, 0: random */
175 void test_ja_rw_sigusr1_handler(int signo
)
179 printf("Add/Remove: random.\n");
180 addremove
= AR_RANDOM
;
183 printf("Add/Remove: remove only.\n");
184 addremove
= AR_REMOVE
;
187 printf("Add/Remove: add only.\n");
194 void *test_ja_rw_thr_reader(void *_count
)
196 unsigned long long *count
= _count
;
197 struct cds_ja_range
*range
;
200 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
201 "reader", pthread_self(), (unsigned long) gettid());
203 URCU_TLS(rand_lookup
) = (unsigned int) gettid() ^ time(NULL
);
207 rcu_register_thread();
217 key
= (uint64_t) rand_r(&URCU_TLS(rand_lookup
));
218 key
+= (uint64_t) rand_r(&URCU_TLS(rand_lookup
)) << 32ULL;
219 key
= (key
% lookup_pool_size
) + lookup_pool_offset
;
222 range
= cds_ja_range_lookup(test_ja
, key
);
224 if (validate_lookup
) {
225 printf("[ERROR] Lookup cannot find initial node.\n");
228 URCU_TLS(lookup_fail
)++;
230 range
= cds_ja_range_lock(range
);
232 if (validate_lookup
) {
233 printf("[ERROR] Lookup cannot find initial node.\n");
237 URCU_TLS(lookup_ok
)++;
238 cds_ja_range_unlock(range
);
241 rcu_debug_yield_read();
242 if (caa_unlikely(rduration
))
243 loop_sleep(rduration
);
245 URCU_TLS(nr_reads
)++;
246 if (caa_unlikely(!test_duration_read()))
248 if (caa_unlikely((URCU_TLS(nr_reads
) & ((1 << 10) - 1)) == 0))
249 rcu_quiescent_state();
252 rcu_unregister_thread();
254 *count
= URCU_TLS(nr_reads
);
255 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
256 "reader", pthread_self(), (unsigned long) gettid());
257 printf_verbose("readid : %lx, lookupfail %lu, lookupok %lu\n",
258 pthread_self(), URCU_TLS(lookup_fail
),
259 URCU_TLS(lookup_ok
));
266 return ((unsigned int) rand_r(&URCU_TLS(rand_lookup
)) % 100) < add_ratio
;
270 void *test_ja_rw_thr_writer(void *_count
)
272 struct wr_count
*count
= _count
;
275 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
276 "writer", pthread_self(), (unsigned long) gettid());
278 URCU_TLS(rand_lookup
) = (unsigned int) gettid() ^ time(NULL
);
282 rcu_register_thread();
290 if ((addremove
== AR_ADD
)
291 || (addremove
== AR_RANDOM
&& is_add())) {
292 struct cds_ja_range
*range
;
293 uint64_t start
, end
, tmp
;
295 start
= (uint64_t) rand_r(&URCU_TLS(rand_lookup
));
296 start
+= (uint64_t) rand_r(&URCU_TLS(rand_lookup
)) << 32ULL;
297 start
= (start
% write_pool_size
) + write_pool_offset
;
299 end
= (uint64_t) rand_r(&URCU_TLS(rand_lookup
));
300 end
+= (uint64_t) rand_r(&URCU_TLS(rand_lookup
)) << 32ULL;
301 end
= (end
% write_pool_size
) + write_pool_offset
;
310 if (end
- start
> range_max_len
) {
311 end
= start
+ range_max_len
;
314 ret
= cds_ja_range_add(test_ja
, start
, end
, NULL
);
316 if (ret
== -EEXIST
) {
317 URCU_TLS(nr_addexist
)++;
326 struct cds_ja_range
*range
;
330 key
= (uint64_t) rand_r(&URCU_TLS(rand_lookup
));
331 key
+= (uint64_t) rand_r(&URCU_TLS(rand_lookup
)) << 32ULL;
332 key
= (key
% write_pool_size
) + write_pool_offset
;
337 range
= cds_ja_range_lookup(test_ja
, key
);
339 ret
= cds_ja_range_del(test_ja
, range
);
343 URCU_TLS(nr_delnoent
)++;
346 URCU_TLS(nr_delnoent
)++;
351 URCU_TLS(nr_writes
)++;
352 if (caa_unlikely(!test_duration_write()))
354 if (caa_unlikely(wdelay
))
356 if (caa_unlikely((URCU_TLS(nr_writes
) & ((1 << 10) - 1)) == 0))
357 rcu_quiescent_state();
360 rcu_unregister_thread();
362 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
363 "writer", pthread_self(), (unsigned long) gettid());
364 printf_verbose("info id %lx: nr_add %lu, nr_addexist %lu, nr_del %lu, "
365 "nr_delnoent %lu\n", pthread_self(), URCU_TLS(nr_add
),
366 URCU_TLS(nr_addexist
), URCU_TLS(nr_del
),
367 URCU_TLS(nr_delnoent
));
368 count
->update_ops
= URCU_TLS(nr_writes
);
369 count
->add
= URCU_TLS(nr_add
);
370 count
->add_exist
= URCU_TLS(nr_addexist
);
371 count
->remove
= URCU_TLS(nr_del
);
376 int do_mt_populate_ja(void)
384 printf("Starting rw test\n");
386 for (iter
= init_pool_offset
; iter
< init_pool_offset
+ init_pool_size
; iter
++) {
387 struct cds_ja_range
*range
;
390 /* note: only inserting ulong keys */
391 key
= (unsigned long) iter
;
394 ret
= cds_ja_range_add(test_ja
, key
, key
, NULL
);
396 URCU_TLS(nr_writes
)++;
398 /* Hash table resize only occurs in call_rcu thread */
400 rcu_quiescent_state();
402 fprintf(stderr
, "Error (%d) adding range %" PRIu64
"\n",
413 pthread_t
*tid_reader
, *tid_writer
;
416 unsigned long long *count_reader
;
417 struct wr_count
*count_writer
;
418 unsigned long long tot_reads
= 0, tot_writes
= 0,
419 tot_add
= 0, tot_add_exist
= 0, tot_remove
= 0;
422 tid_reader
= malloc(sizeof(*tid_reader
) * nr_readers
);
423 tid_writer
= malloc(sizeof(*tid_writer
) * nr_writers
);
424 count_reader
= malloc(sizeof(*count_reader
) * nr_readers
);
425 count_writer
= malloc(sizeof(*count_writer
) * nr_writers
);
427 printf("Allocating %u-bit Judy Array for ranges\n",
429 test_ja
= cds_ja_range_new(key_bits
);
431 printf("Error allocating judy array.\n");
440 for (i
= 0; i
< nr_readers
; i
++) {
441 err
= pthread_create(&tid_reader
[i
],
442 NULL
, test_ja_rw_thr_reader
,
447 for (i
= 0; i
< nr_writers
; i
++) {
448 err
= pthread_create(&tid_writer
[i
],
449 NULL
, test_ja_rw_thr_writer
,
459 rcu_thread_offline_qsbr();
463 remain
= sleep(remain
);
464 } while (remain
> 0);
468 for (i
= 0; i
< nr_readers
; i
++) {
469 err
= pthread_join(tid_reader
[i
], &tret
);
472 tot_reads
+= count_reader
[i
];
474 for (i
= 0; i
< nr_writers
; i
++) {
475 err
= pthread_join(tid_writer
[i
], &tret
);
478 tot_writes
+= count_writer
[i
].update_ops
;
479 tot_add
+= count_writer
[i
].add
;
480 tot_add_exist
+= count_writer
[i
].add_exist
;
481 tot_remove
+= count_writer
[i
].remove
;
483 rcu_thread_online_qsbr();
485 ret
= cds_ja_range_validate(test_ja
);
488 ret
= cds_ja_range_destroy(test_ja
, NULL
);
490 fprintf(stderr
, "Error destroying judy array\n");
503 int main(int argc
, char **argv
)
505 int i
, j
, a
, ret
, err
;
507 struct sigaction act
;
510 show_usage(argc
, argv
);
514 err
= sscanf(argv
[1], "%u", &nr_readers
);
516 show_usage(argc
, argv
);
520 err
= sscanf(argv
[2], "%u", &nr_writers
);
522 show_usage(argc
, argv
);
526 err
= sscanf(argv
[3], "%lu", &duration
);
528 show_usage(argc
, argv
);
532 for (i
= 4; i
< argc
; i
++) {
533 if (argv
[i
][0] != '-')
535 switch (argv
[i
][1]) {
538 yield_active
|= YIELD_READ
;
541 yield_active
|= YIELD_WRITE
;
546 show_usage(argc
, argv
);
550 cpu_affinities
[next_aff
++] = a
;
552 printf_verbose("Adding CPU %d affinity\n", a
);
556 show_usage(argc
, argv
);
559 rduration
= atol(argv
[++i
]);
563 show_usage(argc
, argv
);
566 wdelay
= atol(argv
[++i
]);
572 add_ratio
= atoi(argv
[++i
]);
578 lookup_pool_offset
= atol(argv
[++i
]);
581 write_pool_offset
= atol(argv
[++i
]);
584 init_pool_offset
= atol(argv
[++i
]);
587 lookup_pool_size
= atol(argv
[++i
]);
590 write_pool_size
= atol(argv
[++i
]);
593 init_pool_size
= atol(argv
[++i
]);
602 key_bits
= atol(argv
[++i
]);
605 key_mul
= atoll(argv
[++i
]);
617 range_max_len
= atol(argv
[++i
]);
622 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
623 duration
, nr_readers
, nr_writers
);
624 printf_verbose("Writer delay : %lu loops.\n", wdelay
);
625 printf_verbose("Reader duration : %lu loops.\n", rduration
);
626 printf_verbose("Add ratio: %u%%.\n", add_ratio
);
627 printf_verbose("Mode:%s%s.\n",
629 add_unique
? " uniquify" : ( add_replace
? " replace" : " insert"));
630 printf_verbose("Key multiplication factor: %" PRIu64
".\n", key_mul
);
631 printf_verbose("Init pool size offset %lu size %lu.\n",
632 init_pool_offset
, init_pool_size
);
633 printf_verbose("Lookup pool size offset %lu size %lu.\n",
634 lookup_pool_offset
, lookup_pool_size
);
635 printf_verbose("Update pool size offset %lu size %lu.\n",
636 write_pool_offset
, write_pool_size
);
637 printf_verbose("Range max len: %lu.\n",
640 printf_verbose("Validating lookups.\n");
642 printf_verbose("Memory leak dection activated.\n");
643 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
644 "main", pthread_self(), (unsigned long)gettid());
646 memset(&act
, 0, sizeof(act
));
647 ret
= sigemptyset(&act
.sa_mask
);
649 perror("sigemptyset");
652 act
.sa_handler
= test_ja_rw_sigusr1_handler
;
653 act
.sa_flags
= SA_RESTART
;
654 ret
= sigaction(SIGUSR1
, &act
, NULL
);
660 err
= create_all_cpu_call_rcu_data(0);
662 printf("Per-CPU call_rcu() worker threads unavailable. Using default global worker thread.\n");
665 rcu_register_thread();
669 /* Wait for in-flight call_rcu free to complete for leak detection */
672 rcu_unregister_thread();
673 free_all_cpu_call_rcu_data();
676 printf("Test ended with error: %d\n", ret
);