From patchwork Mon Feb 22 21:26:43 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcelo Tosatti X-Patchwork-Id: 81303 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter.kernel.org (8.14.3/8.14.3) with ESMTP id o1MLSa9r017833 for ; Mon, 22 Feb 2010 21:28:36 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754714Ab0BVV23 (ORCPT ); Mon, 22 Feb 2010 16:28:29 -0500 Received: from mx1.redhat.com ([209.132.183.28]:38922 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754474Ab0BVV22 (ORCPT ); Mon, 22 Feb 2010 16:28:28 -0500 Received: from int-mx05.intmail.prod.int.phx2.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.18]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id o1MLRlxn021628 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Mon, 22 Feb 2010 16:27:47 -0500 Received: from ns3.rdu.redhat.com (ns3.rdu.redhat.com [10.11.255.199]) by int-mx05.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id o1MLRkqc020843; Mon, 22 Feb 2010 16:27:46 -0500 Received: from amt.cnet (vpn-11-163.rdu.redhat.com [10.11.11.163]) by ns3.rdu.redhat.com (8.13.8/8.13.8) with ESMTP id o1MLRiRS016390; Mon, 22 Feb 2010 16:27:45 -0500 Received: from amt.cnet (amt.cnet [127.0.0.1]) by amt.cnet (Postfix) with ESMTP id 70E0F68A6D4; Mon, 22 Feb 2010 18:27:01 -0300 (BRT) Received: (from marcelo@localhost) by amt.cnet (8.14.3/8.14.3/Submit) id o1MLQw7i000782; Mon, 22 Feb 2010 18:26:58 -0300 From: Marcelo Tosatti To: Anthony Liguori Cc: qemu-devel@nongnu.org, kvm@vger.kernel.org, Paolo Bonzini , Avi Kivity Subject: [PATCH 1/8] use eventfd for iothread Date: Mon, 22 Feb 2010 18:26:43 -0300 Message-Id: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.67 on 10.5.11.18 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.3 (demeter.kernel.org [140.211.167.41]); Mon, 22 Feb 2010 21:28:37 +0000 (UTC) diff --git a/osdep.c b/osdep.c index 9059f01..9e4b17b 100644 --- a/osdep.c +++ b/osdep.c @@ -37,6 +37,10 @@ #include #endif +#ifdef CONFIG_EVENTFD +#include +#endif + #ifdef _WIN32 #include #elif defined(CONFIG_BSD) @@ -281,6 +285,34 @@ ssize_t qemu_write_full(int fd, const void *buf, size_t count) #ifndef _WIN32 /* + * Creates an eventfd that looks like a pipe and has EFD_CLOEXEC set. + */ +int qemu_eventfd(int fds[2]) +{ + int ret; + +#ifdef CONFIG_EVENTFD + ret = eventfd(0, 0); + if (ret >= 0) { + fds[0] = ret; + qemu_set_cloexec(ret); + if ((fds[1] = dup(ret)) == -1) { + close(ret); + return -1; + } + qemu_set_cloexec(fds[1]); + return 0; + } + + if (errno != ENOSYS) { + return -1; + } +#endif + + return qemu_pipe(fds); +} + +/* * Creates a pipe with FD_CLOEXEC set on both file descriptors */ int qemu_pipe(int pipefd[2]) diff --git a/qemu-common.h b/qemu-common.h index b09f717..c941006 100644 --- a/qemu-common.h +++ b/qemu-common.h @@ -170,6 +170,7 @@ ssize_t qemu_write_full(int fd, const void *buf, size_t count) void qemu_set_cloexec(int fd); #ifndef _WIN32 +int qemu_eventfd(int pipefd[2]); int qemu_pipe(int pipefd[2]); #endif diff --git a/vl.c b/vl.c index 98918ac..1957018 100644 --- a/vl.c +++ b/vl.c @@ -3211,14 +3211,15 @@ static int io_thread_fd = -1; static void qemu_event_increment(void) { - static const char byte = 0; + /* Write 8 bytes to be compatible with eventfd. */ + static uint64_t val = 1; ssize_t ret; if (io_thread_fd == -1) return; do { - ret = write(io_thread_fd, &byte, sizeof(byte)); + ret = write(io_thread_fd, &val, sizeof(val)); } while (ret < 0 && errno == EINTR); /* EAGAIN is fine, a read must be pending. */ @@ -3235,7 +3236,7 @@ static void qemu_event_read(void *opaque) ssize_t len; char buffer[512]; - /* Drain the notify pipe */ + /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */ do { len = read(fd, buffer, sizeof(buffer)); } while ((len == -1 && errno == EINTR) || len == sizeof(buffer)); @@ -3246,7 +3247,7 @@ static int qemu_event_init(void) int err; int fds[2]; - err = qemu_pipe(fds); + err = qemu_eventfd(fds); if (err == -1) return -errno;