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>
This commit is contained in:
Krzysztof Antonowski 2026-02-02 22:19:19 +01:00 committed by Ryan Liptak
parent b71e593df4
commit 1cd3af43fd
2 changed files with 42 additions and 3 deletions

View file

@ -10849,9 +10849,40 @@ fn nowWindows(clock: Io.Clock) Io.Clock.Error!Io.Timestamp {
const result = (@as(u96, qpc) * scale) >> 32;
return .{ .nanoseconds = @intCast(result) };
},
.cpu_process,
.cpu_thread,
=> return error.UnsupportedClock,
.cpu_process => {
const handle = windows.GetCurrentProcess();
var times: windows.KERNEL_USER_TIMES = undefined;
// https://github.com/reactos/reactos/blob/master/ntoskrnl/ps/query.c#L442-L485
if (windows.ntdll.NtQueryInformationProcess(
handle,
windows.PROCESSINFOCLASS.Times,
&times,
@sizeOf(windows.KERNEL_USER_TIMES),
null,
) != .SUCCESS)
return error.Unexpected;
const sum = @as(i96, times.UserTime) + @as(i96, times.KernelTime);
return .{ .nanoseconds = sum * 100 };
},
.cpu_thread => {
const handle = windows.GetCurrentThread();
var times: windows.KERNEL_USER_TIMES = undefined;
// https://github.com/reactos/reactos/blob/master/ntoskrnl/ps/query.c#L2971-L3019
if (windows.ntdll.NtQueryInformationThread(
handle,
windows.THREADINFOCLASS.Times,
&times,
@sizeOf(windows.KERNEL_USER_TIMES),
null,
) != .SUCCESS)
return error.Unexpected;
const sum = @as(i96, times.UserTime) + @as(i96, times.KernelTime);
return .{ .nanoseconds = sum * 100 };
},
}
}

View file

@ -6191,6 +6191,14 @@ pub const PROCESS_BASIC_INFORMATION = extern struct {
InheritedFromUniqueProcessId: ULONG_PTR,
};
// https://github.com/reactos/reactos/blob/master/sdk/include/ndk/pstypes.h#L977-L983
pub const KERNEL_USER_TIMES = extern struct {
CreationTime: LARGE_INTEGER,
ExitTime: LARGE_INTEGER,
KernelTime: LARGE_INTEGER,
UserTime: LARGE_INTEGER,
};
pub const ReadMemoryError = error{
Unexpected,
};