| 1 | #include "os/os.h" |
| 2 | |
| 3 | #include <windows.h> |
| 4 | |
| 5 | void os_clk_tck(long *clk_tck) |
| 6 | { |
| 7 | /* |
| 8 | * The timer resolution is variable on Windows. Try to query it |
| 9 | * or use 64 Hz, the clock frequency lower bound. See also |
| 10 | * https://carpediemsystems.co.uk/2019/07/18/windows-system-timer-granularity/. |
| 11 | */ |
| 12 | unsigned long minRes, maxRes, curRes; |
| 13 | HMODULE lib; |
| 14 | NTSTATUS NTAPI (*queryTimer) |
| 15 | (OUT PULONG MinimumResolution, |
| 16 | OUT PULONG MaximumResolution, |
| 17 | OUT PULONG CurrentResolution); |
| 18 | NTSTATUS NTAPI (*setTimer) |
| 19 | (IN ULONG DesiredResolution, |
| 20 | IN BOOLEAN SetResolution, |
| 21 | OUT PULONG CurrentResolution); |
| 22 | |
| 23 | if (!(lib = LoadLibrary(TEXT("ntdll.dll"))) || |
| 24 | !(queryTimer = (void *)GetProcAddress(lib, "NtQueryTimerResolution")) || |
| 25 | !(setTimer = (void *)GetProcAddress(lib, "NtSetTimerResolution"))) { |
| 26 | dprint(FD_HELPERTHREAD, |
| 27 | "Failed to load ntdll library, set to lower bound 64 Hz\n"); |
| 28 | *clk_tck = 64; |
| 29 | } else { |
| 30 | queryTimer(&minRes, &maxRes, &curRes); |
| 31 | dprint(FD_HELPERTHREAD, |
| 32 | "minRes = %lu, maxRes = %lu, curRes = %lu\n", |
| 33 | minRes, maxRes, curRes); |
| 34 | |
| 35 | /* Use maximum resolution for most accurate timestamps */ |
| 36 | setTimer(maxRes, 1, &curRes); |
| 37 | *clk_tck = (long) (10000000L / maxRes); |
| 38 | } |
| 39 | } |