main-loop: Use epoll on Linux
v3 cover main-loop: Use epoll on Linux
v3: Use separate structure for poll context instead of AioContext. (Stefan)
Fam Zheng (2):
main-loop: Introduce context parameter for qemu_poll_ns
main-loop: Use epoll on Linux
Makefile.objs | 1 + aio-posix.c | 2 +- include/block/aio.h | 2 + include/qemu/main-loop.h | 1 + include/qemu/timer.h | 17 ++++- main-loop.c | 7 ++- qemu-epoll.c | 161 +++++++++++++++++++++++++++++++++++++++++++++++ qemu-timer.c | 7 ++- tests/Makefile | 2 +- 9 files changed, 193 insertions(+), 7 deletions(-) create mode 100644 qemu-epoll.c
Signature · 1 line
1.9.3
PATCH v3 1/2 main-loop: Introduce context parameter for qemu_poll_ns
So that qemu_poll_ns can save state in the context in the future.
--- aio-posix.c | 2 +- include/block/aio.h | 2 ++ include/qemu/timer.h | 7 ++++++- main-loop.c | 7 +++++-- qemu-timer.c | 3 ++- 5 files changed, 16 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->poll_ctx, (GPollFD *)ctx->pollfds->data, ctx->pollfds->len, blocking ?aio_compute_timeout (ctx) :0 );
include/block/aio.h +2 -0
@@ -90,6 +90,8 @@ struct AioContext {/* TimerLists for calling timers - one per clock type */ QEMUTimerListGroup tlg; + +QEMUPollContext poll_ctx; };/* Used internally to synchronize aio_poll against qemu_bh_schedule. */
include/qemu/timer.h +6 -1
@@ -645,8 +645,12 @@ void timer_put(QEMUFile *f, QEMUTimer *ts);*/ int qemu_timeout_ns_to_ms (int64_t ns); +typedef struct { +}QEMUPollContext ; +/** * qemu_poll_ns: +* @ctx: Poll context we are using * @fds: Array of file descriptors * @nfds: number of file descriptors * @timeout: timeout in nanoseconds
@@ -656,7 +660,8 @@ 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 (QEMUPollContext *ctx,GPollFD *fds, +guint nfds,int64_t timeout);/** * qemu_soonest_timeout:
main-loop.c +5 -2
@@ -113,6 +113,7 @@ static int qemu_signal_init(void)#endif static AioContext *qemu_aio_context; +static QEMUPollContext qemu_poll_context;AioContext *qemu_get_aio_context (void ) {
@@ -234,7 +235,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_poll_context, (GPollFD *)gpollfds->data, + gpollfds->len, timeout);if (timeout) {qemu_mutex_lock_iothread ();
@@ -439,7 +441,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_poll_context, + poll_fds, n_poll_fds + w->num, poll_timeout_ns);qemu_mutex_lock_iothread ();if (g_poll_ret >0 ) {
qemu-timer.c +2 -1
@@ -307,7 +307,8 @@ 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 (QEMUPollContext *ctx,GPollFD *fds, +guint nfds,int64_t timeout) {#ifdef CONFIG_PPOLL if (timeout <0 ) {
Signature · 1 line
1.9.3
PATCH v3 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/qemu/main-loop.h | 1 + include/qemu/timer.h | 10 +++ qemu-epoll.c | 161 +++++++++++++++++++++++++++++++++++++++++++++++ qemu-timer.c | 4 +- tests/Makefile | 2 +- 6 files changed, 177 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/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 (QEMUPollContext *ctx,GPollFD *fds,guint nfds,int64_t timeout);#endif
include/qemu/timer.h +10 -0
@@ -646,6 +646,16 @@ void timer_put(QEMUFile *f, QEMUTimer *ts);int qemu_timeout_ns_to_ms (int64_t ns);typedef struct { +/* 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; }QEMUPollContext ;/**
qemu-epoll.c new file +161 -0
161 lines not on this page · raw message
qemu-timer.c +3 -1
@@ -310,7 +310,9 @@ int qemu_timeout_ns_to_ms(int64_t ns)int qemu_poll_ns (QEMUPollContext *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
@@ -348,7 +348,7 @@ tests/usb-hcd-ohci-test$(EXESUF): tests/usb-hcd-ohci-test.o $(libqos-usb-obj-y) tests/usb-hcd-uhci-test$(EXESUF): tests/usb-hcd-uhci-test.o $(libqos-usb-obj-y) tests/usb-hcd-ehci-test$(EXESUF): tests/usb-hcd-ehci-test.o $(libqos-usb-obj-y) tests/usb-hcd-xhci-test$(EXESUF): tests/usb-hcd-xhci-test.o $(libqos-usb-obj-y) -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
qemu-epoll.c line 75
Quoted 6 lines · Fam Zheng
qemu-epoll.c new file +5
-0+static int epoll_prepare (int epollfd, +GPollFD *fds,guint nfds, +GPollFD **g_poll_fds, +guint *g_poll_nfds, +int **g_poll_fd_idx)
Please pass the QEMUPollContext to epoll_prepare.
qemu-epoll.c line 119
Quoted 4 lines
qemu-epoll.c new file +4
-0+if (ctx->last_fds) { +close (ctx->epollfd); + } + ctx->epollfd =epoll_create (1 );
Please use epoll_create1 with the EPOLL_CLOEXEC flag.
qemu-epoll.c line 123
Quoted 4 lines
qemu-epoll.c new file +4
-0+if (ctx->epollfd <0 ) { +perror ("epoll_create" ); +abort (); + }
Interesting. Is it cheaper to do this than to compute a symmetric difference?
I am worried that the epoll_create fails with EMFILE, and that the destruction/recreation happens often with networking (which uses qemu_set_fd_handler2) if you use the right workload.
Paolo