authorgravatar for tristan.ross@midstall.comTristan Ross <tristan.ross@midstall.com> 2024-01-21 22:16:22-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-01-23 00:17:53-08:00
logc0e0bb385cec80f3c370fb9e1c3b7116fae1bd9c
tree763a7a74362fe90129e15f336bf2e40d0cb7a4f1
parent68ea1121fc73d35311b29d15b731a3e424867265

std.process: return u64 in totalSystemMemory


2 files changed, 12 insertions(+), 9 deletions(-)

lib/build_runner.zig+3-3
......@@ -95,7 +95,7 @@ pub fn main() !void {
9595 var install_prefix: ?[]const u8 = null;
9696 var dir_list = std.Build.DirList{};
9797 var summary: ?Summary = null;
98 var max_rss: usize = 0;
98 var max_rss: u64 = 0;
9999 var skip_oom_steps: bool = false;
100100 var color: Color = .auto;
101101 var seed: u32 = 0;
......@@ -337,7 +337,7 @@ pub fn main() !void {
337337 };
338338
339339 if (run.max_rss == 0) {
340 run.max_rss = process.totalSystemMemory() catch std.math.maxInt(usize);
340 run.max_rss = process.totalSystemMemory() catch std.math.maxInt(u64);
341341 run.max_rss_is_default = true;
342342 }
343343
......@@ -356,7 +356,7 @@ pub fn main() !void {
356356}
357357
358358const Run = struct {
359 max_rss: usize,
359 max_rss: u64,
360360 max_rss_is_default: bool,
361361 max_rss_mutex: std.Thread.Mutex,
362362 skip_oom_steps: bool,
lib/std/process.zig+9-6
......@@ -1522,8 +1522,11 @@ pub const TotalSystemMemoryError = error{
15221522 UnknownTotalSystemMemory,
15231523};
15241524
1525/// Returns the total system memory, in bytes.
1526pub fn totalSystemMemory() TotalSystemMemoryError!usize {
1525/// Returns the total system memory, in bytes as a u64.
1526/// We return a u64 instead of usize due to PAE on ARM
1527/// and Linux's /proc/meminfo reporting more memory when
1528/// using QEMU user mode emulation.
1529pub fn totalSystemMemory() TotalSystemMemoryError!u64 {
15271530 switch (builtin.os.tag) {
15281531 .linux => {
15291532 return totalSystemMemoryLinux() catch return error.UnknownTotalSystemMemory;
......@@ -1552,7 +1555,7 @@ pub fn totalSystemMemory() TotalSystemMemoryError!usize {
15521555 else => return error.UnknownTotalSystemMemory,
15531556 };
15541557 assert(physmem >= 0);
1555 return @as(usize, @bitCast(physmem));
1558 return @as(u64, @bitCast(physmem));
15561559 },
15571560 .windows => {
15581561 var sbi: std.os.windows.SYSTEM_BASIC_INFORMATION = undefined;
......@@ -1565,13 +1568,13 @@ pub fn totalSystemMemory() TotalSystemMemoryError!usize {
15651568 if (rc != .SUCCESS) {
15661569 return error.UnknownTotalSystemMemory;
15671570 }
1568 return @as(usize, sbi.NumberOfPhysicalPages) * sbi.PageSize;
1571 return @as(u64, sbi.NumberOfPhysicalPages) * sbi.PageSize;
15691572 },
15701573 else => return error.UnknownTotalSystemMemory,
15711574 }
15721575}
15731576
1574fn totalSystemMemoryLinux() !usize {
1577fn totalSystemMemoryLinux() !u64 {
15751578 var file = try std.fs.openFileAbsoluteZ("/proc/meminfo", .{});
15761579 defer file.close();
15771580 var buf: [50]u8 = undefined;
......@@ -1583,7 +1586,7 @@ fn totalSystemMemoryLinux() !usize {
15831586 const int_text = it.next() orelse return error.Unexpected;
15841587 const units = it.next() orelse return error.Unexpected;
15851588 if (!std.mem.eql(u8, units, "kB")) return error.Unexpected;
1586 const kilobytes = try std.fmt.parseInt(usize, int_text, 10);
1589 const kilobytes = try std.fmt.parseInt(u64, int_text, 10);
15871590 return kilobytes * 1024;
15881591}
15891592