[PATCH] D14861: [tsan] Make tests that use CLOCK_MONOTONIC portable

Kuba Brecka via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 20 01:25:41 PST 2015


kubabrecka created this revision.
kubabrecka added reviewers: dvyukov, kcc, glider, samsonov.
kubabrecka added subscribers: llvm-commits, zaks.anna, ismailp.

Several tests rely on CLOCK_MONOTONIC, which doesn't exist on OS X.  This patch fixes these tests by either disabling them (in case of `cond_version.c` which doesn't make sense on OS X), or by porting the test to also work on OS X.

http://reviews.llvm.org/D14861

Files:
  test/tsan/cond_version.c
  test/tsan/real_deadlock_detector_stress_test.cc
  test/tsan/setuid2.c
  test/tsan/test.h

Index: test/tsan/test.h
===================================================================
--- test/tsan/test.h
+++ test/tsan/test.h
@@ -6,6 +6,10 @@
 #include <stddef.h>
 #include <sched.h>
 
+#ifdef __APPLE__
+#include <mach/mach_time.h>
+#endif
+
 // TSan-invisible barrier.
 // Tests use it to establish necessary execution order in a way that does not
 // interfere with tsan (does not establish synchronization between threads).
@@ -60,3 +64,17 @@
   fprintf(stderr, format, (unsigned long) address);
 #endif
 }
+
+#ifdef __APPLE__
+unsigned long long monotonic_clock_ns() {
+  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;
+}
+#else
+unsigned long long monotonic_clock_ns() {
+  struct timespec t;
+  clock_gettime(CLOCK_MONOTONIC, &t);
+  return (unsigned long long)t.tv_sec * 1000000000ull + t.tv_nsec;
+}
+#endif
Index: test/tsan/setuid2.c
===================================================================
--- test/tsan/setuid2.c
+++ test/tsan/setuid2.c
@@ -7,11 +7,11 @@
 // Test that setuid call works in presence of stoptheworld.
 
 int main() {
-  struct timespec tp0, tp1;
-  clock_gettime(CLOCK_MONOTONIC, &tp0);
-  clock_gettime(CLOCK_MONOTONIC, &tp1);
-  while (tp1.tv_sec - tp0.tv_sec < 3) {
-    clock_gettime(CLOCK_MONOTONIC, &tp1);
+  unsigned long long tp0, tp1;
+  tp0 = monotonic_clock_ns();
+  tp1 = monotonic_clock_ns();
+  while (tp1 - tp0 < 3 * 1000000000ull) {
+    tp1 = monotonic_clock_ns();
     setuid(0);
   }
   fprintf(stderr, "DONE\n");
Index: test/tsan/real_deadlock_detector_stress_test.cc
===================================================================
--- test/tsan/real_deadlock_detector_stress_test.cc
+++ test/tsan/real_deadlock_detector_stress_test.cc
@@ -8,6 +8,7 @@
 #include <errno.h>
 #include <vector>
 #include <algorithm>
+#include <sys/time.h>
 
 const int kThreads = 4;
 const int kMutexes = 16 << 10;
@@ -165,9 +166,9 @@
 }
 
 int main() {
-  timespec ts;
-  clock_gettime(CLOCK_MONOTONIC, &ts);
-  unsigned s = (unsigned)ts.tv_nsec;
+  struct timeval tv;
+  gettimeofday(&tv, NULL);
+  unsigned s = tv.tv_sec + tv.tv_usec;
   fprintf(stderr, "seed %d\n", s);
   srand(s);
   for (int i = 0; i < kMutexes; i++)
Index: test/tsan/cond_version.c
===================================================================
--- test/tsan/cond_version.c
+++ test/tsan/cond_version.c
@@ -3,6 +3,9 @@
 // previously there were issues with versioned symbols.
 // CHECK: OK
 
+// OS X doesn't have pthread_condattr_setclock.
+// UNSUPPORTED: darwin
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <pthread.h>


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D14861.40748.patch
Type: text/x-patch
Size: 2708 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20151120/6367af34/attachment.bin>


More information about the llvm-commits mailing list