Skip to content

Instantly share code, notes, and snippets.

@liamdon
Created September 10, 2024 20:31
Show Gist options
  • Save liamdon/730ccb724774923f088bac7e9cae1cfd to your computer and use it in GitHub Desktop.
Save liamdon/730ccb724774923f088bac7e9cae1cfd to your computer and use it in GitHub Desktop.
#!/bin/bash
# File to store the path of the last moved GPU trace
LAST_TRACE_FILE=$(mktemp /tmp/last_trace_path.XXXXXX)
# Function to process a line
process_line() {
local line="$1"
echo "$line" # Print the line to the terminal
# Check if the line contains the success message
if [[ $line == *"Success starting GPU frame capture at path"* ]]; then
# Wait for eventual consistency of trace
sleep 5
# Extract the file path
filepath=$(echo "$line" | sed -E 's/.*path (file:\/\/[^ ]+).*/\1/')
# Remove the 'file://' prefix
filepath=${filepath#file://}
# Extract the filename
filename=$(basename "$filepath")
# Ensure the gpuTraces directory exists in the current working directory
mkdir -p "./gpuTraces"
# Move the file to the gpuTraces directory
if mv "$filepath" "./gpuTraces/$filename"; then
echo "Moved $filename to ./gpuTraces/$filename"
echo "./gpuTraces/$filename" > "$LAST_TRACE_FILE"
else
echo "Failed to move $filename to ./gpuTraces/$filename"
fi
fi
}
# Function to trigger GPU trace dump
trigger_gpu_trace() {
echo "Triggering GPU trace dump..."
notifyutil -s com.apple.WebKit.WebGPU.CaptureFrame 2 && notifyutil -p com.apple.WebKit.WebGPU.CaptureFrame
echo "GPU trace dump triggered. Check Safari's output for confirmation."
}
# Function to open the last GPU trace
open_last_trace() {
if [ -s "$LAST_TRACE_FILE" ]; then
last_trace=$(cat "$LAST_TRACE_FILE")
echo "Opening last GPU trace: $last_trace"
open "$last_trace"
else
echo "No GPU trace has been captured yet."
fi
}
# Flag to track if we've started quitting
quitting=false
# Function to clean up and exit
clean_exit() {
if ! $quitting; then
quitting=true
echo -e "\nQuitting..."
# Gracefully terminate Safari
pkill -TERM -f "Safari Technology Preview"
# Kill the tail process
kill $tail_pid 2>/dev/null
# Remove the temporary files
rm -f $TEMP_FILE $LAST_TRACE_FILE
fi
exit 0
}
# Create a temporary file to store Safari's output
TEMP_FILE=$(mktemp /tmp/safari_output.XXXXXX)
# Trap to clean up on exit
trap "clean_exit" EXIT INT TERM
# Start Safari independently
__XPC_METAL_CAPTURE_ENABLED=1 /Applications/Safari\ Technology\ Preview.app/Contents/MacOS/Safari\ Technology\ Preview > $TEMP_FILE 2>&1 &
safari_pid=$!
# Wait a bit for Safari to start up
sleep 2
# Use tail to read Safari's output in real-time
tail -f $TEMP_FILE | while IFS= read -r line || [[ -n "$line" ]]; do
process_line "$line"
done &
tail_pid=$!
echo "Safari Technology Preview started with Metal capture enabled."
echo "Press 'Space' or 'Return' to trigger a GPU trace dump."
echo "Press 'o' to open the last captured GPU trace."
echo "Press 'q' to quit."
# Main loop for handling user input
while true; do
IFS= read -r -n1 -s -d '' input
if [[ $? -eq 0 ]]; then
case "$input" in
$' '|$'\n')
echo "Space or Return pressed"
trigger_gpu_trace
;;
o)
echo "o pressed"
open_last_trace
;;
q)
clean_exit
;;
*)
echo "Other key pressed: $(printf '%q' "$input")"
;;
esac
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment