render / rpms / libvirt

Forked from rpms/libvirt 9 months ago
Clone
99cbc7
From a7292e2be3fef0c14d5393120471b1dc5d7a5adb Mon Sep 17 00:00:00 2001
99cbc7
Message-Id: <a7292e2be3fef0c14d5393120471b1dc5d7a5adb@dist-git>
99cbc7
From: Bing Niu <bing.niu@intel.com>
99cbc7
Date: Mon, 15 Apr 2019 17:33:00 +0200
99cbc7
Subject: [PATCH] conf: Add memory bandwidth allocation capability of host
99cbc7
MIME-Version: 1.0
99cbc7
Content-Type: text/plain; charset=UTF-8
99cbc7
Content-Transfer-Encoding: 8bit
99cbc7
99cbc7
Add new XML section to report host's memory bandwidth allocation
99cbc7
capability. The format as below example:
99cbc7
99cbc7
 <host>
99cbc7
 .....
99cbc7
   <memory_bandwidth>
99cbc7
     <node id='0' cpus='0-19'>
99cbc7
       <control granularity='10' min ='10' maxAllocs='8'/>
99cbc7
     </node>
99cbc7
   </memory_bandwidth>
99cbc7
</host>
99cbc7
99cbc7
granularity   ---- granularity of memory bandwidth, unit percentage.
99cbc7
min           ---- minimum memory bandwidth allowed, unit percentage.
99cbc7
maxAllocs     ---- maximum memory bandwidth allocation group supported.
99cbc7
99cbc7
Signed-off-by: Bing Niu <bing.niu@intel.com>
99cbc7
Reviewed-by: John Ferlan <jferlan@redhat.com>
99cbc7
(cherry picked from commit 7995fecc25c6bc8d0ebb243ea2c8765b076c8974)
99cbc7
99cbc7
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1468650
99cbc7
99cbc7
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
99cbc7
Message-Id: <eda433c39cf6e5462b3076fa3049ac5a042af0c1.1555342313.git.phrdina@redhat.com>
99cbc7
Reviewed-by: Ján Tomko <jtomko@redhat.com>
99cbc7
---
99cbc7
 docs/schemas/capability.rng                   |  33 ++++++
99cbc7
 src/conf/capabilities.c                       | 107 ++++++++++++++++++
99cbc7
 src/conf/capabilities.h                       |  11 ++
99cbc7
 src/util/virresctrl.c                         |  20 ++++
99cbc7
 src/util/virresctrl.h                         |  15 +++
99cbc7
 .../resctrl/info/MB/bandwidth_gran            |   1 +
99cbc7
 .../resctrl/info/MB/min_bandwidth             |   1 +
99cbc7
 .../linux-resctrl/resctrl/info/MB/num_closids |   1 +
99cbc7
 .../vircaps-x86_64-resctrl.xml                |   8 ++
99cbc7
 tests/virresctrldata/resctrl.schemata         |   1 +
99cbc7
 10 files changed, 198 insertions(+)
99cbc7
 create mode 100644 tests/vircaps2xmldata/linux-resctrl/resctrl/info/MB/bandwidth_gran
99cbc7
 create mode 100644 tests/vircaps2xmldata/linux-resctrl/resctrl/info/MB/min_bandwidth
99cbc7
 create mode 100644 tests/vircaps2xmldata/linux-resctrl/resctrl/info/MB/num_closids
99cbc7
99cbc7
diff --git a/docs/schemas/capability.rng b/docs/schemas/capability.rng
99cbc7
index 52164d5ecb..d61515ccbe 100644
99cbc7
--- a/docs/schemas/capability.rng
99cbc7
+++ b/docs/schemas/capability.rng
99cbc7
@@ -51,6 +51,9 @@
99cbc7
       <optional>
99cbc7
         <ref name='cache'/>
99cbc7
       </optional>
99cbc7
+      <optional>
99cbc7
+        <ref name='memory_bandwidth'/>
99cbc7
+      </optional>
99cbc7
       <zeroOrMore>
99cbc7
         <ref name='secmodel'/>
99cbc7
       </zeroOrMore>
99cbc7
@@ -326,6 +329,36 @@
99cbc7
     </attribute>
99cbc7
   </define>
99cbc7
 
