#!/bin/sh
# Record a Perfetto trace with gpu_jank.pbtx and pull it (macOS / Linux / Git Bash).
#   ./record_gpu_trace.sh            # 15 s
#   ./record_gpu_trace.sh 30 <adb serial>
set -e
SECS=${1:-15}
SEL=""; [ -n "$2" ] && SEL="-s $2"
HERE=$(cd "$(dirname "$0")" && pwd)
OUT=${OUT:-gpu_$(date +%Y%m%d_%H%M%S).pftrace}
REMOTE=/data/misc/perfetto-traces/gpu_jank.pftrace
# Git Bash would rewrite /data/... into a Windows path
export MSYS_NO_PATHCONV=1
sed "s/duration_ms: *[0-9]*/duration_ms: $((SECS * 1000))/" "$HERE/gpu_jank.pbtx" \
  | adb $SEL shell "perfetto --txt -c - -o $REMOTE"
adb $SEL pull "$REMOTE" "$OUT"
adb $SEL shell "rm -f $REMOTE" >/dev/null
echo "saved $OUT"
echo "next: python analyze_gpu_trace.py $OUT --pkg <your.package>"
