Skip to content

Commit

Permalink
DEV 21.02.14
Browse files Browse the repository at this point in the history
1. 本次更新合并了40+个commit
2. 新增:ssanalysis分析器,监测system_server过渡动画,减少页面切换掉帧和滞后感
3. 新增:重构sfanalysis状态跟踪,即使app/sf的偏移量不同也能准确跟踪,减少卡顿
4. 新增:hint状态机使用数据驱动,未来版本开放配置文件自定义
5. 新增:UxAffinity=2在掉帧发生前动态迁移主线程到超大核,减少掉帧
6. 新增:三星exynos982x定制频率范围knob,修复睡死问题,感谢Saumer7(github)的思路
7. 新增:初始化过程日志存放在/sdcard/Android/log_uperf_initsvc.log,便于诊断
8. 修复:由于loadmonitor渲染检测误报率上升,使得滑动等hint提前退出,导致卡顿
9. 修复:原有perfhal配置文件内容错误使得服务报错,导致软重启
10. 修复:移除高通HMP平台不存在的负载迁移knob
11. 修复:系统界面主线程被错误固定到小核执行超时,导致ANR
12. 修复:开心消消乐主线程被错误固定到小核,导致卡顿
13. 修复:android.bg可能会阻塞binder,此线程被错误固定到小核,导致卡顿
14. 修复:surfaceflinger/app错误固定到小核,MIUI过渡动画中此线程执行超时,导致卡顿
15. 修复:由于不正确的mask设置使得cnss驱动启动网卡执行超时,导致偶见WiFi无法启用
16. 改进:使用uperf限制foreground不上prime,同时排除pinned中例外的桌面,减少耗电
17. 改进:heavyload动作任务放置能力,负载能够立即放置到超大核并按需调频
18. 改进:白名单优先级设置不高于音频类线程,减少音频卡顿
19. 改进:线程分类器使用表驱动,改进代码可读性
20. 改进:默认使用busybox for android ndk或者magisk的busybox
21. 改进:安装脚本机型信息提示,减少误解

Signed-off-by: Matt Yang <yccy@outlook.com>
  • Loading branch information
yc9559 committed Feb 14, 2021
1 parent 4070883 commit ffb36fb
Show file tree
Hide file tree
Showing 17 changed files with 448 additions and 170 deletions.
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@ https://github.com/yc9559/uperf/releases
### Magisk方式

1. 下载后通过Magisk Manager刷入,Magisk版本不低于18.0
2. 重启后查看/sdcard/Android/log_uperf.txt检查uperf是否正常自启动
2. 重启后查看`/sdcard/Android/log_uperf.txt`检查uperf是否正常自启动

### 手动安装

1. 如果你的设备无法安装Magisk,并且已经获取到ROOT权限
2. 下载后手动解压缩,例如解压缩到`/data/uperf`
3. 修改`setup_uperf.sh``run_uperf.sh``initsvc_uperf.sh`权限为755
4. 执行setup_uperf.sh完成安装,检查输出信息是否有报错
5. 执行run_uperf.sh启动uperf,检查输出信息是否有报错
6. 打开/cache/log_injector.log,检查sfanalysis注入是否成功
7. 如果关联自启动到第三方APP,设置在开机完成后执行run_uperf.sh
4. 执行`setup_uperf.sh`完成安装,检查输出信息是否有报错
5. 执行`run_uperf.sh`启动uperf,检查输出信息是否有报错
6. 打开`/data/cache/injector.log`,检查sfanalysis注入是否成功
7. 如果关联自启动到第三方APP,设置在开机完成后执行`run_uperf.sh`
8. 如果关联自启动到系统启动脚本,插入`sh /data/uperf/initsvc_uperf.sh`
9. 重启后查看/sdcard/Android/log_uperf.txt检查uperf是否正常自启动
9. 重启后查看`/sdcard/Android/log_uperf.txt`检查uperf是否正常自启动

### 性能模式切换

Expand Down Expand Up @@ -158,7 +158,7 @@ Sfanalysis是一个独立于Uperf的模块,注入到surfaceflinger进行修改

![检测到渲染延迟立即拉升CPU频率](./media/sflag.png)