99cbc7
+  <define name='memory_bandwidth'>
99cbc7
+    <element name='memory_bandwidth'>
99cbc7
+      <oneOrMore>
99cbc7
+        <element name='node'>
99cbc7
+          <attribute name='id'>
99cbc7
+            <ref name='unsignedInt'/>
99cbc7
+          </attribute>
99cbc7
+          <attribute name='cpus'>
99cbc7
+            <ref name='cpuset'/>
99cbc7
+          </attribute>
99cbc7
+          <zeroOrMore>
99cbc7
+            <element name='control'>
99cbc7
+              <attribute name='granularity'>
99cbc7
+                <ref name='unsignedInt'/>
99cbc7
+              </attribute>
99cbc7
+            <optional>
99cbc7
+              <attribute name='min'>
99cbc7
+                <ref name='unsignedInt'/>
99cbc7
+            </attribute>
99cbc7
+            </optional>
99cbc7
+              <attribute name='maxAllocs'>
99cbc7
+                <ref name='unsignedInt'/>
99cbc7
+              </attribute>
99cbc7
+            </element>
99cbc7
+          </zeroOrMore>
99cbc7
+        </element>
99cbc7
+      </oneOrMore>
99cbc7
+    </element>
99cbc7
+  </define>
99cbc7
+
99cbc7
   <define name='guestcaps'>
99cbc7
     <element name='guest'>
99cbc7
       <ref name='ostype'/>
99cbc7
diff --git a/src/conf/capabilities.c b/src/conf/capabilities.c
99cbc7
index 7a810efa66..3d893447cb 100644
99cbc7
--- a/src/conf/capabilities.c
99cbc7
+++ b/src/conf/capabilities.c
99cbc7
@@ -197,6 +197,16 @@ virCapabilitiesFreeNUMAInfo(virCapsPtr caps)
99cbc7
     caps->host.nnumaCell = 0;
99cbc7
 }
99cbc7
 
99cbc7
+static void
99cbc7
+virCapsHostMemBWNodeFree(virCapsHostMemBWNodePtr ptr)
99cbc7
+{
99cbc7
+    if (!ptr)
99cbc7
+        return;
99cbc7
+
99cbc7
+    virBitmapFree(ptr->cpus);
99cbc7
+    VIR_FREE(ptr);
99cbc7
+}
99cbc7
+
99cbc7
 static void
99cbc7
 virCapabilitiesClearSecModel(virCapsHostSecModelPtr secmodel)
