Add -Wextra to CFLAGS
[urcu.git] / tests / benchmark / test_urcu_lfs.c
CommitLineData
453629a9
MD
1/*
2 * test_urcu_lfs.c
3 *
cb8818f0 4 * Userspace RCU library - example lock-free stack
453629a9 5 *
cb8818f0 6 * Copyright 2010-2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
453629a9
MD
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
453629a9
MD
24#include <stdio.h>
25#include <pthread.h>
26#include <stdlib.h>
27#include <stdint.h>
28#include <stdbool.h>
29#include <string.h>
30#include <sys/types.h>
31#include <sys/wait.h>
32#include <unistd.h>
33#include <stdio.h>
34#include <assert.h>
453629a9
MD
35#include <errno.h>
36
37#include <urcu/arch.h>
bd252a04 38#include <urcu/tls-compat.h>
2953b501 39#include "cpuset.h"
94df6318 40#include "thread-id.h"
65fcc7e9 41
453629a9
MD
42/* hardcoded number of CPUs */
43#define NR_CPUS 16384
44
453629a9
MD
45#ifndef DYNAMIC_LINK_TEST
46#define _LGPL_SOURCE
47#endif
48#include <urcu.h>
e5a7d709 49#include <urcu/cds.h>
453629a9 50
f9b5c2b6
MD
51#define POISON_PTR ((void *) 0x42UL)
52
111ce0c3
MD
53/*
54 * External synchronization used.
55 */
56enum test_sync {
57 TEST_SYNC_NONE = 0,
58 TEST_SYNC_RCU,
59};
60
61static enum test_sync test_sync;
62
453629a9
MD
63static volatile int test_go, test_stop;
64
65static unsigned long rduration;
66
67static unsigned long duration;
68
69/* read-side C.S. duration, in loops */
70static unsigned long wdelay;
71
ab0aacbe 72static inline void loop_sleep(unsigned long loops)
453629a9 73{
ab0aacbe 74 while (loops-- != 0)
06f22bdb 75 caa_cpu_relax();
453629a9
MD
76}
77
78static int verbose_mode;
79
111ce0c3
MD
80static int test_pop, test_pop_all;
81
453629a9
MD
82#define printf_verbose(fmt, args...) \
83 do { \
84 if (verbose_mode) \
111ce0c3 85 printf(fmt, ## args); \
453629a9
MD
86 } while (0)
87
88static unsigned int cpu_affinities[NR_CPUS];
89static unsigned int next_aff = 0;
90static int use_affinity = 0;
91
92pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
93
453629a9
MD
94static void set_affinity(void)
95{
95bc7fb9 96#if HAVE_SCHED_SETAFFINITY
453629a9 97 cpu_set_t mask;
95bc7fb9
MD
98 int cpu, ret;
99#endif /* HAVE_SCHED_SETAFFINITY */
453629a9
MD
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 */
130static int test_duration_dequeue(void)
131{
132 return !test_stop;
133}
134
135static int test_duration_enqueue(void)
136{
137 return !test_stop;
138}
139
bd252a04
MD
140static DEFINE_URCU_TLS(unsigned long long, nr_dequeues);
141static DEFINE_URCU_TLS(unsigned long long, nr_enqueues);
453629a9 142
bd252a04
MD
143static DEFINE_URCU_TLS(unsigned long long, nr_successful_dequeues);
144static DEFINE_URCU_TLS(unsigned long long, nr_successful_enqueues);
453629a9
MD
145
146static unsigned int nr_enqueuers;
147static unsigned int nr_dequeuers;
148
94aec122 149struct test {
cb8818f0 150 struct cds_lfs_node list;
94aec122
MD
151 struct rcu_head rcu;
152};
153
cb8818f0 154static struct cds_lfs_stack s;
453629a9 155
eb314468 156static void *thr_enqueuer(void *_count)
453629a9
MD
157{
158 unsigned long long *count = _count;
159
94df6318
MD
160 printf_verbose("thread_begin %s, tid %lu\n",
161 "enqueuer", urcu_get_thread_id());
453629a9
MD
162
163 set_affinity();
164
165 rcu_register_thread();
166
167 while (!test_go)
168 {
169 }
5481ddb3 170 cmm_smp_mb();
453629a9
MD
171
172 for (;;) {
94aec122 173 struct test *node = malloc(sizeof(*node));
453629a9
MD
174 if (!node)
175 goto fail;
cb8818f0
MD
176 cds_lfs_node_init(&node->list);
177 cds_lfs_push(&s, &node->list);
bd252a04 178 URCU_TLS(nr_successful_enqueues)++;
453629a9 179
a0b7f7ea 180 if (caa_unlikely(wdelay))
453629a9
MD
181 loop_sleep(wdelay);
182fail:
bd252a04 183 URCU_TLS(nr_enqueues)++;
a0b7f7ea 184 if (caa_unlikely(!test_duration_enqueue()))
453629a9
MD
185 break;
186 }
187
188 rcu_unregister_thread();
189
bd252a04
MD
190 count[0] = URCU_TLS(nr_enqueues);
191 count[1] = URCU_TLS(nr_successful_enqueues);
94df6318
MD
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));
453629a9
MD
197 return ((void*)1);
198
199}
200
94aec122
MD
201static
202void 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
111ce0c3
MD
209static
210void 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
f9b5c2b6 222 snode->next = POISON_PTR;
111ce0c3
MD
223 node = caa_container_of(snode,
224 struct test, list);
225 if (sync == TEST_SYNC_RCU)
226 call_rcu(&node->rcu, free_node_cb);
227 else
228 free(node);
229 URCU_TLS(nr_successful_dequeues)++;
230 }
231 URCU_TLS(nr_dequeues)++;
232}
233
234static
235void do_test_pop_all(enum test_sync sync)
236{
237 struct cds_lfs_node *snode;
238 struct cds_lfs_head *head;
239 struct cds_lfs_node *n;
240
241 head = __cds_lfs_pop_all(&s);
242 cds_lfs_for_each_safe(head, snode, n) {
243 struct test *node;
244
f9b5c2b6 245 snode->next = POISON_PTR;
111ce0c3
MD
246 node = caa_container_of(snode, struct test, list);
247 if (sync == TEST_SYNC_RCU)
248 call_rcu(&node->rcu, free_node_cb);
249 else
250 free(node);
251 URCU_TLS(nr_successful_dequeues)++;
252 URCU_TLS(nr_dequeues)++;
253 }
254
255}
256
eb314468 257static void *thr_dequeuer(void *_count)
453629a9
MD
258{
259 unsigned long long *count = _count;
f42bc7ef 260 unsigned int counter = 0;
453629a9 261
94df6318
MD
262 printf_verbose("thread_begin %s, tid %lu\n",
263 "dequeuer", urcu_get_thread_id());
453629a9
MD
264
265 set_affinity();
266
453629a9
MD
267 rcu_register_thread();
268
269 while (!test_go)
270 {
271 }
5481ddb3 272 cmm_smp_mb();
453629a9 273
111ce0c3 274 assert(test_pop || test_pop_all);
f3eb6ef1 275
111ce0c3 276 for (;;) {
111ce0c3
MD
277 if (test_pop && test_pop_all) {
278 /* both pop and pop all */
279 if (counter & 1)
280 do_test_pop(test_sync);
281 else
282 do_test_pop_all(test_sync);
283 counter++;
284 } else {
285 if (test_pop)
286 do_test_pop(test_sync);
287 else
288 do_test_pop_all(test_sync);
453629a9 289 }
111ce0c3 290
a0b7f7ea 291 if (caa_unlikely(!test_duration_dequeue()))
453629a9 292 break;
a0b7f7ea 293 if (caa_unlikely(rduration))
453629a9
MD
294 loop_sleep(rduration);
295 }
296
297 rcu_unregister_thread();
453629a9 298
94df6318
MD
299 printf_verbose("dequeuer thread_end, tid %lu, "
300 "dequeues %llu, successful_dequeues %llu\n",
301 urcu_get_thread_id(),
302 URCU_TLS(nr_dequeues),
303 URCU_TLS(nr_successful_dequeues));
bd252a04
MD
304 count[0] = URCU_TLS(nr_dequeues);
305 count[1] = URCU_TLS(nr_successful_dequeues);
453629a9
MD
306 return ((void*)2);
307}
308
eb314468 309static void test_end(struct cds_lfs_stack *s, unsigned long long *nr_dequeues)
453629a9 310{
cb8818f0 311 struct cds_lfs_node *snode;
453629a9
MD
312
313 do {
7294103b 314 snode = __cds_lfs_pop(s);
94aec122
MD
315 if (snode) {
316 struct test *node;
317
318 node = caa_container_of(snode, struct test, list);
453629a9
MD
319 free(node);
320 (*nr_dequeues)++;
321 }
94aec122 322 } while (snode);
453629a9
MD
323}
324
eb314468 325static void show_usage(int argc, char **argv)
453629a9 326{
06637138
MD
327 printf("Usage : %s nr_dequeuers nr_enqueuers duration (s) <OPTIONS>\n",
328 argv[0]);
329 printf("OPTIONS:\n");
330 printf(" [-d delay] (enqueuer period (in loops))\n");
331 printf(" [-c duration] (dequeuer period (in loops))\n");
332 printf(" [-v] (verbose output)\n");
333 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
334 printf(" [-p] (test pop)\n");
335 printf(" [-P] (test pop_all, enabled by default)\n");
336 printf(" [-R] (use RCU external synchronization)\n");
337 printf(" Note: default: no external synchronization used.\n");
453629a9
MD
338 printf("\n");
339}
340
341int main(int argc, char **argv)
342{
343 int err;
344 pthread_t *tid_enqueuer, *tid_dequeuer;
345 void *tret;
346 unsigned long long *count_enqueuer, *count_dequeuer;
347 unsigned long long tot_enqueues = 0, tot_dequeues = 0;
348 unsigned long long tot_successful_enqueues = 0,
349 tot_successful_dequeues = 0;
350 unsigned long long end_dequeues = 0;
351 int i, a;
352
353 if (argc < 4) {
354 show_usage(argc, argv);
355 return -1;
356 }
357
358 err = sscanf(argv[1], "%u", &nr_dequeuers);
359 if (err != 1) {
360 show_usage(argc, argv);
361 return -1;
362 }
363
364 err = sscanf(argv[2], "%u", &nr_enqueuers);
365 if (err != 1) {
366 show_usage(argc, argv);
367 return -1;
368 }
369
370 err = sscanf(argv[3], "%lu", &duration);
371 if (err != 1) {
372 show_usage(argc, argv);
373 return -1;
374 }
375
376 for (i = 4; i < argc; i++) {
377 if (argv[i][0] != '-')
378 continue;
379 switch (argv[i][1]) {
380 case 'a':
381 if (argc < i + 2) {
382 show_usage(argc, argv);
383 return -1;
384 }
385 a = atoi(argv[++i]);
386 cpu_affinities[next_aff++] = a;
387 use_affinity = 1;
388 printf_verbose("Adding CPU %d affinity\n", a);
389 break;
390 case 'c':
391 if (argc < i + 2) {
392 show_usage(argc, argv);
393 return -1;
394 }
395 rduration = atol(argv[++i]);
396 break;
397 case 'd':
398 if (argc < i + 2) {
399 show_usage(argc, argv);
400 return -1;
401 }
402 wdelay = atol(argv[++i]);
403 break;
404 case 'v':
405 verbose_mode = 1;
406 break;
111ce0c3
MD
407 case 'p':
408 test_pop = 1;
409 break;
410 case 'P':
411 test_pop_all = 1;
412 break;
413 case 'R':
414 test_sync = TEST_SYNC_RCU;
415 break;
453629a9
MD
416 }
417 }
418
111ce0c3
MD
419 /* activate pop_all test by default */
420 if (!test_pop && !test_pop_all)
421 test_pop_all = 1;
422
453629a9
MD
423 printf_verbose("running test for %lu seconds, %u enqueuers, "
424 "%u dequeuers.\n",
425 duration, nr_enqueuers, nr_dequeuers);
111ce0c3
MD
426 if (test_pop)
427 printf_verbose("pop test activated.\n");
428 if (test_pop_all)
429 printf_verbose("pop_all test activated.\n");
eb314468
MD
430 if (test_sync == TEST_SYNC_RCU)
431 printf_verbose("External sync: RCU.\n");
432 else
433 printf_verbose("External sync: none.\n");
453629a9
MD
434 printf_verbose("Writer delay : %lu loops.\n", rduration);
435 printf_verbose("Reader duration : %lu loops.\n", wdelay);
94df6318
MD
436 printf_verbose("thread %-6s, tid %lu\n",
437 "main", urcu_get_thread_id());
453629a9 438
9aa14175
MD
439 tid_enqueuer = calloc(nr_enqueuers, sizeof(*tid_enqueuer));
440 tid_dequeuer = calloc(nr_dequeuers, sizeof(*tid_dequeuer));
441 count_enqueuer = calloc(nr_enqueuers, 2 * sizeof(*count_enqueuer));
442 count_dequeuer = calloc(nr_dequeuers, 2 * sizeof(*count_dequeuer));
cb8818f0 443 cds_lfs_init(&s);
94aec122 444 err = create_all_cpu_call_rcu_data(0);
8bcbd94a
MD
445 if (err) {
446 printf("Per-CPU call_rcu() worker threads unavailable. Using default global worker thread.\n");
447 }
453629a9
MD
448
449 next_aff = 0;
450
451 for (i = 0; i < nr_enqueuers; i++) {
452 err = pthread_create(&tid_enqueuer[i], NULL, thr_enqueuer,
453 &count_enqueuer[2 * i]);
454 if (err != 0)
455 exit(1);
456 }
457 for (i = 0; i < nr_dequeuers; i++) {
458 err = pthread_create(&tid_dequeuer[i], NULL, thr_dequeuer,
459 &count_dequeuer[2 * i]);
460 if (err != 0)
461 exit(1);
462 }
463
5481ddb3 464 cmm_smp_mb();
453629a9
MD
465
466 test_go = 1;
467
468 for (i = 0; i < duration; i++) {
469 sleep(1);
4b665350
MD
470 if (verbose_mode) {
471 fwrite(".", sizeof(char), 1, stdout);
472 fflush(stdout);
473 }
453629a9
MD
474 }
475
476 test_stop = 1;
477
478 for (i = 0; i < nr_enqueuers; i++) {
479 err = pthread_join(tid_enqueuer[i], &tret);
480 if (err != 0)
481 exit(1);
482 tot_enqueues += count_enqueuer[2 * i];
483 tot_successful_enqueues += count_enqueuer[2 * i + 1];
484 }
485 for (i = 0; i < nr_dequeuers; i++) {
486 err = pthread_join(tid_dequeuer[i], &tret);
487 if (err != 0)
488 exit(1);
489 tot_dequeues += count_dequeuer[2 * i];
490 tot_successful_dequeues += count_dequeuer[2 * i + 1];
491 }
492
493 test_end(&s, &end_dequeues);
494
495 printf_verbose("total number of enqueues : %llu, dequeues %llu\n",
496 tot_enqueues, tot_dequeues);
497 printf_verbose("total number of successful enqueues : %llu, "
498 "successful dequeues %llu\n",
499 tot_successful_enqueues, tot_successful_dequeues);
500 printf("SUMMARY %-25s testdur %4lu nr_enqueuers %3u wdelay %6lu "
501 "nr_dequeuers %3u "
502 "rdur %6lu nr_enqueues %12llu nr_dequeues %12llu "
503 "successful enqueues %12llu successful dequeues %12llu "
504 "end_dequeues %llu nr_ops %12llu\n",
505 argv[0], duration, nr_enqueuers, wdelay,
506 nr_dequeuers, rduration, tot_enqueues, tot_dequeues,
507 tot_successful_enqueues,
508 tot_successful_dequeues, end_dequeues,
509 tot_enqueues + tot_dequeues);
510 if (tot_successful_enqueues != tot_successful_dequeues + end_dequeues)
511 printf("WARNING! Discrepancy between nr succ. enqueues %llu vs "
512 "succ. dequeues + end dequeues %llu.\n",
513 tot_successful_enqueues,
514 tot_successful_dequeues + end_dequeues);
515
0938c541 516 free_all_cpu_call_rcu_data();
2af1c19e 517 cds_lfs_destroy(&s);
453629a9
MD
518 free(count_enqueuer);
519 free(count_dequeuer);
520 free(tid_enqueuer);
521 free(tid_dequeuer);
522 return 0;
523}
This page took 0.059104 seconds and 4 git commands to generate.