9119d9
From e342a8e4885d43c7c598dc5d3a1146b5d8d0051b Mon Sep 17 00:00:00 2001
9119d9
Message-Id: <e342a8e4885d43c7c598dc5d3a1146b5d8d0051b@dist-git>
9119d9
From: John Ferlan <jferlan@redhat.com>
9119d9
Date: Thu, 18 Sep 2014 09:29:55 -0400
9119d9
Subject: [PATCH] domain_conf: Add iothreadpin to cputune
9119d9
9119d9
https://bugzilla.redhat.com/show_bug.cgi?id=1101574
9119d9
9119d9
Add an option 'iothreadpin' to the <cpuset> to allow for setting the
9119d9
CPU affinity for each IOThread.
9119d9
9119d9
The iothreadspin will mimic the vcpupin with respect to being able to
9119d9
assign each iothread to a specific CPU, although iothreads ids start
9119d9
at 1 while vcpu ids start at 0. This matches the iothread naming scheme.
9119d9
9119d9
(cherry picked from commit 938fb12fad6d15c9fdb73f998c4e0ec1e278721f)
9119d9
Signed-off-by: John Ferlan <jferlan@redhat.com>
9119d9
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
9119d9
---
9119d9
 docs/formatdomain.html.in                          |  18 +++
9119d9
 docs/schemas/domaincommon.rng                      |  10 ++
9119d9
 src/conf/domain_conf.c                             | 124 +++++++++++++++++++--
9119d9
 src/conf/domain_conf.h                             |   2 +
9119d9
 .../qemuxml2argv-cputune-iothreads.xml             |  38 +++++++
9119d9
 tests/qemuxml2xmltest.c                            |   1 +
9119d9
 6 files changed, 182 insertions(+), 11 deletions(-)
9119d9
 create mode 100644 tests/qemuxml2argvdata/qemuxml2argv-cputune-iothreads.xml
9119d9
9119d9
diff --git a/docs/formatdomain.html.in b/docs/formatdomain.html.in
9119d9
index b947848..8c03ebb 100644
9119d9
--- a/docs/formatdomain.html.in
9119d9
+++ b/docs/formatdomain.html.in
9119d9
@@ -526,6 +526,8 @@
9119d9
     <vcpupin vcpu="2" cpuset="2,3"/>
9119d9
     <vcpupin vcpu="3" cpuset="0,4"/>
9119d9
     <emulatorpin cpuset="1-3"/>
9119d9
+    <iothreadpin iothread="1" cpuset="5,6"/>
9119d9
+    <iothreadpin iothread="2" cpuset="7,8"/>
9119d9
     <shares>2048</shares>
9119d9
     <period>1000000</period>
9119d9
     <quota>-1</quota>
9119d9
@@ -567,6 +569,22 @@
9119d9
          attribute placement of element vcpu is
9119d9
          "auto".
9119d9
        
9119d9
+       
iothreadpin
9119d9
+       
9119d9
+         The optional iothreadpin element specifies which of host
9119d9
+         physical CPUs the IOThreads will be pinned to. If this is omitted
9119d9
+         and attribute cpuset of element vcpu is
9119d9
+         not specified, the IOThreads are pinned to all the physical CPUs
9119d9
+         by default. There are two required attributes, the attribute
9119d9
+         iothread specifies the IOThread id and the attribute
9119d9
+         cpuset specifying which physical CPUs to pin to. The
9119d9
+         iothread value begins at "1" through the number of
9119d9
+          iothreads
9119d9
+         allocated to the domain. A value of "0" is not permitted.
9119d9
+         NB, iothreadpin is not allowed if attribute
9119d9
+         placement of element vcpu is "auto".
9119d9
+        Since 1.2.9
9119d9
+       
9119d9
       
shares
9119d9
       
9119d9
         The optional shares element specifies the proportional
9119d9
diff --git a/docs/schemas/domaincommon.rng b/docs/schemas/domaincommon.rng
9119d9
index 31974ac..d61d3ad 100644
9119d9
--- a/docs/schemas/domaincommon.rng
9119d9
+++ b/docs/schemas/domaincommon.rng
9119d9
@@ -810,6 +810,16 @@
9119d9
           </attribute>
