Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1Nyli4-0000Ce-NG for qemu-devel@nongnu.org; Mon, 05 Apr 2010 08:47:20 -0400 Received: from [140.186.70.92] (port=47040 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Nyli3-0000C3-88 for qemu-devel@nongnu.org; Mon, 05 Apr 2010 08:47:20 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1Nyli0-0007ez-HU for qemu-devel@nongnu.org; Mon, 05 Apr 2010 08:47:19 -0400 Received: from mx1.redhat.com ([209.132.183.28]:59735) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1Nylhz-0007et-Sb for qemu-devel@nongnu.org; Mon, 05 Apr 2010 08:47:16 -0400 From: Amit Shah Date: Mon, 5 Apr 2010 18:15:35 +0530 Message-Id: <1270471538-31275-3-git-send-email-amit.shah@redhat.com> In-Reply-To: <1270471538-31275-2-git-send-email-amit.shah@redhat.com> References: <1270471538-31275-1-git-send-email-amit.shah@redhat.com> <1270471538-31275-2-git-send-email-amit.shah@redhat.com> Subject: [Qemu-devel] [PATCH 2/5] char: unix write: Add some sleep to ease off spinning in a tight loop List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu list Cc: Amit Shah , Gerd Hoffmann , Juan Quintela When the other end of a chardev connection isn't picking up data as fast as we're sending, we just used to keep spinning in a tight loop till all the data was sent out. Polling for POLLOUT indefinitely gives the other end a chance to catch up and also saves us CPU cycles. Signed-off-by: Amit Shah --- qemu-char.c | 17 +++++++++++++++-- 1 files changed, 15 insertions(+), 2 deletions(-) diff --git a/qemu-char.c b/qemu-char.c index d5b1662..48e1e57 100644 --- a/qemu-char.c +++ b/qemu-char.c @@ -36,6 +36,7 @@ #include #include +#include #include #include #include @@ -513,15 +514,27 @@ int send_all(int fd, const void *buf, size_t *len) static int unix_write(int fd, const uint8_t *buf, size_t *len) { + struct pollfd pollfds[1]; ssize_t tmplen, ret; + pollfds[0].fd = fd; + pollfds[0].events = POLLOUT; + tmplen = *len; *len = 0; while (tmplen > 0) { + ret = poll(pollfds, 1, -1); + if (ret == -1) { + if (errno == EINTR) { + continue; + } + return -1; + } ret = write(fd, buf, tmplen); if (ret < 0) { - if (errno != EINTR && errno != EAGAIN) - return -1; + if (errno != EINTR && errno != EAGAIN) { + return -1; + } } else if (ret == 0) { break; } else { -- 1.6.2.5