Files

28 lines
2.4 KiB
Bash

#!/usr/bin/env bash
# Prefill fixed-latency program: host memory sampler, one per session (test tooling, never in serving). pf_memstat.sh OUT_PREFIX
# OUT_PREFIX.csv every 0.25 s: epoch_ms, PSI memory some/full total (us), direct-reclaim stalls (sum of allocstall_*), major faults, direct scans,
# compaction stalls (system wide), then the engine process: pid, major faults, minor faults, then PSI cpu some total (us) and the summed run-queue wait
# (run_delay, ns) of all threads of the engine process, then PSI io some total (us) and the engine's syscr/read_bytes (unset when unreadable)
# OUT_PREFIX.mem.csv every 15 s: epoch_ms, MemAvailable, MemFree, Cached, Shmem, Mapped, Active(file), Inactive(file) (kB), engine RssAnon, RssFile, RssShmem (kB)
# The engine process is the leleka-inference (ours) or the vanilla Strata engine binary, found by name; 0 when none runs.
OUT=${1:?OUT_PREFIX}
engine_pid() { pgrep -u "$USER" -f 'leleka-inference|components/engine/[^ ]*/strata' 2>/dev/null | head -1; }
pid=; n=0
while :; do
{ [ -n "$pid" ] && [ -r "/proc/$pid/stat" ]; } || pid=$(engine_pid)
sys=$(awk '/^allocstall_/{a+=$2} /^pgmajfault /{m=$2} /^pgscan_direct /{d=$2} /^compact_stall /{c=$2} END{print a","m","d","c}' /proc/vmstat)
psi=$(awk '/^some/{split($5,s,"=")} /^full/{split($5,f,"=")} END{print s[2]","f[2]}' /proc/pressure/memory 2>/dev/null)
proc=0,0,0; [ -n "$pid" ] && proc=$(awk -v p="$pid" '{sub(/^[^)]*\) /,""); split($0,a," "); print p","a[10]","a[8]}' "/proc/$pid/stat" 2>/dev/null || echo "$pid,0,0")
cpu=$(awk '/^some/{split($5,s,"=")} END{print s[2]}' /proc/pressure/cpu 2>/dev/null)
rd=0; [ -n "$pid" ] && rd=$(cat /proc/"$pid"/task/*/schedstat 2>/dev/null | awk '{d+=$2} END{print d+0}')
io=$(awk '/^some/{split($5,s,"=")} END{print s[2]}' /proc/pressure/io 2>/dev/null)
rb=0; [ -n "$pid" ] && rb=$(awk '/^(read_bytes|syscr):/{printf "%s%s",(c++?"/":""),$2}' "/proc/$pid/io" 2>/dev/null)
echo "$(date +%s%3N),$psi,$sys,$proc,$cpu,$rd,$io,${rb:-0}" >>"$OUT.csv"
if [ $((n % 60)) = 0 ]; then
mi=$(awk '{v[$1]=$2} END{print v["MemAvailable:"]","v["MemFree:"]","v["Cached:"]","v["Shmem:"]","v["Mapped:"]","v["Active(file):"]","v["Inactive(file):"]}' /proc/meminfo)
rs=0,0,0; [ -n "$pid" ] && rs=$(awk '/^RssAnon:/{a=$2} /^RssFile:/{f=$2} /^RssShmem:/{s=$2} END{print a","f","s}' "/proc/$pid/status" 2>/dev/null)
echo "$(date +%s%3N),$mi,$rs" >>"$OUT.mem.csv"
fi
n=$((n + 1)); sleep 0.25
done