Watchdog實現分析


系統啟動過程圖:

 

  Framework層所有的Service都是運行在SystemServer進程中;SystemServer進程是由Zygote進程創建。

SystemServer進程啟動分兩個過程init1創建Service和進程狀態對象;init2創建Framework層的Service,將其加入到ServiceManager中,最后啟動launcher;

Android提供了Watchdog類,用來監測Service是否處於正常工作中,是在SystemServer中啟動的。

  下面看一下SystemServer中Watchdog這個過程。

SystemServer.java:

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

 

Watchdog類實現

類繼承結構:

    

 

  看到Watchdog是一個Thread,運行在SystemServer進程中,單例模式;

  HeartbeatHandler處理接受監控的對象(Service),運行在主線程中;

  Monitor提供監控接口,接受監控對象實現此接口;

  XXXService具體實現的檢測對象。

執行流程:

    

 

對外接口

初始化:

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, 0, new Intent(REBOOT_ACTION), 0);      ……   //開機時間
      mBootTime = System.currentTimeMillis(); }

 

注冊監控對象:

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

 

搜索一下此函數的調用,表示被監控;看到在如下Service中實現Watchdog的Monitor接口:

  ActivityManagerService

  InputManagerService

  NetworkManagementService

  PowerManagerService

  WindowManagerService

  都有調用:Watchdog.getInstance().addMonitor(this);

Watchdog線程執行函數:

public void run() { boolean waitedHalf = false; while (true) { //監測完成標志
        mCompleted = false; //發送監測消息
 mHandler.sendEmptyMessage(MONITOR); synchronized (this) { long timeout = TIME_TO_WAIT; long start = SystemClock.uptimeMillis(); while (timeout > 0 && !mForceKillSystem) { //休眠等待檢查結果
              wait(timeout);  // notifyAll() is called when mForceKillSystem is set
                timeout = TIME_TO_WAIT - (SystemClock.uptimeMillis() - start); } if (mCompleted && !mForceKillSystem) { //檢查結果OK
                waitedHalf = false; continue; } //在進行檢查一次
            if (!waitedHalf) { ActivityManagerService.dumpStackTraces(true, pids, null, null, NATIVE_STACKS_OF_INTEREST); waitedHalf = true; continue; } } //表明監控對象有問題 // 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當前進程SystemServer
 Process.killProcess(Process.myPid()); System.exit(10); } } waitedHalf = false; } }

  在此run函數中循環發送消息,判斷標志是否正常,決定檢測對象是否正常工作。

  若監測對象不正常工作,則收集重要的stack信息保存下來,然后重啟SystemServer。

監測消息的處理:

       是在HeartbeatHandler中進行,看看消息處理函數。

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

 

判斷監測對象是否正常工作,通過調用監測對象實現的接口monitor,看看這個接口該如何執行的。

PowerManagerService中:

public void monitor() { //判斷Service是否發生死鎖,如果發生死鎖,程序將在此一直等待
  //主要是線程間同步問題 造成死鎖
  synchronized (mLocks) { } }

   

  以上便是Watchdog監測Service是否正常工作的流程;我們也可以使用Watchdog來監測別的資源如內存等使用情況。

這個Watchdog給我們提供了一種思路,一種框架,對程序正常運行或者資源的正常使用情況等的一種監測機制。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM