mirror of
https://github.com/QuixiAI/nvdriver.git
synced 2026-12-06 06:28:39 +00:00
67 lines
2.8 KiB
Bash
67 lines
2.8 KiB
Bash
#!/bin/sh
|
|
# Health gate for the reboot-free dev loop.
|
|
#
|
|
# sh tools/dev_health.sh [pre|post]
|
|
#
|
|
# pre (default): safe to touch the GPU/module? No GPU node is opened.
|
|
# post: the freshly loaded module answers (opens a node, so it runs under a timeout).
|
|
# Exit 0 = healthy, 1 = stop all GPU work and report to the user, 2 = usage.
|
|
set -u
|
|
if [ "$#" -gt 1 ]; then
|
|
echo "usage: $0 [pre|post]" >&2
|
|
exit 2
|
|
fi
|
|
ROOT=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
|
|
MODE=${1:-pre}
|
|
case "$MODE" in pre|post) ;; *) echo "usage: $0 [pre|post]" >&2; exit 2 ;; esac
|
|
BAD=0
|
|
fail() { echo "FAIL: $*"; BAD=1; }
|
|
warn() { echo "warn: $*"; }
|
|
|
|
TAINT=$(cat /proc/sys/kernel/tainted 2>/dev/null || true)
|
|
case "$TAINT" in
|
|
''|*[!0-9]*) fail "cannot read numeric /proc/sys/kernel/tainted (got '$TAINT')" ;;
|
|
*)
|
|
[ $((TAINT & 128)) -eq 0 ] || fail "kernel has oopsed (taint $TAINT); reboot is the only clean recovery"
|
|
[ $((TAINT & 32)) -eq 0 ] || warn "bad page taint (taint $TAINT)"
|
|
;;
|
|
esac
|
|
|
|
# nvidia_rs_rt* parks in D state by design; any other D-state nvidia thread is a hang.
|
|
STUCK=$(ps -eLo pid=,stat=,comm= | awk '$2 ~ /^D/ && $3 ~ /nv_open_q|nvidia|nv_queue|rmmod|insmod/ && $3 !~ /^nvidia_rs_rt/')
|
|
[ -z "$STUCK" ] || fail "stuck nvidia tasks:
|
|
$STUCK"
|
|
|
|
# /sys/module/nvidia is nvidia_rs's own marker module; stock shows up as a PCI driver.
|
|
if [ -d /sys/bus/pci/drivers/nvidia ]; then
|
|
fail "stock nvidia driver is registered; do not mix it with nvidia_rs"
|
|
fi
|
|
|
|
sudo -n true 2>/dev/null || fail "passwordless sudo is not available"
|
|
|
|
if [ -f "$ROOT/tools/dev_mode.sh" ] && ! sh "$ROOT/tools/dev_mode.sh" status 2>/dev/null | grep -q ': on'; then
|
|
warn "dev mode is off: desktop processes can pin the module (sh tools/dev_mode.sh on)"
|
|
fi
|
|
|
|
if [ "$MODE" = post ]; then
|
|
# A failed safety check above must not be followed by another GPU open.
|
|
[ "$BAD" -eq 0 ] || { echo "health ($MODE): FAILED before the probe; no node was opened"; exit 1; }
|
|
[ -e /sys/module/nvidia_rs ] || fail "nvidia_rs is not loaded"
|
|
fi
|
|
if [ "$MODE" = post ] && [ "$BAD" -eq 0 ]; then
|
|
# The sampled probe is the only step that opens a node; a timeout is a stop signal.
|
|
SMI=$(mktemp) || exit 1
|
|
trap 'rm -f "$SMI"' EXIT
|
|
if ! timeout -k 5 30 sudo -n nvidia-smi -L >"$SMI" 2>&1; then
|
|
fail "nvidia-smi -L failed or timed out (30 s); GSP-RM may be wedged. Do not retry:"
|
|
cat "$SMI"
|
|
else
|
|
cat "$SMI"
|
|
grep -q '^GPU 0:' "$SMI" || fail "nvidia-smi lists no GPU; runtime init failed (journalctl -k; if -12/ENOMEM: sync; echo 3 > /proc/sys/vm/drop_caches; echo 1 > /proc/sys/vm/compact_memory, then reload)"
|
|
sudo -n dmesg --level=err,crit,alert,emerg 2>/dev/null | tail -5 | grep -i nvidia_rs && warn "nvidia_rs logged errors above"
|
|
fi
|
|
fi
|
|
|
|
[ "$BAD" -eq 0 ] && echo "health ($MODE): ok"
|
|
exit "$BAD"
|