File c61d1e9b-virfile-set-pipe-size.patch of Package libvirt.25654

commit c61d1e9ba0a0bec18fdb0bdd485060dc27a4e5cc
Author: Claudio Fontana <cfontana@suse.de>
Date:   Fri Mar 25 16:10:19 2022 +0100

    virfile: set pipe size in virFileWrapperFdNew to improve throughput
    
    currently the only user of virFileWrapperFdNew is the qemu driver;
    virsh save is very slow with a default pipe size.
    This change improves throughput by ~400% on fast nvme or ramdisk.
    
    Best value currently measured is 1MB, which happens to be also
    the kernel default for the pipe-max-size.
    
    Signed-off-by: Claudio Fontana <cfontana@suse.de>
    Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
    Reviewed-by: Michal Privoznik <mprivozn@redhat.com>

Index: libvirt-7.1.0/src/util/virfile.c
===================================================================
--- libvirt-7.1.0.orig/src/util/virfile.c
+++ libvirt-7.1.0/src/util/virfile.c
@@ -201,6 +201,54 @@ struct _virFileWrapperFd {
 };
 
 #ifndef WIN32
+
+#ifdef __linux__
+
+/**
+ * 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 transfers
+ * to fast devices. A value of 1MB has been measured to improve virsh save
+ * by 400% in ideal conditions. We retry multiple times with smaller sizes
+ * on EPERM to account for possible small values of /proc/sys/fs/pipe-max-size.
+ *
+ * OS note: only for linux, on other OS this is a no-op.
+ */
+static void
+virFileWrapperSetPipeSize(int fd)
+{
+    int sz;
+
+    for (sz = 1024 * 1024; sz >= 64 * 1024; sz /= 2) {
+        int rv = fcntl(fd, F_SETPIPE_SZ, sz);
+
+        if (rv < 0 && errno == EPERM) {
+            VIR_DEBUG("EPERM trying to set fd %d pipe size to %d", fd, sz);
+            continue; /* retry with half the size */
+        }
+        if (rv < 0) {
+            break;
+        }
+        VIR_DEBUG("fd %d pipe size adjusted to %d", fd, sz);
+        return;
+    }
+
+    VIR_WARN("unable to set pipe size, data transfer might be slow: %s",
+             g_strerror(errno));
+}
+
+#else /* !__linux__ */
+static void
+virFileWrapperSetPipeSize(int fd G_GNUC_UNUSED)
+{
+    return;
+}
+#endif /* !__linux__ */
+
+
 /**
  * virFileWrapperFdNew:
  * @fd: pointer to fd to wrap
@@ -282,6 +330,8 @@ virFileWrapperFdNew(int *fd, const char
 
     ret->cmd = virCommandNewArgList(iohelper_path, name, NULL);
 
+    virFileWrapperSetPipeSize(pipefd[output]);
+
     if (output) {
         virCommandSetInputFD(ret->cmd, pipefd[0]);
         virCommandSetOutputFD(ret->cmd, fd);
openSUSE Build Service is sponsored by