Created
March 13, 2024 09:42
-
-
Save jacobly0/e648e13f3101dc1c0e4a554fc4c2e90a to your computer and use it in GitHub Desktop.
wait4 maxrss without parent memory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const std = @import("std"); | |
const assert = std.debug.assert; | |
const expect = std.testing.expect; | |
pub fn main() !void { | |
var args = std.process.args(); | |
_ = args.next().?; | |
if (args.next()) |_| { | |
// child process | |
var child = std.ChildProcess.init(&.{"/usr/bin/env"}, std.heap.page_allocator); | |
child.stdin_behavior = .Ignore; | |
child.stdout_behavior = .Pipe; | |
child.stderr_behavior = .Pipe; | |
child.request_resource_usage_statistics = true; | |
try child.spawn(); | |
switch (try child.wait()) { | |
.Exited => |code| if (code != 0) return error.BadExitCode, | |
else => return error.Crashed, | |
} | |
const max_rss = child.resource_usage_statistics.getMaxRss() orelse 0; | |
std.debug.print("child max_rss={d}\n", .{max_rss}); | |
} else { | |
// parent process | |
const big = try std.heap.page_allocator.alloc(u8, 2 * 1024 * 1024 * 1024); | |
_ = big; | |
var self_exe_buf: [std.fs.MAX_PATH_BYTES]u8 = undefined; | |
const self_exe = try std.fs.selfExePath(&self_exe_buf); | |
var child = std.ChildProcess.init(&.{ | |
self_exe, | |
"--child", | |
}, std.heap.page_allocator); | |
child.stdin_behavior = .Ignore; | |
child.stdout_behavior = .Inherit; | |
child.stderr_behavior = .Inherit; | |
child.request_resource_usage_statistics = true; | |
try child.spawn(); | |
switch (try child.wait()) { | |
.Exited => |code| if (code != 0) return error.BadExitCode, | |
else => return error.Crashed, | |
} | |
const max_rss = child.resource_usage_statistics.getMaxRss() orelse 0; | |
std.debug.print("parent max_rss={d}\n", .{max_rss}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment