(11)Android之路====Android Java层守护者Watchdog机制及应用

硬件Watchdog基本上已经是嵌入式芯片的标配了,它可以在系统出现严重错误时(程序跑飞,死锁等)无法恢复系统时,重启系统.原理比较简单:用一个定时器作为看门狗硬件,开启后,要定时"喂狗"(计数重载),如果在规定时间内没有"喂狗",看门狗定时器就会进行硬件重启.

在Android系统中,除了硬件Watchdog,还实现了一个软件Watchdog,主要是为了弥补硬件Watchdog功能的单一性,它本身继承Thread,是一个线程类,Android Watchdog主要作用如下:
1),接收系统内部reboot请求,重启系统;
2),监护SystemServer进程,防止系统死锁.

1,实现原理

主要代码位置:
frameworks/base/services/core/java/com/android/server/Watchdog.java
frameworks/base/services/java/com/android/server/SystemServer.java

流程图如下(网摘):

(11)Android之路====Android Java层守护者Watchdog机制及应用_第1张图片

// 未完,待续...

2,启动

Watchdog是一个Thread,并且采用了单例模式实现,在SystemServer进程中被初始化和启动的.当SystemServer中的ServerThread线程被Start时,各种Android服务被注册和启动,其中包括了Watchdog的初始化和启动.代码实现在startOtherServices方法中,如下:

final Watchdog watchdog = Watchdog.getInstance();
watchdog.init(context, mActivityManagerService);

在SystemServer中startOtherServices的后半段,将通过SystemReady接口通知系统已经就绪,在ActivityManagerService的SystemReady接口的CallBack函数中实现WatchDog的启动:

Watchdog.getInstance().start();

getInstance的实现如下:

public static Watchdog getInstance() {
    if (sWatchdog == null) {
        sWatchdog = new Watchdog();
    }

    return sWatchdog;
}

private Watchdog() {
    super("watchdog");
    // Initialize handler checkers for each common thread we want to check.  Note
    // that we are not currently checking the background thread, since it can
    // potentially hold longer running operations with no guarantees about the timeliness
    // of operations there.

    // The shared foreground thread is the main checker.  It is where we
    // will also dispatch monitor checks and do other work.
    mMonitorChecker = new HandlerChecker(FgThread.getHandler(),
            "foreground thread", DEFAULT_TIMEOUT);
    mHandlerCheckers.add(mMonitorChecker);
    // Add checker for main thread.  We only do a quick check since there
    // can be UI running on the thread.
    mHandlerCheckers.add(new HandlerChecker(new Handler(Looper.getMainLooper()),
            "main thread", DEFAULT_TIMEOUT));
    // Add checker for shared UI thread.
    mHandlerCheckers.add(new HandlerChecker(UiThread.getHandler(),
            "ui thread", DEFAULT_TIMEOUT));
    // And also check IO thread.
    mHandlerCheckers.add(new HandlerChecker(IoThread.getHandler(),
            "i/o thread", DEFAULT_TIMEOUT));
    // And the display thread.
    mHandlerCheckers.add(new HandlerChecker(DisplayThread.getHandler(),
            "display thread", DEFAULT_TIMEOUT));

    // Initialize monitor for Binder threads.
    addMonitor(new BinderThreadMonitor());
}

从上面代码可以看出,在构造方法中,Foreground Thread, Main Thread, UI Thread, I/O Thread, Display Thread加入到mHandlerCheckers列表中,初始化monitor放入mMonitorCheckers列表中:

public void addMonitor(Monitor monitor) {
    synchronized (this) {
        if (isAlive()) {
            throw new RuntimeException("Monitors can't be added once the Watchdog is running");
        }
        mMonitorChecker.addMonitor(monitor);
    }
}

// 未完,待续...

3,使用方法

// 未完,待续...

你可能感兴趣的:((11)Android之路====Android Java层守护者Watchdog机制及应用)