-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpu-tools
89 lines (80 loc) · 3.84 KB
/
cpu-tools
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
# Copyright (C) 2025 sppidy
#
# This file is a part of <https://github.com/sppidy/scripts>
# Please read the GNU General Public License in
# <https://www.github.com/sppidy/scripts/blob/main/LICENSE/>.
#!/bin/bash
total_cores=$1
if [[ -z "$1" || ! "$1" =~ ^[0-9]+$ ]]; then
echo "Usage: $0 [cpu-count] [-h | --help] [-p|--performance min_freq max_freq] [-b|--powersave min_freq max_freq] [--restore-governor]"
exit 1
fi
cpu_vendor=$(grep -m1 "vendor_id" /proc/cpuinfo | awk '{print $3}')
scaling_driver=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_driver 2>/dev/null)
shift
while [[ $# -gt 0 ]]; do
case "$1" in
-p|--performance)
if [[ -z "$2" || -z "$3" || ! "$2" =~ ^[0-9]+$ || ! "$3" =~ ^[0-9]+$ ]]; then
echo "Error: Missing or invalid min_freq and max_freq for performance mode."
exit 1
fi
min_freq_khz=$(( $2 * 1000 ))
max_freq_khz=$(( $3 * 1000 ))
for i in $(seq 0 $((total_cores - 1))); do
echo "Setting CPU: $i to performance mode ($2 MHz - $3 MHz)"
sudo sh -c "echo 'performance' > /sys/devices/system/cpu/cpu$i/cpufreq/scaling_governor"
[[ -f "/sys/devices/system/cpu/cpu$i/cpufreq/energy_performance_preference" ]] && \
sudo sh -c "echo 'performance' > /sys/devices/system/cpu/cpu$i/cpufreq/energy_performance_preference"
sudo cpupower -c $i frequency-set -d "$min_freq_khz" -u "$max_freq_khz" -g performance
done
shift 3
exit 0
;;
-b|--powersave)
min_freq=${2:-400}
max_freq=${3:-1800}
if ! [[ "$min_freq" =~ ^[0-9]+$ && "$max_freq" =~ ^[0-9]+$ ]]; then
echo "Error: Invalid frequency values."
exit 1
fi
min_freq_khz=$(( min_freq * 1000 ))
max_freq_khz=$(( max_freq * 1000 ))
for i in $(seq 0 $((total_cores - 1))); do
echo "Setting CPU: $i to powersave mode ($min_freq MHz - $max_freq MHz)"
sudo sh -c "echo 'powersave' > /sys/devices/system/cpu/cpu$i/cpufreq/scaling_governor"
[[ -f "/sys/devices/system/cpu/cpu$i/cpufreq/energy_performance_preference" ]] && \
sudo sh -c "echo 'power' > /sys/devices/system/cpu/cpu$i/cpufreq/energy_performance_preference"
sudo cpupower -c $i frequency-set -d "$min_freq_khz" -u "$max_freq_khz" -g powersave
done
shift 3
exit 0
;;
--restore-governor|-rg)
echo "Restoring default governor..."
for i in $(seq 0 $((total_cores - 1))); do
if [[ "$cpu_vendor" == "AuthenticAMD" ]]; then
if [[ "$scaling_driver" == "amd-pstate-epp" ]]; then
sudo sh -c "echo 'balance_performance' > /sys/devices/system/cpu/cpu$i/cpufreq/energy_performance_preference"
fi
elif [[ "$cpu_vendor" == "GenuineIntel" ]]; then
if [[ "$scaling_driver" == "intel_pstate" ]]; then
sudo sh -c "echo 'balance_performance' > /sys/devices/system/cpu/cpu$i/cpufreq/energy_performance_preference"
fi
fi
done
exit 0
;;
-h|--help)
echo "Usage: $0 [cpu-count] [-h | --help] [-p|--performance min_freq max_freq] [-b|--powersave min_freq max_freq] [--restore-governor]"
echo "By default, powersave mode sets the CPU frequency to 400MHz - 1800MHz."
echo "In performance mode, specifying min_freq and max_freq is required."
echo "Use --restore-governor to revert to default settings."
exit 0
;;
*)
echo "Invalid option: $1"
exit 1
;;
esac
done