[PATCH] D43981: [XRay] [compiler-rt] [macosx] Add getTSCFrequency implementation for macOS.

Nicholas Levin via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 1 18:28:09 PST 2018


nglevin updated this revision to Diff 136664.
nglevin added a comment.

Correct Mac sysctl name.


Repository:
  rCRT Compiler Runtime

https://reviews.llvm.org/D43981

Files:
  lib/xray/xray_tsc.h
  lib/xray/xray_x86_64.cc


Index: lib/xray/xray_x86_64.cc
===================================================================
--- lib/xray/xray_x86_64.cc
+++ lib/xray/xray_x86_64.cc
@@ -3,7 +3,7 @@
 #include "xray_defs.h"
 #include "xray_interface_internal.h"
 
-#if SANITIZER_FREEBSD || SANITIZER_NETBSD
+#if SANITIZER_FREEBSD || SANITIZER_NETBSD || SANITIZER_MAC
 #include <sys/types.h>
 #include <sys/sysctl.h>
 #endif
@@ -77,12 +77,16 @@
   }
   return TSCFrequency == -1 ? 0 : static_cast<uint64_t>(TSCFrequency);
 }
-#elif SANITIZER_FREEBSD || SANITIZER_NETBSD
+#elif SANITIZER_FREEBSD || SANITIZER_NETBSD || SANITIZER_MAC
 uint64_t getTSCFrequency() XRAY_NEVER_INSTRUMENT {
     long long TSCFrequency = -1;
     size_t tscfreqsz = sizeof(TSCFrequency);
-
-    if (sysctlbyname("machdep.tsc_freq", &TSCFrequency, &tscfreqsz,
+#ifdef SANITIZER_MAC
+    const char* sysctltscname = "machdep.tsc.frequency";
+#else
+    const char* sysctltscname = "machdep.tsc_freq";
+#endif
+    if (sysctlbyname(sysctltscname, &TSCFrequency, &tscfreqsz,
         NULL, 0) != -1) {
         return static_cast<uint64_t>(TSCFrequency);
     } else {
Index: lib/xray/xray_tsc.h
===================================================================
--- lib/xray/xray_tsc.h
+++ lib/xray/xray_tsc.h
@@ -37,13 +37,29 @@
 #include "xray_defs.h"
 #include <cerrno>
 #include <cstdint>
+#if SANITIZER_MAC
+#include <mach/mach_time.h>
+#else
 #include <time.h>
+#endif
 
 namespace __xray {
 
 inline bool probeRequiredCPUFeatures() XRAY_NEVER_INSTRUMENT { return true; }
 
 ALWAYS_INLINE uint64_t readTSC(uint8_t &CPU) XRAY_NEVER_INSTRUMENT {
+#if SANITIZER_MAC
+  static const mach_timebase_info_data_t TI = [] {
+    mach_timebase_info_data_t LocalTI;
+    mach_timebase_info(&LocalTI);
+    return LocalTI;
+  }();
+  uint64_t currentTime = mach_absolute_time();
+  currentTime *= TI.numer;
+  currentTime /= TI.denom;
+  CPU = 0;
+  return currentTime;
+#else
   timespec TS;
   int result = clock_gettime(CLOCK_REALTIME, &TS);
   if (result != 0) {
@@ -53,6 +69,7 @@
   }
   CPU = 0;
   return TS.tv_sec * NanosecondsPerSecond + TS.tv_nsec;
+#endif
 }
 
 inline uint64_t getTSCFrequency() XRAY_NEVER_INSTRUMENT {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D43981.136664.patch
Type: text/x-patch
Size: 2169 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180302/6757a726/attachment.bin>


More information about the llvm-commits mailing list