diff --git a/lib/std/os/windows.zig b/lib/std/os/windows.zig index 1e6f717bb2..f8672bc58d 100644 --- a/lib/std/os/windows.zig +++ b/lib/std/os/windows.zig @@ -4476,6 +4476,34 @@ pub const MODULEENTRY32 = extern struct { szExePath: [MAX_PATH]CHAR, }; +pub const SYSTEM_INFORMATION_CLASS = enum(c_int) { + SystemBasicInformation = 0, + SystemPerformanceInformation = 2, + SystemTimeOfDayInformation = 3, + SystemProcessInformation = 5, + SystemProcessorPerformanceInformation = 8, + SystemInterruptInformation = 23, + SystemExceptionInformation = 33, + SystemRegistryQuotaInformation = 37, + SystemLookasideInformation = 45, + SystemCodeIntegrityInformation = 103, + SystemPolicyInformation = 134, +}; + +pub const SYSTEM_BASIC_INFORMATION = extern struct { + Reserved: ULONG, + TimerResolution: ULONG, + PageSize: ULONG, + NumberOfPhysicalPages: ULONG, + LowestPhysicalPageNumber: ULONG, + HighestPhysicalPageNumber: ULONG, + AllocationGranularity: ULONG, + MinimumUserModeAddress: ULONG_PTR, + MaximumUserModeAddress: ULONG_PTR, + ActiveProcessorsAffinityMask: KAFFINITY, + NumberOfProcessors: UCHAR, +}; + pub const THREADINFOCLASS = enum(c_int) { ThreadBasicInformation, ThreadTimes, diff --git a/lib/std/os/windows/ntdll.zig b/lib/std/os/windows/ntdll.zig index a8af39aa4d..ad0e6c70ad 100644 --- a/lib/std/os/windows/ntdll.zig +++ b/lib/std/os/windows/ntdll.zig @@ -31,6 +31,7 @@ const UNWIND_HISTORY_TABLE = windows.UNWIND_HISTORY_TABLE; const RUNTIME_FUNCTION = windows.RUNTIME_FUNCTION; const KNONVOLATILE_CONTEXT_POINTERS = windows.KNONVOLATILE_CONTEXT_POINTERS; const EXCEPTION_ROUTINE = windows.EXCEPTION_ROUTINE; +const SYSTEM_INFORMATION_CLASS = windows.SYSTEM_INFORMATION_CLASS; const THREADINFOCLASS = windows.THREADINFOCLASS; const PROCESSINFOCLASS = windows.PROCESSINFOCLASS; const LPVOID = windows.LPVOID; @@ -51,6 +52,14 @@ pub extern "ntdll" fn NtQueryInformationThread( ThreadInformationLength: ULONG, ReturnLength: ?*ULONG, ) callconv(WINAPI) NTSTATUS; + +pub extern "ntdll" fn NtQuerySystemInformation( + SystemInformationClass: SYSTEM_INFORMATION_CLASS, + SystemInformation: PVOID, + SystemInformationLength: ULONG, + ReturnLength: ?*ULONG, +) callconv(WINAPI) NTSTATUS; + pub extern "ntdll" fn NtSetInformationThread( ThreadHandle: HANDLE, ThreadInformationClass: THREADINFOCLASS, diff --git a/lib/std/process.zig b/lib/std/process.zig index 096f1f666d..c2d5b8a000 100644 --- a/lib/std/process.zig +++ b/lib/std/process.zig @@ -1163,10 +1163,17 @@ pub fn totalSystemMemory() TotalSystemMemoryError!usize { return totalSystemMemoryLinux() catch return error.UnknownTotalSystemMemory; }, .windows => { - var kilobytes: std.os.windows.ULONGLONG = undefined; - if (std.os.windows.kernel32.GetPhysicallyInstalledSystemMemory(&kilobytes) != std.os.windows.TRUE) + var sbi: std.os.windows.SYSTEM_BASIC_INFORMATION = undefined; + const rc = std.os.windows.ntdll.NtQuerySystemInformation( + .SystemBasicInformation, + &sbi, + @sizeOf(std.os.windows.SYSTEM_BASIC_INFORMATION), + null, + ); + if (rc != .SUCCESS) { return error.UnknownTotalSystemMemory; - return kilobytes * 1024; + } + return @as(usize, sbi.NumberOfPhysicalPages) * sbi.PageSize; }, else => return error.UnknownTotalSystemMemory, }