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.h"
27 DEFINE_URCU_TLS(unsigned int, rand_lookup
);
28 DEFINE_URCU_TLS(unsigned long, nr_add
);
29 DEFINE_URCU_TLS(unsigned long, nr_addexist
);
30 DEFINE_URCU_TLS(unsigned long, nr_del
);
31 DEFINE_URCU_TLS(unsigned long, nr_delnoent
);
32 DEFINE_URCU_TLS(unsigned long, lookup_fail
);
33 DEFINE_URCU_TLS(unsigned long, lookup_ok
);
35 struct cds_ja
*test_ja
;
37 volatile int test_go
, test_stop
;
41 unsigned long duration
;
43 /* read-side C.S. duration, in loops */
44 unsigned long rduration
;
46 unsigned long init_populate
;
49 unsigned long init_pool_offset
, lookup_pool_offset
, write_pool_offset
;
50 unsigned long init_pool_size
= DEFAULT_RAND_POOL
,
51 lookup_pool_size
= DEFAULT_RAND_POOL
,
52 write_pool_size
= DEFAULT_RAND_POOL
;
59 unsigned int cpu_affinities
[NR_CPUS
];
60 unsigned int next_aff
= 0;
63 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
65 DEFINE_URCU_TLS(unsigned long long, nr_writes
);
66 DEFINE_URCU_TLS(unsigned long long, nr_reads
);
68 unsigned int nr_readers
;
69 unsigned int nr_writers
;
71 static pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
73 void set_affinity(void)
82 #if HAVE_SCHED_SETAFFINITY
83 ret
= pthread_mutex_lock(&affinity_mutex
);
85 perror("Error in pthread mutex lock");
88 cpu
= cpu_affinities
[next_aff
++];
89 ret
= pthread_mutex_unlock(&affinity_mutex
);
91 perror("Error in pthread mutex unlock");
96 #if SCHED_SETAFFINITY_ARGS == 2
97 sched_setaffinity(0, &mask
);
99 sched_setaffinity(0, sizeof(mask
), &mask
);
101 #endif /* HAVE_SCHED_SETAFFINITY */
104 void rcu_copy_mutex_lock(void)
107 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
109 perror("Error in pthread mutex lock");
114 void rcu_copy_mutex_unlock(void)
118 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
120 perror("Error in pthread mutex unlock");
126 void free_node_cb(struct rcu_head
*head
)
128 struct ja_test_node
*node
=
129 caa_container_of(head
, struct ja_test_node
, head
);
134 void test_delete_all_nodes(struct cds_lfht
*ht
)
136 struct cds_lfht_iter iter
;
137 struct lfht_test_node
*node
;
138 unsigned long count
= 0;
140 cds_lfht_for_each_entry(ht
, &iter
, node
, node
) {
143 ret
= cds_lfht_del(test_ht
, cds_lfht_iter_get_node(&iter
));
145 call_rcu(&node
->head
, free_node_cb
);
148 printf("deleted %lu nodes.\n", count
);
152 void show_usage(int argc
, char **argv
)
154 printf("Usage : %s nr_readers nr_writers duration (s)\n", argv
[0]);
156 printf(" [-r] [-w] (yield reader and/or writer)\n");
158 printf(" [-d delay] (writer period (us))\n");
159 printf(" [-c duration] (reader C.S. duration (in loops))\n");
160 printf(" [-v] (verbose output)\n");
161 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
162 printf(" [not -u nor -s] Add entries (supports redundant keys).\n");
163 printf(" [-i] Add only (no removal).\n");
164 printf(" [-k nr_nodes] Number of nodes to insert initially.\n");
165 printf(" [-R offset] Lookup pool offset.\n");
166 printf(" [-S offset] Write pool offset.\n");
167 printf(" [-T offset] Init pool offset.\n");
168 printf(" [-M size] Lookup pool size.\n");
169 printf(" [-N size] Write pool size.\n");
170 printf(" [-O size] Init pool size.\n");
171 printf(" [-V] Validate lookups of init values (use with filled init pool, same lookup range, with different write range).\n");
175 int main(int argc
, char **argv
)
178 pthread_t
*tid_reader
, *tid_writer
;
180 unsigned long long *count_reader
;
181 struct wr_count
*count_writer
;
182 unsigned long long tot_reads
= 0, tot_writes
= 0,
183 tot_add
= 0, tot_add_exist
= 0, tot_remove
= 0;
189 show_usage(argc
, argv
);
193 err
= sscanf(argv
[1], "%u", &nr_readers
);
195 show_usage(argc
, argv
);
199 err
= sscanf(argv
[2], "%u", &nr_writers
);
201 show_usage(argc
, argv
);
205 err
= sscanf(argv
[3], "%lu", &duration
);
207 show_usage(argc
, argv
);
211 for (i
= 4; i
< argc
; i
++) {
212 if (argv
[i
][0] != '-')
214 switch (argv
[i
][1]) {
217 yield_active
|= YIELD_READ
;
220 yield_active
|= YIELD_WRITE
;
225 show_usage(argc
, argv
);
229 cpu_affinities
[next_aff
++] = a
;
231 printf_verbose("Adding CPU %d affinity\n", a
);
235 show_usage(argc
, argv
);
238 rduration
= atol(argv
[++i
]);
242 show_usage(argc
, argv
);
245 wdelay
= atol(argv
[++i
]);
254 init_populate
= atol(argv
[++i
]);
257 lookup_pool_offset
= atol(argv
[++i
]);
260 write_pool_offset
= atol(argv
[++i
]);
263 init_pool_offset
= atol(argv
[++i
]);
266 lookup_pool_size
= atol(argv
[++i
]);
269 write_pool_size
= atol(argv
[++i
]);
272 init_pool_size
= atol(argv
[++i
]);
280 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
281 duration
, nr_readers
, nr_writers
);
282 printf_verbose("Writer delay : %lu loops.\n", wdelay
);
283 printf_verbose("Reader duration : %lu loops.\n", rduration
);
284 printf_verbose("Mode:%s.\n",
285 add_only
? " add only" : " add/delete");
286 printf_verbose("Init pool size offset %lu size %lu.\n",
287 init_pool_offset
, init_pool_size
);
288 printf_verbose("Lookup pool size offset %lu size %lu.\n",
289 lookup_pool_offset
, lookup_pool_size
);
290 printf_verbose("Update pool size offset %lu size %lu.\n",
291 write_pool_offset
, write_pool_size
);
292 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
293 "main", pthread_self(), (unsigned long)gettid());
295 tid_reader
= malloc(sizeof(*tid_reader
) * nr_readers
);
296 tid_writer
= malloc(sizeof(*tid_writer
) * nr_writers
);
297 count_reader
= malloc(sizeof(*count_reader
) * nr_readers
);
298 count_writer
= malloc(sizeof(*count_writer
) * nr_writers
);
300 err
= create_all_cpu_call_rcu_data(0);
302 printf("Per-CPU call_rcu() worker threads unavailable. Using default global worker thread.\n");
305 /* Test with 8-bit key */
306 test_ja
= cds_ja_new(8);
308 printf("Error allocating judy array.\n");
313 printf("Test #1: add keys (8-bit).\n");
314 for (key
= 0; key
< 200; key
++) {
315 struct ja_test_node
*node
=
316 calloc(sizeof(*node
), 1);
318 ja_test_node_init(node
, key
);
320 ret
= cds_ja_add(test_ja
, key
, &node
->node
);
323 fprintf(stderr
, "Error (%d) adding node %" PRIu64
"\n",
330 printf("Test #2: successful key lookup (8-bit).\n");
331 for (key
= 0; key
< 200; key
++) {
332 struct cds_hlist_head
*head
;
335 head
= cds_ja_lookup(test_ja
, key
);
337 fprintf(stderr
, "Error lookup node %" PRIu64
"\n", key
);
343 printf("Test #3: unsuccessful key lookup (8-bit).\n");
344 for (key
= 200; key
< 240; key
++) {
345 struct cds_hlist_head
*head
;
348 head
= cds_ja_lookup(test_ja
, key
);
351 "Error unexpected lookup node %" PRIu64
"\n",
359 ret
= cds_ja_destroy(test_ja
);
361 fprintf(stderr
, "Error destroying judy array\n");
364 printf("Test end.\n");
369 * Hash Population needs to be seen as a RCU reader
370 * thread from the point of view of resize.
372 rcu_register_thread();
373 ret
= (get_populate_hash_cb())();
376 rcu_thread_offline();
380 for (i
= 0; i
< nr_readers
; i
++) {
381 err
= pthread_create(&tid_reader
[i
],
382 NULL
, get_thr_reader_cb(),
387 for (i
= 0; i
< nr_writers
; i
++) {
388 err
= pthread_create(&tid_writer
[i
],
389 NULL
, get_thr_writer_cb(),
401 remain
= sleep(remain
);
402 } while (remain
> 0);
406 for (i
= 0; i
< nr_readers
; i
++) {
407 err
= pthread_join(tid_reader
[i
], &tret
);
410 tot_reads
+= count_reader
[i
];
412 for (i
= 0; i
< nr_writers
; i
++) {
413 err
= pthread_join(tid_writer
[i
], &tret
);
416 tot_writes
+= count_writer
[i
].update_ops
;
417 tot_add
+= count_writer
[i
].add
;
418 tot_add_exist
+= count_writer
[i
].add_exist
;
419 tot_remove
+= count_writer
[i
].remove
;
422 /* teardown counter thread */
423 act
.sa_handler
= SIG_IGN
;
424 act
.sa_flags
= SA_RESTART
;
425 ret
= sigaction(SIGUSR2
, &act
, NULL
);
431 char msg
[1] = { 0x42 };
435 ret
= write(count_pipe
[1], msg
, 1); /* wakeup thread */
436 } while (ret
== -1L && errno
== EINTR
);
442 printf("Counting nodes... ");
443 cds_lfht_count_nodes(test_ht
, &approx_before
, &count
, &approx_after
);
445 test_delete_all_nodes(test_ht
);
447 rcu_thread_offline();
449 printf("Approximation before node accounting: %ld nodes.\n",
451 printf("Nodes deleted from hash table before destroy: "
454 printf("Approximation after node accounting: %ld nodes.\n",
457 ret
= cds_lfht_destroy(test_ht
, NULL
);
459 printf_verbose("final delete aborted\n");
461 printf_verbose("final delete success\n");
462 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads
,
464 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu "
466 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu "
467 "nr_add %12llu nr_add_fail %12llu nr_remove %12llu nr_leaked %12lld\n",
468 argv
[0], duration
, nr_readers
, rduration
,
469 nr_writers
, wdelay
, tot_reads
, tot_writes
,
470 tot_reads
+ tot_writes
, tot_add
, tot_add_exist
, tot_remove
,
471 (long long) tot_add
+ init_populate
- tot_remove
- count
);
472 rcu_unregister_thread();
473 free_all_cpu_call_rcu_data();