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