Skip to content

Instantly share code, notes, and snippets.

@fernandofig
Last active August 29, 2015 14:25
Show Gist options
  • Save fernandofig/3d33badfb90bf4a87cfe to your computer and use it in GitHub Desktop.
Save fernandofig/3d33badfb90bf4a87cfe to your computer and use it in GitHub Desktop.
See README for description
For Android devices: Script for setting CPU Scaling parameters (governor, min_freq, max_freq)
for all CPU cores.
Doesn't work for all devices - e.g. for my SGS4 the max_freq for additional cores (core != 0)
gets reset to the nominal CPU maximum frequency (cpuinfo_max_freq) when the cpu reawakens from
sleep state. So I'm leaving this here only for educational purposes and future reference for
myself.
This was tested on CyanogenMod 12.1. It should in theory work (as in the script should execute
without errors) on any rooted device with busybox installed.
The script also has the CPU cores to set the parameters on hardcoded (on the SGS4, 4 cores). Set
the cpusToApply array accordingly.
#!/system/bin/sh
if [[ $# -ne 3 ]]; then
echo "Arguments missing! Usage: $0 <governor> <min_freq> <max_freq>"
exit 1;
fi
cpusToApply=(0 1 2 3)
isCpuOnApplyQueue() {
for c in "${cpusToApply[@]}"; do [[ $c -eq $1 ]] && echo 1 && return; done
echo 0
}
popCpuFromApplyQueue() {
local arrCount
local modifiedArray
arrCount=$((${#cpusToApply[@]} - 1))
modifiedArray=()
for i in `seq 0 ${arrCount}`; do [[ ${cpusToApply[$i]} -ne $1 ]] && modifiedArray+=(${cpusToApply[$i]}); done
echo ${modifiedArray[@]}
}
while [ ${#cpusToApply[@]} -gt 0 ]; do
for p in "${cpusToApply[@]}"; do
echo "1" > /sys/devices/system/cpu/cpu$p/online
echo $1 2> /dev/null > /sys/devices/system/cpu/cpu$p/cpufreq/scaling_governor
echo $2 2> /dev/null > /sys/devices/system/cpu/cpu$p/cpufreq/scaling_min_freq
echo $3 2> /dev/null > /sys/devices/system/cpu/cpu$p/cpufreq/scaling_max_freq
govState=$(cat /sys/devices/system/cpu/cpu$p/cpufreq/scaling_governor 2> /dev/null)
minFreq=$(cat /sys/devices/system/cpu/cpu$p/cpufreq/scaling_min_freq 2> /dev/null)
maxFreq=$(cat /sys/devices/system/cpu/cpu$p/cpufreq/scaling_max_freq 2> /dev/null)
if [[ "$govState" = "$1" && "$minFreq" = "$2" && "$maxFreq" = "$3" ]]; then
cpusToApply=(`popCpuFromApplyQueue ${p}`)
echo "Scaling set done on CPU $p"
fi
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment