-
Notifications
You must be signed in to change notification settings - Fork 0
/
startup.sh
executable file
·171 lines (148 loc) · 4.15 KB
/
startup.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
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
#!/bin/env bash
# Start argument handling
function show_help() {
echo "${0} -m \"AC\" -> AC Plugged, boost CPU accordingly"
echo "${0} -m \"BAT\" -> Using battery, boost CPU accordingly"
echo "${0} -j 2 -> Set two cores enabled when using battery"
echo "${0} -c 1000 -> Set max clock to 1000 Mhz when using battery"
}
function invalid_argument() {
echo "Error, invalid argument ${1}" &> /dev/stderr
exit 1;
}
# POSIX variable, reset for getopt usage
OPTIND=1
# Initialize our own variables:
AC_PLUGGED=false
AC_DISABLE_CORES=false
AC_SET_FREQ=false
num_cores=2
freq=1000
min_freq=false
disable_turbo=1
governor=false
debug=false
# TODO: Parametrize
disable_bluetooth=true
while getopts "h?m:j:c:d:g:tv" opt; do
case "$opt" in
h|\?)
show_help
exit 0
;;
m)
if [ ${OPTARG} == "AC" ]; then
AC_PLUGGED=true
elif [ ${OPTARG} != "BAT" ]; then
if [ ${OPTARG} != "AUTO" ];
then
invalid_argument "\"${0} ${opt} ${OPTARG}\""
fi
fi
;;
j)
num_cores=${OPTARG}
if [ ${AC_PLUGGED} == true ]; then
AC_DISABLE_CORES=true
fi
;;
c)
freq=${OPTARG}
if [ ${AC_PLUGGED} == true ]; then
AC_SET_FREQ=true
fi
;;
*)
# Ideally will not run as getopt handles it
invalid_argument "${opt}"
;;
esac
done
# Start Power save logic
modprobe cpufreq_powersave
modprobe cpufreq_userspace
################################################
# Controls Intel Turbo Boost #
disable_turbo=1
if [ ${AC_PLUGGED} == true ];
then
if [ `cat /sys/devices/system/cpu/intel_pstate/no_turbo` == '1' ];
then
echo 0 > /sys/devices/system/cpu/intel_pstate/no_turbo;
fi
disable_turbo=0
fi
# echo ${disable_turbo} > /sys/devices/system/cpu/intel_pstate/no_turbo
################################################
# Set powersave to all CPUs # Set max clock to 1 Ghz for all
# cpus #
nprocessors_conf=`getconf _NPROCESSORS_CONF`
nprocessors_online=`getconf _NPROCESSORS_ONLN`
cpu_end=$((${nprocessors_conf} - 1))
max_cpu_freq=`cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq`
min_cpu_freq=`cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq`
# Test if CPU can run at specified frequency
if [ $((${freq} * 1000)) -gt ${max_cpu_freq} ]; then
echo "Cannot set frequency: CPU cannot handle ${freq}Mhz, setting to ${max_cpu_freq}Khz"
freq="${max_cpu_freq}Khz"
else
freq="${freq}Mhz"
fi
# Test if number of cores specified is acceptable
if [ ${num_cores} -gt ${nprocessors_conf} ] ||
[ ${num_cores} -lt 1 ]
then
echo "Invalid number of cores specified, try to add \"-j 1\" if you're on a single core machine, or set a lower number in a multi core setup"
exit 1;
fi
for cpu in `seq 0 ${cpu_end}`
do
# Check if all CPUs are enabled
if [ nprocessors_conf != nprocessors_online ]; then
# Needs CPU enabled to set governor
bash -c "echo 1 > /sys/devices/system/cpu/cpu${cpu}/online" &>/dev/null
fi
if [ $AC_PLUGGED == true ]; then
ac_max=""
if [ ${AC_SET_FREQ} == true ]; then
ac_max="${freq}"
else
ac_max="${max_cpu_freq}Khz"
fi
cpufreq-set -r --cpu ${cpu} --governor performance -d ${min_cpu_freq}Khz -u ${ac_max}
else
cpufreq-set -r --cpu ${cpu} --governor powersave -d ${min_cpu_freq} -u ${freq}
fi
# echo 1000000 > /sys/devices/system/cpu/cpu${i}/cpufreq/scaling_max_freq
# echo 1000000 > /sys/devices/system/cpu/cpu${i}/cpufreq/cpuinfo_max_freq
done
################################################
# Disable 6 CPUs on battery #
if [ ${AC_PLUGGED} == false ] ||
[ ${AC_DISABLE_CORES} == true ]
then
disable_cpu=0
cpu_start=${num_cores}
for i in `seq ${cpu_start} ${cpu_end}`
do
# Skip the first CPU, it's always online
if [ $i == 0 ]; then continue; fi
echo ${disable_cpu} > /sys/devices/system/cpu/cpu${i}/online
done
elif [ ${AC_PLUGGED} == true ]
then
enable_cpu=1
cpu_start=${num_cores}
for i in `seq ${cpu_start} ${cpu_end}`
do
# Skip the first CPU, it's always online
if [ $i == 0 ]; then continue; fi
echo ${enable_cpu} > /sys/devices/system/cpu/cpu${i}/online
done
fi
################################################
# Disable bluetooth #
if [ ${disable_bluetooth} == true ]
then
systemctl stop bluetooth
fi