aio: Support epoll by introducing qemu_poll abstraction
v3 cover aio: Support epoll by introducing qemu_poll abstraction
v3: Rebase to master for 2.4.
Although epoll_pwait1 syscall is still being worked on [1], the QEMU part (if any) will base on this, so let's merge it first.
That part is not included in this version because I'm still evaluating by comparing epoll_pwait1 with epoll+timerfd as with current master they seem to be really close.
[1]: http://www.spinics.net/lists/linux-api/msg08216.html
v2: Emulate nanoseconds precison of timeout with ppoll and timerfd.
Their performance is on par with each other, but both much better than qemu.git:
syscall high # of fd low # of fd
-------------------------------------------------
qemu.git(ppoll) 44 96
ppoll+epoll 85 101
timerfd+epoll 87 109
(In high # of fd case, 3 activated but idle virtio-console devices are attached, which will add us hundereds of fds to poll)
v1 cover letter
---------------
ppoll(2) doesn't scale as well as epoll: The elapsed time of the syscall is linear to the number of fd's we poll, which hurts performance a bit when the number of devices are many, or when a virtio device registers many virtqueues (virtio-serial, for instance).
This series introduces qemu_poll, which is implemented with g_poll and epoll, decided at configure time with CONFIG_EPOLL.
Fam
Fam Zheng (7):
poll: Introduce QEMU Poll API
posix-aio: Use QEMU poll interface
poll: Add epoll implementation for qemu_poll
main-loop: Replace qemu_poll_ns with qemu_poll
tests: Add test case for qemu_poll
poll-glib: Support ppoll
poll-linux: Add timerfd support
Makefile.objs | 2 + aio-posix.c | 52 ++++---- async.c | 5 +- include/block/aio.h | 7 +- include/qemu/poll.h | 40 ++++++ include/qemu/timer.h | 13 -- include/qemu/typedefs.h | 4 +- main-loop.c | 35 +++++- poll-glib.c | 153 +++++++++++++++++++++++ poll-linux.c | 314 ++++++++++++++++++++++++++++++++++++++++++++++++ qemu-timer.c | 28 ----- tests/Makefile | 2 + tests/test-poll.c | 272 +++++++++++++++++++++++++++++++++++++++++ 13 files changed, 845 insertions(+), 82 deletions(-) create mode 100644 include/qemu/poll.h create mode 100644 poll-glib.c create mode 100644 poll-linux.c create mode 100644 tests/test-poll.c
Signature · 1 line
1.9.3
PATCH v3 1/7 poll: Introduce QEMU Poll API
This is abstract of underlying poll implementation. A glib implementation is included.
--- Makefile.objs | 2 +- include/qemu/poll.h | 40 +++++++++++++++ include/qemu/typedefs.h | 4 +- poll-glib.c | 130 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 174 insertions(+), 2 deletions(-) create mode 100644 include/qemu/poll.h create mode 100644 poll-glib.c
Makefile.objs +1 -1
@@ -9,7 +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_POSIX) += aio-posix.o +block-obj-$(CONFIG_POSIX) += aio-posix.o poll-glib.o block-obj-$(CONFIG_WIN32) += aio-win32.o block-obj-y += block/ block-obj-y += qemu-io-cmds.o
include/qemu/poll.h new file +40 -0
@@ -0,0 +1,40 @@ +#ifndef QEMU_POLL_H +#define QEMU_POLL_H + +#include "qemu/typedefs.h" +#include "qemu-common.h" + +struct QEMUPollEvent { +int fd; +int events; +int revents; +void *opaque; +}; + +QEMUPoll *qemu_poll_new (void ); +void qemu_poll_free (QEMUPoll *qpoll); +int qemu_poll (QEMUPoll *qpoll,int64_t timeout_ns); + +/* Add an fd to poll. Return -EEXIST if fd already registered. */ +int qemu_poll_add (QEMUPoll *qpoll,int fd,int gio_events,void *opaque); + +/* Delete a previously added fd. Return -ENOENT if fd not registered. */ +int qemu_poll_del (QEMUPoll *qpoll,int fd); + +/** +* A shortcut to: +* 1) remove all the existing fds; +* 2) add all in @fds, while setting each fd's opaque pointer to the pollfd +* struct itself. +*/ +int qemu_poll_set_fds (QEMUPoll *qpoll,GPollFD *fds,int nfds); + +/** +* Query the events from last qemu_poll. ONLY revent and opaque are valid in +* @events after return. +*/ +int qemu_poll_get_events (QEMUPoll *qpoll, +QEMUPollEvent *events, +int max_events); + +#endif
include/qemu/typedefs.h +3 -1
@@ -64,10 +64,12 @@ typedef struct QEMUBH QEMUBH;typedef struct QemuConsole QemuConsole ;typedef struct QEMUFile QEMUFile ;typedef struct QEMUMachine QEMUMachine ; +typedef struct QEMUPoll QEMUPoll ; +typedef struct QEMUPollEvent QEMUPollEvent ;typedef struct QEMUSGList QEMUSGList ;typedef struct QEMUSizedBuffer QEMUSizedBuffer ; -typedef struct QEMUTimerListGroup QEMUTimerListGroup ;typedef struct QEMUTimer QEMUTimer ; +typedef struct QEMUTimerListGroup QEMUTimerListGroup ;typedef struct Range Range ;typedef struct SerialState SerialState ;typedef struct SHPCDevice SHPCDevice ;
poll-glib.c new file +130 -0
130 lines not on this page · raw message
Signature · 1 line
1.9.3
PATCH v3 2/7 posix-aio: Use QEMU poll interface
In reply to Fam ZhengThe AIO handler list is only modified by aio_set_fd_handler, so we can easily add del poll fd there. Initialize a QEMUPoll and keep track of all the fds, so we don't need to rebuild a GPollFD array for g_poll in aio_poll.
--- aio-posix.c | 52 +++++++++++++++++++++------------------------------- async.c | 5 +++-- include/block/aio.h | 7 +++++-- 3 files changed, 29 insertions(+), 35 deletions(-)
aio-posix.c +21 -31
@@ -17,6 +17,7 @@#include "block/block.h" #include "qemu/queue.h" #include "qemu/sockets.h" +#include "qemu/poll.h" struct AioHandler {
@@ -55,6 +56,7 @@ void aio_set_fd_handler(AioContext *ctx,/* Are we deleting the fd handler? */ if (!io_read && !io_write) {if (node) { +qemu_poll_del (ctx->qpoll, fd);g_source_remove_poll (&ctx->source, &node->pfd);/* If the lock is held, just mark the node as deleted */
@@ -71,13 +73,15 @@ void aio_set_fd_handler(AioContext *ctx, } } }else { -if (node ==NULL ) { +if (node) { +/* Remove the old */ +qemu_poll_del (ctx->qpoll, fd); +g_source_remove_poll (&ctx->source, &node->pfd); + }else {/* Alloc and insert if it's not already there */ node =g_new0 (AioHandler,1 ); node->pfd.fd = fd;QLIST_INSERT_HEAD (&ctx->aio_handlers, node, node); - -g_source_add_poll (&ctx->source, &node->pfd); }/* Update handler with latest information */ node->io_read = io_read;
@@ -87,6 +91,8 @@ void aio_set_fd_handler(AioContext *ctx, node->pfd.events = (io_read ?G_IO_IN |G_IO_HUP |G_IO_ERR :0 ); node->pfd.events |= (io_write ?G_IO_OUT |G_IO_ERR :0 ); +qemu_poll_add (ctx->qpoll, fd, node->pfd.events, node); +g_source_add_poll (&ctx->source, &node->pfd); }aio_notify (ctx);
@@ -191,6 +197,7 @@ bool aio_poll(AioContext *ctx, bool blocking)AioHandler *node;bool was_dispatching;int ret; +int i;bool progress; was_dispatching = ctx->dispatching;
@@ -208,38 +215,21 @@ bool aio_poll(AioContext *ctx, bool blocking)*/ aio_set_dispatching (ctx, !blocking); - ctx->walking_handlers++; - -g_array_set_size (ctx->pollfds,0 ); - -/* fill pollfds */ - QLIST_FOREACH(node, &ctx->aio_handlers, node) { - node->pollfds_idx =-1 ; -if (!node->deleted && node->pfd.events) { -GPollFD pfd = { - .fd = node->pfd.fd, - .events = node->pfd.events, - }; - node->pollfds_idx = ctx->pollfds->len; -g_array_append_val (ctx->pollfds, pfd); - } - } - - ctx->walking_handlers--; -/* wait until next event */ - ret =qemu_poll_ns ((GPollFD *)ctx->pollfds->data, - ctx->pollfds->len, - blocking ?aio_compute_timeout (ctx) :0 ); + ret =qemu_poll (ctx->qpoll, blocking ?aio_compute_timeout (ctx) :0 );/* if we have any readable fds, dispatch event */ if (ret >0 ) { - QLIST_FOREACH(node, &ctx->aio_handlers, node) { -if (node->pollfds_idx !=-1 ) { -GPollFD *pfd = &g_array_index (ctx->pollfds, GPollFD, - node->pollfds_idx); - node->pfd.revents = pfd->revents; - } +int r; +g_array_set_size (ctx->events, ret); + r =qemu_poll_get_events (ctx->qpoll, + (QEMUPollEvent *)ctx->events->data, + ret); +assert (r == ret); +for (i =0 ; i < r; i++) { +QEMUPollEvent *e = &g_array_index (ctx->events, QEMUPollEvent, i); + node = e->opaque; + node->pfd.revents = e->revents; } }
async.c +3 -2
@@ -27,6 +27,7 @@#include "block/thread-pool.h" #include "qemu/main-loop.h" #include "qemu/atomic.h" +#include "qemu/poll.h" /***********************************************************/ /* bottom halves (can be seen as timers which expire ASAP) */
@@ -230,7 +231,6 @@ aio_ctx_finalize(GSource *source)event_notifier_cleanup (&ctx->notifier);rfifolock_destroy (&ctx->lock);qemu_mutex_destroy (&ctx->bh_lock); -g_array_free (ctx->pollfds, TRUE);timerlistgroup_deinit (&ctx->tlg); }
@@ -299,10 +299,11 @@ AioContext *aio_context_new(Error **errp)return NULL ; }g_source_set_can_recurse (&ctx->source, true); + ctx->qpoll =qemu_poll_new (); + ctx->events =g_array_new (FALSE, FALSE,sizeof (QEMUPollEvent));aio_set_event_notifier (ctx, &ctx->notifier, (EventNotifierHandler *) event_notifier_test_and_clear); - ctx->pollfds =g_array_new (FALSE, FALSE,sizeof (GPollFD)); ctx->thread_pool =NULL ;qemu_mutex_init (&ctx->bh_lock);rfifolock_init (&ctx->lock, aio_rfifolock_cb, ctx);
include/block/aio.h +5 -2
@@ -82,8 +82,11 @@ struct AioContext {/* Used for aio_notify. */ EventNotifier notifier; -/* GPollFDs for aio_poll() */ -GArray *pollfds; +/* qemu_poll context */ +QEMUPoll *qpoll; + +/* QEMUPollEvents for qemu_poll_get_events() */ +GArray *events;/* Thread pool for performing work and receiving completion callbacks */ struct ThreadPool *thread_pool;
Signature · 1 line
1.9.3
PATCH v3 3/7 poll: Add epoll implementation for qemu_poll
In reply to Fam ZhengThis implements qemu_poll with ppoll + epoll. The only complex part is qemu_poll_set_fds, which will sync up epollfd with epoll_ctl by computing the symmetric difference of previous and new fds.
The ppoll is used to retain ns precision of timeout.
--- Makefile.objs | 4 +- poll-linux.c | 232 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 235 insertions(+), 1 deletion(-) create mode 100644 poll-linux.c
Makefile.objs +3 -1
@@ -9,7 +9,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 -block-obj-$(CONFIG_POSIX) += aio-posix.o poll-glib.o +block-obj-$(CONFIG_POSIX) += aio-posix.o +block-obj-$(CONFIG_EPOLL) += poll-linux.o +block-obj-$(if $(CONFIG_EPOLL),n,$(CONFIG_POSIX)) += poll-glib.o block-obj-$(CONFIG_WIN32) += aio-win32.o block-obj-y += block/ block-obj-y += qemu-io-cmds.o
poll-linux.c new file +232 -0
232 lines not on this page · raw message
Signature · 1 line
1.9.3
PATCH v3 4/7 main-loop: Replace qemu_poll_ns with qemu_poll
In reply to Fam Zhengqemu_poll_set_fds + qemu_poll does the same, and when epoll is available, it is faster.
--- include/qemu/timer.h | 13 ------------- main-loop.c | 35 ++++++++++++++++++++++++++++++----- qemu-timer.c | 28 ---------------------------- 3 files changed, 30 insertions(+), 46 deletions(-)
include/qemu/timer.h +0 -13
@@ -738,19 +738,6 @@ void timer_put(QEMUFile *f, QEMUTimer *ts);int qemu_timeout_ns_to_ms (int64_t ns);/** -* qemu_poll_ns: -* @fds: Array of file descriptors -* @nfds: number of file descriptors -* @timeout: timeout in nanoseconds -* -* Perform a poll like g_poll but with a timeout in nanoseconds. -* See g_poll documentation for further details. -* -* Returns: number of fds ready -*/ -int qemu_poll_ns (GPollFD *fds,guint nfds,int64_t timeout); - -/** * qemu_soonest_timeout: * @timeout1: first timeout in nanoseconds (or -1 for infinite) * @timeout2: second timeout in nanoseconds (or -1 for infinite)
main-loop.c +30 -5
@@ -29,6 +29,7 @@#include "slirp/libslirp.h" #include "qemu/main-loop.h" #include "block/aio.h" +#include "qemu/poll.h" #ifndef _WIN32
@@ -130,6 +131,7 @@ void qemu_notify_event(void) }static GArray *gpollfds; +static QEMUPoll *qpoll;int qemu_init_main_loop (Error **errp) {
@@ -150,6 +152,7 @@ int qemu_init_main_loop(Error **errp)return -EMFILE ; } gpollfds =g_array_new (FALSE, FALSE,sizeof (GPollFD)); + qpoll =qemu_poll_new (); src =aio_get_g_source (qemu_aio_context);g_source_attach (src,NULL );g_source_unref (src);
@@ -207,6 +210,8 @@ static int os_host_main_loop_wait(int64_t timeout) {int ret;static int spin_counter; +QEMUPollEvent events[1024 ]; +int r, i;glib_pollfds_fill (&timeout);
@@ -236,7 +241,17 @@ static int os_host_main_loop_wait(int64_t timeout) spin_counter++; } - ret =qemu_poll_ns ((GPollFD *)gpollfds->data, gpollfds->len, timeout); +qemu_poll_set_fds (qpoll, (GPollFD *)gpollfds->data, gpollfds->len); + ret =qemu_poll (qpoll, timeout); +if (ret >0 ) { + ret =MIN (ret,sizeof (events) /sizeof (QEMUPollEvent)); + r =qemu_poll_get_events (qpoll, events, ret); +for (i =0 ; i < r; i++) { +GPollFD *fd = events[i].opaque; + fd->revents = events[i].revents; + } + ret = r; + }if (timeout) {qemu_mutex_lock_iothread ();
@@ -389,9 +404,11 @@ static int os_host_main_loop_wait(int64_t timeout) {GMainContext *context =g_main_context_default ();GPollFD poll_fds[1024 *2 ];/* this is probably overkill */ +QEMUPollEvent poll_events[1024 *2 ];int select_ret =0 ; -int g_poll_ret, ret, i, n_poll_fds; +int nevents, ret, i, n_poll_fds;PollingEntry *pe; +QEMUPollEvent *events;WaitObjects *w = &wait_objects;gint poll_timeout;int64_t poll_timeout_ns;
@@ -441,10 +458,18 @@ 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); + +qemu_poll_set_fds (qpoll, (GPollFD *)poll_fds, n_poll_fds + w->num) + nevents = qemu_poll(qpoll, poll_timeout_ns);qemu_mutex_lock_iothread (); -if (g_poll_ret >0 ) { +if (nevents >0 ) { + r =qemu_poll_get_events (qpoll, poll_events, nevents); +for (i =0 ; i < r; i++) { +GPollFD *fd = poll_events[i].opaque; + fd->revents = poll_events[i].revents; + } +for (i =0 ; i < w->num; i++) { w->revents[i] = poll_fds[n_poll_fds + i].revents; }
@@ -459,7 +484,7 @@ static int os_host_main_loop_wait(int64_t timeout) g_main_context_dispatch(context); } -return select_ret || g_poll_ret; +return select_ret || nevents; }#endif
qemu-timer.c +0 -28
@@ -299,34 +299,6 @@ int qemu_timeout_ns_to_ms(int64_t ns)return (int ) ms; } - -/* 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) -{ -#ifdef CONFIG_PPOLL -if (timeout <0 ) { -return ppoll ((struct pollfd *)fds, nfds,NULL ,NULL ); - }else { -struct timespec ts; -int64_t tvsec = timeout /1000000000LL ; -/* Avoid possibly overflowing and specifying a negative number of -* seconds, which would turn a very long timeout into a busy-wait. -*/ -if (tvsec > (int64_t )INT32_MAX ) { - tvsec =INT32_MAX ; - } - ts.tv_sec = tvsec; - ts.tv_nsec = timeout %1000000000LL ; -return ppoll ((struct pollfd *)fds, nfds, &ts,NULL ); - } -#else -return g_poll (fds, nfds,qemu_timeout_ns_to_ms (timeout)); -#endif -} - -void timer_init_tl (QEMUTimer *ts,QEMUTimerList *timer_list,int scale,QEMUTimerCB *cb,void *opaque)
Signature · 1 line
1.9.3
PATCH v3 5/7 tests: Add test case for qemu_poll
In reply to Fam Zheng--- tests/Makefile | 2 + tests/test-poll.c | 272 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 274 insertions(+) create mode 100644 tests/test-poll.c
tests/Makefile +2 -0
@@ -41,6 +41,7 @@ check-unit-$(CONFIG_POSIX) += tests/test-rfifolock$(EXESUF) check-unit-y += tests/test-throttle$(EXESUF) gcov-files-test-aio-$(CONFIG_WIN32) = aio-win32.c gcov-files-test-aio-$(CONFIG_POSIX) = aio-posix.c +check-unit-y += tests/test-poll$(EXESUF) check-unit-y += tests/test-thread-pool$(EXESUF) gcov-files-test-thread-pool-y = thread-pool.c gcov-files-test-hbitmap-y = util/hbitmap.c
@@ -251,6 +252,7 @@ tests/check-qjson$(EXESUF): tests/check-qjson.o libqemuutil.a libqemustub.a tests/check-qom-interface$(EXESUF): tests/check-qom-interface.o $(qom-core-obj) libqemuutil.a libqemustub.a tests/test-coroutine$(EXESUF): tests/test-coroutine.o $(block-obj-y) libqemuutil.a libqemustub.a tests/test-aio$(EXESUF): tests/test-aio.o $(block-obj-y) libqemuutil.a libqemustub.a +tests/test-poll$(EXESUF): tests/test-poll.o $(block-obj-y) libqemuutil.a libqemustub.a tests/test-rfifolock$(EXESUF): tests/test-rfifolock.o libqemuutil.a libqemustub.a tests/test-throttle$(EXESUF): tests/test-throttle.o $(block-obj-y) libqemuutil.a libqemustub.a tests/test-thread-pool$(EXESUF): tests/test-thread-pool.o $(block-obj-y) libqemuutil.a libqemustub.a
tests/test-poll.c new file +272 -0
272 lines not on this page · raw message
Signature · 1 line
1.9.3
PATCH v3 6/7 poll-glib: Support ppoll
In reply to Fam Zheng--- poll-glib.c | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-)
poll-glib.c +26 -3
@@ -14,6 +14,9 @@#include "qemu-common.h" #include "qemu/timer.h" #include "qemu/poll.h" +#if CONFIG_PPOLL +#include <poll.h> +#endif struct QEMUPoll {/* Array of GPollFD for g_poll() */
@@ -42,13 +45,33 @@ void qemu_poll_free(QEMUPoll *qpoll)int qemu_poll (QEMUPoll *qpoll,int64_t timeout_ns) {int i; +void *fds = qpoll->gpollfds->data; +int nfds = qpoll->gpollfds->len; +for (i =0 ; i < qpoll->gpollfds->len; i++) {GPollFD *p = &g_array_index (qpoll->gpollfds, GPollFD, i); p->revents =0 ; } -return g_poll ((GPollFD *)qpoll->gpollfds->data, - qpoll->gpollfds->len, -qemu_timeout_ns_to_ms (timeout_ns)); +#if CONFIG_PPOLL +if (timeout_ns <0 ) { +return ppoll (fds, nfds,NULL ,NULL ); + }else { +struct timespec ts; +int64_t tvsec = timeout_ns /1000000000LL ; +/* Avoid possibly overflowing and specifying a negative number of +* seconds, which would turn a very long timeout into a busy-wait. +*/ +if (tvsec > (int64_t )INT32_MAX ) { + tvsec =INT32_MAX ; + } + ts.tv_sec = tvsec; + ts.tv_nsec = timeout_ns %1000000000LL ; +return ppoll (fds, nfds, &ts,NULL ); + } + +#else +return g_poll (data, nfds,qemu_timeout_ns_to_ms (timeout_ns)); +#endif }/* Add an fd to poll. Return -EEXIST if fd already registered. */
Signature · 1 line
1.9.3
PATCH v3 7/7 poll-linux: Add timerfd support
In reply to Fam ZhengIn qemu_poll_timerfd, we arm the timerfd with timeout_ns. The timerfd is also watched by epollfd, so that when there is no other events, epoll_wait will still return on time, even though we pass -1 (wait infinitely).
--- poll-linux.c | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 83 insertions(+), 1 deletion(-)
poll-linux.c +83 -1
@@ -12,20 +12,52 @@*/ #include <sys/epoll.h> + +#define USE_TIMERFD CONFIG_TIMERFD + +#ifdef USE_TIMERFD +#include <sys/timerfd.h> +#endif +#include <glib.h> #include <poll.h> #include "qemu-common.h" #include "qemu/timer.h" #include "qemu/poll.h" +struct QEMUPoll {int epollfd; +#if USE_TIMERFD +int timerfd; +#endif struct epoll_event *events;int max_events;int nevents;GHashTable *fds; }; +static void qemu_poll_init_timerfd (QEMUPoll *qpoll) +{ +#if USE_TIMERFD +int r; +struct epoll_event ev; + qpoll->timerfd =timerfd_create (CLOCK_MONOTONIC , +TFD_NONBLOCK |TFD_CLOEXEC ); +if (qpoll->timerfd <0 ) { +perror ("timerfd_create" ); +abort (); + } + ev.events =EPOLLIN |EPOLLOUT |EPOLLERR |EPOLLHUP ; + ev.data.fd = qpoll->timerfd; + r =epoll_ctl (qpoll->epollfd,EPOLL_CTL_ADD , qpoll->timerfd, &ev); +if (r) { +perror ("epoll_ctl add timerfd" ); +abort (); + } +#endif +} +QEMUPoll *qemu_poll_new (void ) {int epollfd;
@@ -40,6 +72,7 @@ QEMUPoll *qemu_poll_new(void) qpoll->events = g_new(struct epoll_event, 1); qpoll->fds =g_hash_table_new_full (g_int_hash, g_int_equal,NULL , g_free); +qemu_poll_init_timerfd (qpoll);return qpoll; }
@@ -48,10 +81,13 @@ void qemu_poll_free(QEMUPoll *qpoll)g_free (qpoll->events);g_hash_table_destroy (qpoll->fds);close (qpoll->epollfd); +#if USE_TIMERFD +close (qpoll->timerfd); +#endif g_free (qpoll); } -int qemu_poll (QEMUPoll *qpoll,int64_t timeout_ns) +static int qemu_poll_ppoll (QEMUPoll *qpoll,int64_t timeout_ns) {int r;struct pollfd fd = {
@@ -78,6 +114,47 @@ int qemu_poll(QEMUPoll *qpoll, int64_t timeout_ns)return r; } +#if USE_TIMERFD +static int qemu_poll_timerfd (QEMUPoll *qpoll,int64_t timeout_ns) +{ +int r; +struct itimerspec its = { {0 } }; + +if (timeout_ns >0 ) { + its.it_value.tv_sec = timeout_ns /1000000000LL ; + its.it_value.tv_nsec = timeout_ns %1000000000LL ; + } + + r =timerfd_settime (qpoll->timerfd,0 , &its,NULL ); +if (r) { +struct pollfd fd = { + .fd = qpoll->epollfd, + .events =POLLIN |POLLOUT |POLLERR |POLLHUP , + }; +perror ("timerfd_settime" ); +abort (); + r =ppoll (&fd,1 , &its.it_value,NULL ); + } +if (r <0 ) { +return r; + } + r =epoll_wait (qpoll->epollfd, + qpoll->events, + qpoll->max_events, + timeout_ns >0 ?-1 : timeout_ns); + qpoll->nevents = r; +return r; +} +#endif + +int qemu_poll (QEMUPoll *qpoll,int64_t timeout_ns) +{ +#if USE_TIMERFD +return qemu_poll_timerfd (qpoll, timeout_ns); +#endif +return qemu_poll_ppoll (qpoll, timeout_ns); +} +static inline uint32_t epoll_event_from_gio_events (int gio_events) {
@@ -220,6 +297,11 @@ int qemu_poll_get_events(QEMUPoll *qpoll,for (i =0 ; i <MIN (qpoll->nevents, max_events); i++) { ev = &qpoll->events[i]; fd = ev->data.fd; +#if USE_TIMERFD +if (fd == qpoll->timerfd) { +continue ; + } +#endif p =g_hash_table_lookup (qpoll->fds, &fd);assert (p);
Signature · 1 line
1.9.3
Re: [Qemu-devel] [PATCH v3 7/7] poll-linux: Add timerfd support
poll-linux.c int qemu_poll(QEMUPoll *qpoll, int64_t timeout_ns) · line 128
Quoted 11 lines · Fam Zheng
poll-linux.c +11
-0+static int qemu_poll_timerfd (QEMUPoll *qpoll,int64_t timeout_ns) +{ +int r; +struct itimerspec its = { {0 } }; + +if (timeout_ns >0 ) { + its.it_value.tv_sec = timeout_ns /1000000000LL ; + its.it_value.tv_nsec = timeout_ns %1000000000LL ; + } + + r =timerfd_settime (qpoll->timerfd,0 , &its,NULL );
Can't hurt to put a comment here:
/* The timer must be set even when there is no timeout so the readable
* timerfd is cleared (we never call read(2) on it).
*/
poll-linux.c int qemu_poll(QEMUPoll *qpoll, int64_t timeout_ns) · line 136
Quoted 8 lines
poll-linux.c +8
-0+if (r) { +struct pollfd fd = { + .fd = qpoll->epollfd, + .events =POLLIN |POLLOUT |POLLERR |POLLHUP , + }; +perror ("timerfd_settime" ); +abort (); + r =ppoll (&fd,1 , &its.it_value,NULL );
Please remove the dead code.
In fact, there is no reasonable error for timerfd_settime(). It should never fail, I'd be happy with just assert(r == 0).
Quoted 14 lines · Fam Zheng
v3: Rebase to master for 2.4.
Althoughepoll_pwait1syscall is still being worked on [1], the QEMU part (if any) will base on this, so let's merge it first.That part is not included in this version because I'm still evaluating by comparing
epoll_pwait1with epoll+timerfd as with current master they seem to be really close.[1]: http://www.spinics.net/lists/linux-api/msg08216.html
v2: Emulate nanoseconds precison of timeout with ppoll and timerfd.
Their performance is on par with each other, but both much better than qemu.git:
Quoted 8 lines
syscall high # of fd low # of fd
-------------------------------------------------
qemu.git(ppoll)44 96
ppoll+epoll 85 101
timerfd+epoll 87 109(In high # of fd case, 3 activated but idle virtio-console devices are attached, which will add us hundereds of fds to poll)
Have you rerun benchmarks with this patch series?
I wonder how the ppoll-only performance changes. It seems like there are now additional copies of <fd, events, revents> information and corresponding malloc/realloc/frees.
Stefan
Quoted 21 lines · Stefan Hajnoczi
Quoted 20 lines · Fam Zheng
v3: Rebase to master for 2.4.
Althoughepoll_pwait1syscall is still being worked on [1], the QEMU part (if any) will base on this, so let's merge it first.That part is not included in this version because I'm still evaluating by comparing
epoll_pwait1with epoll+timerfd as with current master they seem to be really close.[1]: http://www.spinics.net/lists/linux-api/msg08216.html
v2: Emulate nanoseconds precison of timeout with ppoll and timerfd.
Their performance is on par with each other, but both much better than qemu.git:syscall high # of fd low # of fd
-------------------------------------------------
qemu.git(ppoll)44 96
ppoll+epoll 85 101
timerfd+epoll 87 109
Quoted 4 lines
Quoted 2 lines
(In high # of fd case, 3 activated but idle virtio-console devices are attached, which will add us hundereds of fds to poll)
Have you rerun benchmarks with this patch series?
Yes, here:
syscall high # of fd low # of fd
(Unit MB/s) (Unit MB/s)
-------------------------------------------------
qemu.git(ppoll) 24 73
ppoll+epoll 49 77
timerfd+epoll 49 82
Quoted 6 lines
I wonder how the ppoll-only performance changes. It seems like there are now additional copies of <fd, events, revents> information and corresponding malloc/realloc/frees.
Stefan
Quoted 27 lines · Fam Zheng
Quoted 21 lines · Stefan Hajnoczi
Quoted 20 lines · Fam Zheng
v3: Rebase to master for 2.4.
Althoughepoll_pwait1syscall is still being worked on [1], the QEMU part (if any) will base on this, so let's merge it first.That part is not included in this version because I'm still evaluating by comparing
epoll_pwait1with epoll+timerfd as with current master they seem to be really close.[1]: http://www.spinics.net/lists/linux-api/msg08216.html
v2: Emulate nanoseconds precison of timeout with ppoll and timerfd.
Their performance is on par with each other, but both much better than qemu.git:syscall high # of fd low # of fd
-------------------------------------------------
qemu.git(ppoll)44 96
ppoll+epoll 85 101
timerfd+epoll 87 109Quoted 4 lines
Quoted 2 lines
(In high # of fd case, 3 activated but idle virtio-console devices are attached, which will add us hundereds of fds to poll)
Have you rerun benchmarks with this patch series?
Quoted 8 lines
Yes, here:
syscall high # of fd low # of fd
(Unit MB/s) (Unit MB/s)
-------------------------------------------------
qemu.git(ppoll)24 73
ppoll+epoll 49 77
timerfd+epoll 49 82
Nice. Weird that the results are much lower than the v2 results, but maybe the host changed?
Quoted 4 lines
Quoted 4 lines
I wonder how the ppoll-only performance changes. It seems like there are now additional copies of <fd, events, revents> information and corresponding malloc/realloc/frees.
Please add a ppoll-only column hosts so we can be confident that the non-epoll code works. It will also show that non-Linux POSIX hosts haven't regressed.
Thanks,
Stefan