#!/usr/bin/env nu def cgroup-path [] { open --raw /proc/self/cgroup | lines | parse --regex '^(?P[^:]*):(?P[^:]*):(?P.*)$' | where id == '0' | get path.0 | str trim --left --char '/' | $'/sys/fs/cgroup/($in)' } def read-cgroup-file [cg: string, name: string] { let path = ($cg | path join $name) try { open --raw $path | str trim } catch { $'' } } def scope-info [] { let cg = (cgroup-path) let sensitive_env_names = ( $env | transpose name value | where name =~ '(?i)(KEY|TOKEN|SECRET|PASSWORD)' | get name | sort ) { pid: $nu.pid cwd: (pwd) shell: ($env.SHELL? | default '') wezterm_pane: ($env.WEZTERM_PANE? | default '') wezterm_exec_domain: ($env.WEZTERM_EXEC_DOMAIN? | default '') cgroup: $cg memory_current: (read-cgroup-file $cg memory.current) memory_high: (read-cgroup-file $cg memory.high) memory_max: (read-cgroup-file $cg memory.max) memory_swap_max: (read-cgroup-file $cg memory.swap.max) cpu_max: (read-cgroup-file $cg cpu.max) env_count: ($env | transpose name value | length) sensitive_env_names: $sensitive_env_names } } let chunk_mib = ($env.LEAK_CHUNK_MIB? | default '16' | into int) let sleep_ms = (($env.LEAK_SLEEP_MS? | default '50' | into int) * 1ms) let force = (($env.LEAK_FORCE? | default '0') == '1') let info = (scope-info) let memory_max = $info.memory_max let limited = if $memory_max == 'max' { false } else { try { ($memory_max | into int) <= 1073741824 } catch { false } } print ($info | table --expand) if (not $limited) and (not $force) { print '' print 'REFUSING: memory.max is not <= 1GiB. You are not in the tiny scope.' print 'Open scope first: LEADER b, then run this script again.' print 'Override only if intentional: LEAK_FORCE=1 nu scripts/leak-memory.nu' exit 2 } print '' print $'leaking ($chunk_mib) MiB every ($sleep_ms); expect cgroup OOM kill inside scope:build' mut chunks = [] mut allocated = 0 let chunk_size = ($chunk_mib * 1024 * 1024) loop { let chunk = (random binary $chunk_size) $chunks = ($chunks | append $chunk) $allocated = ($allocated + $chunk_mib) print $'allocated=($allocated) MiB' sleep $sleep_ms }