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"
26 DEFINE_URCU_TLS(unsigned int, rand_lookup
);
27 DEFINE_URCU_TLS(unsigned long, nr_add
);
28 DEFINE_URCU_TLS(unsigned long, nr_addexist
);
29 DEFINE_URCU_TLS(unsigned long, nr_del
);
30 DEFINE_URCU_TLS(unsigned long, nr_delnoent
);
31 DEFINE_URCU_TLS(unsigned long, lookup_fail
);
32 DEFINE_URCU_TLS(unsigned long, lookup_ok
);
34 struct cds_ja
*test_ja
;
36 volatile int test_go
, test_stop
;
40 unsigned long duration
;
42 /* read-side C.S. duration, in loops */
43 unsigned long rduration
;
45 unsigned long init_populate
;
48 unsigned long init_pool_offset
, lookup_pool_offset
, write_pool_offset
;
49 unsigned long init_pool_size
= DEFAULT_RAND_POOL
,
50 lookup_pool_size
= DEFAULT_RAND_POOL
,
51 write_pool_size
= DEFAULT_RAND_POOL
;
58 unsigned int cpu_affinities
[NR_CPUS
];
59 unsigned int next_aff
= 0;
62 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
64 DEFINE_URCU_TLS(unsigned long long, nr_writes
);
65 DEFINE_URCU_TLS(unsigned long long, nr_reads
);
67 unsigned int nr_readers
;
68 unsigned int nr_writers
;
70 static pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
72 void set_affinity(void)
81 #if HAVE_SCHED_SETAFFINITY
82 ret
= pthread_mutex_lock(&affinity_mutex
);
84 perror("Error in pthread mutex lock");
87 cpu
= cpu_affinities
[next_aff
++];
88 ret
= pthread_mutex_unlock(&affinity_mutex
);
90 perror("Error in pthread mutex unlock");
95 #if SCHED_SETAFFINITY_ARGS == 2
96 sched_setaffinity(0, &mask
);
98 sched_setaffinity(0, sizeof(mask
), &mask
);
100 #endif /* HAVE_SCHED_SETAFFINITY */
103 void rcu_copy_mutex_lock(void)
106 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
108 perror("Error in pthread mutex lock");
113 void rcu_copy_mutex_unlock(void)
117 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
119 perror("Error in pthread mutex unlock");
125 void free_node_cb(struct rcu_head
*head
)
127 struct ja_test_node
*node
=
128 caa_container_of(head
, struct ja_test_node
, head
);
133 void test_delete_all_nodes(struct cds_lfht
*ht
)
135 struct cds_lfht_iter iter
;
136 struct lfht_test_node
*node
;
137 unsigned long count
= 0;
139 cds_lfht_for_each_entry(ht
, &iter
, node
, node
) {
142 ret
= cds_lfht_del(test_ht
, cds_lfht_iter_get_node(&iter
));
144 call_rcu(&node
->head
, free_node_cb
);
147 printf("deleted %lu nodes.\n", count
);
151 void show_usage(int argc
, char **argv
)
153 printf("Usage : %s nr_readers nr_writers duration (s)\n", argv
[0]);
155 printf(" [-r] [-w] (yield reader and/or writer)\n");
157 printf(" [-d delay] (writer period (us))\n");
158 printf(" [-c duration] (reader C.S. duration (in loops))\n");
159 printf(" [-v] (verbose output)\n");
160 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
161 printf(" [not -u nor -s] Add entries (supports redundant keys).\n");
162 printf(" [-i] Add only (no removal).\n");
163 printf(" [-k nr_nodes] Number of nodes to insert initially.\n");
164 printf(" [-R offset] Lookup pool offset.\n");
165 printf(" [-S offset] Write pool offset.\n");
166 printf(" [-T offset] Init pool offset.\n");
167 printf(" [-M size] Lookup pool size.\n");
168 printf(" [-N size] Write pool size.\n");
169 printf(" [-O size] Init pool size.\n");
170 printf(" [-V] Validate lookups of init values (use with filled init pool, same lookup range, with different write range).\n");
174 int main(int argc
, char **argv
)
177 pthread_t
*tid_reader
, *tid_writer
;
179 unsigned long long *count_reader
;
180 struct wr_count
*count_writer
;
181 unsigned long long tot_reads
= 0, tot_writes
= 0,
182 tot_add
= 0, tot_add_exist
= 0, tot_remove
= 0;
187 show_usage(argc
, argv
);
191 err
= sscanf(argv
[1], "%u", &nr_readers
);
193 show_usage(argc
, argv
);
197 err
= sscanf(argv
[2], "%u", &nr_writers
);
199 show_usage(argc
, argv
);
203 err
= sscanf(argv
[3], "%lu", &duration
);
205 show_usage(argc
, argv
);
209 for (i
= 4; i
< argc
; i
++) {
210 if (argv
[i
][0] != '-')
212 switch (argv
[i
][1]) {
215 yield_active
|= YIELD_READ
;
218 yield_active
|= YIELD_WRITE
;
223 show_usage(argc
, argv
);
227 cpu_affinities
[next_aff
++] = a
;
229 printf_verbose("Adding CPU %d affinity\n", a
);
233 show_usage(argc
, argv
);
236 rduration
= atol(argv
[++i
]);
240 show_usage(argc
, argv
);
243 wdelay
= atol(argv
[++i
]);
252 init_populate
= atol(argv
[++i
]);
255 lookup_pool_offset
= atol(argv
[++i
]);
258 write_pool_offset
= atol(argv
[++i
]);
261 init_pool_offset
= atol(argv
[++i
]);
264 lookup_pool_size
= atol(argv
[++i
]);
267 write_pool_size
= atol(argv
[++i
]);
270 init_pool_size
= atol(argv
[++i
]);
278 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
279 duration
, nr_readers
, nr_writers
);
280 printf_verbose("Writer delay : %lu loops.\n", wdelay
);
281 printf_verbose("Reader duration : %lu loops.\n", rduration
);
282 printf_verbose("Mode:%s.\n",
283 add_only
? " add only" : " add/delete");
284 printf_verbose("Init pool size offset %lu size %lu.\n",
285 init_pool_offset
, init_pool_size
);
286 printf_verbose("Lookup pool size offset %lu size %lu.\n",
287 lookup_pool_offset
, lookup_pool_size
);
288 printf_verbose("Update pool size offset %lu size %lu.\n",
289 write_pool_offset
, write_pool_size
);
290 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
291 "main", pthread_self(), (unsigned long)gettid());
293 tid_reader
= malloc(sizeof(*tid_reader
) * nr_readers
);
294 tid_writer
= malloc(sizeof(*tid_writer
) * nr_writers
);
295 count_reader
= malloc(sizeof(*count_reader
) * nr_readers
);
296 count_writer
= malloc(sizeof(*count_writer
) * nr_writers
);
298 err
= create_all_cpu_call_rcu_data(0);
300 printf("Per-CPU call_rcu() worker threads unavailable. Using default global worker thread.\n");
303 /* Test with 8-bit key */
304 test_ja
= cds_ja_new(8);
306 printf("Error allocating judy array.\n");
311 for (i
= 0; i
< 200; i
++) {
312 struct ja_test_node
*node
=
313 calloc(sizeof(*node
), 1);
315 ja_test_node_init(node
, i
);
316 ret
= cds_ja_add(test_ja
, i
, &node
->node
);
318 printf("Error adding node (%d)\n", ret
);
323 ret
= cds_ja_destroy(test_ja
);
325 printf("Error destroying judy array\n");
328 printf("Test end.\n");
333 * Hash Population needs to be seen as a RCU reader
334 * thread from the point of view of resize.
336 rcu_register_thread();
337 ret
= (get_populate_hash_cb())();
340 rcu_thread_offline();
344 for (i
= 0; i
< nr_readers
; i
++) {
345 err
= pthread_create(&tid_reader
[i
],
346 NULL
, get_thr_reader_cb(),
351 for (i
= 0; i
< nr_writers
; i
++) {
352 err
= pthread_create(&tid_writer
[i
],
353 NULL
, get_thr_writer_cb(),
365 remain
= sleep(remain
);
366 } while (remain
> 0);
370 for (i
= 0; i
< nr_readers
; i
++) {
371 err
= pthread_join(tid_reader
[i
], &tret
);
374 tot_reads
+= count_reader
[i
];
376 for (i
= 0; i
< nr_writers
; i
++) {
377 err
= pthread_join(tid_writer
[i
], &tret
);
380 tot_writes
+= count_writer
[i
].update_ops
;
381 tot_add
+= count_writer
[i
].add
;
382 tot_add_exist
+= count_writer
[i
].add_exist
;
383 tot_remove
+= count_writer
[i
].remove
;
386 /* teardown counter thread */
387 act
.sa_handler
= SIG_IGN
;
388 act
.sa_flags
= SA_RESTART
;
389 ret
= sigaction(SIGUSR2
, &act
, NULL
);
395 char msg
[1] = { 0x42 };
399 ret
= write(count_pipe
[1], msg
, 1); /* wakeup thread */
400 } while (ret
== -1L && errno
== EINTR
);
406 printf("Counting nodes... ");
407 cds_lfht_count_nodes(test_ht
, &approx_before
, &count
, &approx_after
);
409 test_delete_all_nodes(test_ht
);
411 rcu_thread_offline();
413 printf("Approximation before node accounting: %ld nodes.\n",
415 printf("Nodes deleted from hash table before destroy: "
418 printf("Approximation after node accounting: %ld nodes.\n",
421 ret
= cds_lfht_destroy(test_ht
, NULL
);
423 printf_verbose("final delete aborted\n");
425 printf_verbose("final delete success\n");
426 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads
,
428 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu "
430 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu "
431 "nr_add %12llu nr_add_fail %12llu nr_remove %12llu nr_leaked %12lld\n",
432 argv
[0], duration
, nr_readers
, rduration
,
433 nr_writers
, wdelay
, tot_reads
, tot_writes
,
434 tot_reads
+ tot_writes
, tot_add
, tot_add_exist
, tot_remove
,
435 (long long) tot_add
+ init_populate
- tot_remove
- count
);
436 rcu_unregister_thread();
437 free_all_cpu_call_rcu_data();