欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

android Watchdog 實(shí)現(xiàn)剖析

 更新時(shí)間:2012年11月14日 14:48:02   作者:  
Android提供了Watchdog類,用來監(jiān)測Service是否處于正常工作中,是在SystemServer中啟動(dòng)的;本文將詳細(xì)介紹
系統(tǒng)啟動(dòng)過程圖:
 
Framework層所有的Service都是運(yùn)行在SystemServer進(jìn)程中;SystemServer進(jìn)程是由Zygote進(jìn)程創(chuàng)建。
SystemServer進(jìn)程啟動(dòng)分兩個(gè)過程init1創(chuàng)建Service和進(jìn)程狀態(tài)對象;init2創(chuàng)建Framework層的Service,將其加入到ServiceManager中,最后啟動(dòng)launcher;
Android提供了Watchdog類,用來監(jiān)測Service是否處于正常工作中,是在SystemServer中啟動(dòng)的。
下面看一下SystemServer中Watchdog這個(gè)過程。
SystemServer.java:
復(fù)制代碼 代碼如下:

public void run() {
//初始化Watchdog 傳入各個(gè)Service作為參數(shù)
Watchdog.getInstance().init(context, battery, power, alarm,
ActivityManagerService.self());
//啟動(dòng)Watchdog
Watchdog.getInstance().start();
}

Watchdog類實(shí)現(xiàn)
類繼承結(jié)構(gòu):
 
看到Watchdog是一個(gè)Thread,運(yùn)行在SystemServer進(jìn)程中,單例模式;
HeartbeatHandler處理接受監(jiān)控的對象(Service),運(yùn)行在主線程中;
Monitor提供監(jiān)控接口,接受監(jiān)控對象實(shí)現(xiàn)此接口;
XXXService具體實(shí)現(xiàn)的檢測對象。
執(zhí)行流程:
 
對外接口
初始化:
復(fù)制代碼 代碼如下:

public void init(Context context, BatteryService battery,
PowerManagerService power, AlarmManagerService alarm,
ActivityManagerService activity) {
//存儲(chǔ)Service對象,運(yùn)行在同一個(gè)進(jìn)程中
mResolver = context.getContentResolver();
mBattery = battery; mPower = power;
mAlarm = alarm; mActivity = activity;
//注冊廣播
context.registerReceiver(new RebootReceiver(),
new IntentFilter(REBOOT_ACTION));
mRebootIntent = PendingIntent.getBroadcast(context,
, new Intent(REBOOT_ACTION), 0);
……
//開機(jī)時(shí)間
mBootTime = System.currentTimeMillis();
}

注冊監(jiān)控對象:
復(fù)制代碼 代碼如下:

public void addMonitor(Monitor monitor) {
synchronized (this) {
//將監(jiān)控對象加入到列表中
mMonitors.add(monitor);
}
}

搜索一下此函數(shù)的調(diào)用,表示被監(jiān)控;看到在如下Service中實(shí)現(xiàn)Watchdog的Monitor接口:
ActivityManagerService
InputManagerService
NetworkManagementService
PowerManagerService
WindowManagerService
都有調(diào)用:Watchdog.getInstance().addMonitor(this);
Watchdog線程執(zhí)行函數(shù):
復(fù)制代碼 代碼如下:

public void run() {
boolean waitedHalf = false;
while (true) {
//監(jiān)測完成標(biāo)志
mCompleted = false;
//發(fā)送監(jiān)測消息
mHandler.sendEmptyMessage(MONITOR);
synchronized (this) {
long timeout = TIME_TO_WAIT;
long start = SystemClock.uptimeMillis();
while (timeout > 0 && !mForceKillSystem) {
//休眠等待檢查結(jié)果
wait(timeout); // notifyAll() is called when mForceKillSystem is set
timeout = TIME_TO_WAIT - (SystemClock.uptimeMillis() - start);
}
if (mCompleted && !mForceKillSystem) {
//檢查結(jié)果OK
waitedHalf = false;
continue;
}
//在進(jìn)行檢查一次
if (!waitedHalf) {
ActivityManagerService.dumpStackTraces(true, pids, null, null,
NATIVE_STACKS_OF_INTEREST);
waitedHalf = true;
continue;
}
}
//表明監(jiān)控對象有問題
// If we got here, that means that the system is most likely hung.
// First collect stack traces from all threads of the system process.
// Then kill this process so that the system will restart.
//保存stack信息
……
// Only kill the process if the debugger is not attached.
if(!Debug.isDebuggerConnected()) {
if(SystemProperties.getInt("sys.watchdog.disabled", 0) == 0) {
//kill當(dāng)前進(jìn)程SystemServer
Process.killProcess(Process.myPid());
System.exit(10);
}
}
waitedHalf = false;
}
}

在此run函數(shù)中循環(huán)發(fā)送消息,判斷標(biāo)志是否正常,決定檢測對象是否正常工作。
若監(jiān)測對象不正常工作,則收集重要的stack信息保存下來,然后重啟SystemServer。
監(jiān)測消息的處理:
是在HeartbeatHandler中進(jìn)行,看看消息處理函數(shù)。
復(fù)制代碼 代碼如下:

public void handleMessage(Message msg) {
switch (msg.what) {
case MONITOR: {
// See if we should force a reboot.
//監(jiān)測對象是否正常工作中……
final int size = mMonitors.size();
for (int i = 0 ; i < size ; i++) {
//調(diào)用監(jiān)測對象的monitor接口
mCurrentMonitor = mMonitors.get(i);
mCurrentMonitor.monitor();
}
//走到這里表明監(jiān)測對象正常
synchronized (Watchdog.this) {
mCompleted = true;
mCurrentMonitor = null;
}
} break;
}
}

判斷監(jiān)測對象是否正常工作,通過調(diào)用監(jiān)測對象實(shí)現(xiàn)的接口monitor,看看這個(gè)接口該如何執(zhí)行的。
PowerManagerService中:
public void monitor() {
//判斷Service是否發(fā)生死鎖,如果發(fā)生死鎖,程序?qū)⒃诖艘恢钡却?/主要是線程間同步問題 造成死鎖
synchronized (mLocks) { }
}
以上便是Watchdog監(jiān)測Service是否正常工作的流程;我們也可以使用Watchdog來監(jiān)測別的資源如內(nèi)存等使用情況。
這個(gè)Watchdog給我們提供了一種思路,一種框架,對程序正常運(yùn)行或者資源的正常使用情況等的一種監(jiān)測機(jī)制。

相關(guān)文章

最新評論