99cbc7
From 5b07ca7c378a5e62fec0ed4808e8905fe447e85e Mon Sep 17 00:00:00 2001
99cbc7
Message-Id: <5b07ca7c378a5e62fec0ed4808e8905fe447e85e@dist-git>
99cbc7
From: Jiri Denemark <jdenemar@redhat.com>
99cbc7
Date: Tue, 4 Jun 2019 13:04:30 +0200
99cbc7
Subject: [PATCH] conf: Report TSC frequency in host CPU capabilities
99cbc7
MIME-Version: 1.0
99cbc7
Content-Type: text/plain; charset=UTF-8
99cbc7
Content-Transfer-Encoding: 8bit
99cbc7
99cbc7
This patch adds a new
99cbc7
99cbc7
    <counter name='tsc' frequency='N' scaling='on|off'/>
99cbc7
99cbc7
element into the host CPU capabilities XML.
99cbc7
99cbc7
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
99cbc7
(cherry picked from commit c277b9ad5c740bb4c4b915754ae74621f93f9d37)
99cbc7
99cbc7
https://bugzilla.redhat.com/show_bug.cgi?id=1641702
99cbc7
99cbc7
Conflicts:
99cbc7
	src/conf/cpu_conf.h
99cbc7
            - virenum.h doesn't exist downstream
99cbc7
99cbc7
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
99cbc7
Message-Id: <c0a6203e4a667c31668e0df508b08b515177276f.1559646067.git.jdenemar@redhat.com>
99cbc7
Reviewed-by: Ján Tomko <jtomko@redhat.com>
99cbc7
---
99cbc7
 src/conf/cpu_conf.c | 48 +++++++++++++++++++++++++++++++++++++++++++++
99cbc7
 src/conf/cpu_conf.h |  2 ++
99cbc7
 2 files changed, 50 insertions(+)
99cbc7
99cbc7
diff --git a/src/conf/cpu_conf.c b/src/conf/cpu_conf.c
99cbc7
index 43a3ab5dcd..d0ad265fbe 100644
99cbc7
--- a/src/conf/cpu_conf.c
99cbc7
+++ b/src/conf/cpu_conf.c
99cbc7
@@ -102,6 +102,7 @@ virCPUDefFree(virCPUDefPtr def)
99cbc7
 
99cbc7
     virCPUDefFreeModel(def);
99cbc7
     VIR_FREE(def->cache);
99cbc7
+    VIR_FREE(def->tsc);
99cbc7
     VIR_FREE(def);
99cbc7
 }
99cbc7
 
99cbc7
@@ -223,6 +224,13 @@ virCPUDefCopyWithoutModel(const virCPUDef *cpu)
99cbc7
         *copy->cache = *cpu->cache;
99cbc7
     }
99cbc7
 
99cbc7
+    if (cpu->tsc) {
99cbc7
+        if (VIR_ALLOC(copy->tsc) < 0)
99cbc7
+            goto error;
99cbc7
+
99cbc7
+        *copy->tsc = *cpu->tsc;
99cbc7
+    }
99cbc7
+
99cbc7
     return copy;
99cbc7
 
99cbc7
  error:
99cbc7
@@ -276,6 +284,8 @@ virCPUDefParseXML(xmlXPathContextPtr ctxt,
99cbc7
     char *cpuMode;
99cbc7
     char *fallback = NULL;
99cbc7
     char *vendor_id = NULL;
99cbc7
+    char *tscScaling = NULL;
99cbc7
+    virHostCPUTscInfoPtr tsc = NULL;
99cbc7
     int ret = -1;
99cbc7
 
99cbc7
     *cpu = NULL;
99cbc7
@@ -392,6 +402,32 @@ virCPUDefParseXML(xmlXPathContextPtr ctxt,
99cbc7
                            _("invalid microcode version"));
99cbc7
             goto cleanup;
99cbc7
         }
