-
Notifications
You must be signed in to change notification settings - Fork 7
/
libcommon.sh
484 lines (415 loc) · 14.9 KB
/
libcommon.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
#!/system/bin/sh
# KTSR™ by Pedro (pedrozzz0 @ GitHub)
# Thanks to Draco (tytydraco @ GitHub) and Matt Yang (yc9559 @ CoolApk)
# If you wanna use the code as part of your project, please maintain the credits to it's respectives authors
#####################
# Variables
#####################
bbn_log="/data/media/0/ktsr/bourbon.log"
bbn_info="/data/media/0/ktsr/bourbon.info"
adj_cfg="/data/media/0/ktsr/adjshield.conf"
adj_log="/data/media/0/ktsr/adjshield.log"
sys_frm="/system/framework"
sys_lib="/system/lib64"
vdr_lib="/vendor/lib64"
dvk="/data/dalvik-cache"
apx1="/apex/com.android.art/javalib"
apx2="/apex/com.android.runtime/javalib"
fscc_file_list=""
log_i() {
echo "[$(date +'%Y-%m-%d %T')]: [*] $1" >>"$klog"
echo "" >>"$klog"
}
log_d() {
echo "$1" >>"$kdbg"
echo "$1"
}
log_e() {
echo "[!] $1" >>"$kdbg"
echo "[!] $1"
}
notif_start() { su -lp 2000 -c "cmd notification post -S bigtext -t 'King Tweaks is executing' tag 'Applying $ktsr_prof_en profile...'" >/dev/null 2>&1; }
notif_end() { su -lp 2000 -c "cmd notification post -S bigtext -t 'King Tweaks is executing' tag '$ktsr_prof_en profile applied'" >/dev/null 2>&1; }
notif_pt_start() { su -lp 2000 -c "cmd notification post -S bigtext -t 'King Tweaks is executing' tag 'Aplicando perfil $ktsr_prof_pt...'" >/dev/null 2>&1; }
notif_pt_end() { su -lp 2000 -c "cmd notification post -S bigtext -t 'King Tweaks is executing' tag 'Perfil $ktsr_prof_pt aplicado'" >/dev/null 2>&1; }
notif_tr_start() { su -lp 2000 -c "cmd notification post -S bigtext -t 'King Tweaks is executing' tag '$ktsr_prof_tr profili uygulanıyor...'" >/dev/null 2>&1; }
notif_tr_end() { su -lp 2000 -c "cmd notification post -S bigtext -t 'King Tweaks is executing' tag '$ktsr_prof_tr profili uygulandı'" >/dev/null 2>&1; }
notif_id_start() { su -lp 2000 -c "cmd notification post -S bigtext -t 'King Tweaks is executing' tag 'Menerapkan profil $ktsr_prof_id...'" >/dev/null 2>&1; }
notif_id_end() { su -lp 2000 -c "cmd notification post -S bigtext -t 'King Tweaks is executing' tag 'Profil $ktsr_prof_id terpakai'" >/dev/null 2>&1; }
notif_fr_start() { su -lp 2000 -c "cmd notification post -S bigtext -t 'King Tweaks is executing' tag 'Chargement du profil $ktsr_prof_tr...'" >/dev/null 2>&1; }
notif_fr_end() { su -lp 2000 -c "cmd notification post -S bigtext -t 'King Tweaks is executing' tag 'Profil $ktsr_prof_fr chargé'" >/dev/null 2>&1; }
notif_custom() { su -lp 2000 -c "cmd notification post -S bigtext -t '$1' tag '$2'" >/dev/null 2>&1; }
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 an error is present
! echo "$2" >"$1" 2>/dev/null && {
log_e "Failed: $1 → $2"
return 1
}
log_d "$1 → $2"
}
write_lock() {
[[ ! -f "$1" ]] && return 1
chmod +w "$1" 2>/dev/null
! echo "$2" >"$1" 2>/dev/null && {
log_e "Failed: $1 → $2"
return 1
}
log_d "$1 → $2"
chmod 0444 "$1"
}
kill_svc() {
stop "$1" 2>/dev/null
killall -9 "$1" 2>/dev/null
killall -9 $(find /system/bin -type f -name "$1") 2>/dev/null
killall -9 $(find /vendor/bin -type f -name "$1") 2>/dev/null
}
[[ "$(command -v busybox)" ]] && {
total_ram=$(busybox free -m | awk '/Mem:/{print $2}')
total_ram_kb=$(grep [0-9] /proc/meminfo | awk '/kB/{print $2}' | head -1)
avail_ram=$(busybox free -m | awk '/Mem:/{print $7}')
} || {
total_ram=$(free -m | awk '/Mem:/{print $2}')
total_ram_kb=$(grep [0-9] /proc/meminfo | awk '/kB/{print $2}' | head -1)
avail_ram=$(free -m | awk '/Mem:/{print $7}')
}
sdk=$(getprop ro.build.version.sdk)
[[ "$sdk" == "" ]] && sdk=$(getprop ro.vendor.build.version.sdk)
[[ "$sdk" == "" ]] && sdk=$(getprop ro.vndk.version)
write_info() { echo "$1" >>"$bbn_info"; }
save_info() {
write_info "[*] Bourbon - the essential task optimizer
Version: 1.6.0-r8-master
Last performed: $(date '+%Y-%m-%d %H:%M:%S')
FSCC status: $(fscc_status)
Adjshield status: $(adjshield_status)
Adjshield config file: $adj_cfg"
}
adjshield_write_cfg() { echo "$1" >>"$adj_cfg"; }
adjshield_create_default_cfg() {
adjshield_write_cfg "# AdjShield Config File
# Prevent given packages from being killed by LMK by protecting oom_score_adj.
# List all the package names of the apps which you want to keep alive.
com.riotgames.league.wildrift
com.activision.callofduty.shooter
com.tencent.ig
com.dts.freefireth
com.dts.freefiremax
com.ngame.allstar.eu
com.pubg.newstate
com.mobile.legends
com.ea.gp.fifamobile
com.gameloft.android.ANMP.GloftA9HM
com.gameloft.android.ANMP.GloftMVHM
com.gameloft.android.ANMP.GloftM5HM
com.netease.idv.googleplay
com.titan.cd.gb
com.ea.gp.apexlegendsmobilefps
com.igg.android.omegalegends
com.netease.lztgglobal
com.gamedevltd.modernstrike
com.gamedevltd.wwh
com.edkongames.mobs
com.panzerdog.tacticool
com.gaijin.xom
com.feralinteractive.gridas
com.twoheadedshark.tco
com.madfingergames.legends
com.gameinsight.gobandroid
com.garena.game.kgid
com.epicgames.fortnite
com.criticalforceentertainment.criticalops
com.bhvr.deadbydaylight
com.axlebolt.standoff2
com.gameloft.android.ANMP.GloftINHM
com.codemasters.F1Mobile
com.netease.sheltergp
com.supercell.brawlstars
com.miniclip.eightballpool
com.mojang.minecraftpe
com.supercell.clashroyale
com.gameloft.android.GloftDMKF
com.gameloft.android.GloftMBCF
com.miHoYo.GenshinImpact
com.garena.game.kgvn
com.pubg.krmobile
com.gameloft.android.GloftMOTR
com.tencent.tmgp.sgame
com.ubisoft.rainbowsixmobile.r6.fps.pvp.shooter
com.tencent.iglite
com.pubg.imobile
com.playtika.wsop.gp
com.gameloft.android.GloftR19F
com.kitkagames.fallbuddies
com.gameloft.android.ANMP.GloftDMHM
com.ea.game.nfs14_row
com.scopely.startrek
net.wargaming.wot.blitz
com.blizzard.wtcg.hearthstone
com.ea.games.r3_row
com.wb.goog.mkx
com.pixonic.wwr
com.wb.goog.got.conquest
com.garena.game.fcsac
com.garena.game.codm
com.pixelfederation.ts2
com.gameloft.android.GloftNOMP
"
}
adjshield_start() {
rm -rf "$adj_log"
rm -rf "$bbn_log"
rm -rf "$bbn_info"
# check interval: 120 seconds - Deprecated, use event driven instead
"${modpath}"system/bin/adjshield -o $adj_log -c $adj_cfg &
}
adjshield_stop() { kill_svc adjshield; }
# return:status
adjshield_status() {
[[ "$(pgrep -f "adjshield")" ]] && echo "Adjshield running. see $adj_log for details." || {
# Error: Log file not found
err="$(cat "$adj_log" | grep Error | head -1 | cut -d: -f2)"
[[ "$err" != "" ]] && echo "Not running. $err." || echo "Not running. Unknown reason."
}
}
# $1:task_name $2:cgroup_name $3:"cpuset"/"stune"
change_task_cgroup() {
for temp_pid in $(echo "$ps_ret" | grep -i -E "$1" | awk '{print $1}'); do
for temp_tid in $(ls "/proc/$temp_pid/task/"); do
comm="$(cat "/proc/$temp_pid/task/$temp_tid/comm")"
echo "$temp_tid" >"/dev/$3/$2/tasks"
done
done
}
# $1:process_name $2:cgroup_name $3:"cpuset"/"stune"
change_proc_cgroup() {
for temp_pid in $(echo "$ps_ret" | grep -i -E "$1" | awk '{print $1}'); do
comm="$(cat "/proc/$temp_pid/comm")"
echo "$temp_pid" >"/dev/$3/$2/cgroup.procs"
done
}
# $1:task_name $2:thread_name $3:cgroup_name $4:"cpuset"/"stune"
change_thread_cgroup() {
for temp_pid in $(echo "$ps_ret" | grep -i -E "$1" | awk '{print $1}'); do
for temp_tid in $(ls "/proc/$temp_pid/task/"); do
comm="$(cat "/proc/$temp_pid/task/$temp_tid/comm")"
[[ "$(echo "$comm" | grep -i -E "$2")" != "" ]] && echo "$temp_tid" >"/dev/$4/$3/tasks"
done
done
}
# $1:task_name $2:cgroup_name $3:"cpuset"/"stune"
change_main_thread_cgroup() {
for temp_pid in $(echo "$ps_ret" | grep -i -E "$1" | awk '{print $1}'); do
comm="$(cat "/proc/$temp_pid/comm")"
echo "$temp_pid" >"/dev/$3/$2/tasks"
done
}
# $1:task_name $2:hex_mask(0x00000003 is CPU0 and CPU1)
change_task_affinity() {
for temp_pid in $(echo "$ps_ret" | grep -i -E "$1" | awk '{print $1}'); do
for temp_tid in $(ls "/proc/$temp_pid/task/"); do
comm="$(cat "/proc/$temp_pid/task/$temp_tid/comm")"
taskset -p "$2" "$temp_tid" >>"$bbn_log"
done
done
}
# $1:task_name $2:thread_name $3:hex_mask(0x00000003 is CPU0 and CPU1)
change_thread_affinity() {
for temp_pid in $(echo "$ps_ret" | grep -i -E "$1" | awk '{print $1}'); do
for temp_tid in $(ls "/proc/$temp_pid/task/"); do
comm="$(cat "/proc/$temp_pid/task/$temp_tid/comm")"
[[ "$(echo "$comm" | grep -i -E "$2")" != "" ]] && taskset -p "$3" "$temp_tid" >>"$bbn_log"
done
done
}
# $1:task_name $2:hex_mask(0x00000003 is CPU0 and CPU1)
change_other_thread_affinity() {
for temp_pid in $(echo "$ps_ret" | grep -i -E "$1" | awk '{print $1}'); do
for temp_tid in $(ls /proc/"$temp_pid"/task/); do
comm="$(cat /proc/"$temp_pid"/task/*/comm)"
[[ ! "$uni_task" == "$comm" ]] && [[ ! "$uni_task2" == "$comm" ]] && [[ ! "$etc_task" == "$comm" ]] && [[ ! "$render_task" == "$comm" ]] && [[ ! "$render_task2" == "$comm" ]] && taskset -p "$2" "$temp_tid" >>"$bbn_log"
done
done
}
# $1:task_name $2:nice(relative to 120)
change_task_nice() {
for temp_pid in $(echo "$ps_ret" | grep -i -E "$1" | awk '{print $1}'); do
for temp_tid in $(ls "/proc/$temp_pid/task/"); do
renice -n +40 -p "$temp_tid"
renice -n -19 -p "$temp_tid"
renice -n "$2" -p "$temp_tid"
done
done
}
# $1:task_name $2:thread_name $3:nice(relative to 120)
change_thread_nice() {
for temp_pid in $(echo "$ps_ret" | grep -i -E "$1" | awk '{print $1}'); do
for temp_tid in $(ls "/proc/$temp_pid/task/"); do
comm="$(cat "/proc/$temp_pid/task/$temp_tid/comm")"
[[ "$(echo "$comm" | grep -i -E "$2")" != "" ]] && {
renice -n +40 -p "$temp_tid"
renice -n -19 -p "$temp_tid"
renice -n "$3" -p "$temp_tid"
}
done
done
}
# $1:task_name $2:priority(99-x, 1<=x<=99) (SCHED_RR)
change_task_rt() {
for temp_pid in $(echo "$ps_ret" | grep -i -E "$1" | awk '{print $1}'); do
for temp_tid in $(ls "/proc/$temp_pid/task/"); do
comm="$(cat "/proc/$temp_pid/task/$temp_tid/comm")"
chrt -p "$temp_tid" "$2" >>"$bbn_log"
done
done
}
# $1:task_name $2:priority(99-x, 1<=x<=99) (SCHED_FIFO)
change_task_rt_ff() {
for temp_pid in $(echo "$ps_ret" | grep -i -E "$1" | awk '{print $1}'); do
for temp_tid in $(ls "/proc/$temp_pid/task/"); do
comm="$(cat "/proc/$temp_pid/task/$temp_tid/comm")"
chrt -f -p "$temp_tid" "$2" >>"$bbn_log"
done
done
}
# $1:task_name $2:thread_name $3:priority(99-x, 1<=x<=99)
change_thread_rt() {
for temp_pid in $(echo "$ps_ret" | grep -i -E "$1" | awk '{print $1}'); do
for temp_tid in $(ls "/proc/$temp_pid/task/"); do
comm="$(cat "/proc/$temp_pid/task/$temp_tid/comm")"
[[ "$(echo "$comm" | grep -i -E "$2")" != "" ]] && chrt -p "$3" "$temp_tid" >>"$bbn_log"
done
done
}
# $1:task_name
change_task_high_prio() { change_task_nice "$1" "-20"; }
# $1:task_name $2:thread_name
change_thread_high_prio() { change_thread_nice "$1" "$2" "-20"; }
unpin_thread() { change_thread_cgroup "$1" "$2" "" "cpuset"; }
# 0-3
pin_thread_on_pwr() {
unpin_thread "$1" "$2"
change_thread_affinity "$1" "$2" "0f"
}
# 0-6
pin_thread_on_mid() {
unpin_thread "$1" "$2"
change_thread_affinity "$1" "$2" "7f"
}
# 4-7
pin_thread_on_perf() {
unpin_thread "$1" "$2"
change_thread_affinity "$1" "$2" "f0"
}
# 0-7
pin_thread_on_all() {
unpin_proc "$1"
change_task_affinity "$1" "ff"
}
# $1:task_name $2:thread_name $3:hex_mask
pin_thread_on_custom() {
unpin_thread "$1" "$2"
change_thread_affinity "$1" "$2" "$3"
}
# $1:task_name $2:hex_mask
pin_other_thread_on_custom() {
unpin_thread "$1" "$2"
change_other_thread_affinity "$1" "$2"
}
# $1:task_name
unpin_proc() { change_task_cgroup "$1" "" "cpuset"; }
# 0-3
pin_proc_on_pwr() {
unpin_proc "$1"
change_task_affinity "$1" "0f"
}
# 0-6
pin_proc_on_mid() {
unpin_proc "$1"
change_task_affinity "$1" "7f"
}
# 4-7
pin_proc_on_perf() {
unpin_proc "$1"
change_task_affinity "$1" "f0"
}
# 0-7
pin_proc_on_all() {
unpin_proc "$1"
change_task_affinity "$1" "ff"
}
# $1:task_name $2:hex_mask
pin_proc_on_custom() {
unpin_proc "$1"
change_task_affinity "$1" "$2"
}
rebuild_ps_cache() { ps_ret="$(ps -Ao pid,args)"; }
# $1:apk_path $return:oat_path
# OPSystemUI/OPSystemUI.apk -> OPSystemUI/oat
fscc_path_apk_to_oat() { echo "${1%/*}/oat"; }
# Only append if object doesn't already exists either on pinner service to avoid unnecessary memory expenses
fscc_add_obj() {
[[ "$sdk" -lt "24" ]] && [[ ! "$fscc_file_list" == *"$1"* ]] && [[ "$fscc_file_list" == "" ]] && fscc_file_list="$1" || [[ "$sdk" -lt "24" ]] && [[ ! "$fscc_file_list" == *"$1"* ]] && [[ ! "fscc_file_list" == "" ]] && fscc_file_list+=" $1" || {
while IFS= read -r obj; do
[[ "$1" != "$obj" ]] && [[ ! "$fscc_file_list" == *"$1"* ]] && [[ "$fscc_file_list" == "" ]] && fscc_file_list="$1" || [[ "$1" != "$obj" ]] && [[ ! "$fscc_file_list" == *"$1"* ]] && [[ ! "$fscc_file_list" == "" ]] && fscc_file_list+=" $1"
done <<<"$(dumpsys pinner | grep "$1" | awk '{print $1}')"
}
}
# $1:package_name
# pm path -> "package:/system/product/priv-app/OPSystemUI/OPSystemUI.apk"
fscc_add_apk() { [[ "$1" != "" ]] && fscc_add_obj "$(pm path "$1" | head -1 | cut -d: -f2)"; }
# $1:package_name
fscc_add_dex() {
[[ "$1" != "" ]] \
&& {
# pm path -> "package:/system/product/priv-app/OPSystemUI/OPSystemUI.apk"
package_apk_path="$(pm path "$1" | head -1 | cut -d: -f2)"
# User app: OPSystemUI/OPSystemUI.apk -> OPSystemUI/oat
fscc_add_obj "${package_apk_path%/*}/oat"
# Remove apk name suffix
apk_nm="${package_apk_path%/*}"
# Remove path prefix
apk_nm="${apk_nm##*/}"
# System app: get dex & vdex
# /data/dalvik-cache/arm64/system@product@priv-app@[email protected]@classes.dex
}
for dex in $(find "$dvk" | grep "@$apk_name@"); do
fscc_add_obj "$dex"
done
}
fscc_add_app_home() {
# Well, not working on Android 7.1
intent_act="android.intent.action.MAIN"
intent_cat="android.intent.category.HOME"
# " packageName=com.microsoft.launcher"
pkg_nm="$(pm resolve-activity -a "$intent_act" -c "$intent_cat" | grep packageName | head -1 | cut -d= -f2)"
# /data/dalvik-cache/arm64/system@priv-app@[email protected]@classes.dex 16M/31M 53.2%
# /data/dalvik-cache/arm64/system@priv-app@[email protected]@classes.vdex 120K/120K 100%
# /system/priv-app/OPLauncher2/OPLauncher2.apk 14M/30M 46.1%
fscc_add_apk "$pkg_nm"
fscc_add_dex "$pkg_nm"
}
fscc_add_app_ime() {
# " packageName=com.baidu.input_yijia"
pkg_nm="$(ime list | grep packageName | head -1 | cut -d= -f2)"
# /data/dalvik-cache/arm/system@app@[email protected]@classes.dex 5M/17M 33.1%
# /data/dalvik-cache/arm/system@app@[email protected]@classes.vdex 2M/7M 28.1%
# /system/app/baidushurufa/baidushurufa.apk 1M/28M 5.71%
# pin apk file in memory is not valuable
fscc_add_dex "$pkg_nm"
}
# $1:package_name
fscc_add_apex_lib() { fscc_add_obj "$(find /apex -name "$1" | head -1)"; }
# After appending fscc_file_list
# Multiple parameters, cannot be warped by ""
fscc_start() { "${modpath}"system/bin/fscache-ctrl -fdlb0 "$fscc_file_list"; }
fscc_stop() { kill_svc fscache-ctrl; }
# Return:status
fscc_status() {
# Get the correct value after waiting for fscc loading files
sleep 2
[[ "$(pgrep -f "fscache-ctrl")" ]] && echo "Running $(cat /proc/meminfo | grep Mlocked | cut -d: -f2 | tr -d ' ') in cache." || echo "Not running."
}