From patchwork Wed Apr 17 09:46:52 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 237189 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 9A64C2C010A for ; Wed, 17 Apr 2013 19:47:51 +1000 (EST) Received: from localhost ([::1]:33011 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1USOxx-0006XI-Qc for incoming@patchwork.ozlabs.org; Wed, 17 Apr 2013 05:47:49 -0400 Received: from eggs.gnu.org ([208.118.235.92]:39366) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1USOxM-0006RG-M1 for qemu-devel@nongnu.org; Wed, 17 Apr 2013 05:47:14 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1USOxL-0002Ep-Bb for qemu-devel@nongnu.org; Wed, 17 Apr 2013 05:47:12 -0400 Received: from mail-ee0-f49.google.com ([74.125.83.49]:60600) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1USOxL-0002EX-6K for qemu-devel@nongnu.org; Wed, 17 Apr 2013 05:47:11 -0400 Received: by mail-ee0-f49.google.com with SMTP id l10so633005eei.36 for ; Wed, 17 Apr 2013 02:47:10 -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=kPxHAHd69+fL5vFbOfCi7fjsBzBHlW6RVyNdsNvx2Ps=; b=0INNMYS43DFT75acfwP9kvHM7AnNsx6P61Neer7QQzOO95DtgHz2iuU0N+1SUDFbN8 l+al2GUgVsy7SKd4fdC7EEtsAZ6uIPJYAKAwL0+N80Ulu6tdY60OlJs787LeuqMWoLRL qZ0BeuV8B4IiEd0kThCSukYcFWLFOY4zpXlYgB9OBm2yvPj4+/o3xAcUcTa+gXvhJM/y +24wrmrIRKIoexLuy1VmhpFT3ScqlEGJmTEkoshH6ubtdco2knmSWCbsOdmvWd1LGdpk lF8nhn41/GM0AaX3QAZIrhPQd5aoWSYfoM+u1w/6nxz/NLTqLccxux16SofqjeX/3Ug5 0FGQ== X-Received: by 10.14.0.5 with SMTP id 5mr16497568eea.13.1366192030399; Wed, 17 Apr 2013 02:47:10 -0700 (PDT) Received: from playground.lan (93-34-176-20.ip50.fastwebnet.it. [93.34.176.20]) by mx.google.com with ESMTPS id w51sm7697038eev.13.2013.04.17.02.47.08 (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Wed, 17 Apr 2013 02:47:09 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Wed, 17 Apr 2013 11:46:52 +0200 Message-Id: <1366192012-14872-7-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 1.8.1.4 In-Reply-To: <1366192012-14872-1-git-send-email-pbonzini@redhat.com> References: <1366192012-14872-1-git-send-email-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 74.125.83.49 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. Reviewed-by: Juan Quintela Reviewed-by: Orit Wassermann Signed-off-by: Paolo Bonzini --- savevm.c | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 90 insertions(+), 11 deletions(-) diff --git a/savevm.c b/savevm.c index ffabbff..31dcce9 100644 --- a/savevm.c +++ b/savevm.c @@ -356,9 +356,94 @@ static const QEMUFileOps stdio_file_write_ops = { .close = stdio_fclose }; +static ssize_t unix_writev_buffer(void *opaque, struct iovec *iov, int iovcnt, + int64_t pos) +{ + 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 +452,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 = {