渲染提交滞后对应的Hint`SfLag`与重负载一样,有调用频率限制避免长时间拉升高频,相关参数暂时没有开放更改。`SfLag`使用可用次数缓冲池控制调用频率,每满400ms间隔可用次数+1,最大到20次。为了避免不必要的频率拉升,只允许从`Tap``Swipe``AndroidAM`转移到`SfLag`使用监测ActivityManager的活动推测发生解锁屏幕这一方法存在少量漏检,因此在屏幕熄灭时收到渲染滞后通知也作为`AndroidAM`事件处理。SfAnalysis正常工作后在日志以如下方式体现:
渲染提交滞后对应的Hint`SfLag`与重负载一样,有调用频率限制避免长时间拉升高频,相关参数暂时没有开放更改。`SfLag`使用可用次数缓冲池控制调用频率,每满400ms间隔可用次数+1,最大到20次。为了避免不必要的频率拉升,只允许从`Tap``Swipe``AndroidAM`转移到`SfLag`。SfAnalysis正常工作后在日志以如下方式体现:
```
[13:03:36][I] SfAnalysis: Surfaceflinger analysis connected
```
Expand Down Expand Up @@ -540,6 +540,9 @@ UFS节能开关的`sysfs`节点路径为`/sys/devices/platform/soc/1d84000.ufshc
- @瞬光飞翔(coolapk)
- @kuiot(coolapk)
- @常凯申将军(coolapk)
- emptybot08(github)
- ahzhi(github)
- Saumer7(github)

## 捐赠

Expand Down
7 changes: 5 additions & 2 deletions magisk/common/post-fs-data.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ vendor/etc/powerscntbl.cfg
vendor/etc/powerscntbl.xml
vendor/etc/perf/perfboostsconfig.json
vendor/etc/perf/targetresourceconfigs.json
vendor/etc/perf/commonresourceconfigs.json
"
for f in $perfcfgs; do
[ ! -f "/$f" ] && rm "$MODDIR/system/$f"
done

# pin kworker on little
# drivers/net/wireless/cnss2/main.c in kworker/u16:1 Tainted
# because cnss: fatal: MHI power up returns timeout, which QMI timeout is 10000 ms
# kworker do not run on prime
for f in $(find /sys/devices/virtual/workqueue "cpumask"); do
echo 0f > $f
echo 7f > $f
done
6 changes: 0 additions & 6 deletions magisk/common/service.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,4 @@
BASEDIR="$(dirname $(readlink -f "$0"))"
SCRIPT_DIR="$BASEDIR/script"

if [ "$(cat $SCRIPT_DIR/pathinfo.sh | grep "$PATH")" == "" ]; then
echo "" >> $SCRIPT_DIR/pathinfo.sh
echo "# prefer to use busybox provided by magisk" >> $SCRIPT_DIR/pathinfo.sh
echo "PATH=$PATH" >> $SCRIPT_DIR/pathinfo.sh
fi

sh $BASEDIR/initsvc_uperf.sh
7 changes: 0 additions & 7 deletions magisk/common/system.prop
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,3 @@

# unify surfaceflinger props
debug.sf.latch_unsignaled=1
# used by the Scheduler to trigger inactivity callbacks that will switch the display to a lower refresh rate
ro.surface_flinger.set_idle_timer_ms=1000

# HeapTaskDaemon is controlled by uperf, reserve more room for direct heap reclaim
# default = 512k/8m
dalvik.vm.heapminfree=4m
dalvik.vm.heapmaxfree=12m
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion magisk/module.prop
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
id=uperf
name=Uperf
version=v2 (21.01.31)
version=v2 (21.02.14)
versionCode=2
author=Matt Yang
description=Userspace performance controller for android. Repo: https://github.com/yc9559/uperf/
Expand Down
2 changes: 1 addition & 1 deletion magisk/run_uperf.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ chmod 755 /data/powercfg-base.sh
# powercfg path provided by magisk module
echo "sh $SCRIPT_DIR/powercfg_main.sh \"\$1\"" >> /data/powercfg.sh

sh $SCRIPT_DIR/start_sfanalysis.sh
sh $SCRIPT_DIR/start_injector.sh
sh $SCRIPT_DIR/powercfg_main.sh
sh $SCRIPT_DIR/powercfg_once.sh
24 changes: 13 additions & 11 deletions magisk/script/libcgroup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Cgroup Library
# https://github.com/yc9559/
# Author: Matt Yang
# Version: 202101230
# Version: 20210204

BASEDIR="$(dirname "$0")"
. $BASEDIR/pathinfo.sh
Expand All @@ -12,13 +12,13 @@ BASEDIR="$(dirname "$0")"
# reserve one LITTLE for hwservicemanager which will block composer
# thread priority settings not working on hwservicemanager
# okay, disabling perf-hal resulting hwservicemanager frequently wakeup
CPUMASK_ULV="03"
CPUMASK_LOW="0f"
CPUMASK_MID="63"
CPUMASK_ULV="08"
CPUMASK_LOW="0e"
CPUMASK_MID="73"
CPUMASK_HIGH="f0"
CPUID_ULV="0-1"
CPUID_LOW="0-3"
CPUID_MID="0-1,5-6"
CPUID_ULV="3"
CPUID_LOW="1-3"
CPUID_MID="0-1,4-7" # uperf set 0-1/0-6 for foreground, cpu7 reserved for launcher
CPUID_HIGH="4-7"

# avoid matching grep itself
Expand Down Expand Up @@ -149,9 +149,10 @@ change_task_high_prio()
ps_ret="$(ps -Ao pid,args)"
for temp_pid in $(echo "$ps_ret" | grep -i "$1" | awk '{print $1}'); do
for temp_tid in $(ls "/proc/$temp_pid/task/"); do
# audio thread nice <= -16
comm="$(cat /proc/$temp_pid/task/$temp_tid/comm)"
log "change $1/$comm($temp_tid) -> Nice -20"
renice -n -20 -p "$temp_tid" >> $LOG_FILE
log "change $1/$comm($temp_tid) -> Nice -15"
renice -n -15 -p "$temp_tid" >> $LOG_FILE
done
done
}
Expand All @@ -166,8 +167,9 @@ change_thread_high_prio()
for temp_tid in $(ls "/proc/$temp_pid/task/"); do
comm="$(cat /proc/$temp_pid/task/$temp_tid/comm)"
if [ "$(echo $comm | grep -i "$2")" != "" ]; then
log "change $1/$comm($temp_tid) -> Nice -20"
renice -n -20 -p "$temp_tid" >> $LOG_FILE
# audio thread nice <= -16
log "change $1/$comm($temp_tid) -> Nice -15"
renice -n -15 -p "$temp_tid" >> $LOG_FILE
fi
done
done
Expand Down
13 changes: 8 additions & 5 deletions magisk/script/pathinfo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
# https://github.com/yc9559/
# Author: Matt Yang

SCRIPT_DIR="/script"
BIN_DIR="/bin"
SCRIPT_DIR="script"
BIN_DIR="bin"
MODULE_PATH="$(dirname $(readlink -f "$0"))"
MODULE_PATH="${MODULE_PATH%$SCRIPT_DIR}"
MODULE_PATH="${MODULE_PATH%\/$SCRIPT_DIR}"
PANEL_FILE="/sdcard/Android/panel_uperf.txt"
LOG_FILE="$MODULE_PATH/log.txt"
PATH="/sbin/.magisk/busybox:/sbin:/system/sbin:/product/bin:/apex/com.android.runtime/bin:/system/bin:/system/xbin:/odm/bin:/vendor/bin:/vendor/xbin"
LOG_FILE="/sdcard/Android/log_uperf_initsvc.log"

# prefer to use magisk's busybox and busybox for android ndk
PATH="/sbin:/system/sbin:/system/xbin:/system/bin:/vendor/xbin:/vendor/bin"
PATH="/sbin/.magisk/busybox:/sbin/.core/busybox:/magisk/.core/busybox:$PATH"
2 changes: 1 addition & 1 deletion magisk/script/powercfg_main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ save_panel()
write_panel ""
write_panel "Uperf https://github.com/yc9559/uperf/"
write_panel "Author: Matt Yang"
write_panel "Version: v2 (21.01.31)"
write_panel "Version: v2 (21.02.14)"
write_panel "Last performed: $(date '+%Y-%m-%d %H:%M:%S')"
write_panel ""
write_panel "[Uperf status]"
Expand Down
72 changes: 35 additions & 37 deletions magisk/script/powercfg_once.sh
Original file line number Diff line number Diff line change
Expand Up @@ -69,29 +69,28 @@ unify_cgroup()
# input dispatcher
change_thread_high_prio "system_server" "input"
# transition animation
pin_thread_on_perf "system_server" "android\.anim"
pin_thread_on_perf "system_server" "android\.ui"
pin_thread_on_perf "system_server" "android\.display"
change_thread_rt "system_server" "android\.anim" "1"
change_thread_rt "system_server" "android\.ui" "1"
change_thread_rt "system_server" "android\.display" "1"
# not important
pin_thread_on_pwr "system_server" "Miui"
pin_thread_on_pwr "system_server" "Connect"
pin_thread_on_pwr "system_server" "Network"
pin_thread_on_ulv "system_server" "Async"
pin_thread_on_ulv "system_server" "backup"
pin_thread_on_ulv "system_server" "Greezer"
pin_thread_on_ulv "system_server" "TaskSnapshot"
pin_thread_on_ulv "system_server" "Oom"
pin_thread_on_ulv "system_server" "Sync"
pin_thread_on_ulv "system_server" "\.bg"
pin_thread_on_ulv "system_server" "Miui"
pin_thread_on_ulv "system_server" "Observer"
pin_thread_on_ulv "system_server" "Connect"
pin_thread_on_ulv "system_server" "Network"
pin_thread_on_ulv "system_server" "Power"
pin_thread_on_ulv "system_server" "Sensor"
pin_thread_on_ulv "system_server" "Wifi"
pin_thread_on_ulv "system_server" "Thread-"
pin_thread_on_ulv "system_server" "pool-"
pin_thread_on_ulv "system_server" "Jit thread pool"
# pin_thread_on_ulv "system_server" "\.bg" # it blocks binders
# do not let GC thread block system_server
# pin_thread_on_mid "system_server" "HeapTaskDaemon"
# pin_thread_on_mid "system_server" "FinalizerDaemon"
Expand All @@ -112,12 +111,16 @@ unify_cgroup()
# fix laggy bilibili feed scrolling
change_task_cgroup "android\.phone" "foreground" "cpuset"
change_thread_cgroup "android\.phone" "Binder" "top-app" "cpuset"
# let UX related Binders run with top-app
# sometimes surfaceflinger has quite high load
change_thread_cgroup "surfaceflinger" "surfaceflinger" "top-app" "cpuset"
change_thread_cgroup "surfaceflinger" "Binder" "top-app" "cpuset"
change_thread_cgroup "\.hardware\.display" "Binder" "top-app" "cpuset"
change_thread_cgroup "\.composer" "Binder" "top-app" "cpuset"
change_thread_cgroup "system_server" "Binder" "top-app" "cpuset"
pin_thread_on_mid "surfaceflinger" "app"
# let UX related Binders run with top-app
change_thread_cgroup "surfaceflinger" "^Binder" "top-app" "cpuset"
change_thread_cgroup "system_server" "^Binder" "top-app" "cpuset"
change_thread_cgroup "\.hardware\.display" "^Binder" "top-app" "cpuset"
change_thread_cgroup "\.composer" "^Binder" "top-app" "cpuset"
# transition animation
change_thread_cgroup "system_server" "android\.anim" "top-app" "cpuset"

# Heavy Scene Boost
# camera service
Expand All @@ -142,12 +145,9 @@ unify_cpufreq()
# stop sched core_ctl, game's main thread need be pinned on prime core
set_corectl_param "enable" "0:0 2:0 4:0 6:0 7:0"

# unify governor
if [ "$(is_eas)" == "true" ]; then
set_governor_param "scaling_governor" "0:schedutil 2:schedutil 4:schedutil 6:schedutil 7:schedutil"
else
set_governor_param "scaling_governor" "0:interactive 2:interactive 4:interactive 6:interactive 7:interactive"
fi
# unify governor, use schedutil if kernel has it
set_governor_param "scaling_governor" "0:interactive 2:interactive 4:interactive 6:interactive 7:interactive"
set_governor_param "scaling_governor" "0:schedutil 2:schedutil 4:schedutil 6:schedutil 7:schedutil"

# unify walt schedutil governor
set_governor_param "schedutil/hispeed_freq" "0:0 2:0 4:0 6:0 7:0"
Expand Down Expand Up @@ -207,8 +207,8 @@ unify_sched()
# place a little heavier processes on big cluster, due to Cortex-A55 poor efficiency
# The same Binder, A55@1.0g took 7.3ms,A76@1.0g took 3.0ms, in this case, A76's efficiency is 2.4x of A55's.
# However in EAS model A76's efficiency is 1.7x of A55's, so the migrate thresholds need compensate.
set_sched_migrate "80" "20" "999" "888"
set_sched_migrate "80 80" "20 40" "999" "888"
set_sched_migrate "50" "25" "999" "888"
set_sched_migrate "50 80" "25 40" "999" "888"

# prefer to use prev cpu, decrease jitter from 0.5ms to 0.3ms with lpm settings
# system_server binders maybe pinned on perf cluster due to this
Expand Down Expand Up @@ -270,27 +270,18 @@ disable_kernel_boost()
# [10] PPM_POLICY_HICA: ?
# Usage: echo <policy_idx> <1(enable)/0(disable)> > /proc/ppm/policy_status
lock_val "1" /proc/ppm/enabled
lock_val "0 0" /proc/ppm/policy_status
lock_val "1 0" /proc/ppm/policy_status
lock_val "2 0" /proc/ppm/policy_status
lock_val "3 0" /proc/ppm/policy_status
lock_val "4 0" /proc/ppm/policy_status
lock_val "5 0" /proc/ppm/policy_status
lock_val "6 1" /proc/ppm/policy_status # used by uperf
lock_val "7 0" /proc/ppm/policy_status
lock_val "8 0" /proc/ppm/policy_status
lock_val "9 0" /proc/ppm/policy_status
lock_val "10 0" /proc/ppm/policy_status
# used by uperf
mutate "6 1" /proc/ppm/policy_status

# Samsung
mutate "0" /sys/class/input_booster/level
mutate "0" /sys/class/input_booster/head
mutate "0" /sys/class/input_booster/tail

# Samsung EPIC interfaces
mutate "0" /dev/cluster0_freq_min
mutate "0" /dev/cluster1_freq_min
mutate "0" /dev/cluster2_freq_min
# Samsung EPIC interfaces, used by uperf
# mutate "0" /dev/cluster0_freq_min
# mutate "0" /dev/cluster1_freq_min
# mutate "0" /dev/cluster2_freq_min
# lock_val "0" /dev/bus_throughput
# lock_val "0" /dev/gpu_freq_min
# Samsung /kernel/sched/ems/...
Expand Down Expand Up @@ -347,9 +338,7 @@ disable_userspace_boost()
# Qualcomm perfd
stop perfd 2> /dev/null
# Qualcomm perfhal
# running with empty config file
perfhal_stop
perfhal_start
# brain service maybe not smart
stop oneplus_brain_service 2> /dev/null
# disable service below will BOOM
Expand All @@ -360,14 +349,23 @@ disable_userspace_boost()
# stop vendor.power-hal-1-3
}

clear_log
enable_userspace_boost()
{
# Qualcomm perfhal
# running with empty config file
perfhal_start
}

log "PATH=$PATH"
log "sh=$(which sh)"
disable_userspace_boost
disable_kernel_boost
disable_hotplug
unify_cpufreq
unify_gpufreq
unify_sched
unify_lpm
enable_userspace_boost

# make sure that all the related cpu is online
unify_cgroup
Expand Down
63 changes: 63 additions & 0 deletions magisk/script/start_injector.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/system/bin/sh
# Injector Library
# https://github.com/yc9559/
# Author: Matt Yang
# Version: 20210212

BASEDIR="$(dirname "$0")"
. $BASEDIR/pathinfo.sh
. $BASEDIR/libcommon.sh

###############################
# PATHs
###############################

INJ_REL="$BIN_DIR"
INJ_NAME="injector"

###############################
# Injector tool functions
###############################

# $1:process $2:dynamiclib $3:alog_tag
inj_do_inject()
{
log "[begin] injecting $2 to $1"

"$MODULE_PATH/$INJ_REL/$INJ_NAME" "$1" "$2" >> "$LOG_FILE"
if [ "$?" != "0" ]; then
setenforce 0
log "Set SELinux to permissive, retry..."
"$MODULE_PATH/$INJ_REL/$INJ_NAME" "$1" "$2" >> "$LOG_FILE"
fi

sleep 1
logcat -d | grep -i "$3" >> "$LOG_FILE"

log "[end] injecting $2 to $1"
}

inj_start()
{
# raise inotify limit
lock_val "131072" /proc/sys/fs/inotify/max_queued_events
lock_val "131072" /proc/sys/fs/inotify/max_user_watches
lock_val "1024" /proc/sys/fs/inotify/max_user_instances

local lib_path
if [ "$(is_aarch64)" == "true" ]; then
lib_path="/system/lib64"
else
lib_path="/system/lib"
fi

# fallback to standlone mode
[ ! -e "$lib_path" ] && lib_path="$MODULE_PATH/$lib_path"

log "$(date '+%Y-%m-%d %H:%M:%S')"
inj_do_inject "/system/bin/surfaceflinger" "$lib_path/libsfanalysis.so" "SfAnalysis"
inj_do_inject "system_server" "$lib_path/libssanalysis.so" "SsAnalysis"
}

clear_log
[ -f "$MODULE_PATH/enable_injector" ] && inj_start
Loading

0 comments on commit ffb36fb

Please sign in to comment.