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