You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ktweak/system/bin/ktweak

211 lines
4.7 KiB
Bash

#!/system/bin/sh
# Written by Draco (tytydraco @ GitHub)
ERR_CNT=0
# Log in red and exit
err() {
ERR_CNT=$(($ERR_CNT + 1))
[[ "$DEBUG" -eq 1 ]] && echo -e "\e[91m[!] $@\e[39m"
}
# Log in white and continue (unnecessary)
dbg() {
[[ "$DEBUG" -eq 1 ]] && echo -e "[*] $@"
}
# Safely apply sysctl adjustment
ctl() {
# Fetch the current key value
local curval=`sysctl -e -n "$1"`
# Bail out if sysctl key does not exist
if [[ -z "$curval" ]]
then
err "Key $1 does not exist. Skipping."
return 1
fi
# Bail out if sysctl is already set
if [[ "$curval" == "$2" ]]
then
dbg "Key $1 is already set to $2. Skipping."
return 0
fi
# Set the new value
sysctl -w "$1"="$2" &> /dev/null
# Bail out if write fails
if [[ $? -ne 0 ]]
then
err "Failed to write $2 to $1. Skipping."
return 1
fi
}
# Safely write value to file
write() {
# Bail out if file does not exist
if [[ ! -f "$1" ]]
then
err "File $1 does not exist. Skipping."
return 1
fi
# Fetch the current key value
local curval=`cat "$1" 2> /dev/null`
# Bail out if value is already set
if [[ "$curval" == "$2" ]]
then
dbg "File $1 is already set to $2. Skipping."
return 0
fi
# Write the new value
echo "$2" > "$1"
# Bail out if write fails
if [[ $? -ne 0 ]]
then
err "Failed to write $2 to $1. Skipping."
return 1
fi
}
usage() {
echo -n "Usage: `basename $0` [OPTIONS] [NAME]
Options:
-d Show debug logs
-h Show usage
"
}
while getopts ":dh" opt; do
case $opt in
d)
DEBUG=1
;;
h)
usage
exit 0
;;
*)
usage
exit 1
;;
esac
done
shift $((OPTIND-1))
# Print device information prior to execution
dbg "----- Device Information -----"
# Kernel and device information
dbg `uname -a`
# Scheduler feature check
[[ -f "/sys/kernel/debug/sched_features" ]] && dbg "Scheduler features exposed."
# CPU boost check
[[ -d "/sys/module/cpu_boost" ]] && dbg "CAF CPU boost detected."
# ZRAM support state
[[ -d "/sys/block/zram0" ]] && dbg "ZRAM supported."
dbg "------------------------------"
# Kernel
ctl kernel.perf_cpu_time_max_percent 5
ctl kernel.sched_autogroup_enabled 1
ctl kernel.sched_enable_thread_grouping 1
ctl kernel.sched_child_runs_first 1
ctl kernel.sched_downmigrate "50 50"
ctl kernel.sched_upmigrate "80 80"
ctl kernel.sched_group_downmigrate 50
ctl kernel.sched_group_upmigrate 80
ctl kernel.sched_tunable_scaling 0
ctl kernel.sched_latency_ns 10000000
ctl kernel.sched_min_granularity_ns 1000000
ctl kernel.sched_migration_cost_ns 1000000
ctl kernel.sched_min_task_util_for_boost 25
ctl kernel.sched_min_task_util_for_colocation 50
ctl kernel.sched_nr_migrate 64
ctl kernel.sched_schedstats 0
ctl kernel.sched_wakeup_granularity_ns 5000000
ctl kernel.timer_migration 0
# Net
ctl net.ipv4.tcp_ecn 1
ctl net.ipv4.tcp_fastopen 3
ctl net.ipv4.tcp_syncookies 0
# VM
ctl vm.compact_unevictable_allowed 0
ctl vm.dirty_background_ratio 10
ctl vm.dirty_ratio 30
ctl vm.dirty_expire_centisecs 1000
ctl vm.dirty_writeback_centisecs 0
ctl vm.extfrag_threshold 750
ctl vm.oom_dump_tasks 0
ctl vm.page-cluster 0
ctl vm.reap_mem_on_sigkill 1
ctl vm.stat_interval 10
ctl vm.swappiness 100
ctl vm.vfs_cache_pressure 200
# Scheduler features
if [[ -f "/sys/kernel/debug/sched_features" ]]
then
write /sys/kernel/debug/sched_features NEXT_BUDDY
write /sys/kernel/debug/sched_features NO_STRICT_SKIP_BUDDY
write /sys/kernel/debug/sched_features NO_NONTASK_CAPACITY
write /sys/kernel/debug/sched_features TTWU_QUEUE
fi
# CPU
for cpu in /sys/devices/system/cpu/cpu*/cpufreq/
do
write "${cpu}scaling_min_freq" `cat "${cpu}cpuinfo_min_freq"`
write "${cpu}scaling_max_freq" `cat "${cpu}cpuinfo_max_freq"`
avail_govs=`cat "${cpu}scaling_available_governors"`
[[ "$avail_govs" == *"interactive"* ]] && write "${cpu}scaling_governor" interactive
[[ "$avail_govs" == *"schedutil"* ]] && write "${cpu}scaling_governor" schedutil
# Interactive-specific tweaks
if [[ -d "${cpu}interactive" ]]
then
write "${cpu}interactive/min_sample_time" 10000
write "${cpu}interactive/go_hispeed_load" 90
write "${cpu}interactive/hispeed_freq" `cat "${cpu}cpuinfo_max_freq"`
fi
# Schedutil-specific tweaks
if [[ -d "${cpu}schedutil" ]]
then
write "${cpu}schedutil/up_rate_limit_us" 10000
write "${cpu}schedutil/down_rate_limit_us" 10000
write "${cpu}schedutil/hispeed_load" 90
write "${cpu}schedutil/hispeed_freq" `cat "${cpu}cpuinfo_max_freq"`
fi
done
# CAF CPU boost
if [[ -d "/sys/module/cpu_boost" ]]
then
write "/sys/module/cpu_boost/parameters/input_boost_freq" 0:1400000
write "/sys/module/cpu_boost/parameters/input_boost_ms" 250
fi
# I/O
for queue in /sys/block/*/queue/
do
write "${queue}iostats" 0
write "${queue}read_ahead_kb" 0
write "${queue}nr_requests" 512
write "${queue}scheduler" noop
write "${queue}scheduler" none
done
dbg "Finished with $ERR_CNT failed writes."