99cbc7
 {
99cbc7
@@ -239,6 +249,10 @@ virCapsDispose(void *object)
99cbc7
         virCapsHostCacheBankFree(caps->host.caches[i]);
99cbc7
     VIR_FREE(caps->host.caches);
99cbc7
 
99cbc7
+    for (i = 0; i < caps->host.nnodes; i++)
99cbc7
+        virCapsHostMemBWNodeFree(caps->host.nodes[i]);
99cbc7
+    VIR_FREE(caps->host.nodes);
99cbc7
+
99cbc7
     VIR_FREE(caps->host.netprefix);
99cbc7
     VIR_FREE(caps->host.pagesSize);
99cbc7
     virCPUDefFree(caps->host.cpu);
99cbc7
@@ -957,6 +971,58 @@ virCapabilitiesFormatCaches(virBufferPtr buf,
99cbc7
     return 0;
99cbc7
 }
99cbc7
 
99cbc7
+static int
99cbc7
+virCapabilitiesFormatMemoryBandwidth(virBufferPtr buf,
99cbc7
+                                     size_t nnodes,
99cbc7
+                                     virCapsHostMemBWNodePtr *nodes)
99cbc7
+{
99cbc7
+    size_t i = 0;
99cbc7
+    virBuffer controlBuf = VIR_BUFFER_INITIALIZER;
99cbc7
+
99cbc7
+    if (!nnodes)
99cbc7
+        return 0;
99cbc7
+
99cbc7
+    virBufferAddLit(buf, "<memory_bandwidth>\n");
99cbc7
+    virBufferAdjustIndent(buf, 2);
99cbc7
+
99cbc7
+    for (i = 0; i < nnodes; i++) {
99cbc7
+        virCapsHostMemBWNodePtr node = nodes[i];
99cbc7
+        virResctrlInfoMemBWPerNodePtr control = &node->control;
99cbc7
+        char *cpus_str = virBitmapFormat(node->cpus);
99cbc7
+
99cbc7
+        if (!cpus_str)
99cbc7
+            return -1;
99cbc7
+
99cbc7
+        virBufferAsprintf(buf,
99cbc7
+                          "
99cbc7
+                          node->id, cpus_str);
99cbc7
+        VIR_FREE(cpus_str);
99cbc7
+
99cbc7
+        virBufferSetChildIndent(&controlBuf, buf);
99cbc7
+        virBufferAsprintf(&controlBuf,
99cbc7
+                          "
99cbc7
+                          "maxAllocs='%u'/>\n",
99cbc7
+                          control->granularity, control->min,
99cbc7
+                          control->max_allocation);
99cbc7
+
99cbc7
+        if (virBufferCheckError(&controlBuf) < 0)
99cbc7
+            return -1;
99cbc7
+
99cbc7
+        if (virBufferUse(&controlBuf)) {
99cbc7
+            virBufferAddLit(buf, ">\n");
99cbc7
+            virBufferAddBuffer(buf, &controlBuf);
99cbc7
+            virBufferAddLit(buf, "</node>\n");
99cbc7
+        } else {
99cbc7
+            virBufferAddLit(buf, "/>\n");
99cbc7
+        }
99cbc7
+    }
99cbc7
+
99cbc7
+    virBufferAdjustIndent(buf, -2);
99cbc7
+    virBufferAddLit(buf, "</memory_bandwidth>\n");
99cbc7
+
99cbc7
+    return 0;
99cbc7
+}
99cbc7
+
99cbc7
 /**
99cbc7
  * virCapabilitiesFormatXML:
99cbc7
  * @caps: capabilities to format
99cbc7
@@ -1060,6 +1126,10 @@ virCapabilitiesFormatXML(virCapsPtr caps)
99cbc7
                                     caps->host.caches) < 0)
99cbc7
         goto error;
99cbc7
 
99cbc7
+    if (virCapabilitiesFormatMemoryBandwidth(&buf, caps->host.nnodes,
99cbc7
+                                             caps->host.nodes) < 0)
99cbc7
+        goto error;
99cbc7
+
99cbc7
     for (i = 0; i < caps->host.nsecModels; i++) {
99cbc7
         virBufferAddLit(&buf, "<secmodel>\n");
99cbc7
         virBufferAdjustIndent(&buf, 2);
99cbc7
@@ -1602,6 +1672,40 @@ virCapabilitiesInitResctrl(virCapsPtr caps)
99cbc7
 }
99cbc7
 
99cbc7
 
99cbc7
+static int
99cbc7
+virCapabilitiesInitResctrlMemory(virCapsPtr caps)
99cbc7
+{
99cbc7
+    virCapsHostMemBWNodePtr node = NULL;
99cbc7
+    size_t i = 0;
99cbc7
+    int ret = -1;
99cbc7
+
99cbc7
+    for (i = 0; i < caps->host.ncaches; i++) {
99cbc7
+        virCapsHostCacheBankPtr bank = caps->host.caches[i];
99cbc7
+        if (VIR_ALLOC(node) < 0)
99cbc7
+            goto cleanup;
99cbc7
+
99cbc7
+        if (virResctrlInfoGetMemoryBandwidth(caps->host.resctrl,
99cbc7
+                                             bank->level, &node->control) > 0) {
99cbc7
+            node->id = bank->id;
99cbc7
+            if (!(node->cpus = virBitmapNewCopy(bank->cpus)))
99cbc7
+                goto cleanup;
99cbc7
+
99cbc7
+            if (VIR_APPEND_ELEMENT(caps->host.nodes,
99cbc7
+                                   caps->host.nnodes, node) < 0) {
99cbc7
+                goto cleanup;
99cbc7
+            }
99cbc7
+        }
99cbc7
+        virCapsHostMemBWNodeFree(node);
99cbc7
+        node = NULL;
99cbc7
+    }
99cbc7
+
99cbc7
+    ret = 0;
99cbc7
+ cleanup:
99cbc7
+    virCapsHostMemBWNodeFree(node);
99cbc7
+    return ret;
99cbc7
+}
99cbc7
+
99cbc7
+
99cbc7
 int
99cbc7
 virCapabilitiesInitCaches(virCapsPtr caps)
99cbc7
 {
99cbc7
@@ -1731,6 +1835,9 @@ virCapabilitiesInitCaches(virCapsPtr caps)
99cbc7
     qsort(caps->host.caches, caps->host.ncaches,
99cbc7
           sizeof(*caps->host.caches), virCapsHostCacheBankSorter);
99cbc7
 
99cbc7
+    if (virCapabilitiesInitResctrlMemory(caps) < 0)
99cbc7
+        goto cleanup;
99cbc7
+
99cbc7
     ret = 0;
99cbc7
  cleanup:
99cbc7
     VIR_FREE(type);
99cbc7
diff --git a/src/conf/capabilities.h b/src/conf/capabilities.h
99cbc7
index fe1b9ea455..046e275ac6 100644
99cbc7
--- a/src/conf/capabilities.h
99cbc7
+++ b/src/conf/capabilities.h
99cbc7
@@ -151,6 +151,14 @@ struct _virCapsHostCacheBank {
99cbc7
     virResctrlInfoPerCachePtr *controls;
99cbc7
 };
99cbc7
 
99cbc7
+typedef struct _virCapsHostMemBWNode virCapsHostMemBWNode;
99cbc7
+typedef virCapsHostMemBWNode *virCapsHostMemBWNodePtr;
99cbc7
+struct _virCapsHostMemBWNode {
99cbc7
+    unsigned int id;
99cbc7
+    virBitmapPtr cpus;  /* All CPUs that belong to this node*/
99cbc7
+    virResctrlInfoMemBWPerNode control;
99cbc7
+};
99cbc7
+
99cbc7
 typedef struct _virCapsHost virCapsHost;
