rcuja: fix max depth test
[userspace-rcu.git] / tests / test_urcu_ja.c
CommitLineData
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>
44151f89
MD
26
27DEFINE_URCU_TLS(unsigned int, rand_lookup);
28DEFINE_URCU_TLS(unsigned long, nr_add);
29DEFINE_URCU_TLS(unsigned long, nr_addexist);
30DEFINE_URCU_TLS(unsigned long, nr_del);
31DEFINE_URCU_TLS(unsigned long, nr_delnoent);
32DEFINE_URCU_TLS(unsigned long, lookup_fail);
33DEFINE_URCU_TLS(unsigned long, lookup_ok);
34
35struct cds_ja *test_ja;
36
37volatile int test_go, test_stop;
38
39unsigned long wdelay;
40
41unsigned long duration;
42
43/* read-side C.S. duration, in loops */
44unsigned long rduration;
45
46unsigned long init_populate;
47int add_only;
48
49unsigned long init_pool_offset, lookup_pool_offset, write_pool_offset;
50unsigned long init_pool_size = DEFAULT_RAND_POOL,
51 lookup_pool_size = DEFAULT_RAND_POOL,
52 write_pool_size = DEFAULT_RAND_POOL;
53int validate_lookup;
54
55int count_pipe[2];
56
57int verbose_mode;
58
59unsigned int cpu_affinities[NR_CPUS];
60unsigned int next_aff = 0;
61int use_affinity = 0;
62
63pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
64
65DEFINE_URCU_TLS(unsigned long long, nr_writes);
66DEFINE_URCU_TLS(unsigned long long, nr_reads);
67
68unsigned int nr_readers;
69unsigned int nr_writers;
70
71static pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
72
73void set_affinity(void)
74{
75 cpu_set_t mask;
76 int cpu;
77 int ret;
78
79 if (!use_affinity)
80 return;
81
82#if HAVE_SCHED_SETAFFINITY
83 ret = pthread_mutex_lock(&affinity_mutex);
84 if (ret) {
85 perror("Error in pthread mutex lock");
86 exit(-1);
87 }
88 cpu = cpu_affinities[next_aff++];
89 ret = pthread_mutex_unlock(&affinity_mutex);
90 if (ret) {
91 perror("Error in pthread mutex unlock");
92 exit(-1);
93 }
94 CPU_ZERO(&mask);
95 CPU_SET(cpu, &mask);
96#if SCHED_SETAFFINITY_ARGS == 2
97 sched_setaffinity(0, &mask);
98#else
99 sched_setaffinity(0, sizeof(mask), &mask);
100#endif
101#endif /* HAVE_SCHED_SETAFFINITY */
102}
103
104void rcu_copy_mutex_lock(void)
105{
106 int ret;
107 ret = pthread_mutex_lock(&rcu_copy_mutex);
108 if (ret) {
109 perror("Error in pthread mutex lock");
110 exit(-1);
111 }
112}
113
114void rcu_copy_mutex_unlock(void)
115{
116 int ret;
117
118 ret = pthread_mutex_unlock(&rcu_copy_mutex);
119 if (ret) {
120 perror("Error in pthread mutex unlock");
121 exit(-1);
122 }
123}
124
125#if 0
126void free_node_cb(struct rcu_head *head)
127{
128 struct ja_test_node *node =
129 caa_container_of(head, struct ja_test_node, head);
130 free(node);
131}
132
133static
134void test_delete_all_nodes(struct cds_lfht *ht)
135{
136 struct cds_lfht_iter iter;
137 struct lfht_test_node *node;
138 unsigned long count = 0;
139
140 cds_lfht_for_each_entry(ht, &iter, node, node) {
141 int ret;
142
143 ret = cds_lfht_del(test_ht, cds_lfht_iter_get_node(&iter));
144 assert(!ret);
145 call_rcu(&node->head, free_node_cb);
146 count++;
147 }
148 printf("deleted %lu nodes.\n", count);
149}
150#endif
151
152void show_usage(int argc, char **argv)
153{
154 printf("Usage : %s nr_readers nr_writers duration (s)\n", argv[0]);
155#ifdef DEBUG_YIELD
156 printf(" [-r] [-w] (yield reader and/or writer)\n");
157#endif
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");
162printf(" [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");
172 printf("\n\n");
173}
174
175int main(int argc, char **argv)
176{
177 int err;
178 pthread_t *tid_reader, *tid_writer;
179 void *tret;
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;
184 int i, a, ret;
185 unsigned int remain;
a2a7ff59 186 uint64_t key;
44151f89
MD
187
188 if (argc < 4) {
189 show_usage(argc, argv);
190 return -1;
191 }
192
193 err = sscanf(argv[1], "%u", &nr_readers);
194 if (err != 1) {
195 show_usage(argc, argv);
196 return -1;
197 }
198
199 err = sscanf(argv[2], "%u", &nr_writers);
200 if (err != 1) {
201 show_usage(argc, argv);
202 return -1;
203 }
204
205 err = sscanf(argv[3], "%lu", &duration);
206 if (err != 1) {
207 show_usage(argc, argv);
208 return -1;
209 }
210
211 for (i = 4; i < argc; i++) {
212 if (argv[i][0] != '-')
213 continue;
214 switch (argv[i][1]) {
215#ifdef DEBUG_YIELD
216 case 'r':
217 yield_active |= YIELD_READ;
218 break;
219 case 'w':
220 yield_active |= YIELD_WRITE;
221 break;
222#endif
223 case 'a':
224 if (argc < i + 2) {
225 show_usage(argc, argv);
226 return -1;
227 }
228 a = atoi(argv[++i]);
229 cpu_affinities[next_aff++] = a;
230 use_affinity = 1;
231 printf_verbose("Adding CPU %d affinity\n", a);
232 break;
233 case 'c':
234 if (argc < i + 2) {
235 show_usage(argc, argv);
236 return -1;
237 }
238 rduration = atol(argv[++i]);
239 break;
240 case 'd':
241 if (argc < i + 2) {
242 show_usage(argc, argv);
243 return -1;
244 }
245 wdelay = atol(argv[++i]);
246 break;
247 case 'v':
248 verbose_mode = 1;
249 break;
250 case 'i':
251 add_only = 1;
252 break;
253 case 'k':
254 init_populate = atol(argv[++i]);
255 break;
256 case 'R':
257 lookup_pool_offset = atol(argv[++i]);
258 break;
259 case 'S':
260 write_pool_offset = atol(argv[++i]);
261 break;
262 case 'T':
263 init_pool_offset = atol(argv[++i]);
264 break;
265 case 'M':
266 lookup_pool_size = atol(argv[++i]);
267 break;
268 case 'N':
269 write_pool_size = atol(argv[++i]);
270 break;
271 case 'O':
272 init_pool_size = atol(argv[++i]);
273 break;
274 case 'V':
275 validate_lookup = 1;
276 break;
277 }
278 }
279
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());
294
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);
299
300 err = create_all_cpu_call_rcu_data(0);
301 if (err) {
302 printf("Per-CPU call_rcu() worker threads unavailable. Using default global worker thread.\n");
303 }
304
305 /* Test with 8-bit key */
306 test_ja = cds_ja_new(8);
307 if (!test_ja) {
308 printf("Error allocating judy array.\n");
309 return -1;
310 }
311
312 /* Add keys */
a2a7ff59
MD
313 printf("Test #1: add keys (8-bit).\n");
314 for (key = 0; key < 200; key++) {
44151f89
MD
315 struct ja_test_node *node =
316 calloc(sizeof(*node), 1);
317
a2a7ff59
MD
318 ja_test_node_init(node, key);
319 rcu_read_lock();
320 ret = cds_ja_add(test_ja, key, &node->node);
321 rcu_read_unlock();
44151f89 322 if (ret) {
a2a7ff59
MD
323 fprintf(stderr, "Error (%d) adding node %" PRIu64 "\n",
324 ret, key);
325 assert(0);
44151f89
MD
326 }
327 }
a2a7ff59
MD
328 printf("OK\n");
329
330 printf("Test #2: successful key lookup (8-bit).\n");
331 for (key = 0; key < 200; key++) {
332 struct cds_hlist_head *head;
333
334 rcu_read_lock();
335 head = cds_ja_lookup(test_ja, key);
336 if (!head) {
337 fprintf(stderr, "Error lookup node %" PRIu64 "\n", key);
338 assert(0);
339 }
340 rcu_read_unlock();
341 }
342 printf("OK\n");
343 printf("Test #3: unsuccessful key lookup (8-bit).\n");
344 for (key = 200; key < 240; key++) {
345 struct cds_hlist_head *head;
346
347 rcu_read_lock();
348 head = cds_ja_lookup(test_ja, key);
349 if (head) {
350 fprintf(stderr,
351 "Error unexpected lookup node %" PRIu64 "\n",
352 key);
353 assert(0);
354 }
355 rcu_read_unlock();
356 }
357 printf("OK\n");
44151f89
MD
358
359 ret = cds_ja_destroy(test_ja);
360 if (ret) {
a2a7ff59 361 fprintf(stderr, "Error destroying judy array\n");
44151f89
MD
362 return -1;
363 }
364 printf("Test end.\n");
365 return 0;
366
367#if 0
368 /*
369 * Hash Population needs to be seen as a RCU reader
370 * thread from the point of view of resize.
371 */
372 rcu_register_thread();
373 ret = (get_populate_hash_cb())();
374 assert(!ret);
375
376 rcu_thread_offline();
377
378 next_aff = 0;
379
380 for (i = 0; i < nr_readers; i++) {
381 err = pthread_create(&tid_reader[i],
382 NULL, get_thr_reader_cb(),
383 &count_reader[i]);
384 if (err != 0)
385 exit(1);
386 }
387 for (i = 0; i < nr_writers; i++) {
388 err = pthread_create(&tid_writer[i],
389 NULL, get_thr_writer_cb(),
390 &count_writer[i]);
391 if (err != 0)
392 exit(1);
393 }
394
395 cmm_smp_mb();
396
397 test_go = 1;
398
399 remain = duration;
400 do {
401 remain = sleep(remain);
402 } while (remain > 0);
403
404 test_stop = 1;
405
406 for (i = 0; i < nr_readers; i++) {
407 err = pthread_join(tid_reader[i], &tret);
408 if (err != 0)
409 exit(1);
410 tot_reads += count_reader[i];
411 }
412 for (i = 0; i < nr_writers; i++) {
413 err = pthread_join(tid_writer[i], &tret);
414 if (err != 0)
415 exit(1);
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;
420 }
421
422 /* teardown counter thread */
423 act.sa_handler = SIG_IGN;
424 act.sa_flags = SA_RESTART;
425 ret = sigaction(SIGUSR2, &act, NULL);
426 if (ret == -1) {
427 perror("sigaction");
428 return -1;
429 }
430 {
431 char msg[1] = { 0x42 };
432 ssize_t ret;
433
434 do {
435 ret = write(count_pipe[1], msg, 1); /* wakeup thread */
436 } while (ret == -1L && errno == EINTR);
437 }
438
439 fflush(stdout);
440 rcu_thread_online();
441 rcu_read_lock();
442 printf("Counting nodes... ");
443 cds_lfht_count_nodes(test_ht, &approx_before, &count, &approx_after);
444 printf("done.\n");
445 test_delete_all_nodes(test_ht);
446 rcu_read_unlock();
447 rcu_thread_offline();
448 if (count) {
449 printf("Approximation before node accounting: %ld nodes.\n",
450 approx_before);
451 printf("Nodes deleted from hash table before destroy: "
452 "%lu nodes.\n",
453 count);
454 printf("Approximation after node accounting: %ld nodes.\n",
455 approx_after);
456 }
457 ret = cds_lfht_destroy(test_ht, NULL);
458 if (ret)
459 printf_verbose("final delete aborted\n");
460 else
461 printf_verbose("final delete success\n");
462 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
463 tot_writes);
464 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu "
465 "nr_writers %3u "
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();
474 free(tid_reader);
475 free(tid_writer);
476 free(count_reader);
477 free(count_writer);
478#endif
479 return 0;
480}
This page took 0.040433 seconds and 4 git commands to generate.