[compiler-rt] r324847 - [sanitizer] Implement NanoTime() on Darwin

Kuba Mracek via llvm-commits llvm-commits at lists.llvm.org
Sun Feb 11 11:25:34 PST 2018


Author: kuba.brecka
Date: Sun Feb 11 11:25:34 2018
New Revision: 324847

URL: http://llvm.org/viewvc/llvm-project?rev=324847&view=rev
Log:
[sanitizer] Implement NanoTime() on Darwin

Currently NanoTime() on Darwin is unimplemented and always returns 0. Looks like there's quite a few things broken because of that (TSan periodic memory flush, ASan allocator releasing pages back to the OS). Let's fix that.

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


Modified:
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_mac.cc

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_mac.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_mac.cc?rev=324847&r1=324846&r2=324847&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_mac.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_mac.cc Sun Feb 11 11:25:34 2018
@@ -59,6 +59,7 @@ extern "C" {
 #include <libkern/OSAtomic.h>
 #include <mach-o/dyld.h>
 #include <mach/mach.h>
+#include <mach/mach_time.h>
 #include <mach/vm_statistics.h>
 #include <pthread.h>
 #include <sched.h>
@@ -399,11 +400,17 @@ void BlockingMutex::CheckLocked() {
 }
 
 u64 NanoTime() {
-  return 0;
+  timeval tv;
+  internal_memset(&tv, 0, sizeof(tv));
+  gettimeofday(&tv, 0);
+  return (u64)tv.tv_sec * 1000*1000*1000 + tv.tv_usec * 1000;
 }
 
+// This needs to be called during initialization to avoid being racy.
 u64 MonotonicNanoTime() {
-  return 0;
+  static mach_timebase_info_data_t timebase_info;
+  if (timebase_info.denom == 0) mach_timebase_info(&timebase_info);
+  return (mach_absolute_time() * timebase_info.numer) / timebase_info.denom;
 }
 
 uptr GetTlsSize() {
@@ -710,6 +717,9 @@ bool DyldNeedsEnvVariable() {
 }
 
 void MaybeReexec() {
+  // FIXME: This should really live in some "InitializePlatform" method.
+  MonotonicNanoTime();
+
   if (ReexecDisabled()) return;
 
   // Make sure the dynamic runtime library is preloaded so that the




More information about the llvm-commits mailing list