Téléverser les fichiers vers "common"

This commit is contained in:
Olivier 2023-12-02 20:15:19 +01:00
parent 26e756ff62
commit d5d7c88815
6 changed files with 1297 additions and 0 deletions

309
common/functions.sh Normal file
View File

@ -0,0 +1,309 @@
##########################################################################################
#
# MMT Extended Utility Functions
#
##########################################################################################
require_new_ksu() {
ui_print "**********************************"
ui_print " Please install KernelSU v0.6.6+! "
ui_print "**********************************"
exit 1
}
umount_mirrors() {
[ -d $ORIGDIR ] || return 0
for i in $ORIGDIR/*; do
umount -l $i 2>/dev/null
done
rm -rf $ORIGDIR 2>/dev/null
mount -o ro,remount $MAGISKTMP
}
cleanup() {
$KSU && umount_mirrors
rm -rf $MODPATH/common $MODPATH/install.zip 2>/dev/null
}
abort() {
ui_print "$1"
rm -rf $MODPATH 2>/dev/null
cleanup
rm -rf $TMPDIR 2>/dev/null
exit 1
}
device_check() {
local opt=`getopt -o dm -- "$@"` type=device
eval set -- "$opt"
while true; do
case "$1" in
-d) local type=device; shift;;
-m) local type=manufacturer; shift;;
--) shift; break;;
*) abort "Invalid device_check argument $1! Aborting!";;
esac
done
local prop=$(echo "$1" | tr '[:upper:]' '[:lower:]')
for i in /system /vendor /odm /product; do
if [ -f $i/build.prop ]; then
for j in "ro.product.$type" "ro.build.$type" "ro.product.vendor.$type" "ro.vendor.product.$type"; do
[ "$(sed -n "s/^$j=//p" $i/build.prop 2>/dev/null | head -n 1 | tr '[:upper:]' '[:lower:]')" == "$prop" ] && return 0
done
[ "$type" == "device" ] && [ "$(sed -n "s/^"ro.build.product"=//p" $i/build.prop 2>/dev/null | head -n 1 | tr '[:upper:]' '[:lower:]')" == "$prop" ] && return 0
fi
done
return 1
}
cp_ch() {
local opt=`getopt -o nr -- "$@"` BAK=true UBAK=true FOL=false
eval set -- "$opt"
while true; do
case "$1" in
-n) UBAK=false; shift;;
-r) FOL=true; shift;;
--) shift; break;;
*) abort "Invalid cp_ch argument $1! Aborting!";;
esac
done
local SRC="$1" DEST="$2" OFILES="$1"
$FOL && local OFILES=$(find $SRC -type f 2>/dev/null)
[ -z $3 ] && PERM=0644 || PERM=$3
case "$DEST" in
$TMPDIR/*|$MODULEROOT/*|$NVBASE/modules/$MODID/*) BAK=false;;
esac
for OFILE in ${OFILES}; do
if $FOL; then
if [ "$(basename $SRC)" == "$(basename $DEST)" ]; then
local FILE=$(echo $OFILE | sed "s|$SRC|$DEST|")
else
local FILE=$(echo $OFILE | sed "s|$SRC|$DEST/$(basename $SRC)|")
fi
else
[ -d "$DEST" ] && local FILE="$DEST/$(basename $SRC)" || local FILE="$DEST"
fi
if $BAK && $UBAK; then
[ ! "$(grep "$FILE$" $INFO 2>/dev/null)" ] && echo "$FILE" >> $INFO
[ -f "$FILE" -a ! -f "$FILE~" ] && { mv -f $FILE $FILE~; echo "$FILE~" >> $INFO; }
elif $BAK; then
[ ! "$(grep "$FILE$" $INFO 2>/dev/null)" ] && echo "$FILE" >> $INFO
fi
install -D -m $PERM "$OFILE" "$FILE"
done
}
install_script() {
case "$1" in
-b) shift;
if $KSU; then
local INPATH=$NVBASE/boot-completed.d
else
local INPATH=$SERVICED
sed -i -e '1i (\nwhile [ "$(getprop sys.boot_completed)" != "1" ]; do\n sleep 1\ndone\nsleep 3\n' -e '$a)&' $1
fi;;
-l) shift; local INPATH=$SERVICED;;
-p) shift; local INPATH=$POSTFSDATAD;;
*) local INPATH=$SERVICED;;
esac
[ "$(grep "#!/system/bin/sh" $1)" ] || sed -i "1i #!/system/bin/sh" $1
local i; for i in "MODPATH" "LIBDIR" "MODID" "INFO" "MODDIR"; do
case $i in
"MODPATH") sed -i "1a $i=$NVBASE/modules/$MODID" $1;;
"MODDIR") sed -i "1a $i=\${0%/*}" $1;;
*) sed -i "1a $i=$(eval echo \$$i)" $1;;
esac
done
case $1 in
"$MODPATH/post-fs-data.sh"|"$MODPATH/service.sh"|"$MODPATH/uninstall.sh") sed -i "s|^MODPATH=.*|MODPATH=\$MODDIR|" $1;; # MODPATH=MODDIR for these scripts (located in module directory)
"$MODPATH/boot-completed.sh") $KSU && sed -i "s|^MODPATH=.*|MODPATH=\$MODDIR|" $1 || { cp_ch -n $1 $INPATH/$MODID-$(basename $1) 0755; rm -f $MODPATH/boot-completed.sh; };;
*) cp_ch -n $1 $INPATH/$(basename $1) 0755;;
esac
}
prop_process() {
sed -i -e "/^#/d" -e "/^ *$/d" $1
[ -f $MODPATH/system.prop ] || mktouch $MODPATH/system.prop
while read LINE; do
echo "$LINE" >> $MODPATH/system.prop
done < $1
}
mount_mirrors() {
mount -o rw,remount $MAGISKTMP
mkdir -p $ORIGDIR/system
if $SYSTEM_ROOT; then
mkdir -p $ORIGDIR/system_root
mount -o ro / $ORIGDIR/system_root
mount -o bind $ORIGDIR/system_root/system $ORIGDIR/system
else
mount -o ro /system $ORIGDIR/system
fi
for i in /vendor $PARTITIONS; do
[ ! -d $i -o -d $ORIGDIR$i ] && continue
mkdir -p $ORIGDIR$i
mount -o ro $i $ORIGDIR$i
done
}
# Credits
ui_print "**************************************"
ui_print "* MMT Extended by Zackptg5 @ XDA *"
ui_print "**************************************"
ui_print " "
# Check for min/max api version
[ -z $MINAPI ] || { [ $API -lt $MINAPI ] && abort "! Your system API of $API is less than the minimum api of $MINAPI! Aborting!"; }
[ -z $MAXAPI ] || { [ $API -gt $MAXAPI ] && abort "! Your system API of $API is greater than the maximum api of $MAXAPI! Aborting!"; }
# Min KSU v0.6.6
[ -z $KSU ] && KSU=false
$KSU && { [ $KSU_VER_CODE -lt 11184 ] && require_new_ksu; }
# Start debug
set -x
# Set variables
[ -z $ARCH32 ] && ARCH32="$(echo $ABI32 | cut -c-3)"
[ $API -lt 26 ] && DYNLIB=false
[ -z $DYNLIB ] && DYNLIB=false
[ -z $PARTOVER ] && PARTOVER=false
[ -z $SYSTEM_ROOT ] && SYSTEM_ROOT=$SYSTEM_AS_ROOT # renamed in magisk v26.3
[ -z $SERVICED ] && SERVICED=$NVBASE/service.d # removed in magisk v26.2
[ -z $POSTFSDATAD ] && POSTFSDATAD=$NVBASE/post-fs-data.d # removed in magisk v26.2
INFO=$NVBASE/modules/.$MODID-files
if $KSU; then
MAGISKTMP="/mnt"
ORIGDIR="$MAGISKTMP/mirror"
mount_mirrors
elif [ "$(magisk --path 2>/dev/null)" ]; then
ORIGDIR="$(magisk --path 2>/dev/null)/.magisk/mirror"
elif [ "$(echo $MAGISKTMP | awk -F/ '{ print $NF}')" == ".magisk" ]; then
ORIGDIR="$MAGISKTMP/mirror"
else
ORIGDIR="$MAGISKTMP/.magisk/mirror"
fi
if $DYNLIB; then
LIBPATCH="\/vendor"
LIBDIR=/system/vendor
else
LIBPATCH="\/system"
LIBDIR=/system
fi
# Detect extra partition compatibility (KernelSU or Magisk Delta)
EXTRAPART=false
if $KSU || [ "$(echo $MAGISK_VER | awk -F- '{ print $NF}')" == "delta" ]; then
EXTRAPART=true
elif ! $PARTOVER; then
unset PARTITIONS
fi
if ! $BOOTMODE; then
ui_print "- Only uninstall is supported in recovery"
ui_print " Uninstalling!"
touch $MODPATH/remove
[ -s $INFO ] && install_script $MODPATH/uninstall.sh || rm -f $INFO $MODPATH/uninstall.sh
recovery_cleanup
cleanup
rm -rf $NVBASE/modules_update/$MODID $TMPDIR 2>/dev/null
exit 0
fi
# Extract files
ui_print "- Extracting module files"
unzip -o "$ZIPFILE" -x 'META-INF/*' 'common/functions.sh' -d $MODPATH >&2
[ -f "$MODPATH/common/addon.tar.xz" ] && tar -xf $MODPATH/common/addon.tar.xz -C $MODPATH/common 2>/dev/null
# Run addons
if [ "$(ls -A $MODPATH/common/addon/*/install.sh 2>/dev/null)" ]; then
ui_print " "; ui_print "- Running Addons -"
for i in $MODPATH/common/addon/*/install.sh; do
ui_print " Running $(echo $i | sed -r "s|$MODPATH/common/addon/(.*)/install.sh|\1|")..."
. $i
done
fi
# Remove files outside of module directory
ui_print "- Removing old files"
if [ -f $INFO ]; then
while read LINE; do
if [ "$(echo -n $LINE | tail -c 1)" == "~" ]; then
continue
elif [ -f "$LINE~" ]; then
mv -f $LINE~ $LINE
else
rm -f $LINE
while true; do
LINE=$(dirname $LINE)
[ "$(ls -A $LINE 2>/dev/null)" ] && break 1 || rm -rf $LINE
done
fi
done < $INFO
rm -f $INFO
fi
### Install
ui_print "- Installing"
[ -f "$MODPATH/common/install.sh" ] && . $MODPATH/common/install.sh
ui_print " Installing for $ARCH SDK $API device..."
# Remove comments from files and place them, add blank line to end if not already present
for i in $(find $MODPATH -type f -name "*.sh" -o -name "*.prop" -o -name "*.rule"); do
[ -f $i ] && { sed -i -e "/^#/d" -e "/^ *$/d" $i; [ "$(tail -1 $i)" ] && echo "" >> $i; } || continue
case $i in
"$MODPATH/boot-completed.sh") install_script -b $i;;
"$MODPATH/service.sh") install_script -l $i;;
"$MODPATH/post-fs-data.sh") install_script -p $i;;
"$MODPATH/uninstall.sh") if [ -s $INFO ] || [ "$(head -n1 $MODPATH/uninstall.sh)" != "# Don't modify anything after this" ]; then
cp -f $MODPATH/uninstall.sh $MODPATH/$MODID-uninstall.sh # Fallback script in case module manually deleted
sed -i "1i[ -d \"\$MODPATH\" ] && exit 0" $MODPATH/$MODID-uninstall.sh
echo 'rm -f $0' >> $MODPATH/$MODID-uninstall.sh
install_script -l $MODPATH/$MODID-uninstall.sh
rm -f $MODPATH/$MODID-uninstall.sh
install_script $MODPATH/uninstall.sh
else
rm -f $INFO $MODPATH/uninstall.sh
fi;;
esac
done
$IS64BIT || for i in $(find $MODPATH/system -type d -name "lib64"); do rm -rf $i 2>/dev/null; done
[ -d "/system/priv-app" ] || mv -f $MODPATH/system/priv-app $MODPATH/system/app 2>/dev/null
[ -d "/system/xbin" ] || mv -f $MODPATH/system/xbin $MODPATH/system/bin 2>/dev/null
if $DYNLIB; then
for FILE in $(find $MODPATH/system/lib* -type f 2>/dev/null | sed "s|$MODPATH/system/||"); do
[ -s $MODPATH/system/$FILE ] || continue
case $FILE in
lib*/modules/*) continue;;
esac
mkdir -p $(dirname $MODPATH/system/vendor/$FILE)
mv -f $MODPATH/system/$FILE $MODPATH/system/vendor/$FILE
[ "$(ls -A `dirname $MODPATH/system/$FILE`)" ] || rm -rf `dirname $MODPATH/system/$FILE`
done
# Delete empty lib folders (busybox find doesn't have this capability)
toybox find $MODPATH/system/lib* -type d -empty -delete >/dev/null 2>&1
fi
# Set permissions
ui_print " "
ui_print "- Setting Permissions"
set_perm_recursive $MODPATH 0 0 0755 0644
for i in /system/vendor /vendor /system/vendor/app /vendor/app /system/vendor/etc /vendor/etc /system/odm/etc /odm/etc /system/vendor/odm/etc /vendor/odm/etc /system/vendor/overlay /vendor/overlay; do
if [ -d "$MODPATH$i" ] && [ ! -L "$MODPATH$i" ]; then
case $i in
*"/vendor") set_perm_recursive $MODPATH$i 0 0 0755 0644 u:object_r:vendor_file:s0;;
*"/app") set_perm_recursive $MODPATH$i 0 0 0755 0644 u:object_r:vendor_app_file:s0;;
*"/overlay") set_perm_recursive $MODPATH$i 0 0 0755 0644 u:object_r:vendor_overlay_file:s0;;
*"/etc") set_perm_recursive $MODPATH$i 0 2000 0755 0644 u:object_r:vendor_configs_file:s0;;
esac
fi
done
for i in $(find $MODPATH/system/vendor $MODPATH/vendor -type f -name *".apk" 2>/dev/null); do
chcon u:object_r:vendor_app_file:s0 $i
done
set_permissions
# Complete install
cleanup

28
common/install.sh Normal file
View File

@ -0,0 +1,28 @@
SOC=`getprop ro.board.platform`
ui_print ""
ui_print "*** $MODID"
ui_print "** SDK $API"
ui_print "** ARCH $ARCH"
ui_print "** SOC $SOC"
ui_print "***"
ui_print ""
#
# JoyPixels installation
#
# The following is the default implementation: extract $ZIPFILE/system to $MODPATH
# Extend/change the logic to whatever you want
ui_print "- Extracting module files"
unzip -o "$ZIPFILE" 'system/fonts/JoyPixels.ttf' -d $MODPATH >&2
ui_print "- Searching in fonts.xml"
[[ -d /sbin/.core/mirror ]] && MIRRORPATH=/sbin/.core/mirror || unset MIRRORPATH
FILE=/system/etc/fonts.xml
FILENAME=$(sed -ne '/<family lang="und-Zsye".*>/,/<\/family>/ {s/.*<font weight="400" style="normal">\(.*\)<\/font>.*/\1/p;}' $MIRRORPATH$FILE)
for i in $FILENAME
do
ui_print "- Copying fonts files to $i"
cp -f $MODPATH/system/fonts/JoyPixels.ttf $MODPATH/system/fonts/$i
done

