Cleanup: tests: cast console write return value as void
[urcu.git] / tests / benchmark / test_urcu_lfs.c
1 /*
2 * test_urcu_lfs.c
3 *
4 * Userspace RCU library - example lock-free stack
5 *
6 * Copyright 2010-2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 * Copyright February 2010 - Paolo Bonzini <pbonzini@redhat.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
24 #define _GNU_SOURCE
25 #include "config.h"
26 #include <stdio.h>
27 #include <pthread.h>
28 #include <stdlib.h>
29 #include <stdint.h>
30 #include <stdbool.h>
31 #include <string.h>
32 #include <sys/types.h>
33 #include <sys/wait.h>
34 #include <unistd.h>
35 #include <stdio.h>
36 #include <assert.h>
37 #include <errno.h>
38
39 #include <urcu/arch.h>
40 #include <urcu/tls-compat.h>
41 #include "cpuset.h"
42 #include "thread-id.h"
43
44 /* hardcoded number of CPUs */
45 #define NR_CPUS 16384
46
47 #ifndef DYNAMIC_LINK_TEST
48 #define _LGPL_SOURCE
49 #endif
50 #include <urcu.h>
51 #include <urcu/cds.h>
52
53 /*
54 * External synchronization used.
55 */
56 enum test_sync {
57 TEST_SYNC_NONE = 0,
58 TEST_SYNC_RCU,
59 };
60
61 static enum test_sync test_sync;
62
63 static volatile int test_go, test_stop;
64
65 static unsigned long rduration;
66
67 static unsigned long duration;
68
69 /* read-side C.S. duration, in loops */
70 static unsigned long wdelay;
71
72 static inline void loop_sleep(unsigned long loops)
73 {
74 while (loops-- != 0)
75 caa_cpu_relax();
76 }
77
78 static int verbose_mode;
79
80 static int test_pop, test_pop_all;
81
82 #define printf_verbose(fmt, args...) \
83 do { \
84 if (verbose_mode) \
85 printf(fmt, ## args); \
86 } while (0)
87
88 static unsigned int cpu_affinities[NR_CPUS];
89 static unsigned int next_aff = 0;
90 static int use_affinity = 0;
91
92 pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
93
94 static void set_affinity(void)
95 {
96 #if HAVE_SCHED_SETAFFINITY
97 cpu_set_t mask;
98 int cpu, ret;
99 #endif /* HAVE_SCHED_SETAFFINITY */
100
101 if (!use_affinity)
102 return;
103
104 #if HAVE_SCHED_SETAFFINITY
105 ret = pthread_mutex_lock(&affinity_mutex);
106 if (ret) {
107 perror("Error in pthread mutex lock");
108 exit(-1);
109 }
110 cpu = cpu_affinities[next_aff++];
111 ret = pthread_mutex_unlock(&affinity_mutex);
112 if (ret) {
113 perror("Error in pthread mutex unlock");
114 exit(-1);
115 }
116
117 CPU_ZERO(&mask);
118 CPU_SET(cpu, &mask);
119 #if SCHED_SETAFFINITY_ARGS == 2
120 sched_setaffinity(0, &mask);
121 #else
122 sched_setaffinity(0, sizeof(mask), &mask);
123 #endif
124 #endif /* HAVE_SCHED_SETAFFINITY */
125 }
126
127 /*
128 * returns 0 if test should end.
129 */
130 static int test_duration_dequeue(void)
131 {
132 return !test_stop;
133 }
134
135 static int test_duration_enqueue(void)
136 {
137 return !test_stop;
138 }
139
140 static DEFINE_URCU_TLS(unsigned long long, nr_dequeues);
141 static DEFINE_URCU_TLS(unsigned long long, nr_enqueues);
142
143 static DEFINE_URCU_TLS(unsigned long long, nr_successful_dequeues);
144 static DEFINE_URCU_TLS(unsigned long long, nr_successful_enqueues);
145
146 static unsigned int nr_enqueuers;
147 static unsigned int nr_dequeuers;
148
149 struct test {
150 struct cds_lfs_node list;
151 struct rcu_head rcu;
152 };
153
154 static struct cds_lfs_stack s;
155
156 static void *thr_enqueuer(void *_count)
157 {
158 unsigned long long *count = _count;
159
160 printf_verbose("thread_begin %s, tid %lu\n",
161 "enqueuer", urcu_get_thread_id());
162
163 set_affinity();
164
165 rcu_register_thread();
166
167 while (!test_go)
168 {
169 }
170 cmm_smp_mb();
171
172 for (;;) {
173 struct test *node = malloc(sizeof(*node));
174 if (!node)
175 goto fail;
176 cds_lfs_node_init(&node->list);
177 cds_lfs_push(&s, &node->list);
178 URCU_TLS(nr_successful_enqueues)++;
179
180 if (caa_unlikely(wdelay))
181 loop_sleep(wdelay);
182 fail:
183 URCU_TLS(nr_enqueues)++;
184 if (caa_unlikely(!test_duration_enqueue()))
185 break;
186 }
187
188 rcu_unregister_thread();
189
190 count[0] = URCU_TLS(nr_enqueues);
191 count[1] = URCU_TLS(nr_successful_enqueues);
192 printf_verbose("enqueuer thread_end, tid %lu, "
193 "enqueues %llu successful_enqueues %llu\n",
194 urcu_get_thread_id(),
195 URCU_TLS(nr_enqueues),
196 URCU_TLS(nr_successful_enqueues));
197 return ((void*)1);
198
199 }
200
201 static
202 void free_node_cb(struct rcu_head *head)
203 {
204 struct test *node =
205 caa_container_of(head, struct test, rcu);
206 free(node);
207 }
208
209 static
210 void do_test_pop(enum test_sync sync)
211 {
212 struct cds_lfs_node *snode;
213
214 if (sync == TEST_SYNC_RCU)
215 rcu_read_lock();
216 snode = __cds_lfs_pop(&s);
217 if (sync == TEST_SYNC_RCU)
218 rcu_read_unlock();
219 if (snode) {
220 struct test *node;
221
222 node = caa_container_of(snode,
223 struct test, list);
224 if (sync == TEST_SYNC_RCU)
225 call_rcu(&node->rcu, free_node_cb);
226 else
227 free(node);
228 URCU_TLS(nr_successful_dequeues)++;
229 }
230 URCU_TLS(nr_dequeues)++;
231 }
232
233 static
234 void do_test_pop_all(enum test_sync sync)
235 {
236 struct cds_lfs_node *snode;
237 struct cds_lfs_head *head;
238 struct cds_lfs_node *n;
239
240 head = __cds_lfs_pop_all(&s);
241 cds_lfs_for_each_safe(head, snode, n) {
242 struct test *node;
243
244 node = caa_container_of(snode, struct test, list);
245 if (sync == TEST_SYNC_RCU)
246 call_rcu(&node->rcu, free_node_cb);
247 else
248 free(node);
249 URCU_TLS(nr_successful_dequeues)++;
250 URCU_TLS(nr_dequeues)++;
251 }
252
253 }
254
255 static void *thr_dequeuer(void *_count)
256 {
257 unsigned long long *count = _count;
258 unsigned int counter = 0;
259
260 printf_verbose("thread_begin %s, tid %lu\n",
261 "dequeuer", urcu_get_thread_id());
262
263 set_affinity();
264
265 rcu_register_thread();
266
267 while (!test_go)
268 {
269 }
270 cmm_smp_mb();
271
272 assert(test_pop || test_pop_all);
273
274 for (;;) {
275 if (test_pop && test_pop_all) {
276 /* both pop and pop all */
277 if (counter & 1)
278 do_test_pop(test_sync);
279 else
280 do_test_pop_all(test_sync);
281 counter++;
282 } else {
283 if (test_pop)
284 do_test_pop(test_sync);
285 else
286 do_test_pop_all(test_sync);
287 }
288
289 if (caa_unlikely(!test_duration_dequeue()))
290 break;
291 if (caa_unlikely(rduration))
292 loop_sleep(rduration);
293 }
294
295 rcu_unregister_thread();
296
297 printf_verbose("dequeuer thread_end, tid %lu, "
298 "dequeues %llu, successful_dequeues %llu\n",
299 urcu_get_thread_id(),
300 URCU_TLS(nr_dequeues),
301 URCU_TLS(nr_successful_dequeues));
302 count[0] = URCU_TLS(nr_dequeues);
303 count[1] = URCU_TLS(nr_successful_dequeues);
304 return ((void*)2);
305 }
306
307 static void test_end(struct cds_lfs_stack *s, unsigned long long *nr_dequeues)
308 {
309 struct cds_lfs_node *snode;
310
311 do {
312 snode = __cds_lfs_pop(s);
313 if (snode) {
314 struct test *node;
315
316 node = caa_container_of(snode, struct test, list);
317 free(node);
318 (*nr_dequeues)++;
319 }
320 } while (snode);
321 }
322
323 static void show_usage(int argc, char **argv)
324 {
325 printf("Usage : %s nr_dequeuers nr_enqueuers duration (s) <OPTIONS>\n",
326 argv[0]);
327 printf("OPTIONS:\n");
328 printf(" [-d delay] (enqueuer period (in loops))\n");
329 printf(" [-c duration] (dequeuer period (in loops))\n");
330 printf(" [-v] (verbose output)\n");
331 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
332 printf(" [-p] (test pop)\n");
333 printf(" [-P] (test pop_all, enabled by default)\n");
334 printf(" [-R] (use RCU external synchronization)\n");
335 printf(" Note: default: no external synchronization used.\n");
336 printf("\n");
337 }
338
339 int main(int argc, char **argv)
340 {
341 int err;
342 pthread_t *tid_enqueuer, *tid_dequeuer;
343 void *tret;
344 unsigned long long *count_enqueuer, *count_dequeuer;
345 unsigned long long tot_enqueues = 0, tot_dequeues = 0;
346 unsigned long long tot_successful_enqueues = 0,
347 tot_successful_dequeues = 0;
348 unsigned long long end_dequeues = 0;
349 int i, a;
350
351 if (argc < 4) {
352 show_usage(argc, argv);
353 return -1;
354 }
355
356 err = sscanf(argv[1], "%u", &nr_dequeuers);
357 if (err != 1) {
358 show_usage(argc, argv);
359 return -1;
360 }
361
362 err = sscanf(argv[2], "%u", &nr_enqueuers);
363 if (err != 1) {
364 show_usage(argc, argv);
365 return -1;
366 }
367
368 err = sscanf(argv[3], "%lu", &duration);
369 if (err != 1) {
370 show_usage(argc, argv);
371 return -1;
372 }
373
374 for (i = 4; i < argc; i++) {
375 if (argv[i][0] != '-')
376 continue;
377 switch (argv[i][1]) {
378 case 'a':
379 if (argc < i + 2) {
380 show_usage(argc, argv);
381 return -1;
382 }
383 a = atoi(argv[++i]);
384 cpu_affinities[next_aff++] = a;
385 use_affinity = 1;
386 printf_verbose("Adding CPU %d affinity\n", a);
387 break;
388 case 'c':
389 if (argc < i + 2) {
390 show_usage(argc, argv);
391 return -1;
392 }
393 rduration = atol(argv[++i]);
394 break;
395 case 'd':
396 if (argc < i + 2) {
397 show_usage(argc, argv);
398 return -1;
399 }
400 wdelay = atol(argv[++i]);
401 break;
402 case 'v':
403 verbose_mode = 1;
404 break;
405 case 'p':
406 test_pop = 1;
407 break;
408 case 'P':
409 test_pop_all = 1;
410 break;
411 case 'R':
412 test_sync = TEST_SYNC_RCU;
413 break;
414 }
415 }
416
417 /* activate pop_all test by default */
418 if (!test_pop && !test_pop_all)
419 test_pop_all = 1;
420
421 printf_verbose("running test for %lu seconds, %u enqueuers, "
422 "%u dequeuers.\n",
423 duration, nr_enqueuers, nr_dequeuers);
424 if (test_pop)
425 printf_verbose("pop test activated.\n");
426 if (test_pop_all)
427 printf_verbose("pop_all test activated.\n");
428 if (test_sync == TEST_SYNC_RCU)
429 printf_verbose("External sync: RCU.\n");
430 else
431 printf_verbose("External sync: none.\n");
432 printf_verbose("Writer delay : %lu loops.\n", rduration);
433 printf_verbose("Reader duration : %lu loops.\n", wdelay);
434 printf_verbose("thread %-6s, tid %lu\n",
435 "main", urcu_get_thread_id());
436
437 tid_enqueuer = calloc(nr_enqueuers, sizeof(*tid_enqueuer));
438 tid_dequeuer = calloc(nr_dequeuers, sizeof(*tid_dequeuer));
439 count_enqueuer = calloc(nr_enqueuers, 2 * sizeof(*count_enqueuer));
440 count_dequeuer = calloc(nr_dequeuers, 2 * sizeof(*count_dequeuer));
441 cds_lfs_init(&s);
442 err = create_all_cpu_call_rcu_data(0);
443 if (err) {
444 printf("Per-CPU call_rcu() worker threads unavailable. Using default global worker thread.\n");
445 }
446
447 next_aff = 0;
448
449 for (i = 0; i < nr_enqueuers; i++) {
450 err = pthread_create(&tid_enqueuer[i], NULL, thr_enqueuer,
451 &count_enqueuer[2 * i]);
452 if (err != 0)
453 exit(1);
454 }
455 for (i = 0; i < nr_dequeuers; i++) {
456 err = pthread_create(&tid_dequeuer[i], NULL, thr_dequeuer,
457 &count_dequeuer[2 * i]);
458 if (err != 0)
459 exit(1);
460 }
461
462 cmm_smp_mb();
463
464 test_go = 1;
465
466 for (i = 0; i < duration; i++) {
467 sleep(1);
468 if (verbose_mode)
469 (void) write(1, ".", 1);
470 }
471
472 test_stop = 1;
473
474 for (i = 0; i < nr_enqueuers; i++) {
475 err = pthread_join(tid_enqueuer[i], &tret);
476 if (err != 0)
477 exit(1);
478 tot_enqueues += count_enqueuer[2 * i];
479 tot_successful_enqueues += count_enqueuer[2 * i + 1];
480 }
481 for (i = 0; i < nr_dequeuers; i++) {
482 err = pthread_join(tid_dequeuer[i], &tret);
483 if (err != 0)
484 exit(1);
485 tot_dequeues += count_dequeuer[2 * i];
486 tot_successful_dequeues += count_dequeuer[2 * i + 1];
487 }
488
489 test_end(&s, &end_dequeues);
490
491 printf_verbose("total number of enqueues : %llu, dequeues %llu\n",
492 tot_enqueues, tot_dequeues);
493 printf_verbose("total number of successful enqueues : %llu, "
494 "successful dequeues %llu\n",
495 tot_successful_enqueues, tot_successful_dequeues);
496 printf("SUMMARY %-25s testdur %4lu nr_enqueuers %3u wdelay %6lu "
497 "nr_dequeuers %3u "
498 "rdur %6lu nr_enqueues %12llu nr_dequeues %12llu "
499 "successful enqueues %12llu successful dequeues %12llu "
500 "end_dequeues %llu nr_ops %12llu\n",
501 argv[0], duration, nr_enqueuers, wdelay,
502 nr_dequeuers, rduration, tot_enqueues, tot_dequeues,
503 tot_successful_enqueues,
504 tot_successful_dequeues, end_dequeues,
505 tot_enqueues + tot_dequeues);
506 if (tot_successful_enqueues != tot_successful_dequeues + end_dequeues)
507 printf("WARNING! Discrepancy between nr succ. enqueues %llu vs "
508 "succ. dequeues + end dequeues %llu.\n",
509 tot_successful_enqueues,
510 tot_successful_dequeues + end_dequeues);
511
512 free_all_cpu_call_rcu_data();
513 free(count_enqueuer);
514 free(count_dequeuer);
515 free(tid_enqueuer);
516 free(tid_dequeuer);
517 return 0;
518 }
This page took 0.039263 seconds and 5 git commands to generate.