前段时间开发了1个小屏的功能(效果图以下:),果了1段时间,测试的人员说我们的手机有时开机不了:1直停止在开机的界面.刚开始都不知道甚么缘由.查了各种代码都没有发现问题.到后来感觉问题愈来愈严重了.
到后来感觉问题愈来愈严重了.重要的是这个时低几率事件,很难复现1次,所以有点难.好不容易出现,通过data/anr/traces.txt找到问题,下面来看看关键信息:
"WindowManag4 MONITOR
| group="main" sCount=1 dsCount=0 obj=0x4275d4d8 self=0x57929cb0
| sysTid=732 nice=⑷ sched=0/0 cgrp=apps handle=1469227632
| schedstat=( 1563495873 1380427864 20236 )
- waiting to lock <0x427e72d8> (a Ljava/lang/Object; ) held by tid=1 (main)
| [0]:<0006> Landroid/hardware/display/DisplayManagerGlobal;.getDisplayInfo(I)Landroid/view/DisplayInfo; (<span style="color:#FF0000;">DisplayManagerGlobal.java:108</span>)
| [1]:<0008> Landroid/view/Display;.updateDisplayInfoLocked()V (Display.java:657)
| [2]:<0002> Landroid/view/Display;.getMetrics(Landroid/util/DisplayMetrics;)V (Display.java:585)
| [3]:<0104> Lcom/android/server/display/xunhuOverlayDisplayAdapter;.<init>(Lcom/android/server/display/DisplayManagerService$SyncRoot;Lan (<span style="color:#FF0000;">xunhuOverlayDisplayAdapter.java:104</span>)
| [4]:<0030> Lcom/android/server/display/DisplayManagerService;.register_xunhuOverlayDisplayAdapterLocked()V (DisplayManagerService.java:994)
"main" prio=5 tid=1 MONITOR
| group="main" sCount=1 dsCount=0 obj=0x422c6cf8 self=0x4182e500
| sysTid=715 nice=⑵ sched=0/0 cgrp=apps handle=1074631044
| schedstat=( 7254851604 2756007452 25767 )
- waiting to lock <0x4277acc8> (a Lcom/android/server/display/DisplayManagerService$SyncRoot; ) held by tid=14 (WindowManager)
| [0]:<0020> Lcom/android/server/display/DisplayManagerService;.getDisplayInfo(I)Landroid/view/DisplayInfo; (<span style="color:#FF0000;">DisplayManagerService.java:449</span>)
| [1]:<0012> Landroid/hardware/display/DisplayManagerGlobal;.getDisplayInfo(I)Landroid/view/DisplayInfo; (<span style="color:#FF0000;">DisplayManagerGlobal.java:117</span>)
| [2]:<0000> Landroid/hardware/display/DisplayManagerGlobal;.getCompatibleDisplay(ILandroid/view/DisplayAdjustments;)Landroid/view/Display; (DisplayManagerGlobal.java:176)
上面两段信息的关键是:waiting to lock 看来貌似是进入1个死锁.两个锁等待都在这个文件出现:DisplayManagerGlobal.java,所以看看DisplayManagerGlobal就会发现问题:
public DisplayInfo getDisplayInfo(int displayId) {
try {
synchronized (mLock) {<span style="color:#FF0000;">//108</span>
DisplayInfo info;
if (USE_CACHE) {
info = mDisplayInfoCache.get(displayId);
if (info != null) {
return info;
}
}
info = mDm.getDisplayInfo(displayId);<span style="color:#FF0000;">//117</span>
if (info == null) {
return null;
}
首先是这里mLock锁竞争.在这个锁里调用mDm.getDisplayInfo(displayId)的时候会回调到DisplayManagerService里面,以下代码:
public DisplayInfo getDisplayInfo(int displayId) {
final int callingUid = Binder.getCallingUid();
final long token = Binder.clearCallingIdentity();
try {
synchronized (mSyncRoot) {<span style="color:#FF0000;">//449</span>
/// M:[SmartBook]Ignore extra display devicee info
if (FeatureOption.MTK_SMARTBOOK_SUPPORT) {
if (displayId == mExtraDisplayId) {
return null;
}
}
很明显,在获得了 DisplayManagerGlobal里面的锁:mLock,还需要DisplayManagerService里面的锁mSyncRoot.
而我开发的小屏功能在DisplayManagerService里面register小屏的DisplayAdapter的时候已获得了mSyncRoot锁,所以如果在这同时(register小屏的DisplayAdapter的时候)调用下面的代码:
WindowManager wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
wm.getDefaultDisplay().getMetrics(metric);
就容易出现死锁. 由于在调用getDefaultDisplay()的时候最后会调用DisplayManagerGlobal的getDisplayInfo这个方法.而在开机的时候有大量其他地方调用DisplayManagerGlobal的getDisplayInfo这个方法.