Android Watchdog 机制

用两张图和部分代码来描述:

第一张图(图片需要在新标签中打开):

Android Watchdog 机制_第1张图片

第二张图借自 @宇落无痕 的博客:

Android Watchdog 机制_第2张图片

代码 Watchdog.run():

    @Override
    public void run() {
        boolean waitedHalf = false;
        while (true) {
             ...
            synchronized (this) {
                long timeout = CHECK_INTERVAL;
                // Make sure we (re)spin the checkers that have become idle within
                // this wait-and-check interval
                for (int i=0; i 0) {
                    if (Debug.isDebuggerConnected()) {
                        debuggerWasConnected = 2;
                    }
                    try {
                        wait(timeout);
                    } catch (InterruptedException e) {
                        Log.wtf(TAG, e);
                    }
                    if (Debug.isDebuggerConnected()) {
                        debuggerWasConnected = 2;
                    }
                    timeout = CHECK_INTERVAL - (SystemClock.uptimeMillis() - start);
                }

                final int waitState = evaluateCheckerCompletionLocked();
                if (waitState == COMPLETED) {
                    // The monitors have returned; reset
                    waitedHalf = false;
                    continue;
                } else if (waitState == WAITING) {
                    // still waiting but within their configured intervals; back off and recheck
                    continue;
                } else if (waitState == WAITED_HALF) {
                    if (!waitedHalf) {
                        // We've waited half the deadlock-detection interval.  Pull a stack
                        // trace and wait another half.
                        ArrayList pids = new ArrayList();
                        pids.add(Process.myPid());
                        ActivityManagerService.dumpStackTraces(true, pids, null, null,
                                NATIVE_STACKS_OF_INTEREST);
                        waitedHalf = true;
                    }
                    continue;
                }

                // something is overdue!
                blockedCheckers = getBlockedCheckersLocked();
                subject = describeCheckersLocked(blockedCheckers);
                allowRestart = mAllowRestart;
            }

             //后面的逻辑就是 dump trace和重启了
             ...

HandlerChecker.scheduleCheckLocked():


Android Watchdog 机制_第3张图片

HandlerChecker.run():

Android Watchdog 机制_第4张图片

HandlerChecker.getCompletionStateLocked():

Android Watchdog 机制_第5张图片

Watchdog.evaluateCheckerCompletionLocked():

Android Watchdog 机制_第6张图片



你可能感兴趣的:(Android)