本篇文章主要介绍 ​​Android​​ 开发中的部分知识点,


1.MTK AEE Log分析工具
2.AEE Log分析流程
3.根据Log ,提出 SWT 重启解决方案


MTK 平台 SWT 重启案例分析及解决方案(一)_ide

1.MTK AEE Log分析工具

MTK AEE Log 获取方式:关注微信公众号:​​程序员Android​​,回复 aee 即可获取。

2.AEE Log分析流程

a. 使用AEE 工具解析dbg 文件。

MTK 平台 SWT 重启案例分析及解决方案(一)_android_02

使用解析db.fatal.02.SWT.dbg

MTK 平台 SWT 重启案例分析及解决方案(一)_android_03

AEE Log 解析出来的文件

b.分析查看解析出来的exp_main文件

exp_main 文件会记录发生重启时候的 log 打印信息。

部分信息如下:

$** *** *** *** *** *** *** *** Fatal *** *** *** *** *** *** *** **$
Build Info: 'alps-mp-o1.mp7:alps-mp-o1.mp7:mt6765:S01,ACE/AS0618/AS0618:8.1.0/O11019/1548123508:user/release-keys'
Flavor Info: 'None'
Exception Log Time:[Thu Mar 14 14:00:03 CST 2019] [38684.729626]
Exception Class: SWT
Exception Type: system_server_watchdog
Current Executing Process:
system_server
Trigger time:[2019-03-14 14:00:03.711844] pid:1029
Backtrace:
Process: system_server
Subject: Blocked in handler on ActivityManager (ActivityManager)
Build: ACE/AS0618/AS0618:8.1.0/O11019/1548123508:user/release-keys

从开头的​​Log​​​总体信息概览,我们可以看到 发生​​重启的时间​​​、​​类型​​​,​​触发重启的进程​​​以及​​PID​​​、​​系统Blocked 的地方​​。

Log 的简单分析如下:

// 1.重启触发时间,以及PID 
Trigger time:[2019-03-14 14:00:03.711844] pid:1029
// 2.Blocked 的进程
Backtrace:
Process: system_server
Subject: Blocked in handler on ActivityManager (ActivityManager)
// 3.根据PID 查看Trace信息
----- pid 1029 at 2019-03-14 13:59:58 -----
Cmd line: system_server
// 4.根据Backtrace 查看Blocked的信息
"ActivityManager" prio=5 tid=11 Blocked
// 5.tid=11 等待 tid=106的线程释放资源锁
- waiting to lock <0x090691f3> (a android.util.ArrayMap) held by thread 106
// 6.查看tid = 106 持锁情况
"backup" prio=5 tid=106 Waiting
at java.lang.Object.wait(Native method)
- waiting on <0x06a44c62> (a com.android.server.am.ContentProviderRecord)
// 7.死锁卡住的地方
at com.android.server.am.ActivityManagerService.getContentProviderImpl(ActivityManagerService.java:12127)
- locked <0x06a44c62> (a com.android.server.am.ContentProviderRecord)

"Binder:1029_8" prio=5 tid=107 Blocked
// 8.log中 tid=107 被 106 Blocked 进一步问题的加重
at com.android.server.notification.RankingHelper.getRecord(RankingHelper.java:258)
- waiting to lock <0x090691f3> (a android.util.ArrayMap) held by thread 106

$** *** *** *** *** *** *** *** Fatal *** *** *** *** *** *** *** **$

MTK 平台 SWT 重启案例分析及解决方案(一)_android_04

Log 分析大致过程截图

完整 log 请在公众号上获取

3. 根据Log ,提出 SWT 重启解决方案

通过Log 找到原因后,解决此问题。需要修改ActivityManagerService类。

代码路径如下:​​alps/frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java​

解决思路:

避免 provider 长时间此锁触发 60s 的SWT 重启机制,设置超时时间,超过时间就要释放资源锁,避免发生此问题。

diff 方案如下:

