-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperformance-setup.yaml
More file actions
245 lines (228 loc) · 8.16 KB
/
performance-setup.yaml
File metadata and controls
245 lines (228 loc) · 8.16 KB
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
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: rpi-performance-setup
labels:
app.kubernetes.io/name: rpi-performance-setup
app.kubernetes.io/part-of: spot
spec:
selector:
matchLabels:
app.kubernetes.io/name: rpi-performance-setup
template:
metadata:
labels:
app.kubernetes.io/name: rpi-performance-setup
app.kubernetes.io/part-of: spot
spec:
hostPID: true
hostNetwork: true
restartPolicy: Always
nodeSelector:
kubernetes.io/arch: arm64
gorizond.io/robot: "true"
tolerations:
- operator: "Exists"
effect: "NoSchedule"
- operator: "Exists"
effect: "NoExecute"
containers:
- name: performance-setup
image: ubuntu:24.04
securityContext:
privileged: true
runAsUser: 0
runAsGroup: 0
allowPrivilegeEscalation: true
command: ["/bin/bash", "-c"]
args:
- |
#!/bin/bash
echo "Setting up RPi performance mode..."
# Wait for the host filesystem to be available
while [ ! -f /host/etc/os-release ]; do
echo "Waiting for host filesystem..."
sleep 5
done
# Check if we're on a Raspberry Pi
if grep -q "Raspberry Pi" /host/etc/os-release 2>/dev/null || grep -qi "raspberry" /host/proc/device-tree/model 2>/dev/null; then
echo "Raspberry Pi detected, applying performance settings..."
log() {
echo "[$(date -Iseconds)] $*"
}
BOOT_CONFIG=""
for candidate in /host/boot/firmware/config.txt /host/boot/config.txt; do
if [ -f "${candidate}" ]; then
BOOT_CONFIG="${candidate}"
break
fi
done
if [ -z "${BOOT_CONFIG}" ]; then
log "ERROR: cannot find boot config (tried /boot/firmware/config.txt and /boot/config.txt)"
sleep infinity
fi
MARK_DIR="/host/var/lib/spot-performance-setup"
mkdir -p "${MARK_DIR}"
REBOOT_MARK="${MARK_DIR}/reboot-requested"
REBOOT_REQUIRED_MARK="${MARK_DIR}/reboot-required"
changed=0
ensure_header() {
if ! grep -q "Added by spot performance setup" "${BOOT_CONFIG}"; then
{
echo ""
echo "# Added by spot performance setup"
} >>"${BOOT_CONFIG}"
fi
}
ensure_param() {
local key="$1"
local val="$2"
if grep -Eq "^[[:space:]]*${key}=${val}([[:space:]]|$)" "${BOOT_CONFIG}"; then
return 0
fi
if grep -Eq "^[[:space:]]*#*[[:space:]]*${key}=" "${BOOT_CONFIG}"; then
if sed -i "s|^[[:space:]]*#*[[:space:]]*${key}=.*|${key}=${val}|" "${BOOT_CONFIG}"; then
changed=1
fi
else
ensure_header
if echo "${key}=${val}" >>"${BOOT_CONFIG}"; then
changed=1
fi
fi
}
# Ensure boot-time performance settings (applied after reboot)
ensure_param "arm_freq_min" "1500"
ensure_param "core_freq_min" "500"
ensure_param "sdram_freq_min" "500"
ensure_param "over_voltage_min" "0"
# Set CPU governor to performance
GOVERNOR_PATH="/host/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor"
if [ -w "$GOVERNOR_PATH" ]; then
echo "performance" > "$GOVERNOR_PATH" || echo "Failed to set CPU governor"
fi
if [ "${changed}" -eq 1 ]; then
log "Boot config updated (${BOOT_CONFIG}); reboot is required"
date -Iseconds >"${REBOOT_REQUIRED_MARK}" || true
if [ -f "${REBOOT_MARK}" ]; then
log "Reboot already requested; waiting for node to restart"
sleep infinity
fi
if [ "${SPOT_PERF_SETUP_REBOOT:-1}" != "1" ]; then
log "Reboot is required but disabled (SPOT_PERF_SETUP_REBOOT!=1)"
sleep infinity
fi
date -Iseconds >"${REBOOT_MARK}" || true
sync || true
sleep 5
reboot_initiated=0
log "Rebooting via host systemd..."
if [ -x /host/bin/systemctl ]; then
if chroot /host /bin/systemctl reboot --no-wall; then
reboot_initiated=1
fi
elif [ -x /host/usr/bin/systemctl ]; then
if chroot /host /usr/bin/systemctl reboot --no-wall; then
reboot_initiated=1
fi
fi
log "Rebooting via host reboot(8)..."
if [ "${reboot_initiated}" -eq 0 ] && [ -x /host/sbin/reboot ]; then
if chroot /host /sbin/reboot -f; then
reboot_initiated=1
fi
fi
if [ "${reboot_initiated}" -eq 1 ]; then
log "Reboot command executed; waiting for node to go down..."
sleep 60
fi
log "Forcing reboot via sysrq (fallback)..."
echo 1 >/proc/sys/kernel/sysrq || true
echo s >/proc/sysrq-trigger || true
sleep 2
echo u >/proc/sysrq-trigger || true
sleep 2
echo b >/proc/sysrq-trigger || true
sleep infinity
fi
# Apply settings periodically
while true; do
# Set CPU governor to performance (in case it changed)
if [ -w "$GOVERNOR_PATH" ]; then
echo "performance" > "$GOVERNOR_PATH" 2>/dev/null || true
fi
# Monitor throttling status
if [ -f /host/usr/bin/vcgencmd ]; then
THROTTLE_STATUS=$(chroot /host /bin/bash -c "vcgencmd get_throttled" 2>/dev/null || echo "unknown")
if [[ "$THROTTLE_STATUS" =~ throttled=0x[1-9a-fA-F] ]]; then
echo "WARNING: Throttling detected: $THROTTLE_STATUS"
fi
fi
sleep 30
done
else
echo "Not running on Raspberry Pi, skipping performance setup"
# Just sleep indefinitely if not on RPi
tail -f /dev/null
fi
volumeMounts:
- name: host-etc
mountPath: /host/etc
readOnly: true
- name: host-sys
mountPath: /host/sys
- name: host-proc
mountPath: /host/proc
readOnly: true
- name: host-usr
mountPath: /host/usr
readOnly: true
- name: host-opt
mountPath: /host/opt
readOnly: true
- name: host-dev
mountPath: /host/dev
- name: host-boot
mountPath: /host/boot
- name: host-root
mountPath: /host
resources:
requests:
cpu: 10m
memory: 32Mi
limits:
cpu: 100m
memory: 64Mi
volumes:
- name: host-etc
hostPath:
path: /etc
type: Directory
- name: host-sys
hostPath:
path: /sys
type: Directory
- name: host-proc
hostPath:
path: /proc
type: Directory
- name: host-usr
hostPath:
path: /usr
type: Directory
- name: host-opt
hostPath:
path: /opt
type: Directory
- name: host-dev
hostPath:
path: /dev
type: Directory
- name: host-boot
hostPath:
path: /boot
type: Directory
- name: host-root
hostPath:
path: /
type: Directory