2 * AioContext multithreading tests
4 * Copyright Red Hat, Inc. 2016
9 * This work is licensed under the terms of the GNU LGPL, version 2 or later.
10 * See the COPYING.LIB file in the top-level directory.
13 #include "qemu/osdep.h"
14 #include "block/aio.h"
15 #include "qemu/coroutine.h"
16 #include "qemu/thread.h"
17 #include "qemu/error-report.h"
20 /* AioContext management */
22 #define NUM_CONTEXTS 5
24 static IOThread *threads[NUM_CONTEXTS];
25 static AioContext *ctx[NUM_CONTEXTS];
26 static __thread int id = -1;
28 static QemuEvent done_event;
30 /* Run a function synchronously on a remote iothread. */
32 typedef struct CtxRunData {
37 static void ctx_run_bh_cb(void *opaque)
39 CtxRunData *data = opaque;
42 qemu_event_set(&done_event);
45 static void ctx_run(int i, QEMUBHFunc *cb, void *opaque)
52 qemu_event_reset(&done_event);
53 aio_bh_schedule_oneshot(ctx[i], ctx_run_bh_cb, &data);
54 qemu_event_wait(&done_event);
57 /* Starting the iothreads. */
59 static void set_id_cb(void *opaque)
66 static void create_aio_contexts(void)
70 for (i = 0; i < NUM_CONTEXTS; i++) {
71 threads[i] = iothread_new();
72 ctx[i] = iothread_get_aio_context(threads[i]);
75 qemu_event_init(&done_event, false);
76 for (i = 0; i < NUM_CONTEXTS; i++) {
77 ctx_run(i, set_id_cb, &i);
81 /* Stopping the iothreads. */
83 static void join_aio_contexts(void)
87 for (i = 0; i < NUM_CONTEXTS; i++) {
88 aio_context_ref(ctx[i]);
90 for (i = 0; i < NUM_CONTEXTS; i++) {
91 iothread_join(threads[i]);
93 for (i = 0; i < NUM_CONTEXTS; i++) {
94 aio_context_unref(ctx[i]);
96 qemu_event_destroy(&done_event);
99 /* Basic test for the stuff above. */
101 static void test_lifecycle(void)
103 create_aio_contexts();
107 /* aio_co_schedule test. */
109 static Coroutine *to_schedule[NUM_CONTEXTS];
111 static bool now_stopping;
113 static int count_retry;
114 static int count_here;
115 static int count_other;
117 static bool schedule_next(int n)
121 co = atomic_xchg(&to_schedule[n], NULL);
123 atomic_inc(&count_retry);
128 atomic_inc(&count_here);
130 atomic_inc(&count_other);
133 aio_co_schedule(ctx[n], co);
137 static void finish_cb(void *opaque)
142 static coroutine_fn void test_multi_co_schedule_entry(void *opaque)
144 g_assert(to_schedule[id] == NULL);
146 while (!atomic_mb_read(&now_stopping)) {
149 n = g_test_rand_int_range(0, NUM_CONTEXTS);
152 atomic_mb_set(&to_schedule[id], qemu_coroutine_self());
153 qemu_coroutine_yield();
154 g_assert(to_schedule[id] == NULL);
159 static void test_multi_co_schedule(int seconds)
163 count_here = count_other = count_retry = 0;
164 now_stopping = false;
166 create_aio_contexts();
167 for (i = 0; i < NUM_CONTEXTS; i++) {
168 Coroutine *co1 = qemu_coroutine_create(test_multi_co_schedule_entry, NULL);
169 aio_co_schedule(ctx[i], co1);
172 g_usleep(seconds * 1000000);
174 atomic_mb_set(&now_stopping, true);
175 for (i = 0; i < NUM_CONTEXTS; i++) {
176 ctx_run(i, finish_cb, NULL);
177 to_schedule[i] = NULL;
181 g_test_message("scheduled %d, queued %d, retry %d, total %d",
182 count_other, count_here, count_retry,
183 count_here + count_other + count_retry);
186 static void test_multi_co_schedule_1(void)
188 test_multi_co_schedule(1);
191 static void test_multi_co_schedule_10(void)
193 test_multi_co_schedule(10);
196 /* CoMutex thread-safety. */
198 static uint32_t atomic_counter;
199 static uint32_t running;
200 static uint32_t counter;
201 static CoMutex comutex;
203 static void coroutine_fn test_multi_co_mutex_entry(void *opaque)
205 while (!atomic_mb_read(&now_stopping)) {
206 qemu_co_mutex_lock(&comutex);
208 qemu_co_mutex_unlock(&comutex);
210 /* Increase atomic_counter *after* releasing the mutex. Otherwise
211 * there is a chance (it happens about 1 in 3 runs) that the iothread
212 * exits before the coroutine is woken up, causing a spurious
215 atomic_inc(&atomic_counter);
217 atomic_dec(&running);
220 static void test_multi_co_mutex(int threads, int seconds)
224 qemu_co_mutex_init(&comutex);
227 now_stopping = false;
229 create_aio_contexts();
230 assert(threads <= NUM_CONTEXTS);
232 for (i = 0; i < threads; i++) {
233 Coroutine *co1 = qemu_coroutine_create(test_multi_co_mutex_entry, NULL);
234 aio_co_schedule(ctx[i], co1);
237 g_usleep(seconds * 1000000);
239 atomic_mb_set(&now_stopping, true);
240 while (running > 0) {
245 g_test_message("%d iterations/second", counter / seconds);
246 g_assert_cmpint(counter, ==, atomic_counter);
249 /* Testing with NUM_CONTEXTS threads focuses on the queue. The mutex however
250 * is too contended (and the threads spend too much time in aio_poll)
251 * to actually stress the handoff protocol.
253 static void test_multi_co_mutex_1(void)
255 test_multi_co_mutex(NUM_CONTEXTS, 1);
258 static void test_multi_co_mutex_10(void)
260 test_multi_co_mutex(NUM_CONTEXTS, 10);
263 /* Testing with fewer threads stresses the handoff protocol too. Still, the
264 * case where the locker _can_ pick up a handoff is very rare, happening
265 * about 10 times in 1 million, so increase the runtime a bit compared to
266 * other "quick" testcases that only run for 1 second.
268 static void test_multi_co_mutex_2_3(void)
270 test_multi_co_mutex(2, 3);
273 static void test_multi_co_mutex_2_30(void)
275 test_multi_co_mutex(2, 30);
278 /* Same test with fair mutexes, for performance comparison. */
281 #include "qemu/futex.h"
283 /* The nodes for the mutex reside in this structure (on which we try to avoid
284 * false sharing). The head of the mutex is in the "mutex_head" variable.
289 } nodes[NUM_CONTEXTS] __attribute__((__aligned__(64)));
291 static int mutex_head = -1;
293 static void mcs_mutex_lock(void)
298 nodes[id].locked = 1;
299 prev = atomic_xchg(&mutex_head, id);
301 atomic_set(&nodes[prev].next, id);
302 qemu_futex_wait(&nodes[id].locked, 1);
306 static void mcs_mutex_unlock(void)
309 if (atomic_read(&nodes[id].next) == -1) {
310 if (atomic_read(&mutex_head) == id &&
311 atomic_cmpxchg(&mutex_head, id, -1) == id) {
312 /* Last item in the list, exit. */
315 while (atomic_read(&nodes[id].next) == -1) {
316 /* mcs_mutex_lock did the xchg, but has not updated
317 * nodes[prev].next yet.
322 /* Wake up the next in line. */
323 next = atomic_read(&nodes[id].next);
324 nodes[next].locked = 0;
325 qemu_futex_wake(&nodes[next].locked, 1);
328 static void test_multi_fair_mutex_entry(void *opaque)
330 while (!atomic_mb_read(&now_stopping)) {
334 atomic_inc(&atomic_counter);
336 atomic_dec(&running);
339 static void test_multi_fair_mutex(int threads, int seconds)
343 assert(mutex_head == -1);
346 now_stopping = false;
348 create_aio_contexts();
349 assert(threads <= NUM_CONTEXTS);
351 for (i = 0; i < threads; i++) {
352 Coroutine *co1 = qemu_coroutine_create(test_multi_fair_mutex_entry, NULL);
353 aio_co_schedule(ctx[i], co1);
356 g_usleep(seconds * 1000000);
358 atomic_mb_set(&now_stopping, true);
359 while (running > 0) {
364 g_test_message("%d iterations/second", counter / seconds);
365 g_assert_cmpint(counter, ==, atomic_counter);
368 static void test_multi_fair_mutex_1(void)
370 test_multi_fair_mutex(NUM_CONTEXTS, 1);
373 static void test_multi_fair_mutex_10(void)
375 test_multi_fair_mutex(NUM_CONTEXTS, 10);
379 /* Same test with pthread mutexes, for performance comparison and
382 static QemuMutex mutex;
384 static void test_multi_mutex_entry(void *opaque)
386 while (!atomic_mb_read(&now_stopping)) {
387 qemu_mutex_lock(&mutex);
389 qemu_mutex_unlock(&mutex);
390 atomic_inc(&atomic_counter);
392 atomic_dec(&running);
395 static void test_multi_mutex(int threads, int seconds)
399 qemu_mutex_init(&mutex);
402 now_stopping = false;
404 create_aio_contexts();
405 assert(threads <= NUM_CONTEXTS);
407 for (i = 0; i < threads; i++) {
408 Coroutine *co1 = qemu_coroutine_create(test_multi_mutex_entry, NULL);
409 aio_co_schedule(ctx[i], co1);
412 g_usleep(seconds * 1000000);
414 atomic_mb_set(&now_stopping, true);
415 while (running > 0) {
420 g_test_message("%d iterations/second", counter / seconds);
421 g_assert_cmpint(counter, ==, atomic_counter);
424 static void test_multi_mutex_1(void)
426 test_multi_mutex(NUM_CONTEXTS, 1);
429 static void test_multi_mutex_10(void)
431 test_multi_mutex(NUM_CONTEXTS, 10);
436 int main(int argc, char **argv)
440 g_test_init(&argc, &argv, NULL);
441 g_test_add_func("/aio/multi/lifecycle", test_lifecycle);
442 if (g_test_quick()) {
443 g_test_add_func("/aio/multi/schedule", test_multi_co_schedule_1);
444 g_test_add_func("/aio/multi/mutex/contended", test_multi_co_mutex_1);
445 g_test_add_func("/aio/multi/mutex/handoff", test_multi_co_mutex_2_3);
447 g_test_add_func("/aio/multi/mutex/mcs", test_multi_fair_mutex_1);
449 g_test_add_func("/aio/multi/mutex/pthread", test_multi_mutex_1);
451 g_test_add_func("/aio/multi/schedule", test_multi_co_schedule_10);
452 g_test_add_func("/aio/multi/mutex/contended", test_multi_co_mutex_10);
453 g_test_add_func("/aio/multi/mutex/handoff", test_multi_co_mutex_2_30);
455 g_test_add_func("/aio/multi/mutex/mcs", test_multi_fair_mutex_10);
457 g_test_add_func("/aio/multi/mutex/pthread", test_multi_mutex_10);