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