#!/system/bin/sh                             
#
# Script to set the CPU governors and I/O schedulers if available
#
# Not yet used in Cor Optimi v1.00 but one never knows...
# Fully functional now indeed with Cor Plenus v2
#
# Author:
#       Michelasso @ androidiani.com
#       Miche1asso @ xda
#
# Version 1.03

#
# Select Governor and I/O Schedulers HERE.   
# This scripts can set "lagfree", "ondemand" and "conservative" governor's parameters
#
GOVERNOR=lagfree    
SCHEDULER=sio
#
log -p i -t CPUgovernor "CPUgovernor started"
#
# Check the current governor
log -p i -t CPUgovernor "Governor is: $(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor 2>&1)"
#
# Wait n seconds to give it time to stick due to a bug on stock derivativess
# Also try first setting a different governor. It seems it may move thing
# (at least it does after boot time)
#
#echo powersave > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
sleep 0
#
# And now the real stuff.
log -p i -t CPUgovernor "Setting governor $GOVERNOR"
echo $GOVERNOR > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
#
# Set max and minimum frequency. Max above 1GHz in case we manage to reach OC
# It will be cut down automatically anyhow
#
echo 156000 > /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq
echo 1014000 > /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq
#
# Settings for battery saving
#
echo 0 > /sys/devices/system/cpu/cpufreq/$GOVERNOR/ignore_nice_load
echo 90 > /sys/devices/system/cpu/cpufreq/$GOVERNOR/up_threshold  
echo 80000 > /sys/devices/system/cpu/cpufreq/$GOVERNOR/sampling_rate  

# Specific governor settings
#
if [ "$GOVERNOR" = "ondemand" ]; then
	echo 120 > /sys/devices/system/cpu/cpufreq/$GOVERNOR/powersave_bias  
	echo 1 > /sys/devices/system/cpu/cpufreq/$GOVERNOR/io_is_busy
elif [ "$GOVERNOR" = "conservative" ]; then
	echo 4  > /sys/devices/system/cpu/cpufreq/$GOVERNOR/sampling_down_factor
	echo 5  > /sys/devices/system/cpu/cpufreq/$GOVERNOR/freq_step
	echo 55 > /sys/devices/system/cpu/cpufreq/$GOVERNOR/down_threshold  
elif [ "$GOVERNOR" = "lagfree" ]; then
	echo 4  > /sys/devices/system/cpu/cpu0/cpufreq/$GOVERNOR/sampling_down_factor 
	echo 55 > /sys/devices/system/cpu/cpu0/cpufreq/$GOVERNOR/down_threshold
	echo 0 > /sys/devices/system/cpu/cpu0/cpufreq/$GOVERNOR/ignore_nice_load
	echo 90 > /sys/devices/system/cpu/cpu0/cpufreq/$GOVERNOR/up_threshold  
	echo 80000 > /sys/devices/system/cpu/cpu0/cpufreq/$GOVERNOR/sampling_rate  
fi

log -p i -t CPUgovernor "Setting $SCHEDULER I/O scheduler"
#
for i in $(find /sys/block/*/queue -name scheduler -print); do echo $SCHEDULER > $i; done
#
log -p i -t CPUgovernor "CPUgovernor executed!"
#
# Done.
