tcg/i386: Propagate is64 to tcg_out_qemu_ld_direct
[qemu/ar7.git] / tests / test-aio-multithread.c
blob6440d54ac36c7291463aefc0c0dce10a755afc99
1 /*
2 * AioContext multithreading tests
4 * Copyright Red Hat, Inc. 2016
6 * Authors:
7 * Paolo Bonzini <pbonzini@redhat.com>
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"
18 #include "iothread.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 {
33 QEMUBHFunc *cb;
34 void *arg;
35 } CtxRunData;
37 static void ctx_run_bh_cb(void *opaque)
39 CtxRunData *data = opaque;
41 data->cb(data->arg);
42 qemu_event_set(&done_event);
45 static void ctx_run(int i, QEMUBHFunc *cb, void *opaque)
47 CtxRunData data = {
48 .cb = cb,
49 .arg = 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)
61 int *i = opaque;
63 id = *i;
66 static void create_aio_contexts(void)
68 int i;
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)
85 int i;
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();
104 join_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)
119 Coroutine *co;
121 co = atomic_xchg(&to_schedule[n], NULL);
122 if (!co) {
123 atomic_inc(&count_retry);
124 return false;
127 if (n == id) {
128 atomic_inc(&count_here);
129 } else {
130 atomic_inc(&count_other);
133 aio_co_schedule(ctx[n], co);
134 return true;
137 static void finish_cb(void *opaque)
139 schedule_next(id);
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)) {
147 int n;
149 n = g_test_rand_int_range(0, NUM_CONTEXTS);
150 schedule_next(n);
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)
161 int i;
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;
180 join_aio_contexts();
181 g_test_message("scheduled %d, queued %d, retry %d, total %d\n",
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);
207 counter++;
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
213 * assertion failure.
215 atomic_inc(&atomic_counter);
217 atomic_dec(&running);
220 static void test_multi_co_mutex(int threads, int seconds)
222 int i;
224 qemu_co_mutex_init(&comutex);
225 counter = 0;
226 atomic_counter = 0;
227 now_stopping = false;
229 create_aio_contexts();
230 assert(threads <= NUM_CONTEXTS);
231 running = threads;
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) {
241 g_usleep(100000);
244 join_aio_contexts();
245 g_test_message("%d iterations/second\n", 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. */
280 #ifdef CONFIG_LINUX
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.
286 static struct {
287 int next, locked;
288 int padding[14];
289 } nodes[NUM_CONTEXTS] __attribute__((__aligned__(64)));
291 static int mutex_head = -1;
293 static void mcs_mutex_lock(void)
295 int prev;
297 nodes[id].next = -1;
298 nodes[id].locked = 1;
299 prev = atomic_xchg(&mutex_head, id);
300 if (prev != -1) {
301 atomic_set(&nodes[prev].next, id);
302 qemu_futex_wait(&nodes[id].locked, 1);
306 static void mcs_mutex_unlock(void)
308 int next;
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. */
313 return;
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)) {
331 mcs_mutex_lock();
332 counter++;
333 mcs_mutex_unlock();
334 atomic_inc(&atomic_counter);
336 atomic_dec(&running);
339 static void test_multi_fair_mutex(int threads, int seconds)
341 int i;
343 assert(mutex_head == -1);
344 counter = 0;
345 atomic_counter = 0;
346 now_stopping = false;
348 create_aio_contexts();
349 assert(threads <= NUM_CONTEXTS);
350 running = threads;
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) {
360 g_usleep(100000);
363 join_aio_contexts();
364 g_test_message("%d iterations/second\n", 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);
377 #endif
379 /* Same test with pthread mutexes, for performance comparison and
380 * portability. */
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);
388 counter++;
389 qemu_mutex_unlock(&mutex);
390 atomic_inc(&atomic_counter);
392 atomic_dec(&running);
395 static void test_multi_mutex(int threads, int seconds)
397 int i;
399 qemu_mutex_init(&mutex);
400 counter = 0;
401 atomic_counter = 0;
402 now_stopping = false;
404 create_aio_contexts();
405 assert(threads <= NUM_CONTEXTS);
406 running = threads;
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) {
416 g_usleep(100000);
419 join_aio_contexts();
420 g_test_message("%d iterations/second\n", 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);
434 /* End of tests. */
436 int main(int argc, char **argv)
438 init_clocks(NULL);
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);
446 #ifdef CONFIG_LINUX
447 g_test_add_func("/aio/multi/mutex/mcs", test_multi_fair_mutex_1);
448 #endif
449 g_test_add_func("/aio/multi/mutex/pthread", test_multi_mutex_1);
450 } else {
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);
454 #ifdef CONFIG_LINUX
455 g_test_add_func("/aio/multi/mutex/mcs", test_multi_fair_mutex_10);
456 #endif
457 g_test_add_func("/aio/multi/mutex/pthread", test_multi_mutex_10);
459 return g_test_run();