-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcpu_scale.sh
89 lines (79 loc) · 4.16 KB
/
cpu_scale.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/bin/bash
# Tonton Jo - 2023
# Join me on Youtube: https://www.youtube.com/c/tontonjo
# Usage:
# put script where you want it to be executed
# chmod +x /path/to/cpu_scale.sh
# Edit crontab to run script at every reboot ->
# @reboot bash "/path/to/cpu_scale.sh" >/dev/null 2>&1"
# Reboot
# Test :)
# Version 1.0: Proof of concept
# Version 1.1: Loop is better than cron, will run everytime the script end
# Version 1.2: Use sar to get accurate average cpu load value
# Version 1.3: ensure the gouvernor is available
# --------------------- Settings ------------------------------------
averageloadupscaletime=3 # time to get average CPU load value in order to upscale
averageloaddownscaletime=10 # time to get average CPU load value in order to downscale
lowloadgouvernor=powersave # CPU Scheduler to use when low usage
upscalevalue=50 # At wich usage when LOW load gouvernor is set the CPU will upscale to high load
highloadgouvernor=schedutil # CPU Scheduler to use when low usage
downscalevalue=10 # At wich usage when HIGH load gouvernor is set the CPU will downscale to low load
# --------------------- Settings -------------------------------------
# ------------------- Env Variables ----------------------------------
execdir=$(dirname $0)
# ------------------- Env Variables ----------------------------------
if [ $(dpkg-query -W -f='${Status}' sysstat 2>/dev/null | grep -c "ok installed") -eq 0 ]; then
apt-get install -y sysstat;
fi
echo "- Starting Script" >> $execdir/cpu_scale.log
# Ensuring needed gouvernors are available
if cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors | grep -qi $lowloadgouvernor; then
if cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors | grep -qi $highloadgouvernor; then
echo "- Starting Script"
else
echo "- Missing CPU Gouvernor $highloadgouvernor - check in logs for the list of availables ones on your system"
echo "- Available gouvernors:"
cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors
sleep 5
exit
fi
else
echo "- Missing CPU Gouvernor $lowloadgouvernor - check in logs for the list of availables ones on your system"
echo "- Available gouvernors:"
cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors
sleep 5
exit
fi
while true; do
# --------------------- loop Variables ------------------------------------
actualgouvernor=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor)
date=$(date +%Y_%m_%d-%H_%M_%S)
# --------------------- loop Variables ------------------------------------
# In order to not rank up or down too fast, define the average value to use
if echo "$actualgouvernor" | grep -Eqi "$lowloadgouvernor"; then
cpuload=$(echo "$[100-$(sar -u 1 $averageloadupscaletime | awk '/^Average:/ { printf(" %.0f\n", $8)}')]")
else
cpuload=$(echo "$[100-$(sar -u 1 $averageloaddownscaletime | awk '/^Average:/ { printf(" %.0f\n", $8)}')]")
fi
# If the actual gouvernor is the low load gouvernor, check if CPU load is above upscale value
if echo "$actualgouvernor" | grep -Eqi "$lowloadgouvernor"; then
if (( $(echo "$cpuload > $upscalevalue"))); then
echo "- $date - Upscaling CPU power to $highloadgouvernor at $upscalevalue% CPU load" >> $execdir/cpu_scale.log
echo "- $date - Upscaling CPU power to $highloadgouvernor at $upscalevalue% CPU load"
echo "$highloadgouvernor" | tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
fi
# If the actual gouvernor is the high load gouvernor, check if value is under downscale value
elif echo "$actualgouvernor" | grep -Eqi "$highloadgouvernor"; then
if (( $(echo "$cpuload < $downscalevalue"))) ; then
echo "- $date - Downscaling CPU power to $lowloadgouvernor at $downscalevalue% CPU load" >> $execdir/cpu_scale.log
echo "- $date - Downscaling CPU power to $lowloadgouvernor at $downscalevalue% CPU load"
echo "$lowloadgouvernor" | tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
fi
# If none of the above, define the low load CPU Gouvernor
else
echo "- $date - auto set low load gouvernor" >> $execdir/cpu_scale.log
echo "$lowloadgouvernor" | tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
fi
echo "- CPU USAGE: $cpuload% - Gouvernor: $actualgouvernor"
done