#!/bin/bash # mac-ai-workload-observer.sh: read-only, low-overhead macOS diagnostics. # Usage: bash mac-ai-workload-observer.sh [sample_seconds: 1-60] set -euo pipefail export LC_ALL=C export PATH=/usr/bin:/bin:/usr/sbin:/sbin if [[ "$(uname -s)" != "Darwin" ]]; then printf 'macOS (Darwin) is required.\n' >&2 exit 2 fi seconds="${1-10}" if (( $# > 1 )) || ! [[ "$seconds" =~ ^([1-9]|[1-5][0-9]|60)$ ]]; then printf 'Usage: %s [1-60 seconds]\n' "$0" >&2 exit 2 fi vm_counters() { vm_stat | awk ' /^Swapins:/ && $2 ~ /^[0-9]+\.$/ {swap_in=$2} /^Swapouts:/ && $2 ~ /^[0-9]+\.$/ {swap_out=$2} END { if (swap_in == "" || swap_out == "") exit 1 sub(/\.$/, "", swap_in) sub(/\.$/, "", swap_out) printf "%s %s\n", swap_in, swap_out }' } read_vm_counters() { if ! vm_counters; then printf 'Unable to read macOS swap counters.\n' >&2 return 1 fi } memory_snapshot() { local pressure if pressure=$(memory_pressure -Q 2>/dev/null | grep 'System-wide memory free percentage'); then printf '%s\n' "$pressure" else printf 'System-wide memory free percentage: unavailable\n' fi sysctl vm.swapusage } printf '=== macOS workload snapshot ===\n' printf 'Time: '; date '+%Y-%m-%d %H:%M:%S %Z' printf 'CPU logical cores: '; sysctl -n hw.logicalcpu printf 'Load average: '; uptime | sed -n 's/^.*load averages: / /p' printf '\n=== Initial memory ===\n' memory_snapshot before=$(read_vm_counters) read -r before_in before_out <<< "$before" sleep "$seconds" after=$(read_vm_counters) read -r after_in after_out <<< "$after" printf '\n=== After %s seconds ===\n' "$seconds" printf 'Time: '; date '+%Y-%m-%d %H:%M:%S %Z' printf 'Load average: '; uptime | sed -n 's/^.*load averages: / /p' memory_snapshot if (( after_in < before_in || after_out < before_out )); then printf 'Swap counters decreased during the sample.\n' >&2 exit 1 fi printf 'Swap-in delta (pages): %.0f\n' "$(( after_in - before_in ))" printf 'Swap-out delta (pages): %.0f\n' "$(( after_out - before_out ))" printf '\n=== Data volume free space ===\n' df -h /System/Volumes/Data printf '\n=== Top processes by sampled CPU usage ===\n' ps -Aco pid=,pcpu=,rss=,comm= | sort -k2,2nr | sed -n '1,12p' printf '\nRSS is in KiB. Swap used is a snapshot. Swap deltas are pages during this sample.\n' printf 'The command makes no configuration changes and terminates no process.\n'