负载过高通常分为系统负载过高,应用负载过高。在第一节ANR-分类以及分析流程中的分析流程中有负载的判定条件和流程,这里不在贴了,可以直接去对应章节查看。

我们先看一下系统负载过高的实例。

首先还是先看trace信息:

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
----- pid 2135 at 2024-04-07 03:13:49.397317152+0800 -----
Cmd line: com.android.launcher
Build fingerprint: 'xxx/release-keys'
ABI: 'arm64'
Build type: optimized
suspend all histogram: Sum: 1.164s 99% C.I. 20.542us-4088.026us Avg: 122.869us Max: 225691us
DALVIK THREADS (67):
"main" prio=5 tid=1 Native
| group="main" sCount=1 ucsCount=0 flags=1 obj=0x7200f068 self=0xb400007ba963bc00
| sysTid=2135 nice=-10 cgrp=top-app sched=1073741824/0 handle=0x7c5576b4f8
| state=S schedstat=( 10985995408825 3939822638104 29985904 ) utm=767617 stm=330982 core=7 HZ=100
| stack=0x7fc315e000-0x7fc3160000 stackSize=8188KB
| held mutexes=
native: #00 pc 0009013c /apex/com.android.runtime/lib64/bionic/libc.so (syscall+28) (BuildId: 48e8d7222e541f85801757f11a93754f)
native: #01 pc 0022cfac /apex/com.android.art/lib64/libart.so (art::ConditionVariable::WaitHoldingLocks+140) (BuildId: 3cd43dfd934282c944e52afdac5120fb)
native: #02 pc 00534d48 /apex/com.android.art/lib64/libart.so (art::JNI<false>::CallObjectMethodV+1244) (BuildId: 3cd43dfd934282c944e52afdac5120fb)
native: #03 pc 000dca68 /system/lib64/libandroid_runtime.so (_JNIEnv::CallObjectMethod+120) (BuildId: b0915cca4d49f869577619db688a1196)
native: #04 pc 001489d0 /system/lib64/libandroid_runtime.so (android::NativeDisplayEventReceiver::dispatchVsync+64) (BuildId: b0915cca4d49f869577619db688a1196)
native: #05 pc 000c0914 /system/lib64/libgui.so (android::DisplayEventDispatcher::handleEvent+276) (BuildId: 4547fea431706db2336581c24514c3c8)
native: #06 pc 00018e8c /system/lib64/libutils.so (android::Looper::pollInner+1276) (BuildId: c0e35c024901f53044e8affc60a156b8)
native: #07 pc 0001892c /system/lib64/libutils.so (android::Looper::pollOnce+124) (BuildId: c0e35c024901f53044e8affc60a156b8)
native: #08 pc 0018567c /system/lib64/libandroid_runtime.so (android::android_os_MessageQueue_nativePollOnce+44) (BuildId: b0915cca4d49f869577619db688a1196)
at android.os.MessageQueue.nativePollOnce(Native method)
at android.os.MessageQueue.next(MessageQueue.java:335)
at android.os.Looper.loopOnce(Looper.java:168)
at android.os.Looper.loop(Looper.java:346)
at android.app.ActivityThread.main(ActivityThread.java:8344)
at java.lang.reflect.Method.invoke(Native method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:594)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1050)

从上面信息看,主线程为Native状态,对应堆栈为nativePollOnce,说明主线程阻塞。

再看Event log

1
04-07 03:13:49.417  1444  8816 I am_anr  : [0,2135,com.android.launcher,751550021,Input dispatching timed out (Application does not have a focused window)]

指出了ANR类型为Input dispatching timed out (Application does not have a focused window)

