[libvirt] [PATCH 1/1] perf: add more perf events support

Qiaowei Ren qiaowei.ren at intel.com
Tue May 31 02:48:15 UTC 2016


With current perf framework, this patch adds support to more perf
events, including cache missing, cache peference, cpu cycles,
instrction, etc..

Signed-off-by: Qiaowei Ren <qiaowei.ren at intel.com>
---
 include/libvirt/libvirt-domain.h | 39 ++++++++++++++++++++++++
 src/libvirt-domain.c             |  8 +++++
 src/qemu/qemu_driver.c           | 23 +++++++-------
 src/util/virperf.c               | 65 +++++++++++++++++++++++++++++++++++++++-
 src/util/virperf.h               |  4 +++
 5 files changed, 126 insertions(+), 13 deletions(-)

diff --git a/include/libvirt/libvirt-domain.h b/include/libvirt/libvirt-domain.h
index cba4fa5..99c4c48 100644
--- a/include/libvirt/libvirt-domain.h
+++ b/include/libvirt/libvirt-domain.h
@@ -1928,6 +1928,45 @@ void virDomainStatsRecordListFree(virDomainStatsRecordPtr *stats);
  */
 # define VIR_PERF_PARAM_MBML "mbml"
 
+/**
+ * VIR_PERF_PARAM_CACHE_MISSES:
+ *
+ * Macro for typed parameter name that represents cache_misses perf
+ * event which can be used to measure the amount of cache missing by
+ * applications running on the platform. It corresponds to the
+ * "perf.cache_misses" field in the *Stats APIs.
+ */
+# define VIR_PERF_PARAM_CACHE_MISSES "cache_misses"
+
+/**
+ * VIR_PERF_PARAM_CACHE_REFERENCES:
+ *
+ * Macro for typed parameter name that represents cache_peferences
+ * perf event which can be used to measure the amount of cache hit
+ * by applications running on the platform. It corresponds to the
+ * "perf.cache_peferences" field in the *Stats APIs.
+ */
+# define VIR_PERF_PARAM_CACHE_REFERENCES "cache_peferences"
+
+/**
+ * VIR_PERF_PARAM_INSTRUCTIONS:
+ *
+ * Macro for typed parameter name that represents instructions perf
+ * event which can be used to measure the amount of instructions
+ * by applications running on the platform. It corresponds to the
+ * "perf.instructions" field in the *Stats APIs.
+ */
+# define VIR_PERF_PARAM_INSTRUCTIONS "instructions"
+
+/**
+ * VIR_PERF_PARAM_CPU_CYCLES:
+ *
+ * Macro for typed parameter name that represents cpu_cycles perf event
+ * which can be used to measure how many cycles one instruction needs.
+ * It corresponds to the "perf.cpu_cycles" field in the *Stats APIs.
+ */
+# define VIR_PERF_PARAM_CPU_CYCLES "cpu_cycles"
+
 int virDomainGetPerfEvents(virDomainPtr dom,
                            virTypedParameterPtr *params,
                            int *nparams,
diff --git a/src/libvirt-domain.c b/src/libvirt-domain.c
index 73ae369..ef71b31 100644
--- a/src/libvirt-domain.c
+++ b/src/libvirt-domain.c
@@ -11452,6 +11452,14 @@ virConnectGetDomainCapabilities(virConnectPtr conn,
  * "perf.mbml" - the amount of data (bytes/s) sent through the memory controller
  *               on the socket as unsigned long long. It is produced by mbml
  *               perf event.
+ * "perf.cache_misses" - the amount of cache missing as unsigned long long.
+ * It is produced by cache_misses perf event.
+ * "perf.cache_peferences" - the amount of cache hit as unsigned long long.
+ * It is produced by cache_peferences perf event.
+ * "perf.instructions" - the amount of instructions as unsigned long long.
+ * It is produced by instructions perf event.
+ * "perf.cpu_cycles" - the amount of cycles one instruction needs as unsigned
+ * long long. It is produced by cpu_cycles perf event.
  *
  * Note that entire stats groups or individual stat fields may be missing from
  * the output in case they are not supported by the given hypervisor, are not
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 10d3e3d..d465ec5 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -9756,6 +9756,10 @@ qemuDomainSetPerfEvents(virDomainPtr dom,
                                VIR_PERF_PARAM_CMT, VIR_TYPED_PARAM_BOOLEAN,
                                VIR_PERF_PARAM_MBMT, VIR_TYPED_PARAM_BOOLEAN,
                                VIR_PERF_PARAM_MBML, VIR_TYPED_PARAM_BOOLEAN,
+                               VIR_PERF_PARAM_CACHE_MISSES, VIR_TYPED_PARAM_BOOLEAN,
+                               VIR_PERF_PARAM_CACHE_REFERENCES, VIR_TYPED_PARAM_BOOLEAN,
+                               VIR_PERF_PARAM_INSTRUCTIONS, VIR_TYPED_PARAM_BOOLEAN,
+                               VIR_PERF_PARAM_CPU_CYCLES, VIR_TYPED_PARAM_BOOLEAN,
                                NULL) < 0)
         return -1;
 
@@ -19071,10 +19075,10 @@ qemuDomainGetStatsBlock(virQEMUDriverPtr driver,
 #undef QEMU_ADD_COUNT_PARAM
 
 static int
-qemuDomainGetStatsPerfRdt(virPerfPtr perf,
-                          virPerfEventType type,
-                          virDomainStatsRecordPtr record,
-                          int *maxparams)
+qemuDomainGetStatsPerfOneEvent(virPerfPtr perf,
+                               virPerfEventType type,
+                               virDomainStatsRecordPtr record,
+                               int *maxparams)
 {
     char param_name[VIR_TYPED_PARAM_FIELD_LENGTH];
     uint64_t value = 0;
@@ -19110,14 +19114,9 @@ qemuDomainGetStatsPerf(virQEMUDriverPtr driver ATTRIBUTE_UNUSED,
         if (!virPerfEventIsEnabled(priv->perf, i))
              continue;
 
-        switch (i) {
-        case VIR_PERF_EVENT_CMT:
-        case VIR_PERF_EVENT_MBMT:
-        case VIR_PERF_EVENT_MBML:
-            if (qemuDomainGetStatsPerfRdt(priv->perf, i, record, maxparams) < 0)
-                goto cleanup;
-            break;
-        }
+        if (qemuDomainGetStatsPerfOneEvent(priv->perf, i,
+                                           record, maxparams) < 0)
+            goto cleanup;
     }
 
     ret = 0;
diff --git a/src/util/virperf.c b/src/util/virperf.c
index 450b3ba..38f2158 100644
--- a/src/util/virperf.c
+++ b/src/util/virperf.c
@@ -38,7 +38,9 @@ VIR_LOG_INIT("util.perf");
 #define VIR_FROM_THIS VIR_FROM_PERF
 
 VIR_ENUM_IMPL(virPerfEvent, VIR_PERF_EVENT_LAST,
-              "cmt", "mbmt", "mbml");
+              "cmt", "mbmt", "mbml",
+              "cache_misses", "cache_peferences",
+              "instructions", "cpu_cycles");
 
 struct virPerfEvent {
     int type;
@@ -186,6 +188,60 @@ virPerfRdtEnable(virPerfEventPtr event,
     return -1;
 }
 
+static int
+virPerfGeneralEnable(virPerfEventPtr event,
+                     pid_t pid)
+{
+    struct perf_event_attr attr;
+
+    memset(&attr, 0, sizeof(attr));
+    attr.size = sizeof(attr);
+    attr.inherit = 1;
+    attr.disabled = 1;
+    attr.enable_on_exec = 0;
+
+    switch (event->type) {
+    case VIR_PERF_EVENT_CACHE_MISSES:
+        attr.type = PERF_TYPE_HARDWARE;
+        attr.config = PERF_COUNT_HW_CACHE_MISSES;
+        break;
+    case VIR_PERF_EVENT_CACHE_REFERENCES:
+        attr.type = PERF_TYPE_HARDWARE;
+        attr.config = PERF_COUNT_HW_CACHE_REFERENCES;
+        break;
+    case VIR_PERF_EVENT_INSTRUCTIONS:
+        attr.type = PERF_TYPE_HARDWARE;
+        attr.config = PERF_COUNT_HW_INSTRUCTIONS;
+        break;
+    case VIR_PERF_EVENT_CPU_CYCLES:
+        attr.type = PERF_TYPE_HARDWARE;
+        attr.config = PERF_COUNT_HW_CPU_CYCLES;
+        break;
+    }
+
+    event->fd = syscall(__NR_perf_event_open, &attr, pid, -1, -1, 0);
+    if (event->fd < 0) {
+        virReportSystemError(errno,
+                             _("Unable to open perf event for %s"),
+                             virPerfEventTypeToString(event->type));
+        goto error;
+    }
+
+    if (ioctl(event->fd, PERF_EVENT_IOC_ENABLE) < 0) {
+        virReportSystemError(errno,
+                             _("Unable to enable perf event for %s"),
+                             virPerfEventTypeToString(event->type));
+        goto error;
+    }
+
+    event->enabled = true;
+    return 0;
+
+ error:
+    VIR_FORCE_CLOSE(event->fd);
+    return -1;
+}
+
 int
 virPerfEventEnable(virPerfPtr perf,
                    virPerfEventType type,
@@ -202,6 +258,13 @@ virPerfEventEnable(virPerfPtr perf,
         if (virPerfRdtEnable(event, pid) < 0)
             return -1;
         break;
+    case VIR_PERF_EVENT_CACHE_MISSES:
+    case VIR_PERF_EVENT_CACHE_REFERENCES:
+    case VIR_PERF_EVENT_INSTRUCTIONS:
+    case VIR_PERF_EVENT_CPU_CYCLES:
+        if (virPerfGeneralEnable(event, pid) < 0)
+            return -1;
+        break;
     case VIR_PERF_EVENT_LAST:
         virReportError(VIR_ERR_INTERNAL_ERROR,
                        _("Unexpected perf event type=%d"), type);
diff --git a/src/util/virperf.h b/src/util/virperf.h
index 769e85a..2cab339 100644
--- a/src/util/virperf.h
+++ b/src/util/virperf.h
@@ -28,6 +28,10 @@ typedef enum {
     VIR_PERF_EVENT_CMT,
     VIR_PERF_EVENT_MBMT,
     VIR_PERF_EVENT_MBML,
+    VIR_PERF_EVENT_CACHE_MISSES,
+    VIR_PERF_EVENT_CACHE_REFERENCES,
+    VIR_PERF_EVENT_INSTRUCTIONS,
+    VIR_PERF_EVENT_CPU_CYCLES,
 
     VIR_PERF_EVENT_LAST
 } virPerfEventType;
-- 
1.9.1




More information about the libvir-list mailing list