#!/usr/bin/env bash # Record a CPU profile of one basemind command against a repository. # # scripts/profile.sh [scan|rescan|-- ] # # Builds the `profiling` cargo profile (release code generation plus line tables), then runs the # command under `samply record`, or under the xctrace Time Profiler when samply is missing. The run # gets a throwaway data home and comms directory, so it never reads or writes a user's cache; the # default `scan` is therefore always cold. The profile and the data home land in $PROFILE_OUT # (default: a fresh directory under the temp dir, printed at the end). # # Environment: # PROFILE_FEATURES cargo features to build (default: full) # PROFILE_OUT directory for the profile and the isolated data home (default: mktemp -d) # PROFILE_NO_BUILD set to 1 to reuse an existing target/profiling/basemind # PROFILE_OPEN set to 1 to serve and open the profile when samply finishes # PROFILE_FRAME_POINTERS # set to 1 to build with -C force-frame-pointers=yes (better stacks through # code without unwind tables, at a small cost in speed) # # The xctrace fallback is macOS only. set -euo pipefail if [ $# -lt 1 ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then echo "usage: scripts/profile.sh [scan|rescan|-- ]" >&2 echo "environment: PROFILE_FEATURES PROFILE_OUT PROFILE_NO_BUILD PROFILE_OPEN PROFILE_FRAME_POINTERS" >&2 exit 2 fi repo="$1" shift [ -d "$repo" ] || { echo "profile.sh: $repo is not a directory" >&2; exit 2; } repo="$(cd "$repo" && pwd)" mode="${1:-scan}" case "$mode" in scan | rescan) cmd=("$mode" --root "$repo") ;; --) shift [ $# -gt 0 ] || { echo "profile.sh: nothing after --" >&2; exit 2; } cmd=("$@") ;; *) echo "profile.sh: unknown mode '$mode' (scan, rescan or -- )" >&2 exit 2 ;; esac here="$(cd "$(dirname "$0")/.." && pwd)" features="${PROFILE_FEATURES:-full}" out="${PROFILE_OUT:-$(mktemp -d "${TMPDIR:-/tmp}/basemind-profile.XXXXXX")}" mkdir -p "$out/data" "$out/comms" if [ "${PROFILE_NO_BUILD:-0}" != "1" ]; then if [ "${PROFILE_FRAME_POINTERS:-0}" = "1" ]; then export RUSTFLAGS="${RUSTFLAGS:-} -C force-frame-pointers=yes" fi (cd "$here" && cargo build --profile profiling --features "$features" --bin basemind) fi target_dir="$(cd "$here" && cargo metadata --format-version 1 --no-deps | python3 -c 'import json, sys; print(json.load(sys.stdin)["target_directory"])')" bin="$target_dir/profiling/basemind" [ -x "$bin" ] || { echo "profile.sh: $bin was not built" >&2; exit 1; } export BASEMIND_DATA_HOME="$out/data" export BASEMIND_COMMS_DIR="$out/comms" stamp="$(date +%Y%m%d-%H%M%S)" if command -v samply >/dev/null 2>&1; then profile="$out/profile-$stamp.json.gz" serve_flags=(--save-only) [ "${PROFILE_OPEN:-0}" = "1" ] && serve_flags=() samply record "${serve_flags[@]}" -o "$profile" -- "$bin" "${cmd[@]}" echo echo "profile: $profile" echo "open it: samply load $profile" elif command -v xctrace >/dev/null 2>&1; then profile="$out/profile-$stamp.trace" xctrace record --template 'Time Profiler' --output "$profile" --launch -- "$bin" "${cmd[@]}" echo echo "profile: $profile" echo "open it: open $profile (Instruments)" else echo "profile.sh: install samply (cargo install samply) or Xcode's xctrace" >&2 exit 1 fi echo "data home (isolated, safe to delete): $out"