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 {...@@ -95,7 +95,7 @@ pub fn main() !void {
95 var install_prefix: ?[]const u8 = null;95 var install_prefix: ?[]const u8 = null;
96 var dir_list = std.Build.DirList{};96 var dir_list = std.Build.DirList{};
97 var summary: ?Summary = null;97 var summary: ?Summary = null;
98 var max_rss: usize = 0;98 var max_rss: u64 = 0;
99 var skip_oom_steps: bool = false;99 var skip_oom_steps: bool = false;
100 var color: Color = .auto;100 var color: Color = .auto;
101 var seed: u32 = 0;101 var seed: u32 = 0;
...@@ -337,7 +337,7 @@ pub fn main() !void {...@@ -337,7 +337,7 @@ pub fn main() !void {
337 };337 };
338338
339 if (run.max_rss == 0) {339 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);
341 run.max_rss_is_default = true;341 run.max_rss_is_default = true;
342 }342 }
343343
...@@ -356,7 +356,7 @@ pub fn main() !void {...@@ -356,7 +356,7 @@ pub fn main() !void {
356}356}
357357
358const Run = struct {358const Run = struct {
359 max_rss: usize,359 max_rss: u64,
360 max_rss_is_default: bool,360 max_rss_is_default: bool,
361 max_rss_mutex: std.Thread.Mutex,361 max_rss_mutex: std.Thread.Mutex,
362 skip_oom_steps: bool,362 skip_oom_steps: bool,
lib/std/process.zig+9-6
...@@ -1522,8 +1522,11 @@ pub const TotalSystemMemoryError = error{...@@ -1522,8 +1522,11 @@ pub const TotalSystemMemoryError = error{
1522 UnknownTotalSystemMemory,1522 UnknownTotalSystemMemory,
1523};1523};
15241524
1525/// Returns the total system memory, in bytes.1525/// Returns the total system memory, in bytes as a u64.
1526pub fn totalSystemMemory() TotalSystemMemoryError!usize {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 {
1527 switch (builtin.os.tag) {1530 switch (builtin.os.tag) {
1528 .linux => {1531 .linux => {
1529 return totalSystemMemoryLinux() catch return error.UnknownTotalSystemMemory;1532 return totalSystemMemoryLinux() catch return error.UnknownTotalSystemMemory;
...@@ -1552,7 +1555,7 @@ pub fn totalSystemMemory() TotalSystemMemoryError!usize {...@@ -1552,7 +1555,7 @@ pub fn totalSystemMemory() TotalSystemMemoryError!usize {
1552 else => return error.UnknownTotalSystemMemory,1555 else => return error.UnknownTotalSystemMemory,
1553 };1556 };
1554 assert(physmem >= 0);1557 assert(physmem >= 0);
1555 return @as(usize, @bitCast(physmem));1558 return @as(u64, @bitCast(physmem));
1556 },1559 },
1557 .windows => {1560 .windows => {
1558 var sbi: std.os.windows.SYSTEM_BASIC_INFORMATION = undefined;1561 var sbi: std.os.windows.SYSTEM_BASIC_INFORMATION = undefined;
...@@ -1565,13 +1568,13 @@ pub fn totalSystemMemory() TotalSystemMemoryError!usize {...@@ -1565,13 +1568,13 @@ pub fn totalSystemMemory() TotalSystemMemoryError!usize {
1565 if (rc != .SUCCESS) {1568 if (rc != .SUCCESS) {
1566 return error.UnknownTotalSystemMemory;1569 return error.UnknownTotalSystemMemory;
1567 }1570 }
1568 return @as(usize, sbi.NumberOfPhysicalPages) * sbi.PageSize;1571 return @as(u64, sbi.NumberOfPhysicalPages) * sbi.PageSize;
1569 },1572 },
1570 else => return error.UnknownTotalSystemMemory,1573 else => return error.UnknownTotalSystemMemory,
1571 }1574 }
1572}1575}
15731576
1574fn totalSystemMemoryLinux() !usize {1577fn totalSystemMemoryLinux() !u64 {
1575 var file = try std.fs.openFileAbsoluteZ("/proc/meminfo", .{});1578 var file = try std.fs.openFileAbsoluteZ("/proc/meminfo", .{});
1576 defer file.close();1579 defer file.close();
1577 var buf: [50]u8 = undefined;1580 var buf: [50]u8 = undefined;
...@@ -1583,7 +1586,7 @@ fn totalSystemMemoryLinux() !usize {...@@ -1583,7 +1586,7 @@ fn totalSystemMemoryLinux() !usize {
1583 const int_text = it.next() orelse return error.Unexpected;1586 const int_text = it.next() orelse return error.Unexpected;
1584 const units = it.next() orelse return error.Unexpected;1587 const units = it.next() orelse return error.Unexpected;
1585 if (!std.mem.eql(u8, units, "kB")) return error.Unexpected;1588 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);
1587 return kilobytes * 1024;1590 return kilobytes * 1024;
1588}1591}
15891592