From 33a33edeb05e13648d384cf83e5554f30a495715 Mon Sep 17 00:00:00 2001 From: Jeremy Sinclair <4016293+snickler@users.noreply.github.com> Date: Mon, 12 Oct 2020 23:06:58 -0400 Subject: [PATCH 1/3] Replaced mach_absolute_time instances with clock_gettime_nsec_np --- .../Native/Unix/Common/pal_config.h.in | 2 +- .../Native/Unix/System.Native/pal_time.c | 24 +++++++------------ src/libraries/Native/Unix/configure.cmake | 6 ++--- 3 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/libraries/Native/Unix/Common/pal_config.h.in b/src/libraries/Native/Unix/Common/pal_config.h.in index d889d1045a1c83..856bd637ee468c 100644 --- a/src/libraries/Native/Unix/Common/pal_config.h.in +++ b/src/libraries/Native/Unix/Common/pal_config.h.in @@ -85,7 +85,7 @@ #cmakedefine01 HAVE_INOTIFY #cmakedefine01 HAVE_CLOCK_MONOTONIC #cmakedefine01 HAVE_CLOCK_REALTIME -#cmakedefine01 HAVE_MACH_ABSOLUTE_TIME +#cmakedefine01 HAVE_CLOCK_GETTIME_NSEC_NP #cmakedefine01 HAVE_TCP_H_TCPSTATE_ENUM #cmakedefine01 HAVE_TCP_FSM_H #cmakedefine01 HAVE_GSSFW_HEADERS diff --git a/src/libraries/Native/Unix/System.Native/pal_time.c b/src/libraries/Native/Unix/System.Native/pal_time.c index decfbadea8db47..d8ec62d8ca3bde 100644 --- a/src/libraries/Native/Unix/System.Native/pal_time.c +++ b/src/libraries/Native/Unix/System.Native/pal_time.c @@ -12,8 +12,8 @@ #include #include #include -#if HAVE_MACH_ABSOLUTE_TIME -#include +#if HAVE_CLOCK_GETTIME_NSEC_NP +#include #endif enum @@ -53,20 +53,12 @@ int32_t SystemNative_UTimensat(const char* path, TimeSpec* times) // that is "nanoseconds per tick" in which case we need to scale before returning. uint64_t SystemNative_GetTimestampResolution() { -#if HAVE_MACH_ABSOLUTE_TIME - mach_timebase_info_data_t mtid; - - if (mach_timebase_info(&mtid) != KERN_SUCCESS) +#if HAVE_CLOCK_GETTIME_NSEC_NP + if (clock_gettime_nsec_np(CLOCK_UPTIME_RAW) == 0) { return 0; } - - // (numer / denom) gives you the nanoseconds per tick, so the below code - // computes the number of ticks per second. We explicitly do the multiplication - // first in order to help minimize the error that is produced by integer division. - - return (SecondsToNanoSeconds * (uint64_t)(mtid.denom)) / (uint64_t)(mtid.numer); -#else +#endif // 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 @@ -74,13 +66,13 @@ uint64_t SystemNative_GetTimestampResolution() // to the user. return SecondsToNanoSeconds; -#endif + } uint64_t SystemNative_GetTimestamp() { -#if HAVE_MACH_ABSOLUTE_TIME - return mach_absolute_time(); +#if HAVE_CLOCK_GETTIME_NSEC_NP + return clock_gettime_nsec_np(CLOCK_UPTIME_RAW); #else struct timespec ts; diff --git a/src/libraries/Native/Unix/configure.cmake b/src/libraries/Native/Unix/configure.cmake index 57e356f8239dfe..6cb5616b6ab444 100644 --- a/src/libraries/Native/Unix/configure.cmake +++ b/src/libraries/Native/Unix/configure.cmake @@ -563,9 +563,9 @@ else() endif() check_symbol_exists( - mach_absolute_time - mach/mach_time.h - HAVE_MACH_ABSOLUTE_TIME) + clock_gettime_nsec_np + time.h + HAVE_CLOCK_GETTIME_NSEC_NP) check_symbol_exists( futimes From d472d469413e08c25fff5d613da18b411c3970fd Mon Sep 17 00:00:00 2001 From: Jeremy Sinclair <4016293+snickler@users.noreply.github.com> Date: Tue, 13 Oct 2020 21:20:30 -0400 Subject: [PATCH 2/3] Removed GetTimestampResolution method and replaced with 10^9 --- .../System.Native/Interop.GetTimestamp.cs | 3 --- .../Native/Unix/System.Native/pal_time.c | 27 +------------------ .../Native/Unix/System.Native/pal_time.h | 5 ---- .../src/System/Diagnostics/Stopwatch.Unix.cs | 3 ++- 4 files changed, 3 insertions(+), 35 deletions(-) diff --git a/src/libraries/Common/src/Interop/Unix/System.Native/Interop.GetTimestamp.cs b/src/libraries/Common/src/Interop/Unix/System.Native/Interop.GetTimestamp.cs index cd18a3a385dc68..a374e485f9cce6 100644 --- a/src/libraries/Common/src/Interop/Unix/System.Native/Interop.GetTimestamp.cs +++ b/src/libraries/Common/src/Interop/Unix/System.Native/Interop.GetTimestamp.cs @@ -7,9 +7,6 @@ internal static partial class Interop { internal static partial class Sys { - [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_GetTimestampResolution", ExactSpelling = true)] - internal static extern ulong GetTimestampResolution(); - [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_GetTimestamp", ExactSpelling = true)] [SuppressGCTransition] internal static extern ulong GetTimestamp(); diff --git a/src/libraries/Native/Unix/System.Native/pal_time.c b/src/libraries/Native/Unix/System.Native/pal_time.c index d8ec62d8ca3bde..b299430843a922 100644 --- a/src/libraries/Native/Unix/System.Native/pal_time.c +++ b/src/libraries/Native/Unix/System.Native/pal_time.c @@ -47,28 +47,6 @@ int32_t SystemNative_UTimensat(const char* path, TimeSpec* times) return result; } -// Gets the number of "ticks per second" of the underlying monotonic timer. -// -// On most Unix platforms, the methods that query the resolution return a value -// that is "nanoseconds per tick" in which case we need to scale before returning. -uint64_t SystemNative_GetTimestampResolution() -{ -#if HAVE_CLOCK_GETTIME_NSEC_NP - if (clock_gettime_nsec_np(CLOCK_UPTIME_RAW) == 0) - { - return 0; - } -#endif - // 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. - - return SecondsToNanoSeconds; - -} - uint64_t SystemNative_GetTimestamp() { #if HAVE_CLOCK_GETTIME_NSEC_NP @@ -105,10 +83,7 @@ int32_t SystemNative_GetCpuUtilization(ProcessCpuInformation* previousCpuInfo) ((uint64_t)(resUsage.ru_utime.tv_usec) * MicroSecondsToNanoSeconds); } - uint64_t resolution = SystemNative_GetTimestampResolution(); - uint64_t timestamp = SystemNative_GetTimestamp(); - - uint64_t currentTime = (uint64_t)((double)timestamp * ((double)SecondsToNanoSeconds / (double)resolution)); + uint64_t currentTime = SystemNative_GetTimestamp() uint64_t lastRecordedCurrentTime = previousCpuInfo->lastRecordedCurrentTime; uint64_t lastRecordedKernelTime = previousCpuInfo->lastRecordedKernelTime; diff --git a/src/libraries/Native/Unix/System.Native/pal_time.h b/src/libraries/Native/Unix/System.Native/pal_time.h index 44030dff8a094c..12fe5001334183 100644 --- a/src/libraries/Native/Unix/System.Native/pal_time.h +++ b/src/libraries/Native/Unix/System.Native/pal_time.h @@ -27,11 +27,6 @@ typedef struct ProcessCpuInformation */ PALEXPORT int32_t SystemNative_UTimensat(const char* path, TimeSpec* times); -/** - * Gets the resolution of the timestamp, in counts per second. - */ -PALEXPORT uint64_t SystemNative_GetTimestampResolution(void); - /** * Gets a high-resolution timestamp that can be used for time-interval measurements. */ diff --git a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Stopwatch.Unix.cs b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Stopwatch.Unix.cs index 193886e4f10d5d..487923148aa0df 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Stopwatch.Unix.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Stopwatch.Unix.cs @@ -7,7 +7,8 @@ public partial class Stopwatch { private static long QueryPerformanceFrequency() { - return (long)Interop.Sys.GetTimestampResolution(); + const long SecondsToNanoSeconds = 1000000000; + return SecondsToNanoSeconds; } private static long QueryPerformanceCounter() From 40e582397309f54200bde780b609ff50e2d482fe Mon Sep 17 00:00:00 2001 From: Jan Kotas Date: Tue, 13 Oct 2020 18:33:02 -0700 Subject: [PATCH 3/3] Update src/libraries/Native/Unix/System.Native/pal_time.c --- src/libraries/Native/Unix/System.Native/pal_time.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/Native/Unix/System.Native/pal_time.c b/src/libraries/Native/Unix/System.Native/pal_time.c index b299430843a922..56e23138a7b77e 100644 --- a/src/libraries/Native/Unix/System.Native/pal_time.c +++ b/src/libraries/Native/Unix/System.Native/pal_time.c @@ -83,7 +83,7 @@ int32_t SystemNative_GetCpuUtilization(ProcessCpuInformation* previousCpuInfo) ((uint64_t)(resUsage.ru_utime.tv_usec) * MicroSecondsToNanoSeconds); } - uint64_t currentTime = SystemNative_GetTimestamp() + uint64_t currentTime = SystemNative_GetTimestamp(); uint64_t lastRecordedCurrentTime = previousCpuInfo->lastRecordedCurrentTime; uint64_t lastRecordedKernelTime = previousCpuInfo->lastRecordedKernelTime;