1、watchdog的产生
在系统启动阶段,systemserver.java中startOtherServices
final Watchdog watchdog = Watchdog.getInstance();
watchdog.init(context, mActivityManagerService);
......省略各种服务启动代码
在ActivityManagerService、PowerManagerService服务启动时有:
Watchdog.getInstance().addMonitor(this);
Watchdog.getInstance().addThread(mHandler);
在PackageManagerService、InputManagerService、MediaRouterService、MountService、WindowManagerService等服务启动时有:
Watchdog.getInstance().addMonitor(this);
或
Watchdog.getInstance().addThread(mHandler)
服务启动完成之后运行watchdog
Watchdog.getInstance().start();
运行watchdog线程
watchdog.java:
public static Watchdog getInstance() {
if (sWatchdog == null) {
sWatchdog = new Watchdog();
}
return sWatchdog;
}
public void init(Context context, ActivityManagerService activity) {
mResolver = context.getContentResolver();
mActivity = activity;
context.registerReceiver(new RebootRequestReceiver(),
new IntentFilter(Intent.ACTION_REBOOT),
android.Manifest.permission.REBOOT, null);
}
构造函数中将主线程、i/o、fg、ui、display加入到mHandlerCheckers列表中
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));
// A