gpu_meter.sh 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #!/bin/bash
  2. # Measures how much of the GPU (and CPU) one wallpaper takes, from the engine's own amdgpu/i915/xe per-client
  3. # counters (/proc/<pid>/fdinfo drm-engine-gfx/render), so other programs on the same GPU don't skew it.
  4. # Renders headlessly through headless_render.sh, waits WARMUP seconds, then samples for SECONDS.
  5. #
  6. # Usage: tools/gpu_meter.sh <engine binary> <workshop item dir> [WxH] [engine args...]
  7. # Env: WARMUP (default 15), SECONDS_SAMPLED (default 15), LWE_ASSETS_DIR. Don't set LWE_FRAME_STATS, its glFinish
  8. # and timer queries roughly triple the numbers.
  9. set -euo pipefail
  10. BINARY=$(readlink -f "$1")
  11. ITEM=$2
  12. shift 2
  13. SIZE=3840x2160
  14. if [[ ${1:-} =~ ^[0-9]+x[0-9]+$ ]]; then
  15. SIZE=$1
  16. shift
  17. fi
  18. WARMUP=${WARMUP:-15}
  19. SAMPLED=${SECONDS_SAMPLED:-15}
  20. OUT=$(mktemp -d)
  21. RUNNER=
  22. trap '[ -n "$RUNNER" ] && kill "$RUNNER" 2>/dev/null; rm -rf "$OUT"' EXIT
  23. ulimit -c 0
  24. HEADLESS_RENDER_KEEP_RUNNING=1 HEADLESS_RENDER_SIZE=$SIZE HEADLESS_RENDER_DELAY=5000 \
  25. HEADLESS_RENDER_TIMEOUT=$((WARMUP + SAMPLED + 10)) \
  26. "$(dirname "$0")/headless_render.sh" "$BINARY" "$OUT/shot.png" \
  27. ${LWE_ASSETS_DIR:+--assets-dir "$LWE_ASSETS_DIR"} "$@" "$ITEM" > "$OUT/log" 2>&1 &
  28. RUNNER=$!
  29. sleep "$WARMUP"
  30. PID=$(pgrep -f "^$BINARY --window 0x0x$SIZE" | head -1 || true)
  31. if [ -z "$PID" ]; then
  32. echo "engine did not start, log:" >&2
  33. cat "$OUT/log" >&2
  34. exit 1
  35. fi
  36. gpu_ns () {
  37. # one fd per DRM client can be listed several times, count each client id once
  38. cat /proc/"$PID"/fdinfo/* 2>/dev/null |
  39. awk '/^drm-client-id/ { c = $2 } /^drm-engine-(gfx|render):/ { if (!(c in s)) { s[c] = 1; t += $2 } } END { print t + 0 }'
  40. }
  41. cpu_ticks () { awk '{ print $14 + $15 }' /proc/"$PID"/stat; }
  42. g0=$(gpu_ns); c0=$(cpu_ticks); t0=$(date +%s.%N)
  43. sleep "$SAMPLED"
  44. g1=$(gpu_ns); c1=$(cpu_ticks); t1=$(date +%s.%N)
  45. awk -v g="$((g1 - g0))" -v c="$((c1 - c0))" -v t0="$t0" -v t1="$t1" -v hz="$(getconf CLK_TCK)" \
  46. 'BEGIN { dt = t1 - t0; printf "gpu %.1f%% cpu %.1f%% of one core\n", g / 1e9 / dt * 100, c / hz / dt * 100 }'
  47. kill "$RUNNER" 2>/dev/null || true
  48. wait "$RUNNER" 2>/dev/null || true