Android WatchDog mechanism

Source: Internet
Author: User

The Android platform implements a software watchdog to monitor the systemserver. Systemserver is undoubtedly the most important process in the Android platform, which runs the vast majority of services throughout the platform.
Systemserver exit is not a problem, because the INIT process restarts it, but it's a deadlock because the system can't move. So we need to use the watchdog to monitor it, and when the dog hasn't been fed for a long time, it will restart the systemserver process.
The role of watchdog:

1). Receive system internal reboot request, reboot system.
2). Monitor the systemserver process to prevent the system from deadlock.

How do I add a watch dog to a service that will be monitored?
Among the services running in Systemserver, several of the most important services should be activitymanager, WindowManager, and PowerManager.
1). Each monitored service must implement the Watchdog.monitor interface, taking Activitymanagerservice as an example:

publicvoidmonitor() {        synchronized (this) { }     }

It locks the object, does nothing, and then returns. If the object does not have a deadlock, the process will be smooth. If the object is deadlocked, the function is hung here.

2). Register the Activitymanagerservice with the watchdog service at the time of initialization:

Watchdog.getInstance().addMonitor(this);

Finally, we look at the implementation of the watchdog service.
First look at the watchdog object, that is, the process that needs to be monitored has "foreground thread", "Main thread", "UI thread", "I/O thread". The default time-out for Android is 60 seconds.

Private Watchdog () {Super ("Watchdog"); InitializeHandlerCheckers for  eachCommon thread We want to Check. Note//That we is  notCurrently checking the background thread, since it can//potentially hold longer running operations with NoGuarantees about the timeliness// ofOperations there. The shared foreground thread isThe main checker. It is wherewe//would also dispatch monitor checks and  DoOther Work. Mmonitorchecker = new Handlerchecker (Fgthread.gethandler (),"Foreground thread", default_timeout);Mhandlercheckers.add (Mmonitorchecker);  Add Checker for main thread. We onlydo a quick check since there//can is 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 look at the init function.
The rebootrequestreceiver is responsible for receiving the restart intent messages from the system and rebooting the system.

    publicvoidinit(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),                null);    }

Watchdog services include two areas:

1. Periodically invoke the monitor function of the monitored object, which is done in the main thread. If the monitored object is deadlocked, it will be blocked here.

sizemMonitors.size();     fori0isizei{                        mCurrentMonitor = mMonitors.get(i);                        mCurrentMonitor.monitor();                                     }

2. Detects if a deadlock has occurred, which is running in the watchdog thread. If deadlocks occur and are not debugged, exiting the systemserver,init process restarts the systemserver process.

       // 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");            }

At this point, you can see the kernel print, where the Dalvik.vm.stack-trace-file attribute defines the storage.

Android WatchDog mechanism

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.