diff options
| author | Tristan Ross <tristan.ross@midstall.com> | 2024-01-21 22:16:22 -0800 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2024-01-23 00:17:53 -0800 |
| commit | c0e0bb385cec80f3c370fb9e1c3b7116fae1bd9c (patch) | |
| tree | 763a7a74362fe90129e15f336bf2e40d0cb7a4f1 | |
| parent | 68ea1121fc73d35311b29d15b731a3e424867265 (diff) | |
| download | zig-c0e0bb385cec80f3c370fb9e1c3b7116fae1bd9c.tar.gz zig-c0e0bb385cec80f3c370fb9e1c3b7116fae1bd9c.zip | |
std.process: return u64 in totalSystemMemory
| -rw-r--r-- | lib/build_runner.zig | 6 | ||||
| -rw-r--r-- | lib/std/process.zig | 15 |
2 files changed, 12 insertions, 9 deletions
diff --git a/lib/build_runner.zig b/lib/build_runner.zig index 522b4249b5..54186685d6 100644 --- a/lib/build_runner.zig +++ b/lib/build_runner.zig @@ -95,7 +95,7 @@ pub fn main() !void { var install_prefix: ?[]const u8 = null; var dir_list = std.Build.DirList{}; var summary: ?Summary = null; - var max_rss: usize = 0; + var max_rss: u64 = 0; var skip_oom_steps: bool = false; var color: Color = .auto; var seed: u32 = 0; @@ -337,7 +337,7 @@ pub fn main() !void { }; if (run.max_rss == 0) { - run.max_rss = process.totalSystemMemory() catch std.math.maxInt(usize); + run.max_rss = process.totalSystemMemory() catch std.math.maxInt(u64); run.max_rss_is_default = true; } @@ -356,7 +356,7 @@ pub fn main() !void { } const Run = struct { - max_rss: usize, + max_rss: u64, max_rss_is_default: bool, max_rss_mutex: std.Thread.Mutex, skip_oom_steps: bool, diff --git a/lib/std/process.zig b/lib/std/process.zig index 0ab8a30d96..e51076d889 100644 --- a/lib/std/process.zig +++ b/lib/std/process.zig @@ -1522,8 +1522,11 @@ pub const TotalSystemMemoryError = error{ UnknownTotalSystemMemory, }; -/// Returns the total system memory, in bytes. -pub fn totalSystemMemory() TotalSystemMemoryError!usize { +/// Returns the total system memory, in bytes as a u64. +/// We return a u64 instead of usize due to PAE on ARM +/// and Linux's /proc/meminfo reporting more memory when +/// using QEMU user mode emulation. +pub fn totalSystemMemory() TotalSystemMemoryError!u64 { switch (builtin.os.tag) { .linux => { return totalSystemMemoryLinux() catch return error.UnknownTotalSystemMemory; @@ -1552,7 +1555,7 @@ pub fn totalSystemMemory() TotalSystemMemoryError!usize { else => return error.UnknownTotalSystemMemory, }; assert(physmem >= 0); - return @as(usize, @bitCast(physmem)); + return @as(u64, @bitCast(physmem)); }, .windows => { var sbi: std.os.windows.SYSTEM_BASIC_INFORMATION = undefined; @@ -1565,13 +1568,13 @@ pub fn totalSystemMemory() TotalSystemMemoryError!usize { if (rc != .SUCCESS) { return error.UnknownTotalSystemMemory; } - return @as(usize, sbi.NumberOfPhysicalPages) * sbi.PageSize; + return @as(u64, sbi.NumberOfPhysicalPages) * sbi.PageSize; }, else => return error.UnknownTotalSystemMemory, } } -fn totalSystemMemoryLinux() !usize { +fn totalSystemMemoryLinux() !u64 { var file = try std.fs.openFileAbsoluteZ("/proc/meminfo", .{}); defer file.close(); var buf: [50]u8 = undefined; @@ -1583,7 +1586,7 @@ fn totalSystemMemoryLinux() !usize { const int_text = it.next() orelse return error.Unexpected; const units = it.next() orelse return error.Unexpected; if (!std.mem.eql(u8, units, "kB")) return error.Unexpected; - const kilobytes = try std.fmt.parseInt(usize, int_text, 10); + const kilobytes = try std.fmt.parseInt(u64, int_text, 10); return kilobytes * 1024; } |
