temp / debug_gpu.sh
Cccccz's picture
Upload debug_gpu.sh
78d1d73 verified
Raw
History Blame Contribute Delete
19.9 kB
#!/bin/bash
# Script to display the resource usage of running/pending jobs, users, and nodes
# on a Slurm-based HPC system.
# Parse command line arguments
filter_user=""
filter_node=""
# Help message
show_help() {
echo "Usage: $0 [options]"
echo "Options:"
echo " -u, --user [USER] Filter results by username (defaults to $USER if no argument provided)"
echo " -n, --node [NODE] Filter results by node name (defaults to $HOSTNAME if no argument provided)"
echo " -h, --help Show this help message"
exit 0
}
# Parse command line options
while [[ $# -gt 0 ]]; do
case "$1" in
-u|--user)
if [[ -n "$2" && ! "$2" =~ ^- ]]; then
filter_user="$2"
shift 2
else
# Use current user as default
filter_user="$USER"
shift 1
fi
;;
-n|--node)
if [[ -n "$2" && ! "$2" =~ ^- ]]; then
filter_node="$2"
shift 2
else
# Use current hostname as default
filter_node="$HOSTNAME"
shift 1
fi
;;
-h|--help)
show_help
;;
*)
echo "Unknown option: $1"
show_help
;;
esac
done
# Print active filters
if [[ -n "$filter_user" || -n "$filter_node" ]]; then
echo -e "\033[1;35mActive filters:\033[0m"
[[ -n "$filter_user" ]] && echo -e " User: \033[1;35m$filter_user\033[0m"
[[ -n "$filter_node" ]] && echo -e " Node: \033[1;35m$filter_node\033[0m"
echo
fi
# Set output format for squeue command
# JOBID | PARTITION | NAME | USER | STATE | TIME | NODES | REASON | NODELIST | CPUS | TRES_PER_NODE | MIN_MEMORY | REQ_NODES | COMMAND | WORK_DIR
squeue_format="%i | %P | %j | %u | %T | %M | %D | %r | %N | %C | %b | %m | %n | %o | %Z"
# Get squeue output for the specified partition
squeue_output=$(squeue -p q-g7hdnrvb --states=RUNNING,PENDING --format="$squeue_format")
declare -a HEADERS
declare -A HEADER_INDEX
declare -i HEADER_COUNT
declare -A jobidx_statidx
declare -i job_count
# Parse header and data using awk
parse_squeue_output() {
local command_output="$1"
echo "$command_output" | awk -F' *\\| *' '
NR == 1 {
for (i = 1; i <= NF; i++) {
gsub(/^[ \t]+|[ \t]+$/, "", $i); # Trim whitespace
printf("HEADERS[%d]=\"%s\"\n", i-1, $i);
printf("HEADER_INDEX[\"%s\"]=%d\n", $i, i-1);
}
printf("HEADER_COUNT=%d\n", NF);
}
NR > 1 {
# Store all fields for this job
for (i = 1; i <= NF; i++) {
gsub(/^[ \t]+|[ \t]+$/, "", $i); # Trim whitespace
printf("jobidx_statidx[\"%d,%d\"]=\"%s\"\n", NR-2, i-1, $i);
}
}
END {
printf("job_count=%d\n", NR-1);
}'
}
eval "$(parse_squeue_output "$squeue_output")"
# Get list of nodes and their number of GPUs
sinfo_format="NodeHost: | ,Gres: | ,GresUsed:30"
sinfo_output=$(sinfo -p q-g7hdnrvb -h -N --Format="$sinfo_format")
declare -a node_names
declare -A gpu_counts
declare -A used_gpu_counts
# Process the output to extract node names and GPU counts
while IFS=" | " read -r node gres gres_used; do
# Extract just the number from gpu:8(S:0-1) format
gpu_count=$(echo "$gres" | grep -o "gpu:[0-9]*" | cut -d':' -f2)
# Extract just the number from gpu:(null):8(IDX:0-7) format
gpu_used=$(echo "$gres_used" | grep -o "gpu:(null):[0-9]*" | cut -d':' -f3)
# Handle case where gpu_count is empty (no GPUs or different format)
if [[ -z "$gpu_count" ]]; then
gpu_count=0
fi
# Store in arrays
node_names+=("$node")
gpu_counts["$node"]="$gpu_count"
used_gpu_counts["$node"]="$gpu_used"
done < <(echo "$sinfo_output")
# Sort the node names array
IFS=$'\n' node_names=($(sort <<<"${node_names[*]}"))
# Function used to expand host patterns (e.g., hk01dgx[025-027, 030, 032-033] -> hk01dgx025, hk01dgx026, hk01dgx027, hk01dgx030, hk01dgx032, hk01dgx033)
get_expanded_hosts() {
local input="$1"
local result=()
# Check if input contains bracket pattern
if [[ "$input" == *"["*"]"* ]]; then
# Extract prefix and the content inside brackets
local prefix="${input%%\[*}"
local bracket_content="${input#*\[}"
bracket_content="${bracket_content%\]*}"
# Split the bracket content by comma
IFS=',' read -ra patterns <<< "$bracket_content"
for pattern in "${patterns[@]}"; do
# Trim whitespace
pattern=$(echo "$pattern" | sed 's/^ *//;s/ *$//')
# Check if pattern contains range (e.g., 025-027)
if [[ "$pattern" == *"-"* ]]; then
local start="${pattern%-*}"
local end="${pattern#*-}"
# Convert to integers (removing leading zeros)
local start_num=$((10#${start}))
local end_num=$((10#${end}))
# Generate range
for ((i=start_num; i<=end_num; i++)); do
# Format the number with leading zeros
formatted_num=$(printf "%0${#start}d" $i)
result+=("$prefix$formatted_num")
done
else
# Single item
result+=("$prefix$pattern")
fi
done
else
# No pattern, just return the input as is
result+=("$input")
fi
# Return each array element on a separate line
printf '%s\n' "${result[@]}"
}
# Load user list from file
userlist_file="/home/dyvm6xra/dyvm6xrauser04/bin/sgpu.userlist"
declare -A username_realname
while read -r line; do
username="${line%% *}" # 第一个字段(用户名)
realname="${line#"$username"}" # 剩下的部分(姓名)
realname="${realname#"${realname%%[![:space:]]*}"}" # 去掉前导空格
username_realname["$username"]="$realname"
done < "$userlist_file"
# Add Job Summary Section Header - bright yellow
echo -e "\033[1;93m■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ SUMMARY BY JOB ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■\033[0m"
users_with_job=""
declare -A user_stats
declare -A node_stats
declare -A user_node_stats
# Get running jobs - bright green
echo -e "\033[1;92m▶▶▶ RUNNING JOBS ◀◀◀\033[0m"
# Column headers for running jobs
echo -e "\033[1;36m------------------------------------------------------------------------------------------------------------\033[0m"
echo -e "\033[1;36mJOBID PARTITION NAME USER STATE TIME NODES CPUS MIN_MEMORY GPUS NODELIST\033[0m"
echo -e "\033[1;36m------------------------------------------------------------------------------------------------------------\033[0m"
# Loop through each running job, record user-node-specific information and print running jobs details, apply filters if specified
num_displayed_running=0
for ((i=0; i<job_count; i++)); do
jobid="${jobidx_statidx["${i},${HEADER_INDEX["JOBID"]}"]}"
job_partition="${jobidx_statidx["${i},${HEADER_INDEX["PARTITION"]}"]}"
job_name="${jobidx_statidx["${i},${HEADER_INDEX["NAME"]}"]}"
job_user="${jobidx_statidx["${i},${HEADER_INDEX["USER"]}"]}"
job_state="${jobidx_statidx["${i},${HEADER_INDEX["STATE"]}"]}"
job_time="${jobidx_statidx["${i},${HEADER_INDEX["TIME"]}"]}"
job_nodes="${jobidx_statidx["${i},${HEADER_INDEX["NODES"]}"]}"
job_cpus="${jobidx_statidx["${i},${HEADER_INDEX["CPUS"]}"]}"
job_min_memory="${jobidx_statidx["${i},${HEADER_INDEX["MIN_MEMORY"]}"]}"
job_gpus="${jobidx_statidx["${i},${HEADER_INDEX["TRES_PER_NODE"]}"]}"
job_nodelist="${jobidx_statidx["${i},${HEADER_INDEX["NODELIST"]}"]}"
# Skip this job if it is not in the RUNNING state
if [[ "$job_state" != "RUNNING" ]]; then
continue
fi
# Skip this job if user filter is applied and it doesn't match
if [[ -n "$filter_user" && "$job_user" != "$filter_user" ]]; then
continue
fi
# Skip this job if node filter is applied and it doesn't match
if [[ -n "$filter_node" ]]; then
# Check if the node list contains the target node
node_found=false
while read -r node; do
if [[ "$node" == "$filter_node" ]]; then
node_found=true
break
fi
done <<< "$(get_expanded_hosts "$job_nodelist")"
[[ "$node_found" == false ]] && continue
fi
# Add user to the list of users with running/pending jobs
users_with_job+="$job_user\n"
# Record user-specific, node-specific, and user-node-specific information
job_min_memory_parsed="${job_min_memory%[A-Z]*}"
((user_stats["$job_user,running"]++))
while read -r node; do
if [[ -n "$filter_node" && "$node" != "$filter_node" ]]; then
continue
fi
((user_stats["$job_user,cpus"]+=(job_cpus/job_nodes)))
((user_stats["$job_user,min_memory"]+=job_min_memory_parsed))
((user_stats["$job_user,gpus"]+=(${job_gpus:9}+0)))
((node_stats["$node,running"]++))
((node_stats["$node,cpus"]+=(job_cpus/job_nodes)))
((node_stats["$node,min_memory"]+=job_min_memory_parsed))
((node_stats["$node,gpus"]+=(${job_gpus:9}+0)))
((user_node_stats["$job_user,$node,running"]++))
((user_node_stats["$job_user,$node,cpus"]+=(job_cpus/job_nodes))) # job cpus is total, divide by nodes
((user_node_stats["$job_user,$node,min_memory"]+=job_min_memory_parsed)) # job min_memory is per node
((user_node_stats["$job_user,$node,gpus"]+=(${job_gpus:9}+0))) # job gpus is per node
done <<< "$(get_expanded_hosts "$job_nodelist")"
# Print job details with color formatting
printf "%-6.6s %-11.11s %-10.10s %-15.15s %-8.8s %-11.11s %-6.6s %-5.5s %-11.11s %-5.5s %-s\n" "$jobid" "$job_partition" "$job_name" "$job_user" "$job_state" "$job_time" "$job_nodes" "$job_cpus" "$job_min_memory" "${job_gpus:9}" "$job_nodelist"
((num_displayed_running++))
done
if [[ $num_displayed_running -eq 0 ]]; then
echo -e "\033[1;31mNo running jobs found\033[0m"
fi
# Get pending jobs - bright orange
if [[ -n "$filter_node" ]]; then
# Include a note that node filter is not applicable for pending jobs
echo -e "\n\033[38;5;208m▶▶▶ PENDING JOBS ◀◀◀ \033[1;33m(Node filter not applicable for pending jobs)\033[0m"
else
echo -e "\n\033[38;5;208m▶▶▶ PENDING JOBS ◀◀◀\033[0m"
fi
# Column headers for pending jobs
echo -e "\033[1;36m------------------------------------------------------------------------------------------------------------\033[0m"
echo -e "\033[1;36mJOBID PARTITION NAME USER STATE NODES REASON CPUS MIN_MEMORY GPUS REQ_NODES\033[0m"
echo -e "\033[1;36m------------------------------------------------------------------------------------------------------------\033[0m"
# Loop through each pending job, record number of pending jobs per user and print pending jobs details, apply filters if specified
num_displayed_pending=0
for ((i=0; i<job_count; i++)); do
jobid="${jobidx_statidx["${i},${HEADER_INDEX["JOBID"]}"]}"
job_partition="${jobidx_statidx["${i},${HEADER_INDEX["PARTITION"]}"]}"
job_name="${jobidx_statidx["${i},${HEADER_INDEX["NAME"]}"]}"
job_user="${jobidx_statidx["${i},${HEADER_INDEX["USER"]}"]}"
job_state="${jobidx_statidx["${i},${HEADER_INDEX["STATE"]}"]}"
job_nodes="${jobidx_statidx["${i},${HEADER_INDEX["NODES"]}"]}"
job_reason="${jobidx_statidx["${i},${HEADER_INDEX["REASON"]}"]}"
job_cpus="${jobidx_statidx["${i},${HEADER_INDEX["CPUS"]}"]}"
job_min_memory="${jobidx_statidx["${i},${HEADER_INDEX["MIN_MEMORY"]}"]}"
job_gpus="${jobidx_statidx["${i},${HEADER_INDEX["TRES_PER_NODE"]}"]}"
job_req_nodes="${jobidx_statidx["${i},${HEADER_INDEX["REQ_NODES"]}"]}"
# Skip this job if it is not in the PENDING state
if [[ "$job_state" != "PENDING" ]]; then
continue
fi
# Skip this job if user filter is applied and it doesn't match
if [[ -n "$filter_user" && "$job_user" != "$filter_user" ]]; then
continue
fi
# Add user to the list of users with running/pending jobs
users_with_job+="$job_user\n"
# Record number of pending jobs per user
((user_stats["$job_user,pending"]++))
# Print job details with color formatting
printf "%-6.6s %-11.11s %-10.10s %-15.15s %-8.8s %-6.6s %-11.11s %-5.5s %-11.11s %-5.5s %-s\n" "$jobid" "$job_partition" "$job_name" "$job_user" "$job_state" "$job_nodes" "$job_reason" "$job_cpus" "$job_min_memory" "${job_gpus:9}" "$job_req_nodes"
((num_displayed_pending++))
done
if [[ $num_displayed_pending -eq 0 ]]; then
echo -e "\033[1;31mNo pending jobs found\033[0m"
fi
# Add user summary section header - bright purple
echo -e "\n\033[1;95m■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ SUMMARY BY USER ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■\033[0m"
# Column headers for user summary
echo -e "\033[1;36m------------------------------------------------------------------------------------------------------------\033[0m"
echo -e "\033[1;36mUSER PENDING/NODE RUNNING CPUS MIN_MEMORY GPUS REALNAME\033[0m"
echo -e "\033[1;36m------------------------------------------------------------------------------------------------------------\033[0m"
# Get sorted list of users that have at least one running/pending job
users_with_job=$(echo -e "$users_with_job" | sort -u)
# Loop through each user, print user summary details, apply filters if specified
num_displayed_users=0
total_pending=0
total_running=0
total_cpus=0
total_min_memory=0
total_gpus=0
for user in $users_with_job; do
# Skip this user if node filter is applied and the user has no running job on the target node
if [[ -n "$filter_node" && -z "${user_node_stats["$user,$filter_node,running"]}" ]]; then
continue
fi
# Print divider between users
if [[ $num_displayed_users -gt 0 ]]; then
echo -e "\033[1;36m-------------------------------------------------------------------------\033[0m"
fi
# Print user summary details
((user_stats["$user,pending"]+=0)) # Initialize to 0 if not set
((user_stats["$user,running"]+=0)) # Initialize to 0 if not set
((user_stats["$user,cpus"]+=0)) # Initialize to 0 if not set
((user_stats["$user,min_memory"]+=0)) # Initialize to 0 if not set
((user_stats["$user,gpus"]+=0)) # Initialize to 0 if not set
printf "\e[0;95m%-15.15s %-15.15s %-8.8s %-5.5s %-11.11s %-5.5s %s\n\e[0m" "$user" "${user_stats["$user,pending"]}" "${user_stats["$user,running"]}" "${user_stats["$user,cpus"]}" "${user_stats["$user,min_memory"]}G" "${user_stats["$user,gpus"]}" "${username_realname["$user"]}"
# Print nodes where this user has running jobs
for node in "${node_names[@]}"; do
if [[ -z "${user_node_stats["$user,$node,running"]}" ]]; then
continue
fi
printf " %-15.15s %-8.8s %-5.5s %-11.11s %s\n" "$node" "${user_node_stats["$user,$node,running"]}" "${user_node_stats["$user,$node,cpus"]}" "${user_node_stats["$user,$node,min_memory"]}G" "${user_node_stats["$user,$node,gpus"]}"
done
((num_displayed_users++))
((total_pending+=user_stats["$user,pending"]))
((total_running+=user_stats["$user,running"]))
((total_cpus+=user_stats["$user,cpus"]))
((total_min_memory+=user_stats["$user,min_memory"]))
((total_gpus+=user_stats["$user,gpus"]))
done
if [[ $num_displayed_users -eq 0 ]]; then
echo -e "\033[1;31mNo users with running/pending jobs found\033[0m"
else
# Print overall summary
echo -e "\033[1;36m------------------------------------------------------------------------------------------------------------\033[0m"
printf "\e[0;95m%-15.15s %-15.15s %-8.8s %-5.5s %-11.11s %s\n\e[0m" "TOTAL" "$total_pending" "$total_running" "$total_cpus" "${total_min_memory}G" "$total_gpus"
fi
# Add node summary section header - bright red
echo -e "\n\033[1;91m■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ SUMMARY BY NODE ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■\033[0m"
# Column headers for node summary
echo -e "\033[1;36m------------------------------------------------------------------------------------------------------------\033[0m"
echo -e "\033[1;36mNODE USER RUNNING CPUS MIN_MEMORY GPUS\033[0m"
echo -e "\033[1;36m------------------------------------------------------------------------------------------------------------\033[0m"
# Loop through each node, print node summary details, apply filters if specified
num_displayed_nodes=0
total_cpus=0
total_min_memory=0
total_gpus=0
for node in "${node_names[@]}"; do
# Skip this node if node filter is applied and it doesn't match
if [[ -n "$filter_node" && "$node" != "$filter_node" ]]; then
continue
fi
# Skip this node if user filter is applied and the target user has no running job on this node
if [[ -n "$filter_user" && -z "${user_node_stats["$filter_user,$node,running"]}" ]]; then
continue
fi
# Print divider between nodes
if [[ $num_displayed_nodes -gt 0 ]]; then
echo -e "\033[1;36m-------------------------------------------------------------------------\033[0m"
fi
# Print node summary details
((node_stats["$node,running"]+=0)) # Initialize to 0 if not set
((node_stats["$node,cpus"]+=0)) # Initialize to 0 if not set
((node_stats["$node,min_memory"]+=0)) # Initialize to 0 if not set
((node_stats["$node,gpus"]+=0)) # Initialize to 0 if not set
if [[ "${used_gpu_counts["$node"]}" -lt "${gpu_counts["$node"]}" ]]; then
printf "\e[0;92m%-31.31s %-8.8s %-5.5s %-11.11s %-5.5s %s\n\e[0m" "$node" "${node_stats["$node,running"]}" "${node_stats["$node,cpus"]}" "${node_stats["$node,min_memory"]}G" "${node_stats["$node,gpus"]}" "Available GPUs: $((${gpu_counts["$node"]}-${used_gpu_counts["$node"]}))"
else
printf "\e[0;91m%-31.31s %-8.8s %-5.5s %-11.11s %s\n\e[0m" "$node" "${node_stats["$node,running"]}" "${node_stats["$node,cpus"]}" "${node_stats["$node,min_memory"]}G" "${node_stats["$node,gpus"]}"
fi
# Print users running jobs on this node
for user in $users_with_job; do
if [[ -z "${user_node_stats["$user,$node,running"]}" ]]; then
continue
fi
printf " %-15.15s %-8.8s %-5.5s %-11.11s %-5.5s %s\n" "$user" "${user_node_stats["$user,$node,running"]}" "${user_node_stats["$user,$node,cpus"]}" "${user_node_stats["$user,$node,min_memory"]}G" "${user_node_stats["$user,$node,gpus"]}" "${username_realname["$user"]}"
done
((num_displayed_nodes++))
((total_cpus+=node_stats["$node,cpus"]))
((total_min_memory+=node_stats["$node,min_memory"]))
((total_gpus+=node_stats["$node,gpus"]))
done
if [[ $num_displayed_nodes -eq 0 ]]; then
echo -e "\033[1;31mNo nodes with running jobs found\033[0m"
else
# Print overall summary
echo -e "\033[1;36m------------------------------------------------------------------------------------------------------------\033[0m"
printf "\e[0;91m%-17.17s %-13.13s %-8.8s %-5.5s %-11.11s %s\n\e[0m" "TOTAL" "" "" "$total_cpus" "${total_min_memory}G" "$total_gpus"
fi