Query Windows clock frequency and use reported max
[fio.git] / os / windows / dlls.c
CommitLineData
3d0d549a
BP
1#include "os/os.h"
2
3#include <windows.h>
4
5void 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 FARPROC queryTimer;
15 FARPROC setTimer;
16
17 if (!(lib = LoadLibrary(TEXT("ntdll.dll"))) ||
18 !(queryTimer = GetProcAddress(lib, "NtQueryTimerResolution")) ||
19 !(setTimer = GetProcAddress(lib, "NtSetTimerResolution"))) {
20 dprint(FD_HELPERTHREAD,
21 "Failed to load ntdll library, set to lower bound 64 Hz\n");
22 *clk_tck = 64;
23 } else {
24 queryTimer(&minRes, &maxRes, &curRes);
25 dprint(FD_HELPERTHREAD,
26 "minRes = %lu, maxRes = %lu, curRes = %lu\n",
27 minRes, maxRes, curRes);
28
29 /* Use maximum resolution for most accurate timestamps */
30 setTimer(maxRes, 1, &curRes);
31 *clk_tck = (long) (10000000L / maxRes);
32 }
33}