7548c0
From fe08906feb3ab006c4013957895cfb4fa69b7396 Mon Sep 17 00:00:00 2001
7548c0
Message-Id: <fe08906feb3ab006c4013957895cfb4fa69b7396@dist-git>
7548c0
From: Michal Privoznik <mprivozn@redhat.com>
7548c0
Date: Wed, 7 Oct 2020 18:45:43 +0200
7548c0
Subject: [PATCH] qemu: Build HMAT command line
7548c0
MIME-Version: 1.0
7548c0
Content-Type: text/plain; charset=UTF-8
7548c0
Content-Transfer-Encoding: 8bit
7548c0
7548c0
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1786303
7548c0
7548c0
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
7548c0
Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>
7548c0
(cherry picked from commit aeecbc87b7317e88a8ef8c82b29bcacd1005c8c2)
7548c0
7548c0
Apart from conflicts below, I had to remove '-cpu qemu64' from
7548c0
tests/qemuxml2argvdata/numatune-hmat.x86_64-latest.args to make
7548c0
qemuxml2argvtest happy. This is because
7548c0
3b8feb4793cef66f5dbfb9bdabe4d40834f1e90e isn't backported.
7548c0
7548c0
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1749518
7548c0
7548c0
Conflicts:
7548c0
- src/qemu/qemu_command.c: Context
7548c0
- src/qemu/qemu_validate.c: The file doesn't exist in downstream.
7548c0
I've made the change to validator that lives in qemu_domain.c.
7548c0
7548c0
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
7548c0
Message-Id: <0e8dfded8022b564ec7d0563cd745a0d3ffc815f.1602087923.git.mprivozn@redhat.com>
7548c0
Reviewed-by: Ján Tomko <jtomko@redhat.com>
7548c0
---
7548c0
 src/conf/numa_conf.c                          |   7 +
7548c0
 src/qemu/qemu_command.c                       | 171 ++++++++++++++++++
7548c0
 src/qemu/qemu_domain.c                        |   7 +
7548c0
 .../numatune-hmat.x86_64-latest.args          |  52 ++++++
7548c0
 tests/qemuxml2argvtest.c                      |   1 +
7548c0
 tests/qemuxml2xmltest.c                       |   2 +-
7548c0
 6 files changed, 239 insertions(+), 1 deletion(-)
7548c0
 create mode 100644 tests/qemuxml2argvdata/numatune-hmat.x86_64-latest.args
