#!/bin/bash set -euo pipefail cd "$(dirname "$0")/.." if [[ $# -gt 1 || ( $# -eq 1 && ! $1 =~ ^[1-9][0-9]*$ ) ]]; then printf 'Usage: %s [running-Parcel-pid]\n' "$0" >&2 exit 2 fi pid=${1:-} if [[ -z $pid ]]; then pid=$(pgrep -x Parcel) || { printf 'No running Parcel; this report will not launch it.\n' >&2; exit 2; } fi [[ $pid =~ ^[1-9][0-9]*$ ]] || { printf 'Select one Parcel PID explicitly.\n' >&2; exit 2; } process=$(ps -p "$pid" -o comm=) [[ ${process##*/} == Parcel ]] || { printf 'PID is not Parcel.\n' >&2; exit 2; } mkdir -p build/idle output=$(mktemp -d "$PWD/build/idle/report.XXXXXX") top_pid= cleanup() { if [[ -n $top_pid ]]; then kill "$top_pid" 2>/dev/null || true; wait "$top_pid" 2>/dev/null || true; fi; } trap cleanup EXIT # Noninteractive sudo fails clearly; authorize the scoped tool before running this report. top -l 61 -s 1 -pid "$pid" -stats pid,cpu,idlew,power > "$output/top.txt" 2> "$output/top.stderr" & top_pid=$! status=0 # Report files intentionally belong to the invoking user, not root. # shellcheck disable=SC2024 sudo -n /usr/bin/powermetrics --samplers tasks --show-process-energy --show-process-wakeups -i 1000 -n 60 \ > "$output/powermetrics.txt" 2> "$output/powermetrics.stderr" || status=$? if (( status != 0 )); then printf 'powermetrics failed (%s); inspect %s/powermetrics.stderr.\n' "$status" "$output" >&2 exit "$status" fi wait "$top_pid" top_pid= kill -0 "$pid" || { printf 'Parcel exited during the report.\n' >&2; exit 2; } Scripts/parcel-dev.sh idle-summary Parcel --pid "$pid" --powermetrics "$output/powermetrics.txt" \ --top "$output/top.txt" --output "$output/summary.json" printf 'Idle diagnostics (report-only): %s\n' "$output"