99cbc7
 typedef virCapsHost *virCapsHostPtr;
99cbc7
 struct _virCapsHost {
99cbc7
@@ -175,6 +183,9 @@ struct _virCapsHost {
99cbc7
     size_t ncaches;
99cbc7
     virCapsHostCacheBankPtr *caches;
99cbc7
 
99cbc7
+    size_t nnodes;
99cbc7
+    virCapsHostMemBWNodePtr *nodes;
99cbc7
+
99cbc7
     size_t nsecModels;
99cbc7
     virCapsHostSecModelPtr secModels;
99cbc7
 
99cbc7
diff --git a/src/util/virresctrl.c b/src/util/virresctrl.c
99cbc7
index adf36a7c0a..4b5442f879 100644
99cbc7
--- a/src/util/virresctrl.c
99cbc7
+++ b/src/util/virresctrl.c
99cbc7
@@ -629,6 +629,26 @@ virResctrlInfoIsEmpty(virResctrlInfoPtr resctrl)
99cbc7
 }
99cbc7
 
99cbc7
 
99cbc7
+int
99cbc7
+virResctrlInfoGetMemoryBandwidth(virResctrlInfoPtr resctrl,
99cbc7
+                                 unsigned int level,
99cbc7
+                                 virResctrlInfoMemBWPerNodePtr control)
99cbc7
+{
99cbc7
+    virResctrlInfoMemBWPtr membw_info = resctrl->membw_info;
99cbc7
+
99cbc7
+    if (!membw_info)
99cbc7
+        return 0;
99cbc7
+
99cbc7
+    if (membw_info->last_level_cache != level)
99cbc7
+        return 0;
99cbc7
+
99cbc7
+    control->granularity = membw_info->bandwidth_granularity;
99cbc7
+    control->min = membw_info->min_bandwidth;
99cbc7
+    control->max_allocation = membw_info->max_allocation;
99cbc7
+    return 1;
99cbc7
+}
99cbc7
+
99cbc7
+
99cbc7
 int
99cbc7
 virResctrlInfoGetCache(virResctrlInfoPtr resctrl,
99cbc7
                        unsigned int level,
99cbc7
diff --git a/src/util/virresctrl.h b/src/util/virresctrl.h
99cbc7
index 8d62517aa1..cfd56ddd06 100644
99cbc7
--- a/src/util/virresctrl.h
99cbc7
+++ b/src/util/virresctrl.h
99cbc7
@@ -50,6 +50,17 @@ struct _virResctrlInfoPerCache {
99cbc7
     unsigned int max_allocation;
99cbc7
 };
99cbc7
 
99cbc7
+typedef struct _virResctrlInfoMemBWPerNode virResctrlInfoMemBWPerNode;
99cbc7
+typedef virResctrlInfoMemBWPerNode *virResctrlInfoMemBWPerNodePtr;
99cbc7
+struct _virResctrlInfoMemBWPerNode {
99cbc7
+    /* Smallest possible increase of the allocation bandwidth in percentage */
99cbc7
+    unsigned int granularity;
99cbc7
+    /* Minimal allocatable bandwidth in percentage */
99cbc7
+    unsigned int min;
99cbc7
+    /* Maximum number of simultaneous allocations */
99cbc7
+    unsigned int max_allocation;
99cbc7
+};
99cbc7
+
99cbc7
 typedef struct _virResctrlInfo virResctrlInfo;
99cbc7
 typedef virResctrlInfo *virResctrlInfoPtr;
99cbc7
 
99cbc7
@@ -63,6 +74,10 @@ virResctrlInfoGetCache(virResctrlInfoPtr resctrl,
99cbc7
                        size_t *ncontrols,
99cbc7
                        virResctrlInfoPerCachePtr **controls);
99cbc7
 
99cbc7
+int
99cbc7
+virResctrlInfoGetMemoryBandwidth(virResctrlInfoPtr resctrl,
99cbc7
+                                 unsigned int level,
99cbc7
+                                 virResctrlInfoMemBWPerNodePtr control);
99cbc7
 /* Alloc-related things */
99cbc7
 typedef struct _virResctrlAlloc virResctrlAlloc;
99cbc7
 typedef virResctrlAlloc *virResctrlAllocPtr;
99cbc7
diff --git a/tests/vircaps2xmldata/linux-resctrl/resctrl/info/MB/bandwidth_gran b/tests/vircaps2xmldata/linux-resctrl/resctrl/info/MB/bandwidth_gran
99cbc7
new file mode 100644
99cbc7
index 0000000000..f599e28b8a
99cbc7
--- /dev/null
99cbc7
+++ b/tests/vircaps2xmldata/linux-resctrl/resctrl/info/MB/bandwidth_gran
99cbc7
@@ -0,0 +1 @@
99cbc7
+10
99cbc7
diff --git a/tests/vircaps2xmldata/linux-resctrl/resctrl/info/MB/min_bandwidth b/tests/vircaps2xmldata/linux-resctrl/resctrl/info/MB/min_bandwidth
99cbc7
new file mode 100644
99cbc7
index 0000000000..f599e28b8a
99cbc7
--- /dev/null
99cbc7
+++ b/tests/vircaps2xmldata/linux-resctrl/resctrl/info/MB/min_bandwidth
99cbc7
@@ -0,0 +1 @@
99cbc7
+10
99cbc7
diff --git a/tests/vircaps2xmldata/linux-resctrl/resctrl/info/MB/num_closids b/tests/vircaps2xmldata/linux-resctrl/resctrl/info/MB/num_closids
99cbc7
new file mode 100644
99cbc7
index 0000000000..b8626c4cff
99cbc7
--- /dev/null
99cbc7
+++ b/tests/vircaps2xmldata/linux-resctrl/resctrl/info/MB/num_closids
99cbc7
@@ -0,0 +1 @@
99cbc7
+4
99cbc7
diff --git a/tests/vircaps2xmldata/vircaps-x86_64-resctrl.xml b/tests/vircaps2xmldata/vircaps-x86_64-resctrl.xml
99cbc7
index 4840614e86..9b00cf0995 100644
99cbc7
--- a/tests/vircaps2xmldata/vircaps-x86_64-resctrl.xml
99cbc7
+++ b/tests/vircaps2xmldata/vircaps-x86_64-resctrl.xml
99cbc7
@@ -49,6 +49,14 @@
99cbc7
         <control granularity='768' min='1536' unit='KiB' type='both' maxAllocs='4'/>
99cbc7
       </bank>
99cbc7
     </cache>
99cbc7
+    <memory_bandwidth>
99cbc7
+      <node id='0' cpus='0-5'>
99cbc7
+        <control granularity='10' min ='10' maxAllocs='4'/>
99cbc7
+      </node>
99cbc7
+      <node id='1' cpus='6-11'>
99cbc7
+        <control granularity='10' min ='10' maxAllocs='4'/>
99cbc7
+      </node>
99cbc7
+    </memory_bandwidth>
99cbc7
   </host>
99cbc7
 
99cbc7
 </capabilities>
99cbc7
diff --git a/tests/virresctrldata/resctrl.schemata b/tests/virresctrldata/resctrl.schemata
99cbc7
index fa980e58c9..2578822b70 100644
99cbc7
--- a/tests/virresctrldata/resctrl.schemata
99cbc7
+++ b/tests/virresctrldata/resctrl.schemata
99cbc7
@@ -1 +1,2 @@
99cbc7
 L3:0=000ff;1=000f0
99cbc7
+MB:0=100;1=100
99cbc7
-- 
99cbc7
2.21.0
99cbc7