-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogUsageGPU.sh
71 lines (59 loc) · 1.84 KB
/
logUsageGPU.sh
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
#!/bin/bash
# script :logUsageGPU.sh
# description :Script to monitor and log system resource usage
# author :SP@NC (AI)
# date :2025-02-05
# version :1.1
# usage :./logUsage.sh [-i interval] [-o output_path]
# notes :Monitors RAM, CPU, and GPU usage during SLURM or local jobs
# Default values
INTERVAL=10
OUTPUT_PATH="."
# Parse command line options
while getopts ":i:o:" opt; do
case $opt in
i) INTERVAL="$OPTARG"
;;
o) OUTPUT_PATH="$OPTARG"
;;
\?) echo "Invalid option -$OPTARG" >&2
exit 1
;;
esac
done
# set to SLURM job if exists otherwise to 'local'
pfx=${SLURM_JOB_ID:-local}
# Set the output file name using SLURM_JOB_ID
OUTPUT_FILE="${OUTPUT_PATH}/job_${pfx}_metrics.txt"
# Print header
echo "Timestamp,UnixTimestamp,CPU_Usage(cores),RAM_Usage(GB),GPU_Usage(%),GPU_Memory(GB)" > "$OUTPUT_FILE"
# Function to get CPU usage in cores
get_cpu_usage() {
top -bn1 | grep "Cpu(s)" | awk '{print $2 + $4}' | awk '{printf "%.2f", $1 * '"$(nproc)"' / 100}'
}
# Function to get RAM usage in GB
get_ram_usage() {
free -b | awk '/Mem:/ {printf "%.2f", $3 / (1024*1024*1024)}'
}
get_gpu_metrics() {
if command -v nvidia-smi &> /dev/null; then
nvidia-smi --query-gpu=utilization.gpu,memory.used --format=csv,noheader,nounits | awk -F',' '{
sum_util += $1
sum_mem += $2
} END {
printf "%.2f,%.2f", sum_util/NR, sum_mem/(NR*1024)
}'
else
echo "0,0"
fi
}
# Main monitoring loop (end with Ctrl-C)
while true; do
UNIX_TIMESTAMP=$(date +%s)
TIMESTAMP=$(date -d @$UNIX_TIMESTAMP +"%Y-%m-%d %H:%M:%S")
CPU=$(get_cpu_usage)
RAM=$(get_ram_usage)
GPU_METRICS=$(get_gpu_metrics)
echo "${TIMESTAMP},${UNIX_TIMESTAMP},${CPU},${RAM},${GPU_METRICS}" >> "$OUTPUT_FILE"
sleep $INTERVAL
done