From e1710939ce783e01093ba762b3571f6473d23f90 Mon Sep 17 00:00:00 2001 From: Cameron Youell Date: Wed, 30 Aug 2023 16:57:14 +1000 Subject: [PATCH] AK: Implement now_time_from_clock for Windows Monotonic uses QueryPerformanceCounter, while realtime uses GetSystemTimeAsFileTime. These should approximate clock_gettime fairly accurately. The QPC implementation only grabs microseconds, but if we have actual use cases for nanos, we can bump that up. Co-authored-by: Andrew Kaster --- AK/Time.cpp | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++- AK/Time.h | 6 +++++- 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/AK/Time.cpp b/AK/Time.cpp index d016e930d3a..16a1620f9ac 100644 --- a/AK/Time.cpp +++ b/AK/Time.cpp @@ -6,8 +6,13 @@ #include #include -#include + #include +#if defined(AK_OS_WINDOWS) +# include +#else +# include +#endif namespace AK { @@ -201,12 +206,62 @@ Duration Duration::from_half_sanitized(i64 seconds, i32 extra_seconds, u32 nanos } namespace { +#if defined(AK_OS_WINDOWS) +# define CLOCK_REALTIME 0 +# define CLOCK_MONOTONIC 1 + +// Ref https://stackoverflow.com/a/51974214 +Duration now_time_from_filetime() +{ + FILETIME ft {}; + GetSystemTimeAsFileTime(&ft); + + // Units: 1 LSB == 100 ns + ULARGE_INTEGER hundreds_of_nanos { + .LowPart = ft.dwLowDateTime, + .HighPart = ft.dwHighDateTime + }; + + constexpr u64 num_hundred_nanos_per_sec = 1000ULL * 1000ULL * 10ULL; + constexpr u64 seconds_from_jan_1601_to_jan_1970 = 11644473600ULL; + + // To convert to Unix Epoch, subtract the number of hundred nanosecond intervals from Jan 1, 1601 to Jan 1, 1970. + hundreds_of_nanos.QuadPart -= (seconds_from_jan_1601_to_jan_1970 * num_hundred_nanos_per_sec); + + return Duration::from_nanoseconds(hundreds_of_nanos.QuadPart * 100); +} + +Duration now_time_from_query_performance_counter() +{ + static LARGE_INTEGER ticks_per_second; + // FIXME: Limit to microseconds for now, but could probably use nanos? + static float ticks_per_microsecond; + if (ticks_per_second.QuadPart == 0) { + QueryPerformanceFrequency(&ticks_per_second); + VERIFY(ticks_per_second.QuadPart != 0); + ticks_per_microsecond = static_cast(ticks_per_second.QuadPart) / 1'000'000.0F; + } + + LARGE_INTEGER now_time {}; + QueryPerformanceCounter(&now_time); + return Duration::from_microseconds(static_cast(now_time.QuadPart / ticks_per_microsecond)); +} + +Duration now_time_from_clock(int clock_id) +{ + if (clock_id == CLOCK_REALTIME) + return now_time_from_filetime(); + return now_time_from_query_performance_counter(); +} +#else static Duration now_time_from_clock(clockid_t clock_id) { timespec now_spec {}; ::clock_gettime(clock_id, &now_spec); return Duration::from_timespec(now_spec); } +#endif + } MonotonicTime MonotonicTime::now() diff --git a/AK/Time.h b/AK/Time.h index 89c649d8d1f..6e7ce50067d 100644 --- a/AK/Time.h +++ b/AK/Time.h @@ -12,7 +12,11 @@ #include #include #include -#include +#if defined(AK_OS_WINDOWS) +# include +#else +# include +#endif #include namespace AK {