404507
From ba1ce05bd6e49217cd8c89b75e741e518d2b9292 Mon Sep 17 00:00:00 2001
404507
Message-Id: <ba1ce05bd6e49217cd8c89b75e741e518d2b9292@dist-git>
404507
From: Martin Kletzander <mkletzan@redhat.com>
404507
Date: Wed, 31 Jan 2018 16:32:30 +0100
404507
Subject: [PATCH] conf: Add support for cputune/cachetune
404507
404507
More info in the documentation, this is basically the XML parsing/formatting
404507
support, schemas, tests and documentation for the new cputune/cachetune element
404507
that will get used by following patches.
404507
404507
Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
404507
(cherry picked from commit 7387e3fea44e28118e1f72841d79503849e15985)
404507
Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
404507
404507
https://bugzilla.redhat.com/show_bug.cgi?id=1289368
404507
404507
Downstream changes:
404507
404507
- Rename test XML files due to ab7a2fe230bb742cc4bc7f8d1475201b97fe49ab
404507
404507
Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
404507
---
404507
 docs/formatdomain.html.in                          |  54 ++++
404507
 docs/schemas/domaincommon.rng                      |  32 +++
404507
 src/conf/domain_conf.c                             | 295 ++++++++++++++++++++-
404507
 src/conf/domain_conf.h                             |  13 +
404507
 .../genericxml2xmlindata/generic-cachetune-cdp.xml |  36 +++
404507
 .../generic-cachetune-colliding-allocs.xml         |  30 +++
404507
 .../generic-cachetune-colliding-tunes.xml          |  32 +++
404507
 .../generic-cachetune-colliding-types.xml          |  30 +++
404507
 .../generic-cachetune-small.xml                    |  29 ++
404507
 tests/genericxml2xmlindata/generic-cachetune.xml   |  33 +++
404507
 tests/genericxml2xmltest.c                         |  10 +
404507
 11 files changed, 592 insertions(+), 2 deletions(-)
404507
 create mode 100644 tests/genericxml2xmlindata/generic-cachetune-cdp.xml
404507
 create mode 100644 tests/genericxml2xmlindata/generic-cachetune-colliding-allocs.xml
404507
 create mode 100644 tests/genericxml2xmlindata/generic-cachetune-colliding-tunes.xml
404507
 create mode 100644 tests/genericxml2xmlindata/generic-cachetune-colliding-types.xml
404507
 create mode 100644 tests/genericxml2xmlindata/generic-cachetune-small.xml
404507
 create mode 100644 tests/genericxml2xmlindata/generic-cachetune.xml
404507
404507
diff --git a/docs/formatdomain.html.in b/docs/formatdomain.html.in
404507
index 0cfbf051b7..f33261d05c 100644
404507
--- a/docs/formatdomain.html.in
404507
+++ b/docs/formatdomain.html.in
404507
@@ -689,6 +689,10 @@
404507
     <iothread_quota>-1</iothread_quota>
404507
     <vcpusched vcpus='0-4,^3' scheduler='fifo' priority='1'/>
404507
     <iothreadsched iothreads='2' scheduler='batch'/>
404507
+    <cachetune vcpus='0-3'>
404507
+      <cache id='0' level='3' type='both' size='3' unit='MiB'/>
404507
+      <cache id='1' level='3' type='both' size='3' unit='MiB'/>
404507
+    </cachetune>
404507
   </cputune>
404507
   ...
404507
 </domain>
404507
@@ -834,6 +838,56 @@
404507
         Since 1.2.13
404507
       
404507
 
404507
+      
cachetuneSince 4.1.0
404507
+      
404507
+        Optional cachetune element can control allocations for CPU
404507
+        caches using the resctrl on the host. Whether or not is this supported
404507
+        can be gathered from capabilities where some limitations like minimum
404507
+        size and required granularity are reported as well. The required
404507
+        attribute vcpus specifies to which vCPUs this allocation
404507
+        applies. A vCPU can only be member of one cachetune element
404507
+        allocations. Supported subelements are:
404507
+        
404507
+          
cache
404507
+          
404507
+            This element controls the allocation of CPU cache and has the
404507
+            following attributes:
404507
+            
404507
+              
level
404507
+              
404507
+                Host cache level from which to allocate.
404507
+              
404507
+              
id
404507
+              
404507
+                Host cache id from which to allocate.
404507
+              
404507
+              
type
404507
+              
404507
+                Type of allocation. Can be code for code
404507
+                (instructions), data for data or both
404507
+                for both code and data (unified). Currently the allocation can
404507
+                be done only with the same type as the host supports, meaning
404507
+                you cannot request both for host with CDP
404507
+                (code/data prioritization) enabled.
404507
+              
404507
+              
size
404507
+              
404507
+                The size of the region to allocate. The value by default is in
404507
+                bytes, but the unit attribute can be used to scale
404507
+                the value.
404507
+              
404507
+              
unit (optional)
404507
+              
404507
+                If specified it is the unit such as KiB, MiB, GiB, or TiB
404507
+                (described in the memory element
404507
+                for Memory Allocation)
404507
+                in which size is specified, defaults to bytes.
404507
+              
404507
+            
404507
+          
404507
+        
404507
+
404507
+      
404507
     
