-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpupower_2.0_bash.sh
More file actions
452 lines (391 loc) · 16.9 KB
/
cpupower_2.0_bash.sh
File metadata and controls
452 lines (391 loc) · 16.9 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
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
#!/bin/bash
# CPU Control Menu - Improved & Fixed (No Hangs)
# Requires: cpufrequtils, dialog, stress (optional), cpupower (optional)
# ─── Strict mode (without set -e to avoid hangs) ────
set -uo pipefail
# ─── Configuration ────────────────────────────────────────────────────────────
LOGFILE="${HOME}/cpu_control.log"
readonly LOGFILE
readonly RED='\033[0;31m'
readonly GREEN='\033[0;32m'
readonly YELLOW='\033[1;33m'
readonly NC='\033[0m'
# ─── Logging ──────────────────────────────────────────────────────────────────
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*" >> "$LOGFILE"
}
# ─── Error display ────────────────────────────────────────────────────────────
error_msg() {
log "ERROR: $1"
dialog --msgbox "Error: $1" 8 55 2>/dev/null || echo "Error: $1"
}
# ─── Dependency checker (FIXED: no hangs, timeout for read) ───────────────────
check_dep() {
local pkg="$1" cmd="$2"
command -v "$cmd" &>/dev/null && return 0
echo -e "${YELLOW}Missing dependency: $pkg ($cmd)${NC}"
# Use timeout to prevent hang, read with -t timeout
local ans=""
read -t 10 -rp "Install $pkg now? [y/N]: " ans 2>/dev/null || ans="n"
if [[ "$ans" =~ ^[Yy]$ ]]; then
echo "Installing $pkg..."
sudo apt-get update -qq 2>/dev/null || true
if sudo apt-get install -y "$pkg" 2>/dev/null; then
log "Installed dependency: $pkg"
return 0
else
echo -e "${RED}Failed to install $pkg${NC}"
return 1
fi
else
echo -e "${RED}Skipping $pkg. Some features may not work.${NC}"
return 1
fi
}
# ─── CPU helpers ──────────────────────────────────────────────────────────────
get_cpu_count() {
nproc 2>/dev/null || grep -c "^processor" /proc/cpuinfo 2>/dev/null || echo "1"
}
get_cpu_model() {
grep -m1 "model name" /proc/cpuinfo 2>/dev/null | cut -d: -f2 | xargs || echo "Unknown"
}
get_max_cpu_frequency() {
local path="/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq"
if [[ -r "$path" ]]; then
awk '{printf "%.1fGHz", $1/1000000}' "$path" 2>/dev/null
return 0
fi
if command -v cpufreq-info &>/dev/null; then
local val
val=$(cpufreq-info -l 2>/dev/null | awk '{print $2}' | head -1)
[[ -n "$val" ]] && awk -v v="$val" 'BEGIN{printf "%.1fGHz", v/1000000}' && return 0
fi
echo "Unknown"
return 1
}
get_frequency_range() {
local min_path="/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq"
local max_path="/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq"
if [[ -r "$min_path" && -r "$max_path" ]]; then
awk '{printf "%.1fGHz", $1/1000000}' "$min_path" 2>/dev/null | tr -d '\n'
echo -n " - "
awk '{printf "%.1fGHz\n", $1/1000000}' "$max_path" 2>/dev/null
return 0
fi
echo "Unknown"
return 1
}
# ─── Validation ───────────────────────────────────────────────────────────────
validate_frequency() {
local freq="$1"
if [[ ! "$freq" =~ ^[0-9]+(\.[0-9]+)?(GHz|MHz|kHz)?$ ]]; then
error_msg "Invalid frequency format. Use e.g. '3.6GHz' or '2400MHz'."
return 1
fi
return 0
}
# ─── Governor ─────────────────────────────────────────────────────────────────
set_governor() {
local governor="$1"
local failed=0 success=0
log "Setting governor → $governor"
# Check if cpufreq is available
if ! ls /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor 2>/dev/null | head -1 >/dev/null; then
error_msg "cpufreq not available. Check if cpufrequtils is installed and CPU driver loaded."
return 1
fi
while IFS= read -r -d '' cpu; do
if echo "$governor" | sudo tee "$cpu" > /dev/null 2>&1; then
(( success++ )) || true
else
(( failed++ )) || true
log "Failed to set governor for $cpu"
fi
done < <(find /sys/devices/system/cpu -name "scaling_governor" -print0 2>/dev/null)
if [[ $failed -eq 0 && $success -gt 0 ]]; then
dialog --msgbox "Governor set to '$governor' for all $success core(s)." 8 55 2>/dev/null
log "Governor → $governor (ok, $success cores)"
elif [[ $success -eq 0 ]]; then
error_msg "Could not set governor. Is cpufrequtils installed and the driver loaded?"
else
error_msg "Governor set on $success core(s) but failed on $failed core(s)."
fi
}
get_available_governors() {
local path="/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors"
if [[ -r "$path" ]]; then
cat "$path" 2>/dev/null
elif command -v cpufreq-info &>/dev/null; then
cpufreq-info 2>/dev/null | grep "available cpufreq governors" | cut -d: -f2
else
echo "performance powersave"
fi
}
# ─── Frequency setters ────────────────────────────────────────────────────────
set_fixed_frequency_all() {
local freq="$1"
validate_frequency "$freq" || return 1
local failed=0 success=0 ncores
ncores=$(get_cpu_count)
log "Setting fixed frequency → $freq (all cores)"
for (( i=0; i<ncores; i++ )); do
if sudo cpufreq-set -c "$i" -f "$freq" 2>>"$LOGFILE"; then
(( success++ )) || true
else
(( failed++ )) || true
fi
done
if [[ $failed -eq 0 ]]; then
dialog --msgbox "Frequency set to $freq on all $success core(s)." 8 55 2>/dev/null
log "Fixed frequency → $freq (ok)"
else
error_msg "Set $freq on $success core(s); failed on $failed core(s). Check log."
fi
}
set_max_frequency() {
local max_freq
if ! max_freq=$(get_max_cpu_frequency); then
error_msg "Could not detect maximum CPU frequency."
return 1
fi
dialog --yesno "Set all cores to maximum frequency: $max_freq?" 8 60 2>/dev/null || return 0
set_fixed_frequency_all "$max_freq"
}
set_frequency_limit() {
local flag="$1"
local prompt="$2"
local default="$3"
local freq
freq=$(dialog --stdout --inputbox "$prompt" 10 55 "$default" 2>/dev/null) || return 0
[[ -z "$freq" ]] && return 0
validate_frequency "$freq" || return 1
local ncores failed=0 success=0
ncores=$(get_cpu_count)
for (( i=0; i<ncores; i++ )); do
if sudo cpufreq-set -c "$i" "$flag" "$freq" 2>>"$LOGFILE"; then
(( success++ )) || true
else
(( failed++ )) || true
fi
done
if [[ $failed -eq 0 ]]; then
dialog --msgbox "Limit set to $freq on $success core(s)." 8 50 2>/dev/null
log "Freq limit $flag → $freq (ok)"
else
error_msg "Failed on $failed core(s); set on $success. Check log."
fi
}
# ─── Display helpers ──────────────────────────────────────────────────────────
show_frequencies() {
local output=""
for f in /sys/devices/system/cpu/cpu*/cpufreq/scaling_cur_freq; do
if [[ -r "$f" ]]; then
local cpu
cpu=$(echo "$f" | grep -oP 'cpu\d+' 2>/dev/null || echo "cpu?")
local freq_mhz
freq_mhz=$(awk '{printf "%.0f MHz", $1/1000}' "$f" 2>/dev/null || echo "N/A")
output+="${cpu}: ${freq_mhz}\n"
fi
done
if [[ -z "$output" ]]; then
output=$(grep "cpu MHz" /proc/cpuinfo 2>/dev/null | nl -w3 -s'. ' | sed 's/cpu MHz\s*://g' 2>/dev/null || echo "Could not read frequencies")
fi
dialog --title "Current CPU Frequencies" --msgbox "$(printf '%b' "$output")" 30 50 2>/dev/null
}
show_governors() {
local output=""
for g in /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor; do
if [[ -r "$g" ]]; then
local cpu
cpu=$(echo "$g" | grep -oP 'cpu\d+' 2>/dev/null || echo "cpu?")
output+="${cpu}: $(cat "$g" 2>/dev/null)\n"
fi
done
if [[ -z "$output" ]]; then
output="Could not read governors"
fi
dialog --title "Active Governors" --msgbox "$(printf '%b' "$output")" 30 40 2>/dev/null
}
show_cpu_info() {
local cpu_model cores freq_range max_freq
cpu_model=$(get_cpu_model)
cores=$(get_cpu_count)
freq_range=$(get_frequency_range)
max_freq=$(get_max_cpu_frequency 2>/dev/null || echo "Unknown")
local gov="Unknown"
[[ -r /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor ]] && \
gov=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor 2>/dev/null)
dialog --title "CPU Information" --msgbox \
"CPU Model : $cpu_model
Cores : $cores
Freq Range : $freq_range
Max Boost : $max_freq
Governor : $gov" 12 70 2>/dev/null
}
show_available_governors() {
local govs
govs=$(get_available_governors)
dialog --title "Available Governors" --msgbox \
"Available governors:\n\n${govs// /\\n}" 15 50 2>/dev/null
}
show_hardware_limits() {
local output=""
if command -v cpufreq-info &>/dev/null; then
output=$(cpufreq-info 2>/dev/null | grep "hardware limits" || echo "Not available via cpufreq-info")
fi
if [[ -z "$output" ]]; then
local min max
min=$(awk '{printf "%.1fGHz", $1/1000000}' \
/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq 2>/dev/null || echo "?")
max=$(awk '{printf "%.1fGHz", $1/1000000}' \
/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq 2>/dev/null || echo "?")
output="Hardware limits: ${min} - ${max}"
fi
dialog --title "Hardware Limits" --msgbox "$output" 20 65 2>/dev/null
}
# ─── Turbo Boost ──────────────────────────────────────────────────────────────
toggle_turbo() {
local turbo_path="" mode=""
if [[ -f /sys/devices/system/cpu/intel_pstate/no_turbo ]]; then
turbo_path="/sys/devices/system/cpu/intel_pstate/no_turbo"
mode="intel"
elif [[ -f /sys/devices/system/cpu/cpufreq/boost ]]; then
turbo_path="/sys/devices/system/cpu/cpufreq/boost"
mode="amd"
else
error_msg "Turbo Boost control not found.\n(Intel: intel_pstate/no_turbo AMD: cpufreq/boost)"
return 1
fi
local current action new_val msg
current=$(cat "$turbo_path" 2>/dev/null || echo "0")
if [[ "$mode" == "intel" ]]; then
if [[ "$current" == "0" ]]; then
action="disable"; new_val="1"; msg="Turbo Boost will be DISABLED"
else
action="enable"; new_val="0"; msg="Turbo Boost will be ENABLED"
fi
else
if [[ "$current" == "1" ]]; then
action="disable"; new_val="0"; msg="Turbo Boost will be DISABLED"
else
action="enable"; new_val="1"; msg="Turbo Boost will be ENABLED"
fi
fi
dialog --yesno "$msg. Continue?" 8 50 2>/dev/null || return 0
if echo "$new_val" | sudo tee "$turbo_path" > /dev/null 2>&1; then
dialog --msgbox "Turbo Boost ${action}d successfully." 8 45 2>/dev/null
log "Turbo Boost ${action}d (path: $turbo_path)"
else
error_msg "Failed to $action Turbo Boost. Are you root / is sudo available?"
fi
}
# ─── Stress test ──────────────────────────────────────────────────────────────
run_stress_test() {
if ! command -v stress &>/dev/null; then
error_msg "stress not installed. Install it with: sudo apt install stress"
return 1
fi
local duration=30
local cores
cores=$(get_cpu_count)
dialog --yesno "Run stress test on all $cores cores for ${duration}s?" 8 55 2>/dev/null || return 0
log "Stress test: $cores cores × ${duration}s"
(
stress --cpu "$cores" --timeout "${duration}s" &>/dev/null &
local spid=$!
for (( i=0; i<=duration; i++ )); do
echo $(( i * 100 / duration ))
sleep 1
done
wait "$spid" 2>/dev/null || true
) | dialog --gauge "Stress testing $cores core(s) for ${duration}s …" 8 55 0 2>/dev/null
dialog --msgbox "Stress test complete!\n\nUse 'Show current frequencies' to check results." 8 55 2>/dev/null
log "Stress test complete"
}
# ─── Custom governor picker ───────────────────────────────────────────────────
pick_custom_governor() {
local govs avail=()
govs=$(get_available_governors)
read -ra avail <<< "$govs"
local menu_args=()
local i=1
for g in "${avail[@]}"; do
menu_args+=( "$i" "$g" )
(( i++ )) || true
done
local sel
sel=$(dialog --stdout --menu "Select governor:" 15 50 8 "${menu_args[@]}" 2>/dev/null) || return 0
local chosen="${avail[$((sel-1))]}"
set_governor "$chosen"
}
# ─── Bootstrap ────────────────────────────────────────────────────────────────
echo "Checking dependencies…"
# Check dependencies without hanging
check_dep "cpufrequtils" "cpufreq-set" || echo "cpufrequtils not installed - frequency features limited"
check_dep "dialog" "dialog" || {
echo "dialog is required. Installing..."
sudo apt-get update -qq 2>/dev/null
sudo apt-get install -y dialog 2>/dev/null || { echo "Failed to install dialog. Exiting."; exit 1; }
}
check_dep "stress" "stress" || echo "stress not installed - stress test feature limited"
# Ensure log file exists
touch "$LOGFILE" 2>/dev/null || { echo "Cannot create log file at $LOGFILE"; exit 1; }
log "=== CPU Control Menu Started ==="
MAX_FREQ=$(get_max_cpu_frequency 2>/dev/null || echo "Unknown")
CPU_MODEL=$(get_cpu_model)
CORES=$(get_cpu_count)
# ─── Main loop ────────────────────────────────────────────────────────────────
while true; do
CHOICE=$(dialog --clear --stdout \
--title "CPU Control Menu — $CPU_MODEL" \
--menu "Cores: $CORES | Max Boost: ${MAX_FREQ:-Unknown} | Choose action:" \
27 82 16 \
0 "CPU Information Summary" \
1 "Set ALL cores to MAX frequency (${MAX_FREQ:-auto-detect})" \
2 "Set custom fixed frequency" \
3 "Set governor: performance" \
4 "Set governor: powersave" \
5 "Set governor: other (pick from available)" \
6 "Set minimum frequency limit" \
7 "Set maximum frequency limit" \
8 "Show current frequencies (per core)" \
9 "Show active governor (per core)" \
10 "Show available governors" \
11 "Show hardware limits" \
12 "Toggle Turbo Boost (Intel / AMD)" \
13 "Run stress test (${CORES} cores, 30 s)" \
14 "View log file" \
15 "Exit" 2>/dev/null) || break
case "$CHOICE" in
0) show_cpu_info ;;
1) set_max_frequency ;;
2)
FREQ=$(dialog --stdout --inputbox \
"Enter frequency (e.g. 3.6GHz or 2400MHz):" \
8 50 "${MAX_FREQ:-3.6GHz}" 2>/dev/null) || continue
[[ -n "$FREQ" ]] && set_fixed_frequency_all "$FREQ"
;;
3) set_governor "performance" ;;
4) set_governor "powersave" ;;
5) pick_custom_governor ;;
6) set_frequency_limit "-d" "Enter minimum frequency:" "0.8GHz" ;;
7) set_frequency_limit "-u" "Enter maximum frequency:" "${MAX_FREQ:-3.6GHz}" ;;
8) show_frequencies ;;
9) show_governors ;;
10) show_available_governors ;;
11) show_hardware_limits ;;
12) toggle_turbo ;;
13) run_stress_test ;;
14)
if [[ -s "$LOGFILE" ]]; then
dialog --title "Log: $LOGFILE" --textbox "$LOGFILE" 30 82 2>/dev/null
else
dialog --msgbox "Log file is empty or not found." 8 45 2>/dev/null
fi
;;
15) break ;;
esac
done
log "=== CPU Control Menu Exited ==="
clear
echo -e "${GREEN}CPU Control Menu exited cleanly.${NC}"