From patchwork Wed Mar 27 16:36:32 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 231760 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id EBD402C007E for ; Thu, 28 Mar 2013 03:55:50 +1100 (EST) Received: from localhost ([::1]:54590 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UKtNG-0000zc-4O for incoming@patchwork.ozlabs.org; Wed, 27 Mar 2013 12:38:54 -0400 Received: from eggs.gnu.org ([208.118.235.92]:46134) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UKtMm-0000pS-SO for qemu-devel@nongnu.org; Wed, 27 Mar 2013 12:38:26 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UKtLG-0002vl-9c for qemu-devel@nongnu.org; Wed, 27 Mar 2013 12:36:57 -0400 Received: from mail-ea0-x235.google.com ([2a00:1450:4013:c01::235]:63836) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UKtLG-0002vR-16 for qemu-devel@nongnu.org; Wed, 27 Mar 2013 12:36:50 -0400 Received: by mail-ea0-f181.google.com with SMTP id z10so3421849ead.26 for ; Wed, 27 Mar 2013 09:36:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:sender:from:to:cc:subject:date:message-id:x-mailer :in-reply-to:references; bh=yDonGecgK9saveZTKop5E3cLfHLf3XGzT3FtN88s9Ss=; b=PxZFI9TCyrA67UAXFuYo1a2gSMhM+ERxOQg6V6rgGh+thdbLAtsQ7jVI3n6IST/fgx J/01a/mhW6sQLZFC3hjpqMHjqybHduJzwuLPaebGW5Q/Zmhr4uUJ0zVcJgd1wNDMLiIz HIYz2ig1WUYYtu1QjnNNRqHqEraCxSTsJj3oqqXd6hV1YklcUuu4ZiBpq7qP9UhqWTPK r1/yWIBAUeIgjxTTCxk75iLLIlbRuNB4Bdhp9lZR2nu6wYuYjTaF7f90QeUaUVDZ28Th Cc3FjLKBszAsRvvGGegDmy3vrvh8Wq9RZda1M/VF/nTHK1F/hQl+hxSAKulgQn9rFiug 7D7w== X-Received: by 10.14.223.69 with SMTP id u45mr57264882eep.23.1364402209213; Wed, 27 Mar 2013 09:36:49 -0700 (PDT) Received: from playground.lan (93-34-176-20.ip50.fastwebnet.it. [93.34.176.20]) by mx.google.com with ESMTPS id d47sm32366397eem.9.2013.03.27.09.36.47 (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Wed, 27 Mar 2013 09:36:48 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Wed, 27 Mar 2013 17:36:32 +0100 Message-Id: <1364402192-18169-7-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 1.8.1.4 In-Reply-To: <1364402192-18169-1-git-send-email-pbonzini@redhat.com> References: <1364402192-18169-1-git-send-email-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2a00:1450:4013:c01::235 Cc: owasserm@redhat.com, quintela@redhat.com Subject: [Qemu-devel] [PATCH 6/6] qemu-file: do not use stdio for qemu_fdopen X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org This uses system calls directly for Unix file descriptors, so that the efficient writev_buffer can be used. Pay attention to the possibility of partial writes in writev. Signed-off-by: Paolo Bonzini Reviewed-by: Juan Quintela --- savevm.c | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 89 insertions(+), 11 deletions(-) diff --git a/savevm.c b/savevm.c index 0415830..8eb5aab 100644 --- a/savevm.c +++ b/savevm.c @@ -356,9 +356,93 @@ static const QEMUFileOps stdio_file_write_ops = { .close = stdio_fclose }; +static ssize_t unix_writev_buffer(void *opaque, struct iovec *iov, int iovcnt) +{ + QEMUFileSocket *s = opaque; + ssize_t len, offset; + ssize_t size = iov_size(iov, iovcnt); + ssize_t total = 0; + + assert(iovcnt > 0); + offset = 0; + while (size > 0) { + /* Find the next start position; skip all full-sized vector elements */ + while (offset >= iov[0].iov_len) { + offset -= iov[0].iov_len; + iov++, iovcnt--; + } + + /* skip `offset' bytes from the (now) first element, undo it on exit */ + assert(iovcnt > 0); + iov[0].iov_base += offset; + iov[0].iov_len -= offset; + + do { + len = writev(s->fd, iov, iovcnt); + } while (len == -1 && errno == EINTR); + if (len == -1) { + return -errno; + } + + /* Undo the changes above */ + iov[0].iov_base -= offset; + iov[0].iov_len += offset; + + /* Prepare for the next iteration */ + offset += len; + total += len; + size -= len; + } + + return total; +} + +static int unix_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size) +{ + QEMUFileSocket *s = opaque; + ssize_t len; + + for (;;) { + len = read(s->fd, buf, size); + if (len != -1) { + break; + } + if (errno == EAGAIN) { + yield_until_fd_readable(s->fd); + } else if (errno != EINTR) { + break; + } + } + + if (len == -1) { + len = -errno; + } + return len; +} + +static int unix_close(void *opaque) +{ + QEMUFileSocket *s = opaque; + close(s->fd); + g_free(s); + return 0; +} + +static const QEMUFileOps unix_read_ops = { + .get_fd = socket_get_fd, + .get_buffer = unix_get_buffer, + .close = unix_close +}; + +static const QEMUFileOps unix_write_ops = { + .get_fd = socket_get_fd, + .writev_buffer = unix_writev_buffer, + .close = unix_close +}; + QEMUFile *qemu_fdopen(int fd, const char *mode) { - QEMUFileStdio *s; + QEMUFileSocket *s; if (mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || @@ -367,21 +451,15 @@ QEMUFile *qemu_fdopen(int fd, const char *mode) return NULL; } - s = g_malloc0(sizeof(QEMUFileStdio)); - s->stdio_file = fdopen(fd, mode); - if (!s->stdio_file) - goto fail; + s = g_malloc0(sizeof(QEMUFileSocket)); + s->fd = fd; if(mode[0] == 'r') { - s->file = qemu_fopen_ops(s, &stdio_file_read_ops); + s->file = qemu_fopen_ops(s, &unix_read_ops); } else { - s->file = qemu_fopen_ops(s, &stdio_file_write_ops); + s->file = qemu_fopen_ops(s, &unix_write_ops); } return s->file; - -fail: - g_free(s); - return NULL; } static const QEMUFileOps socket_read_ops = {