From nobody Wed May 15 22:09:58 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) client-ip=170.10.129.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail(p=none dis=none) header.from=suse.de Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by mx.zohomail.com with SMTPS id 1647850416660180.4801894279235; Mon, 21 Mar 2022 01:13:36 -0700 (PDT) Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-171-__SR25X3PXOiJEpnr9AmJQ-1; Mon, 21 Mar 2022 04:13:32 -0400 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 2FC153C153A4; Mon, 21 Mar 2022 08:13:30 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id 72EDF4229D4; Mon, 21 Mar 2022 08:13:29 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (localhost [IPv6:::1]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 0FCAE19451EF; Mon, 21 Mar 2022 08:13:29 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 3412819452D2 for ; Mon, 21 Mar 2022 08:13:28 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id 12702400F721; Mon, 21 Mar 2022 08:13:28 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast06.extmail.prod.ext.rdu2.redhat.com [10.11.55.22]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 0E3F140CF8F0 for ; Mon, 21 Mar 2022 08:13:28 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-2.mimecast.com [205.139.110.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id EAB5B18812CB for ; Mon, 21 Mar 2022 08:13:27 +0000 (UTC) Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-612-yPiIhg8HMpuuDyHKjHXHkQ-1; Mon, 21 Mar 2022 04:13:26 -0400 Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 315C8210E5; Mon, 21 Mar 2022 08:13:24 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id EFBCA13AD5; Mon, 21 Mar 2022 08:13:23 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id 7AbTOKMzOGJ8NAAAMHmgww (envelope-from ); Mon, 21 Mar 2022 08:13:23 +0000 X-MC-Unique: __SR25X3PXOiJEpnr9AmJQ-1 X-Original-To: libvir-list@listman.corp.redhat.com X-MC-Unique: yPiIhg8HMpuuDyHKjHXHkQ-1 From: Claudio Fontana To: =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= Subject: [libvirt RFC v2] virfile: set pipe size in virFileWrapperFdNew to improve throughput Date: Mon, 21 Mar 2022 09:13:20 +0100 Message-Id: <20220321081320.5871-1-cfontana@suse.de> MIME-Version: 1.0 X-Mimecast-Impersonation-Protect: Policy=CLT - Impersonation Protection Definition; Similar Internal Domain=false; Similar Monitored External Domain=false; Custom External Domain=false; Mimecast External Domain=false; Newly Observed Domain=false; Internal User Name=false; Custom Display Name List=false; Reply-to Address Mismatch=false; Targeted Threat Dictionary=false; Mimecast Threat Dictionary=false; Custom Threat Dictionary=false X-Scanned-By: MIMEDefang 2.84 on 10.11.54.1 X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Ani Sinha , =?UTF-8?q?Michal=20Pr=C3=ADvozn=C3=ADk?= , Claudio Fontana , libvir-list@redhat.com Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" X-Scanned-By: MIMEDefang 2.85 on 10.11.54.9 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZM-MESSAGEID: 1647850418594100001 Content-Type: text/plain; charset="utf-8" virsh save is very slow with a default pipe size, so set a larger one. This change improves throughput by ~400% on fast nvme or ramdisk, for the current only user of virFileWrapperFdNew: the qemu driver. Best value currently measured is 1MB, which happens to be also the kernel default for the pipe-max-size. We do not try to use a pipe buffer larger than what the setting of /proc/sys/fs/pipe-max-size currently allows. Signed-off-by: Claudio Fontana --- src/util/virfile.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) see v1 at https://listman.redhat.com/archives/libvir-list/2022-March/229252.html Changes v1 -> v2: * removed VIR_FILE_WRAPPER_BIG_PIPE, made the new pipe resizing unconditional (Michal) * moved code to separate functions (Michal) * removed ternary op, disliked in libvirt (Michal) * added #ifdef __linux__ (Ani Sinha) * try smallest value between currently best measured value (1MB) and the pipe-max-size setting. If pipe-max-size cannot be read, try kernel default max (1MB). (Daniel) diff --git a/src/util/virfile.c b/src/util/virfile.c index a04f888e06..13bdd42c68 100644 --- a/src/util/virfile.c +++ b/src/util/virfile.c @@ -201,6 +201,71 @@ struct _virFileWrapperFd { }; =20 #ifndef WIN32 + +#ifdef __linux__ +/** + * virFileWrapperGetBestPipeSize: + * + * get the best pipe size to use with virFileWrapper. + * + * We first check the maximum we are allowed by the system pipe-max-size, + * and then use the minimum between that and our tested best value. + * This is because a request beyond pipe-max-size may fail with EPERM. + * If we are unable to read pipe-max-size, use the kernel default (1MB). + * + * Return value is the pipe size to use. + */ + +static int virFileWrapperGetBestPipeSize(void) +{ + const char path[] =3D "/proc/sys/fs/pipe-max-size"; + int best_sz =3D 1024 * 1024; /* good virsh save results with this size= */ + int max_sz; + + if (virFileReadValueInt(&max_sz, path) < 0) { + max_sz =3D 1024 * 1024; /* this is the kernel default pipe-max-siz= e */ + VIR_WARN("failed to read %s, trying default %d", path, max_sz); + } else if (max_sz > best_sz) { + max_sz =3D best_sz; + } + return max_sz; +} + +/** + * virFileWrapperSetPipeSize: + * @fd: the fd of the pipe + * + * Set best pipe size on the passed file descriptor for bulk transfers of = data. + * + * default pipe size (usually 64K) is generally not suited for large trans= fers + * to fast devices. This has been measured to improve virsh save by 400% + * in ideal conditions. + * + * Return value is 0 on success, -1 and errno set on error. + * OS note: only for linux, on other OS this is a no-op. + */ +static int +virFileWrapperSetPipeSize(int fd) +{ + int pipe_sz =3D virFileWrapperGetBestPipeSize(); + int rv =3D fcntl(fd, F_SETPIPE_SZ, pipe_sz); + + if (rv < 0) { + VIR_ERROR(_("failed to set pipe size to %d (errno=3D%d)"), pipe_sz= , errno); + return -1; + } + VIR_INFO("fd %d pipe size adjusted to %d", fd, rv); + return 0; +} + +#else /* !__linux__ */ +static int virFileWrapperSetPipeSize(int fd) +{ + return 0; +} +#endif /* !__linux__ */ + + /** * virFileWrapperFdNew: * @fd: pointer to fd to wrap @@ -282,6 +347,10 @@ virFileWrapperFdNew(int *fd, const char *name, unsigne= d int flags) =20 ret->cmd =3D virCommandNewArgList(iohelper_path, name, NULL); =20 + if (virFileWrapperSetPipeSize(pipefd[!output]) < 0) { + virReportError(VIR_ERR_SYSTEM_ERROR, "%s", _("unable to set pipe s= ize, data transfer might be slow")); + } + if (output) { virCommandSetInputFD(ret->cmd, pipefd[0]); virCommandSetOutputFD(ret->cmd, fd); --=20 2.35.1