Fix to get the correct value for Environment.TickCount in OSX
authorSridhar Periyasamy <sridhper@microsoft.com>
Wed, 22 Jul 2015 01:48:36 +0000 (18:48 -0700)
committerSridhar Periyasamy <sridhper@microsoft.com>
Wed, 22 Jul 2015 01:48:36 +0000 (18:48 -0700)
On linux monotonic clock API clock_gettime is available. But on OSX the PAL implementation falls back to gettimeofday. This always retured the time elapsed since Unix Epoch (1970/01/01). I fixed it by using the mach API clock_get_time on OSX to get the time elapsed since system start.

Fixes #2441
https://github.com/dotnet/corefx/issues/2441

src/pal/src/config.h.in
src/pal/src/configure.cmake
src/pal/src/misc/time.cpp

index 5de45b3..c267a39 100644 (file)
@@ -85,6 +85,7 @@
 #cmakedefine01 HAVE_WORKING_GETTIMEOFDAY
 #cmakedefine01 HAVE_WORKING_CLOCK_GETTIME
 #cmakedefine01 HAVE_CLOCK_MONOTONIC
+#cmakedefine01 HAVE_MACH_CLOCK_MONOTONIC
 #cmakedefine01 STATVFS64_PROTOTYPE_BROKEN
 #cmakedefine01 HAVE_MMAP_DEV_ZERO
 #cmakedefine01 MMAP_IGNORES_HINT
index e5d2dc0..dce3ef4 100644 (file)
@@ -328,6 +328,20 @@ int main()
 }" HAVE_CLOCK_MONOTONIC)
 check_cxx_source_runs("
 #include <stdlib.h>
+#include <mach/mach.h>
+#include <mach/clock.h>
+
+int main()
+{
+  int ret;
+  clock_serv_t clock;
+  mach_timespec_t mts;
+  host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &clock);
+  ret = clock_get_time(clock, &mts);
+  exit(ret);
+}" HAVE_MACH_CLOCK_MONOTONIC)
+check_cxx_source_runs("
+#include <stdlib.h>
 #include <sys/types.h>
 #include <sys/mman.h>
 #include <fcntl.h>
index 8ce7dfd..5ecaff4 100644 (file)
@@ -28,6 +28,11 @@ Abstract:
 #include <errno.h>
 #include <string.h>
 
+#if HAVE_MACH_CLOCK_MONOTONIC
+#include <mach/mach.h>
+#include <mach/clock.h>
+#endif
+
 SET_DEFAULT_DEBUG_CHANNEL(MISC);
 
 /*++
@@ -270,6 +275,21 @@ GetTickCount64()
         }
         retval = (ts.tv_sec * tccSecondsToMillieSeconds)+(ts.tv_nsec / tccMillieSecondsToNanoSeconds);
     }
+#elif HAVE_MACH_CLOCK_MONOTONIC
+    {
+        kern_return_t machRet;
+        clock_serv_t clock;
+        mach_timespec_t mts;
+
+        host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &clock);
+        if((machRet = clock_get_time(clock, &mts)) != KERN_SUCCESS)
+        {
+            ASSERT("clock_get_time() failed: %d\n", machRet);
+            goto EXIT;
+        }
+        mach_port_deallocate(mach_task_self(), clock);
+        retval = (mts.tv_sec * tccSecondsToMillieSeconds)+(mts.tv_nsec / tccMillieSecondsToNanoSeconds);
+    }
 #elif HAVE_GETHRTIME
     {
         retval = (ULONGLONG)(gethrtime() / tccMillieSecondsToNanoSeconds);