9119d9
         </element>
9119d9
       </optional>
9119d9
+      <zeroOrMore>
9119d9
+        <element name="iothreadpin">
9119d9
+          <attribute name="iothread">
9119d9
+            <ref name="unsignedInt"/>
9119d9
+          </attribute>
9119d9
+          <attribute name="cpuset">
9119d9
+            <ref name="cpuset"/>
9119d9
+          </attribute>
9119d9
+        </element>
9119d9
+      </zeroOrMore>
9119d9
     </element>
9119d9
   </define>
9119d9
 
9119d9
diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
9119d9
index 7d78b0b..c03affe 100644
9119d9
--- a/src/conf/domain_conf.c
9119d9
+++ b/src/conf/domain_conf.c
9119d9
@@ -2169,6 +2169,9 @@ void virDomainDefFree(virDomainDefPtr def)
9119d9
 
9119d9
     virDomainVcpuPinDefFree(def->cputune.emulatorpin);
9119d9
 
9119d9
+    virDomainVcpuPinDefArrayFree(def->cputune.iothreadspin,
9119d9
+                                 def->cputune.niothreadspin);
9119d9
+
9119d9
     virDomainNumatuneFree(def->numatune);
9119d9
 
9119d9
     virSysinfoDefFree(def->sysinfo);
9119d9
@@ -11415,6 +11418,9 @@ virDomainPanicDefParseXML(xmlNodePtr node)
9119d9
  * and emulatorpin has the form of
9119d9
  *   <emulatorpin cpuset='0'/>
9119d9
  *
9119d9
+ * and an iothreadspin has the form
9119d9
+ *   <iothreadpin iothread='1' cpuset='2'/>
9119d9
+ *
9119d9
  * A vcpuid of -1 is valid and only valid for emulatorpin. So callers
9119d9
  * have to check the returned cpuid for validity.
9119d9
  */
