|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Copyright 2025- FlagOS Contributors |
| 4 | +# |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +# of this software and associated documentation files (the "Software"), to deal |
| 7 | +# in the Software without restriction, including without limitation the rights |
| 8 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +# copies of the Software, and to permit persons to whom the Software is |
| 10 | +# furnished to do so, subject to the following conditions: |
| 11 | +# |
| 12 | +# The above copyright notice and this permission notice shall be included in all |
| 13 | +# copies or substantial portions of the Software. |
| 14 | +# |
| 15 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +# SOFTWARE. |
| 22 | + |
| 23 | +set -euo pipefail |
| 24 | + |
| 25 | +if [[ -z "${MUSA_VISIBLE_DEVICES:-}" ]]; then |
| 26 | + echo "[WARNING] MUSA_VISIBLE_DEVICES is unset; skipping GPU cleanup." |
| 27 | + exit 0 |
| 28 | +fi |
| 29 | + |
| 30 | +IFS=',' read -ra GPU_IDS <<< "$MUSA_VISIBLE_DEVICES" |
| 31 | +DEVICES=() |
| 32 | +for gpu_id in "${GPU_IDS[@]}"; do |
| 33 | + # Allow whitespace; require non-negative integer IDs. |
| 34 | + gpu_id="${gpu_id//[[:space:]]/}" |
| 35 | + if [[ ! "$gpu_id" =~ ^[0-9]+$ ]]; then |
| 36 | + echo "[FATAL] Invalid MUSA_VISIBLE_DEVICES: $MUSA_VISIBLE_DEVICES" >&2 |
| 37 | + exit 1 |
| 38 | + fi |
| 39 | + DEVICES+=("/dev/mtgpu.${gpu_id}") |
| 40 | +done |
| 41 | + |
| 42 | +# Find PIDs using the devices. |
| 43 | +mapfile -t PIDS < <( |
| 44 | + fuser "${DEVICES[@]}" 2>/dev/null \ |
| 45 | + | grep -oE '[0-9]+' \ |
| 46 | + | sort -u |
| 47 | +) |
| 48 | + |
| 49 | +if ((${#PIDS[@]} == 0)); then |
| 50 | + echo "[INFO] No processes found on ${DEVICES[*]}." |
| 51 | + exit 0 |
| 52 | +fi |
| 53 | + |
| 54 | +echo "[INFO] Terminating processes: ${PIDS[*]}." |
| 55 | +ps -fp "${PIDS[@]}" || true |
| 56 | +kill "${PIDS[@]}" 2>/dev/null || true |
| 57 | +sleep 5 |
| 58 | + |
| 59 | +# Force-kill remaining processes. |
| 60 | +mapfile -t REMAINING < <( |
| 61 | + fuser "${DEVICES[@]}" 2>/dev/null \ |
| 62 | + | grep -oE '[0-9]+' \ |
| 63 | + | sort -u |
| 64 | +) |
| 65 | + |
| 66 | +if ((${#REMAINING[@]} > 0)); then |
| 67 | + echo "[WARNING] Force-killing processes: ${REMAINING[*]}" |
| 68 | + kill -9 "${REMAINING[@]}" 2>/dev/null || true |
| 69 | +fi |
| 70 | + |
| 71 | +echo "[INFO] Current usage:" |
| 72 | +fuser -v "${DEVICES[@]}" || true |
0 commit comments