From 3b29d00c9826d8053b63fe2bcd86c6d1517fcc51 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 6 Mar 2023 00:19:32 -0700 Subject: add std.process.totalSystemMemory --- lib/std/process.zig | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'lib/std/process.zig') diff --git a/lib/std/process.zig b/lib/std/process.zig index eff29e86fa..8652dd1bbc 100644 --- a/lib/std/process.zig +++ b/lib/std/process.zig @@ -1169,3 +1169,38 @@ pub fn execve( return os.execvpeZ_expandArg0(.no_expand, argv_buf.ptr[0].?, argv_buf.ptr, envp); } + +pub const TotalSystemMemoryError = error{ + UnknownTotalSystemMemory, +}; + +/// Returns the total system memory, in bytes. +pub fn totalSystemMemory() TotalSystemMemoryError!usize { + switch (builtin.os.tag) { + .linux => { + return totalSystemMemoryLinux() catch return error.UnknownTotalSystemMemory; + }, + .windows => { + var kilobytes: std.os.windows.ULONGLONG = undefined; + assert(std.os.windows.kernel32.GetPhysicallyInstalledSystemMemory(&kilobytes) == std.os.windows.TRUE); + return kilobytes * 1024; + }, + else => return error.UnknownTotalSystemMemory, + } +} + +fn totalSystemMemoryLinux() !usize { + var file = try std.fs.openFileAbsoluteZ("/proc/meminfo", .{}); + defer file.close(); + var buf: [50]u8 = undefined; + const amt = try file.read(&buf); + if (amt != 50) return error.Unexpected; + var it = std.mem.tokenize(u8, buf[0..amt], " \n"); + const label = it.next().?; + if (!std.mem.eql(u8, label, "MemTotal:")) return error.Unexpected; + 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); + return kilobytes * 1024; +} -- cgit v1.2.3 From 98299e7787146e1572cd2038654dbbcf84fa32d1 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 12 Mar 2023 00:34:11 -0700 Subject: add std.process.cleanExit --- lib/std/process.zig | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'lib/std/process.zig') diff --git a/lib/std/process.zig b/lib/std/process.zig index 8652dd1bbc..d9bf09ee2a 100644 --- a/lib/std/process.zig +++ b/lib/std/process.zig @@ -1204,3 +1204,16 @@ fn totalSystemMemoryLinux() !usize { const kilobytes = try std.fmt.parseInt(usize, int_text, 10); return kilobytes * 1024; } + +/// Indicate that we are now terminating with a successful exit code. +/// In debug builds, this is a no-op, so that the calling code's +/// cleanup mechanisms are tested and so that external tools that +/// check for resource leaks can be accurate. In release builds, this +/// calls exit(0), and does not return. +pub fn cleanExit() void { + if (builtin.mode == .Debug) { + return; + } else { + exit(0); + } +} -- cgit v1.2.3 From 61d7e31078fc54010009494d894e2461b41eeee2 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 13 Mar 2023 12:15:45 -0700 Subject: remove bad unit test from std lib This unit test tested the command line arguments which were passed to the test runner, which is not really something that unit tests are supposed to observe. The proper way to test command line argument parsing is with a standalone test, where the set of command line arguments being tested for are also being controlled by the test itself. --- lib/std/process.zig | 18 ------------------ 1 file changed, 18 deletions(-) (limited to 'lib/std/process.zig') diff --git a/lib/std/process.zig b/lib/std/process.zig index d9bf09ee2a..d06a012af2 100644 --- a/lib/std/process.zig +++ b/lib/std/process.zig @@ -828,24 +828,6 @@ pub fn argsWithAllocator(allocator: Allocator) ArgIterator.InitError!ArgIterator return ArgIterator.initWithAllocator(allocator); } -test "args iterator" { - var ga = std.testing.allocator; - var it = try argsWithAllocator(ga); - defer it.deinit(); // no-op unless WASI or Windows - - const prog_name = it.next() orelse unreachable; - const expected_suffix = switch (builtin.os.tag) { - .wasi => "test.wasm", - .windows => "test.exe", - else => "test", - }; - const given_suffix = std.fs.path.basename(prog_name); - - try testing.expect(mem.eql(u8, expected_suffix, given_suffix)); - try testing.expect(it.next() == null); - try testing.expect(!it.skip()); -} - /// Caller must call argsFree on result. pub fn argsAlloc(allocator: Allocator) ![][:0]u8 { // TODO refactor to only make 1 allocation. -- cgit v1.2.3