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