File size: 19,945 Bytes
78d1d73 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 | #!/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
|