系統(tǒng)啟動過程圖:
Framework層所有的Service都是運行在SystemServer進程中;SystemServer進程是由Zygote進程創(chuàng)建。
SystemServer進程啟動分兩個過程init1創(chuàng)建Service和進程狀態(tài)對象;init2創(chuàng)建Framework層的Service,將其加入到ServiceManager中,最后啟動launcher;
Android提供了Watchdog類,用來監(jiān)測Service是否處于正常工作中,是在SystemServer中啟動的。
下面看一下SystemServer中Watchdog這個過程。
SystemServer.java:
public void run() {
//初始化Watchdog 傳入各個Service作為參數(shù)
Watchdog.getInstance().init(context, battery, power, alarm,
ActivityManagerService.self());
//啟動Watchdog
Watchdog.getInstance().start();
}
Watchdog類實現(xiàn)
類繼承結(jié)構(gòu):
看到Watchdog是一個Thread,運行在SystemServer進程中,單例模式;
HeartbeatHandler處理接受監(jiān)控的對象(Service),運行在主線程中;
Monitor提供監(jiān)控接口,接受監(jiān)控對象實現(xiàn)此接口;
XXXService具體實現(xiàn)的檢測對象。
執(zhí)行流程:
對外接口
初始化:
public void init(Context context, BatteryService battery,
PowerManagerService power, AlarmManagerService alarm,
ActivityManagerService activity) {
//存儲Service對象,運行在同一個進程中
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);
……
//開機時間
mBootTime = System.currentTimeMillis();
}
注冊監(jiān)控對象:
public void addMonitor(Monitor monitor) {
synchronized (this) {
//將監(jiān)控對象加入到列表中
mMonitors.add(monitor);
}
}
搜索一下此函數(shù)的調(diào)用,表示被監(jiān)控;看到在如下Service中實現(xiàn)Watchdog的Monitor接口:
ActivityManagerService
InputManagerService
NetworkManagementService
PowerManagerService
WindowManagerService
都有調(diào)用:Watchdog.getInstance().addMonitor(this);
Watchdog線程執(zhí)行函數(shù):
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;
}
//在進行檢查一次
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)前進程SystemServer
Process.killProcess(Process.myPid());
System.exit(10);
}
}
waitedHalf = false;
}
}
在此run函數(shù)中循環(huán)發(fā)送消息,判斷標(biāo)志是否正常,決定檢測對象是否正常工作。
若監(jiān)測對象不正常工作,則收集重要的stack信息保存下來,然后重啟SystemServer。
監(jiān)測消息的處理:
是在HeartbeatHandler中進行,看看消息處理函數(shù)。
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)測對象實現(xiàn)的接口monitor,看看這個接口該如何執(zhí)行的。
PowerManagerService中:
public void monitor() {
//判斷Service是否發(fā)生死鎖,如果發(fā)生死鎖,程序?qū)⒃诖艘恢钡却?/主要是線程間同步問題 造成死鎖
synchronized (mLocks) { }
}
以上便是Watchdog監(jiān)測Service是否正常工作的流程;我們也可以使用Watchdog來監(jiān)測別的資源如內(nèi)存等使用情況。
這個Watchdog給我們提供了一種思路,一種框架,對程序正常運行或者資源的正常使用情況等的一種監(jiān)測機制。