404507
 
404507
 
404507
diff --git a/docs/schemas/domaincommon.rng b/docs/schemas/domaincommon.rng
404507
index 05709afc0c..564674b659 100644
404507
--- a/docs/schemas/domaincommon.rng
404507
+++ b/docs/schemas/domaincommon.rng
404507
@@ -900,6 +900,38 @@
404507
             <ref name="schedparam"/>
404507
           </element>
404507
         </zeroOrMore>
404507
+        <zeroOrMore>
404507
+          <element name="cachetune">
404507
+            <attribute name="vcpus">
404507
+              <ref name='cpuset'/>
404507
+            </attribute>
404507
+            <oneOrMore>
404507
+              <element name="cache">
404507
+                <attribute name="id">
404507
+                  <ref name='unsignedInt'/>
404507
+                </attribute>
404507
+                <attribute name="level">
404507
+                  <ref name='unsignedInt'/>
404507
+                </attribute>
404507
+                <attribute name="type">
404507
+                  <choice>
404507
+                    <value>both</value>
404507
+                    <value>code</value>
404507
+                    <value>data</value>
404507
+                  </choice>
404507
+                </attribute>
404507
+                <attribute name="size">
404507
+                  <ref name='unsignedLong'/>
404507
+                </attribute>
404507
+                <optional>
404507
+                  <attribute name='unit'>
404507
+                    <ref name='unit'/>
404507
+                  </attribute>
404507
+                </optional>
404507
+              </element>
404507
+            </oneOrMore>
404507
+          </element>
404507
+        </zeroOrMore>
404507
       </interleave>
404507
     </element>
404507
   </define>
404507
diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
404507
index ad9c16a1e0..181b035647 100644
404507
--- a/src/conf/domain_conf.c
404507
+++ b/src/conf/domain_conf.c
404507
@@ -2886,6 +2886,19 @@ virDomainLoaderDefFree(virDomainLoaderDefPtr loader)
404507
     VIR_FREE(loader);
404507
 }
404507
 
404507
+
404507
+static void
404507
+virDomainCachetuneDefFree(virDomainCachetuneDefPtr cachetune)
404507
+{
404507
+    if (!cachetune)
404507
+        return;
404507
+
404507
+    virObjectUnref(cachetune->alloc);
404507
+    virBitmapFree(cachetune->vcpus);
404507
+    VIR_FREE(cachetune);
404507
+}
404507
+
404507
+
404507
 void virDomainDefFree(virDomainDefPtr def)