9119d9
@@ -11422,11 +11428,13 @@ static virDomainVcpuPinDefPtr
9119d9
 virDomainVcpuPinDefParseXML(xmlNodePtr node,
9119d9
                             xmlXPathContextPtr ctxt,
9119d9
                             int maxvcpus,
9119d9
-                            bool emulator)
9119d9
+                            bool emulator,
9119d9
+                            bool iothreads)
9119d9
 {
9119d9
     virDomainVcpuPinDefPtr def;
9119d9
     xmlNodePtr oldnode = ctxt->node;
9119d9
     int vcpuid = -1;
9119d9
+    unsigned int iothreadid;
9119d9
     char *tmp = NULL;
9119d9
     int ret;
9119d9
 
9119d9
@@ -11435,7 +11443,7 @@ virDomainVcpuPinDefParseXML(xmlNodePtr node,
9119d9
 
9119d9
     ctxt->node = node;
9119d9
 
9119d9
-    if (!emulator) {
9119d9
+    if (!emulator && !iothreads) {
9119d9
         ret = virXPathInt("string(./@vcpu)", ctxt, &vcpuid);
9119d9
         if ((ret == -2) || (vcpuid < -1)) {
9119d9
             virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
9119d9
@@ -11456,10 +11464,41 @@ virDomainVcpuPinDefParseXML(xmlNodePtr node,
9119d9
         def->vcpuid = vcpuid;
9119d9
     }
9119d9
 
9119d9
+    if (iothreads && (tmp = virXPathString("string(./@iothread)", ctxt))) {
9119d9
+        if (virStrToLong_uip(tmp, NULL, 10, &iothreadid) < 0) {
9119d9
+            virReportError(VIR_ERR_XML_ERROR,
9119d9
+                           _("invalid setting for iothread '%s'"), tmp);
9119d9
+            goto error;
9119d9
+        }
9119d9
+        VIR_FREE(tmp);
9119d9
+
9119d9
+        if (iothreadid == 0) {
9119d9
+            virReportError(VIR_ERR_XML_ERROR, "%s",
9119d9
+                           _("zero is an invalid iothread id value"));
9119d9
+            goto error;
9119d9
+        }
9119d9
+
9119d9
+        /* NB: maxvcpus is actually def->iothreads
9119d9
+         * IOThreads are numbered "iothread1...iothread<n>", where
9119d9
+         * "n" is the iothreads value
9119d9
+         */
9119d9
+        if (iothreadid > maxvcpus) {
9119d9
+            virReportError(VIR_ERR_XML_ERROR, "%s",
9119d9
+                           _("iothread id must not exceed iothreads"));
9119d9
+            goto error;
9119d9
+        }
9119d9
+
9119d9
+        /* Rather than creating our own structure we are reusing the vCPU */
9119d9
+        def->vcpuid = iothreadid;
9119d9
+    }
9119d9
+
9119d9
     if (!(tmp = virXMLPropString(node, "cpuset"))) {
9119d9
         if (emulator)
9119d9
             virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
9119d9
                            _("missing cpuset for emulatorpin"));
9119d9
+        else if (iothreads)
9119d9
+            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
9119d9
+                           _("missing cpuset for iothreadpin"));
9119d9
         else
9119d9
             virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
9119d9
                            _("missing cpuset for vcpupin"));
9119d9
@@ -12138,7 +12177,7 @@ virDomainDefParseXML(xmlDocPtr xml,
9119d9
     for (i = 0; i < n; i++) {
9119d9
         virDomainVcpuPinDefPtr vcpupin = NULL;
9119d9
         vcpupin = virDomainVcpuPinDefParseXML(nodes[i], ctxt,
9119d9
-                                              def->maxvcpus, false);
9119d9
+                                              def->maxvcpus, false, false);
9119d9
 
9119d9
         if (!vcpupin)
9119d9
             goto error;
9119d9
@@ -12213,8 +12252,9 @@ virDomainDefParseXML(xmlDocPtr xml,
9119d9
                 goto error;
9119d9
             }
9119d9
 
9119d9
-            def->cputune.emulatorpin = virDomainVcpuPinDefParseXML(nodes[0], ctxt,
9119d9
-                                                                   0, true);
9119d9
+            def->cputune.emulatorpin = virDomainVcpuPinDefParseXML(nodes[0],
9119d9
+                                                                   ctxt, 0,
9119d9
+                                                                   true, false);
9119d9
 
9119d9
             if (!def->cputune.emulatorpin)
9119d9
                 goto error;
9119d9
@@ -12224,6 +12264,49 @@ virDomainDefParseXML(xmlDocPtr xml,
9119d9
     }
9119d9
     VIR_FREE(nodes);
9119d9
 
9119d9
+
9119d9
+    if ((n = virXPathNodeSet("./cputune/iothreadpin", ctxt, &nodes)) < 0) {
9119d9
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
9119d9
+                       _("cannot extract iothreadpin nodes"));
9119d9
+        goto error;
9119d9
+    }
9119d9
+
9119d9
+    /* Ignore iothreadpin if <vcpu> placement is "auto", they
9119d9
+     * conflict with each other, and <vcpu> placement can't be
9119d9
+     * simply ignored, as <numatune>'s placement defaults to it.
9119d9
+     */
9119d9
+    if (n) {
9119d9
+        if (def->placement_mode != VIR_DOMAIN_CPU_PLACEMENT_MODE_AUTO) {
9119d9
+            if (VIR_ALLOC_N(def->cputune.iothreadspin, n) < 0)
9119d9
+                goto error;
9119d9
+
9119d9
+            for (i = 0; i < n; i++) {
9119d9
+                virDomainVcpuPinDefPtr iothreadpin = NULL;
9119d9
+                iothreadpin = virDomainVcpuPinDefParseXML(nodes[i], ctxt,
9119d9
+                                                          def->iothreads,
9119d9
+                                                          false, true);
9119d9
+                if (!iothreadpin)
9119d9
+                    goto error;
9119d9
+
9119d9
+                if (virDomainVcpuPinIsDuplicate(def->cputune.iothreadspin,
9119d9
+                                                def->cputune.niothreadspin,
9119d9
+                                                iothreadpin->vcpuid)) {
9119d9
+                    virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
9119d9
+                                   _("duplicate iothreadpin for same iothread"));
9119d9
+                    virDomainVcpuPinDefFree(iothreadpin);
9119d9
+                    goto error;
9119d9
+                }
9119d9
+
9119d9
+                def->cputune.iothreadspin[def->cputune.niothreadspin++] =
9119d9
+                    iothreadpin;
9119d9
+            }
9119d9
+        } else {
9119d9
+            VIR_WARN("Ignore iothreadpin for <vcpu> placement is 'auto'");
9119d9
+        }
9119d9
+    }
9119d9
+    VIR_FREE(nodes);
9119d9
+
9119d9
+
9119d9
     /* analysis of cpu handling */
9119d9
     if ((node = virXPathNode("./cpu[1]", ctxt)) != NULL) {
9119d9
         xmlNodePtr oldnode = ctxt->node;
9119d9
@@ -17917,6 +18000,7 @@ virDomainDefFormatInternal(virDomainDefPtr def,
9119d9
     int n;
9119d9
     size_t i;
9119d9
     bool blkio = false;
9119d9
+    bool cputune = false;
9119d9
 
9119d9
     virCheckFlags(DUMPXML_FLAGS |
9119d9
                   VIR_DOMAIN_XML_INTERNAL_STATUS |
9119d9
@@ -18108,8 +18192,11 @@ virDomainDefFormatInternal(virDomainDefPtr def,
9119d9
         (def->cputune.nvcpupin && !virDomainIsAllVcpupinInherited(def)) ||
9119d9
         def->cputune.period || def->cputune.quota ||
9119d9
         def->cputune.emulatorpin ||
9119d9
-        def->cputune.emulator_period || def->cputune.emulator_quota)
9119d9
+        def->cputune.emulator_period || def->cputune.emulator_quota ||
9119d9
+        def->cputune.niothreadspin) {
9119d9
         virBufferAddLit(buf, "<cputune>\n");
9119d9
+        cputune = true;
9119d9
+    }
9119d9
 
9119d9
     virBufferAdjustIndent(buf, 2);
9119d9
     if (def->cputune.sharesSpecified)
9119d9
@@ -18160,12 +18247,27 @@ virDomainDefFormatInternal(virDomainDefPtr def,
9119d9
         virBufferAsprintf(buf, "cpuset='%s'/>\n", cpumask);
9119d9
         VIR_FREE(cpumask);
9119d9
     }
9119d9
+
9119d9
+    for (i = 0; i < def->cputune.niothreadspin; i++) {
9119d9
+        char *cpumask;
9119d9
+        /* Ignore the iothreadpin which inherit from "cpuset of "<vcpu>." */
9119d9
+        if (def->cpumask &&
9119d9
+            virBitmapEqual(def->cpumask,
9119d9
+                           def->cputune.iothreadspin[i]->cpumask))
9119d9
+            continue;
9119d9
+
9119d9
+        virBufferAsprintf(buf, "
9119d9
+                          def->cputune.iothreadspin[i]->vcpuid);
9119d9
+
9119d9
+        if (!(cpumask = virBitmapFormat(def->cputune.iothreadspin[i]->cpumask)))
9119d9
+            goto error;
9119d9
+
9119d9
+        virBufferAsprintf(buf, "cpuset='%s'/>\n", cpumask);
9119d9
+        VIR_FREE(cpumask);
9119d9
+    }
9119d9
+
9119d9
     virBufferAdjustIndent(buf, -2);
9119d9
-    if (def->cputune.sharesSpecified ||
9119d9
-        (def->cputune.nvcpupin && !virDomainIsAllVcpupinInherited(def)) ||
9119d9
-        def->cputune.period || def->cputune.quota ||
9119d9
-        def->cputune.emulatorpin ||
9119d9
-        def->cputune.emulator_period || def->cputune.emulator_quota)
9119d9
+    if (cputune)
9119d9
         virBufferAddLit(buf, "</cputune>\n");
9119d9
 
9119d9
     if (virDomainNumatuneFormatXML(buf, def->numatune) < 0)
9119d9
diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h
9119d9
index c93b73c..decd4be 100644
9119d9
--- a/src/conf/domain_conf.h
9119d9
+++ b/src/conf/domain_conf.h
9119d9
@@ -1953,6 +1953,8 @@ struct _virDomainDef {
9119d9
         size_t nvcpupin;
9119d9
         virDomainVcpuPinDefPtr *vcpupin;
9119d9
         virDomainVcpuPinDefPtr emulatorpin;
9119d9
+        size_t niothreadspin;
9119d9
+        virDomainVcpuPinDefPtr *iothreadspin;
9119d9
     } cputune;
9119d9
 
9119d9
     virDomainNumatunePtr numatune;
9119d9
diff --git a/tests/qemuxml2argvdata/qemuxml2argv-cputune-iothreads.xml b/tests/qemuxml2argvdata/qemuxml2argv-cputune-iothreads.xml
9119d9
new file mode 100644
9119d9
index 0000000..435d0ae
9119d9
--- /dev/null
9119d9
+++ b/tests/qemuxml2argvdata/qemuxml2argv-cputune-iothreads.xml
9119d9
@@ -0,0 +1,38 @@
9119d9
+<domain type='qemu'>
9119d9
+  <name>QEMUGuest1</name>
9119d9
+  <uuid>c7a5fdbd-edaf-9455-926a-d65c16db1809</uuid>
9119d9
+  <memory unit='KiB'>219136</memory>
9119d9
+  <currentMemory unit='KiB'>219136</currentMemory>
9119d9
+  <vcpu placement='static'>2</vcpu>
9119d9
+  <iothreads>2</iothreads>
9119d9
+  <cputune>
9119d9
+    <shares>2048</shares>
9119d9
+    <period>1000000</period>
9119d9
+    <quota>-1</quota>
9119d9
+    <vcpupin vcpu='0' cpuset='0'/>
9119d9
+    <vcpupin vcpu='1' cpuset='1'/>
9119d9
+    <emulatorpin cpuset='1'/>
9119d9
+    <iothreadpin iothread='1' cpuset='2'/>
9119d9
+    <iothreadpin iothread='2' cpuset='3'/>
9119d9
+  </cputune>
9119d9
+  <os>
9119d9
+    <type arch='i686' machine='pc'>hvm</type>
9119d9
+    <boot dev='hd'/>
9119d9
+  </os>
9119d9
+  <clock offset='utc'/>
9119d9
+  <on_poweroff>destroy</on_poweroff>
9119d9
+  <on_reboot>restart</on_reboot>
9119d9
+  <on_crash>destroy</on_crash>
9119d9
+  <devices>
9119d9
+    <emulator>/usr/bin/qemu</emulator>
9119d9
+    <disk type='block' device='disk'>
9119d9
+      <source dev='/dev/HostVG/QEMUGuest1'/>
9119d9
+      <target dev='hda' bus='ide'/>
9119d9
+      <address type='drive' controller='0' bus='0' target='0' unit='0'/>
9119d9
+    </disk>
9119d9
+    <controller type='usb' index='0'/>
9119d9
+    <controller type='ide' index='0'/>
9119d9
+    <controller type='pci' index='0' model='pci-root'/>
9119d9
+    <memballoon model='virtio'/>
9119d9
+  </devices>
9119d9
+</domain>
9119d9
diff --git a/tests/qemuxml2xmltest.c b/tests/qemuxml2xmltest.c
9119d9
index 1e06f38..843e66d 100644
9119d9
--- a/tests/qemuxml2xmltest.c
9119d9
+++ b/tests/qemuxml2xmltest.c
9119d9
@@ -303,6 +303,7 @@ mymain(void)
9119d9
 
9119d9
     DO_TEST("smp");
9119d9
     DO_TEST("iothreads");
9119d9
+    DO_TEST("cputune-iothreads");
9119d9
     DO_TEST("iothreads-disk");
9119d9
     DO_TEST("lease");
9119d9
     DO_TEST("event_idx");
9119d9
-- 
9119d9
2.1.0
9119d9