[compiler-rt] r293870 - [XRay] Probe for CPU features that XRay needs

Dean Michael Berris via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 1 23:51:22 PST 2017


Author: dberris
Date: Thu Feb  2 01:51:21 2017
New Revision: 293870

URL: http://llvm.org/viewvc/llvm-project?rev=293870&view=rev
Log:
[XRay] Probe for CPU features that XRay needs

Summary:
In llvm.org/PR31756 it's pointed out that sometimes rdtscp isn't
available. We fix it here by checking first whether it's availble before
installing the logging handler. In future commits we can have
alternative implementations, maybe working around some of the
constraints on some systems.

This change enables us to make that determination, but report an error
instead when the features aren't available.

Reviewers: sdardis, javed.absar, rSerge

Subscribers: pelikan, llvm-commits

Differential Revision: https://reviews.llvm.org/D29438

Modified:
    compiler-rt/trunk/lib/xray/xray_AArch64.cc
    compiler-rt/trunk/lib/xray/xray_arm.cc
    compiler-rt/trunk/lib/xray/xray_emulate_tsc.h
    compiler-rt/trunk/lib/xray/xray_inmemory_log.cc
    compiler-rt/trunk/lib/xray/xray_x86_64.cc
    compiler-rt/trunk/lib/xray/xray_x86_64.h

Modified: compiler-rt/trunk/lib/xray/xray_AArch64.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/xray/xray_AArch64.cc?rev=293870&r1=293869&r2=293870&view=diff
==============================================================================
--- compiler-rt/trunk/lib/xray/xray_AArch64.cc (original)
+++ compiler-rt/trunk/lib/xray/xray_AArch64.cc Thu Feb  2 01:51:21 2017
@@ -120,4 +120,7 @@ bool patchFunctionTailExit(const bool En
   return patchSled(Enable, FuncId, Sled, __xray_FunctionTailExit);
 }
 
+// FIXME: Maybe implement this better?
+bool probeRequiredCPUFeatures() XRAY_NEVER_INSTRUMENT { return true; }
+
 } // namespace __xray

Modified: compiler-rt/trunk/lib/xray/xray_arm.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/xray/xray_arm.cc?rev=293870&r1=293869&r2=293870&view=diff
==============================================================================
--- compiler-rt/trunk/lib/xray/xray_arm.cc (original)
+++ compiler-rt/trunk/lib/xray/xray_arm.cc Thu Feb  2 01:51:21 2017
@@ -156,4 +156,7 @@ bool patchFunctionTailExit(const bool En
   return patchSled(Enable, FuncId, Sled, __xray_FunctionTailExit);
 }
 
+// FIXME: Maybe implement this better?
+bool probeRequiredCPUFeatures() XRAY_NEVER_INSTRUMENT { return true; }
+
 } // namespace __xray

Modified: compiler-rt/trunk/lib/xray/xray_emulate_tsc.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/xray/xray_emulate_tsc.h?rev=293870&r1=293869&r2=293870&view=diff
==============================================================================
--- compiler-rt/trunk/lib/xray/xray_emulate_tsc.h (original)
+++ compiler-rt/trunk/lib/xray/xray_emulate_tsc.h Thu Feb  2 01:51:21 2017
@@ -35,6 +35,9 @@ ALWAYS_INLINE uint64_t readTSC(uint8_t &
   CPU = 0;
   return TS.tv_sec * NanosecondsPerSecond + TS.tv_nsec;
 }
-}
+
+bool probeRequiredCPUFeatures();
+
+} // namespace __xray
 
 #endif // XRAY_EMULATE_TSC_H

Modified: compiler-rt/trunk/lib/xray/xray_inmemory_log.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/xray/xray_inmemory_log.cc?rev=293870&r1=293869&r2=293870&view=diff
==============================================================================
--- compiler-rt/trunk/lib/xray/xray_inmemory_log.cc (original)
+++ compiler-rt/trunk/lib/xray/xray_inmemory_log.cc Thu Feb  2 01:51:21 2017
@@ -138,6 +138,11 @@ void __xray_InMemoryRawLog(int32_t FuncI
 }
 
 static auto Unused = [] {
+  if (!probeRequiredCPUFeatures()) {
+    Report("Required CPU features missing for XRay instrumentation, not "
+           "installing instrumentation hooks.\n");
+    return false;
+  }
   if (flags()->xray_naive_log)
     __xray_set_handler(__xray_InMemoryRawLog);
   return true;

Modified: compiler-rt/trunk/lib/xray/xray_x86_64.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/xray/xray_x86_64.cc?rev=293870&r1=293869&r2=293870&view=diff
==============================================================================
--- compiler-rt/trunk/lib/xray/xray_x86_64.cc (original)
+++ compiler-rt/trunk/lib/xray/xray_x86_64.cc Thu Feb  2 01:51:21 2017
@@ -1,6 +1,8 @@
+#include "cpuid.h"
 #include "sanitizer_common/sanitizer_common.h"
 #include "xray_defs.h"
 #include "xray_interface_internal.h"
+
 #include <atomic>
 #include <cstdint>
 #include <errno.h>
@@ -61,8 +63,8 @@ uint64_t cycleFrequency() XRAY_NEVER_INS
                         &CPUFrequency)) {
     CPUFrequency *= 1000;
   } else if (readValueFromFile(
-      "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq",
-      &CPUFrequency)) {
+                 "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq",
+                 &CPUFrequency)) {
     CPUFrequency *= 1000;
   } else {
     Report("Unable to determine CPU frequency for TSC accounting.\n");
@@ -198,5 +200,21 @@ bool patchFunctionTailExit(const bool En
   }
   return true;
 }
+
+// We determine whether the CPU we're running on has the correct features we
+// need. In x86_64 this will be rdtscp support.
+bool probeRequiredCPUFeatures() XRAY_NEVER_INSTRUMENT {
+  unsigned int EAX, EBX, ECX, EDX;
+
+  // We check whether rdtscp support is enabled. According to the x86_64 manual,
+  // level should be set at 0x80000001, and we should have a look at bit 27 in
+  // EDX. That's 0x8000000 (or 1u << 26).
+  __get_cpuid(0x80000001, &EAX, &EBX, &ECX, &EDX);
+  if (!(EDX & (1u << 26))) {
+    Report("Missing rdtscp support.\n");
+    return false;
+  }
+  return true;
+}
 
 } // namespace __xray

Modified: compiler-rt/trunk/lib/xray/xray_x86_64.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/xray/xray_x86_64.h?rev=293870&r1=293869&r2=293870&view=diff
==============================================================================
--- compiler-rt/trunk/lib/xray/xray_x86_64.h (original)
+++ compiler-rt/trunk/lib/xray/xray_x86_64.h Thu Feb  2 01:51:21 2017
@@ -27,6 +27,9 @@ ALWAYS_INLINE uint64_t readTSC(uint8_t &
   CPU = LongCPU;
   return TSC;
 }
+
+bool probeRequiredCPUFeatures();
+
 } // namespace __xray
 
 #endif // XRAY_X86_64_H




More information about the llvm-commits mailing list