authorgravatar for krzysztofantonowski@proton.meKrzysztof Antonowski <krzysztofantonowski@proton.me> 2026-02-02 22:19:19+01:00
committergravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2026-02-02 22:19:19+01:00
log1cd3af43fd74a8481b35d58c351ae056c1cba362
tree93830df7ae16f96b29c68ed58d06571dae370d3e
parentb71e593df442f04eb21d18fc829fb50c29989014

std.Io.Threaded: implement CPU-based clocks on Windows (#31093)

Reviewed-on: https://codeberg.org/ziglang/zig/pulls/31093 Co-authored-by: Krzysztof Antonowski <krzysztofantonowski@proton.me> Co-committed-by: Krzysztof Antonowski <krzysztofantonowski@proton.me>

2 files changed, 42 insertions(+), 3 deletions(-)

lib/std/Io/Threaded.zig+34-3
......@@ -10849,9 +10849,40 @@ fn nowWindows(clock: Io.Clock) Io.Clock.Error!Io.Timestamp {
1084910849 const result = (@as(u96, qpc) * scale) >> 32;
1085010850 return .{ .nanoseconds = @intCast(result) };
1085110851 },
10852 .cpu_process,
10853 .cpu_thread,
10854 => return error.UnsupportedClock,
10852 .cpu_process => {
10853 const handle = windows.GetCurrentProcess();
10854 var times: windows.KERNEL_USER_TIMES = undefined;
10855
10856 // https://github.com/reactos/reactos/blob/master/ntoskrnl/ps/query.c#L442-L485
10857 if (windows.ntdll.NtQueryInformationProcess(
10858 handle,
10859 windows.PROCESSINFOCLASS.Times,
10860 &times,
10861 @sizeOf(windows.KERNEL_USER_TIMES),
10862 null,
10863 ) != .SUCCESS)
10864 return error.Unexpected;
10865
10866 const sum = @as(i96, times.UserTime) + @as(i96, times.KernelTime);
10867 return .{ .nanoseconds = sum * 100 };
10868 },
10869 .cpu_thread => {
10870 const handle = windows.GetCurrentThread();
10871 var times: windows.KERNEL_USER_TIMES = undefined;
10872
10873 // https://github.com/reactos/reactos/blob/master/ntoskrnl/ps/query.c#L2971-L3019
10874 if (windows.ntdll.NtQueryInformationThread(
10875 handle,
10876 windows.THREADINFOCLASS.Times,
10877 &times,
10878 @sizeOf(windows.KERNEL_USER_TIMES),
10879 null,
10880 ) != .SUCCESS)
10881 return error.Unexpected;
10882
10883 const sum = @as(i96, times.UserTime) + @as(i96, times.KernelTime);
10884 return .{ .nanoseconds = sum * 100 };
10885 },
1085510886 }
1085610887}
1085710888
lib/std/os/windows.zig+8
......@@ -6191,6 +6191,14 @@ pub const PROCESS_BASIC_INFORMATION = extern struct {
61916191 InheritedFromUniqueProcessId: ULONG_PTR,
61926192};
61936193
6194// https://github.com/reactos/reactos/blob/master/sdk/include/ndk/pstypes.h#L977-L983
6195pub const KERNEL_USER_TIMES = extern struct {
6196 CreationTime: LARGE_INTEGER,
6197 ExitTime: LARGE_INTEGER,
6198 KernelTime: LARGE_INTEGER,
6199 UserTime: LARGE_INTEGER,
6200};
6201
61946202pub const ReadMemoryError = error{
61956203 Unexpected,
61966204};