--- a/[alps/frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java](http://192.168.11.104/gitweb/?p=alps-mp-o1.mp1-V1.git;a=blob;f=alps/frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java;h=d5e2e1411f3698a829e997d402c7482ec277fa8c;hb=d5e2e1411f3698a829e997d402c7482ec277fa8c)
+++ b/[alps/frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java](http://192.168.11.104/gitweb/?p=alps-mp-o1.mp1-V1.git;a=blob;f=alps/frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java;h=03208f78a2bf3167b4f0790019326e3939cc5444;hb=03208f78a2bf3167b4f0790019326e3939cc5444)
@@ [-545,7](http://192.168.11.104/gitweb/?p=alps-mp-o1.mp1-V1.git;a=blob;f=alps/frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java;h=d5e2e1411f3698a829e997d402c7482ec277fa8c;hb=d5e2e1411f3698a829e997d402c7482ec277fa8c#l545) [+545,9](http://192.168.11.104/gitweb/?p=alps-mp-o1.mp1-V1.git;a=blob;f=alps/frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java;h=03208f78a2bf3167b4f0790019326e3939cc5444;hb=03208f78a2bf3167b4f0790019326e3939cc5444#l545) @@ public class ActivityManagerService extends IActivityManager.Stub
// How long we wait for an attached process to publish its content providers
// before we decide it must be hung.
static final int CONTENT_PROVIDER_PUBLISH_TIMEOUT = 10*1000;
-
+ // How long we wait for provider to be notify before we decide it may be hung.
+ static final int CONTENT_PROVIDER_WAIT_TIMEOUT = 20*1000;
+
// How long we wait for a launched process to attach to the activity manager
// before we decide it's never going to come up for real, when the process was
// started with a wrapper for instrumentation (such as Valgrind) because it
@@ [-1745,6](http://192.168.11.104/gitweb/?p=alps-mp-o1.mp1-V1.git;a=blob;f=alps/frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java;h=d5e2e1411f3698a829e997d402c7482ec277fa8c;hb=d5e2e1411f3698a829e997d402c7482ec277fa8c#l1745) [+1747,7](http://192.168.11.104/gitweb/?p=alps-mp-o1.mp1-V1.git;a=blob;f=alps/frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java;h=03208f78a2bf3167b4f0790019326e3939cc5444;hb=03208f78a2bf3167b4f0790019326e3939cc5444#l1747) @@ public class ActivityManagerService extends IActivityManager.Stub
static final int PUSH_TEMP_WHITELIST_UI_MSG = 68;
static final int SERVICE_FOREGROUND_CRASH_MSG = 69;
static final int DISPATCH_OOM_ADJ_OBSERVER_MSG = 70;
+ static final int CONTENT_PROVIDER_WAIT_TIMEOUT_MSG = 71;
static final int START_USER_SWITCH_FG_MSG = 712;
static final int NOTIFY_VR_KEYGUARD_MSG = 74;
@@ [-2108,6](http://192.168.11.104/gitweb/?p=alps-mp-o1.mp1-V1.git;a=blob;f=alps/frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java;h=d5e2e1411f3698a829e997d402c7482ec277fa8c;hb=d5e2e1411f3698a829e997d402c7482ec277fa8c#l2108) [+2111,12](http://192.168.11.104/gitweb/?p=alps-mp-o1.mp1-V1.git;a=blob;f=alps/frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java;h=03208f78a2bf3167b4f0790019326e3939cc5444;hb=03208f78a2bf3167b4f0790019326e3939cc5444#l2111) @@ public class ActivityManagerService extends IActivityManager.Stub
synchronized (ActivityManagerService.this) {
mActivityStarter.doPendingActivityLaunchesLocked(true);
}
+ } break;
+ case CONTENT_PROVIDER_WAIT_TIMEOUT_MSG: {
+ ContentProviderRecord cpr = (ContentProviderRecord)msg.obj;
+ synchronized (ActivityManagerService.this) {
+ processContentProviderWaitTimedOutLocked(cpr);
+ }
} break;
case KILL_APPLICATION_MSG: {
synchronized (ActivityManagerService.this) {
@@ [-7029,7](http://192.168.11.104/gitweb/?p=alps-mp-o1.mp1-V1.git;a=blob;f=alps/frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java;h=d5e2e1411f3698a829e997d402c7482ec277fa8c;hb=d5e2e1411f3698a829e997d402c7482ec277fa8c#l7029) [+7038,31](http://192.168.11.104/gitweb/?p=alps-mp-o1.mp1-V1.git;a=blob;f=alps/frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java;h=03208f78a2bf3167b4f0790019326e3939cc5444;hb=03208f78a2bf3167b4f0790019326e3939cc5444#l7038) @@ public class ActivityManagerService extends IActivityManager.Stub
cleanupAppInLaunchingProvidersLocked(app, true);
removeProcessLocked(app, false, true, "timeout publishing content providers");
}
+
+ @GuardedBy("this")
+ private final void processContentProviderWaitTimedOutLocked(ContentProviderRecord cpr) {
+ try {
+ if (mLaunchingProviders.contains(cpr)) {
+ if (DEBUG_MU) Slog.v(TAG_MU,
+ "Remove from mLaunchingProviders, " + cpr
+ + " launchingApp=" + cpr.launchingApp);
+ mLaunchingProviders.remove(cpr);
+ }
+ if (DEBUG_MU) Slog.v(TAG_MU,
+ "RemoveMessages CONTENT_PROVIDER_WAIT_TIMEOUT_MSG, " + cpr
+ + " launchingApp=" + cpr.launchingApp);
+ mHandler.removeMessages(CONTENT_PROVIDER_WAIT_TIMEOUT_MSG, cpr);
+ synchronized (cpr) {
+ cpr.notifyAll();
+ cpr.launchingApp = null;
+ }
+ } catch (Exception e) {
+ if (DEBUG_MU) Slog.v(TAG_MU,
+ "processContentProviderWaitTimedOutLocked exception, " + e);
+ }
+ }
+
private final void processStartTimedOutLocked(ProcessRecord app) {
final int pid = app.pid;
boolean gone = false;
@@ [-12124,11](http://192.168.11.104/gitweb/?p=alps-mp-o1.mp1-V1.git;a=blob;f=alps/frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java;h=d5e2e1411f3698a829e997d402c7482ec277fa8c;hb=d5e2e1411f3698a829e997d402c7482ec277fa8c#l12124) [+12157,33](http://192.168.11.104/gitweb/?p=alps-mp-o1.mp1-V1.git;a=blob;f=alps/frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java;h=03208f78a2bf3167b4f0790019326e3939cc5444;hb=03208f78a2bf3167b4f0790019326e3939cc5444#l12157) @@ public class ActivityManagerService extends IActivityManager.Stub
if (conn != null) {
conn.waiting = true;
}
+ // add 20s wait timeout,avoid
+ if (!mHandler.hasMessages(CONTENT_PROVIDER_WAIT_TIMEOUT_MSG, cpr)) {
+ if (DEBUG_MU) Slog.v(TAG_MU,
+ "SendMessageDelayed CONTENT_PROVIDER_WAIT_TIMEOUT_MSG, " + cpr
+ + " launchingApp=" + cpr.launchingApp);
+ Message msg = mHandler.obtainMessage(CONTENT_PROVIDER_WAIT_TIMEOUT_MSG);
+ msg.obj = cpr;
+ mHandler.sendMessageDelayed(msg, CONTENT_PROVIDER_WAIT_TIMEOUT);
+ } else {
+ if (DEBUG_MU) Slog.v(TAG_MU,
+ "There is another waiting to start provider " + cpr
+ + " launchingApp=" + cpr.launchingApp
+ + ", not send CONTENT_PROVIDER_WAIT_TIMEOUT_MSG again");
+ }
+
cpr.wait();
} catch (InterruptedException ex) {
} finally {
if (conn != null) {
conn.waiting = false;
+ }
+ // remove wait time out message
+ if (mHandler.hasMessages(CONTENT_PROVIDER_WAIT_TIMEOUT_MSG, cpr)) {
+ if (DEBUG_MU) Slog.v(TAG_MU,
+ "After wait removeMessages CONTENT_PROVIDER_WAIT_TIMEOUT_MSG, "
+ + cpr + " launchingApp=" + cpr.launchingApp);
+ mHandler.removeMessages(CONTENT_PROVIDER_WAIT_TIMEOUT_MSG, cpr);
}
}
}