tracy_profile.sh 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #!/usr/bin/env bash
  2. # Renders a wallpaper headlessly with a Tracy build (-DLWE_TRACY=ON), captures the zones and prints the busiest ones
  3. # and the GPU time per frame. Open the saved capture with tracy-profiler for the timeline.
  4. #
  5. # Usage:
  6. # tools/tracy_profile.sh <tracy build binary> <item dir> [seconds] [engine args...]
  7. #
  8. # The loader zones are only recorded by a build configured with -DLWE_TRACY_ON_DEMAND=OFF, an on demand build starts
  9. # recording when the capture connects, after the wallpaper has loaded.
  10. #
  11. # Needs tracy-capture and tracy-csvexport (0.14.x, the client is 0.14.1). HEADLESS_RENDER_SIZE=WxH, LWE_ASSETS_DIR and
  12. # TRACY_OUT (capture file, default /tmp/lwe-<item>.tracy) are read from the environment.
  13. set -euo pipefail
  14. if [ "$#" -lt 2 ]; then
  15. echo "Usage: $0 <binary> <item dir> [seconds] [engine args...]" >&2
  16. exit 1
  17. fi
  18. BINARY="$1"
  19. ITEM="$2"
  20. SECONDS_TO_CAPTURE="${3:-10}"
  21. shift 2
  22. [ "$#" -gt 0 ] && shift
  23. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  24. OUT="${TRACY_OUT:-/tmp/lwe-$(basename "$ITEM").tracy}"
  25. LOG="$(mktemp)"
  26. if ss -ltn 2>/dev/null | grep -q ':8086 '; then
  27. echo "port 8086 is taken, an engine of an earlier run is still alive: kill it by pid (ss -ltnp | grep 8086)" >&2
  28. exit 1
  29. fi
  30. # the wait for the client below takes up to 90 s and the capture starts 3 s after it, the engine has to live that long.
  31. # Its own process group, so stopping it reaches the timeout and the engine below the script as well
  32. HEADLESS_RENDER_KEEP_RUNNING=1 HEADLESS_RENDER_TIMEOUT=$((SECONDS_TO_CAPTURE + 130)) \
  33. setsid "$SCRIPT_DIR/headless_render.sh" "$BINARY" "$(mktemp -u).png" ${LWE_ASSETS_DIR:+--assets-dir "$LWE_ASSETS_DIR"} \
  34. --silent "$@" "$ITEM" > "$LOG" 2>&1 &
  35. ENGINE=$!
  36. # the client listens on 8086 once the first frame is out, scenes take a while to load
  37. for _ in $(seq 90); do
  38. ss -ltn 2>/dev/null | grep -q ':8086 ' && break
  39. kill -0 "$ENGINE" 2>/dev/null || { echo "the engine stopped before Tracy could connect:" >&2; tail -20 "$LOG" >&2; exit 1; }
  40. sleep 1
  41. done
  42. sleep 3
  43. timeout $((SECONDS_TO_CAPTURE + 30)) tracy-capture -o "$OUT" -a 127.0.0.1 -s "$SECONDS_TO_CAPTURE" -f > /dev/null
  44. kill -- "-$ENGINE" 2>/dev/null || true
  45. # timeout(1) moves the engine into a process group of its own, which the kill above does not reach
  46. ENGINE_PID="$(ss -ltnp 'sport = :8086' 2>/dev/null | grep -o 'pid=[0-9]*' | head -n 1 | cut -d= -f2 || true)"
  47. [ -n "$ENGINE_PID" ] && kill "$ENGINE_PID" 2>/dev/null || true
  48. wait "$ENGINE" 2>/dev/null || true
  49. rm -f "$LOG"
  50. echo "capture: $OUT"
  51. "$SCRIPT_DIR/tracy_summary.py" "$OUT"