2008-09-23 03:17:18 +08:00
|
|
|
/*
|
|
|
|
* QEMU aio implementation
|
|
|
|
*
|
2012-06-09 10:01:51 +08:00
|
|
|
* Copyright IBM Corp., 2008
|
|
|
|
* Copyright Red Hat Inc., 2012
|
2008-09-23 03:17:18 +08:00
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Anthony Liguori <aliguori@us.ibm.com>
|
2012-06-09 10:01:51 +08:00
|
|
|
* Paolo Bonzini <pbonzini@redhat.com>
|
2008-09-23 03:17:18 +08:00
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2. See
|
|
|
|
* the COPYING file in the top-level directory.
|
|
|
|
*
|
2012-01-14 00:44:23 +08:00
|
|
|
* Contributions after 2012-01-13 are licensed under the terms of the
|
|
|
|
* GNU GPL, version 2 or (at your option) any later version.
|
2008-09-23 03:17:18 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "qemu-common.h"
|
2012-12-18 01:19:44 +08:00
|
|
|
#include "block/block.h"
|
2012-12-18 01:20:00 +08:00
|
|
|
#include "qemu/queue.h"
|
|
|
|
#include "qemu/sockets.h"
|
2008-09-23 03:17:18 +08:00
|
|
|
|
2012-06-09 10:01:51 +08:00
|
|
|
struct AioHandler {
|
|
|
|
EventNotifier *e;
|
2014-07-09 17:53:10 +08:00
|
|
|
IOHandler *io_read;
|
|
|
|
IOHandler *io_write;
|
2012-06-09 10:01:51 +08:00
|
|
|
EventNotifierHandler *io_notify;
|
2012-09-24 20:57:22 +08:00
|
|
|
GPollFD pfd;
|
2008-09-23 03:17:18 +08:00
|
|
|
int deleted;
|
2014-07-09 17:53:10 +08:00
|
|
|
void *opaque;
|
2009-09-12 15:36:22 +08:00
|
|
|
QLIST_ENTRY(AioHandler) node;
|
2008-09-23 03:17:18 +08:00
|
|
|
};
|
|
|
|
|
2014-07-09 17:53:10 +08:00
|
|
|
void aio_set_fd_handler(AioContext *ctx,
|
|
|
|
int fd,
|
|
|
|
IOHandler *io_read,
|
|
|
|
IOHandler *io_write,
|
|
|
|
void *opaque)
|
|
|
|
{
|
|
|
|
/* fd is a SOCKET in our case */
|
|
|
|
AioHandler *node;
|
|
|
|
|
|
|
|
QLIST_FOREACH(node, &ctx->aio_handlers, node) {
|
|
|
|
if (node->pfd.fd == fd && !node->deleted) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Are we deleting the fd handler? */
|
|
|
|
if (!io_read && !io_write) {
|
|
|
|
if (node) {
|
|
|
|
/* If the lock is held, just mark the node as deleted */
|
|
|
|
if (ctx->walking_handlers) {
|
|
|
|
node->deleted = 1;
|
|
|
|
node->pfd.revents = 0;
|
|
|
|
} else {
|
|
|
|
/* Otherwise, delete it for real. We can't just mark it as
|
|
|
|
* deleted because deleted nodes are only cleaned up after
|
|
|
|
* releasing the walking_handlers lock.
|
|
|
|
*/
|
|
|
|
QLIST_REMOVE(node, node);
|
|
|
|
g_free(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
HANDLE event;
|
|
|
|
|
|
|
|
if (node == NULL) {
|
|
|
|
/* Alloc and insert if it's not already there */
|
|
|
|
node = g_malloc0(sizeof(AioHandler));
|
|
|
|
node->pfd.fd = fd;
|
|
|
|
QLIST_INSERT_HEAD(&ctx->aio_handlers, node, node);
|
|
|
|
}
|
|
|
|
|
|
|
|
node->pfd.events = 0;
|
|
|
|
if (node->io_read) {
|
|
|
|
node->pfd.events |= G_IO_IN;
|
|
|
|
}
|
|
|
|
if (node->io_write) {
|
|
|
|
node->pfd.events |= G_IO_OUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
node->e = &ctx->notifier;
|
|
|
|
|
|
|
|
/* Update handler with latest information */
|
|
|
|
node->opaque = opaque;
|
|
|
|
node->io_read = io_read;
|
|
|
|
node->io_write = io_write;
|
|
|
|
|
|
|
|
event = event_notifier_get_handle(&ctx->notifier);
|
|
|
|
WSAEventSelect(node->pfd.fd, event,
|
|
|
|
FD_READ | FD_ACCEPT | FD_CLOSE |
|
|
|
|
FD_CONNECT | FD_WRITE | FD_OOB);
|
|
|
|
}
|
|
|
|
|
|
|
|
aio_notify(ctx);
|
|
|
|
}
|
|
|
|
|
2012-06-09 10:01:51 +08:00
|
|
|
void aio_set_event_notifier(AioContext *ctx,
|
|
|
|
EventNotifier *e,
|
2013-04-11 23:26:25 +08:00
|
|
|
EventNotifierHandler *io_notify)
|
2008-09-23 03:17:18 +08:00
|
|
|
{
|
|
|
|
AioHandler *node;
|
|
|
|
|
2012-09-13 18:28:51 +08:00
|
|
|
QLIST_FOREACH(node, &ctx->aio_handlers, node) {
|
2012-06-09 10:01:51 +08:00
|
|
|
if (node->e == e && !node->deleted) {
|
|
|
|
break;
|
|
|
|
}
|
2008-09-23 03:17:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Are we deleting the fd handler? */
|
2012-06-09 10:01:51 +08:00
|
|
|
if (!io_notify) {
|
2008-09-23 03:17:18 +08:00
|
|
|
if (node) {
|
2012-09-24 20:57:41 +08:00
|
|
|
g_source_remove_poll(&ctx->source, &node->pfd);
|
|
|
|
|
2008-09-23 03:17:18 +08:00
|
|
|
/* If the lock is held, just mark the node as deleted */
|
2012-09-24 20:57:22 +08:00
|
|
|
if (ctx->walking_handlers) {
|
2008-09-23 03:17:18 +08:00
|
|
|
node->deleted = 1;
|
2012-09-24 20:57:22 +08:00
|
|
|
node->pfd.revents = 0;
|
|
|
|
} else {
|
2008-09-23 03:17:18 +08:00
|
|
|
/* Otherwise, delete it for real. We can't just mark it as
|
|
|
|
* deleted because deleted nodes are only cleaned up after
|
|
|
|
* releasing the walking_handlers lock.
|
|
|
|
*/
|
2009-09-12 15:36:22 +08:00
|
|
|
QLIST_REMOVE(node, node);
|
2011-08-21 11:09:37 +08:00
|
|
|
g_free(node);
|
2008-09-23 03:17:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (node == NULL) {
|
|
|
|
/* Alloc and insert if it's not already there */
|
2011-08-21 11:09:37 +08:00
|
|
|
node = g_malloc0(sizeof(AioHandler));
|
2012-06-09 10:01:51 +08:00
|
|
|
node->e = e;
|
|
|
|
node->pfd.fd = (uintptr_t)event_notifier_get_handle(e);
|
|
|
|
node->pfd.events = G_IO_IN;
|
2012-09-13 18:28:51 +08:00
|
|
|
QLIST_INSERT_HEAD(&ctx->aio_handlers, node, node);
|
2012-09-24 20:57:41 +08:00
|
|
|
|
|
|
|
g_source_add_poll(&ctx->source, &node->pfd);
|
2008-09-23 03:17:18 +08:00
|
|
|
}
|
|
|
|
/* Update handler with latest information */
|
2012-06-09 10:01:51 +08:00
|
|
|
node->io_notify = io_notify;
|
2008-09-23 03:17:18 +08:00
|
|
|
}
|
2012-09-25 16:22:39 +08:00
|
|
|
|
|
|
|
aio_notify(ctx);
|
2012-06-09 09:44:00 +08:00
|
|
|
}
|
|
|
|
|
2014-07-09 17:53:08 +08:00
|
|
|
bool aio_prepare(AioContext *ctx)
|
|
|
|
{
|
2014-07-09 17:53:10 +08:00
|
|
|
static struct timeval tv0;
|
|
|
|
AioHandler *node;
|
|
|
|
bool have_select_revents = false;
|
|
|
|
fd_set rfds, wfds;
|
|
|
|
|
|
|
|
/* fill fd sets */
|
|
|
|
FD_ZERO(&rfds);
|
|
|
|
FD_ZERO(&wfds);
|
|
|
|
QLIST_FOREACH(node, &ctx->aio_handlers, node) {
|
|
|
|
if (node->io_read) {
|
|
|
|
FD_SET ((SOCKET)node->pfd.fd, &rfds);
|
|
|
|
}
|
|
|
|
if (node->io_write) {
|
|
|
|
FD_SET ((SOCKET)node->pfd.fd, &wfds);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (select(0, &rfds, &wfds, NULL, &tv0) > 0) {
|
|
|
|
QLIST_FOREACH(node, &ctx->aio_handlers, node) {
|
|
|
|
node->pfd.revents = 0;
|
|
|
|
if (FD_ISSET(node->pfd.fd, &rfds)) {
|
|
|
|
node->pfd.revents |= G_IO_IN;
|
|
|
|
have_select_revents = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FD_ISSET(node->pfd.fd, &wfds)) {
|
|
|
|
node->pfd.revents |= G_IO_OUT;
|
|
|
|
have_select_revents = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return have_select_revents;
|
2014-07-09 17:53:08 +08:00
|
|
|
}
|
|
|
|
|
2012-09-24 20:57:22 +08:00
|
|
|
bool aio_pending(AioContext *ctx)
|
|
|
|
{
|
|
|
|
AioHandler *node;
|
|
|
|
|
|
|
|
QLIST_FOREACH(node, &ctx->aio_handlers, node) {
|
2012-06-09 10:01:51 +08:00
|
|
|
if (node->pfd.revents && node->io_notify) {
|
2012-09-24 20:57:22 +08:00
|
|
|
return true;
|
|
|
|
}
|
2014-07-09 17:53:10 +08:00
|
|
|
|
|
|
|
if ((node->pfd.revents & G_IO_IN) && node->io_read) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if ((node->pfd.revents & G_IO_OUT) && node->io_write) {
|
|
|
|
return true;
|
|
|
|
}
|
2012-09-24 20:57:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-07-09 17:53:03 +08:00
|
|
|
static bool aio_dispatch_handlers(AioContext *ctx, HANDLE event)
|
2008-09-23 03:17:18 +08:00
|
|
|
{
|
2012-04-12 20:00:56 +08:00
|
|
|
AioHandler *node;
|
2014-07-09 17:53:03 +08:00
|
|
|
bool progress = false;
|
2012-09-24 20:37:53 +08:00
|
|
|
|
2012-09-24 20:57:22 +08:00
|
|
|
/*
|
2014-07-07 21:18:02 +08:00
|
|
|
* We have to walk very carefully in case aio_set_fd_handler is
|
2012-09-24 20:57:22 +08:00
|
|
|
* called while we're walking.
|
|
|
|
*/
|
|
|
|
node = QLIST_FIRST(&ctx->aio_handlers);
|
|
|
|
while (node) {
|
|
|
|
AioHandler *tmp;
|
2014-07-09 17:53:10 +08:00
|
|
|
int revents = node->pfd.revents;
|
2012-09-24 20:57:22 +08:00
|
|
|
|
|
|
|
ctx->walking_handlers++;
|
|
|
|
|
2014-07-09 17:53:03 +08:00
|
|
|
if (!node->deleted &&
|
2014-07-09 17:53:10 +08:00
|
|
|
(revents || event_notifier_get_handle(node->e) == event) &&
|
2014-07-09 17:53:03 +08:00
|
|
|
node->io_notify) {
|
2012-06-09 10:01:51 +08:00
|
|
|
node->pfd.revents = 0;
|
|
|
|
node->io_notify(node->e);
|
2013-04-11 22:56:50 +08:00
|
|
|
|
|
|
|
/* aio_notify() does not count as progress */
|
2013-08-22 21:28:35 +08:00
|
|
|
if (node->e != &ctx->notifier) {
|
2013-04-11 22:56:50 +08:00
|
|
|
progress = true;
|
|
|
|
}
|
2012-09-24 20:57:22 +08:00
|
|
|
}
|
|
|
|
|
2014-07-09 17:53:10 +08:00
|
|
|
if (!node->deleted &&
|
|
|
|
(node->io_read || node->io_write)) {
|
|
|
|
node->pfd.revents = 0;
|
|
|
|
if ((revents & G_IO_IN) && node->io_read) {
|
|
|
|
node->io_read(node->opaque);
|
|
|
|
progress = true;
|
|
|
|
}
|
|
|
|
if ((revents & G_IO_OUT) && node->io_write) {
|
|
|
|
node->io_write(node->opaque);
|
|
|
|
progress = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if the next select() will return an event, we have progressed */
|
|
|
|
if (event == event_notifier_get_handle(&ctx->notifier)) {
|
|
|
|
WSANETWORKEVENTS ev;
|
|
|
|
WSAEnumNetworkEvents(node->pfd.fd, event, &ev);
|
|
|
|
if (ev.lNetworkEvents) {
|
|
|
|
progress = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-24 20:57:22 +08:00
|
|
|
tmp = node;
|
|
|
|
node = QLIST_NEXT(node, node);
|
|
|
|
|
|
|
|
ctx->walking_handlers--;
|
|
|
|
|
|
|
|
if (!ctx->walking_handlers && tmp->deleted) {
|
|
|
|
QLIST_REMOVE(tmp, node);
|
|
|
|
g_free(tmp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-09 17:53:03 +08:00
|
|
|
return progress;
|
|
|
|
}
|
|
|
|
|
2014-07-09 17:53:05 +08:00
|
|
|
bool aio_dispatch(AioContext *ctx)
|
2014-07-09 17:53:03 +08:00
|
|
|
{
|
|
|
|
bool progress;
|
|
|
|
|
2014-07-09 17:53:05 +08:00
|
|
|
progress = aio_bh_poll(ctx);
|
|
|
|
progress |= aio_dispatch_handlers(ctx, INVALID_HANDLE_VALUE);
|
2014-07-09 17:53:02 +08:00
|
|
|
progress |= timerlistgroup_run_timers(&ctx->tlg);
|
2014-07-09 17:53:03 +08:00
|
|
|
return progress;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool aio_poll(AioContext *ctx, bool blocking)
|
|
|
|
{
|
|
|
|
AioHandler *node;
|
|
|
|
HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
|
2014-07-09 17:53:10 +08:00
|
|
|
bool was_dispatching, progress, have_select_revents, first;
|
2014-07-09 17:53:03 +08:00
|
|
|
int count;
|
|
|
|
int timeout;
|
|
|
|
|
2014-09-12 18:08:49 +08:00
|
|
|
have_select_revents = aio_prepare(ctx);
|
|
|
|
if (have_select_revents) {
|
2014-07-09 17:53:10 +08:00
|
|
|
blocking = false;
|
|
|
|
}
|
|
|
|
|
2014-07-09 17:53:07 +08:00
|
|
|
was_dispatching = ctx->dispatching;
|
2014-07-09 17:53:03 +08:00
|
|
|
progress = false;
|
|
|
|
|
2014-07-09 17:53:07 +08:00
|
|
|
/* aio_notify can avoid the expensive event_notifier_set if
|
|
|
|
* everything (file descriptors, bottom halves, timers) will
|
|
|
|
* be re-evaluated before the next blocking poll(). This is
|
|
|
|
* already true when aio_poll is called with blocking == false;
|
|
|
|
* if blocking == true, it is only true after poll() returns.
|
|
|
|
*
|
|
|
|
* If we're in a nested event loop, ctx->dispatching might be true.
|
|
|
|
* In that case we can restore it just before returning, but we
|
|
|
|
* have to clear it now.
|
|
|
|
*/
|
|
|
|
aio_set_dispatching(ctx, !blocking);
|
|
|
|
|
2012-09-13 18:28:51 +08:00
|
|
|
ctx->walking_handlers++;
|
2008-09-23 03:17:18 +08:00
|
|
|
|
2012-04-12 20:00:56 +08:00
|
|
|
/* fill fd sets */
|
2012-06-09 10:01:51 +08:00
|
|
|
count = 0;
|
2012-09-13 18:28:51 +08:00
|
|
|
QLIST_FOREACH(node, &ctx->aio_handlers, node) {
|
2012-06-09 10:01:51 +08:00
|
|
|
if (!node->deleted && node->io_notify) {
|
|
|
|
events[count++] = event_notifier_get_handle(node->e);
|
2012-04-12 20:00:56 +08:00
|
|
|
}
|
|
|
|
}
|
2008-09-23 03:17:18 +08:00
|
|
|
|
2012-09-13 18:28:51 +08:00
|
|
|
ctx->walking_handlers--;
|
2014-07-09 17:53:04 +08:00
|
|
|
first = true;
|
2008-09-23 03:17:18 +08:00
|
|
|
|
2012-04-12 20:00:56 +08:00
|
|
|
/* wait until next event */
|
2012-11-23 22:59:43 +08:00
|
|
|
while (count > 0) {
|
2014-07-09 17:53:10 +08:00
|
|
|
HANDLE event;
|
2013-08-21 23:02:53 +08:00
|
|
|
int ret;
|
|
|
|
|
2014-07-09 17:53:01 +08:00
|
|
|
timeout = blocking
|
|
|
|
? qemu_timeout_ns_to_ms(aio_compute_timeout(ctx)) : 0;
|
2013-08-21 23:02:53 +08:00
|
|
|
ret = WaitForMultipleObjects(count, events, FALSE, timeout);
|
2014-07-09 17:53:07 +08:00
|
|
|
aio_set_dispatching(ctx, true);
|
2012-06-09 10:01:51 +08:00
|
|
|
|
2014-07-09 17:53:04 +08:00
|
|
|
if (first && aio_bh_poll(ctx)) {
|
|
|
|
progress = true;
|
|
|
|
}
|
|
|
|
first = false;
|
|
|
|
|
2012-06-09 10:01:51 +08:00
|
|
|
/* if we have any signaled events, dispatch event */
|
2014-07-09 17:53:10 +08:00
|
|
|
event = NULL;
|
|
|
|
if ((DWORD) (ret - WAIT_OBJECT_0) < count) {
|
|
|
|
event = events[ret - WAIT_OBJECT_0];
|
2014-09-15 20:52:58 +08:00
|
|
|
events[ret - WAIT_OBJECT_0] = events[--count];
|
2014-07-09 17:53:10 +08:00
|
|
|
} else if (!have_select_revents) {
|
2012-06-09 10:01:51 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-07-09 17:53:10 +08:00
|
|
|
have_select_revents = false;
|
2012-06-09 10:01:51 +08:00
|
|
|
blocking = false;
|
2012-04-12 20:00:56 +08:00
|
|
|
|
2014-07-09 17:53:10 +08:00
|
|
|
progress |= aio_dispatch_handlers(ctx, event);
|
2012-04-12 20:00:56 +08:00
|
|
|
}
|
2012-04-12 20:00:55 +08:00
|
|
|
|
2014-07-09 17:53:05 +08:00
|
|
|
progress |= timerlistgroup_run_timers(&ctx->tlg);
|
2013-08-21 23:02:53 +08:00
|
|
|
|
2014-07-09 17:53:07 +08:00
|
|
|
aio_set_dispatching(ctx, was_dispatching);
|
2013-04-11 22:56:50 +08:00
|
|
|
return progress;
|
2008-09-23 03:17:18 +08:00
|
|
|
}
|