404507
 {
404507
     size_t i;
404507
@@ -3058,6 +3071,10 @@ void virDomainDefFree(virDomainDefPtr def)
404507
         virDomainShmemDefFree(def->shmems[i]);
404507
     VIR_FREE(def->shmems);
404507
 
404507
+    for (i = 0; i < def->ncachetunes; i++)
404507
+        virDomainCachetuneDefFree(def->cachetunes[i]);
404507
+    VIR_FREE(def->cachetunes);
404507
+
404507
     VIR_FREE(def->keywrap);
404507
 
404507
     if (def->namespaceData && def->ns.free)
404507
@@ -18233,6 +18250,194 @@ virDomainDefParseBootOptions(virDomainDefPtr def,
404507
 }
404507
 
404507
 
404507
+static int
404507
+virDomainCachetuneDefParseCache(xmlXPathContextPtr ctxt,
404507
+                                xmlNodePtr node,
404507
+                                virResctrlAllocPtr alloc)
404507
+{
404507
+    xmlNodePtr oldnode = ctxt->node;
404507
+    unsigned int level;
404507
+    unsigned int cache;
404507
+    int type;
404507
+    unsigned long long size;
404507
+    char *tmp = NULL;
404507
+    int ret = -1;
404507
+
404507
+    ctxt->node = node;
404507
+
404507
+    tmp = virXMLPropString(node, "id");
404507
+    if (!tmp) {
404507
+        virReportError(VIR_ERR_XML_ERROR, "%s",
404507
+                       _("Missing cachetune attribute 'id'"));
404507
+        goto cleanup;
404507
+    }
404507
+    if (virStrToLong_uip(tmp, NULL, 10, &cache) < 0) {
404507
+        virReportError(VIR_ERR_XML_ERROR,
404507
+                       _("Invalid cachetune attribute 'id' value '%s'"),
404507
+                       tmp);
404507
+        goto cleanup;
404507
+    }
404507
+    VIR_FREE(tmp);
404507
+
404507
+    tmp = virXMLPropString(node, "level");
404507
+    if (!tmp) {
404507
+        virReportError(VIR_ERR_XML_ERROR, "%s",
404507
+                       _("Missing cachetune attribute 'level'"));
404507
+        goto cleanup;
404507
+    }
404507
+    if (virStrToLong_uip(tmp, NULL, 10, &level) < 0) {
404507
+        virReportError(VIR_ERR_XML_ERROR,
404507
+                       _("Invalid cachetune attribute 'level' value '%s'"),
404507
+                       tmp);
404507
+        goto cleanup;
404507
+    }
404507
+    VIR_FREE(tmp);
404507
+
404507
+    tmp = virXMLPropString(node, "type");
404507
+    if (!tmp) {
404507
+        virReportError(VIR_ERR_XML_ERROR, "%s",
404507
+                       _("Missing cachetune attribute 'type'"));
404507
+        goto cleanup;
404507
+    }
404507
+    type = virCacheTypeFromString(tmp);
404507
+    if (type < 0) {
404507
+        virReportError(VIR_ERR_XML_ERROR,
404507
+                       _("Invalid cachetune attribute 'type' value '%s'"),
404507
+                       tmp);
404507
+        goto cleanup;
404507
+    }
404507
+    VIR_FREE(tmp);
404507
+
404507
+    if (virDomainParseScaledValue("./@size", "./@unit",
404507
+                                  ctxt, &size, 1024,
404507
+                                  ULLONG_MAX, true) < 0)
404507
+        goto cleanup;
404507
+
404507
+    if (virResctrlAllocSetSize(alloc, level, type, cache, size) < 0)
404507
+        goto cleanup;
404507
+
404507
+    ret = 0;
404507
+ cleanup:
404507
+    ctxt->node = oldnode;
404507
+    VIR_FREE(tmp);
404507
+    return ret;
404507
+}
404507
+
404507
+
404507
+static int
404507
+virDomainCachetuneDefParse(virDomainDefPtr def,
404507
+                           xmlXPathContextPtr ctxt,
404507
+                           xmlNodePtr node,
404507
+                           unsigned int flags)
404507
+{
404507
+    xmlNodePtr oldnode = ctxt->node;
404507
+    xmlNodePtr *nodes = NULL;
404507
+    virBitmapPtr vcpus = NULL;
404507
+    virResctrlAllocPtr alloc = virResctrlAllocNew();
404507
+    virDomainCachetuneDefPtr tmp_cachetune = NULL;
404507
+    char *tmp = NULL;
404507
+    char *vcpus_str = NULL;
404507
+    char *alloc_id = NULL;
404507
+    ssize_t i = 0;
404507
+    int n;
404507
+    int ret = -1;
404507
+
404507
+    ctxt->node = node;
404507
+
404507
+    if (!alloc)
404507
+        goto cleanup;
404507
+
404507
+    if (VIR_ALLOC(tmp_cachetune) < 0)
404507
+        goto cleanup;
404507
+
404507
+    vcpus_str = virXMLPropString(node, "vcpus");
404507
+    if (!vcpus_str) {
404507
+        virReportError(VIR_ERR_XML_ERROR, "%s",
404507
+                       _("Missing cachetune attribute 'vcpus'"));
404507
+        goto cleanup;
404507
+    }
404507
+    if (virBitmapParse(vcpus_str, &vcpus, VIR_DOMAIN_CPUMASK_LEN) < 0) {
404507
+        virReportError(VIR_ERR_XML_ERROR,
404507
+                       _("Invalid cachetune attribute 'vcpus' value '%s'"),
404507
+                       vcpus_str);
404507
+        goto cleanup;
404507
+    }
404507
+
404507
+    /* We need to limit the bitmap to number of vCPUs.  If there's nothing left,
404507
+     * then we can just clean up and return 0 immediately */
404507
+    virBitmapShrink(vcpus, def->maxvcpus);
404507
+    if (virBitmapIsAllClear(vcpus)) {
404507
+        ret = 0;
404507
+        goto cleanup;
404507
+    }
404507
+
404507
+    if ((n = virXPathNodeSet("./cache", ctxt, &nodes)) < 0) {
404507
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
404507
+                       _("Cannot extract cache nodes under cachetune"));
404507
+        goto cleanup;
404507
+    }
404507
+
404507
+    for (i = 0; i < n; i++) {
404507
+        if (virDomainCachetuneDefParseCache(ctxt, nodes[i], alloc) < 0)
404507
+            goto cleanup;
404507
+    }
404507
+
404507
+    if (virResctrlAllocIsEmpty(alloc)) {
404507
+        ret = 0;
404507
+        goto cleanup;
404507
+    }
404507
+
404507
+    for (i = 0; i < def->ncachetunes; i++) {
404507
+        if (virBitmapOverlaps(def->cachetunes[i]->vcpus, vcpus)) {
404507
+            virReportError(VIR_ERR_XML_ERROR, "%s",
404507
+                           _("Overlapping vcpus in cachetunes"));
404507
+            goto cleanup;
404507
+        }
404507
+    }
404507
+
404507
+    /* We need to format it back because we need to be consistent in the naming
404507
+     * even when users specify some "sub-optimal" string there. */
404507
+    VIR_FREE(vcpus_str);
404507
+    vcpus_str = virBitmapFormat(vcpus);
404507
+    if (!vcpus_str)
404507
+        goto cleanup;
404507
+
404507
+    if (!(flags & VIR_DOMAIN_DEF_PARSE_INACTIVE))
404507
+        alloc_id = virXMLPropString(node, "id");
404507
+
404507
+    if (!alloc_id) {
404507
+        /* The number of allocations is limited and the directory structure is flat,
404507
+         * not hierarchical, so we need to have all same allocations in one
404507
+         * directory, so it's nice to have it named appropriately.  For now it's
404507
+         * 'vcpus_...' but it's designed in order for it to be changeable in the
404507
+         * future (it's part of the status XML). */
404507
+        if (virAsprintf(&alloc_id, "vcpus_%s", vcpus_str) < 0)
404507
+            goto cleanup;
404507
+    }
404507
+
404507
+    if (virResctrlAllocSetID(alloc, alloc_id) < 0)
404507
+        goto cleanup;
404507
+
404507
+    VIR_STEAL_PTR(tmp_cachetune->vcpus, vcpus);
404507
+    VIR_STEAL_PTR(tmp_cachetune->alloc, alloc);
404507
+
404507
+    if (VIR_APPEND_ELEMENT(def->cachetunes, def->ncachetunes, tmp_cachetune) < 0)
404507
+        goto cleanup;
404507
+
404507
+    ret = 0;
404507
+ cleanup:
404507
+    ctxt->node = oldnode;
404507
+    virDomainCachetuneDefFree(tmp_cachetune);
404507
+    virObjectUnref(alloc);
404507
+    virBitmapFree(vcpus);
404507
+    VIR_FREE(alloc_id);
404507
+    VIR_FREE(vcpus_str);
404507
+    VIR_FREE(nodes);
404507
+    VIR_FREE(tmp);
404507
+    return ret;
404507
+}
404507
+
404507
+
404507
 static virDomainDefPtr
