Skip to content

Commit

Permalink
patches from working version
Browse files Browse the repository at this point in the history
  • Loading branch information
wh1t3h47 committed Jul 10, 2021
1 parent d37cca1 commit 30cdb48
Showing 1 changed file with 44 additions and 107 deletions.
151 changes: 44 additions & 107 deletions startup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,29 @@
# Start argument handling

function show_help() {
echo -e "Script to easily manage your CPU using cpufreq as a CPU power backend\n"
echo -e "${0} -m \"AC\" -> AC Plugged, boost CPU accordingly"
echo -e "${0} -m \"BAT\" -> Using battery, boost CPU accordingly"
echo -e "${0} -m \"AUTO\" -> Will automatically detect if using battery or AC power"
echo -e "${0} -j 2 -> Default behaviour: Set two cores enabled when using battery\n\t\tIf mode is AC, -j will specify the maximum number of cores to keep enabled at full power"
echo -e "${0} -c 1000 -> Set max clock to 1000 MHz when using battery\n\t\tIf mode is AC, -c will specify the maximum clock at full power\n"
echo -e "Examples:\n"
echo -e "1. The examples below are suitable when the AC adapter is connected\n"
echo -e "${0} -m AC # Set CPU fully powered"
echo -e "${0} -m AC -c 3200 # Set CPU fully powered, but ALWAYS under 3.2Ghz"
echo -e "${0} -m AC -j 4 # Set CPU fully powered, but will never use more than 4 cores\n"
echo -e "2. The examples below are suitable if you want to save battery\n"
echo -e "${0} -m BAT # Set the cpu to dual core at 1Ghz with pstate enabled (default) and powersave/ ondemand governor"
echo -e "${0} -m BAT -c 1800 -j 4 # Set the CPU to, at best, quad core at 1.8Ghz with pstate turbo disabled (intel power save) and powersave/ ondemand governor"
echo -e "${0} -m BAT -c 1800 -j 4 -t # Set the CPU to, at best, quad core at 1.8Ghz with pstate turbo enabled (not using intel power save) and powersave/ ondemand governor\n"
echo -e "3. The examples below are suitable when you are running this script periodically in an interval of time (chron or daemon) and want to automatically detect if AC is plugged (requires ACPI) \n"
echo -e -n "${0} -m AUTO # If you unplug your AC and run this program, it will put CPU on powersave, at 1Ghz with 2 cores enabled"
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

# Argument Variables
# 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

while getopts "h?m:j:c:d:tv" opt; do
while getopts "h?m:j:c:" opt; do
case "$opt" in
h|\?)
show_help
Expand All @@ -49,16 +34,11 @@ while getopts "h?m:j:c:d:tv" opt; do
m)
if [ ${OPTARG} == "AC" ]; then
AC_PLUGGED=true
elif [ ${OPTARG} == "AUTO" ]; then
supply_online=`cat /sys/class/power_supply/AC0/online`
# echo $supply_online
if [ $supply_online == '0' ]; then
AC_PLUGGED=false
elif [ $supply_online == '1' ]; then
AC_PLUGGED=true;
fi
elif [ ${OPTARG} != "BAT" ]; then
invalid_argument "\"${0} -${opt} ${OPTARG}\""
if [ ${OPTARG} != "AUTO" ];
then
invalid_argument "\"${0} ${opt} ${OPTARG}\""
fi
fi
;;
j)
Expand All @@ -73,130 +53,77 @@ while getopts "h?m:j:c:d:tv" opt; do
AC_SET_FREQ=true
fi
;;
t)
disable_turbo=0
;;
g)
governor=${OPTARG}
;;
v)
debug=true
;;
*)
# Ideally will not run as getopt handles it
invalid_argument "${0} -${opt}"
invalid_argument "${opt}"
;;
esac
done

# Start Power save logic

modprobe cpufreq_powersave
modprobe cpufreq_ondemand
modprobe cpufreq_userspace

################################################
# Controls Intel Turbo Boost #

# The above variable is an argument
# disable_turbo=1
if [ ${AC_PLUGGED} == true ]; then
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
# echo ${disable_turbo} > /sys/devices/system/cpu/intel_pstate/no_turbo

################################################
# Set powersave to all CPUs #
# Set max clock to n Ghz for all cpus #
# 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`
max_cpu_freq=`lscpu | grep -Po 'CPU max MHz:\s*\K[0-9]+' | sed "s/\.//g"`
min_cpu_freq=`lscpu | grep -Po 'CPU min MHz:\s*\K[0-9]+' | sed "s/\.//g"`
# echo "min cpu $min_cpu_freq"
specified_freq=${freq}

function invalid_frequency() {
echo "Cannot set frequency: CPU cannot handle ${1}MHz, setting to ${2}Khz"
}
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} * 1)) -gt ${max_cpu_freq} ] ||
[ $((${freq} * 1)) -lt ${min_cpu_freq} ]
then
invalid_frequency ${freq} ${max_cpu_freq}
freq=${max_cpu_freq}
else
freq="${freq}MHz"
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

# Test if specified_freq and min_freq are acceptable

if [ $((${specified_freq} * 1)) -gt ${max_cpu_freq} ] ||
[ $((${specified_freq} * 1)) -lt ${min_cpu_freq} ]
then
# echo 'freq test'
invalid_frequency ${specified_freq} ${max_cpu_freq}
freq=${max_cpu_freq}
fi

if [ ${min_freq} != false ]; then
# echo 'min freq test'
if [ $((${min_freq} * 1)) -lt ${min_cpu_freq} ] ||
[ $((${min_freq} * 1)) -gt ${max_cpu_freq} ]
then
invalid_frequency ${min_freq} ${min_cpu_freq}
min_freq=${min_cpu_freq}
else
min_freq=$((min_freq * 1))
fi
else
min_freq=${min_cpu_freq}
fi

for cpu in `seq 0 ${cpu_end}`
do
# Check if all CPUs are enabled
if [ ${nprocessors_conf} != ${nprocessors_online} ]; then
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
nprocessors_online=`getconf _NPROCESSORS_ONLN`
fi

if [ $AC_PLUGGED == true ]; then
ac_max=""
if [ ${AC_SET_FREQ} == true ]; then
ac_max="${freq}"
else
ac_max="${max_cpu_freq}"
ac_max="${max_cpu_freq}Khz"
fi
if [ $governor == false ]; then
governor='performance'
fi

if [ $debug == true ]; then echo "cpufreq-set -r --cpu ${cpu} --governor ${governor} -d ${min_freq}MHz -u ${ac_max}MHz"; fi
cpufreq-set -r --cpu ${cpu} --governor ${governor} -d ${min_freq}MHz -u ${ac_max}MHz
cpufreq-set -r --cpu ${cpu} --governor performance -d ${min_cpu_freq}Khz -u ${ac_max}
else
if [ $governor == false ]; then
governor='powersave'
fi

if [ $debug == true ]; then echo "cpufreq-set -r --cpu ${cpu} --governor $governor -d ${min_freq}MHz -u ${freq}"; fi
cpufreq-set -r --cpu ${cpu} --governor $governor -d ${min_freq}MHz -u ${freq}
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
Expand All @@ -216,5 +143,15 @@ then
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

0 comments on commit 30cdb48

Please sign in to comment.