aio-posix: polling scalability improvements
cover aio-posix: polling scalability improvements
A guest with 100 virtio-blk-pci,num-queues=32 devices only reaches 10k IOPS while a guest with a single device reaches 105k IOPS (rw=randread,bs=4k,iodepth=1,ioengine=libaio).
The bottleneck is that aio_poll() userspace polling iterates over all AioHandlers to invoke their ->io_poll() callbacks. All AioHandlers are polled even if only one of them was recently active. Therefore a guest with many disks is slower than a guest with a single disk even when the workload only accesses a single disk.
This patch series solves this scalability problem so that IOPS is unaffected by the number of devices. The trick is to poll only AioHandlers that were recently active so that userspace polling scales well.
Unfortunately it's not possible to accomplish this with the existing epoll(7) fd monitoring implementation. This patch series adds a Linux io_uring fd monitoring implementation. The critical feature is that io_uring can check the readiness of file descriptors through userspace polling. This makes it possible to safely poll a subset of AioHandlers from userspace without risk of starving the other AioHandlers.
Stefan Hajnoczi (7):
aio-posix: completely stop polling when disabled
aio-posix: move RCU_READ_LOCK() into run_poll_handlers()
aio-posix: extract ppoll(2) and epoll(7) fd monitoring
aio-posix: simplify FDMonOps->update() prototype
aio-posix: add io_uring fd monitoring implementation
aio-posix: support userspace polling of fd monitoring
aio-posix: remove idle poll handlers to improve scalability
MAINTAINERS | 2 + configure | 5 + include/block/aio.h | 70 ++++++- util/Makefile.objs | 3 + util/aio-posix.c | 449 ++++++++++++++---------------------------- util/aio-posix.h | 81 ++++++++ util/fdmon-epoll.c | 155 +++++++++++++++ util/fdmon-io_uring.c | 332 +++++++++++++++++++++++++++++++ util/fdmon-poll.c | 107 ++++++++++ util/trace-events | 2 + 10 files changed, 898 insertions(+), 308 deletions(-) create mode 100644 util/aio-posix.h create mode 100644 util/fdmon-epoll.c create mode 100644 util/fdmon-io_uring.c create mode 100644 util/fdmon-poll.c
Signature · 1 line
2.24.1
PATCH 1/7 aio-posix: completely stop polling when disabled
One iteration of polling is always performed even when polling is disabled. This is done because:
1. Userspace polling is cheaper than making a syscall. We might get lucky.
2. We must poll once more after polling has stopped in case an event occurred while stopping polling.
However, there are downsides:
1. Polling becomes a bottleneck when the number of event sources is very high. It's more efficient to monitor fds in that case.
2. A high-frequency polling event source can starve non-polling event sources because ppoll(2)/epoll(7) is never invoked.
This patch removes the forced polling iteration so that poll_ns=0 really means no polling.
IOPS increases from 10k to 60k when the guest has 100 virtio-blk-pci,num-queues=32 devices and 1 virtio-blk-pci,num-queues=1 device because the large number of event sources being polled slows down the event loop.
--- util/aio-posix.c | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-)
util/aio-posix.c +15 -7
@@ -361,12 +361,13 @@ void aio_set_event_notifier_poll(AioContext *ctx, (IOHandler *)io_poll_end); } -static void poll_set_started (AioContext *ctx,bool started) +static bool poll_set_started (AioContext *ctx,bool started) {AioHandler *node; +bool progress = false;if (started == ctx->poll_started) { -return ; +return false; } ctx->poll_started = started;
@@ -388,8 +389,15 @@ static void poll_set_started(AioContext *ctx, bool started)if (fn) {fn (node->opaque); } + +/* Poll one last time in case ->io_poll_end() raced with the event */ +if (!started) { + progress = node->io_poll (node->opaque) || progress; + } }qemu_lockcnt_dec (&ctx->list_lock); + +return progress; }
@@ -670,12 +678,12 @@ static bool try_poll_mode(AioContext *ctx, int64_t *timeout) } } -poll_set_started (ctx, false); +if (poll_set_started (ctx, false)) { + *timeout =0 ; +return true; + } -/* Even if we don't run busy polling, try polling once in case it can make -* progress and the caller will be able to avoid ppoll(2)/epoll_wait(2). -*/ -return run_poll_handlers_once (ctx, timeout); +return false; }bool aio_poll (AioContext *ctx,bool blocking)
Signature · 1 line
2.24.1
PATCH 2/7 aio-posix: move RCU_READ_LOCK() into run_poll_handlers()
In reply to Stefan HajnocziNow that run_poll_handlers_once() is only called by run_poll_handlers() we can improve the CPU time profile by moving the expensive RCU_READ_LOCK() out of the polling loop.
This reduces the run_poll_handlers() from 40% CPU to 10% CPU in perf's sampling profiler output.
--- util/aio-posix.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-)
util/aio-posix.c +10 -10
@@ -583,16 +583,6 @@ static bool run_poll_handlers_once(AioContext *ctx, int64_t *timeout)bool progress = false;AioHandler *node; -/* -* Optimization: ->io_poll() handlers often contain RCU read critical -* sections and we therefore see many rcu_read_lock() -> rcu_read_unlock() -* -> rcu_read_lock() -> ... sequences with expensive memory -* synchronization primitives. Make the entire polling loop an RCU -* critical section because nested rcu_read_lock()/rcu_read_unlock() calls -* are cheap. -*/ -RCU_READ_LOCK_GUARD (); - QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) { if (!QLIST_IS_INSERTED(node, node_deleted) && node->io_poll && aio_node_check(ctx, node->is_external) &&
@@ -636,6 +626,16 @@ static bool run_poll_handlers(AioContext *ctx, int64_t max_ns, int64_t *timeout)trace_run_poll_handlers_begin (ctx, max_ns, *timeout); +/* +* Optimization: ->io_poll() handlers often contain RCU read critical +* sections and we therefore see many rcu_read_lock() -> rcu_read_unlock() +* -> rcu_read_lock() -> ... sequences with expensive memory +* synchronization primitives. Make the entire polling loop an RCU +* critical section because nested rcu_read_lock()/rcu_read_unlock() calls +* are cheap. +*/ +RCU_READ_LOCK_GUARD (); + start_time =qemu_clock_get_ns (QEMU_CLOCK_REALTIME ); do { progress = run_poll_handlers_once(ctx, timeout);
Signature · 1 line
2.24.1
Re: [PATCH 2/7] aio-posix: move RCU_READ_LOCK() into run_poll_handlers()
util/aio-posix.c static bool run_poll_handlers(AioContext *ctx, int64_t max_ns, int64_t *timeout) · line 638
Quoted 11 lines · Stefan Hajnoczi
util/aio-posix.c +10
-0+/* +* Optimization: ->io_poll() handlers often contain RCU read critical +* sections and we therefore see many rcu_read_lock() -> rcu_read_unlock() +* -> rcu_read_lock() -> ... sequences with expensive memory +* synchronization primitives. Make the entire polling loop an RCU +* critical section because nested rcu_read_lock()/rcu_read_unlock() calls +* are cheap. +*/ +RCU_READ_LOCK_GUARD (); +
Looks good, but I suggest that you compile with --enable-membarrier as that makes RCU critical sections basically free.
Paolo
Re: [PATCH 2/7] aio-posix: move RCU_READ_LOCK() into run_poll_handlers()
Quoted 13 lines · Paolo Bonzini
util/aio-posix.c static bool run_poll_handlers(AioContext *ctx, int64_t max_ns, int64_t *timeout) · line 638
Quoted 11 lines · Stefan Hajnoczi
util/aio-posix.c +10
-0+/* +* Optimization: ->io_poll() handlers often contain RCU read critical +* sections and we therefore see many rcu_read_lock() -> rcu_read_unlock() +* -> rcu_read_lock() -> ... sequences with expensive memory +* synchronization primitives. Make the entire polling loop an RCU +* critical section because nested rcu_read_lock()/rcu_read_unlock() calls +* are cheap. +*/ +RCU_READ_LOCK_GUARD (); +
Quoted 2 lines
Looks good, but I suggest that you compile with --enable-membarrier as that makes RCU critical sections basically free.
Interesting, --enable-membarrier decreases performance from 105k to 97k IOPS in the NVMe latency benchmark that I'm running.
Stefan
PATCH 3/7 aio-posix: extract ppoll(2) and epoll(7) fd monitoring
In reply to Stefan HajnocziThe ppoll(2) and epoll(7) file descriptor monitoring implementations are mixed with the core util/aio-posix.c code. Before adding another implementation for Linux io_uring, extract out the existing ones so there is a clear interface and the core code is simpler.
The new interface is AioContext->fdmon_ops, a pointer to a FDMonOps struct. See the patch for details.
Semantic changes:
1. ppoll(2) now reflects events from pollfds[] back into AioHandlers while we're still on the clock for adaptive polling. This was already happening for epoll(7), so if it's really an issue then we'll need to fix both in the future.
2. epoll(7)'s fallback to ppoll(2) while external events are disabled was broken when the number of fds exceeded the epoll(7) upgrade threshold. I guess this code path simply wasn't tested and no one noticed the bug. I didn't go out of my way to fix it but the correct code is simpler than preserving the bug.
I also took some liberties in removing the unnecessary AioContext->epoll_available (just check AioContext->epollfd != -1 instead) and AioContext->epoll_enabled (it's implicit if our AioContext->fdmon_ops callbacks are being invoked) fields.
--- MAINTAINERS | 2 + include/block/aio.h | 36 +++++- util/Makefile.objs | 2 + util/aio-posix.c | 286 ++------------------------------------------ util/aio-posix.h | 61 ++++++++++ util/fdmon-epoll.c | 151 +++++++++++++++++++++++ util/fdmon-poll.c | 104 ++++++++++++++++ 7 files changed, 366 insertions(+), 276 deletions(-) create mode 100644 util/aio-posix.h create mode 100644 util/fdmon-epoll.c create mode 100644 util/fdmon-poll.c
MAINTAINERS +2 -0
@@ -1884,6 +1884,8 @@ L: [email protected] S: Supported F: util/async.c F: util/aio-*.c +F: util/aio-*.h +F: util/fdmon-*.c F: block/io.c F: migration/block* F: include/block/aio.h
include/block/aio.h +34 -2
@@ -52,6 +52,38 @@ struct ThreadPool;struct LinuxAioState ;struct LuringState ; +/* Callbacks for file descriptor monitoring implementations */ +typedef struct { +/* +* update: +* @ctx: the AioContext +* @node: the handler +* @is_new: is the file descriptor already being monitored? +* +* Add/remove/modify a monitored file descriptor. There are three cases: +* 1. node->pfd.events == 0 means remove the file descriptor. +* 2. !is_new means modify an already monitored file descriptor. +* 3. is_new means add a new file descriptor. +* +* Called with ctx->list_lock acquired. +*/ +void (*update)(AioContext *ctx,AioHandler *node,bool is_new); + +/* +* wait: +* @ctx: the AioContext +* @ready_list: list for handlers that become ready +* @timeout: maximum duration to wait, in nanoseconds +* +* Wait for file descriptors to become ready and place them on ready_list. +* +* Called with ctx->list_lock incremented but not locked. +* +* Returns: number of ready file descriptors. +*/ +int (*wait)(AioContext *ctx,AioHandlerList *ready_list,int64_t timeout); +}FDMonOps ; +/* * Each aio_bh_poll() call carves off a slice of the BH list, so that newly * scheduled BHs are not processed until the next aio_bh_poll() call. All
@@ -173,8 +205,8 @@ struct AioContext {/* epoll(7) state used when built with CONFIG_EPOLL */ int epollfd; -bool epoll_enabled; -bool epoll_available; + +const FDMonOps *fdmon_ops; };/**
util/Makefile.objs +2 -0
@@ -5,6 +5,8 @@ util-obj-y += aiocb.o async.o aio-wait.o thread-pool.o qemu-timer.o util-obj-y += main-loop.o util-obj-$(call lnot,$(CONFIG_ATOMIC64)) += atomic64.o util-obj-$(CONFIG_POSIX) += aio-posix.o +util-obj-$(CONFIG_POSIX) += fdmon-poll.o +util-obj-$(CONFIG_EPOLL_CREATE1) += fdmon-epoll.o util-obj-$(CONFIG_POSIX) += compatfd.o util-obj-$(CONFIG_POSIX) += event_notifier-posix.o util-obj-$(CONFIG_POSIX) += mmap-alloc.o
util/aio-posix.c +12 -274
341 lines not on this page · raw message
util/aio-posix.h new file +61 -0
61 lines not on this page · raw message
util/fdmon-epoll.c new file +151 -0
151 lines not on this page · raw message
util/fdmon-poll.c new file +104 -0
104 lines not on this page · raw message
Signature · 1 line
2.24.1
PATCH 4/7 aio-posix: simplify FDMonOps->update() prototype
In reply to Stefan HajnocziThe AioHandler *node, bool is_new arguments are more complicated to think about than simply being given AioHandler *old_node, AioHandler *new_node.
Furthermore, the new Linux io_uring file descriptor monitoring mechanism added by the new patch requires access to both the old and the new nodes. Make this change now in preparation.
--- include/block/aio.h | 13 ++++++------- util/aio-posix.c | 7 +------ util/fdmon-epoll.c | 21 ++++++++++++--------- util/fdmon-poll.c | 4 +++- 4 files changed, 22 insertions(+), 23 deletions(-)
include/block/aio.h +6 -7
@@ -57,17 +57,16 @@ typedef struct {/* * update: * @ctx: the AioContext -* @node: the handler -* @is_new: is the file descriptor already being monitored? +* @old_node: the existing handler or NULL if this file descriptor is being +* monitored for the first time +* @new_node: the new handler or NULL if this file descriptor is being +* removed * -* Add/remove/modify a monitored file descriptor. There are three cases: -* 1. node->pfd.events == 0 means remove the file descriptor. -* 2. !is_new means modify an already monitored file descriptor. -* 3. is_new means add a new file descriptor. +* Add/remove/modify a monitored file descriptor. * * Called with ctx->list_lock acquired. */ -void (*update)(AioContext *ctx,AioHandler *node,bool is_new); +void (*update)(AioContext *ctx,AioHandler *old_node,AioHandler *new_node);/* * wait:
util/aio-posix.c +1 -6
@@ -139,12 +139,7 @@ void aio_set_fd_handler(AioContext *ctx,atomic_set (&ctx->poll_disable_cnt,atomic_read (&ctx->poll_disable_cnt) + poll_disable_change); -if (new_node) { - ctx->fdmon_ops->update (ctx, new_node, is_new); - }else if (node) { -/* Unregister deleted fd_handler */ - ctx->fdmon_ops->update (ctx, node, false); - } + ctx->fdmon_ops->update (ctx, node, new_node);qemu_lockcnt_unlock (&ctx->list_lock);aio_notify (ctx);
util/fdmon-epoll.c +12 -9
@@ -30,21 +30,24 @@ static inline int epoll_events_from_pfd(int pfd_events) (pfd_events &G_IO_ERR ?EPOLLERR :0 ); } -static void fdmon_epoll_update (AioContext *ctx,AioHandler *node,bool is_new) +static void fdmon_epoll_update (AioContext *ctx, +AioHandler *old_node, +AioHandler *new_node) { -struct epoll_event event; +struct epoll_event event = { + .data.ptr = new_node, + .events = new_node ?epoll_events_from_pfd (new_node->pfd.events) :0 , + };int r; -int ctl; -if (!node->pfd.events) { - ctl =EPOLL_CTL_DEL ; +if (!new_node) { + r =epoll_ctl (ctx->epollfd,EPOLL_CTL_DEL , old_node->pfd.fd, &event); + }else if (!old_node) { + r =epoll_ctl (ctx->epollfd,EPOLL_CTL_ADD , new_node->pfd.fd, &event); }else { - event.data.ptr = node; - event.events =epoll_events_from_pfd (node->pfd.events); - ctl = is_new ?EPOLL_CTL_ADD :EPOLL_CTL_MOD ; + r =epoll_ctl (ctx->epollfd,EPOLL_CTL_MOD , new_node->pfd.fd, &event); } - r =epoll_ctl (ctx->epollfd, ctl, node->pfd.fd, &event);if (r) {fdmon_epoll_disable (ctx); }
util/fdmon-poll.c +3 -1
@@ -93,7 +93,9 @@ static int fdmon_poll_wait(AioContext *ctx, AioHandlerList *ready_list,return ret; } -static void fdmon_poll_update (AioContext *ctx,AioHandler *node,bool is_new) +static void fdmon_poll_update (AioContext *ctx, +AioHandler *old_node, +AioHandler *new_node) {/* Do nothing, AioHandler already contains the state we'll need */ }
Signature · 1 line
2.24.1
PATCH 5/7 aio-posix: add io_uring fd monitoring implementation
In reply to Stefan HajnocziThe recent Linux io_uring API has several advantages over ppoll(2) and epoll(2). Details are given in the source code.
Add an io_uring implementation and make it the default on Linux. Performance is the same as with epoll(7) but later patches add optimizations that take advantage of io_uring.
It is necessary to change how aio_set_fd_handler() deals with deleting AioHandlers since removing monitored file descriptors is asynchronous in io_uring. fdmon_io_uring_remove() marks the AioHandler deleted and aio_set_fd_handler() will let it handle deletion in that case.
--- configure | 5 + include/block/aio.h | 9 ++ util/Makefile.objs | 1 + util/aio-posix.c | 20 ++- util/aio-posix.h | 20 ++- util/fdmon-io_uring.c | 326 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 376 insertions(+), 5 deletions(-) create mode 100644 util/fdmon-io_uring.c
configure +5 -0
@@ -4093,6 +4093,11 @@ if test "$linux_io_uring" != "no" ; then linux_io_uring_cflags=$($pkg_config --cflags liburing) linux_io_uring_libs=$($pkg_config --libs liburing) linux_io_uring=yes + + # io_uring is used in libqemuutil.a where per-file -libs variables are not + # seen by programs linking the archive. It's not ideal, but just add the + # library dependency globally. + LIBS="$linux_io_uring_libs $LIBS" else if test "$linux_io_uring" = "yes" ; then feature_not_found "linux io_uring" "Install liburing devel"
include/block/aio.h +9 -0
@@ -14,6 +14,9 @@#ifndef QEMU_AIO_H #define QEMU_AIO_H +#ifdef CONFIG_LINUX_IO_URING +#include <liburing.h> +#endif #include "qemu/queue.h" #include "qemu/event_notifier.h" #include "qemu/thread.h"
@@ -96,6 +99,8 @@ struct BHListSlice {QSIMPLEQ_ENTRY (BHListSlice) next; }; +typedef QSLIST_HEAD(, AioHandler) AioHandlerSList; +struct AioContext {GSource source;
@@ -181,6 +186,10 @@ struct AioContext {* locking. */ struct LuringState *linux_io_uring; + +/* State for file descriptor monitoring using Linux io_uring */ +struct io_uring fdmon_io_uring; +AioHandlerSList submit_list;#endif /* TimerLists for calling timers - one per clock type. Has its own
util/Makefile.objs +1 -0
@@ -7,6 +7,7 @@ util-obj-$(call lnot,$(CONFIG_ATOMIC64)) += atomic64.o util-obj-$(CONFIG_POSIX) += aio-posix.o util-obj-$(CONFIG_POSIX) += fdmon-poll.o util-obj-$(CONFIG_EPOLL_CREATE1) += fdmon-epoll.o +util-obj-$(CONFIG_LINUX_IO_URING) += fdmon-io_uring.o util-obj-$(CONFIG_POSIX) += compatfd.o util-obj-$(CONFIG_POSIX) += event_notifier-posix.o util-obj-$(CONFIG_POSIX) += mmap-alloc.o
util/aio-posix.c +16 -4
@@ -57,10 +57,16 @@ static bool aio_remove_fd_handler(AioContext *ctx, AioHandler *node)g_source_remove_poll (&ctx->source, &node->pfd); } + node->pfd.revents =0 ; + +/* If the fd monitor has already marked it deleted, leave it alone */ +if (QLIST_IS_INSERTED (node, node_deleted)) { +return false; + } +/* If a read is in progress, just mark the node as deleted */ if (qemu_lockcnt_count (&ctx->list_lock)) {QLIST_INSERT_HEAD_RCU (&ctx->deleted_aio_handlers, node, node_deleted); - node->pfd.revents =0 ;return false; }/* Otherwise, delete it for real. We can't just mark it as
@@ -126,9 +132,6 @@ void aio_set_fd_handler(AioContext *ctx,QLIST_INSERT_HEAD_RCU (&ctx->aio_handlers, new_node, node); } -if (node) { - deleted =aio_remove_fd_handler (ctx, node); - }/* No need to order poll_disable_cnt writes against other updates; * the counter is only used to avoid wasting time and latency on
@@ -140,6 +143,9 @@ void aio_set_fd_handler(AioContext *ctx, atomic_read(&ctx->poll_disable_cnt) + poll_disable_change); ctx->fdmon_ops->update (ctx, node, new_node); +if (node) { + deleted =aio_remove_fd_handler (ctx, node); + }qemu_lockcnt_unlock (&ctx->list_lock);aio_notify (ctx);
@@ -565,11 +571,17 @@ void aio_context_setup(AioContext *ctx) ctx->fdmon_ops = &fdmon_poll_ops; ctx->epollfd =-1 ; +/* Use the fastest fd monitoring implementation if available */ +if (fdmon_io_uring_setup (ctx)) { +return ; + } +fdmon_epoll_setup (ctx); }void aio_context_destroy (AioContext *ctx) { +fdmon_io_uring_destroy (ctx);fdmon_epoll_disable (ctx); }
util/aio-posix.h +19 -1
@@ -27,10 +27,14 @@ struct AioHandler {IOHandler *io_poll_begin;IOHandler *io_poll_end;void *opaque; -bool is_external;QLIST_ENTRY (AioHandler) node;QLIST_ENTRY (AioHandler) node_ready;/* only used during aio_poll() */ QLIST_ENTRY (AioHandler) node_deleted; +#ifdef CONFIG_LINUX_IO_URING +QSLIST_ENTRY (AioHandler) node_submitted; +unsigned flags;/* see fdmon-io_uring.c */ +#endif +bool is_external; };/* Add a handler to a ready list */
@@ -58,4 +62,18 @@ static inline void fdmon_epoll_disable(AioContext *ctx) }#endif /* !CONFIG_EPOLL_CREATE1 */ +#ifdef CONFIG_LINUX_IO_URING +bool fdmon_io_uring_setup (AioContext *ctx); +void fdmon_io_uring_destroy (AioContext *ctx); +#else +static inline bool fdmon_io_uring_setup (AioContext *ctx) +{ +return false; +} + +static inline void fdmon_io_uring_destroy (AioContext *ctx) +{ +} +#endif /* !CONFIG_LINUX_IO_URING */ +#endif /* AIO_POSIX_H */
util/fdmon-io_uring.c new file +326 -0
326 lines not on this page · raw message
Signature · 1 line
2.24.1
PATCH 6/7 aio-posix: support userspace polling of fd monitoring
In reply to Stefan HajnocziUnlike ppoll(2) and epoll(7), Linux io_uring completions can be polled from userspace. Previously userspace polling was only allowed when all AioHandler's had an ->io_poll() callback. This prevented starvation of fds by userspace pollable handlers.
Add the FDMonOps->need_wait() callback that enables userspace polling even when some AioHandlers lack ->io_poll().
For example, it's now possible to do userspace polling when a TCP/IP socket is monitored thanks to Linux io_uring.
--- include/block/aio.h | 19 +++++++++++++++++++ util/aio-posix.c | 11 ++++++++--- util/fdmon-epoll.c | 1 + util/fdmon-io_uring.c | 6 ++++++ util/fdmon-poll.c | 1 + 5 files changed, 35 insertions(+), 3 deletions(-)
include/block/aio.h +19 -0
@@ -55,6 +55,9 @@ struct ThreadPool;struct LinuxAioState ;struct LuringState ; +/* Is polling disabled? */ +bool aio_poll_disabled (AioContext *ctx); +/* Callbacks for file descriptor monitoring implementations */ typedef struct { /*
@@ -84,6 +87,22 @@ typedef struct {* Returns: number of ready file descriptors. */ int (*wait)(AioContext *ctx,AioHandlerList *ready_list,int64_t timeout); + +/* +* need_wait: +* @ctx: the AioContext +* +* Tell aio_poll() when to stop userspace polling early because ->wait() +* has fds ready. +* +* File descriptor monitoring implementations that cannot poll fd readiness +* from userspace should use aio_poll_disabled() here. This ensures that +* file descriptors are not starved by handlers that frequently make +* progress via userspace polling. +* +* Returns: true if ->wait() should be called, false otherwise. +*/ +bool (*need_wait)(AioContext *ctx); } FDMonOps;/*
util/aio-posix.c +8 -3
@@ -22,6 +22,11 @@#include "trace.h" #include "aio-posix.h" +bool aio_poll_disabled (AioContext *ctx) +{ +return atomic_read (&ctx->poll_disable_cnt); +} +void aio_add_ready_handler (AioHandlerList *ready_list,AioHandler *node,int revents)
@@ -423,7 +428,7 @@ static bool run_poll_handlers(AioContext *ctx, int64_t max_ns, int64_t *timeout) elapsed_time =qemu_clock_get_ns (QEMU_CLOCK_REALTIME ) - start_time; max_ns =qemu_soonest_timeout (*timeout, max_ns);assert (!(max_ns && progress)); - }while (elapsed_time < max_ns && !atomic_read (&ctx->poll_disable_cnt)); + }while (elapsed_time < max_ns && !ctx->fdmon_ops->need_wait (ctx));/* If time has passed with no successful polling, adjust *timeout to * keep the same ending time.
@@ -451,7 +456,7 @@ static bool try_poll_mode(AioContext *ctx, int64_t *timeout) {int64_t max_ns =qemu_soonest_timeout (*timeout, ctx->poll_ns); -if (max_ns && !atomic_read (&ctx->poll_disable_cnt)) { +if (max_ns && !ctx->fdmon_ops->need_wait (ctx)) {poll_set_started (ctx, true);if (run_poll_handlers (ctx, max_ns, timeout)) {
@@ -501,7 +506,7 @@ bool aio_poll(AioContext *ctx, bool blocking)/* If polling is allowed, non-blocking aio_poll does not need the * system call---a single round of run_poll_handlers_once suffices. */ -if (timeout ||atomic_read (&ctx->poll_disable_cnt)) { +if (timeout || ctx->fdmon_ops->need_wait (ctx)) { ret = ctx->fdmon_ops->wait (ctx, &ready_list, timeout); }
util/fdmon-epoll.c +1 -0
@@ -100,6 +100,7 @@ out:static const FDMonOps fdmon_epoll_ops = { .update = fdmon_epoll_update, .wait = fdmon_epoll_wait, + .need_wait = aio_poll_disabled, };static bool fdmon_epoll_try_enable (AioContext *ctx)
util/fdmon-io_uring.c +6 -0
@@ -288,9 +288,15 @@ static int fdmon_io_uring_wait(AioContext *ctx, AioHandlerList *ready_list,return process_cq_ring (ctx, ready_list); } +static bool fdmon_io_uring_need_wait (AioContext *ctx) +{ +return io_uring_cq_ready (&ctx->fdmon_io_uring); +} +static const FDMonOps fdmon_io_uring_ops = { .update = fdmon_io_uring_update, .wait = fdmon_io_uring_wait, + .need_wait = fdmon_io_uring_need_wait, };bool fdmon_io_uring_setup (AioContext *ctx)
util/fdmon-poll.c +1 -0
@@ -103,4 +103,5 @@ static void fdmon_poll_update(AioContext *ctx,const FDMonOps fdmon_poll_ops = { .update = fdmon_poll_update, .wait = fdmon_poll_wait, + .need_wait = aio_poll_disabled, };
Signature · 1 line
2.24.1
PATCH 7/7 aio-posix: remove idle poll handlers to improve scalability
In reply to Stefan HajnocziWhen there are many poll handlers it's likely that some of them are idle most of the time. Remove handlers that haven't had activity recently so that the polling loop scales better for guests with a large number of devices.
This feature only takes effect for the Linux io_uring fd monitoring implementation because it is capable of combining fd monitoring with userspace polling. The other implementations can't do that and risk starving fds in favor of poll handlers, so don't try this optimization when they are in use.
IOPS improves from 10k to 105k when the guest has 100 virtio-blk-pci,num-queues=32 devices and 1 virtio-blk-pci,num-queues=1 device for rw=randread,iodepth=1,bs=4k,ioengine=libaio on NVMe.
--- include/block/aio.h | 7 ++++ util/aio-posix.c | 93 +++++++++++++++++++++++++++++++++++++++++---- util/aio-posix.h | 2 + util/trace-events | 2 + 4 files changed, 97 insertions(+), 7 deletions(-)
include/block/aio.h +7 -0
@@ -227,6 +227,13 @@ struct AioContext {int64_t poll_grow;/* polling time growth factor */ int64_t poll_shrink;/* polling time shrink factor */ +/* +* List of handlers participating in userspace polling. Accessed almost +* exclusively from aio_poll() and therefore not an RCU list. Protected by +* ctx->list_lock. +*/ +AioHandlerList poll_aio_handlers; +/* Are we in polling mode or monitoring file descriptors? */ bool poll_started;
util/aio-posix.c +86 -7
158 lines not on this page · raw message
util/aio-posix.h +2 -0
@@ -30,10 +30,12 @@ struct AioHandler {QLIST_ENTRY (AioHandler) node;QLIST_ENTRY (AioHandler) node_ready;/* only used during aio_poll() */ QLIST_ENTRY (AioHandler) node_deleted; +QLIST_ENTRY (AioHandler) node_poll;#ifdef CONFIG_LINUX_IO_URING QSLIST_ENTRY (AioHandler) node_submitted;unsigned flags;/* see fdmon-io_uring.c */ #endif +int64_t poll_idle_timeout;/* when to stop userspace polling */ bool is_external; };
util/trace-events +2 -0
@@ -5,6 +5,8 @@ run_poll_handlers_begin(void *ctx, int64_t max_ns, int64_t timeout) "ctx %p max_ run_poll_handlers_end(void *ctx, bool progress, int64_t timeout) "ctx %p progress %d new timeout %"PRId64 poll_shrink(void *ctx, int64_t old, int64_t new) "ctx %p old %"PRId64" new %"PRId64 poll_grow(void *ctx, int64_t old, int64_t new) "ctx %p old %"PRId64" new %"PRId64 +poll_add(void *ctx, void *node, int fd, unsigned revents) "ctx %p node %p fd %d revents 0x%x" +poll_remove(void *ctx, void *node, int fd) "ctx %p node %p fd %d" # async.c aio_co_schedule(void *ctx, void *co) "ctx %p co %p"
Signature · 1 line
2.24.1
Re: [PATCH 7/7] aio-posix: remove idle poll handlers to improve scalability
include/block/aio.h struct AioContext { · line 236
Quoted 7 lines · Stefan Hajnoczi
include/block/aio.h +7
-0+/* +* List of handlers participating in userspace polling. Accessed almost +* exclusively from aio_poll() and therefore not an RCU list. Protected by +* ctx->list_lock. +*/ +AioHandlerList poll_aio_handlers; +
Not sure I understand the "almost" part. If it's accessed only from aio_poll() it is protected via either AIO_WAIT_WHILE or the BQL, not by ctx->list_lock; if it's protected by ctx->list_lock (using qemu_lockcnt_inc in readers), it is an RCU list.
Paolo
Re: [PATCH 7/7] aio-posix: remove idle poll handlers to improve scalability
Quoted 9 lines · Paolo Bonzini
include/block/aio.h struct AioContext { · line 236
Quoted 7 lines · Stefan Hajnoczi
include/block/aio.h +7
-0+/* +* List of handlers participating in userspace polling. Accessed almost +* exclusively from aio_poll() and therefore not an RCU list. Protected by +* ctx->list_lock. +*/ +AioHandlerList poll_aio_handlers; +
Quoted 4 lines
Not sure I understand the "almost" part. If it's accessed only from
aio_poll()it is protected via eitherAIO_WAIT_WHILEor the BQL, not byctx->list_lock; if it's protected byctx->list_lock(usingqemu_lockcnt_incin readers), it is an RCU list.
aio_remove_fd_handler() removes nodes from the list during aio_set_fd_handler(), but only while holding ctx->list_lock and the count is zero (no readers).
All other access is done from with ctx->list_lock incremented. This code needs to be reentrant in case of nested aio_poll() but nothing else will access the list at the same time.
Therefore RCU is not needed. ctx->list_lock acts more like a rwlock.
Stefan
Re: [PATCH 7/7] aio-posix: remove idle poll handlers to improve scalability
Quoted 11 lines · Stefan Hajnoczi
Quoted 4 lines
Not sure I understand the "almost" part. If it's accessed only from
aio_poll()it is protected via eitherAIO_WAIT_WHILEor the BQL, not byctx->list_lock; if it's protected byctx->list_lock(usingqemu_lockcnt_incin readers), it is an RCU list.
aio_remove_fd_handler()removes nodes from the list duringaio_set_fd_handler(), but only while holdingctx->list_lockand the count is zero (no readers).All other access is done from with
ctx->list_lockincremented. This code needs to be reentrant in case of nestedaio_poll()but nothing else will access the list at the same time.
Oh, I see, adds are only done under ctx->list_lock and those are the part that need the write barriers in the RCU iterators.
Paolo
Quoted 1 line
Therefore RCU is not needed.
ctx->list_lockacts more like a rwlock.
Re: [PATCH 7/7] aio-posix: remove idle poll handlers to improve scalability
Quoted 9 lines · Paolo Bonzini
Quoted 8 lines · Stefan Hajnoczi
Quoted 4 lines
Not sure I understand the "almost" part. If it's accessed only from
aio_poll()it is protected via eitherAIO_WAIT_WHILEor the BQL, not byctx->list_lock; if it's protected byctx->list_lock(usingqemu_lockcnt_incin readers), it is an RCU list.
aio_remove_fd_handler()removes nodes from the list duringaio_set_fd_handler(), but only while holdingctx->list_lockand the count is zero (no readers).
Quoted 6 lines
Quoted 3 lines
All other access is done from with
ctx->list_lockincremented. This code needs to be reentrant in case of nestedaio_poll()but nothing else will access the list at the same time.Oh, I see, adds are only done under
ctx->list_lockand those are the part that need the write barriers in the RCU iterators.
I'll update the comment when merging this series.
Stefan
Stefan Hajnoczi
AppliedQuoted 30 lines · Stefan Hajnoczi
A guest with 100 virtio-blk-pci,num-queues=32 devices only reaches 10k IOPS while a guest with a single device reaches 105k IOPS (rw=randread,bs=4k,iodepth=1,ioengine=libaio).
The bottleneck is that
aio_poll()userspace polling iterates over all AioHandlers to invoke their ->io_poll() callbacks. All AioHandlers are polled even if only one of them was recently active. Therefore a guest with many disks is slower than a guest with a single disk even when the workload only accesses a single disk.This patch series solves this scalability problem so that IOPS is unaffected by the number of devices. The trick is to poll only AioHandlers that were recently active so that userspace polling scales well.
Unfortunately it's not possible to accomplish this with the existing epoll(7) fd monitoring implementation. This patch series adds a Linux
io_uringfd monitoring implementation. The critical feature is thatio_uringcan check the readiness of file descriptors through userspace polling. This makes it possible to safely poll a subset of AioHandlers from userspace without risk of starving the other AioHandlers.Stefan Hajnoczi (7):
aio-posix: completely stop polling when disabled
aio-posix: moveRCU_READ_LOCK()intorun_poll_handlers()
aio-posix: extract ppoll(2) and epoll(7) fd monitoring
aio-posix: simplifyFDMonOps->update()prototype
aio-posix: addio_uringfd monitoring implementation
aio-posix: support userspace polling of fd monitoring
aio-posix: remove idle poll handlers to improve scalability
Quoted 19 lines
MAINTAINERS | 2 + configure | 5 + include/block/aio.h | 70 ++++++- util/Makefile.objs | 3 + util/aio-posix.c | 449 ++++++++++++++---------------------------- util/aio-posix.h | 81 ++++++++ util/fdmon-epoll.c | 155 +++++++++++++++ util/fdmon-io_uring.c | 332 +++++++++++++++++++++++++++++++ util/fdmon-poll.c | 107 ++++++++++ util/trace-events | 2 + 10 files changed, 898 insertions(+), 308 deletions(-) create mode 100644 util/aio-posix.h create mode 100644 util/fdmon-epoll.c create mode 100644 util/fdmon-io_uring.c create mode 100644 util/fdmon-poll.cSignature · 1 line
2.24.1
Thanks, applied to my block tree:
https://github.com/stefanha/qemu/commits/block
Stefan