404507
 virDomainDefParseXML(xmlDocPtr xml,
404507
                      xmlNodePtr root,
404507
@@ -18785,6 +18990,18 @@ virDomainDefParseXML(xmlDocPtr xml,
404507
     }
404507
     VIR_FREE(nodes);
404507
 
404507
+    if ((n = virXPathNodeSet("./cputune/cachetune", ctxt, &nodes)) < 0) {
404507
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
404507
+                       _("cannot extract cachetune nodes"));
404507
+        goto error;
404507
+    }
404507
+
404507
+    for (i = 0; i < n; i++) {
404507
+        if (virDomainCachetuneDefParse(def, ctxt, nodes[i], flags) < 0)
404507
+            goto error;
404507
+    }
404507
+    VIR_FREE(nodes);
404507
+
404507
     if (virCPUDefParseXML(ctxt, "./cpu[1]", VIR_CPU_TYPE_GUEST, &def->cpu) < 0)
404507
         goto error;
404507
 
404507
@@ -25736,9 +25953,80 @@ virDomainSchedulerFormat(virBufferPtr buf,
404507
 }
404507
 
404507
 
404507
+static int
404507
+virDomainCachetuneDefFormatHelper(unsigned int level,
404507
+                                  virCacheType type,
404507
+                                  unsigned int cache,
404507
+                                  unsigned long long size,
404507
+                                  void *opaque)
404507
+{
404507
+    const char *unit;
404507
+    virBufferPtr buf = opaque;
404507
+    unsigned long long short_size = virFormatIntPretty(size, &unit);
404507
+
404507
+    virBufferAsprintf(buf,
404507
+                      "
404507
+                      "size='%llu' unit='%s'/>\n",
404507
+                      cache, level, virCacheTypeToString(type),
404507
+                      short_size, unit);
404507
+
404507
+    return 0;
404507
+}
404507
+
404507
+
404507
+static int
404507
+virDomainCachetuneDefFormat(virBufferPtr buf,
404507
+                            virDomainCachetuneDefPtr cachetune,
404507
+                            unsigned int flags)
404507
+{
404507
+    virBuffer childrenBuf = VIR_BUFFER_INITIALIZER;
404507
+    char *vcpus = NULL;
404507
+    int ret = -1;
404507
+
404507
+    virBufferSetChildIndent(&childrenBuf, buf);
404507
+    virResctrlAllocForeachSize(cachetune->alloc,
404507
+                               virDomainCachetuneDefFormatHelper,
404507
+                               &childrenBuf);
404507
+
404507
+
404507
+    if (virBufferCheckError(&childrenBuf) < 0)
404507
+        goto cleanup;
404507
+
404507
+    if (!virBufferUse(&childrenBuf)) {
404507
+        ret = 0;
404507
+        goto cleanup;
404507
+    }
404507
+
404507
+    vcpus = virBitmapFormat(cachetune->vcpus);
404507
+    if (!vcpus)
404507
+        goto cleanup;
404507
+
404507
+    virBufferAsprintf(buf, "
404507
+
404507
+    if (!(flags & VIR_DOMAIN_DEF_FORMAT_INACTIVE)) {
404507
+        const char *alloc_id = virResctrlAllocGetID(cachetune->alloc);
404507
+        if (!alloc_id)
404507
+            goto cleanup;
404507
+
404507
+        virBufferAsprintf(buf, " id='%s'", alloc_id);
404507
+    }
404507
+    virBufferAddLit(buf, ">\n");
404507
+
404507
+    virBufferAddBuffer(buf, &childrenBuf);
404507
+    virBufferAddLit(buf, "</cachetune>\n");
404507
+
404507
+    ret = 0;
404507
+ cleanup:
404507
+    virBufferFreeAndReset(&childrenBuf);
404507
+    VIR_FREE(vcpus);
404507
+    return ret;
404507
+}
404507
+
404507
+
404507
 static int
404507
 virDomainCputuneDefFormat(virBufferPtr buf,
404507
-                          virDomainDefPtr def)
404507
+                          virDomainDefPtr def,
404507
+                          unsigned int flags)
404507
 {
404507
     size_t i;
404507
     virBuffer childrenBuf = VIR_BUFFER_INITIALIZER;
404507
@@ -25837,6 +26125,9 @@ virDomainCputuneDefFormat(virBufferPtr buf,
404507
                                  def->iothreadids[i]->iothread_id);
404507
     }