7548c0
7548c0
diff --git a/src/conf/numa_conf.c b/src/conf/numa_conf.c
7548c0
index 277a695d84..5a92eb35cc 100644
7548c0
--- a/src/conf/numa_conf.c
7548c0
+++ b/src/conf/numa_conf.c
7548c0
@@ -1875,6 +1875,13 @@ virDomainNumaGetNodeInitiator(const virDomainNuma *numa,
7548c0
     if (!numa || node >= numa->nmem_nodes)
7548c0
         return -1;
7548c0
 
7548c0
+    /* A NUMA node which has at least one vCPU is initiator to itself by
7548c0
+     * definition. */
7548c0
+    if (numa->mem_nodes[node].cpumask)
7548c0
+        return node;
7548c0
+
7548c0
+    /* For the rest, "NUMA node that has best performance (the lowest
7548c0
+     * latency or largest bandwidth) to this NUMA node." */
7548c0
     for (i = 0; i < numa->ninterconnects; i++) {
7548c0
         const virDomainNumaInterconnect *l = &numa->interconnects[i];
7548c0
 
7548c0
diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
7548c0
index ac63d18a42..959207bfea 100644
7548c0
--- a/src/qemu/qemu_command.c
7548c0
+++ b/src/qemu/qemu_command.c
7548c0
@@ -7172,6 +7172,9 @@ qemuBuildMachineCommandLine(virCommandPtr cmd,
7548c0
             virBufferAsprintf(&buf, ",pflash1=%s", priv->pflash1->nodeformat);
7548c0
     }
7548c0
 
7548c0
+    if (virDomainNumaHasHMAT(def->numa))
7548c0
+        virBufferAddLit(&buf, ",hmat=on");
7548c0
+
7548c0
     virCommandAddArgBuffer(cmd, &buf;;
7548c0
 
7548c0
     return 0;
7548c0
@@ -7355,6 +7358,134 @@ qemuBuildIOThreadCommandLine(virCommandPtr cmd,
7548c0
 }
7548c0
 
7548c0
 
7548c0
+static int
7548c0
+qemuBuilNumaCellCache(virCommandPtr cmd,
7548c0
+                      const virDomainDef *def,
7548c0
+                      size_t cell)
7548c0
+{
7548c0
+    size_t ncaches = virDomainNumaGetNodeCacheCount(def->numa, cell);
7548c0
+    size_t i;
7548c0
+
7548c0
+    if (ncaches == 0)
7548c0
+        return 0;
7548c0
+
7548c0
+    for (i = 0; i < ncaches; i++) {
7548c0
+        g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
7548c0
+        unsigned int level;
7548c0
+        unsigned int size;
7548c0
+        unsigned int line;
7548c0
+        virDomainCacheAssociativity associativity;
7548c0
+        virDomainCachePolicy policy;
7548c0
+
7548c0
+        if (virDomainNumaGetNodeCache(def->numa, cell, i,
7548c0
+                                      &level, &size, &line,
7548c0
+                                      &associativity, &policy) < 0) {
7548c0
+            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
7548c0
+                           _("Unable to format NUMA node cache"));
7548c0
+            return -1;
7548c0
+        }
7548c0
+
7548c0
+        virBufferAsprintf(&buf,
7548c0
+                          "hmat-cache,node-id=%zu,size=%uK,level=%u",
7548c0
+                          cell, size, level);
7548c0
+
7548c0
+        switch (associativity) {
7548c0
+        case VIR_DOMAIN_CACHE_ASSOCIATIVITY_NONE:
7548c0
+            virBufferAddLit(&buf, ",associativity=none");
7548c0
+            break;
7548c0
+        case VIR_DOMAIN_CACHE_ASSOCIATIVITY_DIRECT:
7548c0
+            virBufferAddLit(&buf, ",associativity=direct");
7548c0
+            break;
7548c0
+        case VIR_DOMAIN_CACHE_ASSOCIATIVITY_FULL:
7548c0
+            virBufferAddLit(&buf, ",associativity=complex");
7548c0
+            break;
7548c0
+        case VIR_DOMAIN_CACHE_ASSOCIATIVITY_LAST:
7548c0
+            break;
7548c0
+        }
7548c0
+
7548c0
+        switch (policy) {
7548c0
+        case VIR_DOMAIN_CACHE_POLICY_NONE:
7548c0
+            virBufferAddLit(&buf, ",policy=none");
7548c0
+            break;
7548c0
+        case VIR_DOMAIN_CACHE_POLICY_WRITEBACK:
7548c0
+            virBufferAddLit(&buf, ",policy=write-back");
7548c0
+            break;
7548c0
+        case VIR_DOMAIN_CACHE_POLICY_WRITETHROUGH:
7548c0
+            virBufferAddLit(&buf, ",policy=write-through");
7548c0
+            break;
7548c0
+        case VIR_DOMAIN_CACHE_POLICY_LAST:
7548c0
+            break;
7548c0
+        }
7548c0
+
7548c0
+        if (line > 0)
7548c0
+            virBufferAsprintf(&buf, ",line=%u", line);
7548c0
+
7548c0
+        virCommandAddArg(cmd, "-numa");
7548c0
+        virCommandAddArgBuffer(cmd, &buf;;
7548c0
+    }
7548c0
+
7548c0
+    return 0;
7548c0
+}
7548c0
+
7548c0
+
7548c0
+VIR_ENUM_DECL(qemuDomainMemoryHierarchy);
7548c0
+VIR_ENUM_IMPL(qemuDomainMemoryHierarchy,
7548c0
+              4, /* Maximum level of cache */
7548c0
+              "memory", /* Special case, whole memory not specific cache */
7548c0
+              "first-level",
7548c0
+              "second-level",
7548c0
+              "third-level");
7548c0
+
7548c0
+static int
7548c0
+qemuBuildNumaHMATCommandLine(virCommandPtr cmd,
7548c0
+                             const virDomainDef *def)
7548c0
+{
7548c0
+    size_t nlatencies;
7548c0
+    size_t i;
7548c0
+
7548c0
+    if (!def->numa)
7548c0
+        return 0;
7548c0
+
7548c0
+    nlatencies = virDomainNumaGetInterconnectsCount(def->numa);
7548c0
+    for (i = 0; i < nlatencies; i++) {
7548c0
+        g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
7548c0
+        virDomainNumaInterconnectType type;
7548c0
+        unsigned int initiator;
7548c0
+        unsigned int target;
7548c0
+        unsigned int cache;
7548c0
+        virDomainMemoryLatency accessType;
7548c0
+        unsigned long value;
7548c0
+        const char *hierarchyStr;
7548c0
+        const char *accessStr;
7548c0
+
7548c0
+        if (virDomainNumaGetInterconnect(def->numa, i,
7548c0
+                                         &type, &initiator, &target,
7548c0
+                                         &cache, &accessType, &value) < 0)
7548c0
+            return -1;
7548c0
+
7548c0
+        hierarchyStr = qemuDomainMemoryHierarchyTypeToString(cache);
7548c0
+        accessStr = virDomainMemoryLatencyTypeToString(accessType);
7548c0
+        virBufferAsprintf(&buf,
7548c0
+                          "hmat-lb,initiator=%u,target=%u,hierarchy=%s,data-type=%s-",
7548c0
+                          initiator, target, hierarchyStr, accessStr);
7548c0
+
7548c0
+        switch (type) {
7548c0
+        case VIR_DOMAIN_NUMA_INTERCONNECT_TYPE_LATENCY:
7548c0
+            virBufferAsprintf(&buf, "latency,latency=%lu", value);
7548c0
+            break;
7548c0
+        case VIR_DOMAIN_NUMA_INTERCONNECT_TYPE_BANDWIDTH:
7548c0
+            virBufferAsprintf(&buf, "bandwidth,bandwidth=%luK", value);
7548c0
+            break;
7548c0
+        }
7548c0
+
7548c0
+        virCommandAddArg(cmd, "-numa");
7548c0
+        virCommandAddArgBuffer(cmd, &buf;;
7548c0
+    }
7548c0
+
7548c0
+    return 0;
7548c0
+}
7548c0
+
7548c0
+
7548c0
 static int
7548c0
 qemuBuildNumaCommandLine(virQEMUDriverConfigPtr cfg,
7548c0
                          virDomainDefPtr def,
7548c0
@@ -7367,13 +7498,20 @@ qemuBuildNumaCommandLine(virQEMUDriverConfigPtr cfg,
7548c0
     char *next = NULL;
7548c0
     virBufferPtr nodeBackends = NULL;
7548c0
     bool needBackend = false;
7548c0
+    bool hmat = false;
7548c0
     int rc;
7548c0
     int ret = -1;
7548c0
     size_t ncells = virDomainNumaGetNodeCount(def->numa);
7548c0
+    ssize_t masterInitiator = -1;
7548c0
 
7548c0
     if (!virDomainNumatuneNodesetIsAvailable(def->numa, priv->autoNodeset))
7548c0
         goto cleanup;
7548c0
 
7548c0
+    if (virDomainNumaHasHMAT(def->numa)) {
7548c0
+        needBackend = true;
7548c0
+        hmat = true;
7548c0
+    }
7548c0
+
7548c0
     if (VIR_ALLOC_N(nodeBackends, ncells) < 0)
7548c0
         goto cleanup;
7548c0
 
7548c0
@@ -7397,8 +7535,22 @@ qemuBuildNumaCommandLine(virQEMUDriverConfigPtr cfg,
7548c0
         qemuBuildMemPathStr(cfg, def, cmd, priv) < 0)
7548c0
         goto cleanup;
7548c0
 
7548c0
+    for (i = 0; i < ncells; i++) {
7548c0
+        if (virDomainNumaGetNodeCpumask(def->numa, i)) {
7548c0
+            masterInitiator = i;
7548c0
+            break;
7548c0
+        }
7548c0
+    }
7548c0
+
7548c0
+    if (masterInitiator) {
7548c0
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
7548c0
+                       _("At least one NUMA node has to have CPUs"));
7548c0
+        goto cleanup;
7548c0
+    }
7548c0
+
7548c0
     for (i = 0; i < ncells; i++) {
7548c0
         virBitmapPtr cpumask = virDomainNumaGetNodeCpumask(def->numa, i);
7548c0
+        ssize_t initiator = virDomainNumaGetNodeInitiator(def->numa, i);
7548c0
 
7548c0
         if (needBackend) {
7548c0
             virCommandAddArg(cmd, "-object");
7548c0
@@ -7423,6 +7575,13 @@ qemuBuildNumaCommandLine(virQEMUDriverConfigPtr cfg,
7548c0
             }
7548c0
         }
7548c0
 
7548c0
+        if (hmat) {
7548c0
+            if (initiator < 0)
7548c0
+                initiator = masterInitiator;
7548c0
+
7548c0
+            virBufferAsprintf(&buf, ",initiator=%zd", initiator);
7548c0
+        }
7548c0
+
7548c0
         if (needBackend)
7548c0
             virBufferAsprintf(&buf, ",memdev=ram-node%zu", i);
7548c0
         else
7548c0
@@ -7448,6 +7607,18 @@ qemuBuildNumaCommandLine(virQEMUDriverConfigPtr cfg,
7548c0
         }
7548c0
     }
7548c0
 
7548c0
+    if (hmat) {
7548c0
+        if (qemuBuildNumaHMATCommandLine(cmd, def) < 0)
7548c0
+            goto cleanup;
7548c0
+
7548c0
+        /* This can't be moved into any of the loops above,
7548c0
+         * because hmat-cache can be specified only after hmat-lb. */
7548c0
+        for (i = 0; i < ncells; i++) {
7548c0
+            if (qemuBuilNumaCellCache(cmd, def, i) < 0)
7548c0
+                goto cleanup;
7548c0
+        }
7548c0
+    }
7548c0
+
7548c0
     ret = 0;
7548c0
 
7548c0
  cleanup:
7548c0
diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c
7548c0
index be25790f12..e51e176a80 100644
7548c0
--- a/src/qemu/qemu_domain.c
7548c0
+++ b/src/qemu/qemu_domain.c
7548c0
@@ -5904,6 +5904,13 @@ qemuDomainDefValidate(const virDomainDef *def,
7548c0
         }
7548c0
     }
7548c0
 
7548c0
+    if (virDomainNumaHasHMAT(def->numa) &&
7548c0
+        !virQEMUCapsGet(qemuCaps, QEMU_CAPS_NUMA_HMAT)) {
7548c0
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
7548c0
+                       _("HMAT is not supported with this QEMU"));
7548c0
+        return -1;
7548c0
+    }
7548c0
+
7548c0
     if (def->genidRequested &&
7548c0
         !virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE_VMGENID)) {
7548c0
         virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
7548c0
diff --git a/tests/qemuxml2argvdata/numatune-hmat.x86_64-latest.args b/tests/qemuxml2argvdata/numatune-hmat.x86_64-latest.args
7548c0
new file mode 100644
7548c0
index 0000000000..413d247a4d
7548c0
--- /dev/null
7548c0
+++ b/tests/qemuxml2argvdata/numatune-hmat.x86_64-latest.args
7548c0
@@ -0,0 +1,52 @@
7548c0
+LC_ALL=C \
7548c0
+PATH=/bin \
7548c0
+HOME=/tmp/lib/domain--1-QEMUGuest \
7548c0
+USER=test \
7548c0
+LOGNAME=test \
7548c0
+XDG_DATA_HOME=/tmp/lib/domain--1-QEMUGuest/.local/share \
7548c0
+XDG_CACHE_HOME=/tmp/lib/domain--1-QEMUGuest/.cache \
7548c0
+XDG_CONFIG_HOME=/tmp/lib/domain--1-QEMUGuest/.config \
7548c0
+QEMU_AUDIO_DRV=none \
7548c0
+/usr/bin/qemu-system-x86_64 \
7548c0
+-name guest=QEMUGuest,debug-threads=on \
7548c0
+-S \
7548c0
+-object secret,id=masterKey0,format=raw,\
7548c0
+file=/tmp/lib/domain--1-QEMUGuest/master-key.aes \
7548c0
+-machine pc,accel=tcg,usb=off,dump-guest-core=off,hmat=on \
7548c0
+-m 12288 \
7548c0
+-overcommit mem-lock=off \
7548c0
+-smp 12,sockets=12,cores=1,threads=1 \
7548c0
+-object memory-backend-ram,id=ram-node0,size=2147483648 \
7548c0
+-numa node,nodeid=0,cpus=0-3,initiator=0,memdev=ram-node0 \
7548c0
+-object memory-backend-ram,id=ram-node1,size=2147483648 \
7548c0
+-numa node,nodeid=1,cpus=4-7,initiator=1,memdev=ram-node1 \
7548c0
+-object memory-backend-ram,id=ram-node2,size=2147483648 \
7548c0
+-numa node,nodeid=2,cpus=8-11,initiator=2,memdev=ram-node2 \
7548c0
+-object memory-backend-ram,id=ram-node3,size=2147483648 \
7548c0
+-numa node,nodeid=3,initiator=0,memdev=ram-node3 \
7548c0
+-object memory-backend-ram,id=ram-node4,size=2147483648 \
7548c0
+-numa node,nodeid=4,initiator=0,memdev=ram-node4 \
7548c0
+-object memory-backend-ram,id=ram-node5,size=2147483648 \
7548c0
+-numa node,nodeid=5,initiator=0,memdev=ram-node5 \
7548c0
+-numa hmat-lb,initiator=0,target=0,hierarchy=memory,data-type=access-latency,\
7548c0
+latency=5 \
7548c0
+-numa hmat-lb,initiator=0,target=0,hierarchy=first-level,\
7548c0
+data-type=access-latency,latency=10 \
7548c0
+-numa hmat-lb,initiator=0,target=0,hierarchy=memory,data-type=access-bandwidth,\
7548c0
+bandwidth=204800K \
7548c0
+-numa hmat-cache,node-id=0,size=10K,level=1,associativity=direct,\
7548c0
+policy=write-back,line=8 \
7548c0
+-uuid c7a5fdb2-cdaf-9455-926a-d65c16db1809 \
7548c0
+-display none \
7548c0
+-no-user-config \
7548c0
+-nodefaults \
7548c0
+-chardev socket,id=charmonitor,fd=1729,server,nowait \
7548c0
+-mon chardev=charmonitor,id=monitor,mode=control \
7548c0
+-rtc base=utc \
7548c0
+-no-shutdown \
7548c0
+-boot strict=on \
7548c0
+-device piix3-usb-uhci,id=usb,bus=pci.0,addr=0x1.0x2 \
7548c0
+-device virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x3 \
7548c0
+-sandbox on,obsolete=deny,elevateprivileges=deny,spawn=deny,\
7548c0
+resourcecontrol=deny \
7548c0
+-msg timestamp=on
7548c0
diff --git a/tests/qemuxml2argvtest.c b/tests/qemuxml2argvtest.c
7548c0
index 49699e495d..629f5ac100 100644
7548c0
--- a/tests/qemuxml2argvtest.c
7548c0
+++ b/tests/qemuxml2argvtest.c
7548c0
@@ -1813,6 +1813,7 @@ mymain(void)
7548c0
 
7548c0
     DO_TEST("numatune-distances", QEMU_CAPS_NUMA, QEMU_CAPS_NUMA_DIST);
7548c0
     DO_TEST("numatune-no-vcpu", NONE);
7548c0
+    DO_TEST_CAPS_LATEST("numatune-hmat");
7548c0
 
7548c0
     DO_TEST("numatune-auto-nodeset-invalid", NONE);
7548c0
     DO_TEST("numatune-auto-prefer", QEMU_CAPS_OBJECT_MEMORY_RAM,
7548c0
diff --git a/tests/qemuxml2xmltest.c b/tests/qemuxml2xmltest.c
7548c0
index de1d720e1d..f790bbc6f1 100644
7548c0
--- a/tests/qemuxml2xmltest.c
7548c0
+++ b/tests/qemuxml2xmltest.c
7548c0
@@ -1106,7 +1106,7 @@ mymain(void)
7548c0
     DO_TEST("numatune-memnode-no-memory", QEMU_CAPS_OBJECT_MEMORY_FILE);
7548c0
     DO_TEST("numatune-distances", QEMU_CAPS_NUMA, QEMU_CAPS_NUMA_DIST);
7548c0
     DO_TEST("numatune-no-vcpu", QEMU_CAPS_NUMA);
7548c0
-    DO_TEST("numatune-hmat", NONE);
7548c0
+    DO_TEST("numatune-hmat", QEMU_CAPS_NUMA_HMAT);
7548c0
 
7548c0
     DO_TEST("bios-nvram", NONE);
7548c0
     DO_TEST("bios-nvram-os-interleave", NONE);
7548c0
-- 
7548c0
2.29.2
7548c0