gem/scripts/profile-metal.sh
2026-08-30 18:24:55 -06:00

57 lines
1.7 KiB
Bash
Executable file

#!/bin/sh
# Record a Metal trace for a previously selected benchmark winner.
set -eu
if [ "$#" -ge 3 ] && [ "$2" = -- ]; then
trace=$1
shift 2
elif [ "$#" -eq 3 ]; then
gem=$1
model=$2
trace=$3
set -- "$gem" serve --model "$model"
else
echo "usage: $0 GEM_BINARY MODEL_GGUF TRACE_PATH" >&2
echo " $0 TRACE_PATH -- COMMAND [ARG ...]" >&2
exit 1
fi
if [ -n "${DEVELOPER_DIR:-}" ]; then
developer_dir=$DEVELOPER_DIR
elif xcrun --find xctrace >/dev/null 2>&1; then
developer_dir=
elif [ -d /Applications/Xcode.app/Contents/Developer ]; then
developer_dir=/Applications/Xcode.app/Contents/Developer
elif [ -d /Applications/Xcode-beta.app/Contents/Developer ]; then
developer_dir=/Applications/Xcode-beta.app/Contents/Developer
else
echo "error: xctrace requires Xcode; set DEVELOPER_DIR to its Contents/Developer directory" >&2
exit 1
fi
template=${GEM_XCTRACE_TEMPLATE:-Metal System Trace}
if [ -z "$template" ]; then
echo "error: GEM_XCTRACE_TEMPLATE must not be empty" >&2
exit 1
fi
instrument=${GEM_XCTRACE_INSTRUMENT:-}
if [ -n "$developer_dir" ]; then
if [ -n "$instrument" ]; then
DEVELOPER_DIR=$developer_dir \
xcrun xctrace record --template "$template" --instrument "$instrument" \
--output "$trace" --launch -- "$@"
else
DEVELOPER_DIR=$developer_dir \
xcrun xctrace record --template "$template" --output "$trace" --launch -- \
"$@"
fi
else
if [ -n "$instrument" ]; then
xcrun xctrace record --template "$template" --instrument "$instrument" \
--output "$trace" --launch -- "$@"
else
xcrun xctrace record --template "$template" --output "$trace" --launch -- \
"$@"
fi
fi