99cbc7
+
99cbc7
+        if (virXPathBoolean("boolean(./counter[@name='tsc'])", ctxt) > 0) {
99cbc7
+            if (VIR_ALLOC(tsc) < 0)
99cbc7
+                goto cleanup;
99cbc7
+
99cbc7
+            if (virXPathULongLong("./counter[@name='tsc']/@frequency", ctxt,
99cbc7
+                                  &tsc->frequency) < 0) {
99cbc7
+                virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
99cbc7
+                               _("Invalid TSC frequency"));
99cbc7
+                goto cleanup;
99cbc7
+            }
99cbc7
+
99cbc7
+            tscScaling = virXPathString("string(./counter[@name='tsc']/@scaling)",
99cbc7
+                                        ctxt);
99cbc7
+            if (tscScaling) {
99cbc7
+                int scaling = virTristateBoolTypeFromString(tscScaling);
99cbc7
+                if (scaling < 0) {
99cbc7
+                    virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
99cbc7
+                                   _("Invalid TSC scaling attribute"));
99cbc7
+                    goto cleanup;
99cbc7
+                }
99cbc7
+                tsc->scaling = scaling;
99cbc7
+            }
99cbc7
+
99cbc7
+            VIR_STEAL_PTR(def->tsc, tsc);
99cbc7
+        }
99cbc7
     }
99cbc7
 
99cbc7
     if (!(def->model = virXPathString("string(./model[1])", ctxt)) &&
99cbc7
@@ -577,6 +613,8 @@ virCPUDefParseXML(xmlXPathContextPtr ctxt,
99cbc7
     VIR_FREE(fallback);
99cbc7
     VIR_FREE(vendor_id);
99cbc7
     VIR_FREE(nodes);
99cbc7
+    VIR_FREE(tscScaling);
99cbc7
+    VIR_FREE(tsc);
99cbc7
     virCPUDefFree(def);
99cbc7
     return ret;
99cbc7
 }
99cbc7
@@ -734,6 +772,16 @@ virCPUDefFormatBuf(virBufferPtr buf,
99cbc7
         virBufferAsprintf(buf, "<microcode version='%u'/>\n",
99cbc7
                           def->microcodeVersion);
99cbc7
 
99cbc7
+    if (def->type == VIR_CPU_TYPE_HOST && def->tsc) {
99cbc7
+        virBufferAddLit(buf, "
99cbc7
+        virBufferAsprintf(buf, " frequency='%llu'", def->tsc->frequency);
99cbc7
+        if (def->tsc->scaling) {
99cbc7
+            virBufferAsprintf(buf, " scaling='%s'",
99cbc7
+                              virTristateBoolTypeToString(def->tsc->scaling));
99cbc7
+        }
99cbc7
+        virBufferAddLit(buf, "/>\n");
99cbc7
+    }
99cbc7
+
99cbc7
     if (def->sockets && def->cores && def->threads) {
99cbc7
         virBufferAddLit(buf, "
99cbc7
         virBufferAsprintf(buf, " sockets='%u'", def->sockets);
99cbc7
diff --git a/src/conf/cpu_conf.h b/src/conf/cpu_conf.h
99cbc7
index 9f2e7ee264..5bda4d6961 100644
99cbc7
--- a/src/conf/cpu_conf.h
99cbc7
+++ b/src/conf/cpu_conf.h
99cbc7
@@ -30,6 +30,7 @@
99cbc7
 # include "virbitmap.h"
99cbc7
 # include "virarch.h"
99cbc7
 # include "numa_conf.h"
99cbc7
+# include "virhostcpu.h"
99cbc7
 
99cbc7
 # define VIR_CPU_VENDOR_ID_LENGTH 12
99cbc7
 
99cbc7
@@ -141,6 +142,7 @@ struct _virCPUDef {
99cbc7
     size_t nfeatures_max;
99cbc7
     virCPUFeatureDefPtr features;
99cbc7
     virCPUCacheDefPtr cache;
99cbc7
+    virHostCPUTscInfoPtr tsc;
99cbc7
 };
99cbc7
 
99cbc7
 
99cbc7
-- 
99cbc7
2.21.0
99cbc7