67 lines
4.7 KiB
Bash
67 lines
4.7 KiB
Bash
#!/usr/bin/env bash
|
|
# Host RAM inventory of the serve stack: where every GiB is, per kernel class and per process, so that "the model bytes appear once" can be read off a table.
|
|
# Read-only (/proc and fincore only). One snapshot per call; the A/B job calls it at marks (idle after READY, after the last turn) and every 60 s.
|
|
#
|
|
# ram-inventory.sh LABEL [FILE...] prints a block starting with "== RAM-INVENTORY LABEL"; FILE... are the model files whose page-cache residency is reported
|
|
#
|
|
# Blocks:
|
|
# meminfo MemTotal MemFree MemAvailable Cached Shmem AnonPages Mlocked Unevictable AnonHugePages Buffers (GiB)
|
|
# process the engine (leleka-inference), the supervisor (leleka), the gateway/daemon (leleka-daemon, leleka-api, leleka-cli), the lab holder (python kar_holder*), and the
|
|
# caller of this script (the shell that holds a `leleka seal-exec` memfd): Rss, Pss, anon/file/shmem Pss, Locked (smaps_rollup), VmLck. Shared pages (a memfd the
|
|
# supervisor and the engine both map) are split by Pss, so the sum over processes does not double count them.
|
|
# memfd every memfd descriptor of those processes: apparent size and ALLOCATED bytes (st_blocks), so a sparse memfd shows its holes (allocated << apparent)
|
|
# pagecache resident bytes of the model files (fincore, mincore based, no I/O). A model byte that is both in the engine's own memory and here is held twice.
|
|
# model the rollup: Pss of the stack, page cache of the model files, MemAvailable
|
|
set -uo pipefail
|
|
label=${1:-snapshot}; shift || true
|
|
gib() { awk -v b="$1" 'BEGIN{printf "%.2f", b/1073741824}'; }
|
|
cmdline() { tr '\000' ' ' < "/proc/$1/cmdline" 2>/dev/null | cut -c1-90; }
|
|
echo "== RAM-INVENTORY $label $(date -u +%FT%TZ)"
|
|
awk '/^(MemTotal|MemFree|MemAvailable|Buffers|Cached|SwapFree|Shmem|AnonPages|Mlocked|Unevictable|AnonHugePages):/{printf "meminfo %-14s %8.2f GiB\n", $1, $2/1048576}' /proc/meminfo
|
|
total_pss=0; total_anon=0; total_shm=0; total_file=0
|
|
report_pid() { # pid name cmd
|
|
local pid=$1 name=$2 cmd=$3 rss pss panon pfile pshm locked vmlck fd tgt apparent blocks
|
|
[ -r "/proc/$pid/smaps_rollup" ] || return
|
|
read -r rss pss panon pfile pshm locked < <(awk '
|
|
/^Rss:/{r=$2} /^Pss:/{p=$2} /^Pss_Anon:/{a=$2} /^Pss_File:/{f=$2} /^Pss_Shmem:/{s=$2} /^Locked:/{l=$2}
|
|
END{print r+0, p+0, a+0, f+0, s+0, l+0}' "/proc/$pid/smaps_rollup")
|
|
vmlck=$(awk '/^VmLck:/{print $2}' "/proc/$pid/status" 2>/dev/null)
|
|
printf "process pid=%-7s %-16s rss=%7s pss=%7s anon=%7s file=%7s shmem=%7s locked=%7s vmlck=%7s GiB %s\n" "$pid" "$name" \
|
|
"$(gib $((rss*1024)))" "$(gib $((pss*1024)))" "$(gib $((panon*1024)))" "$(gib $((pfile*1024)))" "$(gib $((pshm*1024)))" "$(gib $((locked*1024)))" "$(gib $((${vmlck:-0}*1024)))" "$cmd"
|
|
total_pss=$((total_pss+pss)); total_anon=$((total_anon+panon)); total_shm=$((total_shm+pshm)); total_file=$((total_file+pfile))
|
|
for fd in /proc/$pid/fd/*; do
|
|
tgt=$(readlink "$fd" 2>/dev/null) || continue
|
|
case "$tgt" in *memfd:*)
|
|
read -r apparent blocks < <(stat -L -c '%s %b %B' "$fd" 2>/dev/null | awk '{print $1, $2*$3}')
|
|
[ -n "${apparent:-}" ] && printf "memfd pid=%-7s fd=%-4s %s apparent=%8s allocated=%8s GiB\n" "$pid" "$(basename "$fd")" "$tgt" "$(gib "$apparent")" "$(gib "$blocks")" ;;
|
|
esac
|
|
done
|
|
}
|
|
seen=" "
|
|
for name in leleka-inferenc leleka leleka-daemon leleka-api leleka-cli python3; do # comm is truncated to 15 chars: pgrep -x leleka-inference never matches
|
|
for pid in $(pgrep -u "$USER" -x "$name" 2>/dev/null); do
|
|
cmd=$(cmdline "$pid")
|
|
if [ "$name" = python3 ]; then case "$cmd" in *kar_holder*|*single-copy-holder*) ;; *) continue ;; esac; fi
|
|
seen="$seen$pid "; report_pid "$pid" "$name" "$cmd"
|
|
done
|
|
done
|
|
# the caller (a `leleka seal-exec` child shell that holds the memfd) and any pids named by the harness
|
|
for pid in $PPID ${INV_EXTRA_PIDS:-}; do
|
|
case "$seen" in *" $pid "*) continue ;; esac
|
|
seen="$seen$pid "; report_pid "$pid" "caller" "$(cmdline "$pid")"
|
|
done
|
|
printf "process-sum pss=%s GiB (anon %s, file %s, shmem %s)\n" "$(gib $((total_pss*1024)))" "$(gib $((total_anon*1024)))" "$(gib $((total_file*1024)))" "$(gib $((total_shm*1024)))"
|
|
cache_total=0
|
|
if command -v fincore >/dev/null 2>&1; then
|
|
for f in "$@"; do
|
|
[ -r "$f" ] || continue
|
|
res=$(fincore -b -n -o RES "$f" 2>/dev/null | awk '{s+=$1} END{print s+0}')
|
|
printf "pagecache %-70s resident=%8s GiB of %8s GiB\n" "$(basename "$f")" "$(gib "$res")" "$(gib "$(stat -c %s "$f")")"
|
|
cache_total=$((cache_total+res))
|
|
done
|
|
else
|
|
echo "pagecache fincore not installed"
|
|
fi
|
|
avail=$(awk '/^MemAvailable:/{print $2*1024}' /proc/meminfo)
|
|
printf "model stack-pss=%s GiB model-file-page-cache=%s GiB MemAvailable=%s GiB\n" "$(gib $((total_pss*1024)))" "$(gib "$cache_total")" "$(gib "$avail")"
|