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_work_done
);
132 static DEFINE_URCU_TLS(unsigned long long, nr_incoming
);
133 static DEFINE_URCU_TLS(unsigned long long, nr_discard
);
135 static unsigned int nr_dispatchers
;
136 static unsigned int nr_workers
;
138 static struct urcu_workqueue workqueue
;
145 void discard_queue(struct urcu_workqueue
*queue
)
147 struct urcu_worker dummy_worker
;
149 urcu_worker_init(queue
, &dummy_worker
);
150 if (!urcu_workqueue_steal_all(queue
, &dummy_worker
))
153 struct urcu_work
*work
;
156 work
= urcu_dequeue_work(&dummy_worker
);
159 t
= caa_container_of(work
, struct test_work
, w
);
160 printf_verbose("discard work %p\n", t
);
161 URCU_TLS(nr_discard
)++;
166 static void *thr_dispatcher(void *_count
)
168 unsigned long long *count
= _count
;
171 printf_verbose("thread_begin %s, tid %lu\n",
172 "dispatcher", urcu_get_thread_id());
182 struct test_work
*work
= malloc(sizeof(*work
));
183 enum urcu_enqueue_ret ret
;
187 printf_verbose("incoming work %p\n", work
);
188 URCU_TLS(nr_incoming
)++;
189 ret
= urcu_queue_work(&workqueue
, &work
->w
);
190 if (ret
== URCU_ENQUEUE_FULL
) {
191 printf_verbose("queue work %p (queue full)\n", work
);
192 printf_verbose("discard work %p\n", work
);
193 URCU_TLS(nr_discard
)++;
195 discard_queue(&workqueue
);
198 printf_verbose("queue work %p (ok)\n", work
);
200 if (caa_unlikely(dispatch_delay_loops
))
201 loop_sleep(dispatch_delay_loops
);
203 if (caa_unlikely(!test_duration_enqueue()))
207 uatomic_inc(&test_enqueue_stopped
);
208 count
[0] = URCU_TLS(nr_incoming
);
209 count
[1] = URCU_TLS(nr_discard
);
210 printf_verbose("dispatcher thread_end, tid %lu, "
211 "incoming %llu discard %llu\n",
212 urcu_get_thread_id(),
213 URCU_TLS(nr_incoming
),
214 URCU_TLS(nr_discard
));
218 static void *thr_worker(void *_count
)
220 unsigned long long *count
= _count
;
221 unsigned int counter
= 0;
222 struct urcu_worker worker
;
224 printf_verbose("thread_begin %s, tid %lu\n",
225 "worker", urcu_get_thread_id());
229 rcu_register_thread();
230 urcu_worker_init(&workqueue
, &worker
);
231 urcu_worker_register(&workqueue
, &worker
);
239 enum urcu_accept_ret ret
;
241 ret
= urcu_accept_work(&worker
);
242 if (ret
== URCU_ACCEPT_SHUTDOWN
)
245 struct urcu_work
*work
;
248 work
= urcu_dequeue_work(&worker
);
251 t
= caa_container_of(work
, struct test_work
, w
);
252 printf_verbose("dequeue work %p\n", t
);
253 URCU_TLS(nr_work_done
)++;
254 if (caa_unlikely(work_loops
))
255 loop_sleep(work_loops
);
260 urcu_worker_unregister(&workqueue
, &worker
);
261 rcu_unregister_thread();
263 printf_verbose("worker thread_end, tid %lu, "
265 urcu_get_thread_id(),
266 URCU_TLS(nr_work_done
));
267 count
[0] = URCU_TLS(nr_work_done
);
271 static void show_usage(int argc
, char **argv
)
273 printf("Usage : %s nr_workers nr_dispatchers duration (s) <OPTIONS>\n",
275 printf("OPTIONS:\n");
276 printf(" [-d delay] (dispatcher period (in loops))\n");
277 printf(" [-c duration] (worker period (in loops))\n");
278 printf(" [-v] (verbose output)\n");
279 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
280 printf(" [-w] Wait for worker to empty stack\n");
281 printf(" [-m len] (Max queue length. 0 means infinite.))\n");
282 printf(" [-s] (Enable work-stealing between workers.))\n");
286 int main(int argc
, char **argv
)
289 pthread_t
*tid_dispatcher
, *tid_worker
;
291 unsigned long long *count_dispatcher
, *count_worker
;
292 unsigned long long tot_incoming
= 0, tot_work_done
= 0, tot_discard
= 0;
293 int i
, a
, retval
= 0;
294 int worker_flags
= 0;
297 show_usage(argc
, argv
);
301 err
= sscanf(argv
[1], "%u", &nr_workers
);
303 show_usage(argc
, argv
);
307 err
= sscanf(argv
[2], "%u", &nr_dispatchers
);
309 show_usage(argc
, argv
);
313 err
= sscanf(argv
[3], "%lu", &duration
);
315 show_usage(argc
, argv
);
319 for (i
= 4; i
< argc
; i
++) {
320 if (argv
[i
][0] != '-')
322 switch (argv
[i
][1]) {
325 show_usage(argc
, argv
);
329 cpu_affinities
[next_aff
++] = a
;
331 printf_verbose("Adding CPU %d affinity\n", a
);
335 show_usage(argc
, argv
);
338 max_queue_len
= atol(argv
[++i
]);
342 show_usage(argc
, argv
);
345 work_loops
= atol(argv
[++i
]);
349 show_usage(argc
, argv
);
352 dispatch_delay_loops
= atol(argv
[++i
]);
366 printf_verbose("running test for %lu seconds, %u dispatchers, "
368 duration
, nr_dispatchers
, nr_workers
);
370 printf_verbose("Wait for workers to empty workqueue.\n");
371 printf_verbose("Work duration: %lu loops.\n", work_loops
);
372 printf_verbose("Dispatcher arrival delay: %lu loops.\n", dispatch_delay_loops
);
373 printf_verbose("thread %-6s, tid %lu\n",
374 "main", urcu_get_thread_id());
376 tid_dispatcher
= calloc(nr_dispatchers
, sizeof(*tid_dispatcher
));
377 tid_worker
= calloc(nr_workers
, sizeof(*tid_worker
));
378 count_dispatcher
= calloc(nr_dispatchers
,
379 2 * sizeof(*count_dispatcher
));
380 count_worker
= calloc(nr_workers
, sizeof(*count_worker
));
382 worker_flags
|= URCU_WORKER_STEAL
;
383 urcu_workqueue_init(&workqueue
, max_queue_len
, worker_flags
);
387 for (i
= 0; i
< nr_dispatchers
; i
++) {
388 err
= pthread_create(&tid_dispatcher
[i
], NULL
, thr_dispatcher
,
389 &count_dispatcher
[2 * i
]);
393 for (i
= 0; i
< nr_workers
; i
++) {
394 err
= pthread_create(&tid_worker
[i
], NULL
, thr_worker
,
404 for (i
= 0; i
< duration
; i
++) {
407 (void) write(1, ".", 1);
410 test_stop_enqueue
= 1;
411 while (nr_dispatchers
!= uatomic_read(&test_enqueue_stopped
)) {
415 if (test_wait_empty
) {
416 while (!cds_wfcq_empty(&workqueue
.head
, &workqueue
.tail
)) {
420 urcu_workqueue_shutdown(&workqueue
);
422 for (i
= 0; i
< nr_dispatchers
; i
++) {
423 err
= pthread_join(tid_dispatcher
[i
], &tret
);
426 tot_incoming
+= count_dispatcher
[2 * i
];
427 tot_discard
+= count_dispatcher
[(2 * i
) + 1];
429 for (i
= 0; i
< nr_workers
; i
++) {
430 err
= pthread_join(tid_worker
[i
], &tret
);
433 tot_work_done
+= count_worker
[i
];
436 printf("SUMMARY %-25s testdur %4lu nr_dispatchers %3u dispatch_delay_loops %6lu "
437 "work_loops %lu nr_workers %3u "
438 "nr_incoming %12llu nr_work_done %12llu nr_discard %12llu "
439 "max_queue_len %lu work_stealing %s\n",
440 argv
[0], duration
, nr_dispatchers
, dispatch_delay_loops
, work_loops
,
441 nr_workers
, tot_incoming
, tot_work_done
, tot_discard
,
442 max_queue_len
, test_steal
? "enabled" : "disabled");
443 if (nr_incoming
!= nr_work_done
+ nr_discard
) {
444 printf("ERROR: nr_incoming does not match sum of work done and discard.\n");
447 free(count_dispatcher
);
449 free(tid_dispatcher
);