eabc977973
This patch rewrites the ctx->dispatching optimization, which was the cause of some mysterious hangs that could be reproduced on aarch64 KVM only. The hangs were indirectly caused by aio_poll() and in particular by flash memory updates's call to blk_write(), which invokes aio_poll(). Fun stuff: they had an extremely short race window, so much that adding all kind of tracing to either the kernel or QEMU made it go away (a single printf made it half as reproducible). On the plus side, the failure mode (a hang until the next keypress) made it very easy to examine the state of the process with a debugger. And there was a very nice reproducer from Laszlo, which failed pretty often (more than half of the time) on any version of QEMU with a non-debug kernel; it also failed fast, while still in the firmware. So, it could have been worse. For some unknown reason they happened only with virtio-scsi, but that's not important. It's more interesting that they disappeared with io=native, making thread-pool.c a likely suspect for where the bug arose. thread-pool.c is also one of the few places which use bottom halves across threads, by the way. I hope that no other similar bugs exist, but just in case :) I am going to describe how the successful debugging went... Since the likely culprit was the ctx->dispatching optimization, which mostly affects bottom halves, the first observation was that there are two qemu_bh_schedule() invocations in the thread pool: the one in the aio worker and the one in thread_pool_completion_bh. The latter always causes the optimization to trigger, the former may or may not. In order to restrict the possibilities, I introduced new functions qemu_bh_schedule_slow() and qemu_bh_schedule_fast(): /* qemu_bh_schedule_slow: */ ctx = bh->ctx; bh->idle = 0; if (atomic_xchg(&bh->scheduled, 1) == 0) { event_notifier_set(&ctx->notifier); } /* qemu_bh_schedule_fast: */ ctx = bh->ctx; bh->idle = 0; assert(ctx->dispatching); atomic_xchg(&bh->scheduled, 1); Notice how the atomic_xchg is still in qemu_bh_schedule_slow(). This was already debated a few months ago, so I assumed it to be correct. In retrospect this was a very good idea, as you'll see later. Changing thread_pool_completion_bh() to qemu_bh_schedule_fast() didn't trigger the assertion (as expected). Changing the worker's invocation to qemu_bh_schedule_slow() didn't hide the bug (another assumption which luckily held). This already limited heavily the amount of interaction between the threads, hinting that the problematic events must have triggered around thread_pool_completion_bh(). As mentioned early, invoking a debugger to examine the state of a hung process was pretty easy; the iothread was always waiting on a poll(..., -1) system call. Infinite timeouts are much rarer on x86, and this could be the reason why the bug was never observed there. With the buggy sequence more or less resolved to an interaction between thread_pool_completion_bh() and poll(..., -1), my "tracing" strategy was to just add a few qemu_clock_get_ns(QEMU_CLOCK_REALTIME) calls, hoping that the ordering of aio_ctx_prepare(), aio_ctx_dispatch, poll() and qemu_bh_schedule_fast() would provide some hint. The output was: (gdb) p last_prepare $3 = 103885451 (gdb) p last_dispatch $4 = 103876492 (gdb) p last_poll $5 = 115909333 (gdb) p last_schedule $6 = 115925212 Notice how the last call to qemu_poll_ns() came after aio_ctx_dispatch(). This makes little sense unless there is an aio_poll() call involved, and indeed with a slightly different instrumentation you can see that there is one: (gdb) p last_prepare $3 = 107569679 (gdb) p last_dispatch $4 = 107561600 (gdb) p last_aio_poll $5 = 110671400 (gdb) p last_schedule $6 = 110698917 So the scenario becomes clearer: iothread VCPU thread -------------------------------------------------------------------------- aio_ctx_prepare aio_ctx_check qemu_poll_ns(timeout=-1) aio_poll aio_dispatch thread_pool_completion_bh qemu_bh_schedule() At this point bh->scheduled = 1 and the iothread has not been woken up. The solution must be close, but this alone should not be a problem, because the bottom half is only rescheduled to account for rare situations (see commit 3c80ca1, thread-pool: avoid deadlock in nested aio_poll() calls, 2014-07-15). Introducing a third thread---a thread pool worker thread, which also does qemu_bh_schedule()---does bring out the problematic case. The third thread must be awakened *after* the callback is complete and thread_pool_completion_bh has redone the whole loop, explaining the short race window. And then this is what happens: thread pool worker -------------------------------------------------------------------------- <I/O completes> qemu_bh_schedule() Tada, bh->scheduled is already 1, so qemu_bh_schedule() does nothing and the iothread is never woken up. This is where the bh->scheduled optimization comes into play---it is correct, but removing it would have masked the bug. So, what is the bug? Well, the question asked by the ctx->dispatching optimization ("is any active aio_poll dispatching?") was wrong. The right question to ask instead is "is any active aio_poll *not* dispatching", i.e. in the prepare or poll phases? In that case, the aio_poll is sleeping or might go to sleep anytime soon, and the EventNotifier must be invoked to wake it up. In any other case (including if there is *no* active aio_poll at all!) we can just wait for the next prepare phase to pick up the event (e.g. a bottom half); the prepare phase will avoid the blocking and service the bottom half. Expressing the invariant with a logic formula, the broken one looked like: !(exists(thread): in_dispatching(thread)) => !optimize or equivalently: !(exists(thread): in_aio_poll(thread) && in_dispatching(thread)) => !optimize In the correct one, the negation is in a slightly different place: (exists(thread): in_aio_poll(thread) && !in_dispatching(thread)) => !optimize or equivalently: (exists(thread): in_prepare_or_poll(thread)) => !optimize Even if the difference boils down to moving an exclamation mark :) the implementation is quite different. However, I think the new one is simpler to understand. In the old implementation, the "exists" was implemented with a boolean value. This didn't really support well the case of multiple concurrent event loops, but I thought that this was okay: aio_poll holds the AioContext lock so there cannot be concurrent aio_poll invocations, and I was just considering nested event loops. However, aio_poll _could_ indeed be concurrent with the GSource. This is why I came up with the wrong invariant. In the new implementation, "exists" is computed simply by counting how many threads are in the prepare or poll phases. There are some interesting points to consider, but the gist of the idea remains: 1) AioContext can be used through GSource as well; as mentioned in the patch, bit 0 of the counter is reserved for the GSource. 2) the counter need not be updated for a non-blocking aio_poll, because it won't sleep forever anyway. This is just a matter of checking the "blocking" variable. This requires some changes to the win32 implementation, but is otherwise not too complicated. 3) as mentioned above, the new implementation will not call aio_notify when there is *no* active aio_poll at all. The tests have to be adjusted for this change. The calls to aio_notify in async.c are fine; they only want to kick aio_poll out of a blocking wait, but need not do anything if aio_poll is not running. 4) nested aio_poll: these just work with the new implementation; when a nested event loop is invoked, the outer event loop is never in the prepare or poll phases. The outer event loop thus has already decremented the counter. Reported-by: Richard W. M. Jones <rjones@redhat.com> Reported-by: Laszlo Ersek <lersek@redhat.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> Reviewed-by: Fam Zheng <famz@redhat.com> Tested-by: Richard W.M. Jones <rjones@redhat.com> Message-id: 1437487673-23740-5-git-send-email-pbonzini@redhat.com Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
846 lines
24 KiB
C
846 lines
24 KiB
C
/*
|
|
* AioContext tests
|
|
*
|
|
* Copyright Red Hat, Inc. 2012
|
|
*
|
|
* Authors:
|
|
* Paolo Bonzini <pbonzini@redhat.com>
|
|
*
|
|
* This work is licensed under the terms of the GNU LGPL, version 2 or later.
|
|
* See the COPYING.LIB file in the top-level directory.
|
|
*/
|
|
|
|
#include <glib.h>
|
|
#include "block/aio.h"
|
|
#include "qemu/timer.h"
|
|
#include "qemu/sockets.h"
|
|
#include "qemu/error-report.h"
|
|
|
|
static AioContext *ctx;
|
|
|
|
typedef struct {
|
|
EventNotifier e;
|
|
int n;
|
|
int active;
|
|
bool auto_set;
|
|
} EventNotifierTestData;
|
|
|
|
/* Wait until event notifier becomes inactive */
|
|
static void wait_until_inactive(EventNotifierTestData *data)
|
|
{
|
|
while (data->active > 0) {
|
|
aio_poll(ctx, true);
|
|
}
|
|
}
|
|
|
|
/* Simple callbacks for testing. */
|
|
|
|
typedef struct {
|
|
QEMUBH *bh;
|
|
int n;
|
|
int max;
|
|
} BHTestData;
|
|
|
|
typedef struct {
|
|
QEMUTimer timer;
|
|
QEMUClockType clock_type;
|
|
int n;
|
|
int max;
|
|
int64_t ns;
|
|
AioContext *ctx;
|
|
} TimerTestData;
|
|
|
|
static void bh_test_cb(void *opaque)
|
|
{
|
|
BHTestData *data = opaque;
|
|
if (++data->n < data->max) {
|
|
qemu_bh_schedule(data->bh);
|
|
}
|
|
}
|
|
|
|
static void timer_test_cb(void *opaque)
|
|
{
|
|
TimerTestData *data = opaque;
|
|
if (++data->n < data->max) {
|
|
timer_mod(&data->timer,
|
|
qemu_clock_get_ns(data->clock_type) + data->ns);
|
|
}
|
|
}
|
|
|
|
static void dummy_io_handler_read(EventNotifier *e)
|
|
{
|
|
}
|
|
|
|
static void bh_delete_cb(void *opaque)
|
|
{
|
|
BHTestData *data = opaque;
|
|
if (++data->n < data->max) {
|
|
qemu_bh_schedule(data->bh);
|
|
} else {
|
|
qemu_bh_delete(data->bh);
|
|
data->bh = NULL;
|
|
}
|
|
}
|
|
|
|
static void event_ready_cb(EventNotifier *e)
|
|
{
|
|
EventNotifierTestData *data = container_of(e, EventNotifierTestData, e);
|
|
g_assert(event_notifier_test_and_clear(e));
|
|
data->n++;
|
|
if (data->active > 0) {
|
|
data->active--;
|
|
}
|
|
if (data->auto_set && data->active) {
|
|
event_notifier_set(e);
|
|
}
|
|
}
|
|
|
|
/* Tests using aio_*. */
|
|
|
|
typedef struct {
|
|
QemuMutex start_lock;
|
|
bool thread_acquired;
|
|
} AcquireTestData;
|
|
|
|
static void *test_acquire_thread(void *opaque)
|
|
{
|
|
AcquireTestData *data = opaque;
|
|
|
|
/* Wait for other thread to let us start */
|
|
qemu_mutex_lock(&data->start_lock);
|
|
qemu_mutex_unlock(&data->start_lock);
|
|
|
|
aio_context_acquire(ctx);
|
|
aio_context_release(ctx);
|
|
|
|
data->thread_acquired = true; /* success, we got here */
|
|
|
|
return NULL;
|
|
}
|
|
|
|
static void dummy_notifier_read(EventNotifier *unused)
|
|
{
|
|
g_assert(false); /* should never be invoked */
|
|
}
|
|
|
|
static void test_acquire(void)
|
|
{
|
|
QemuThread thread;
|
|
EventNotifier notifier;
|
|
AcquireTestData data;
|
|
|
|
/* Dummy event notifier ensures aio_poll() will block */
|
|
event_notifier_init(¬ifier, false);
|
|
aio_set_event_notifier(ctx, ¬ifier, dummy_notifier_read);
|
|
g_assert(!aio_poll(ctx, false)); /* consume aio_notify() */
|
|
|
|
qemu_mutex_init(&data.start_lock);
|
|
qemu_mutex_lock(&data.start_lock);
|
|
data.thread_acquired = false;
|
|
|
|
qemu_thread_create(&thread, "test_acquire_thread",
|
|
test_acquire_thread,
|
|
&data, QEMU_THREAD_JOINABLE);
|
|
|
|
/* Block in aio_poll(), let other thread kick us and acquire context */
|
|
aio_context_acquire(ctx);
|
|
qemu_mutex_unlock(&data.start_lock); /* let the thread run */
|
|
g_assert(!aio_poll(ctx, true));
|
|
aio_context_release(ctx);
|
|
|
|
qemu_thread_join(&thread);
|
|
aio_set_event_notifier(ctx, ¬ifier, NULL);
|
|
event_notifier_cleanup(¬ifier);
|
|
|
|
g_assert(data.thread_acquired);
|
|
}
|
|
|
|
static void test_bh_schedule(void)
|
|
{
|
|
BHTestData data = { .n = 0 };
|
|
data.bh = aio_bh_new(ctx, bh_test_cb, &data);
|
|
|
|
qemu_bh_schedule(data.bh);
|
|
g_assert_cmpint(data.n, ==, 0);
|
|
|
|
g_assert(aio_poll(ctx, true));
|
|
g_assert_cmpint(data.n, ==, 1);
|
|
|
|
g_assert(!aio_poll(ctx, false));
|
|
g_assert_cmpint(data.n, ==, 1);
|
|
qemu_bh_delete(data.bh);
|
|
}
|
|
|
|
static void test_bh_schedule10(void)
|
|
{
|
|
BHTestData data = { .n = 0, .max = 10 };
|
|
data.bh = aio_bh_new(ctx, bh_test_cb, &data);
|
|
|
|
qemu_bh_schedule(data.bh);
|
|
g_assert_cmpint(data.n, ==, 0);
|
|
|
|
g_assert(aio_poll(ctx, false));
|
|
g_assert_cmpint(data.n, ==, 1);
|
|
|
|
g_assert(aio_poll(ctx, true));
|
|
g_assert_cmpint(data.n, ==, 2);
|
|
|
|
while (data.n < 10) {
|
|
aio_poll(ctx, true);
|
|
}
|
|
g_assert_cmpint(data.n, ==, 10);
|
|
|
|
g_assert(!aio_poll(ctx, false));
|
|
g_assert_cmpint(data.n, ==, 10);
|
|
qemu_bh_delete(data.bh);
|
|
}
|
|
|
|
static void test_bh_cancel(void)
|
|
{
|
|
BHTestData data = { .n = 0 };
|
|
data.bh = aio_bh_new(ctx, bh_test_cb, &data);
|
|
|
|
qemu_bh_schedule(data.bh);
|
|
g_assert_cmpint(data.n, ==, 0);
|
|
|
|
qemu_bh_cancel(data.bh);
|
|
g_assert_cmpint(data.n, ==, 0);
|
|
|
|
g_assert(!aio_poll(ctx, false));
|
|
g_assert_cmpint(data.n, ==, 0);
|
|
qemu_bh_delete(data.bh);
|
|
}
|
|
|
|
static void test_bh_delete(void)
|
|
{
|
|
BHTestData data = { .n = 0 };
|
|
data.bh = aio_bh_new(ctx, bh_test_cb, &data);
|
|
|
|
qemu_bh_schedule(data.bh);
|
|
g_assert_cmpint(data.n, ==, 0);
|
|
|
|
qemu_bh_delete(data.bh);
|
|
g_assert_cmpint(data.n, ==, 0);
|
|
|
|
g_assert(!aio_poll(ctx, false));
|
|
g_assert_cmpint(data.n, ==, 0);
|
|
}
|
|
|
|
static void test_bh_delete_from_cb(void)
|
|
{
|
|
BHTestData data1 = { .n = 0, .max = 1 };
|
|
|
|
data1.bh = aio_bh_new(ctx, bh_delete_cb, &data1);
|
|
|
|
qemu_bh_schedule(data1.bh);
|
|
g_assert_cmpint(data1.n, ==, 0);
|
|
|
|
while (data1.n < data1.max) {
|
|
aio_poll(ctx, true);
|
|
}
|
|
g_assert_cmpint(data1.n, ==, data1.max);
|
|
g_assert(data1.bh == NULL);
|
|
|
|
g_assert(!aio_poll(ctx, false));
|
|
}
|
|
|
|
static void test_bh_delete_from_cb_many(void)
|
|
{
|
|
BHTestData data1 = { .n = 0, .max = 1 };
|
|
BHTestData data2 = { .n = 0, .max = 3 };
|
|
BHTestData data3 = { .n = 0, .max = 2 };
|
|
BHTestData data4 = { .n = 0, .max = 4 };
|
|
|
|
data1.bh = aio_bh_new(ctx, bh_delete_cb, &data1);
|
|
data2.bh = aio_bh_new(ctx, bh_delete_cb, &data2);
|
|
data3.bh = aio_bh_new(ctx, bh_delete_cb, &data3);
|
|
data4.bh = aio_bh_new(ctx, bh_delete_cb, &data4);
|
|
|
|
qemu_bh_schedule(data1.bh);
|
|
qemu_bh_schedule(data2.bh);
|
|
qemu_bh_schedule(data3.bh);
|
|
qemu_bh_schedule(data4.bh);
|
|
g_assert_cmpint(data1.n, ==, 0);
|
|
g_assert_cmpint(data2.n, ==, 0);
|
|
g_assert_cmpint(data3.n, ==, 0);
|
|
g_assert_cmpint(data4.n, ==, 0);
|
|
|
|
g_assert(aio_poll(ctx, false));
|
|
g_assert_cmpint(data1.n, ==, 1);
|
|
g_assert_cmpint(data2.n, ==, 1);
|
|
g_assert_cmpint(data3.n, ==, 1);
|
|
g_assert_cmpint(data4.n, ==, 1);
|
|
g_assert(data1.bh == NULL);
|
|
|
|
while (data1.n < data1.max ||
|
|
data2.n < data2.max ||
|
|
data3.n < data3.max ||
|
|
data4.n < data4.max) {
|
|
aio_poll(ctx, true);
|
|
}
|
|
g_assert_cmpint(data1.n, ==, data1.max);
|
|
g_assert_cmpint(data2.n, ==, data2.max);
|
|
g_assert_cmpint(data3.n, ==, data3.max);
|
|
g_assert_cmpint(data4.n, ==, data4.max);
|
|
g_assert(data1.bh == NULL);
|
|
g_assert(data2.bh == NULL);
|
|
g_assert(data3.bh == NULL);
|
|
g_assert(data4.bh == NULL);
|
|
}
|
|
|
|
static void test_bh_flush(void)
|
|
{
|
|
BHTestData data = { .n = 0 };
|
|
data.bh = aio_bh_new(ctx, bh_test_cb, &data);
|
|
|
|
qemu_bh_schedule(data.bh);
|
|
g_assert_cmpint(data.n, ==, 0);
|
|
|
|
g_assert(aio_poll(ctx, true));
|
|
g_assert_cmpint(data.n, ==, 1);
|
|
|
|
g_assert(!aio_poll(ctx, false));
|
|
g_assert_cmpint(data.n, ==, 1);
|
|
qemu_bh_delete(data.bh);
|
|
}
|
|
|
|
static void test_set_event_notifier(void)
|
|
{
|
|
EventNotifierTestData data = { .n = 0, .active = 0 };
|
|
event_notifier_init(&data.e, false);
|
|
aio_set_event_notifier(ctx, &data.e, event_ready_cb);
|
|
g_assert(!aio_poll(ctx, false));
|
|
g_assert_cmpint(data.n, ==, 0);
|
|
|
|
aio_set_event_notifier(ctx, &data.e, NULL);
|
|
g_assert(!aio_poll(ctx, false));
|
|
g_assert_cmpint(data.n, ==, 0);
|
|
event_notifier_cleanup(&data.e);
|
|
}
|
|
|
|
static void test_wait_event_notifier(void)
|
|
{
|
|
EventNotifierTestData data = { .n = 0, .active = 1 };
|
|
event_notifier_init(&data.e, false);
|
|
aio_set_event_notifier(ctx, &data.e, event_ready_cb);
|
|
while (aio_poll(ctx, false));
|
|
g_assert_cmpint(data.n, ==, 0);
|
|
g_assert_cmpint(data.active, ==, 1);
|
|
|
|
event_notifier_set(&data.e);
|
|
g_assert(aio_poll(ctx, false));
|
|
g_assert_cmpint(data.n, ==, 1);
|
|
g_assert_cmpint(data.active, ==, 0);
|
|
|
|
g_assert(!aio_poll(ctx, false));
|
|
g_assert_cmpint(data.n, ==, 1);
|
|
g_assert_cmpint(data.active, ==, 0);
|
|
|
|
aio_set_event_notifier(ctx, &data.e, NULL);
|
|
g_assert(!aio_poll(ctx, false));
|
|
g_assert_cmpint(data.n, ==, 1);
|
|
|
|
event_notifier_cleanup(&data.e);
|
|
}
|
|
|
|
static void test_flush_event_notifier(void)
|
|
{
|
|
EventNotifierTestData data = { .n = 0, .active = 10, .auto_set = true };
|
|
event_notifier_init(&data.e, false);
|
|
aio_set_event_notifier(ctx, &data.e, event_ready_cb);
|
|
while (aio_poll(ctx, false));
|
|
g_assert_cmpint(data.n, ==, 0);
|
|
g_assert_cmpint(data.active, ==, 10);
|
|
|
|
event_notifier_set(&data.e);
|
|
g_assert(aio_poll(ctx, false));
|
|
g_assert_cmpint(data.n, ==, 1);
|
|
g_assert_cmpint(data.active, ==, 9);
|
|
g_assert(aio_poll(ctx, false));
|
|
|
|
wait_until_inactive(&data);
|
|
g_assert_cmpint(data.n, ==, 10);
|
|
g_assert_cmpint(data.active, ==, 0);
|
|
g_assert(!aio_poll(ctx, false));
|
|
|
|
aio_set_event_notifier(ctx, &data.e, NULL);
|
|
g_assert(!aio_poll(ctx, false));
|
|
event_notifier_cleanup(&data.e);
|
|
}
|
|
|
|
static void test_wait_event_notifier_noflush(void)
|
|
{
|
|
EventNotifierTestData data = { .n = 0 };
|
|
EventNotifierTestData dummy = { .n = 0, .active = 1 };
|
|
|
|
event_notifier_init(&data.e, false);
|
|
aio_set_event_notifier(ctx, &data.e, event_ready_cb);
|
|
|
|
g_assert(!aio_poll(ctx, false));
|
|
g_assert_cmpint(data.n, ==, 0);
|
|
|
|
/* Until there is an active descriptor, aio_poll may or may not call
|
|
* event_ready_cb. Still, it must not block. */
|
|
event_notifier_set(&data.e);
|
|
g_assert(aio_poll(ctx, true));
|
|
data.n = 0;
|
|
|
|
/* An active event notifier forces aio_poll to look at EventNotifiers. */
|
|
event_notifier_init(&dummy.e, false);
|
|
aio_set_event_notifier(ctx, &dummy.e, event_ready_cb);
|
|
|
|
event_notifier_set(&data.e);
|
|
g_assert(aio_poll(ctx, false));
|
|
g_assert_cmpint(data.n, ==, 1);
|
|
g_assert(!aio_poll(ctx, false));
|
|
g_assert_cmpint(data.n, ==, 1);
|
|
|
|
event_notifier_set(&data.e);
|
|
g_assert(aio_poll(ctx, false));
|
|
g_assert_cmpint(data.n, ==, 2);
|
|
g_assert(!aio_poll(ctx, false));
|
|
g_assert_cmpint(data.n, ==, 2);
|
|
|
|
event_notifier_set(&dummy.e);
|
|
wait_until_inactive(&dummy);
|
|
g_assert_cmpint(data.n, ==, 2);
|
|
g_assert_cmpint(dummy.n, ==, 1);
|
|
g_assert_cmpint(dummy.active, ==, 0);
|
|
|
|
aio_set_event_notifier(ctx, &dummy.e, NULL);
|
|
event_notifier_cleanup(&dummy.e);
|
|
|
|
aio_set_event_notifier(ctx, &data.e, NULL);
|
|
g_assert(!aio_poll(ctx, false));
|
|
g_assert_cmpint(data.n, ==, 2);
|
|
|
|
event_notifier_cleanup(&data.e);
|
|
}
|
|
|
|
static void test_timer_schedule(void)
|
|
{
|
|
TimerTestData data = { .n = 0, .ctx = ctx, .ns = SCALE_MS * 750LL,
|
|
.max = 2,
|
|
.clock_type = QEMU_CLOCK_VIRTUAL };
|
|
EventNotifier e;
|
|
|
|
/* aio_poll will not block to wait for timers to complete unless it has
|
|
* an fd to wait on. Fixing this breaks other tests. So create a dummy one.
|
|
*/
|
|
event_notifier_init(&e, false);
|
|
aio_set_event_notifier(ctx, &e, dummy_io_handler_read);
|
|
aio_poll(ctx, false);
|
|
|
|
aio_timer_init(ctx, &data.timer, data.clock_type,
|
|
SCALE_NS, timer_test_cb, &data);
|
|
timer_mod(&data.timer,
|
|
qemu_clock_get_ns(data.clock_type) +
|
|
data.ns);
|
|
|
|
g_assert_cmpint(data.n, ==, 0);
|
|
|
|
/* timer_mod may well cause an event notifer to have gone off,
|
|
* so clear that
|
|
*/
|
|
do {} while (aio_poll(ctx, false));
|
|
|
|
g_assert(!aio_poll(ctx, false));
|
|
g_assert_cmpint(data.n, ==, 0);
|
|
|
|
g_usleep(1 * G_USEC_PER_SEC);
|
|
g_assert_cmpint(data.n, ==, 0);
|
|
|
|
g_assert(aio_poll(ctx, false));
|
|
g_assert_cmpint(data.n, ==, 1);
|
|
|
|
/* timer_mod called by our callback */
|
|
do {} while (aio_poll(ctx, false));
|
|
|
|
g_assert(!aio_poll(ctx, false));
|
|
g_assert_cmpint(data.n, ==, 1);
|
|
|
|
g_assert(aio_poll(ctx, true));
|
|
g_assert_cmpint(data.n, ==, 2);
|
|
|
|
/* As max is now 2, an event notifier should not have gone off */
|
|
|
|
g_assert(!aio_poll(ctx, false));
|
|
g_assert_cmpint(data.n, ==, 2);
|
|
|
|
aio_set_event_notifier(ctx, &e, NULL);
|
|
event_notifier_cleanup(&e);
|
|
|
|
timer_del(&data.timer);
|
|
}
|
|
|
|
/* Now the same tests, using the context as a GSource. They are
|
|
* very similar to the ones above, with g_main_context_iteration
|
|
* replacing aio_poll. However:
|
|
* - sometimes both the AioContext and the glib main loop wake
|
|
* themselves up. Hence, some "g_assert(!aio_poll(ctx, false));"
|
|
* are replaced by "while (g_main_context_iteration(NULL, false));".
|
|
* - there is no exact replacement for a blocking wait.
|
|
* "while (g_main_context_iteration(NULL, true)" seems to work,
|
|
* but it is not documented _why_ it works. For these tests a
|
|
* non-blocking loop like "while (g_main_context_iteration(NULL, false)"
|
|
* works well, and that's what I am using.
|
|
*/
|
|
|
|
static void test_source_flush(void)
|
|
{
|
|
g_assert(!g_main_context_iteration(NULL, false));
|
|
aio_notify(ctx);
|
|
while (g_main_context_iteration(NULL, false));
|
|
g_assert(!g_main_context_iteration(NULL, false));
|
|
}
|
|
|
|
static void test_source_bh_schedule(void)
|
|
{
|
|
BHTestData data = { .n = 0 };
|
|
data.bh = aio_bh_new(ctx, bh_test_cb, &data);
|
|
|
|
qemu_bh_schedule(data.bh);
|
|
g_assert_cmpint(data.n, ==, 0);
|
|
|
|
g_assert(g_main_context_iteration(NULL, true));
|
|
g_assert_cmpint(data.n, ==, 1);
|
|
|
|
g_assert(!g_main_context_iteration(NULL, false));
|
|
g_assert_cmpint(data.n, ==, 1);
|
|
qemu_bh_delete(data.bh);
|
|
}
|
|
|
|
static void test_source_bh_schedule10(void)
|
|
{
|
|
BHTestData data = { .n = 0, .max = 10 };
|
|
data.bh = aio_bh_new(ctx, bh_test_cb, &data);
|
|
|
|
qemu_bh_schedule(data.bh);
|
|
g_assert_cmpint(data.n, ==, 0);
|
|
|
|
g_assert(g_main_context_iteration(NULL, false));
|
|
g_assert_cmpint(data.n, ==, 1);
|
|
|
|
g_assert(g_main_context_iteration(NULL, true));
|
|
g_assert_cmpint(data.n, ==, 2);
|
|
|
|
while (g_main_context_iteration(NULL, false));
|
|
g_assert_cmpint(data.n, ==, 10);
|
|
|
|
g_assert(!g_main_context_iteration(NULL, false));
|
|
g_assert_cmpint(data.n, ==, 10);
|
|
qemu_bh_delete(data.bh);
|
|
}
|
|
|
|
static void test_source_bh_cancel(void)
|
|
{
|
|
BHTestData data = { .n = 0 };
|
|
data.bh = aio_bh_new(ctx, bh_test_cb, &data);
|
|
|
|
qemu_bh_schedule(data.bh);
|
|
g_assert_cmpint(data.n, ==, 0);
|
|
|
|
qemu_bh_cancel(data.bh);
|
|
g_assert_cmpint(data.n, ==, 0);
|
|
|
|
while (g_main_context_iteration(NULL, false));
|
|
g_assert_cmpint(data.n, ==, 0);
|
|
qemu_bh_delete(data.bh);
|
|
}
|
|
|
|
static void test_source_bh_delete(void)
|
|
{
|
|
BHTestData data = { .n = 0 };
|
|
data.bh = aio_bh_new(ctx, bh_test_cb, &data);
|
|
|
|
qemu_bh_schedule(data.bh);
|
|
g_assert_cmpint(data.n, ==, 0);
|
|
|
|
qemu_bh_delete(data.bh);
|
|
g_assert_cmpint(data.n, ==, 0);
|
|
|
|
while (g_main_context_iteration(NULL, false));
|
|
g_assert_cmpint(data.n, ==, 0);
|
|
}
|
|
|
|
static void test_source_bh_delete_from_cb(void)
|
|
{
|
|
BHTestData data1 = { .n = 0, .max = 1 };
|
|
|
|
data1.bh = aio_bh_new(ctx, bh_delete_cb, &data1);
|
|
|
|
qemu_bh_schedule(data1.bh);
|
|
g_assert_cmpint(data1.n, ==, 0);
|
|
|
|
g_main_context_iteration(NULL, true);
|
|
g_assert_cmpint(data1.n, ==, data1.max);
|
|
g_assert(data1.bh == NULL);
|
|
|
|
g_assert(!g_main_context_iteration(NULL, false));
|
|
}
|
|
|
|
static void test_source_bh_delete_from_cb_many(void)
|
|
{
|
|
BHTestData data1 = { .n = 0, .max = 1 };
|
|
BHTestData data2 = { .n = 0, .max = 3 };
|
|
BHTestData data3 = { .n = 0, .max = 2 };
|
|
BHTestData data4 = { .n = 0, .max = 4 };
|
|
|
|
data1.bh = aio_bh_new(ctx, bh_delete_cb, &data1);
|
|
data2.bh = aio_bh_new(ctx, bh_delete_cb, &data2);
|
|
data3.bh = aio_bh_new(ctx, bh_delete_cb, &data3);
|
|
data4.bh = aio_bh_new(ctx, bh_delete_cb, &data4);
|
|
|
|
qemu_bh_schedule(data1.bh);
|
|
qemu_bh_schedule(data2.bh);
|
|
qemu_bh_schedule(data3.bh);
|
|
qemu_bh_schedule(data4.bh);
|
|
g_assert_cmpint(data1.n, ==, 0);
|
|
g_assert_cmpint(data2.n, ==, 0);
|
|
g_assert_cmpint(data3.n, ==, 0);
|
|
g_assert_cmpint(data4.n, ==, 0);
|
|
|
|
g_assert(g_main_context_iteration(NULL, false));
|
|
g_assert_cmpint(data1.n, ==, 1);
|
|
g_assert_cmpint(data2.n, ==, 1);
|
|
g_assert_cmpint(data3.n, ==, 1);
|
|
g_assert_cmpint(data4.n, ==, 1);
|
|
g_assert(data1.bh == NULL);
|
|
|
|
while (g_main_context_iteration(NULL, false));
|
|
g_assert_cmpint(data1.n, ==, data1.max);
|
|
g_assert_cmpint(data2.n, ==, data2.max);
|
|
g_assert_cmpint(data3.n, ==, data3.max);
|
|
g_assert_cmpint(data4.n, ==, data4.max);
|
|
g_assert(data1.bh == NULL);
|
|
g_assert(data2.bh == NULL);
|
|
g_assert(data3.bh == NULL);
|
|
g_assert(data4.bh == NULL);
|
|
}
|
|
|
|
static void test_source_bh_flush(void)
|
|
{
|
|
BHTestData data = { .n = 0 };
|
|
data.bh = aio_bh_new(ctx, bh_test_cb, &data);
|
|
|
|
qemu_bh_schedule(data.bh);
|
|
g_assert_cmpint(data.n, ==, 0);
|
|
|
|
g_assert(g_main_context_iteration(NULL, true));
|
|
g_assert_cmpint(data.n, ==, 1);
|
|
|
|
g_assert(!g_main_context_iteration(NULL, false));
|
|
g_assert_cmpint(data.n, ==, 1);
|
|
qemu_bh_delete(data.bh);
|
|
}
|
|
|
|
static void test_source_set_event_notifier(void)
|
|
{
|
|
EventNotifierTestData data = { .n = 0, .active = 0 };
|
|
event_notifier_init(&data.e, false);
|
|
aio_set_event_notifier(ctx, &data.e, event_ready_cb);
|
|
while (g_main_context_iteration(NULL, false));
|
|
g_assert_cmpint(data.n, ==, 0);
|
|
|
|
aio_set_event_notifier(ctx, &data.e, NULL);
|
|
while (g_main_context_iteration(NULL, false));
|
|
g_assert_cmpint(data.n, ==, 0);
|
|
event_notifier_cleanup(&data.e);
|
|
}
|
|
|
|
static void test_source_wait_event_notifier(void)
|
|
{
|
|
EventNotifierTestData data = { .n = 0, .active = 1 };
|
|
event_notifier_init(&data.e, false);
|
|
aio_set_event_notifier(ctx, &data.e, event_ready_cb);
|
|
while (g_main_context_iteration(NULL, false));
|
|
g_assert_cmpint(data.n, ==, 0);
|
|
g_assert_cmpint(data.active, ==, 1);
|
|
|
|
event_notifier_set(&data.e);
|
|
g_assert(g_main_context_iteration(NULL, false));
|
|
g_assert_cmpint(data.n, ==, 1);
|
|
g_assert_cmpint(data.active, ==, 0);
|
|
|
|
while (g_main_context_iteration(NULL, false));
|
|
g_assert_cmpint(data.n, ==, 1);
|
|
g_assert_cmpint(data.active, ==, 0);
|
|
|
|
aio_set_event_notifier(ctx, &data.e, NULL);
|
|
while (g_main_context_iteration(NULL, false));
|
|
g_assert_cmpint(data.n, ==, 1);
|
|
|
|
event_notifier_cleanup(&data.e);
|
|
}
|
|
|
|
static void test_source_flush_event_notifier(void)
|
|
{
|
|
EventNotifierTestData data = { .n = 0, .active = 10, .auto_set = true };
|
|
event_notifier_init(&data.e, false);
|
|
aio_set_event_notifier(ctx, &data.e, event_ready_cb);
|
|
while (g_main_context_iteration(NULL, false));
|
|
g_assert_cmpint(data.n, ==, 0);
|
|
g_assert_cmpint(data.active, ==, 10);
|
|
|
|
event_notifier_set(&data.e);
|
|
g_assert(g_main_context_iteration(NULL, false));
|
|
g_assert_cmpint(data.n, ==, 1);
|
|
g_assert_cmpint(data.active, ==, 9);
|
|
g_assert(g_main_context_iteration(NULL, false));
|
|
|
|
while (g_main_context_iteration(NULL, false));
|
|
g_assert_cmpint(data.n, ==, 10);
|
|
g_assert_cmpint(data.active, ==, 0);
|
|
g_assert(!g_main_context_iteration(NULL, false));
|
|
|
|
aio_set_event_notifier(ctx, &data.e, NULL);
|
|
while (g_main_context_iteration(NULL, false));
|
|
event_notifier_cleanup(&data.e);
|
|
}
|
|
|
|
static void test_source_wait_event_notifier_noflush(void)
|
|
{
|
|
EventNotifierTestData data = { .n = 0 };
|
|
EventNotifierTestData dummy = { .n = 0, .active = 1 };
|
|
|
|
event_notifier_init(&data.e, false);
|
|
aio_set_event_notifier(ctx, &data.e, event_ready_cb);
|
|
|
|
while (g_main_context_iteration(NULL, false));
|
|
g_assert_cmpint(data.n, ==, 0);
|
|
|
|
/* Until there is an active descriptor, glib may or may not call
|
|
* event_ready_cb. Still, it must not block. */
|
|
event_notifier_set(&data.e);
|
|
g_main_context_iteration(NULL, true);
|
|
data.n = 0;
|
|
|
|
/* An active event notifier forces aio_poll to look at EventNotifiers. */
|
|
event_notifier_init(&dummy.e, false);
|
|
aio_set_event_notifier(ctx, &dummy.e, event_ready_cb);
|
|
|
|
event_notifier_set(&data.e);
|
|
g_assert(g_main_context_iteration(NULL, false));
|
|
g_assert_cmpint(data.n, ==, 1);
|
|
g_assert(!g_main_context_iteration(NULL, false));
|
|
g_assert_cmpint(data.n, ==, 1);
|
|
|
|
event_notifier_set(&data.e);
|
|
g_assert(g_main_context_iteration(NULL, false));
|
|
g_assert_cmpint(data.n, ==, 2);
|
|
g_assert(!g_main_context_iteration(NULL, false));
|
|
g_assert_cmpint(data.n, ==, 2);
|
|
|
|
event_notifier_set(&dummy.e);
|
|
while (g_main_context_iteration(NULL, false));
|
|
g_assert_cmpint(data.n, ==, 2);
|
|
g_assert_cmpint(dummy.n, ==, 1);
|
|
g_assert_cmpint(dummy.active, ==, 0);
|
|
|
|
aio_set_event_notifier(ctx, &dummy.e, NULL);
|
|
event_notifier_cleanup(&dummy.e);
|
|
|
|
aio_set_event_notifier(ctx, &data.e, NULL);
|
|
while (g_main_context_iteration(NULL, false));
|
|
g_assert_cmpint(data.n, ==, 2);
|
|
|
|
event_notifier_cleanup(&data.e);
|
|
}
|
|
|
|
static void test_source_timer_schedule(void)
|
|
{
|
|
TimerTestData data = { .n = 0, .ctx = ctx, .ns = SCALE_MS * 750LL,
|
|
.max = 2,
|
|
.clock_type = QEMU_CLOCK_VIRTUAL };
|
|
EventNotifier e;
|
|
int64_t expiry;
|
|
|
|
/* aio_poll will not block to wait for timers to complete unless it has
|
|
* an fd to wait on. Fixing this breaks other tests. So create a dummy one.
|
|
*/
|
|
event_notifier_init(&e, false);
|
|
aio_set_event_notifier(ctx, &e, dummy_io_handler_read);
|
|
do {} while (g_main_context_iteration(NULL, false));
|
|
|
|
aio_timer_init(ctx, &data.timer, data.clock_type,
|
|
SCALE_NS, timer_test_cb, &data);
|
|
expiry = qemu_clock_get_ns(data.clock_type) +
|
|
data.ns;
|
|
timer_mod(&data.timer, expiry);
|
|
|
|
g_assert_cmpint(data.n, ==, 0);
|
|
|
|
g_usleep(1 * G_USEC_PER_SEC);
|
|
g_assert_cmpint(data.n, ==, 0);
|
|
|
|
g_assert(g_main_context_iteration(NULL, true));
|
|
g_assert_cmpint(data.n, ==, 1);
|
|
expiry += data.ns;
|
|
|
|
while (data.n < 2) {
|
|
g_main_context_iteration(NULL, true);
|
|
}
|
|
|
|
g_assert_cmpint(data.n, ==, 2);
|
|
g_assert(qemu_clock_get_ns(data.clock_type) > expiry);
|
|
|
|
aio_set_event_notifier(ctx, &e, NULL);
|
|
event_notifier_cleanup(&e);
|
|
|
|
timer_del(&data.timer);
|
|
}
|
|
|
|
|
|
/* End of tests. */
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
Error *local_error = NULL;
|
|
GSource *src;
|
|
|
|
init_clocks();
|
|
|
|
ctx = aio_context_new(&local_error);
|
|
if (!ctx) {
|
|
error_report("Failed to create AIO Context: '%s'",
|
|
error_get_pretty(local_error));
|
|
error_free(local_error);
|
|
exit(1);
|
|
}
|
|
src = aio_get_g_source(ctx);
|
|
g_source_attach(src, NULL);
|
|
g_source_unref(src);
|
|
|
|
while (g_main_context_iteration(NULL, false));
|
|
|
|
g_test_init(&argc, &argv, NULL);
|
|
g_test_add_func("/aio/acquire", test_acquire);
|
|
g_test_add_func("/aio/bh/schedule", test_bh_schedule);
|
|
g_test_add_func("/aio/bh/schedule10", test_bh_schedule10);
|
|
g_test_add_func("/aio/bh/cancel", test_bh_cancel);
|
|
g_test_add_func("/aio/bh/delete", test_bh_delete);
|
|
g_test_add_func("/aio/bh/callback-delete/one", test_bh_delete_from_cb);
|
|
g_test_add_func("/aio/bh/callback-delete/many", test_bh_delete_from_cb_many);
|
|
g_test_add_func("/aio/bh/flush", test_bh_flush);
|
|
g_test_add_func("/aio/event/add-remove", test_set_event_notifier);
|
|
g_test_add_func("/aio/event/wait", test_wait_event_notifier);
|
|
g_test_add_func("/aio/event/wait/no-flush-cb", test_wait_event_notifier_noflush);
|
|
g_test_add_func("/aio/event/flush", test_flush_event_notifier);
|
|
g_test_add_func("/aio/timer/schedule", test_timer_schedule);
|
|
|
|
g_test_add_func("/aio-gsource/flush", test_source_flush);
|
|
g_test_add_func("/aio-gsource/bh/schedule", test_source_bh_schedule);
|
|
g_test_add_func("/aio-gsource/bh/schedule10", test_source_bh_schedule10);
|
|
g_test_add_func("/aio-gsource/bh/cancel", test_source_bh_cancel);
|
|
g_test_add_func("/aio-gsource/bh/delete", test_source_bh_delete);
|
|
g_test_add_func("/aio-gsource/bh/callback-delete/one", test_source_bh_delete_from_cb);
|
|
g_test_add_func("/aio-gsource/bh/callback-delete/many", test_source_bh_delete_from_cb_many);
|
|
g_test_add_func("/aio-gsource/bh/flush", test_source_bh_flush);
|
|
g_test_add_func("/aio-gsource/event/add-remove", test_source_set_event_notifier);
|
|
g_test_add_func("/aio-gsource/event/wait", test_source_wait_event_notifier);
|
|
g_test_add_func("/aio-gsource/event/wait/no-flush-cb", test_source_wait_event_notifier_noflush);
|
|
g_test_add_func("/aio-gsource/event/flush", test_source_flush_event_notifier);
|
|
g_test_add_func("/aio-gsource/timer/schedule", test_source_timer_schedule);
|
|
return g_test_run();
|
|
}
|