aio: Use epoll in aio_poll()
cover aio: Use epoll in aio_poll()
This series adds the ability to use epoll in aio_poll() on Linux. It's switched on in a dynamic way rather than static for two reasons: 1) when the number of fds is not high enough, using epoll has little advantage; 2) when an epoll incompatible fd needs to be handled, we need to fall back. The epoll is enabled when a fd number threshold is met.
Fam Zheng (3):
aio: Move AioHandler struct to header
aio: Introduce aio_context_setup
aio: Introduce aio-epoll.c
Makefile.objs | 1 + aio-epoll.c | 150 +++++++++++++++++++++++++++++++++++++++++++ aio-posix.c | 31 +++++---- aio-win32.c | 16 ++--- async.c | 14 +++- include/block/aio-internal.h | 47 ++++++++++++++ include/block/aio.h | 5 ++ stubs/Makefile.objs | 1 + stubs/aio-epoll.c | 37 +++++++++++ 9 files changed, 277 insertions(+), 25 deletions(-) create mode 100644 aio-epoll.c create mode 100644 include/block/aio-internal.h create mode 100644 stubs/aio-epoll.c
Signature · 1 line
2.6.1
PATCH 1/3 aio: Move AioHandler struct to header
AioHandler for win32 is a superset of the counterpart in aio-posix, move that to a new header "aio-internal.h" and drop the posix variation.
--- aio-posix.c | 11 +---------- aio-win32.c | 12 +----------- include/block/aio-internal.h | 30 ++++++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 21 deletions(-) create mode 100644 include/block/aio-internal.h
aio-posix.c +1 -10
@@ -17,16 +17,7 @@#include "block/block.h" #include "qemu/queue.h" #include "qemu/sockets.h" - -struct AioHandler -{ -GPollFD pfd; -IOHandler *io_read; -IOHandler *io_write; -int deleted; -void *opaque; -QLIST_ENTRY (AioHandler) node; -}; +#include "block/aio-internal.h" static AioHandler *find_aio_handler (AioContext *ctx,int fd) {
aio-win32.c +1 -11
@@ -19,17 +19,7 @@#include "block/block.h" #include "qemu/queue.h" #include "qemu/sockets.h" - -struct AioHandler { -EventNotifier *e; -IOHandler *io_read; -IOHandler *io_write; -EventNotifierHandler *io_notify; -GPollFD pfd; -int deleted; -void *opaque; -QLIST_ENTRY (AioHandler) node; -}; +#include "block/aio-internal.h" void aio_set_fd_handler(AioContext *ctx, int fd,
include/block/aio-internal.h new file +30 -0
@@ -0,0 +1,30 @@ +/* +* QEMU aio internal interface +* +* Copyright Red Hat, Inc. 2015 +* +* Authors: +* Fam Zheng <[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2. See +* the COPYING file in the top-level directory. +* +*/ + +#ifndef QEMU_AIO_INTERNAL_H +#define QEMU_AIO_INTERNAL_H + +#include "block/aio.h" + +struct AioHandler { +EventNotifier *e; +IOHandler *io_read; +IOHandler *io_write; +EventNotifierHandler *io_notify; +GPollFD pfd; +int deleted; +void *opaque; +QLIST_ENTRY (AioHandler) node; +}; + +#endif
Signature · 1 line
2.6.1
PATCH 2/3 aio: Introduce aio_context_setup
In reply to Fam ZhengThis is the place to initialize platform specific bits of AioContext.
--- aio-posix.c | 4 ++++ aio-win32.c | 4 ++++ async.c | 14 ++++++++++++-- include/block/aio-internal.h | 2 ++ 4 files changed, 22 insertions(+), 2 deletions(-)
aio-posix.c +4 -0
@@ -288,3 +288,7 @@ bool aio_poll(AioContext *ctx, bool blocking)return progress; } + +void aio_context_setup (AioContext *ctx,Error **errp) +{ +}
aio-win32.c +4 -0
@@ -353,3 +353,7 @@ bool aio_poll(AioContext *ctx, bool blocking)aio_context_release (ctx);return progress; } + +void aio_context_setup (AioContext *ctx,Error **errp) +{ +}
async.c +12 -2
@@ -27,6 +27,7 @@#include "block/thread-pool.h" #include "qemu/main-loop.h" #include "qemu/atomic.h" +#include "block/aio-internal.h" /***********************************************************/ /* bottom halves (can be seen as timers which expire ASAP) */
@@ -320,12 +321,18 @@ AioContext *aio_context_new(Error **errp) {int ret;AioContext *ctx; +Error *local_err =NULL ; + ctx = (AioContext *)g_source_new (&aio_source_funcs,sizeof (AioContext)); +aio_context_setup (ctx, &local_err); +if (local_err) { +error_propagate (errp, local_err); + goto fail; + } ret =event_notifier_init (&ctx->notifier, false);if (ret <0 ) { -g_source_destroy (&ctx->source);error_setg_errno (errp, -ret,"Failed to initialize event notifier" ); -return NULL ; + goto fail; }g_source_set_can_recurse (&ctx->source, true); aio_set_event_notifier(ctx, &ctx->notifier,
@@ -339,6 +346,9 @@ AioContext *aio_context_new(Error **errp) ctx->notify_dummy_bh =aio_bh_new (ctx, notify_dummy_bh,NULL );return ctx; +fail: +g_source_destroy (&ctx->source); +return NULL ; }void aio_context_ref (AioContext *ctx)
include/block/aio-internal.h +2 -0
@@ -27,4 +27,6 @@ struct AioHandler { QLIST_ENTRY(AioHandler) node; }; +void aio_context_setup (AioContext *ctx,Error **errp); +#endif
Signature · 1 line
2.6.1
PATCH 3/3 aio: Introduce aio-epoll.c
In reply to Fam ZhengTo minimize code duplication, epoll is hooked into aio-posix's aio_poll() instead of rolling its own. This approach also has the advantage that both compile time and run time ability to switch from between the two:
1) If configure script didn't find epoll, the libqemustub.a nop functions will be used, which selects the usual ppoll.
2) When QEMU starts with a small number of fds in the event loop, ppoll is used.
3) When QEMU starts with a big number of fds, or when more devices are hot plugged after starting up, epoll automatically kicks in after the number of fds hits the threshold.
4) Some fds may not support epoll, such as tty based stdio. In this case, we can fall back to ppoll.
--- Makefile.objs | 1 + aio-epoll.c | 150 +++++++++++++++++++++++++++++++++++++++++++ aio-posix.c | 16 ++++- include/block/aio-internal.h | 15 +++++ include/block/aio.h | 5 ++ stubs/Makefile.objs | 1 + stubs/aio-epoll.c | 37 +++++++++++ 7 files changed, 223 insertions(+), 2 deletions(-) create mode 100644 aio-epoll.c create mode 100644 stubs/aio-epoll.c
Makefile.objs +1 -0
@@ -10,6 +10,7 @@ util-obj-y += qmp-introspect.o qapi-types.o qapi-visit.o qapi-event.o block-obj-y = async.o thread-pool.o block-obj-y += nbd.o block.o blockjob.o block-obj-y += main-loop.o iohandler.o qemu-timer.o +block-obj-$(CONFIG_EPOLL) += aio-epoll.o block-obj-$(CONFIG_POSIX) += aio-posix.o block-obj-$(CONFIG_WIN32) += aio-win32.o block-obj-y += block/
aio-epoll.c new file +150 -0
150 lines not on this page · raw message
aio-posix.c +14 -2
@@ -39,6 +39,7 @@ void aio_set_fd_handler(AioContext *ctx, void *opaque) {AioHandler *node; +bool is_new = false; node =find_aio_handler (ctx, fd);
@@ -68,6 +69,7 @@ void aio_set_fd_handler(AioContext *ctx,QLIST_INSERT_HEAD (&ctx->aio_handlers, node, node);g_source_add_poll (&ctx->source, &node->pfd); + is_new = true; }/* Update handler with latest information */ node->io_read = io_read;
@@ -78,6 +80,7 @@ void aio_set_fd_handler(AioContext *ctx, node->pfd.events |= (io_write ?G_IO_OUT |G_IO_ERR :0 ); } +aio_epoll_update (ctx, node, is_new);aio_notify (ctx); }
@@ -248,7 +251,7 @@ bool aio_poll(AioContext *ctx, bool blocking)/* fill pollfds */ QLIST_FOREACH(node, &ctx->aio_handlers, node) { -if (!node->deleted && node->pfd.events) { +if (!node->deleted && node->pfd.events && !ctx->epoll_enabled) {add_pollfd (node); } }
@@ -259,7 +262,15 @@ bool aio_poll(AioContext *ctx, bool blocking)if (timeout) {aio_context_release (ctx); } - ret =qemu_poll_ns ((GPollFD *)pollfds, npfd, timeout); +if (aio_epoll_check_poll (ctx, pollfds, npfd, timeout)) { + npfd =0 ; +AioHandler epoll_handler = {0 }; +aio_epoll_fill (ctx, &epoll_handler); +add_pollfd (&epoll_handler); + ret =aio_epoll (ctx, pollfds, npfd, timeout); + }else { + ret =qemu_poll_ns (pollfds, npfd, timeout); + }if (blocking) {atomic_sub (&ctx->notify_me,2 ); }
@@ -291,4 +302,5 @@ bool aio_poll(AioContext *ctx, bool blocking)void aio_context_setup (AioContext *ctx,Error **errp) { +aio_context_setup_epoll (ctx, errp); }
include/block/aio-internal.h +15 -0
@@ -29,4 +29,19 @@ struct AioHandler {void aio_context_setup (AioContext *ctx,Error **errp); +void aio_epoll_update (AioContext *ctx,AioHandler *node,bool is_new); + +int aio_epoll (AioContext *ctx,GPollFD *pfds,unsigned npfd,int64_t timeout); + +bool aio_epoll_check_poll (AioContext *ctx,GPollFD *pfds, +unsigned npfd,int64_t timeout); + +void aio_context_setup_epoll (AioContext *ctx,Error **errp); + +static inline void aio_epoll_fill (AioContext *ctx,AioHandler *node) +{ + node->pfd.fd = ctx->epollfd; + node->pfd.events =G_IO_IN |G_IO_OUT |G_IO_HUP |G_IO_ERR ; +} +#endif
include/block/aio.h +5 -0
@@ -122,6 +122,11 @@ struct AioContext {/* TimerLists for calling timers - one per clock type */ QEMUTimerListGroup tlg; + +/* Fields below are used by aio-epoll. */ +int epollfd; +bool epoll_enabled; +bool epoll_available; };/**
stubs/Makefile.objs +1 -0
@@ -39,3 +39,4 @@ stub-obj-y += cpus.o stub-obj-y += kvm.o stub-obj-y += qmp_pc_dimm_device_list.o stub-obj-y += target-monitor-defs.o +stub-obj-y += aio-epoll.o
stubs/aio-epoll.c new file +37 -0
@@ -0,0 +1,37 @@ +/* +* QEMU aio epoll stub functions +* +* Copyright Red Hat, Inc, 2015 +* +* Authors: +* Fam Zheng <[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2. See +* the COPYING file in the top-level directory. +* +* Contributions after 2012-01-13 are licensed under the terms of the +* GNU GPL, version 2 or (at your option) any later version. +*/ + +#include "qemu-common.h" +#include "block/aio-internal.h" + +void aio_epoll_update (AioContext *ctx,AioHandler *node,bool is_new) +{ +} + +int aio_epoll (AioContext *ctx,GPollFD *pfds,unsigned npfd,int64_t timeout) +{ +assert (false); +} + +bool aio_epoll_check_poll (AioContext *ctx,GPollFD *pfds, +unsigned npfd,int64_t timeout) +{ +return false; +} + +void aio_context_setup_epoll (AioContext *ctx,Error **errp) +{ +} +
Signature · 1 line
2.6.1
Re: [Qemu-devel] [PATCH 3/3] aio: Introduce aio-epoll.c
Quoted 12 lines · Fam Zheng
Signed-off-by Fam Zheng <[email protected]>--- Makefile.objs | 1 + aio-epoll.c | 150 +++++++++++++++++++++++++++++++++++++++++++ aio-posix.c | 16 ++++- include/block/aio-internal.h | 15 +++++ include/block/aio.h | 5 ++ stubs/Makefile.objs | 1 + stubs/aio-epoll.c | 37 +++++++++++ 7 files changed, 223 insertions(+), 2 deletions(-) create mode 100644 aio-epoll.c create mode 100644 stubs/aio-epoll.c
aio-epoll.c seems small enough that you can just include everything in aio-posix.c. This should simplify everything and make patch 1 unnecessary, too.
Paolo
Re: [Qemu-devel] [PATCH 3/3] aio: Introduce aio-epoll.c
Quoted 16 lines · Paolo Bonzini
Quoted 12 lines · Fam Zheng
Signed-off-by Fam Zheng <[email protected]>--- Makefile.objs | 1 + aio-epoll.c | 150 +++++++++++++++++++++++++++++++++++++++++++ aio-posix.c | 16 ++++- include/block/aio-internal.h | 15 +++++ include/block/aio.h | 5 ++ stubs/Makefile.objs | 1 + stubs/aio-epoll.c | 37 +++++++++++ 7 files changed, 223 insertions(+), 2 deletions(-) create mode 100644 aio-epoll.c create mode 100644 stubs/aio-epoll.c
Quoted 3 lines
aio-epoll.cseems small enough that you can just include everything inaio-posix.c. This should simplify everything and make patch 1 unnecessary, too.
OK, I can do that.
Fam