标签:
Android 平台实现了一个软件的WatchDog来监护SystemServer。SystemServer无疑是Android平台中最重要的进程了,里面运行了整个平台中绝大多数的服务。
SystemServer退出反而问题不大,因为 init进程会重新启动它,但是它死锁就麻烦了,因为整个系统就没法动了。所以我们需要使用看门狗来监护它,当很长一段时间没有喂狗,就会重启SystemServer进程。
WatchDog的作用:
1).接收系统内部reboot请求,重启系统。
2).监护SystemServer进程,防止系统死锁。
如何将要监护的服务添加进看门狗呢?
在 SystemServer里运行的服务中,最重要的几个服务应该数ActivityManager、WindowManager和 PowerManager。
1).每个被监护的Service必须实现Watchdog.Monitor接口,以ActivityManagerService为例来:
public void monitor() { synchronized (this) { } }
它去锁一下对象,什么也不做,然后就返回。如果对象没有死锁,这个过程就会很顺利。如果对象死锁了,这个函数就会挂在这里。
2).把ActivityManagerService注册到WatchDog服务中,在初始化时:
Watchdog.getInstance().addMonitor(this);
最后我们看看WatchDog服务的实现。
先看看WatchDog对象,也就是所,需要监护的进程有”foreground thread”,”main thread”,”ui thread”,”i/o thread”。android系统默认的超时时间是60秒。
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));
}
再看看init函数
RebootRequestReceiver负责接收系统内部发出的重启Intent消息,并进行系统重启。
public void init(Context context, BatteryService battery,
PowerManagerService power, AlarmManagerService alarm,
ActivityManagerService activity) {
mResolver = context.getContentResolver();
mBattery = battery;
mPower = power;
mAlarm = alarm;
mActivity = activity;
context.registerReceiver(new RebootRequestReceiver(),
new IntentFilter(Intent.ACTION_REBOOT),
android.Manifest.permission.REBOOT, null);
}
WatchDog服务包括两个方面:
1.定期调用被监护对象的monitor函数,这是在主线程中完成的。如果被监护对象死锁,则会阻塞在这里。
final int size = mMonitors.size();
for (int i = 0 ; i < size ; i++) { mCurrentMonitor = mMonitors.get(i); mCurrentMonitor.monitor();
}
2.检测是否发生死锁,这是在Watchdog线程中运行的。如果发生死锁而且没有被调试,则退出SystemServer,init进程就会重启SystemServer进程。
// Only kill the process if the debugger is not attached. if (!Debug.isDebuggerConnected()) { Slog.w(TAG, "*** WATCHDOG KILLING SYSTEM PROCESS: " + name); Process.killProcess(Process.myPid()); System.exit(10); } else { Slog.w(TAG, "Debugger connected: Watchdog is *not* killing the system process"); }
此时,可以看内核打印,在dalvik.vm.stack-trace-file这个属性定义了存储的地方。
标签:
原文地址:http://blog.csdn.net/myvest/article/details/51354513