From 276f542965a25a5132b2b98f55050b5898b3a7e8 Mon Sep 17 00:00:00 2001
From: ZyCromerZ <neetroid97@gmail.com>
Date: Thu, 16 Jun 2022 11:18:48 +0700
Subject: [PATCH] update cpu tweak method

Signed-off-by: ZyCromerZ <neetroid97@gmail.com>
---
 README-id.md                                  |  8 ++-
 README.md                                     |  8 ++-
 .../etc/ZyC-Core/configs/use_cpu_tweak.conf   |  2 +-
 system/etc/ZyC-Core/main.sh                   | 36 +++-------
 system/etc/ZyC-Core/misc/funclist.sh          | 66 ++++++++++++++++---
 5 files changed, 80 insertions(+), 40 deletions(-)

diff --git a/README-id.md b/README-id.md
index a85963b..bd58d75 100644
--- a/README-id.md
+++ b/README-id.md
@@ -57,7 +57,13 @@ dan jangan sentuh / ubah file apapun di dalam folder <code>/data/adb/modules/(Id
 * force_doze.conf
     * paksa ke mode doze ketika layarnya mati
 * use_cpu_tweak.conf
-    * kalo anda mau pake beberapa tweak cpu, tinggal set ke 1 atau 2
+    * kalo anda mau pake beberapa tweak cpu, tinggal set "ketika off":"ketika on", contoh 1:1
+        * 1 = Balance
+        * 2 = Balance V2
+        * 3 = Performance
+        * 4 = Performance V2
+        * 5 = Latency
+        * 0 and etc = jangan tweak cpu
 * silent_overwrite.conf
     * kalo situ pake modol tweak laennya, teros jadi conflict, cobain set ke 1, default 0 
 * write_info.conf
diff --git a/README.md b/README.md
index c9819bb..e5519f3 100644
--- a/README.md
+++ b/README.md
@@ -57,7 +57,13 @@ and please dont touch / edit any files inside folder <code>/data/adb/modules/(Mo
 * force_doze.conf
     * forcing to doze idle state when display/screen off
 * use_cpu_tweak.conf
-    * if u want to apply some cpu tweak from this modules, just set it to 1 or 2
+    * if u want to apply some cpu tweak from this modules, just set it to "when off":"when on", example 1:1
+        * 1 = Balance
+        * 2 = Balance V2
+        * 3 = Performance
+        * 4 = Performance V2
+        * 5 = Latency
+        * 0 and etc = dont tweak cpu
 * silent_overwrite.conf
     * if u use another tweak modules, then its conflict, try set this to 1, default 0
 * write_info.conf
diff --git a/system/etc/ZyC-Core/configs/use_cpu_tweak.conf b/system/etc/ZyC-Core/configs/use_cpu_tweak.conf
index 56a6051..9a7ed84 100644
--- a/system/etc/ZyC-Core/configs/use_cpu_tweak.conf
+++ b/system/etc/ZyC-Core/configs/use_cpu_tweak.conf
@@ -1 +1 @@
-1
\ No newline at end of file
+1:1
\ No newline at end of file
diff --git a/system/etc/ZyC-Core/main.sh b/system/etc/ZyC-Core/main.sh
index c1d8686..cb985ed 100644
--- a/system/etc/ZyC-Core/main.sh
+++ b/system/etc/ZyC-Core/main.sh
@@ -16,11 +16,10 @@
 
         CheckFileConfig
 
-        CpuTweakMode="$(cat $PMConfig/use_cpu_tweak.conf)"
-
-        if [[ "$CpuTweakMode" != "1" ]];then
-            [[ "$CpuTweakMode" != "0" ]] && WriteOnly "0" $PMConfig/use_cpu_tweak.conf && CpuTweakMode="0"
-        fi
+        CpuTweakModeOff="$(cat $PMConfig/use_cpu_tweak.conf | awk -F ":" '{print $1}')"
+        [[ -z "$CpuTweakModeOff" ]] && CpuTweakModeOff="0"
+        CpuTweakModeOn="$(cat $PMConfig/use_cpu_tweak.conf | awk -F ":" '{print $2}')"
+        [[ -z "$CpuTweakModeOn" ]] && CpuTweakModeOn="0"
 
         if [[ ! -z "$1" ]] && [[ "$1" == "boot" ]];then
             FirstBootMsg
@@ -30,10 +29,7 @@
             SetOff
             CpuInpuBoostUpdate "off"
             UpdateGov write "$PMConfig/gov_off.conf"
-            if [[ "$CpuTweakMode" == "1" ]];then
-                CpuBalance
-                VmBalance
-            fi
+            DoCpuTweak "$CpuTweakModeOff"
             FullDebug="on"
             UpdateFastCharging
             CpuFreqLock "Boot"
@@ -135,16 +131,7 @@
                     SetOn
                     CpuInpuBoostUpdate "on"
                     UpdateGov write "$PMConfig/gov_on.conf"
-                    if [[ "$CpuTweakMode" == "1" ]];then
-                         # https://github.com/tytydraco/KTweak/tree/latency
-                        CpuPerformance
-                        VmPerformance
-                    fi
-                    if [[ "$CpuTweakMode" == "2" ]];then
-                         # https://github.com/tytydraco/KTweak/tree/throughput
-                        CpuPerformanceB
-                        VmPerformanceB
-                    fi
+                    DoCpuTweak "$CpuTweakModeOn"
                     GoTurbo="0"
                     GoNormal="0"
                 elif [[ "$GoNormal" -ge "$MaxCheckGpuUsage" ]] && [[ "$GheymingMode" == "y" ]];then
@@ -152,11 +139,7 @@
                         SetOff
                         CpuInpuBoostUpdate "off"
                         UpdateGov write "$PMConfig/gov_off.conf"
-                        if [[ "$CpuTweakMode" == "1" ]] || [[ "$CpuTweakMode" == "2" ]];then
-                             # https://github.com/tytydraco/KTweak/tree/balance
-                            CpuBalance
-                            VmBalance
-                        fi
+                        DoCpuTweak "$CpuTweakModeOff"
                     fi
                     GoTurbo="0"
                     GoNormal="0"
@@ -165,10 +148,7 @@
                         if [[ "$GheymingMode" == "y" ]] && [[ "$DoSilentWrite" == "3" ]];then
                             FullDebug="off"
                             SetOn "silent"
-                            if [[ "$CpuTweakMode" == "1" ]];then
-                                CpuPerformance
-                                VmPerformance
-                            fi
+                            DoCpuTweak "$CpuTweakModeOn"
                             FullDebug="on"
                             DoSilentWrite="0"
                         else
diff --git a/system/etc/ZyC-Core/misc/funclist.sh b/system/etc/ZyC-Core/misc/funclist.sh
index d5ab145..c0374a6 100644
--- a/system/etc/ZyC-Core/misc/funclist.sh
+++ b/system/etc/ZyC-Core/misc/funclist.sh
@@ -748,7 +748,7 @@ CheckFileConfig(){
 
     [[ ! -f $PMConfig/force_doze.conf ]] && WriteOnly "0" $PMConfig/force_doze.conf
 
-    [[ ! -f $PMConfig/use_cpu_tweak.conf ]] && WriteOnly "1" $PMConfig/use_cpu_tweak.conf
+    [[ ! -f $PMConfig/use_cpu_tweak.conf ]] && WriteOnly "1:1" $PMConfig/use_cpu_tweak.conf
 
     [[ ! -f $PMConfig/silent_overwrite.conf ]] && WriteOnly "0" $PMConfig/silent_overwrite.conf
 
@@ -851,6 +851,7 @@ CpuBalance(){
     local IFS
     local governor
     local queue
+    local VTwo="${1}"
     WriteSysKernel  perf_cpu_time_max_percent:5 sched_autogroup_enabled:1 sched_child_runs_first:1 sched_tunable_scaling:0 "sched_latency_ns:$SCHED_PERIOD" \
                     "sched_min_granularity_ns:$((SCHED_PERIOD / SCHED_TASKS))" "sched_wakeup_granularity_ns:$((SCHED_PERIOD / 2))" sched_migration_cost_ns:5000000 \
                     sched_min_task_util_for_colocation:0 sched_nr_migrate:32 sched_schedstats:0 printk_devkmsg:off
@@ -859,18 +860,28 @@ CpuBalance(){
 
     find /sys/devices/system/cpu/ -name schedutil -type d | while IFS= read -r governor
     do
-        WriteTo "$((SCHED_PERIOD / 1000))" "$governor/up_rate_limit_us"
+        if [[ ! -z "$VTwo" ]];then
+            WriteTo "$((4 * SCHED_PERIOD / 1000))" "$governor/up_rate_limit_us"
+            WriteTo "$((4 * SCHED_PERIOD / 1000))" "$governor/rate_limit_us"
+        else
+            WriteTo "$((SCHED_PERIOD / 1000))" "$governor/up_rate_limit_us"
+            WriteTo "$((SCHED_PERIOD / 1000))" "$governor/rate_limit_us"
+        fi
         WriteTo "$((4 * SCHED_PERIOD / 1000))" "$governor/down_rate_limit_us"
-        WriteTo "$((SCHED_PERIOD / 1000))" "$governor/rate_limit_us"
         WriteTo 90 "$governor/hispeed_load"
         WriteTo "$UINT_MAX" "$governor/hispeed_freq"
     done
 
     find /sys/devices/system/cpu/ -name blu_schedutil -type d | while IFS= read -r governor
     do
-        WriteTo "$((SCHED_PERIOD / 1000))" "$governor/up_rate_limit_us"
+        if [[ ! -z "$VTwo" ]];then
+            WriteTo "$((4 * SCHED_PERIOD / 1000))" "$governor/up_rate_limit_us"
+            WriteTo "$((4 * SCHED_PERIOD / 1000))" "$governor/rate_limit_us"
+        else
+            WriteTo "$((SCHED_PERIOD / 1000))" "$governor/up_rate_limit_us"
+            WriteTo "$((SCHED_PERIOD / 1000))" "$governor/rate_limit_us"
+        fi
         WriteTo "$((4 * SCHED_PERIOD / 1000))" "$governor/down_rate_limit_us"
-        WriteTo "$((SCHED_PERIOD / 1000))" "$governor/rate_limit_us"
         WriteTo 90 "$governor/hispeed_load"
         WriteTo "$UINT_MAX" "$governor/hispeed_freq"
     done
@@ -904,6 +915,7 @@ CpuPerformance(){
     local IFS
     local governor
     local queue
+    local VTwo="${1}"
     WriteSysKernel  perf_cpu_time_max_percent:20 sched_autogroup_enabled:0 sched_child_runs_first:0 sched_tunable_scaling:0 "sched_latency_ns:$SCHED_PERIOD" \
                     "sched_min_granularity_ns:$((SCHED_PERIOD / SCHED_TASKS))" "sched_wakeup_granularity_ns:$((SCHED_PERIOD / 2))" sched_migration_cost_ns:5000000 \
                     sched_min_task_util_for_colocation:0 sched_nr_migrate:128 sched_schedstats:0 printk_devkmsg:off
@@ -913,18 +925,28 @@ CpuPerformance(){
 
     find /sys/devices/system/cpu/ -name schedutil -type d | while IFS= read -r governor
     do
-        WriteTo "$((SCHED_PERIOD / 1000))" "$governor/up_rate_limit_us"
+        if [[ ! -z "$VTwo" ]];then
+            WriteTo "$((4 * SCHED_PERIOD / 1000))" "$governor/up_rate_limit_us"
+            WriteTo "$((4 * SCHED_PERIOD / 1000))" "$governor/rate_limit_us"
+        else
+            WriteTo "$((SCHED_PERIOD / 1000))" "$governor/up_rate_limit_us"
+            WriteTo "$((SCHED_PERIOD / 1000))" "$governor/rate_limit_us"
+        fi
         WriteTo "$((4 * SCHED_PERIOD / 1000))" "$governor/down_rate_limit_us"
-        WriteTo "$((SCHED_PERIOD / 1000))" "$governor/rate_limit_us"
         WriteTo 85 "$governor/hispeed_load"
         WriteTo "$UINT_MAX" "$governor/hispeed_freq"
     done
 
     find /sys/devices/system/cpu/ -name blu_schedutil -type d | while IFS= read -r governor
     do
-        WriteTo "$((SCHED_PERIOD / 1000))" "$governor/up_rate_limit_us"
+        if [[ ! -z "$VTwo" ]];then
+            WriteTo "$((4 * SCHED_PERIOD / 1000))" "$governor/up_rate_limit_us"
+            WriteTo "$((4 * SCHED_PERIOD / 1000))" "$governor/rate_limit_us"
+        else
+            WriteTo "$((SCHED_PERIOD / 1000))" "$governor/up_rate_limit_us"
+            WriteTo "$((SCHED_PERIOD / 1000))" "$governor/rate_limit_us"
+        fi
         WriteTo "$((4 * SCHED_PERIOD / 1000))" "$governor/down_rate_limit_us"
-        WriteTo "$((SCHED_PERIOD / 1000))" "$governor/rate_limit_us"
         WriteTo 85 "$governor/hispeed_load"
         WriteTo "$UINT_MAX" "$governor/hispeed_freq"
     done
@@ -1007,6 +1029,32 @@ VmPerformanceB(){
                 swappiness:100 vfs_cache_pressure:80
 }
 
+DoCpuTweak()
+{
+    local mode="${1}"
+    if [[ "$mode" == "1" ]];then
+        # https://github.com/tytydraco/KTweak/tree/balance
+        CpuBalance
+        VmBalance
+    elif [[ "$mode" == "2" ]];then
+        # https://github.com/tytydraco/KTweak/tree/balance + tweaked
+        CpuBalance "y"
+        VmBalance
+    elif [[ "$mode" == "3" ]];then
+        # https://github.com/tytydraco/KTweak/tree/throughput
+        CpuPerformance
+        VmPerformance
+    elif [[ "$mode" == "4" ]];then
+        # https://github.com/tytydraco/KTweak/tree/throughput + tweaked
+        CpuPerformance "y"
+        VmPerformance
+    elif [[ "$mode" == "5" ]];then
+        # https://github.com/tytydraco/KTweak/tree/latency
+        CpuPerformanceB
+        VmPerformanceB
+    fi
+}
+
 SetFreqCpu(){
     local Lt="-1"
     local Bg="-1"
-- 
GitLab