/
sonm-fan-control
executable file
·245 lines (206 loc) · 6 KB
/
sonm-fan-control
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
#!/usr/bin/env bash
VERSION="0.3.2"
cleanup() {
if [[ $FAIL_TO_START -eq 0 ]]; then
echo "INFO: Exit. Transfer fan control to driver."
for ((i=0; i<$CARDS_NUM; i++)); do
nvidia-settings -a [gpu:$i]/GPUFanControlState=0
if ! [[ $? -eq 0 ]]; then
echo "CRITICAL: Failed to transfer fan control to the driver. Forcing reboot.."
reboot
fi
if [[ $MANAGE_PL -eq 1 ]]; then
echo "INFO: Adjusting PL for GPU${i} to default value: ${pl_current[$i]} -> ${pl_defaults[$i]}"
nvidia-smi -i $i -pl ${pl_defaults[$i]} &>1
fi
done
nvidia-smi -pm 0
else
echo "INFO: Exit. Fan-control failed to start."
fi
}
init() {
FAIL_TO_START=0
echo "Sonm-fan-control service, version ${VERSION}"
if [[ "$(id -u)" != "0" ]]; then
echo "ERROR: This script must be run as superuser"
FAIL_TO_START=1
exit 1
fi
if ! lspci -nm | grep '"\(0300\|0302\)" "10de"' 2>&1 >/dev/null; then
echo "ERROR: No NVIDIA GPUs detected"
FAIL_TO_START=1
exit 0
fi
if ! [ -f /usr/bin/nvidia-settings ]; then
echo "ERROR: Nvidia-settings not installed, exit.."
FAIL_TO_START=1
exit 1
fi
export DISPLAY=:0
reload_config
CARDS_NUM=`nvidia-smi -L | grep UUID | wc -l`
DELAY=5
echo "INFO: Found ${CARDS_NUM} GPU(s)"
echo $(print_config)
for ((i=0; i<$CARDS_NUM; i++)); do
nvidia-settings -a [gpu:$i]/GPUFanControlState=1 1>/dev/null
if [ "$?" -ne 0 ]; then
echo "ERROR: Cannot take control of the GPU fans. Exit"
exit 1
fi
done
echo "INFO: GPUFanControlState set to 1 for all cards"
if [[ $MANAGE_PL -eq 1 ]]; then
echo "INFO: PL management is ENABLED in config"
enable_pm
set_pl_defaults
set_init_pl
else
echo "INFO: PL management is DISABLED in config"
fi
}
print_config() {
echo "INFO: Fan control settings: TEMP: Min ${MIN_TEMP}°C, Max ${MAX_TEMP}°C, Critical ${CRIT_TEMP}°C; Min fan speed ${MIN_FAN_SPEED}% : "
echo "PL MANAGEMENT: ${MANAGE_PL} (1-on, 0-off), MaxPL ${MAX_PL}%, MinPL ${MIN_PL}%; PL change step ${PL_CHANGE_STEP}W"
}
reload_config() {
MANAGE_PL_OLD=$MANAGE_PL
export $(cat /etc/sonm/fan-control.cfg | grep -v "#")
if [[ -z $SETTINGS_TS ]]; then
SETTINGS_TS=$(date +%s)
fi
CONFIG_CHANGE_TS=$(stat /etc/sonm/fan-control.cfg --format='%Y')
if [[ $CONFIG_CHANGE_TS -gt $SETTINGS_TS ]]; then
echo "INFO: Detected config change"
echo $(print_config)
SETTINGS_TS=$(date +%s)
switch_pl
fi
# check config
if [[ $MIN_TEMP -ge $MAX_TEMP ]]; then
echo "ERROR: Configuration error, MIN_TEMP>=MAX_TEMP"
exit 1
elif [[ $MAX_TEMP -ge $CRIT_TEMP ]]; then
echo "ERROR: Configuration error, MAX_TEMP>=CRIT_TEMP"
exit 1
elif [[ $MIN_FAN_SPEED -lt 0 ]]; then
echo "ERROR: Configuration error, MIN_FAN_SPEED<=0"
exit 1
elif [[ $MIN_FAN_SPEED -gt 100 ]]; then
echo "ERROR: Configuration error, MIN_FAN_SPEED>=100"
exit 1
fi
return 0
}
switch_pl() {
set_pl_defaults
if [[ $(( $MANAGE_PL - $MANAGE_PL_OLD )) -gt 0 ]]; then
echo "INFO: PL management is ENABLED in config"
enable_pm
set_init_pl
elif [[ $(( $MANAGE_PL - $MANAGE_PL_OLD )) -lt 0 ]]; then
echo "INFO: PL management is DISABLED in config"
disable_pm
fi
}
get_gpu_temp() {
temp=$(nvidia-smi -i $1 --query-gpu=temperature.gpu --format=csv,noheader)
ERR=$?
if ! [[ $ERR -eq 0 ]]; then
echo "INFO:" $(nvidia-smi -L)
echo "CRITICAL: Cannot get temp for GPU${1}. Seems like GPU is lost. Forcing reboot"
reboot
fi
if [[ $temp -gt $CRIT_TEMP ]]; then
echo "CRITICAL: GPU${1} exceeded critical temp. Forcing reboot"
reboot
fi
echo $temp
}
set_pl_defaults() {
# TODO: Handle errors
for ((i=0; i<$CARDS_NUM; i++)); do
pl_defaults[$i]=$(nvidia-smi --format=csv,noheader -i $i --query-gpu=power.default_limit | cut -d "." -f1)
pl_min[$i]=$(( pl_defaults[$i] * $MIN_PL / 100 ))
pl_max[$i]=$(( pl_defaults[$i] * $MAX_PL / 100 ))
echo "INFO: Default (driver settings) PL for GPU${i} is ${pl_defaults[$i]}"
echo "INFO: PL range for GPU${i}: ${pl_min[$i]} - ${pl_max[$i]}"
done
}
set_init_pl() {
for ((i=0; i<$CARDS_NUM; i++)); do
nvidia-smi -i $i -pl ${pl_max[$i]} &>1
pl_current[$i]=${pl_max[$i]}
# TODO: Handle errors
echo "INFO: Initial PL for GPU${i} adjusted to ${pl_max[$i]}"
done
}
enable_pm() {
echo "INFO: Enabling persistence mode.."
nvidia-smi -pm 1 &>1
}
disable_pm() {
echo "INFO: Switching to default PL settings.."
for ((i=0; i<$CARDS_NUM; i++)); do
nvidia-smi -i $i -pl ${pl_defaults[$i]} &>1
# TODO: Handle errors
echo "INFO: PL for GPU${i} adjusted to ${pl_defaults[$i]}"
done
echo "INFO: Disabling persistence mode.."
nvidia-smi -pm 0 &>1
}
adjust_pl() {
# TODO: Handle errors
new_pl=0
if [[ $2 -gt $(( $MAX_TEMP + 5 )) ]]; then
new_pl=$(( ${pl_current[$1]} - $PL_CHANGE_STEP ))
if [[ $new_pl -lt ${pl_min[$1]} ]]; then
new_pl=${pl_min[$1]}
fi
elif [[ $2 -lt $MAX_TEMP ]]; then
new_pl=$(( ${pl_current[$1]} + $PL_CHANGE_STEP ))
if [[ $new_pl -gt ${pl_max[$1]} ]]; then
new_pl=${pl_max[$1]}
fi
else
new_pl=${pl_current[$1]}
fi
if ! [[ $new_pl -eq ${pl_current[$1]} ]]; then
echo "INFO: Adjusting PL for GPU${i}: ${pl_current[$i]} -> ${new_pl}"
pl_current[$1]=$new_pl
nvidia-smi -i $1 -pl $new_pl &>1
fi
}
if [[ -z $1 ]]; then
# Executes cleanup function at script exit.
trap cleanup EXIT
init
while true; do
for ((i=0; i<$CARDS_NUM; i++)); do
GPU_TEMP=$(get_gpu_temp $i)
if [[ $GPU_TEMP -lt $MIN_TEMP ]]; then
FAN_SPEED=$MIN_FAN_SPEED
elif [[ $GPU_TEMP -gt $MAX_TEMP ]]; then
echo "WARN: GPU${i} temp ${GPU_TEMP}°C"
FAN_SPEED=100
else
FAN_SPEED=$(( $MIN_FAN_SPEED + ($GPU_TEMP - $MIN_TEMP)*(100 - $MIN_FAN_SPEED)/($MAX_TEMP - $MIN_TEMP) ))
fi
echo "INFO: GPU${i} ${GPU_TEMP}°C, fan -> ${FAN_SPEED}%"
nvidia-settings -a [fan:$i]/GPUTargetFanSpeed=$FAN_SPEED 1>/dev/null
if [[ $MANAGE_PL -eq 1 ]]; then
adjust_pl $i $GPU_TEMP
fi
done
sleep $DELAY
reload_config
done
fi
if [[ $1 == "version" ]]; then
echo "Sonm-fan-control service, version ${VERSION}"
export $(cat /etc/sonm/fan-control.cfg | grep -v "#")
print_config
else
echo "Unknown argument"
fi