240
common/service_balance.txt Normal file
View File

@ -0,0 +1,240 @@
#
_SLEEPBOOT=60
RETRY_INTERVAL=${_SLEEPBOOT} #in seconds
MAX_RETRY=3
retry=${MAX_RETRY}
while (("$retry" > "0")) && [ "$(getprop sys.boot_completed)" != "1" ]; do
sleep ${RETRY_INTERVAL}
(($retry--))
done
### --------------------------------------------------
### Kraoc
### --------------------------------------------------
# Random entropy
setprop kernel.random.read_wakeup_threshold 128
setprop kernel.random.write_wakeup_threshold 896
sysctl -e -w kernel.random.read_wakeup_threshold=128
sysctl -e -w kernel.random.write_wakeup_threshold=896
# UI scale
settings put global window_animation_scale 0.25
settings put global transition_animation_scale 0.25
settings put global animator_duration_scale 0.25
### --------------------------------------------------
### Ktweaks
### --------------------------------------------------
#!/usr/bin/env bash
# Written by Draco (tytydraco @ GitHub)
# The name of the current branch for logging purposes
BRANCH="balance"
# Maximum unsigned integer size in C
UINT_MAX="4294967295"
# Duration in nanoseconds of one scheduling period
SCHED_PERIOD="$((4 * 1000 * 1000))"
# How many tasks should we have at a maximum in one scheduling period
SCHED_TASKS="8"
write() {
# Bail out if file does not exist
[[ ! -f "$1" ]] && return 1
# Make file writable in case it is not already
chmod +w "$1" 2> /dev/null
# Write the new value and bail if there's an error
if ! echo "$2" > "$1" 2> /dev/null
then
# echo "Failed: $1 → $2"
return 1
fi
# Log the success
# echo "$1 → $2"
}
## Check for root permissions and bail if not granted
#if [[ "$(id -u)" -ne 0 ]]
#then
## echo "No root permissions. Exiting."
# exit 1
#fi
# Detect if we are running on Android
#grep -q android /proc/cmdline && ANDROID=true
ANDROID=true
## Log the date and time for records sake
#echo "Time of execution: $(date)"
#echo "Branch: $BRANCH"
# Sync to data in the rare case a device crashes
#sync
# Limit max perf event processing time to this much CPU usage
write /proc/sys/kernel/perf_cpu_time_max_percent 5
# Group tasks for less stutter but less throughput
write /proc/sys/kernel/sched_autogroup_enabled 1
# Execute child process before parent after fork
write /proc/sys/kernel/sched_child_runs_first 1
# Preliminary requirement for the following values
write /proc/sys/kernel/sched_tunable_scaling 0
# Reduce the maximum scheduling period for lower latency
write /proc/sys/kernel/sched_latency_ns "$SCHED_PERIOD"
# Schedule this ratio of tasks in the guarenteed sched period
write /proc/sys/kernel/sched_min_granularity_ns "$((SCHED_PERIOD / SCHED_TASKS))"
# Require preeptive tasks to surpass half of a sched period in vmruntime
write /proc/sys/kernel/sched_wakeup_granularity_ns "$((SCHED_PERIOD / 2))"
# Reduce the frequency of task migrations
write /proc/sys/kernel/sched_migration_cost_ns 5000000
# Always allow sched boosting on top-app tasks
[[ "$ANDROID" == true ]] && write /proc/sys/kernel/sched_min_task_util_for_colocation 0
# Improve real time latencies by reducing the scheduler migration time
write /proc/sys/kernel/sched_nr_migrate 32
# Disable scheduler statistics to reduce overhead
write /proc/sys/kernel/sched_schedstats 0
# Disable unnecessary printk logging
write /proc/sys/kernel/printk_devkmsg off
# Start non-blocking writeback later
write /proc/sys/vm/dirty_background_ratio 10
# Start blocking writeback later
write /proc/sys/vm/dirty_ratio 30
# Require dirty memory to stay in memory for longer
write /proc/sys/vm/dirty_expire_centisecs 3000
# Run the dirty memory flusher threads less often
write /proc/sys/vm/dirty_writeback_centisecs 3000
# Disable read-ahead for swap devices
write /proc/sys/vm/page-cluster 0
# Update /proc/stat less often to reduce jitter
write /proc/sys/vm/stat_interval 10
# Swap to the swap device at a fair rate
write /proc/sys/vm/swappiness 100
# Fairly prioritize page cache and file structures
write /proc/sys/vm/vfs_cache_pressure 100
# Enable Explicit Congestion Control
write /proc/sys/net/ipv4/tcp_ecn 1
# Enable fast socket open for receiver and sender
write /proc/sys/net/ipv4/tcp_fastopen 3
# Disable SYN cookies
write /proc/sys/net/ipv4/tcp_syncookies 0
if [[ -f "/sys/kernel/debug/sched_features" ]]
then
# Consider scheduling tasks that are eager to run
write /sys/kernel/debug/sched_features NEXT_BUDDY
# Schedule tasks on their origin CPU if possible
write /sys/kernel/debug/sched_features TTWU_QUEUE
fi
[[ "$ANDROID" == true ]] && if [[ -d "/dev/stune/" ]]
then
# We are not concerned with prioritizing latency
write /dev/stune/top-app/schedtune.prefer_idle 0
# Mark top-app as boosted, find high-performing CPUs
write /dev/stune/top-app/schedtune.boost 1
fi
# Loop over each CPU in the system
for cpu in /sys/devices/system/cpu/cpu*/cpufreq
do
# Fetch the available governors from the CPU
avail_govs="$(cat "$cpu/scaling_available_governors")"
# Attempt to set the governor in this order
for governor in schedutil interactive
do
# Once a matching governor is found, set it and break for this CPU
if [[ "$avail_govs" == *"$governor"* ]]
then
write "$cpu/scaling_governor" "$governor"
break
fi
done
done
# Apply governor specific tunables for schedutil
find /sys/devices/system/cpu/ -name schedutil -type d | while IFS= read -r governor
do
# Consider changing frequencies once per scheduling period
write "$governor/up_rate_limit_us" "$((SCHED_PERIOD / 1000))"
write "$governor/down_rate_limit_us" "$((4 * SCHED_PERIOD / 1000))"
write "$governor/rate_limit_us" "$((SCHED_PERIOD / 1000))"
# Jump to hispeed frequency at this load percentage
write "$governor/hispeed_load" 90
write "$governor/hispeed_freq" "$UINT_MAX"
done
# Apply governor specific tunables for interactive
find /sys/devices/system/cpu/ -name interactive -type d | while IFS= read -r governor
do
# Consider changing frequencies once per scheduling period
write "$governor/timer_rate" "$((SCHED_PERIOD / 1000))"
write "$governor/min_sample_time" "$((SCHED_PERIOD / 1000))"
# Jump to hispeed frequency at this load percentage
write "$governor/go_hispeed_load" 90
write "$governor/hispeed_freq" "$UINT_MAX"
done
for queue in /sys/block/*/queue
do
# Choose the first governor available
avail_scheds="$(cat "$queue/scheduler")"
for sched in cfq noop kyber bfq mq-deadline none
do
if [[ "$avail_scheds" == *"$sched"* ]]
then
write "$queue/scheduler" "$sched"
break
fi
done
# Do not use I/O as a source of randomness
write "$queue/add_random" 0
# Disable I/O statistics accounting
write "$queue/iostats" 0
# Reduce heuristic read-ahead in exchange for I/O latency
write "$queue/read_ahead_kb" 128
# Reduce the maximum number of I/O requests in exchange for latency
write "$queue/nr_requests" 64
done
# Always return success, even if the last write fails
#exit 0

240
common/service_budget.txt Normal file
View File

@ -0,0 +1,240 @@
#
_SLEEPBOOT=60
RETRY_INTERVAL=${_SLEEPBOOT} #in seconds
MAX_RETRY=3
retry=${MAX_RETRY}
while (("$retry" > "0")) && [ "$(getprop sys.boot_completed)" != "1" ]; do
sleep ${RETRY_INTERVAL}
(($retry--))
done
### --------------------------------------------------
### Kraoc
### --------------------------------------------------
# Random entropy
setprop kernel.random.read_wakeup_threshold 128
setprop kernel.random.write_wakeup_threshold 896
sysctl -e -w kernel.random.read_wakeup_threshold=128
sysctl -e -w kernel.random.write_wakeup_threshold=896
# UI scale
settings put global window_animation_scale 0.25
settings put global transition_animation_scale 0.25
settings put global animator_duration_scale 0.25
### --------------------------------------------------
### Ktweaks
### --------------------------------------------------
#!/usr/bin/env bash
# Written by Draco (tytydraco @ GitHub)
# The name of the current branch for logging purposes
BRANCH="budget"
# Maximum unsigned integer size in C
UINT_MAX="4294967295"
# Duration in nanoseconds of one scheduling period
SCHED_PERIOD="$((5 * 1000 * 1000))"
# How many tasks should we have at a maximum in one scheduling period
SCHED_TASKS="5"
write() {
# Bail out if file does not exist
[[ ! -f "$1" ]] && return 1
# Make file writable in case it is not already
chmod +w "$1" 2> /dev/null
# Write the new value and bail if there's an error
if ! echo "$2" > "$1" 2> /dev/null
then
# echo "Failed: $1 → $2"
return 1
fi
# Log the success
# echo "$1 → $2"
}
## Check for root permissions and bail if not granted
#if [[ "$(id -u)" -ne 0 ]]
#then
## echo "No root permissions. Exiting."
# exit 1
#fi
# Detect if we are running on Android
#grep -q android /proc/cmdline && ANDROID=true
ANDROID=true
## Log the date and time for records sake
#echo "Time of execution: $(date)"
#echo "Branch: $BRANCH"
# Sync to data in the rare case a device crashes
#sync
# Limit max perf event processing time to this much CPU usage
write /proc/sys/kernel/perf_cpu_time_max_percent 2
# Group tasks for less stutter but less throughput
write /proc/sys/kernel/sched_autogroup_enabled 1
# Execute child process before parent after fork
write /proc/sys/kernel/sched_child_runs_first 0
# Preliminary requirement for the following values
write /proc/sys/kernel/sched_tunable_scaling 0
# Reduce the maximum scheduling period for lower latency
write /proc/sys/kernel/sched_latency_ns "$SCHED_PERIOD"
# Schedule this ratio of tasks in the guarenteed sched period
write /proc/sys/kernel/sched_min_granularity_ns "$((SCHED_PERIOD / SCHED_TASKS))"
# Require preeptive tasks to surpass half of a sched period in vmruntime
write /proc/sys/kernel/sched_wakeup_granularity_ns "$((SCHED_PERIOD / 2))"
# Reduce the frequency of task migrations
write /proc/sys/kernel/sched_migration_cost_ns 5000000
# Always allow sched boosting on top-app tasks
[[ "$ANDROID" == true ]] && write /proc/sys/kernel/sched_min_task_util_for_colocation 0
# Give up some latency to give the CPU a break
write /proc/sys/kernel/sched_nr_migrate 256
# Disable scheduler statistics to reduce overhead
write /proc/sys/kernel/sched_schedstats 0
# Disable unnecessary printk logging
write /proc/sys/kernel/printk_devkmsg off
# Budget devices are low-ram devices, don't let this build up
write /proc/sys/vm/dirty_background_ratio 2
# Flush completely when this much of the device is fulled
write /proc/sys/vm/dirty_ratio 5
# Expire dirty memory very early
write /proc/sys/vm/dirty_expire_centisecs 500
# Run the dirty memory flusher threads more often
write /proc/sys/vm/dirty_writeback_centisecs 500
# Disable read-ahead for swap devices
write /proc/sys/vm/page-cluster 0
# Update /proc/stat less often to reduce jitter
write /proc/sys/vm/stat_interval 10
# Swap to the swap device at a fair rate
write /proc/sys/vm/swappiness 100
# Fairly prioritize page cache and file structures
write /proc/sys/vm/vfs_cache_pressure 100
# Enable Explicit Congestion Control
write /proc/sys/net/ipv4/tcp_ecn 1
# Enable fast socket open for receiver and sender
write /proc/sys/net/ipv4/tcp_fastopen 3
# Disable SYN cookies
write /proc/sys/net/ipv4/tcp_syncookies 0
if [[ -f "/sys/kernel/debug/sched_features" ]]
then
# Consider scheduling tasks that are eager to run
write /sys/kernel/debug/sched_features NEXT_BUDDY
# Prioritize power over cache hits
write /sys/kernel/debug/sched_features NO_TTWU_QUEUE
fi
[[ "$ANDROID" == true ]] && if [[ -d "/dev/stune/" ]]
then
# We are not concerned with prioritizing latency
write /dev/stune/top-app/schedtune.prefer_idle 0
# Don't boost foreground tasks, let the governor handle it
write /dev/stune/top-app/schedtune.boost 0
fi
# Loop over each CPU in the system
for cpu in /sys/devices/system/cpu/cpu*/cpufreq
do
# Fetch the available governors from the CPU
avail_govs="$(cat "$cpu/scaling_available_governors")"
# Attempt to set the governor in this order
for governor in schedutil interactive
do
# Once a matching governor is found, set it and break for this CPU
if [[ "$avail_govs" == *"$governor"* ]]
then
write "$cpu/scaling_governor" "$governor"
break
fi
done
done
# Apply governor specific tunables for schedutil
find /sys/devices/system/cpu/ -name schedutil -type d | while IFS= read -r governor
do
# Consider changing frequencies once per scheduling period
write "$governor/up_rate_limit_us" "$((SCHED_PERIOD / 1000))"
write "$governor/down_rate_limit_us" "$((SCHED_PERIOD / 1000))"
write "$governor/rate_limit_us" "$((SCHED_PERIOD / 1000))"
# Jump to hispeed frequency at this load percentage
write "$governor/hispeed_load" 99
write "$governor/hispeed_freq" "$UINT_MAX"
done
# Apply governor specific tunables for interactive
find /sys/devices/system/cpu/ -name interactive -type d | while IFS= read -r governor
do
# Consider changing frequencies once per scheduling period
write "$governor/timer_rate" "$((SCHED_PERIOD / 1000))"
write "$governor/min_sample_time" "$((SCHED_PERIOD / 1000))"
# Jump to hispeed frequency at this load percentage
write "$governor/go_hispeed_load" 99
write "$governor/hispeed_freq" "$UINT_MAX"
done
for queue in /sys/block/*/queue
do
# Choose the first governor available
avail_scheds="$(cat "$queue/scheduler")"
for sched in cfq noop kyber bfq mq-deadline none
do
if [[ "$avail_scheds" == *"$sched"* ]]
then
write "$queue/scheduler" "$sched"
break
fi
done
# Do not use I/O as a source of randomness
write "$queue/add_random" 0
# Disable I/O statistics accounting
write "$queue/iostats" 0
# Reduce heuristic read-ahead in exchange for I/O latency
write "$queue/read_ahead_kb" 64
# Increase maximum requests to reduce processing power
write "$queue/nr_requests" 512
done
# Always return success, even if the last write fails
#exit 0

240
common/service_latency.txt Normal file
View File

@ -0,0 +1,240 @@
#
_SLEEPBOOT=60
RETRY_INTERVAL=${_SLEEPBOOT} #in seconds
MAX_RETRY=3
retry=${MAX_RETRY}
while (("$retry" > "0")) && [ "$(getprop sys.boot_completed)" != "1" ]; do
sleep ${RETRY_INTERVAL}
(($retry--))
done
### --------------------------------------------------
### Kraoc
### --------------------------------------------------
# Random entropy
setprop kernel.random.read_wakeup_threshold 128
setprop kernel.random.write_wakeup_threshold 896
sysctl -e -w kernel.random.read_wakeup_threshold=128
sysctl -e -w kernel.random.write_wakeup_threshold=896
# UI scale
settings put global window_animation_scale 0.25
settings put global transition_animation_scale 0.25
settings put global animator_duration_scale 0.25
### --------------------------------------------------
### Ktweaks
### --------------------------------------------------
#!/usr/bin/env bash
# Written by Draco (tytydraco @ GitHub)
# The name of the current branch for logging purposes
BRANCH="latency"
# Maximum unsigned integer size in C
UINT_MAX="4294967295"
# Duration in nanoseconds of one scheduling period
SCHED_PERIOD="$((1 * 1000 * 1000))"
# How many tasks should we have at a maximum in one scheduling period
SCHED_TASKS="10"
write() {
# Bail out if file does not exist
[[ ! -f "$1" ]] && return 1
# Make file writable in case it is not already
chmod +w "$1" 2> /dev/null
# Write the new value and bail if there's an error
if ! echo "$2" > "$1" 2> /dev/null
then
# echo "Failed: $1 → $2"
return 1
fi
# Log the success
# echo "$1 → $2"
}
## Check for root permissions and bail if not granted
#if [[ "$(id -u)" -ne 0 ]]
#then
## echo "No root permissions. Exiting."
# exit 1
#fi
# Detect if we are running on Android
#grep -q android /proc/cmdline && ANDROID=true
ANDROID=true
# Log the date and time for records sake
#echo "Time of execution: $(date)"
#echo "Branch: $BRANCH"
# Sync to data in the rare case a device crashes
#sync
# Limit max perf event processing time to this much CPU usage
write /proc/sys/kernel/perf_cpu_time_max_percent 3
# Group tasks for less stutter but less throughput
write /proc/sys/kernel/sched_autogroup_enabled 1
# Execute child process before parent after fork
write /proc/sys/kernel/sched_child_runs_first 1
# Preliminary requirement for the following values
write /proc/sys/kernel/sched_tunable_scaling 0
# Reduce the maximum scheduling period for lower latency
write /proc/sys/kernel/sched_latency_ns "$SCHED_PERIOD"
# Schedule this ratio of tasks in the guarenteed sched period
write /proc/sys/kernel/sched_min_granularity_ns "$((SCHED_PERIOD / SCHED_TASKS))"
# Require preeptive tasks to surpass half of a sched period in vmruntime
write /proc/sys/kernel/sched_wakeup_granularity_ns "$((SCHED_PERIOD / 2))"
# Reduce the frequency of task migrations
write /proc/sys/kernel/sched_migration_cost_ns 5000000
# Always allow sched boosting on top-app tasks
[[ "$ANDROID" == true ]] && write /proc/sys/kernel/sched_min_task_util_for_colocation 0
# Improve real time latencies by reducing the scheduler migration time
write /proc/sys/kernel/sched_nr_migrate 4
# Disable scheduler statistics to reduce overhead
write /proc/sys/kernel/sched_schedstats 0
# Disable unnecessary printk logging
write /proc/sys/kernel/printk_devkmsg off
# Start non-blocking writeback later
write /proc/sys/vm/dirty_background_ratio 3
# Start blocking writeback later
write /proc/sys/vm/dirty_ratio 30
# Require dirty memory to stay in memory for longer
write /proc/sys/vm/dirty_expire_centisecs 3000
# Run the dirty memory flusher threads less often
write /proc/sys/vm/dirty_writeback_centisecs 3000
# Disable read-ahead for swap devices
write /proc/sys/vm/page-cluster 0
# Update /proc/stat less often to reduce jitter
write /proc/sys/vm/stat_interval 10
# Swap to the swap device at a fair rate
write /proc/sys/vm/swappiness 100
# Prioritize page cache over simple file structure nodes
write /proc/sys/vm/vfs_cache_pressure 200
# Enable Explicit Congestion Control
write /proc/sys/net/ipv4/tcp_ecn 1
# Enable fast socket open for receiver and sender
write /proc/sys/net/ipv4/tcp_fastopen 3
# Disable SYN cookies
write /proc/sys/net/ipv4/tcp_syncookies 0
if [[ -f "/sys/kernel/debug/sched_features" ]]
then
# Consider scheduling tasks that are eager to run
write /sys/kernel/debug/sched_features NEXT_BUDDY
# Some sources report large latency spikes during large migrations
write /sys/kernel/debug/sched_features NO_TTWU_QUEUE
fi
[[ "$ANDROID" == true ]] && if [[ -d "/dev/stune/" ]]
then
# Prefer to schedule top-app tasks on idle CPUs
write /dev/stune/top-app/schedtune.prefer_idle 1
# Mark top-app as boosted, find high-performing CPUs
write /dev/stune/top-app/schedtune.boost 1
fi
# Loop over each CPU in the system
for cpu in /sys/devices/system/cpu/cpu*/cpufreq
do
# Fetch the available governors from the CPU
avail_govs="$(cat "$cpu/scaling_available_governors")"
# Attempt to set the governor in this order
for governor in schedutil interactive
do
# Once a matching governor is found, set it and break for this CPU
if [[ "$avail_govs" == *"$governor"* ]]
then
write "$cpu/scaling_governor" "$governor"
break
fi
done
done
# Apply governor specific tunables for schedutil
find /sys/devices/system/cpu/ -name schedutil -type d | while IFS= read -r governor
do
# Consider changing frequencies once per scheduling period
write "$governor/up_rate_limit_us" 0
write "$governor/down_rate_limit_us" 0
write "$governor/rate_limit_us" 0
# Jump to hispeed frequency at this load percentage
write "$governor/hispeed_load" 85
write "$governor/hispeed_freq" "$UINT_MAX"
done
# Apply governor specific tunables for interactive
find /sys/devices/system/cpu/ -name interactive -type d | while IFS= read -r governor
do
# Consider changing frequencies once per scheduling period
write "$governor/timer_rate" 0
write "$governor/min_sample_time" 0
# Jump to hispeed frequency at this load percentage
write "$governor/go_hispeed_load" 85
write "$governor/hispeed_freq" "$UINT_MAX"
done
for queue in /sys/block/*/queue
do
# Choose the first governor available
avail_scheds="$(cat "$queue/scheduler")"
for sched in cfq noop kyber bfq mq-deadline none
do
if [[ "$avail_scheds" == *"$sched"* ]]
then
write "$queue/scheduler" "$sched"
break
fi
done
# Do not use I/O as a source of randomness
write "$queue/add_random" 0
# Disable I/O statistics accounting
write "$queue/iostats" 0
# Reduce heuristic read-ahead in exchange for I/O latency
write "$queue/read_ahead_kb" 32
# Reduce the maximum number of I/O requests in exchange for latency
write "$queue/nr_requests" 32
done
# Always return success, even if the last write fails
#exit 0

View File

@ -0,0 +1,240 @@
#
_SLEEPBOOT=60
RETRY_INTERVAL=${_SLEEPBOOT} #in seconds
MAX_RETRY=3
retry=${MAX_RETRY}
while (("$retry" > "0")) && [ "$(getprop sys.boot_completed)" != "1" ]; do
sleep ${RETRY_INTERVAL}
(($retry--))
done
### --------------------------------------------------
### Kraoc
### --------------------------------------------------
# Random entropy
setprop kernel.random.read_wakeup_threshold 128
setprop kernel.random.write_wakeup_threshold 896
sysctl -e -w kernel.random.read_wakeup_threshold=128
sysctl -e -w kernel.random.write_wakeup_threshold=896
# UI scale
settings put global window_animation_scale 0.25
settings put global transition_animation_scale 0.25
settings put global animator_duration_scale 0.25
### --------------------------------------------------
### Ktweaks
### --------------------------------------------------
#!/usr/bin/env bash
# Written by Draco (tytydraco @ GitHub)
# The name of the current branch for logging purposes
BRANCH="throughput"
# Maximum unsigned integer size in C
UINT_MAX="4294967295"
# Duration in nanoseconds of one scheduling period
SCHED_PERIOD="$((10 * 1000 * 1000))"
# How many tasks should we have at a maximum in one scheduling period
SCHED_TASKS="6"
write() {
# Bail out if file does not exist
[[ ! -f "$1" ]] && return 1
# Make file writable in case it is not already
chmod +w "$1" 2> /dev/null
# Write the new value and bail if there's an error
if ! echo "$2" > "$1" 2> /dev/null
then
# echo "Failed: $1 → $2"
return 1
fi
# Log the success
# echo "$1 → $2"
}
## Check for root permissions and bail if not granted
#if [[ "$(id -u)" -ne 0 ]]
#then
## echo "No root permissions. Exiting."
# exit 1
#fi
# Detect if we are running on Android
#grep -q android /proc/cmdline && ANDROID=true
ANDROID=true
## Log the date and time for records sake
#echo "Time of execution: $(date)"
#echo "Branch: $BRANCH"
# Sync to data in the rare case a device crashes
#sync
# Limit max perf event processing time to this much CPU usage
write /proc/sys/kernel/perf_cpu_time_max_percent 20
# Sacrifice latency for throughput when grouping tasks
write /proc/sys/kernel/sched_autogroup_enabled 0
# Process child-parent forks naturally
write /proc/sys/kernel/sched_child_runs_first 0
# Preliminary requirement for the following values
write /proc/sys/kernel/sched_tunable_scaling 0
# Reduce the maximum scheduling period for lower latency
write /proc/sys/kernel/sched_latency_ns "$SCHED_PERIOD"
# Schedule this ratio of tasks in the guarenteed sched period
write /proc/sys/kernel/sched_min_granularity_ns "$((SCHED_PERIOD / SCHED_TASKS))"
# Require preeptive tasks to surpass half of a sched period in vmruntime
write /proc/sys/kernel/sched_wakeup_granularity_ns "$((SCHED_PERIOD / 2))"
# Reduce the frequency of task migrations
write /proc/sys/kernel/sched_migration_cost_ns 5000000
# Always allow sched boosting on top-app tasks
[[ "$ANDROID" == true ]] && write /proc/sys/kernel/sched_min_task_util_for_colocation 0
# Improve real time latencies by reducing the scheduler migration time
write /proc/sys/kernel/sched_nr_migrate 128
# Disable scheduler statistics to reduce overhead
write /proc/sys/kernel/sched_schedstats 0
# Disable unnecessary printk logging
write /proc/sys/kernel/printk_devkmsg off
# Start non-blocking writeback later
write /proc/sys/vm/dirty_background_ratio 15
# Start blocking writeback later
write /proc/sys/vm/dirty_ratio 30
# Require dirty memory to stay in memory for longer
write /proc/sys/vm/dirty_expire_centisecs 3000
# Run the dirty memory flusher threads less often
write /proc/sys/vm/dirty_writeback_centisecs 3000
# Disable read-ahead for swap devices
write /proc/sys/vm/page-cluster 0
# Update /proc/stat less often to reduce jitter
write /proc/sys/vm/stat_interval 10
# Swap to the swap device at a fair rate
write /proc/sys/vm/swappiness 100
# Allow inodes and dentries to build up a larger cache
write /proc/sys/vm/vfs_cache_pressure 80
# Enable Explicit Congestion Control
write /proc/sys/net/ipv4/tcp_ecn 1
# Enable fast socket open for receiver and sender
write /proc/sys/net/ipv4/tcp_fastopen 3
# Disable SYN cookies
write /proc/sys/net/ipv4/tcp_syncookies 0
if [[ -f "/sys/kernel/debug/sched_features" ]]
then
# Consider scheduling tasks that are eager to run
write /sys/kernel/debug/sched_features NEXT_BUDDY
# Schedule tasks on their origin CPU if possible
write /sys/kernel/debug/sched_features TTWU_QUEUE
fi
[[ "$ANDROID" == true ]] && if [[ -d "/dev/stune/" ]]
then
# We are not concerned with prioritizing latency
write /dev/stune/top-app/schedtune.prefer_idle 0
# Mark top-app as boosted, find high-performing CPUs
write /dev/stune/top-app/schedtune.boost 1
fi
# Loop over each CPU in the system
for cpu in /sys/devices/system/cpu/cpu*/cpufreq
do
# Fetch the available governors from the CPU
avail_govs="$(cat "$cpu/scaling_available_governors")"
# Attempt to set the governor in this order
for governor in schedutil interactive
do
# Once a matching governor is found, set it and break for this CPU
if [[ "$avail_govs" == *"$governor"* ]]
then
write "$cpu/scaling_governor" "$governor"
break
fi
done
done
# Apply governor specific tunables for schedutil
find /sys/devices/system/cpu/ -name schedutil -type d | while IFS= read -r governor
do
# Consider changing frequencies once per scheduling period
write "$governor/up_rate_limit_us" "$((SCHED_PERIOD / 1000))"
write "$governor/down_rate_limit_us" "$((4 * SCHED_PERIOD / 1000))"
write "$governor/rate_limit_us" "$((SCHED_PERIOD / 1000))"
# Jump to hispeed frequency at this load percentage
write "$governor/hispeed_load" 85
write "$governor/hispeed_freq" "$UINT_MAX"
done
# Apply governor specific tunables for interactive
find /sys/devices/system/cpu/ -name interactive -type d | while IFS= read -r governor
do
# Consider changing frequencies once per scheduling period
write "$governor/timer_rate" "$((SCHED_PERIOD / 1000))"
write "$governor/min_sample_time" "$((SCHED_PERIOD / 1000))"
# Jump to hispeed frequency at this load percentage
write "$governor/go_hispeed_load" 85
write "$governor/hispeed_freq" "$UINT_MAX"
done
for queue in /sys/block/*/queue
do
# Choose the first governor available
avail_scheds="$(cat "$queue/scheduler")"
for sched in cfq noop kyber bfq mq-deadline none
do
if [[ "$avail_scheds" == *"$sched"* ]]
then
write "$queue/scheduler" "$sched"
break
fi
done
# Do not use I/O as a source of randomness
write "$queue/add_random" 0
# Disable I/O statistics accounting
write "$queue/iostats" 0
# Sacrifice I/O and memory efficiency for a possible throughput gain
write "$queue/read_ahead_kb" 256
# Buffer more requests at once
write "$queue/nr_requests" 512
done
# Always return success, even if the last write fails
#exit 0