iohandler: Convert to GSource and use epoll on Linux
cover iohandler: Convert to GSource and use epoll on Linux
ppoll is not scalable, it has a complexity of O(n). When we have many virtio queues, the main loop could be slowed down.
epoll, which is O(1), could solve this problem well.
In order to do this, we need to factor out an interface between main loop and iohandler. What we have now is not good, so patch 1 changed it to GSource, which is attached to the main context. The posix implementation, which is identical, attaches each iohandler fd to the iohandler GSrouce. They are automatically polled by g_poll, or extracted to ppoll.
Patch 2 adds another GSource which attaches a single epoll fd. The epoll fd set manages all the iohandler fds. Each time the epoll fd is poked by main loop polling, we call epoll_wait on it to dispatch the fds that are ready to read/write.
A pitfall is that certain type(s) of fds can't be added to a epoll set. One such case is the normal file or here document stdin fd. We have to special case it (where epoll_ctl returns -1 with errno equals to EPERM), and always notify these fds in the dispatch function.
To show the difference, let's compare the iodepth=1 read test on virtio-blk by toggling the virtio-serial module (which adds or removes tens of ioeventfds).
Before:
case rw bs bw iops latency
----------------------------------------------------------------------
vserial=on read 64k 1346 21548 45 vserial=off read 64k 1956 31305 30
After:
case rw bs bw iops latency
----------------------------------------------------------------------
vserial=on read 64k 1727 27647 34 vserial=off read 64k 1741 27868 34
The vserial=on case is better than before because we turned "ppoll with many fds" into ("ppoll with a few fds" + "epoll with a few fds").
The vserial=off case is worse than before because we turned "ppoll with a few fds" into ("ppoll with a few fds" + "epoll with a few fds").
To be optimal in both cases, we may dynamically enable/disable epoll depending on the active ioeventfds. But that's not done yet.
Fam
Fam Zheng (2):
iohandler: Convert I/O handler to GSource
iohandler: Add Linux implementation of iohandler GSource
Makefile.objs | 2 + include/qemu/iohandler.h | 65 +++++++++++++++ include/qemu/main-loop.h | 2 - iohandler-linux.c | 213 +++++++++++++++++++++++++++++++++++++++++++++++ iohandler-posix.c | 150 +++++++++++++++++++++++++++++++++ iohandler.c | 90 ++------------------ main-loop.c | 6 +- 7 files changed, 442 insertions(+), 86 deletions(-) create mode 100644 include/qemu/iohandler.h create mode 100644 iohandler-linux.c create mode 100644 iohandler-posix.c
Signature · 1 line
1.9.3
PATCH 1/2 iohandler: Convert I/O handler to GSource
Previously, I/O handler fd's are hooked into main loop by:
1) qemu_iohandler_fill to add the list of io handler fds to g_poll.
2) qemu_iohandler_poll to check the revent values and do the dispatch.
This patch moves all the fds into a GSource, which is attached to the main event loop. This way we don't have to rebuild the whole list of fds for every iteration, and there is a cleaner interface between us and main loop. Furthermore, it makes adding a Linux specific implementation (epoll) a lot easier.
--- Makefile.objs | 2 +- include/qemu/iohandler.h | 52 ++++++++++++++++ include/qemu/main-loop.h | 2 - iohandler-posix.c | 150 +++++++++++++++++++++++++++++++++++++++++++++++ iohandler.c | 90 +++------------------------- main-loop.c | 6 +- 6 files changed, 215 insertions(+), 87 deletions(-) create mode 100644 include/qemu/iohandler.h create mode 100644 iohandler-posix.c
Makefile.objs +1 -1
@@ -8,7 +8,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-y += main-loop.o iohandler.o qemu-timer.o iohandler-posix.o block-obj-$(CONFIG_POSIX) += aio-posix.o block-obj-$(CONFIG_WIN32) += aio-win32.o block-obj-y += block/
include/qemu/iohandler.h new file +52 -0
52 lines not on this page · raw message
include/qemu/main-loop.h +0 -2
@@ -302,8 +302,6 @@ void qemu_mutex_unlock_iothread(void);/* internal interfaces */ void qemu_fd_register (int fd); -void qemu_iohandler_fill (GArray *pollfds); -void qemu_iohandler_poll (GArray *pollfds,int rc);QEMUBH *qemu_bh_new (QEMUBHFunc *cb,void *opaque);void qemu_bh_schedule_idle (QEMUBH *bh);
iohandler-posix.c new file +150 -0
150 lines not on this page · raw message
iohandler.c +8 -82
133 lines not on this page · raw message
main-loop.c +4 -2
@@ -28,6 +28,7 @@#include "sysemu/qtest.h" #include "slirp/libslirp.h" #include "qemu/main-loop.h" +#include "qemu/iohandler.h" #include "block/aio.h" #ifndef _WIN32
@@ -148,6 +149,9 @@ int qemu_init_main_loop(Error **errp) src =aio_get_g_source (qemu_aio_context);g_source_attach (src,NULL );g_source_unref (src); + src =qemu_iohandler_get_source (); +g_source_attach (src,NULL ); +g_source_unref (src);return 0 ; }
@@ -474,7 +478,6 @@ int main_loop_wait(int nonblocking)#ifdef CONFIG_SLIRP slirp_pollfds_fill (gpollfds, &timeout);#endif -qemu_iohandler_fill (gpollfds);if (timeout ==UINT32_MAX ) { timeout_ns =-1 ;
@@ -487,7 +490,6 @@ int main_loop_wait(int nonblocking) &main_loop_tlg)); ret =os_host_main_loop_wait (timeout_ns); -qemu_iohandler_poll (gpollfds, ret);#ifdef CONFIG_SLIRP slirp_pollfds_poll (gpollfds, (ret <0 ));#endif
Signature · 1 line
1.9.3
PATCH 2/2 iohandler: Add Linux implementation of iohandler GSource
In reply to Fam ZhengThe Linux-specific syscall epoll(7) has a constant complexity, whereas ppoll/g_poll is linear complexity, depending on the number of fds.
The event loop is more efficient with epoll, because we only need to poll on few fds now.
Sometimes EPOLL_CTL_ADD returns -1 with errno = EPERM, when the target file descriptor doesn't support epoll and they are always ready for read/write. We mark such fds and always dispatch.
--- Makefile.objs | 4 +- include/qemu/iohandler.h | 13 +++ iohandler-linux.c | 213 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 229 insertions(+), 1 deletion(-) create mode 100644 iohandler-linux.c
Makefile.objs +3 -1
@@ -8,7 +8,9 @@ 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 iohandler-posix.o +block-obj-y += main-loop.o iohandler.o qemu-timer.o +block-obj-$(call lnot,$(CONFIG_LINUX)) += iohandler-posix.o +block-obj-$(CONFIG_LINUX) += iohandler-linux.o block-obj-$(CONFIG_POSIX) += aio-posix.o block-obj-$(CONFIG_WIN32) += aio-win32.o block-obj-y += block/
include/qemu/iohandler.h +13 -0
@@ -27,7 +27,11 @@ #ifndef QEMU_IOHANDLER_H #define QEMU_IOHANDLER_H +#include "config-host.h" #include "qemu/main-loop.h" +#ifdef CONFIG_LINUX +#include <sys/epoll.h> +#endif typedef struct IOHandlerRecord { IOCanReadHandler *fd_read_poll;
@@ -39,11 +43,20 @@ typedef struct IOHandlerRecord {bool deleted;GPollFD gpfd;bool attached; +#ifdef CONFIG_LINUX +struct epoll_event epoll_event; +bool fallback; +#endif + } IOHandlerRecord;typedef struct {GSource source; +#ifdef CONFIG_LINUX +GPollFD epollfd; +#endif + QLIST_HEAD(, IOHandlerRecord) io_handlers; }IOHandlerSource ;
iohandler-linux.c new file +213 -0
213 lines not on this page · raw message
Signature · 1 line
1.9.3
Re: [Qemu-devel] [PATCH 2/2] iohandler: Add Linux implementation of iohandler GSource
Il 25/09/2014 19:21, Fam Zheng ha scritto:
iohandler-linux.c line 153
Quoted 2 lines
iohandler-linux.c new file +2
-0+while (true) { + r =epoll_pwait (s->epollfd.fd, events,MAX_EVENTS ,0 , &origmask);
You can save a syscall by doing this just once. You would just get a readable epoll file descriptor on the next main loop iteration. Also, origmask is an input parameter, so you need to use epoll_wait.
Also, as an extra optimization perhaps you can make a second list with iohandlers that were modified or have a read_poll handler, and only call iohandler_get_events on that one.
Perhaps this together lowers the cost of epoll enough to more easily reach the break-even point.
Though I wonder if it would be acceptable to make ioeventfd off by default for virtio-serial...
Thanks,
Paolo
Quoted 14 lines
iohandler-linux.c new file +14
-0+if (r <0 ) { +break ; + }else if (r ==0 ) { +break ; + }else { +for (i =0 ; i < r; i++) { +iohandler_dispatch_event (s, &events[i]); + } +if (r <MAX_EVENTS ) { +break ; + } + } + } +
Re: [Qemu-devel] [PATCH 2/2] iohandler: Add Linux implementation of iohandler GSource
Quoted 6 lines · Paolo Bonzini
Il 25/09/2014 19:21, Fam Zheng ha scritto:
iohandler-linux.c line 153
Quoted 2 lines
iohandler-linux.c new file +2
-0+while (true) { + r =epoll_pwait (s->epollfd.fd, events,MAX_EVENTS ,0 , &origmask);You can save a syscall by doing this just once. You would just get a readable epoll file descriptor on the next main loop iteration.
There is only one call here if r < MAX_EVENTS, which is the normal case.
Quoted 2 lines
Also,
origmask is an input parameter, so you need to useepoll_wait.
Yeah, I missed the point completely. :p
Quoted 4 lines
Also, as an extra optimization perhaps you can make a second list with iohandlers that were modified or have a
read_pollhandler, and only calliohandler_get_eventson that one.
Sounds good, but I need to benchmark it to tell :)
There isn't a lot of computation if there is no read_poll.
Quoted 3 lines
Perhaps this together lowers the cost of epoll enough to more easily reach the break-even point.
Dynamic swithing between ppoll and epoll shoudn't be hard to implement, but I'm not sure how to write the condition. Anyway, do you think it is a good idea?
Fam
Quoted 7 lines
Though I wonder if it would be acceptable to make ioeventfd off by default for virtio-serial...
Thanks,
Paolo
Re: [Qemu-devel] [PATCH 2/2] iohandler: Add Linux implementation of iohandler GSource
Il 26/09/2014 03:23, Fam Zheng ha scritto:
Quoted 6 lines
Quoted 3 lines
Also, as an extra optimization perhaps you can make a second list with iohandlers that were modified or have a
read_pollhandler, and only calliohandler_get_eventson that one.Sounds good, but I need to benchmark it to tell :)
Quoted 7 lines
There isn't a lot of computation if there is no
read_poll.Quoted 2 lines
Perhaps this together lowers the cost of epoll enough to more easily reach the break-even point.
Dynamic swithing between ppoll and epoll shoudn't be hard to implement, but I'm not sure how to write the condition.
Perhaps just a count of the number of polled file descriptors.
Quoted 1 line
Anyway, do you think it is a good idea?
It depends. I think first of all we need to profile it for low file descriptor counts, and see if obvious sources of overhead stand out either in QEMU or in the kernel...
There's time anyway, since we also need a Win32 implementation.
Paolo