2020-08-02 10:41:13 +00:00
|
|
|
#!/system/bin/sh
|
|
|
|
# Written by Draco (tytydraco @ GitHub)
|
|
|
|
|
|
|
|
# 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
|
|
|
|
echo "[!] Key $1 does not exist. Skipping."
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Bail out if sysctl is already set
|
|
|
|
if [[ "$curval" == "$2" ]]
|
|
|
|
then
|
|
|
|
echo "[*] 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
|
|
|
|
echo "[!] Failed to write $2 to $1. Skipping."
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Print new state
|
|
|
|
echo "[*] $1: $curval --> $2"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Safely write value to file
|
|
|
|
write() {
|
|
|
|
# Bail out if file does not exist
|
2020-08-02 10:51:26 +00:00
|
|
|
if [[ ! -f "$1" ]]
|
2020-08-02 10:41:13 +00:00
|
|
|
then
|
|
|
|
echo "[!] File $1 does not exist. Skipping."
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Fetch the current key value
|
2020-08-02 10:51:26 +00:00
|
|
|
local curval=`cat "$1"`
|
2020-08-02 10:41:13 +00:00
|
|
|
|
|
|
|
# Bail out if value is already set
|
2020-08-02 10:51:26 +00:00
|
|
|
if [[ "$curval" == "$2" ]]
|
2020-08-02 10:41:13 +00:00
|
|
|
then
|
2020-08-02 10:51:26 +00:00
|
|
|
echo "[*] File $1 is already set to $2. Skipping."
|
2020-08-02 10:41:13 +00:00
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Write the new value
|
2020-08-02 10:51:26 +00:00
|
|
|
echo "$2" > "$1"
|
2020-08-02 10:41:13 +00:00
|
|
|
|
|
|
|
# Bail out if write fails
|
|
|
|
if [[ $? -ne 0 ]]
|
|
|
|
then
|
2020-08-02 10:51:26 +00:00
|
|
|
echo "[!] Failed to write $2 to $1. Skipping."
|
2020-08-02 10:41:13 +00:00
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Print new state
|
2020-08-02 10:51:26 +00:00
|
|
|
echo "[*] $1: $curval --> $2"
|
2020-08-02 10:41:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Wait 60s into boot before applying changes
|
|
|
|
echo "[*] Waiting for 60s of uptime."
|
|
|
|
while [[ `cat /proc/uptime | awk '{print $1}' | awk -F. '{print $1}'` -lt 60 ]]
|
|
|
|
do
|
|
|
|
sleep 1
|
|
|
|
done
|
|
|
|
echo "[*] Done waiting."
|
|
|
|
|
|
|
|
# Kernel
|
|
|
|
ctl kernel.perf_cpu_time_max_percent 5
|
2020-08-02 10:51:26 +00:00
|
|
|
write /proc/sys/kernel/printk_devkmsg off
|
2020-08-02 10:41:13 +00:00
|
|
|
ctl kernel.randomize_va_space 0
|
|
|
|
ctl kernel.sched_autogroup_enabled 1
|
|
|
|
ctl kernel.sched_child_runs_first 1
|
|
|
|
ctl kernel.sched_downmigrate "40 40"
|
|
|
|
ctl kernel.sched_upmigrate "60 60"
|
|
|
|
ctl kernel.sched_group_downmigrate 40
|
|
|
|
ctl kernel.sched_group_upmigrate 60
|
|
|
|
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 40
|
|
|
|
ctl kernel.sched_min_task_util_for_colocation 20
|
|
|
|
ctl kernel.sched_nr_migrate 64
|
|
|
|
ctl kernel.sched_rt_runtime_us 1000000
|
|
|
|
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_slow_start_after_idle 0
|
|
|
|
ctl net.ipv4.tcp_syncookies 0
|
|
|
|
ctl net.ipv4.tcp_timestamps 0
|
|
|
|
|
|
|
|
# VM
|
2020-08-02 20:46:45 +00:00
|
|
|
ctl vm.dirty_background_ratio 3
|
|
|
|
ctl vm.dirty_ratio 30
|
2020-08-02 10:41:13 +00:00
|
|
|
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.overcommit_memory 1
|
2020-08-02 20:51:33 +00:00
|
|
|
ctl vm.page-cluster 0
|
2020-08-02 10:41:13 +00:00
|
|
|
ctl vm.reap_mem_on_sigkill 1
|
|
|
|
ctl vm.stat_interval 10
|
|
|
|
ctl vm.swappiness 50
|
|
|
|
ctl vm.vfs_cache_pressure 200
|
|
|
|
ctl vm.watermark_scale_factor 100
|
|
|
|
|
|
|
|
# Scheduler features
|
|
|
|
if [[ -f "/sys/kernel/debug/sched_features" ]]
|
|
|
|
then
|
2020-08-02 10:51:26 +00:00
|
|
|
write /sys/kernel/debug/sched_features NO_GENTLE_FAIR_SLEEPER
|
|
|
|
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
|
2020-08-02 10:41:13 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# CPU
|
|
|
|
for cpu in /sys/devices/system/cpu/cpu*/cpufreq/
|
|
|
|
do
|
2020-08-02 10:51:26 +00:00
|
|
|
write "${cpu}scaling_min_freq" `cat "${cpu}cpuinfo_min_freq"`
|
|
|
|
write "${cpu}scaling_max_freq" `cat "${cpu}cpuinfo_max_freq"`
|
2020-08-02 10:41:13 +00:00
|
|
|
|
|
|
|
avail_govs=`cat "${cpu}scaling_available_governors"`
|
2020-08-02 10:51:26 +00:00
|
|
|
[[ "$avail_govs" == *"interactive"* ]] && write "${cpu}scaling_governor" interactive
|
|
|
|
[[ "$avail_govs" == *"schedutil"* ]] && write "${cpu}scaling_governor" schedutil
|
2020-08-02 10:41:13 +00:00
|
|
|
|
|
|
|
# Interactive-specific tweaks
|
|
|
|
if [[ -d "${cpu}interactive" ]]
|
|
|
|
then
|
2020-08-02 10:51:26 +00:00
|
|
|
write "${cpu}interactive/go_hispeed_load" 80
|
|
|
|
write "${cpu}interactive/hispeed_freq" `cat "${cpu}cpuinfo_max_freq"`
|
2020-08-02 10:41:13 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Schedutil-specific tweaks
|
|
|
|
if [[ -d "${cpu}schedutil" ]]
|
|
|
|
then
|
2020-08-02 10:51:26 +00:00
|
|
|
write "${cpu}schedutil/up_rate_limit_us" 0
|
|
|
|
write "${cpu}schedutil/down_rate_limit_us" 0
|
|
|
|
write "${cpu}schedutil/hispeed_load" 80
|
|
|
|
write "${cpu}schedutil/hispeed_freq" `cat "${cpu}cpuinfo_max_freq"`
|
2020-08-02 10:41:13 +00:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
# CAF CPU boost
|
|
|
|
if [[ -d "/sys/module/cpu_boost" ]]
|
|
|
|
then
|
2020-08-02 10:51:26 +00:00
|
|
|
write "/sys/module/cpu_boost/parameters/input_boost_freq" 1200000
|
|
|
|
write "/sys/module/cpu_boost/parameters/input_boost_ms" 250
|
2020-08-02 10:41:13 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Stune
|
|
|
|
if [[ -d "/dev/stune" ]]
|
|
|
|
then
|
2020-08-02 10:51:26 +00:00
|
|
|
write /dev/stune/top-app/schedtune.prefer_idle 1
|
2020-08-02 10:41:13 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# I/O
|
|
|
|
for queue in /sys/block/*/queue/
|
|
|
|
do
|
2020-08-02 10:51:26 +00:00
|
|
|
write "${queue}iostats" 0
|
|
|
|
write "${queue}read_ahead_kb" 0
|
|
|
|
write "${queue}nr_requests" 512
|
2020-08-02 12:53:53 +00:00
|
|
|
write "${queue}scheduler" noop
|
|
|
|
write "${queue}scheduler" none
|
2020-08-02 10:41:13 +00:00
|
|
|
done
|
|
|
|
|
2020-08-02 11:19:10 +00:00
|
|
|
# ZRAM
|
|
|
|
if [[ -d "/sys/block/zram0" ]]
|
|
|
|
then
|
|
|
|
memsize=`cat /proc/meminfo | grep "MemTotal" | awk '{print $2}'`
|
2020-08-02 12:45:53 +00:00
|
|
|
halfmemsize=`echo "$(($memsize/2))"`
|
2020-08-02 11:19:10 +00:00
|
|
|
|
|
|
|
swapoff /dev/block/zram0
|
|
|
|
write /sys/block/zram0/reset 1
|
|
|
|
write /sys/block/zram0/disksize "${halfmemsize}KB"
|
|
|
|
mkswap /dev/block/zram0
|
|
|
|
swapon /dev/block/zram0
|
|
|
|
fi
|
|
|
|
|
2020-08-02 20:46:45 +00:00
|
|
|
echo "[*] Done."
|