From: Fam Zheng <famz@redhat.com>
To: qemu-devel@nongnu.org
Cc: Kevin Wolf <kwolf@redhat.com>,
Paolo Bonzini <pbonzini@redhat.com>,
Stefan Hajnoczi <stefanha@redhat.com>
Subject: [Qemu-devel] [PATCH v3 7/7] poll-linux: Add timerfd support
Date: Thu, 16 Apr 2015 12:57:36 +0800 [thread overview]
Message-ID: <1429160256-27231-8-git-send-email-famz@redhat.com> (raw)
In-Reply-To: <1429160256-27231-1-git-send-email-famz@redhat.com>
In 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).
Signed-off-by: Fam Zheng <famz@redhat.com>
---
poll-linux.c | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 83 insertions(+), 1 deletion(-)
diff --git a/poll-linux.c b/poll-linux.c
index 7605004..154da8b 100644
--- a/poll-linux.c
+++ b/poll-linux.c
@@ -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);
--
1.9.3
next prev parent reply other threads:[~2015-04-16 4:58 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-04-16 4:57 [Qemu-devel] [PATCH v3 0/7] aio: Support epoll by introducing qemu_poll abstraction Fam Zheng
2015-04-16 4:57 ` [Qemu-devel] [PATCH v3 1/7] poll: Introduce QEMU Poll API Fam Zheng
2015-04-16 4:57 ` [Qemu-devel] [PATCH v3 2/7] posix-aio: Use QEMU poll interface Fam Zheng
2015-04-16 4:57 ` [Qemu-devel] [PATCH v3 3/7] poll: Add epoll implementation for qemu_poll Fam Zheng
2015-04-16 4:57 ` [Qemu-devel] [PATCH v3 4/7] main-loop: Replace qemu_poll_ns with qemu_poll Fam Zheng
2015-04-16 4:57 ` [Qemu-devel] [PATCH v3 5/7] tests: Add test case for qemu_poll Fam Zheng
2015-04-16 4:57 ` [Qemu-devel] [PATCH v3 6/7] poll-glib: Support ppoll Fam Zheng
2015-04-16 4:57 ` Fam Zheng [this message]
2015-04-16 13:00 ` [Qemu-devel] [PATCH v3 7/7] poll-linux: Add timerfd support Stefan Hajnoczi
2015-04-16 13:03 ` [Qemu-devel] [PATCH v3 0/7] aio: Support epoll by introducing qemu_poll abstraction Stefan Hajnoczi
2015-04-17 2:02 ` Fam Zheng
2015-04-20 10:36 ` Stefan Hajnoczi
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1429160256-27231-8-git-send-email-famz@redhat.com \
--to=famz@redhat.com \
--cc=kwolf@redhat.com \
--cc=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=stefanha@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).