接着看AnrManager信息:

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
行   4808: 04-07 03:13:49.421  1444  8816 I AnrManager: startAnrDump
行 4809: 04-07 03:13:49.424 1444 8816 I AnrManager: isANRFlowSkipped() AnrFlow = 0
行 4810: 04-07 03:13:49.424 1444 8816 I AnrManager: enableTraceLog: false
行 4912: 04-07 03:13:49.754 1444 8816 I AnrManager: dumpAnrDebugInfo begin: AnrDumpRecord{ Input dispatching timed out (Application does not have a focused window) ProcessRecord{8d2c936 2135:com.android.launcher/u0a80} IsCompleted:false IsCancelled:false }, onlyDumpSelf = false, isSilentANR = false
行 4913: 04-07 03:13:49.755 1444 8816 I AnrManager: dumpAnrDebugInfoLocked: AnrDumpRecord{ Input dispatching timed out (Application does not have a focused window) ProcessRecord{8d2c936 2135:com.android.launcher/u0a80} IsCompleted:false IsCancelled:false }, onlyDumpSelf = false, isSilentANR = false
行 4914: 04-07 03:13:49.763 1444 8832 I AnrManager: /dev/binderfs/binder_logs/timeout_log isn't exist
行 4924: 04-07 03:13:49.780 1444 8816 I AnrManager: dumpStackTraces begin!
行 7333: 04-07 03:13:57.798 1444 8816 I AnrManager: dumpStackTraces end!
行 7351: 04-07 03:13:57.910 1444 8816 I AnrManager: ANR in com.android.launcher (com.android.launcher/.uioverrides.QuickstepLauncher), time=407812568
行 7352: 04-07 03:13:57.910 1444 8816 I AnrManager: Reason: Input dispatching timed out (Application does not have a focused window)
行 7353: 04-07 03:13:57.910 1444 8816 I AnrManager: Load: 56.48 / 30.74 / 22.68
行 7354: 04-07 03:13:57.910 1444 8816 I AnrManager: ----- Output from /proc/pressure/memory -----
行 7355: 04-07 03:13:57.910 1444 8816 I AnrManager: some avg10=82.71 avg60=58.68 avg300=20.55 total=27859841941
行 7356: 04-07 03:13:57.910 1444 8816 I AnrManager: full avg10=51.17 avg60=34.93 avg300=12.29 total=18781648073
行 7357: 04-07 03:13:57.910 1444 8816 I AnrManager: ----- End output from /proc/pressure/memory -----
行 7358: 04-07 03:13:57.910 1444 8816 I AnrManager: ----- Output from /proc/pressure/cpu -----
行 7359: 04-07 03:13:57.910 1444 8816 I AnrManager: some avg10=23.92 avg60=30.50 avg300=17.91 total=46206700180
行 7360: 04-07 03:13:57.910 1444 8816 I AnrManager: full avg10=0.00 avg60=0.00 avg300=0.00 total=0
行 7361: 04-07 03:13:57.910 1444 8816 I AnrManager: ----- End output from /proc/pressure/cpu -----
行 7362: 04-07 03:13:57.910 1444 8816 I AnrManager: ----- Output from /proc/pressure/io -----
行 7363: 04-07 03:13:57.910 1444 8816 I AnrManager: some avg10=85.37 avg60=63.13 avg300=23.13 total=28918147428
行 7364: 04-07 03:13:57.910 1444 8816 I AnrManager: full avg10=38.46 avg60=20.76 avg300=7.13 total=10431119093
行 7365: 04-07 03:13:57.910 1444 8816 I AnrManager: ----- End output from /proc/pressure/io -----
行 7366: 04-07 03:13:57.910 1444 8816 I AnrManager:
行 7367: 04-07 03:13:57.910 1444 8816 I AnrManager: Android time :[2024-04-07 03:13:57.79] [407820.969]
行 7368: 04-07 03:13:57.910 1444 8816 I AnrManager: CPU usage from 9451ms to -1ms ago (2024-04-07 03:13:39.965 to 2024-04-07 03:13:49.418):
行 7369: 04-07 03:13:57.910 1444 8816 I AnrManager: 80% 84/kswapd0: 0% user + 80% kernel
行 7370: 04-07 03:13:57.910 1444 8816 I AnrManager: 55% 1444/system_server: 26% user + 29% kernel / faults: 14342 minor 18504 major
行 7371: 04-07 03:13:57.910 1444 8816 I AnrManager: 21% 3976/com.ss.android.ugc.aweme: 3.8% user + 17% kernel / faults: 3570 minor 9649 major
行 7372: 04-07 03:13:57.910 1444 8816 I AnrManager: 20% 389/logd: 12% user + 8.7% kernel / faults: 65855 minor 8977 major
行 7373: 04-07 03:13:57.910 1444 8816 I AnrManager: 19% 7155/com.google.android.youtube: 7.2% user + 11% kernel / faults: 7929 minor 10255 major
行 7374: 04-07 03:13:57.910 1444 8816 I AnrManager: 17% 2135/com.android.launcher: 9% user + 8.4% kernel / faults: 4759 minor 9068 major
行 7375: 04-07 03:13:57.910 1444 8816 I AnrManager: 17% 32020/com.google.android.gms.persistent: 6.8% user + 10% kernel / faults: 11799 minor 14280 major
行 7376: 04-07 03:13:57.910 1444 8816 I AnrManager: 15% 8414/com.google.android.inputmethod.latin: 8.6% user + 6.9% kernel / faults: 16812 minor 6831 major
行 7377: 04-07 03:13:57.910 1444 8816 I AnrManager: 15% 829/surfaceflinger: 10% user + 5% kernel / faults: 1740 minor 1164 major
行 7378: 04-07 03:13:57.910 1444 8816 I AnrManager: 8.6% 8211/com.google.android.dialer: 3% user + 5.6% kernel / faults: 6111 minor 5496 major
行 7379: 04-07 03:13:57.910 1444 8816 I AnrManager: 8.1% 789/android.hardware.memtrack-service.mediatek: 0.7% user + 7.3% kernel / faults: 578 minor 59 major
行 7380: 04-07 03:13:57.910 1444 8816 I AnrManager: 7.8% 1003/adbd: 2.3% user + 5.4% kernel / faults: 677 minor 114 major
行 7381: 04-07 03:13:57.910 1444 8816 I AnrManager: 6.7% 1839/com.android.systemui: 1.6% user + 5% kernel / faults: 6294 minor 12817 major
行 7382: 04-07 03:13:57.910 1444 8816 I AnrManager: 5.8% 768/android.hardware.graphics.composer@3.1-service: 2.7% user + 3% kernel / faults: 26 minor 84 major
行 7383: 04-07 03:13:57.910 1444 8816 I AnrManager: 5.8% 8424/com.google.android.contacts: 2.1% user + 3.6% kernel / faults: 3656 minor 5021 major
行 7384: 04-07 03:13:57.910 1444 8816 I AnrManager: 5.8% 31874/com.google.android.gms: 0.6% user + 5.1% kernel / faults: 1732 minor 2833 major
行 7385: 04-07 03:13:57.910 1444 8816 I AnrManager: 4.9% 8519/com.google.android.apps.youtube.music: 2.2% user + 2.7% kernel / faults: 7228 minor 2302 major
行 7386: 04-07 03:13:57.910 1444 8816 I AnrManager: 4.3% 391/lmkd: 0.5% user + 3.8% kernel
行 7387: 04-07 03:13:57.910 1444 8816 I AnrManager: 4.2% 2022/com.android.phone: 2.1% user + 2.1% kernel / faults: 2381 minor 2034 major
行 7388: 04-07 03:13:57.910 1444 8816 I AnrManager: 3.4% 8464/com.google.android.apps.tachyon: 1.6% user + 1.7% kernel / faults: 4656 minor 1930 major
行 7389: 04-07 03:13:57.910 1444 8816 I AnrManager: 3.4% 8512/com.ttvs.persistent: 1.1% user + 2.3% kernel / faults: 4144 minor 3188 major
行 7390: 04-07 03:13:57.910 1444 8816 I AnrManager: 2.7% 7716/com.google.android.googlequicksearchbox:search: 0.1% user + 2.6% kernel / faults: 895 minor 1680 major
...
行 7516: 04-07 03:13:57.915 1444 8816 I AnrManager: 99% TOTAL: 14% user + 36% kernel + 43% iowait + 3% irq + 1.8% softirq
行 7517: 04-07 03:13:57.915 1444 8816 I AnrManager: CPU usage from 1343ms to 2037ms later (2024-04-07 03:13:50.759 to 2024-04-07 03:13:51.454):
行 7518: 04-07 03:13:57.915 1444 8816 I AnrManager: 88% 84/kswapd0: 0% user + 88% kernel
行 7519: 04-07 03:13:57.915 1444 8816 I AnrManager: 94% 1444/system_server: 36% user + 57% kernel / faults: 1383 minor 712 major
行 7520: 04-07 03:13:57.915 1444 8816 I AnrManager: 50% 8672/AnrAuxiliaryTas: 13% user + 36% kernel
行 7521: 04-07 03:13:57.915 1444 8816 I AnrManager: 32% 1451/Signal Catcher: 19% user + 13% kernel
行 7522: 04-07 03:13:57.915 1444 8816 I AnrManager: 1.9% 1444/system_server: 1.9% user + 0% kernel
行 7523: 04-07 03:13:57.915 1444 8816 I AnrManager: 1.9% 1586/LowStorageServi: 0% user + 1.9% kernel
行 7524: 04-07 03:13:57.915 1444 8816 I AnrManager: 1.9% 1592/AccountManagerS: 0% user + 1.9% kernel
行 7525: 04-07 03:13:57.915 1444 8816 I AnrManager: 1.9% 1594/appboot-policy: 0% user + 1.9% kernel
行 7526: 04-07 03:13:57.915 1444 8816 I AnrManager: 1.9% 1688/hidl_ssvc_poll: 1.9% user + 0% kernel
行 7527: 04-07 03:13:57.915 1444 8816 I AnrManager: 1.9% 1767/binder:1444_7: 1.9% user + 0% kernel
行 7528: 04-07 03:13:57.915 1444 8816 I AnrManager: 28% 389/logd: 24% user + 4.5% kernel / faults: 4500 minor 283 major
行 7529: 04-07 03:13:57.915 1444 8816 I AnrManager: 25% 446/logd.writer: 21% user + 4.5% kernel
行 7530: 04-07 03:13:57.915 1444 8816 I AnrManager: 39% 7716/com.google.android.googlequicksearchbox:search: 14% user + 24% kernel / faults: 982 minor 1175 major
行 7531: 04-07 03:13:57.915 1444 8816 I AnrManager: 34% 7716/earchbox:search: 12% user + 22% kernel
行 7532: 04-07 03:13:57.915 1444 8816 I AnrManager: 23% 829/surfaceflinger: 17% user + 5.3% kernel / faults: 153 minor 68 major
行 7533: 04-07 03:13:57.915 1444 8816 I AnrManager: 14% 829/surfaceflinger: 12% user + 1.7% kernel
行 7534: 04-07 03:13:57.915 1444 8816 I AnrManager: 3.5% 1282/surfaceflinger: 1.7% user + 1.7% kernel
行 7535: 04-07 03:13:57.915 1444 8816 I AnrManager: 1.7% 912/binder:829_2: 1.7% user + 0% kernel
行 7536: 04-07 03:13:57.915 1444 8816 I AnrManager: 1.7% 1273/app: 0% user + 1.7% kernel
行 7537: 04-07 03:13:57.915 1444 8816 I AnrManager: 1.7% 1274/appSf: 1.7% user + 0% kernel
行 7538: 04-07 03:13:57.915 1444 8816 I AnrManager: 26% 2135/com.android.launcher: 14% user + 12% kernel / faults: 28 minor 95 major
行 7539: 04-07 03:13:57.915 1444 8816 I AnrManager: 12% 2781/RenderThread: 10% user + 2% kernel
行 7540: 04-07 03:13:57.915 1444 8816 I AnrManager: 8.2% 2840/mali-cmar-backe: 0% user + 8.2% kernel
行 7541: 04-07 03:13:57.915 1444 8816 I AnrManager: 2% 2135/ndroid.launcher: 2% user + 0% kernel
行 7542: 04-07 03:13:57.915 1444 8816 I AnrManager: 2% 2841/ged-swd: 0% user + 2% kernel
行 7543: 04-07 03:13:57.915 1444 8816 I AnrManager: 2% 26989/binder:2135_10: 2% user + 0% kernel
...
行 7595: 04-07 03:13:57.916 1444 8816 I AnrManager: 99% TOTAL: 21% user + 40% kernel + 33% iowait + 2.8% irq + 1.3% softirq
行 7596: 04-07 03:13:57.918 1444 8816 I AnrManager: dumpAnrDebugInfo end: AnrDumpRecord{ Input dispatching timed out (Application does not have a focused window) ProcessRecord{8d2c936 2135:com.android.launcher/u0a80} IsCompleted:true IsCancelled:false }, onlyDumpSelf = false , isSilentANR = false
行 7597: 04-07 03:13:57.922 1444 8816 I AnrManager: addErrorToDropBox app = ProcessRecord{8d2c936 2135:com.android.launcher/u0a80} processName = com.android.launcher activityShortComponentName = com.android.launcher/.uioverrides.QuickstepLauncher parentShortComponentName = com.android.launcher/.uioverrides.QuickstepLauncher parentProcess = ProcessRecord{8d2c936 2135:com.android.launcher/u0a80} annotation = Input dispatching timed out (Application does not have a focused window) mTracesFile = /data/anr/anr_2024-04-07-03-13-49-780
行 7598: 04-07 03:13:57.930 1444 8816 I AnrManager: controller = null

第11-23行:显示了内存,cpu,io的压力,从值看内存和io压力很大。

第26-50行:显示了从ANR发生前 9451ms-1ms的负载,第50行显示整体负载99%,io占了43%,说明IO压力非常大,整体负载非常大99%,因此导致了ANR的发生,

第51-79行:显示了ANR发生后1343ms2037ms的负载,依然很大,99%整体负载,33%的IO压力。说明系统负载确实很大,而且ANR发生后没有得到有效释放。

这就需要系统层去分析负载大的原因。另外有关负载的详细介绍在ANR-分析之基础知识介绍:logcat有介绍,详细信息可以在该章节找到。

另外一种是应用负载高,但是系统整体负载不够,这要分析应用负载,是否有什么流程在一直操作,或者死循环等,导致应用负载非常大,找到对应的原因并解决。