main-loop: Use epoll on Linux
v2 cover main-loop: Use epoll on Linux
v2: Introduce patch 1 to allow qemu_epoll to access AioContext.
Don't pollute g_* namespace. (Stefan)
Fam Zheng (2):
main-loop: Pass AioContext into qemu_poll_ns
main-loop: Use epoll on Linux
Makefile.objs | 1 + aio-posix.c | 2 +- include/block/aio.h | 16 +++++ include/qemu/main-loop.h | 1 + include/qemu/timer.h | 3 +- main-loop.c | 6 +- qemu-epoll.c | 163 +++++++++++++++++++++++++++++++++++++++++++++++ qemu-timer.c | 6 +- tests/Makefile | 2 +- 9 files changed, 193 insertions(+), 7 deletions(-) create mode 100644 qemu-epoll.c
Signature · 1 line
1.9.3
PATCH v2 1/2 main-loop: Pass AioContext into qemu_poll_ns
qemu_poll_ns may ultilize the information in AioContext to achieve better performance.
--- aio-posix.c | 2 +- include/qemu/timer.h | 3 ++- main-loop.c | 6 ++++-- qemu-timer.c | 2 +- 4 files changed, 8 insertions(+), 5 deletions(-)
aio-posix.c +1 -1
@@ -228,7 +228,7 @@ bool aio_poll(AioContext *ctx, bool blocking) ctx->walking_handlers--;/* wait until next event */ - ret =qemu_poll_ns ((GPollFD *)ctx->pollfds->data, + ret =qemu_poll_ns (ctx, (GPollFD *)ctx->pollfds->data, ctx->pollfds->len, blocking ?aio_compute_timeout (ctx) :0 );
include/qemu/timer.h +2 -1
@@ -647,6 +647,7 @@ int qemu_timeout_ns_to_ms(int64_t ns);/** * qemu_poll_ns: +* @ctx: Aio Context we are polling in * @fds: Array of file descriptors * @nfds: number of file descriptors * @timeout: timeout in nanoseconds
@@ -656,7 +657,7 @@ int qemu_timeout_ns_to_ms(int64_t ns);* * Returns: number of fds ready */ -int qemu_poll_ns (GPollFD *fds,guint nfds,int64_t timeout); +int qemu_poll_ns (AioContext *ctx,GPollFD *fds,guint nfds,int64_t timeout);/** * qemu_soonest_timeout:
main-loop.c +4 -2
@@ -234,7 +234,8 @@ static int os_host_main_loop_wait(int64_t timeout) spin_counter++; } - ret =qemu_poll_ns ((GPollFD *)gpollfds->data, gpollfds->len, timeout); + ret =qemu_poll_ns (qemu_aio_context, (GPollFD *)gpollfds->data, + gpollfds->len, timeout);if (timeout) {qemu_mutex_lock_iothread ();
@@ -439,7 +440,8 @@ static int os_host_main_loop_wait(int64_t timeout) poll_timeout_ns =qemu_soonest_timeout (poll_timeout_ns, timeout);qemu_mutex_unlock_iothread (); - g_poll_ret =qemu_poll_ns (poll_fds, n_poll_fds + w->num, poll_timeout_ns); + g_poll_ret =qemu_poll_ns (qemu_aio_context, + poll_fds, n_poll_fds + w->num, poll_timeout_ns);qemu_mutex_lock_iothread ();if (g_poll_ret >0 ) {
qemu-timer.c +1 -1
@@ -307,7 +307,7 @@ int qemu_timeout_ns_to_ms(int64_t ns)/* qemu implementation of g_poll which uses a nanosecond timeout but is * otherwise identical to g_poll */ -int qemu_poll_ns (GPollFD *fds,guint nfds,int64_t timeout) +int qemu_poll_ns (AioContext *ctx,GPollFD *fds,guint nfds,int64_t timeout) {#ifdef CONFIG_PPOLL if (timeout <0 ) {
Signature · 1 line
1.9.3
Re: [Qemu-devel] [PATCH v2 1/2] main-loop: Pass AioContext into qemu_poll_ns
Quoted 15 lines · Fam Zheng
main-loop.c +2
-1@@ -234,7 +234,8 @@ static int os_host_main_loop_wait(int64_t timeout) spin_counter++; } - ret =qemu_poll_ns ((GPollFD *)gpollfds->data, gpollfds->len, timeout); + ret =qemu_poll_ns (qemu_aio_context, (GPollFD *)gpollfds->data, + gpollfds->len, timeout);if (timeout) {qemu_mutex_lock_iothread ();
main-loop.c static int os_host_main_loop_wait(int64_t timeout) · line 447
Quoted 9 lines
main-loop.c +2
-1@@ -439,7 +440,8 @@ static int os_host_main_loop_wait(int64_t timeout) poll_timeout_ns =qemu_soonest_timeout (poll_timeout_ns, timeout);qemu_mutex_unlock_iothread (); - g_poll_ret =qemu_poll_ns (poll_fds, n_poll_fds + w->num, poll_timeout_ns); + g_poll_ret =qemu_poll_ns (qemu_aio_context, + poll_fds, n_poll_fds + w->num, poll_timeout_ns);qemu_mutex_lock_iothread ();if (g_poll_ret >0 ) {
This is a hack.
The immediate problem is that we're not holding the QEMU global mutex but are accessing qemu_aio_context. What if other threads touch qemu_aio_context while they hold the QEMU global mutex?
You didn't indicate what thread-safety rules need to be obeyed so I wonder whether you looked into this.
I'm also concerned that this is somewhat abusing AioContext. You want to stash fields in there but this poll call is about more than just AioContext, it also includes non-AioContext file descriptors. So it seems like a kludge to put the fields into AioContext.
Can you put the state into a separate struct so it's easy to understand its thread-safety characteristics?
Stefan
PATCH v2 2/2 main-loop: Use epoll on Linux
In reply to Fam ZhengA new implementation for qemu_poll_ns based on epoll is introduced here to address the slowness of g_poll and ppoll when the number of fds are high.
On my laptop this would reduce the virtio-blk on top of null-aio device's response time from 32 us to 29 us with few fds (~10), and 48 us to 32 us with more fds (for example when virtio-serial is plugged and ~64 more io handlers are enabled).
--- Makefile.objs | 1 + include/block/aio.h | 16 +++++ include/qemu/main-loop.h | 1 + qemu-epoll.c | 163 +++++++++++++++++++++++++++++++++++++++++++++++ qemu-timer.c | 4 +- tests/Makefile | 2 +- 6 files changed, 185 insertions(+), 2 deletions(-) create mode 100644 qemu-epoll.c
Makefile.objs +1 -0
@@ -9,6 +9,7 @@ util-obj-y = util/ qobject/ qapi/ 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_LINUX) += qemu-epoll.o block-obj-$(CONFIG_POSIX) += aio-posix.o block-obj-$(CONFIG_WIN32) += aio-win32.o block-obj-y += block/
include/block/aio.h +16 -0
@@ -90,6 +90,22 @@ struct AioContext {/* TimerLists for calling timers - one per clock type */ QEMUTimerListGroup tlg; + +#ifdef CONFIG_LINUX +struct EpollState { +/* A copy of last fd array, used to skip epoll_prepare when nothing +* changed. */ +GPollFD *last_fds; +guint last_nfds; +/* An array of fds that failed epoll_ctl and fall back to ppoll. Rare +* case too. */ +GPollFD *g_poll_fds; +guint g_poll_nfds; +int *g_poll_fd_idx; +int epollfd; + } epoll_state; +#endif + };/* Used internally to synchronize aio_poll against qemu_bh_schedule. */
include/qemu/main-loop.h +1 -0
@@ -307,5 +307,6 @@ void qemu_iohandler_poll(GArray *pollfds, int rc);QEMUBH *qemu_bh_new (QEMUBHFunc *cb,void *opaque);void qemu_bh_schedule_idle (QEMUBH *bh); +int qemu_epoll (AioContext *ctx,GPollFD *fds,guint nfds,int64_t timeout);#endif
qemu-epoll.c new file +163 -0
163 lines not on this page · raw message
qemu-timer.c +3 -1
@@ -309,7 +309,9 @@ int qemu_timeout_ns_to_ms(int64_t ns)*/ int qemu_poll_ns (AioContext *ctx,GPollFD *fds,guint nfds,int64_t timeout) { -#ifdef CONFIG_PPOLL +#ifdef CONFIG_LINUX +return qemu_epoll (ctx, fds, nfds, timeout); +#elif CONFIG_PPOLL if (timeout <0 ) {return ppoll ((struct pollfd *)fds, nfds,NULL ,NULL ); }else {
tests/Makefile +1 -1
@@ -347,7 +347,7 @@ tests/usb-hcd-ohci-test$(EXESUF): tests/usb-hcd-ohci-test.o tests/usb-hcd-uhci-test$(EXESUF): tests/usb-hcd-uhci-test.o tests/usb-hcd-ehci-test$(EXESUF): tests/usb-hcd-ehci-test.o $(libqos-pc-obj-y) tests/usb-hcd-xhci-test$(EXESUF): tests/usb-hcd-xhci-test.o -tests/vhost-user-test$(EXESUF): tests/vhost-user-test.o qemu-char.o qemu-timer.o $(qtest-obj-y) +tests/vhost-user-test$(EXESUF): tests/vhost-user-test.o qemu-char.o qemu-timer.o qemu-epoll.o $(qtest-obj-y) tests/qemu-iotests/socket_scm_helper$(EXESUF): tests/qemu-iotests/socket_scm_helper.o tests/test-qemu-opts$(EXESUF): tests/test-qemu-opts.o libqemuutil.a libqemustub.a
Signature · 1 line
1.9.3