Skip to content

Instantly share code, notes, and snippets.

@andrewrk
Created March 13, 2024 00:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewrk/58e97241ae487beae3cb775911bfb579 to your computer and use it in GitHub Desktop.
Save andrewrk/58e97241ae487beae3cb775911bfb579 to your computer and use it in GitHub Desktop.
wait4 maxrss includes parent memory, why?
const std = @import("std");
const assert = std.debug.assert;
const expect = std.testing.expect;
pub fn main() !void {
const big = try std.heap.page_allocator.alloc(u8, 2 * 1024 * 1024 * 1024);
_ = big;
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("max_rss={d}\n", .{max_rss});
}
$ zig run example.zig
max_rss=2148065280
execve("./example", ["./example"], 0x7fff06f6c9d0 /* 132 vars */) = 0
arch_prctl(ARCH_SET_FS, 0x10e5010) = 0
prlimit64(0, RLIMIT_STACK, NULL, {rlim_cur=8192*1024, rlim_max=RLIM64_INFINITY}) = 0
prlimit64(0, RLIMIT_STACK, {rlim_cur=16384*1024, rlim_max=RLIM64_INFINITY}, NULL) = 0
rt_sigaction(SIGSEGV, {sa_handler=0x106a9f0, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART|SA_RESETHAND|SA_SIGINFO, sa_restorer=0x107d230}, NULL, 8) = 0
rt_sigaction(SIGILL, {sa_handler=0x106a9f0, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART|SA_RESETHAND|SA_SIGINFO, sa_restorer=0x107d230}, NULL, 8) = 0
rt_sigaction(SIGBUS, {sa_handler=0x106a9f0, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART|SA_RESETHAND|SA_SIGINFO, sa_restorer=0x107d230}, NULL, 8) = 0
rt_sigaction(SIGFPE, {sa_handler=0x106a9f0, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART|SA_RESETHAND|SA_SIGINFO, sa_restorer=0x107d230}, NULL, 8) = 0
rt_sigaction(SIGPIPE, {sa_handler=0x1039ec0, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x107d230}, NULL, 8) = 0
mmap(NULL, 2147483648, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fc98796a000
pipe2([3, 4], 0) = 0
pipe2([5, 6], 0) = 0
open("/dev/null", O_RDWR) = 7
mmap(0x7fca0796a000, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fca0796a000
eventfd2(0, EFD_CLOEXEC) = 8
fork() = 963690
close(4) = 0
close(6) = 0
munmap(0x7fca0796a000, 4096) = 0
close(7) = 0
wait4(963690, [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], 0, {ru_utime={tv_sec=0, tv_usec=0}, ru_stime={tv_sec=0, tv_usec=27418}, ...}) = 963690
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=963690, si_uid=1000, si_status=0, si_utime=0, si_stime=2 /* 0.02 s */} ---
close(3) = 0
close(5) = 0
poll([{fd=8, events=POLLIN}], 1, 0) = 0 (Timeout)
close(8) = 0
gettid() = 963689
write(2, "max_rss=", 8max_rss=) = 8
write(2, "2148069376", 102148069376) = 10
write(2, "\n", 1
) = 1
exit_group(0) = ?
+++ exited with 0 +++
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment