Tencent / matrix

Matrix is a plugin style, non-invasive APM system developed by WeChat.
Other
11.65k stars 1.89k forks source link

BatteryCanary 集成后有很多 ANR #818

Closed twiceyuan closed 1 year ago

twiceyuan commented 1 year ago

版本:2.0.8 初步推测和机型没有关系,在内部很多测试机上都遇到了同类问题

Bugreport 抓到的 ANR 日志:

"main" prio=5 tid=1 Runnable
  | group="main" sCount=0 ucsCount=0 flags=0 obj=0x71a0c6a8 self=0x77b4207010
  | sysTid=17431 nice=-10 cgrp=default sched=0/0 handle=0x78ef59b4f8
  | state=R schedstat=( 164103502523 5784834805 70953 ) utm=14903 stm=1507 core=5 HZ=100
  | stack=0x7ff63f5000-0x7ff63f7000 stackSize=8188KB
  | held mutexes= "mutator lock"(shared held)
  native: #00 pc 000000000055f850  /apex/com.android.art/lib64/libart.so (art::DumpNativeStack(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, int, BacktraceMap*, char const*, art::ArtMethod*, void*, bool)+140)
  native: #01 pc 0000000000676270  /apex/com.android.art/lib64/libart.so (art::Thread::DumpStack(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, bool, BacktraceMap*, bool) const+360)
  native: #02 pc 0000000000693f4c  /apex/com.android.art/lib64/libart.so (art::DumpCheckpoint::Run(art::Thread*)+920)
  native: #03 pc 00000000006771d4  /apex/com.android.art/lib64/libart.so (art::Thread::RunCheckpointFunction()+176)
  native: #04 pc 0000000000739848  /apex/com.android.art/lib64/libart.so (art::JniMethodFastEnd(unsigned int, art::Thread*)+112)
  at java.lang.System.arraycopy(Native method)
  at java.util.ArrayList.add(ArrayList.java:486)
  at com.tencent.matrix.batterycanary.monitor.feature.AbsTaskMonitorFeature.onStatTask(AbsTaskMonitorFeature.java:300)
  - locked <0x0d1443d3> (a android.util.SparseArray)
  at com.tencent.matrix.batterycanary.monitor.feature.LooperTaskMonitorFeature.onTaskFinished(LooperTaskMonitorFeature.java:294)
  at com.tencent.matrix.batterycanary.monitor.feature.LooperTaskMonitorFeature$1.onDispatchEnd(LooperTaskMonitorFeature.java:87)
  at com.tencent.matrix.trace.core.LooperMonitor.dispatch(LooperMonitor.java:348)
  - locked <0x0745183c> (a java.util.HashSet)
  at com.tencent.matrix.trace.core.LooperMonitor.access$000(LooperMonitor.java:42)
  at com.tencent.matrix.trace.core.LooperMonitor$LooperPrinter.println(LooperMonitor.java:267)
  at android.os.Looper.loopOnce(Looper.java:236)
  at android.os.Looper.loop(Looper.java:288)
  at android.app.ActivityThread.main(ActivityThread.java:7870)
  at java.lang.reflect.Method.invoke(Native method)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
kaedea commented 1 year ago

https://github.com/Tencent/matrix/blob/0ee872351b29e78d3b0b192959735a8234c35fa7/samples/sample-android/app/src/main/java/sample/tencent/matrix/battery/BatteryCanaryInitHelper.java#L120-L130

首先,不推荐监控 UI 线程的 Looper 开销,这是个实验性功能,要关闭的话可以把 .enable(LooperTaskMonitorFeature.class) 去掉,或者 Looper 监控清单 .addLooperWatchList("xxx") 里不要添加 main(main 表示开启对 UI 线程的 Tracing,如果有其他 Looper 线程监控的话,可以在这里添加响应的线程名)。

如果一定要开启对 UI 线程的 Looper 开销监控,建议设置 .useThreadClock(true),这样能大幅度降低 Tracing UI 线程每个 Message 带来的负担。

twiceyuan commented 1 year ago

@kaedea 谢谢解答,我试下