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