@@ -23,6 +23,7 @@ max_snaps=${4:-10}
2323
2424mkdir -p " ${outdir} "
2525self=$$
26+ RSS_THRESHOLD_KB=${HEAP_SNAPSHOT_RSS_THRESHOLD_KB:- 50000}
2627
2728if [[ " $( uname -s) " != " Darwin" ]]; then
2829 echo " heap_snapshots: only supported on macOS" >&2
@@ -35,8 +36,9 @@ if (( ! sudo_ok )); then
3536 echo " heap_snapshots: passwordless sudo unavailable; heap/malloc_history need it" >&2
3637fi
3738
38- # Pick the matching PID with the largest RSS (the instrumented test binary), so
39- # we never attach to this script or the run_tests wrapper shell.
39+ # Pick the matching PID with the largest RSS. `pgrep -f` can also match this
40+ # script and the run_tests wrapper shell, so wait for the real test binary to
41+ # cross an RSS threshold before attaching heap/malloc_history.
4042pick_target () {
4143 local best=" " best_rss=0 p rss
4244 for p in $( pgrep -f " ${pattern} " 2> /dev/null) ; do
@@ -45,13 +47,18 @@ pick_target() {
4547 [[ -z " ${rss} " ]] && continue
4648 if (( rss > best_rss )) ; then best_rss=${rss} ; best=${p} ; fi
4749 done
48- echo " ${best} "
50+ echo " ${best} ${best_rss} "
4951}
5052
53+ # Wait up to 120s for the real binary (RSS over threshold) to come up. Fall back
54+ # to the largest match seen if nothing crosses the threshold before timeout.
5155pid=" "
5256for _ in $( seq 1 120) ; do
53- pid=$( pick_target)
54- [[ -n " ${pid} " ]] && break
57+ read -r cand cand_rss <<< " $(pick_target)"
58+ if [[ -n " ${cand} " ]]; then
59+ pid=${cand}
60+ (( cand_rss >= RSS_THRESHOLD_KB )) && break
61+ fi
5562 sleep 1
5663done
5764if [[ -z " ${pid} " ]]; then
0 commit comments