Blob Blame History Raw
From d274b66f58d59d97543c8b64e9f88449581e4299 Mon Sep 17 00:00:00 2001
From: Jerome Marchand <jmarchan@redhat.com>
Date: Tue, 7 Aug 2018 17:24:31 +0200
Subject: [PATCH] llcstat: print a nicer error message when hardware events are
 missing

Hardware events such as CACHE_MISSES and CACHE_REFERENCES are usually
not available on virtual machine. Print a more useful message when
this happen.
---
 tools/llcstat.py | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)

diff --git a/tools/llcstat.py b/tools/llcstat.py
index e59f9a8..fe8bdd9 100755
--- a/tools/llcstat.py
+++ b/tools/llcstat.py
@@ -78,12 +78,16 @@ int on_cache_ref(struct bpf_perf_event_data *ctx) {
     exit()
 
 b = BPF(text=bpf_text)
-b.attach_perf_event(
-    ev_type=PerfType.HARDWARE, ev_config=PerfHWConfig.CACHE_MISSES,
-    fn_name="on_cache_miss", sample_period=args.sample_period)
-b.attach_perf_event(
-    ev_type=PerfType.HARDWARE, ev_config=PerfHWConfig.CACHE_REFERENCES,
-    fn_name="on_cache_ref", sample_period=args.sample_period)
+try:
+    b.attach_perf_event(
+        ev_type=PerfType.HARDWARE, ev_config=PerfHWConfig.CACHE_MISSES,
+        fn_name="on_cache_miss", sample_period=args.sample_period)
+    b.attach_perf_event(
+        ev_type=PerfType.HARDWARE, ev_config=PerfHWConfig.CACHE_REFERENCES,
+        fn_name="on_cache_ref", sample_period=args.sample_period)
+except:
+    print("Failed to attach to a hardware event. Is this a virtual machine?")
+    exit()
 
 print("Running for {} seconds or hit Ctrl-C to end.".format(args.duration))
 
-- 
2.17.1