workqueue: add approximate upper bound to queue length
[userspace-rcu.git] / tests / benchmark / test_urcu_workqueue.c
CommitLineData
1abec5a0
MD
1/*
2 * test_urcu_workqueue.c
3 *
4 * Userspace RCU library - workqueue test
5 *
6 * Copyright February 2010-2014 - 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 <urcu/uatomic.h>
42#include "cpuset.h"
43#include "thread-id.h"
44
45/* hardcoded number of CPUs */
46#define NR_CPUS 16384
47
48#ifndef DYNAMIC_LINK_TEST
49#define _LGPL_SOURCE
50#endif
51#include <urcu.h>
52#include <urcu/wfstack.h>
53#include <urcu/workqueue-fifo.h>
54
1b0a9891 55static volatile int test_go, test_stop_enqueue;
1abec5a0
MD
56
57static unsigned long work_loops;
58
59static unsigned long duration;
60
61static unsigned long dispatch_delay_loops;
62
8a2c74fe
MD
63static unsigned long max_queue_len;
64
1abec5a0
MD
65static inline void loop_sleep(unsigned long loops)
66{
67 while (loops-- != 0)
68 caa_cpu_relax();
69}
70
71static int verbose_mode;
72
73static int test_wait_empty;
74static int test_enqueue_stopped;
75
76#define printf_verbose(fmt, args...) \
77 do { \
78 if (verbose_mode) \
79 fprintf(stderr, fmt, ## args); \
80 } while (0)
81
82static unsigned int cpu_affinities[NR_CPUS];
83static unsigned int next_aff = 0;
84static int use_affinity = 0;
85
86pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
87
88static void set_affinity(void)
89{
90#if HAVE_SCHED_SETAFFINITY
91 cpu_set_t mask;
92 int cpu, ret;
93#endif /* HAVE_SCHED_SETAFFINITY */
94
95 if (!use_affinity)
96 return;
97
98#if HAVE_SCHED_SETAFFINITY
99 ret = pthread_mutex_lock(&affinity_mutex);
100 if (ret) {
101 perror("Error in pthread mutex lock");
102 exit(-1);
103 }
104 cpu = cpu_affinities[next_aff++];
105 ret = pthread_mutex_unlock(&affinity_mutex);
106 if (ret) {
107 perror("Error in pthread mutex unlock");
108 exit(-1);
109 }
110
111 CPU_ZERO(&mask);
112 CPU_SET(cpu, &mask);
113#if SCHED_SETAFFINITY_ARGS == 2
114 sched_setaffinity(0, &mask);
115#else
116 sched_setaffinity(0, sizeof(mask), &mask);
117#endif
118#endif /* HAVE_SCHED_SETAFFINITY */
119}
120
121/*
122 * returns 0 if test should end.
123 */
1abec5a0
MD
124static int test_duration_enqueue(void)
125{
126 return !test_stop_enqueue;
127}
128
129static DEFINE_URCU_TLS(unsigned long long, nr_dequeues);
130static DEFINE_URCU_TLS(unsigned long long, nr_enqueues);
131
132static unsigned int nr_dispatchers;
133static unsigned int nr_workers;
134
135static struct urcu_workqueue workqueue;
136
137struct test_work {
138 struct urcu_work w;
139};
140
141static void *thr_dispatcher(void *_count)
142{
143 unsigned long long *count = _count;
144 bool was_nonempty;
145
146 printf_verbose("thread_begin %s, tid %lu\n",
147 "dispatcher", urcu_get_thread_id());
148
149 set_affinity();
150
151 while (!test_go)
152 {
153 }
154 cmm_smp_mb();
155
156 for (;;) {
157 struct test_work *work = malloc(sizeof(*work));
8a2c74fe
MD
158 enum urcu_enqueue_ret ret;
159
1abec5a0
MD
160 if (!work)
161 goto fail;
8a2c74fe
MD
162retry:
163 printf_verbose("attempt queue work %p\n", work);
164 ret = urcu_queue_work(&workqueue, &work->w);
165 if (ret == URCU_ENQUEUE_FULL) {
166 printf_verbose("queue work %p (queue full)\n", work);
167 (void) poll(NULL, 0, 10);
168 goto retry;
169 }
170 printf_verbose("queue work %p (ok)\n", work);
1abec5a0
MD
171 URCU_TLS(nr_enqueues)++;
172
173 if (caa_unlikely(dispatch_delay_loops))
174 loop_sleep(dispatch_delay_loops);
175fail:
176 if (caa_unlikely(!test_duration_enqueue()))
177 break;
178 }
179
180 uatomic_inc(&test_enqueue_stopped);
181 count[0] = URCU_TLS(nr_enqueues);
182 printf_verbose("dispatcher thread_end, tid %lu, "
183 "enqueues %llu\n",
184 urcu_get_thread_id(),
185 URCU_TLS(nr_enqueues));
186 return ((void*)1);
187}
188
189static void *thr_worker(void *_count)
190{
191 unsigned long long *count = _count;
192 unsigned int counter = 0;
193 struct urcu_worker worker;
1abec5a0
MD
194
195 printf_verbose("thread_begin %s, tid %lu\n",
196 "worker", urcu_get_thread_id());
197
198 set_affinity();
199
200 rcu_register_thread();
8a2c74fe
MD
201 urcu_worker_init(&workqueue, &worker, URCU_WORKER_STEAL);
202 //urcu_worker_init(&workqueue, &worker, 0);
1abec5a0
MD
203 urcu_worker_register(&workqueue, &worker);
204
205 while (!test_go)
206 {
207 }
208 cmm_smp_mb();
209
210 for (;;) {
1b0a9891 211 enum urcu_accept_ret ret;
1abec5a0 212
8a2c74fe 213 ret = urcu_accept_work(&worker);
1b0a9891
MD
214 if (ret == URCU_ACCEPT_SHUTDOWN)
215 break;
1abec5a0
MD
216 for (;;) {
217 struct urcu_work *work;
218 struct test_work *t;
219
220 work = urcu_dequeue_work(&worker);
221 if (!work)
222 break;
223 t = caa_container_of(work, struct test_work, w);
224 printf_verbose("dequeue work %p\n", t);
1abec5a0
MD
225 URCU_TLS(nr_dequeues)++;
226 if (caa_unlikely(work_loops))
227 loop_sleep(work_loops);
228 free(t);
229 }
1abec5a0
MD
230 }
231end:
232 urcu_worker_unregister(&workqueue, &worker);
233 rcu_unregister_thread();
234
235 printf_verbose("worker thread_end, tid %lu, "
236 "dequeues %llu\n",
237 urcu_get_thread_id(),
238 URCU_TLS(nr_dequeues));
239 count[0] = URCU_TLS(nr_dequeues);
240 return ((void*)2);
241}
242
243static void show_usage(int argc, char **argv)
244{
245 printf("Usage : %s nr_workers nr_dispatchers duration (s) <OPTIONS>\n",
246 argv[0]);
247 printf("OPTIONS:\n");
248 printf(" [-d delay] (dispatcher period (in loops))\n");
249 printf(" [-c duration] (worker period (in loops))\n");
250 printf(" [-v] (verbose output)\n");
251 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
252 printf(" [-w] Wait for worker to empty stack\n");
8a2c74fe 253 printf(" [-m len] (Max queue length. 0 means infinite.))\n");
1abec5a0
MD
254 printf("\n");
255}
256
257int main(int argc, char **argv)
258{
259 int err;
260 pthread_t *tid_dispatcher, *tid_worker;
261 void *tret;
262 unsigned long long *count_dispatcher, *count_worker;
263 unsigned long long tot_enqueues = 0, tot_dequeues = 0;
264 unsigned long long end_dequeues = 0;
265 int i, a, retval = 0;
266
267 if (argc < 4) {
268 show_usage(argc, argv);
269 return -1;
270 }
271
272 err = sscanf(argv[1], "%u", &nr_workers);
273 if (err != 1) {
274 show_usage(argc, argv);
275 return -1;
276 }
277
278 err = sscanf(argv[2], "%u", &nr_dispatchers);
279 if (err != 1) {
280 show_usage(argc, argv);
281 return -1;
282 }
283
284 err = sscanf(argv[3], "%lu", &duration);
285 if (err != 1) {
286 show_usage(argc, argv);
287 return -1;
288 }
289
290 for (i = 4; i < argc; i++) {
291 if (argv[i][0] != '-')
292 continue;
293 switch (argv[i][1]) {
294 case 'a':
295 if (argc < i + 2) {
296 show_usage(argc, argv);
297 return -1;
298 }
299 a = atoi(argv[++i]);
300 cpu_affinities[next_aff++] = a;
301 use_affinity = 1;
302 printf_verbose("Adding CPU %d affinity\n", a);
303 break;
8a2c74fe
MD
304 case 'm':
305 if (argc < i + 2) {
306 show_usage(argc, argv);
307 return -1;
308 }
309 max_queue_len = atol(argv[++i]);
310 break;
1abec5a0
MD
311 case 'c':
312 if (argc < i + 2) {
313 show_usage(argc, argv);
314 return -1;
315 }
316 work_loops = atol(argv[++i]);
317 break;
318 case 'd':
319 if (argc < i + 2) {
320 show_usage(argc, argv);
321 return -1;
322 }
323 dispatch_delay_loops = atol(argv[++i]);
324 break;
325 case 'v':
326 verbose_mode = 1;
327 break;
328 case 'w':
329 test_wait_empty = 1;
330 break;
331 }
332 }
333
334 printf_verbose("running test for %lu seconds, %u dispatchers, "
335 "%u workers.\n",
336 duration, nr_dispatchers, nr_workers);
337 if (test_wait_empty)
338 printf_verbose("Wait for workers to empty workqueue.\n");
339 printf_verbose("Work duration: %lu loops.\n", work_loops);
340 printf_verbose("Dispatcher arrival delay: %lu loops.\n", dispatch_delay_loops);
341 printf_verbose("thread %-6s, tid %lu\n",
342 "main", urcu_get_thread_id());
343
344 tid_dispatcher = calloc(nr_dispatchers, sizeof(*tid_dispatcher));
345 tid_worker = calloc(nr_workers, sizeof(*tid_worker));
346 count_dispatcher = calloc(nr_dispatchers, sizeof(*count_dispatcher));
347 count_worker = calloc(nr_workers, sizeof(*count_worker));
8a2c74fe 348 urcu_workqueue_init(&workqueue, max_queue_len);
1abec5a0
MD
349
350 next_aff = 0;
351
352 for (i = 0; i < nr_dispatchers; i++) {
353 err = pthread_create(&tid_dispatcher[i], NULL, thr_dispatcher,
354 &count_dispatcher[i]);
355 if (err != 0)
356 exit(1);
357 }
358 for (i = 0; i < nr_workers; i++) {
359 err = pthread_create(&tid_worker[i], NULL, thr_worker,
360 &count_worker[i]);
361 if (err != 0)
362 exit(1);
363 }
364
365 cmm_smp_mb();
366
367 test_go = 1;
368
369 for (i = 0; i < duration; i++) {
370 sleep(1);
371 if (verbose_mode)
372 (void) write(1, ".", 1);
373 }
374
375 test_stop_enqueue = 1;
376 while (nr_dispatchers != uatomic_read(&test_enqueue_stopped)) {
377 sleep(1);
378 }
379
380 if (test_wait_empty) {
381 while (!cds_wfcq_empty(&workqueue.head, &workqueue.tail)) {
382 sleep(1);
383 }
384 }
1b0a9891 385 urcu_workqueue_shutdown(&workqueue);
1abec5a0
MD
386
387 for (i = 0; i < nr_dispatchers; i++) {
388 err = pthread_join(tid_dispatcher[i], &tret);
389 if (err != 0)
390 exit(1);
391 tot_enqueues += count_dispatcher[i];
392 }
393 for (i = 0; i < nr_workers; i++) {
394 err = pthread_join(tid_worker[i], &tret);
395 if (err != 0)
396 exit(1);
397 tot_dequeues += count_worker[i];
398 }
399
400 printf("SUMMARY %-25s testdur %4lu nr_dispatchers %3u dispatch_delay_loops %6lu "
401 "work_loops %lu nr_workers %3u "
8a2c74fe 402 "nr_enqueues %12llu nr_dequeues %12llu max_queue_len %lu\n",
1abec5a0 403 argv[0], duration, nr_dispatchers, dispatch_delay_loops, work_loops,
8a2c74fe 404 nr_workers, tot_enqueues, tot_dequeues, max_queue_len);
1abec5a0
MD
405 free(count_dispatcher);
406 free(count_worker);
407 free(tid_dispatcher);
408 free(tid_worker);
409 return retval;
410}
This page took 0.037783 seconds and 4 git commands to generate.