diff --git a/scripts/memory-soak/README.md b/scripts/memory-soak/README.md new file mode 100644 index 00000000..24675fcb --- /dev/null +++ b/scripts/memory-soak/README.md @@ -0,0 +1,32 @@ +# memory-soak + +An hourly, read-only memory sample of the installed Hidden Bar, for a 14-day soak. It exists to answer dwarvesf/hidden #386 (footprint reported to grow to 600 MB after weeks of uptime) with our own numbers, using the reporter's method: `footprint` and `heap`. + +## What it measures + +One line per hour in `~/Library/Logs/hiddenbar-memory-soak.log`: + +``` +2026-11-16T20:40:00+0700 pid=63409 up=04:12:33 footprint=19235872 peak=19350560 nodes=61739 heap_bytes=6706064 version=1.13.0+21 +``` + +- `footprint`, `peak`: `phys_footprint` and `phys_footprint_peak` in bytes (`footprint -p PID`), the number Activity Monitor shows as Memory. +- `nodes`, `heap_bytes`: the `All zones` line of `heap PID`, the malloc node count and total bytes. +- `up`: process uptime (`ps` etime, `[[dd-]hh:]mm:ss`). `version`: `CFBundleShortVersionString+CFBundleVersion` of /Applications/Hidden Bar.app. +- App not running: `pid=- state=not-running`. If `heap` is denied: `nodes=NA heap=failed`, footprint still logged. + +The sampler never signals or changes the app. `heap` pauses the process for a few milliseconds while it walks the malloc zones. + +## Run it + +```bash scripts/memory-soak/install +scripts/memory-soak/install # from the MAIN checkout, after merge and pull +scripts/memory-soak/summarize # first, last, min, max per pid, one screen +scripts/memory-soak/install --uninstall # stop early; the plist goes to the Trash +``` + +The LaunchAgent is `mini.hiddenbar-memory-soak`. It runs at load and every 3600 s, and boots itself out once the end date (install time + 14 days, passed as an epoch argument) has passed. Check with `launchctl print gui/$UID/mini.hiddenbar-memory-soak`. Launchd stderr goes to `~/Library/Logs/hiddenbar-memory-soak.err`. + +## Reading a leak + +A leak is footprint and node count both rising across days at the same pid; a new pid (app restarted) restarts the series, so compare within a pid only. diff --git a/scripts/memory-soak/install b/scripts/memory-soak/install new file mode 100755 index 00000000..7a3d8721 --- /dev/null +++ b/scripts/memory-soak/install @@ -0,0 +1,65 @@ +#!/bin/bash +# scripts/memory-soak/install : start the 14-day hourly Hidden Bar memory soak as a LaunchAgent. +# +# install write ~/Library/LaunchAgents/mini.hiddenbar-memory-soak.plist and bootstrap it +# install --uninstall boot it out and move the plist to the Trash (never rm) +# +# The plist points at `sample` in the MAIN checkout (not a worktree), so run this from there after +# the branch is merged and pulled. Env: HB_SOAK_DAYS (default 14). +set -eu + +LABEL="mini.hiddenbar-memory-soak" +PLIST="$HOME/Library/LaunchAgents/$LABEL.plist" +DOMAIN="gui/$(id -u)" +DAYS="${HB_SOAK_DAYS:-14}" + +if [ "${1:-}" = "--uninstall" ]; then + launchctl bootout "$DOMAIN/$LABEL" 2>/dev/null || echo "not loaded: $LABEL" + if [ -e "$PLIST" ]; then + trash "${PLIST:?}" 2>/dev/null || mv "$PLIST" "$HOME/.Trash/$LABEL.$(date +%Y%m%d%H%M%S).plist" + echo "plist moved to the Trash: $PLIST" + fi + exit 0 +fi + +# The main checkout is the parent of the shared git dir, also when this runs from a worktree. +common="$(cd "$(dirname "$0")" && git rev-parse --path-format=absolute --git-common-dir)" +SAMPLE="$(dirname "$common")/scripts/memory-soak/sample" +if [ ! -x "$SAMPLE" ]; then + echo "missing or not executable: $SAMPLE (merge and pull the main checkout first)" >&2 + exit 1 +fi + +end_epoch="$(date -v+"$DAYS"d +%s)" +end_human="$(date -r "$end_epoch" +%Y-%m-%dT%H:%M:%S%z)" + +mkdir -p "$(dirname "$PLIST")" "$HOME/Library/Logs" +cat > "$PLIST" < + + + + Label + $LABEL + ProgramArguments + + $SAMPLE + $end_epoch + + StartInterval + 3600 + RunAtLoad + + StandardErrorPath + $HOME/Library/Logs/hiddenbar-memory-soak.err + StandardOutPath + $HOME/Library/Logs/hiddenbar-memory-soak.err + + +PLISTEOF +plutil -lint "$PLIST" >/dev/null + +launchctl bootout "$DOMAIN/$LABEL" 2>/dev/null || true +launchctl bootstrap "$DOMAIN" "$PLIST" +echo "installed $LABEL: sample $SAMPLE, hourly, ends $end_human" +echo "log: $HOME/Library/Logs/hiddenbar-memory-soak.log" diff --git a/scripts/memory-soak/sample b/scripts/memory-soak/sample new file mode 100755 index 00000000..2f8090c5 --- /dev/null +++ b/scripts/memory-soak/sample @@ -0,0 +1,61 @@ +#!/bin/bash +# scripts/memory-soak/sample : append ONE memory sample of the installed Hidden Bar to the soak log. +# Read-only on the app: footprint and heap only inspect it. (heap pauses the process for a few +# milliseconds while it walks the malloc zones; it sends no signal and changes nothing.) +# +# sample [END_EPOCH] END_EPOCH: unix seconds. Past it, boot the LaunchAgent out and exit. +# +# Env: HB_SOAK_LOG (default ~/Library/Logs/hiddenbar-memory-soak.log), HB_SOAK_APP, HB_SOAK_LABEL. +# Line: TS pid=N up=ETIME footprint=B peak=B nodes=N heap_bytes=B version=SHORT+BUILD +set -u + +LOG="${HB_SOAK_LOG:-$HOME/Library/Logs/hiddenbar-memory-soak.log}" +APP="${HB_SOAK_APP:-/Applications/Hidden Bar.app}" +LABEL="${HB_SOAK_LABEL:-mini.hiddenbar-memory-soak}" +END_EPOCH="${1:-}" + +mkdir -p "$(dirname "$LOG")" +ts() { date +%Y-%m-%dT%H:%M:%S%z; } +log() { printf '%s %s\n' "$(ts)" "$*" >> "$LOG"; } + +if [ -n "$END_EPOCH" ] && [ "$(date +%s)" -ge "$END_EPOCH" ]; then + log "state=soak-ended (end date reached, booting out $LABEL)" + launchctl bootout "gui/$(id -u)/$LABEL" + exit 0 +fi + +PLIST="$APP/Contents/Info.plist" +short=NA +build=NA +if [ -f "$PLIST" ]; then + short="$(/usr/libexec/PlistBuddy -c 'Print :CFBundleShortVersionString' "$PLIST" 2>/dev/null)" || short=NA + build="$(/usr/libexec/PlistBuddy -c 'Print :CFBundleVersion' "$PLIST" 2>/dev/null)" || build=NA +fi +version="$short+$build" + +pid="$(pgrep -f "$APP/Contents/MacOS/Hidden Bar" | head -n 1)" +if [ -z "$pid" ]; then + log "pid=- state=not-running version=$version" + exit 0 +fi + +up="$(ps -o etime= -p "$pid" 2>/dev/null | tr -d ' ')" +[ -n "$up" ] || up=NA + +fp_out="$(footprint -f bytes --noCategories -p "$pid" 2>&1)" +footprint="$(printf '%s\n' "$fp_out" | awk '$1 == "phys_footprint:" { print $2; exit }')" +peak="$(printf '%s\n' "$fp_out" | awk '$1 == "phys_footprint_peak:" { print $2; exit }')" +[ -n "$footprint" ] || footprint=NA +[ -n "$peak" ] || peak=NA + +heap_out="$(heap "$pid" 2>&1)" +nodes="$(printf '%s\n' "$heap_out" | sed -n 's/^All zones: \([0-9]*\) nodes (\([0-9]*\) bytes).*/\1/p' | head -n 1)" +heap_bytes="$(printf '%s\n' "$heap_out" | sed -n 's/^All zones: \([0-9]*\) nodes (\([0-9]*\) bytes).*/\2/p' | head -n 1)" +note="" +if [ -z "$nodes" ]; then + nodes=NA + heap_bytes=NA + note=" heap=failed" +fi + +log "pid=$pid up=$up footprint=$footprint peak=$peak nodes=$nodes heap_bytes=$heap_bytes version=$version$note" diff --git a/scripts/memory-soak/summarize b/scripts/memory-soak/summarize new file mode 100755 index 00000000..7a89df90 --- /dev/null +++ b/scripts/memory-soak/summarize @@ -0,0 +1,51 @@ +#!/bin/bash +# scripts/memory-soak/summarize : first, last, min, max footprint and malloc nodes per pid. +# +# summarize [LOG] default ~/Library/Logs/hiddenbar-memory-soak.log +set -eu + +LOG="${1:-$HOME/Library/Logs/hiddenbar-memory-soak.log}" +[ -r "$LOG" ] || { echo "no log: $LOG" >&2; exit 1; } + +awk ' +function field(name, i) { + for (i = 2; i <= NF; i++) { + if (index($i, name "=") == 1) { return substr($i, length(name) + 2) } + } + return "" +} +function mb(b) { return sprintf("%.1f", b / 1048576) } +function nodes(p) { return (p in nd0) ? sprintf("%d %d %d %d", nd0[p], ndl[p], ndmin[p], ndmax[p]) : "NA" } +{ + pid = field("pid") + if (pid == "" || pid == "-") { if (pid == "-") gaps++; next } + fp = field("footprint"); nd = field("nodes") + if (fp == "NA" || fp == "") next + fp += 0 + if (!(pid in n)) { + order[++np] = pid; first_ts[pid] = $1 + fp0[pid] = fp; fpmin[pid] = fp; fpmax[pid] = fp + } + n[pid]++ + last_up[pid] = field("up") + fpl[pid] = fp + if (fp < fpmin[pid]) fpmin[pid] = fp + if (fp > fpmax[pid]) fpmax[pid] = fp + if (nd != "NA" && nd != "") { + nd += 0 + if (!(pid in nd0)) { nd0[pid] = nd; ndmin[pid] = nd; ndmax[pid] = nd } + ndl[pid] = nd + if (nd < ndmin[pid]) ndmin[pid] = nd + if (nd > ndmax[pid]) ndmax[pid] = nd + } +} +END { + if (np == 0) { print "no samples with a running app yet"; exit } + printf "%-7s %-4s %-24s %-11s | footprint MB: first last min max | malloc nodes: first last min max\n", "pid", "n", "first sample", "last up" + for (i = 1; i <= np; i++) { + p = order[i] + printf "%-7s %-4d %-24s %-11s | %7s %7s %7s %7s | %s\n", p, n[p], first_ts[p], last_up[p], mb(fp0[p]), mb(fpl[p]), mb(fpmin[p]), mb(fpmax[p]), nodes(p) + } + if (gaps > 0) printf "%d sample(s) with the app not running\n", gaps +} +' "$LOG"