Watchdog in Android
这里讲的Watchdog是AndoirdFramework中提到Watchdog类,不是一般CPU中用于系统复位的Watchdog硬件. 我认为Android的Watchdog就是系统一种软件检查一些关键服务是否发生死锁的机制
1,先看一下Watchdog在Android中的位置
2,在SystemServer使用单例模式构造自己
1 | SystemServer.java |
2 | |
3 | Slog.i(TAG, "Init Watchdog"); |
4 | Watchdog.getInstance().init(context, battery, power, alarm, |
5 | ActivityManagerService.self()); |
6 | |
7 | ////////////////////////// |
8 | Watchdog.java |
9 | public static Watchdog getInstance() { |
10 | if (sWatchdog == null) { |
11 | sWatchdog = new Watchdog(); |
12 | } |
13 | |
14 | return sWatchdog; |
15 | } |
16 | |
17 | private Watchdog() { |
18 | super("watchdog"); |
19 | mHandler = new HeartbeatHandler(); //得到一个mHander用于发送MONITOR message 去Check 相关Service |
20 | } |
21 | |
22 | public void init(Context context, BatteryService battery, |
23 | PowerManagerService power, AlarmManagerService alarm, |
24 | ActivityManagerService activity) { |
25 | mResolver = context.getContentResolver(); |
26 | mBattery = battery; |
27 | mPower = power; |
28 | mAlarm = alarm; |
29 | mActivity = activity; |
30 | |
31 | context.registerReceiver(new RebootReceiver(), |
32 | new IntentFilter(REBOOT_ACTION)); |
33 | mRebootIntent = PendingIntent.getBroadcast(context, |
34 | 0, new Intent(REBOOT_ACTION), 0); |
35 | |
36 | context.registerReceiver(new RebootRequestReceiver(), |
37 | new IntentFilter(Intent.ACTION_REBOOT), |
38 | android.Manifest.permission.REBOOT, null); |
39 | |
40 | mBootTime = System.currentTimeMillis(); |
41 | } |
42 |
3,Watchog是继承处Thread,它必须要实现run()方法,而且这个run()方法也是由SystemServer start()的。
1 | SystemServer.java |
2 | Watchdog.getInstance().start(); |
3 | |
4 | Watchdog.java |
5 | public void run() { |
6 | boolean waitedHalf = false; |
7 | while (true) { //循环检查 |
8 | mCompleted = false; //通过mCompleted来检查相关进程是否正常返回 |
9 | mHandler.sendEmptyMessage(MONITOR); //set MONITOR Messager |
10 | |
11 | synchronized (this) { |
12 | ... //分半时逻辑实现 |
13 | } |
14 | } |
15 | |
16 | |
17 | for (int i = 0 ; i < size ; i++) { |
18 | mCurrentMonitor = mMonitors.get(i); |
19 | mCurrentMonitor.monitor(); //调用所有实现了Moniter接口中的monitor函数,看是正常返回 |
20 | } |
21 |
4,以InputMangerServer为例是如何实现Watchdog来检查系统的关键服务是否发生死锁
1 | InputManagerService.java |
2 | // Called by the heartbeat to ensure locks are not held indefinitely (for deadlock detection). |
3 | @Override |
4 | public void monitor() { |
5 | synchronized (mInputFilterLock) { } |
6 | nativeMonitor(mPtr); |
7 | } |
8 | -> |
9 | static void nativeMonitor(JNIEnv* env, jclass clazz, jint ptr) { |
10 | NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr); |
11 | |
12 | im->getInputManager()->getReader()->monitor(); //分别调用InputReader和InputDispatcher的monitor() |
13 | im->getInputManager()->getDispatcher()->monitor(); |
14 | } |
15 | -> |
16 | void InputReader::monitor() { |
17 | // Acquire and release the lock to ensure that the reader has not deadlocked. |
18 | mLock.lock(); |
19 | mEventHub->wake(); |
20 | mReaderIsAliveCondition.wait(mLock); |
21 | mLock.unlock(); |
22 | |
23 | // Check the EventHub |
24 | mEventHub->monitor(); |
25 | } |
26 | -> |
27 | inline status_t Condition::wait(Mutex& mutex) { |
28 | return -pthread_cond_wait(&mCond, &mutex.mMutex); //判断线程是否活 |
29 | } |
30 | |
31 | -> |
32 | void InputDispatcher::monitor() { |
33 | // Acquire and release the lock to ensure that the dispatcher has not deadlocked. |
34 | mLock.lock(); |
35 | mLooper->wake(); |
36 | mDispatcherIsAliveCondition.wait(mLock); //同样,mDispatchcherIsAliveCondition也是一个Condition类 |
37 | mLock.unlock(); |
38 | } |
39 | -> |
40 | void EventHub::monitor() { |
41 | // Acquire and release the lock to ensure that the event hub has not deadlocked. |
42 | mLock.lock(); |
43 | mLock.unlock(); |
44 | } |
45 |