404507
 
404507
+    for (i = 0; i < def->ncachetunes; i++)
404507
+        virDomainCachetuneDefFormat(&childrenBuf, def->cachetunes[i], flags);
404507
+
404507
     if (virBufferCheckError(&childrenBuf) < 0)
404507
         return -1;
404507
 
404507
@@ -26174,7 +26465,7 @@ virDomainDefFormatInternal(virDomainDefPtr def,
404507
         }
404507
     }
404507
 
404507
-    if (virDomainCputuneDefFormat(buf, def) < 0)
404507
+    if (virDomainCputuneDefFormat(buf, def, flags) < 0)
404507
         goto error;
404507
 
404507
     if (virDomainNumatuneFormatXML(buf, def->numa) < 0)
404507
diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h
404507
index 5e67c9a9c1..ed7b587bed 100644
404507
--- a/src/conf/domain_conf.h
404507
+++ b/src/conf/domain_conf.h
404507
@@ -56,6 +56,7 @@
404507
 # include "virperf.h"
404507
 # include "virtypedparam.h"
404507
 # include "virsavecookie.h"
404507
+# include "virresctrl.h"
404507
 
404507
 /* forward declarations of all device types, required by
404507
  * virDomainDeviceDef
404507
@@ -2190,6 +2191,15 @@ struct _virDomainCputune {
404507
 };
404507
 
404507
 
404507
+typedef struct _virDomainCachetuneDef virDomainCachetuneDef;
404507
+typedef virDomainCachetuneDef *virDomainCachetuneDefPtr;
404507
+
404507
+struct _virDomainCachetuneDef {
404507
+    virBitmapPtr vcpus;
404507
+    virResctrlAllocPtr alloc;
404507
+};
404507
+
404507
+
404507
 typedef struct _virDomainVcpuDef virDomainVcpuDef;
404507
 typedef virDomainVcpuDef *virDomainVcpuDefPtr;
404507
 
404507
@@ -2318,6 +2328,9 @@ struct _virDomainDef {
404507
 
404507
     virDomainCputune cputune;
404507
 
404507
+    virDomainCachetuneDefPtr *cachetunes;
404507
+    size_t ncachetunes;
404507
+
404507
     virDomainNumaPtr numa;
404507
     virDomainResourceDefPtr resource;
404507
     virDomainIdMapDef idmap;
404507
diff --git a/tests/genericxml2xmlindata/generic-cachetune-cdp.xml b/tests/genericxml2xmlindata/generic-cachetune-cdp.xml
404507
new file mode 100644
404507
index 0000000000..9718f06098
404507
--- /dev/null
404507
+++ b/tests/genericxml2xmlindata/generic-cachetune-cdp.xml
404507
@@ -0,0 +1,36 @@
404507
+<domain type='qemu'>
404507
+  <name>QEMUGuest1</name>
404507
+  <uuid>c7a5fdbd-edaf-9455-926a-d65c16db1809</uuid>
404507
+  <memory unit='KiB'>219136</memory>
404507
+  <currentMemory unit='KiB'>219136</currentMemory>
404507
+  <vcpu placement='static'>4</vcpu>
404507
+  <cputune>
404507
+    <cachetune vcpus='0-1'>
404507
+      <cache id='0' level='3' type='code' size='7680' unit='KiB'/>
404507
+      <cache id='1' level='3' type='data' size='3840' unit='KiB'/>
404507
+    </cachetune>
404507
+    <cachetune vcpus='2'>
404507
+      <cache id='1' level='3' type='code' size='6' unit='MiB'/>
404507
+    </cachetune>
404507
+    <cachetune vcpus='3'>
404507
+      <cache id='1' level='3' type='data' size='6912' unit='KiB'/>
404507
+    </cachetune>
404507
+  </cputune>
404507
+  <os>
404507
+    <type arch='i686' machine='pc'>hvm</type>
404507
+    <boot dev='hd'/>
404507
+  </os>
404507
+  <clock offset='utc'/>
404507
+  <on_poweroff>destroy</on_poweroff>
404507
+  <on_reboot>restart</on_reboot>
404507
+  <on_crash>destroy</on_crash>
404507
+  <devices>
404507
+    <emulator>/usr/bin/qemu-system-i686</emulator>
404507
+    <controller type='usb' index='0'/>
404507
+    <controller type='ide' index='0'/>
404507
+    <controller type='pci' index='0' model='pci-root'/>
404507
+    <input type='mouse' bus='ps2'/>
404507
+    <input type='keyboard' bus='ps2'/>
404507
+    <memballoon model='virtio'/>
404507
+  </devices>
404507
+</domain>
404507
diff --git a/tests/genericxml2xmlindata/generic-cachetune-colliding-allocs.xml b/tests/genericxml2xmlindata/generic-cachetune-colliding-allocs.xml
404507
new file mode 100644
404507
index 0000000000..82c9176cba
404507
--- /dev/null
404507
+++ b/tests/genericxml2xmlindata/generic-cachetune-colliding-allocs.xml
404507
@@ -0,0 +1,30 @@
404507
+<domain type='qemu'>
404507
+  <name>QEMUGuest1</name>
404507
+  <uuid>c7a5fdbd-edaf-9455-926a-d65c16db1809</uuid>
404507
+  <memory unit='KiB'>219136</memory>
404507
+  <currentMemory unit='KiB'>219136</currentMemory>
404507
+  <vcpu placement='static'>4</vcpu>
404507
+  <cputune>
404507
+    <cachetune vcpus='0'>
404507
+      <cache id='0' level='3' type='code' size='12' unit='KiB'/>
404507
+      <cache id='0' level='3' type='code' size='18' unit='KiB'/>
404507
+    </cachetune>
404507
+  </cputune>
404507
+  <os>
404507
+    <type arch='i686' machine='pc'>hvm</type>
404507
+    <boot dev='hd'/>
404507
+  </os>
404507
+  <clock offset='utc'/>
404507
+  <on_poweroff>destroy</on_poweroff>
404507
+  <on_reboot>restart</on_reboot>
404507
+  <on_crash>destroy</on_crash>
404507
+  <devices>
404507
+    <emulator>/usr/bin/qemu-system-i686</emulator>
404507
+    <controller type='usb' index='0'/>
404507
+    <controller type='ide' index='0'/>
404507
+    <controller type='pci' index='0' model='pci-root'/>
404507
+    <input type='mouse' bus='ps2'/>
404507
+    <input type='keyboard' bus='ps2'/>
404507
+    <memballoon model='virtio'/>
404507
+  </devices>
404507
+</domain>
404507
diff --git a/tests/genericxml2xmlindata/generic-cachetune-colliding-tunes.xml b/tests/genericxml2xmlindata/generic-cachetune-colliding-tunes.xml
404507
new file mode 100644
404507
index 0000000000..a0f37028c9
404507
--- /dev/null
404507
+++ b/tests/genericxml2xmlindata/generic-cachetune-colliding-tunes.xml
404507
@@ -0,0 +1,32 @@
404507
+<domain type='qemu'>
404507
+  <name>QEMUGuest1</name>
404507
+  <uuid>c7a5fdbd-edaf-9455-926a-d65c16db1809</uuid>
404507
+  <memory unit='KiB'>219136</memory>
404507
+  <currentMemory unit='KiB'>219136</currentMemory>
404507
+  <vcpu placement='static'>4</vcpu>
404507
+  <cputune>
404507
+    <cachetune vcpus='0'>
404507
+      <cache id='0' level='3' type='code' size='12' unit='KiB'/>
404507
+    </cachetune>
404507
+    <cachetune vcpus='0'>
404507
+      <cache id='0' level='3' type='data' size='18' unit='KiB'/>
404507
+    </cachetune>
404507
+  </cputune>
404507
+  <os>
404507
+    <type arch='i686' machine='pc'>hvm</type>
404507
+    <boot dev='hd'/>
404507
+  </os>
404507
+  <clock offset='utc'/>
404507
+  <on_poweroff>destroy</on_poweroff>
404507
+  <on_reboot>restart</on_reboot>
404507
+  <on_crash>destroy</on_crash>
404507
+  <devices>
404507
+    <emulator>/usr/bin/qemu-system-i686</emulator>
404507
+    <controller type='usb' index='0'/>
404507
+    <controller type='ide' index='0'/>
404507
+    <controller type='pci' index='0' model='pci-root'/>
404507
+    <input type='mouse' bus='ps2'/>
404507
+    <input type='keyboard' bus='ps2'/>
404507
+    <memballoon model='virtio'/>
404507
+  </devices>
404507
+</domain>
404507
diff --git a/tests/genericxml2xmlindata/generic-cachetune-colliding-types.xml b/tests/genericxml2xmlindata/generic-cachetune-colliding-types.xml
404507
new file mode 100644
404507
index 0000000000..c229eccee4
404507
--- /dev/null
404507
+++ b/tests/genericxml2xmlindata/generic-cachetune-colliding-types.xml
404507
@@ -0,0 +1,30 @@
404507
+<domain type='qemu'>
404507
+  <name>QEMUGuest1</name>
404507
+  <uuid>c7a5fdbd-edaf-9455-926a-d65c16db1809</uuid>
404507
+  <memory unit='KiB'>219136</memory>
404507
+  <currentMemory unit='KiB'>219136</currentMemory>
404507
+  <vcpu placement='static'>4</vcpu>
404507
+  <cputune>
404507
+    <cachetune vcpus='0-1'>
404507
+      <cache id='0' level='3' type='both' size='12' unit='KiB'/>
404507
+      <cache id='0' level='3' type='code' size='6' unit='KiB'/>
404507
+    </cachetune>
404507
+  </cputune>
404507
+  <os>
404507
+    <type arch='i686' machine='pc'>hvm</type>
404507
+    <boot dev='hd'/>
404507
+  </os>
404507
+  <clock offset='utc'/>
404507
+  <on_poweroff>destroy</on_poweroff>
404507
+  <on_reboot>restart</on_reboot>
404507
+  <on_crash>destroy</on_crash>
404507
+  <devices>
404507
+    <emulator>/usr/bin/qemu-system-i686</emulator>
404507
+    <controller type='usb' index='0'/>
404507
+    <controller type='ide' index='0'/>
404507
+    <controller type='pci' index='0' model='pci-root'/>
404507
+    <input type='mouse' bus='ps2'/>
404507
+    <input type='keyboard' bus='ps2'/>
404507
+    <memballoon model='virtio'/>
404507
+  </devices>
404507
+</domain>
404507
diff --git a/tests/genericxml2xmlindata/generic-cachetune-small.xml b/tests/genericxml2xmlindata/generic-cachetune-small.xml
404507
new file mode 100644
404507
index 0000000000..ab2d9cf885
404507
--- /dev/null
404507
+++ b/tests/genericxml2xmlindata/generic-cachetune-small.xml
404507
@@ -0,0 +1,29 @@
404507
+<domain type='qemu'>
404507
+  <name>QEMUGuest1</name>
404507
+  <uuid>c7a5fdbd-edaf-9455-926a-d65c16db1809</uuid>
404507
+  <memory unit='KiB'>219136</memory>
404507
+  <currentMemory unit='KiB'>219136</currentMemory>
404507
+  <vcpu placement='static'>4</vcpu>
404507
+  <cputune>
404507
+    <cachetune vcpus='0-1'>
404507
+      <cache id='0' level='3' type='both' size='768' unit='KiB'/>
404507
+    </cachetune>
404507
+  </cputune>
404507
+  <os>
404507
+    <type arch='i686' machine='pc'>hvm</type>
404507
+    <boot dev='hd'/>
404507
+  </os>
404507
+  <clock offset='utc'/>
404507
+  <on_poweroff>destroy</on_poweroff>
404507
+  <on_reboot>restart</on_reboot>
404507
+  <on_crash>destroy</on_crash>
404507
+  <devices>
404507
+    <emulator>/usr/bin/qemu-system-i686</emulator>
404507
+    <controller type='usb' index='0'/>
404507
+    <controller type='ide' index='0'/>
404507
+    <controller type='pci' index='0' model='pci-root'/>
404507
+    <input type='mouse' bus='ps2'/>
404507
+    <input type='keyboard' bus='ps2'/>
404507
+    <memballoon model='virtio'/>
404507
+  </devices>
404507
+</domain>
404507
diff --git a/tests/genericxml2xmlindata/generic-cachetune.xml b/tests/genericxml2xmlindata/generic-cachetune.xml
404507
new file mode 100644
404507
index 0000000000..645cab7771
404507
--- /dev/null
404507
+++ b/tests/genericxml2xmlindata/generic-cachetune.xml
404507
@@ -0,0 +1,33 @@
404507
+<domain type='qemu'>
404507
+  <name>QEMUGuest1</name>
404507
+  <uuid>c7a5fdbd-edaf-9455-926a-d65c16db1809</uuid>
404507
+  <memory unit='KiB'>219136</memory>
404507
+  <currentMemory unit='KiB'>219136</currentMemory>
404507
+  <vcpu placement='static'>4</vcpu>
404507
+  <cputune>
404507
+    <cachetune vcpus='0-1'>
404507
+      <cache id='0' level='3' type='both' size='3' unit='MiB'/>
404507
+      <cache id='1' level='3' type='both' size='3' unit='MiB'/>
404507
+    </cachetune>
404507
+    <cachetune vcpus='3'>
404507
+      <cache id='0' level='3' type='both' size='3' unit='MiB'/>
404507
+    </cachetune>
404507
+  </cputune>
404507
+  <os>
404507
+    <type arch='i686' machine='pc'>hvm</type>
404507
+    <boot dev='hd'/>
404507
+  </os>
404507
+  <clock offset='utc'/>
404507
+  <on_poweroff>destroy</on_poweroff>
404507
+  <on_reboot>restart</on_reboot>
404507
+  <on_crash>destroy</on_crash>
404507
+  <devices>
404507
+    <emulator>/usr/bin/qemu-system-i686</emulator>
404507
+    <controller type='usb' index='0'/>
404507
+    <controller type='ide' index='0'/>
404507
+    <controller type='pci' index='0' model='pci-root'/>
404507
+    <input type='mouse' bus='ps2'/>
404507
+    <input type='keyboard' bus='ps2'/>
404507
+    <memballoon model='virtio'/>
404507
+  </devices>
404507
+</domain>
404507
diff --git a/tests/genericxml2xmltest.c b/tests/genericxml2xmltest.c
404507
index 0377a05e9c..4ced5c349c 100644
404507
--- a/tests/genericxml2xmltest.c
404507
+++ b/tests/genericxml2xmltest.c
404507
@@ -130,6 +130,16 @@ mymain(void)
404507
     DO_TEST_FULL("chardev-reconnect-invalid-mode", 0, false,
404507
                  TEST_COMPARE_DOM_XML2XML_RESULT_FAIL_PARSE);
404507
 
404507
+    DO_TEST("cachetune");
404507
+    DO_TEST("cachetune-small");
404507
+    DO_TEST("cachetune-cdp");
404507
+    DO_TEST_FULL("cachetune-colliding-allocs", false, true,
404507
+                 TEST_COMPARE_DOM_XML2XML_RESULT_FAIL_PARSE);
404507
+    DO_TEST_FULL("cachetune-colliding-tunes", false, true,
404507
+                 TEST_COMPARE_DOM_XML2XML_RESULT_FAIL_PARSE);
404507
+    DO_TEST_FULL("cachetune-colliding-types", false, true,
404507
+                 TEST_COMPARE_DOM_XML2XML_RESULT_FAIL_PARSE);
404507
+
404507
     virObjectUnref(caps);
404507
     virObjectUnref(xmlopt);
404507
 
404507
-- 
404507
2.16.1
404507