rculfstack: header implementation should be defined as static inline
[urcu.git] / tests / test_urcu_wfs.c
CommitLineData
a5eb5ffc
MD
1/*
2 * test_urcu_wfs.c
3 *
4 * Userspace RCU library - example RCU-based lock-free stack
5 *
6 * Copyright February 2010 - 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>
a5eb5ffc
MD
37#include <sched.h>
38#include <errno.h>
39
40#include <urcu/arch.h>
41
65fcc7e9
MD
42#ifdef __linux__
43#include <syscall.h>
44#endif
45
a5eb5ffc
MD
46/* hardcoded number of CPUs */
47#define NR_CPUS 16384
48
49#if defined(_syscall0)
50_syscall0(pid_t, gettid)
51#elif defined(__NR_gettid)
52static inline pid_t gettid(void)
53{
54 return syscall(__NR_gettid);
55}
56#else
57#warning "use pid as tid"
58static inline pid_t gettid(void)
59{
60 return getpid();
61}
62#endif
63
64#ifndef DYNAMIC_LINK_TEST
65#define _LGPL_SOURCE
66#endif
67#include <urcu.h>
cb3f3d6b 68#include <urcu/wfstack.h>
a5eb5ffc
MD
69
70static volatile int test_go, test_stop;
71
72static unsigned long rduration;
73
74static unsigned long duration;
75
76/* read-side C.S. duration, in loops */
77static unsigned long wdelay;
78
79static inline void loop_sleep(unsigned long l)
80{
81 while(l-- != 0)
06f22bdb 82 caa_cpu_relax();
a5eb5ffc
MD
83}
84
85static int verbose_mode;
86
87#define printf_verbose(fmt, args...) \
88 do { \
89 if (verbose_mode) \
90 printf(fmt, args); \
91 } while (0)
92
93static unsigned int cpu_affinities[NR_CPUS];
94static unsigned int next_aff = 0;
95static int use_affinity = 0;
96
97pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
98
99#ifndef HAVE_CPU_SET_T
100typedef unsigned long cpu_set_t;
101# define CPU_ZERO(cpuset) do { *(cpuset) = 0; } while(0)
102# define CPU_SET(cpu, cpuset) do { *(cpuset) |= (1UL << (cpu)); } while(0)
103#endif
104
105static void set_affinity(void)
106{
107 cpu_set_t mask;
108 int cpu;
109 int ret;
110
111 if (!use_affinity)
112 return;
113
114#if HAVE_SCHED_SETAFFINITY
115 ret = pthread_mutex_lock(&affinity_mutex);
116 if (ret) {
117 perror("Error in pthread mutex lock");
118 exit(-1);
119 }
120 cpu = cpu_affinities[next_aff++];
121 ret = pthread_mutex_unlock(&affinity_mutex);
122 if (ret) {
123 perror("Error in pthread mutex unlock");
124 exit(-1);
125 }
126
127 CPU_ZERO(&mask);
128 CPU_SET(cpu, &mask);
129#if SCHED_SETAFFINITY_ARGS == 2
130 sched_setaffinity(0, &mask);
131#else
132 sched_setaffinity(0, sizeof(mask), &mask);
133#endif
134#endif /* HAVE_SCHED_SETAFFINITY */
135}
136
137/*
138 * returns 0 if test should end.
139 */
140static int test_duration_dequeue(void)
141{
142 return !test_stop;
143}
144
145static int test_duration_enqueue(void)
146{
147 return !test_stop;
148}
149
150static unsigned long long __thread nr_dequeues;
151static unsigned long long __thread nr_enqueues;
152
153static unsigned long long __thread nr_successful_dequeues;
154static unsigned long long __thread nr_successful_enqueues;
155
156static unsigned int nr_enqueuers;
157static unsigned int nr_dequeuers;
158
16aa9ee8 159static struct cds_wfs_stack s;
a5eb5ffc
MD
160
161void *thr_enqueuer(void *_count)
162{
163 unsigned long long *count = _count;
164
165 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
166 "enqueuer", pthread_self(), (unsigned long)gettid());
167
168 set_affinity();
169
a5eb5ffc
MD
170 while (!test_go)
171 {
172 }
5481ddb3 173 cmm_smp_mb();
a5eb5ffc
MD
174
175 for (;;) {
16aa9ee8 176 struct cds_wfs_node *node = malloc(sizeof(*node));
a5eb5ffc
MD
177 if (!node)
178 goto fail;
16aa9ee8
DG
179 cds_wfs_node_init(node);
180 cds_wfs_push(&s, node);
a5eb5ffc
MD
181 nr_successful_enqueues++;
182
183 if (unlikely(wdelay))
184 loop_sleep(wdelay);
185fail:
186 nr_enqueues++;
187 if (unlikely(!test_duration_enqueue()))
188 break;
189 }
190
a5eb5ffc
MD
191 count[0] = nr_enqueues;
192 count[1] = nr_successful_enqueues;
193 printf_verbose("enqueuer thread_end, thread id : %lx, tid %lu, "
194 "enqueues %llu successful_enqueues %llu\n",
195 pthread_self(), (unsigned long)gettid(), nr_enqueues,
196 nr_successful_enqueues);
197 return ((void*)1);
198
199}
200
201void *thr_dequeuer(void *_count)
202{
203 unsigned long long *count = _count;
204
205 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
206 "dequeuer", pthread_self(), (unsigned long)gettid());
207
208 set_affinity();
209
a5eb5ffc
MD
210 while (!test_go)
211 {
212 }
5481ddb3 213 cmm_smp_mb();
a5eb5ffc
MD
214
215 for (;;) {
16aa9ee8 216 struct cds_wfs_node *node = cds_wfs_pop_blocking(&s);
a5eb5ffc
MD
217
218 if (node) {
cb3f3d6b 219 free(node);
a5eb5ffc
MD
220 nr_successful_dequeues++;
221 }
222
223 nr_dequeues++;
224 if (unlikely(!test_duration_dequeue()))
225 break;
226 if (unlikely(rduration))
227 loop_sleep(rduration);
228 }
229
a5eb5ffc
MD
230 printf_verbose("dequeuer thread_end, thread id : %lx, tid %lu, "
231 "dequeues %llu, successful_dequeues %llu\n",
232 pthread_self(), (unsigned long)gettid(), nr_dequeues,
233 nr_successful_dequeues);
234 count[0] = nr_dequeues;
235 count[1] = nr_successful_dequeues;
236 return ((void*)2);
237}
238
16aa9ee8 239void test_end(struct cds_wfs_stack *s, unsigned long long *nr_dequeues)
a5eb5ffc 240{
16aa9ee8 241 struct cds_wfs_node *node;
a5eb5ffc
MD
242
243 do {
16aa9ee8 244 node = cds_wfs_pop_blocking(s);
a5eb5ffc
MD
245 if (node) {
246 free(node);
247 (*nr_dequeues)++;
248 }
249 } while (node);
250}
251
252void show_usage(int argc, char **argv)
253{
254 printf("Usage : %s nr_dequeuers nr_enqueuers duration (s)", argv[0]);
255 printf(" [-d delay] (enqueuer period (in loops))");
256 printf(" [-c duration] (dequeuer period (in loops))");
257 printf(" [-v] (verbose output)");
258 printf(" [-a cpu#] [-a cpu#]... (affinity)");
259 printf("\n");
260}
261
262int main(int argc, char **argv)
263{
264 int err;
265 pthread_t *tid_enqueuer, *tid_dequeuer;
266 void *tret;
267 unsigned long long *count_enqueuer, *count_dequeuer;
268 unsigned long long tot_enqueues = 0, tot_dequeues = 0;
269 unsigned long long tot_successful_enqueues = 0,
270 tot_successful_dequeues = 0;
271 unsigned long long end_dequeues = 0;
272 int i, a;
273
274 if (argc < 4) {
275 show_usage(argc, argv);
276 return -1;
277 }
278
279 err = sscanf(argv[1], "%u", &nr_dequeuers);
280 if (err != 1) {
281 show_usage(argc, argv);
282 return -1;
283 }
284
285 err = sscanf(argv[2], "%u", &nr_enqueuers);
286 if (err != 1) {
287 show_usage(argc, argv);
288 return -1;
289 }
290
291 err = sscanf(argv[3], "%lu", &duration);
292 if (err != 1) {
293 show_usage(argc, argv);
294 return -1;
295 }
296
297 for (i = 4; i < argc; i++) {
298 if (argv[i][0] != '-')
299 continue;
300 switch (argv[i][1]) {
301 case 'a':
302 if (argc < i + 2) {
303 show_usage(argc, argv);
304 return -1;
305 }
306 a = atoi(argv[++i]);
307 cpu_affinities[next_aff++] = a;
308 use_affinity = 1;
309 printf_verbose("Adding CPU %d affinity\n", a);
310 break;
311 case 'c':
312 if (argc < i + 2) {
313 show_usage(argc, argv);
314 return -1;
315 }
316 rduration = atol(argv[++i]);
317 break;
318 case 'd':
319 if (argc < i + 2) {
320 show_usage(argc, argv);
321 return -1;
322 }
323 wdelay = atol(argv[++i]);
324 break;
325 case 'v':
326 verbose_mode = 1;
327 break;
328 }
329 }
330
331 printf_verbose("running test for %lu seconds, %u enqueuers, "
332 "%u dequeuers.\n",
333 duration, nr_enqueuers, nr_dequeuers);
334 printf_verbose("Writer delay : %lu loops.\n", rduration);
335 printf_verbose("Reader duration : %lu loops.\n", wdelay);
336 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
337 "main", pthread_self(), (unsigned long)gettid());
338
339 tid_enqueuer = malloc(sizeof(*tid_enqueuer) * nr_enqueuers);
340 tid_dequeuer = malloc(sizeof(*tid_dequeuer) * nr_dequeuers);
341 count_enqueuer = malloc(2 * sizeof(*count_enqueuer) * nr_enqueuers);
342 count_dequeuer = malloc(2 * sizeof(*count_dequeuer) * nr_dequeuers);
16aa9ee8 343 cds_wfs_init(&s);
a5eb5ffc
MD
344
345 next_aff = 0;
346
347 for (i = 0; i < nr_enqueuers; i++) {
348 err = pthread_create(&tid_enqueuer[i], NULL, thr_enqueuer,
349 &count_enqueuer[2 * i]);
350 if (err != 0)
351 exit(1);
352 }
353 for (i = 0; i < nr_dequeuers; i++) {
354 err = pthread_create(&tid_dequeuer[i], NULL, thr_dequeuer,
355 &count_dequeuer[2 * i]);
356 if (err != 0)
357 exit(1);
358 }
359
5481ddb3 360 cmm_smp_mb();
a5eb5ffc
MD
361
362 test_go = 1;
363
364 for (i = 0; i < duration; i++) {
365 sleep(1);
366 if (verbose_mode)
367 write (1, ".", 1);
368 }
369
370 test_stop = 1;
371
372 for (i = 0; i < nr_enqueuers; i++) {
373 err = pthread_join(tid_enqueuer[i], &tret);
374 if (err != 0)
375 exit(1);
376 tot_enqueues += count_enqueuer[2 * i];
377 tot_successful_enqueues += count_enqueuer[2 * i + 1];
378 }
379 for (i = 0; i < nr_dequeuers; i++) {
380 err = pthread_join(tid_dequeuer[i], &tret);
381 if (err != 0)
382 exit(1);
383 tot_dequeues += count_dequeuer[2 * i];
384 tot_successful_dequeues += count_dequeuer[2 * i + 1];
385 }
386
387 test_end(&s, &end_dequeues);
388
389 printf_verbose("total number of enqueues : %llu, dequeues %llu\n",
390 tot_enqueues, tot_dequeues);
391 printf_verbose("total number of successful enqueues : %llu, "
392 "successful dequeues %llu\n",
393 tot_successful_enqueues, tot_successful_dequeues);
394 printf("SUMMARY %-25s testdur %4lu nr_enqueuers %3u wdelay %6lu "
395 "nr_dequeuers %3u "
396 "rdur %6lu nr_enqueues %12llu nr_dequeues %12llu "
397 "successful enqueues %12llu successful dequeues %12llu "
398 "end_dequeues %llu nr_ops %12llu\n",
399 argv[0], duration, nr_enqueuers, wdelay,
400 nr_dequeuers, rduration, tot_enqueues, tot_dequeues,
401 tot_successful_enqueues,
402 tot_successful_dequeues, end_dequeues,
403 tot_enqueues + tot_dequeues);
404 if (tot_successful_enqueues != tot_successful_dequeues + end_dequeues)
405 printf("WARNING! Discrepancy between nr succ. enqueues %llu vs "
406 "succ. dequeues + end dequeues %llu.\n",
407 tot_successful_enqueues,
408 tot_successful_dequeues + end_dequeues);
409
410 free(count_enqueuer);
411 free(count_dequeuer);
412 free(tid_enqueuer);
413 free(tid_dequeuer);
414 return 0;
415}
This page took 0.045645 seconds and 4 git commands to generate.