2 * test_urcu_workqueue.c
4 * Userspace RCU library - workqueue test
6 * Copyright February 2010-2014 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 * Copyright February 2010 - Paolo Bonzini <pbonzini@redhat.com>
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.
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.
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.
32 #include <sys/types.h>
39 #include <urcu/arch.h>
40 #include <urcu/tls-compat.h>
41 #include <urcu/uatomic.h>
43 #include "thread-id.h"
45 /* hardcoded number of CPUs */
48 #ifndef DYNAMIC_LINK_TEST
52 #include <urcu/wfstack.h>
53 #include <urcu/workqueue-fifo.h>
55 static volatile int test_go
, test_stop_enqueue
;
57 static unsigned long work_loops
;
59 static unsigned long duration
;
61 static unsigned long dispatch_delay_loops
;
63 static unsigned long max_queue_len
;
65 static int test_steal
;
67 static inline void loop_sleep(unsigned long loops
)
73 static int verbose_mode
;
75 static int test_wait_empty
;
76 static int test_enqueue_stopped
;
78 #define printf_verbose(fmt, args...) \
81 fprintf(stderr, fmt, ## args); \
84 static unsigned int cpu_affinities
[NR_CPUS
];
85 static unsigned int next_aff
= 0;
86 static int use_affinity
= 0;
88 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
90 static void set_affinity(void)
92 #if HAVE_SCHED_SETAFFINITY
95 #endif /* HAVE_SCHED_SETAFFINITY */
100 #if HAVE_SCHED_SETAFFINITY
101 ret
= pthread_mutex_lock(&affinity_mutex
);
103 perror("Error in pthread mutex lock");
106 cpu
= cpu_affinities
[next_aff
++];
107 ret
= pthread_mutex_unlock(&affinity_mutex
);
109 perror("Error in pthread mutex unlock");
115 #if SCHED_SETAFFINITY_ARGS == 2
116 sched_setaffinity(0, &mask
);
118 sched_setaffinity(0, sizeof(mask
), &mask
);
120 #endif /* HAVE_SCHED_SETAFFINITY */
124 * returns 0 if test should end.
126 static int test_duration_enqueue(void)
128 return !test_stop_enqueue
;
131 static DEFINE_URCU_TLS(unsigned long long, nr_dequeues
);
132 static DEFINE_URCU_TLS(unsigned long long, nr_enqueues
);
134 static unsigned int nr_dispatchers
;
135 static unsigned int nr_workers
;
137 static struct urcu_workqueue workqueue
;
143 static void *thr_dispatcher(void *_count
)
145 unsigned long long *count
= _count
;
148 printf_verbose("thread_begin %s, tid %lu\n",
149 "dispatcher", urcu_get_thread_id());
159 struct test_work
*work
= malloc(sizeof(*work
));
160 enum urcu_enqueue_ret ret
;
165 printf_verbose("attempt queue work %p\n", work
);
166 ret
= urcu_queue_work(&workqueue
, &work
->w
);
167 if (ret
== URCU_ENQUEUE_FULL
) {
168 printf_verbose("queue work %p (queue full)\n", work
);
169 (void) poll(NULL
, 0, 10);
172 printf_verbose("queue work %p (ok)\n", work
);
173 URCU_TLS(nr_enqueues
)++;
175 if (caa_unlikely(dispatch_delay_loops
))
176 loop_sleep(dispatch_delay_loops
);
178 if (caa_unlikely(!test_duration_enqueue()))
182 uatomic_inc(&test_enqueue_stopped
);
183 count
[0] = URCU_TLS(nr_enqueues
);
184 printf_verbose("dispatcher thread_end, tid %lu, "
186 urcu_get_thread_id(),
187 URCU_TLS(nr_enqueues
));
191 static void *thr_worker(void *_count
)
193 unsigned long long *count
= _count
;
194 unsigned int counter
= 0;
195 struct urcu_worker worker
;
197 printf_verbose("thread_begin %s, tid %lu\n",
198 "worker", urcu_get_thread_id());
202 rcu_register_thread();
203 urcu_worker_init(&workqueue
, &worker
);
204 urcu_worker_register(&workqueue
, &worker
);
212 enum urcu_accept_ret ret
;
214 ret
= urcu_accept_work(&worker
);
215 if (ret
== URCU_ACCEPT_SHUTDOWN
)
218 struct urcu_work
*work
;
221 work
= urcu_dequeue_work(&worker
);
224 t
= caa_container_of(work
, struct test_work
, w
);
225 printf_verbose("dequeue work %p\n", t
);
226 URCU_TLS(nr_dequeues
)++;
227 if (caa_unlikely(work_loops
))
228 loop_sleep(work_loops
);
233 urcu_worker_unregister(&workqueue
, &worker
);
234 rcu_unregister_thread();
236 printf_verbose("worker thread_end, tid %lu, "
238 urcu_get_thread_id(),
239 URCU_TLS(nr_dequeues
));
240 count
[0] = URCU_TLS(nr_dequeues
);
244 static void show_usage(int argc
, char **argv
)
246 printf("Usage : %s nr_workers nr_dispatchers duration (s) <OPTIONS>\n",
248 printf("OPTIONS:\n");
249 printf(" [-d delay] (dispatcher period (in loops))\n");
250 printf(" [-c duration] (worker period (in loops))\n");
251 printf(" [-v] (verbose output)\n");
252 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
253 printf(" [-w] Wait for worker to empty stack\n");
254 printf(" [-m len] (Max queue length. 0 means infinite.))\n");
255 printf(" [-s] (Enable work-stealing between workers.))\n");
259 int main(int argc
, char **argv
)
262 pthread_t
*tid_dispatcher
, *tid_worker
;
264 unsigned long long *count_dispatcher
, *count_worker
;
265 unsigned long long tot_enqueues
= 0, tot_dequeues
= 0;
266 unsigned long long end_dequeues
= 0;
267 int i
, a
, retval
= 0;
268 int worker_flags
= 0;
271 show_usage(argc
, argv
);
275 err
= sscanf(argv
[1], "%u", &nr_workers
);
277 show_usage(argc
, argv
);
281 err
= sscanf(argv
[2], "%u", &nr_dispatchers
);
283 show_usage(argc
, argv
);
287 err
= sscanf(argv
[3], "%lu", &duration
);
289 show_usage(argc
, argv
);
293 for (i
= 4; i
< argc
; i
++) {
294 if (argv
[i
][0] != '-')
296 switch (argv
[i
][1]) {
299 show_usage(argc
, argv
);
303 cpu_affinities
[next_aff
++] = a
;
305 printf_verbose("Adding CPU %d affinity\n", a
);
309 show_usage(argc
, argv
);
312 max_queue_len
= atol(argv
[++i
]);
316 show_usage(argc
, argv
);
319 work_loops
= atol(argv
[++i
]);
323 show_usage(argc
, argv
);
326 dispatch_delay_loops
= atol(argv
[++i
]);
340 printf_verbose("running test for %lu seconds, %u dispatchers, "
342 duration
, nr_dispatchers
, nr_workers
);
344 printf_verbose("Wait for workers to empty workqueue.\n");
345 printf_verbose("Work duration: %lu loops.\n", work_loops
);
346 printf_verbose("Dispatcher arrival delay: %lu loops.\n", dispatch_delay_loops
);
347 printf_verbose("thread %-6s, tid %lu\n",
348 "main", urcu_get_thread_id());
350 tid_dispatcher
= calloc(nr_dispatchers
, sizeof(*tid_dispatcher
));
351 tid_worker
= calloc(nr_workers
, sizeof(*tid_worker
));
352 count_dispatcher
= calloc(nr_dispatchers
, sizeof(*count_dispatcher
));
353 count_worker
= calloc(nr_workers
, sizeof(*count_worker
));
355 worker_flags
|= URCU_WORKER_STEAL
;
356 urcu_workqueue_init(&workqueue
, max_queue_len
, worker_flags
);
360 for (i
= 0; i
< nr_dispatchers
; i
++) {
361 err
= pthread_create(&tid_dispatcher
[i
], NULL
, thr_dispatcher
,
362 &count_dispatcher
[i
]);
366 for (i
= 0; i
< nr_workers
; i
++) {
367 err
= pthread_create(&tid_worker
[i
], NULL
, thr_worker
,
377 for (i
= 0; i
< duration
; i
++) {
380 (void) write(1, ".", 1);
383 test_stop_enqueue
= 1;
384 while (nr_dispatchers
!= uatomic_read(&test_enqueue_stopped
)) {
388 if (test_wait_empty
) {
389 while (!cds_wfcq_empty(&workqueue
.head
, &workqueue
.tail
)) {
393 urcu_workqueue_shutdown(&workqueue
);
395 for (i
= 0; i
< nr_dispatchers
; i
++) {
396 err
= pthread_join(tid_dispatcher
[i
], &tret
);
399 tot_enqueues
+= count_dispatcher
[i
];
401 for (i
= 0; i
< nr_workers
; i
++) {
402 err
= pthread_join(tid_worker
[i
], &tret
);
405 tot_dequeues
+= count_worker
[i
];
408 printf("SUMMARY %-25s testdur %4lu nr_dispatchers %3u dispatch_delay_loops %6lu "
409 "work_loops %lu nr_workers %3u "
410 "nr_enqueues %12llu nr_dequeues %12llu max_queue_len %lu "
411 "work_stealing %s\n",
412 argv
[0], duration
, nr_dispatchers
, dispatch_delay_loops
, work_loops
,
413 nr_workers
, tot_enqueues
, tot_dequeues
, max_queue_len
,
414 test_steal
? "enabled" : "disabled");
415 free(count_dispatcher
);
417 free(tid_dispatcher
);