Fixing SystemNative_GetTimestampResolution for CLOCK_MONOTONIC (dotnet/corefx#37494)
authorTanner Gooding <tagoo@outlook.com>
Tue, 7 May 2019 20:17:47 +0000 (13:17 -0700)
committerGitHub <noreply@github.com>
Tue, 7 May 2019 20:17:47 +0000 (13:17 -0700)
Commit migrated from https://github.com/dotnet/corefx/commit/7890b88686e8592c19d1d055f229b699276e4f40

src/libraries/Native/Unix/System.Native/pal_time.c

index 90665e7..1b12667 100644 (file)
@@ -68,15 +68,13 @@ uint64_t SystemNative_GetTimestampResolution()
 
     return (SecondsToNanoSeconds * (uint64_t)(mtid.denom)) / (uint64_t)(mtid.numer);
 #else
-    struct timespec ts;
-
-    if (clock_getres(CLOCK_MONOTONIC, &ts) != 0)
-    {
-        return 0;
-    }
+    // clock_gettime() returns a result in terms of nanoseconds rather than a count. This
+    // means that we need to either always scale the result by the actual resolution (to
+    // get a count) or we need to say the resolution is in terms of nanoseconds. We prefer
+    // the latter since it allows the highest throughput and should minimize error propagated
+    // to the user.
 
-    uint64_t nanosecondsPerTick = ((uint64_t)(ts.tv_sec) * SecondsToNanoSeconds) + (uint64_t)(ts.tv_nsec);
-    return SecondsToNanoSeconds / nanosecondsPerTick;
+    return SecondsToNanoSeconds;
 #endif
 }