From 487f296902096ca12d79d3c7cc09ad69c99ac447 Mon Sep 17 00:00:00 2001 From: wutno Date: Tue, 17 Oct 2023 18:31:10 -0400 Subject: [PATCH] winapi: Squash me --- lib/winapi/profiling.c | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/lib/winapi/profiling.c b/lib/winapi/profiling.c index 9df232c12..594c4c0b3 100644 --- a/lib/winapi/profiling.c +++ b/lib/winapi/profiling.c @@ -1,7 +1,7 @@ // SPDX-License-Identifier: MIT // SPDX-FileCopyrightText: 2019 Stefan Schmidt - +#define USE_RDTSC_FOR_FREQ #include #ifdef USE_RDTSC_FOR_FREQ #include @@ -11,33 +11,32 @@ #include #ifdef USE_RDTSC_FOR_FREQ -static LARGE_INTEGER frequency = {0, 0}; +static LARGE_INTEGER frequency = {{0, 0}}; static void __attribute__((constructor)) PrimeQueryPerformanceFrequency () { - #define AVG_SET 2 - ULARGE_INTEGER f_rdtsc, avg = {0, 0}, s_rdtsc; + ULARGE_INTEGER f_rdtsc = {{0, 0}}, s_rdtsc = {{0, 0}}; ULONG f_ticks = 0, s_ticks = 0; - Sleep(500); + KeEnterCriticalRegion(); + + // The values generated after launching aren't accurate, give it time to increment... + Sleep(700); - for (int i = 0; i < AVG_SET; i++) { - // If we call rdtsc too fast we'll end up with div by 0 - Sleep(200); + f_rdtsc.QuadPart = __rdtsc(); + f_ticks = KeTickCount; - s_rdtsc.QuadPart = __rdtsc(); - s_ticks = KeTickCount; + Sleep(200); - s_rdtsc.QuadPart -= f_rdtsc.QuadPart; - s_rdtsc.QuadPart /= s_ticks - f_ticks; + s_rdtsc.QuadPart = __rdtsc(); + s_ticks = KeTickCount; - f_rdtsc.QuadPart = __rdtsc(); - f_ticks = KeTickCount; + s_rdtsc.QuadPart -= f_rdtsc.QuadPart; + s_rdtsc.QuadPart /= s_ticks - f_ticks; - // Skip the first result as invalid - if (i) - avg.QuadPart += s_rdtsc.QuadPart; - } - frequency.QuadPart = avg.QuadPart / (AVG_SET - 1) * 1000LL; + frequency.QuadPart = s_rdtsc.QuadPart; + frequency.QuadPart *= 1000LL; + + KeLeaveCriticalRegion(); } #endif @@ -59,4 +58,4 @@ BOOL QueryPerformanceFrequency (LARGE_INTEGER *lpFrequency) lpFrequency->QuadPart = 733333333; #endif return TRUE; -} \ No newline at end of file +}