Commit c0e0bb385c

Tristan Ross <tristan.ross@midstall.com>
2024-01-22 07:16:22
std.process: return u64 in totalSystemMemory
1 parent 68ea112
Changed files (2)
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;
 }
 
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,