blob: 7eedc2a52c84b2fd888df5a3e3d7c1824703857f [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
Dianne Hackborn5b88a2f2013-05-03 16:25:11 -070019import android.app.IActivityController;
20import android.os.Binder;
21import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import com.android.server.am.ActivityManagerService;
Jeff Brown4f8ecd82012-06-18 18:29:13 -070023import com.android.server.power.PowerManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024
25import android.app.AlarmManager;
26import android.app.PendingIntent;
27import android.content.BroadcastReceiver;
28import android.content.ContentResolver;
29import android.content.Context;
30import android.content.Intent;
31import android.content.IntentFilter;
Jeff Browna4d82042012-10-02 19:11:19 -070032import android.os.BatteryManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.os.Debug;
34import android.os.Handler;
John Michelau11641522013-03-18 18:28:23 -050035import android.os.Looper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.os.Process;
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -080037import android.os.ServiceManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.os.SystemClock;
39import android.os.SystemProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.util.EventLog;
Dan Egnor9bdc94b2010-03-04 14:20:31 -080041import android.util.Log;
Joe Onorato8a9b2202010-02-26 18:56:32 -080042import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043
Dan Egnor9bdc94b2010-03-04 14:20:31 -080044import java.io.File;
Colin Cross5df1d872012-11-29 11:42:11 -080045import java.io.FileWriter;
46import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import java.util.ArrayList;
48import java.util.Calendar;
49
50/** This class calls its monitor every minute. Killing this process if they don't return **/
51public class Watchdog extends Thread {
52 static final String TAG = "Watchdog";
Joe Onorato43a17652011-04-06 19:22:23 -070053 static final boolean localLOGV = false || false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054
55 // Set this to true to use debug default values.
56 static final boolean DB = false;
57
Christopher Tateecaa7b42010-06-04 14:55:02 -070058 // Set this to true to have the watchdog record kernel thread stacks when it fires
59 static final boolean RECORD_KERNEL_THREADS = true;
60
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061 static final int MONITOR = 2718;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062
Mathias Agopiancf2317e2011-08-25 17:12:37 -070063 static final int TIME_TO_RESTART = DB ? 15*1000 : 60*1000;
Christopher Tate6ee412d2010-05-28 12:01:56 -070064 static final int TIME_TO_WAIT = TIME_TO_RESTART / 2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066 static final int MEMCHECK_DEFAULT_MIN_SCREEN_OFF = DB ? 1*60 : 5*60; // 5 minutes
67 static final int MEMCHECK_DEFAULT_MIN_ALARM = DB ? 1*60 : 3*60; // 3 minutes
68 static final int MEMCHECK_DEFAULT_RECHECK_INTERVAL = DB ? 1*60 : 5*60; // 5 minutes
69
70 static final int REBOOT_DEFAULT_INTERVAL = DB ? 1 : 0; // never force reboot
71 static final int REBOOT_DEFAULT_START_TIME = 3*60*60; // 3:00am
72 static final int REBOOT_DEFAULT_WINDOW = 60*60; // within 1 hour
73
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074 static final String REBOOT_ACTION = "com.android.service.Watchdog.REBOOT";
75
Dianne Hackbornf72467a2012-06-08 17:23:59 -070076 static final String[] NATIVE_STACKS_OF_INTEREST = new String[] {
77 "/system/bin/mediaserver",
78 "/system/bin/sdcard",
79 "/system/bin/surfaceflinger"
80 };
81
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082 static Watchdog sWatchdog;
83
84 /* This handler will be used to post message back onto the main thread */
Dianne Hackborn8d044e82013-04-30 17:24:15 -070085 final ArrayList<HandlerChecker> mHandlerCheckers = new ArrayList<HandlerChecker>();
86 final HandlerChecker mMonitorChecker;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 ContentResolver mResolver;
88 BatteryService mBattery;
89 PowerManagerService mPower;
90 AlarmManagerService mAlarm;
91 ActivityManagerService mActivity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 int mPhonePid;
Dianne Hackborn5b88a2f2013-05-03 16:25:11 -070094 IActivityController mController;
Dianne Hackborn8bd64df2013-05-06 16:07:26 -070095 boolean mAllowRestart = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096
97 final Calendar mCalendar = Calendar.getInstance();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 int mMinScreenOff = MEMCHECK_DEFAULT_MIN_SCREEN_OFF;
99 int mMinAlarm = MEMCHECK_DEFAULT_MIN_ALARM;
100 boolean mNeedScheduledCheck;
101 PendingIntent mCheckupIntent;
102 PendingIntent mRebootIntent;
103
104 long mBootTime;
105 int mRebootInterval;
106
107 boolean mReqRebootNoWait; // should wait for one interval before reboot?
108 int mReqRebootInterval = -1; // >= 0 if a reboot has been requested
109 int mReqRebootStartTime = -1; // >= 0 if a specific start time has been requested
110 int mReqRebootWindow = -1; // >= 0 if a specific window has been requested
111 int mReqMinScreenOff = -1; // >= 0 if a specific screen off time has been requested
112 int mReqMinNextAlarm = -1; // >= 0 if specific time to next alarm has been requested
113 int mReqRecheckInterval= -1; // >= 0 if a specific recheck interval has been requested
114
115 /**
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700116 * Used for checking status of handle threads and scheduling monitor callbacks.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 */
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700118 public final class HandlerChecker implements Runnable {
119 private final Handler mHandler;
120 private final String mName;
121 private final ArrayList<Monitor> mMonitors = new ArrayList<Monitor>();
122 private final boolean mCheckReboot;
123 private boolean mCompleted;
124 private Monitor mCurrentMonitor;
125
126 HandlerChecker(Handler handler, String name, boolean checkReboot) {
127 mHandler = handler;
128 mName = name;
129 mCheckReboot = checkReboot;
130 }
131
132 public void addMonitor(Monitor monitor) {
133 mMonitors.add(monitor);
134 }
135
136 public void scheduleCheckLocked() {
Dianne Hackbornefa92b22013-05-03 14:11:43 -0700137 if (!mCheckReboot && mMonitors.size() == 0 && mHandler.getLooper().isIdling()) {
138 // If the target looper is or just recently was idling, then
139 // there is no reason to enqueue our checker on it since that
140 // is as good as it not being deadlocked. This avoid having
141 // to do a context switch to check the thread. Note that we
142 // only do this if mCheckReboot is false and we have no
143 // monitors, since those would need to be executed at this point.
144 mCompleted = true;
145 return;
146 }
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700147 mCompleted = false;
148 mCurrentMonitor = null;
149 mHandler.postAtFrontOfQueue(this);
150 }
151
152 public boolean isCompletedLocked() {
153 return mCompleted;
154 }
155
156 public String describeBlockedStateLocked() {
157 return mCurrentMonitor == null ? mName : mCurrentMonitor.getClass().getName();
John Michelau11641522013-03-18 18:28:23 -0500158 }
159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 @Override
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700161 public void run() {
162 // See if we should force a reboot.
163 if (mCheckReboot) {
164 int rebootInterval = mReqRebootInterval >= 0
165 ? mReqRebootInterval : REBOOT_DEFAULT_INTERVAL;
166 if (mRebootInterval != rebootInterval) {
167 mRebootInterval = rebootInterval;
168 // We have been running long enough that a reboot can
169 // be considered...
170 checkReboot(false);
171 }
172 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700174 final int size = mMonitors.size();
175 for (int i = 0 ; i < size ; i++) {
176 synchronized (Watchdog.this) {
177 mCurrentMonitor = mMonitors.get(i);
178 }
179 mCurrentMonitor.monitor();
180 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700182 synchronized (Watchdog.this) {
183 mCompleted = true;
184 mCurrentMonitor = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 }
186 }
187 }
188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 final class RebootReceiver extends BroadcastReceiver {
190 @Override
191 public void onReceive(Context c, Intent intent) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800192 if (localLOGV) Slog.v(TAG, "Alarm went off, checking reboot.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 checkReboot(true);
194 }
195 }
196
197 final class RebootRequestReceiver extends BroadcastReceiver {
198 @Override
199 public void onReceive(Context c, Intent intent) {
200 mReqRebootNoWait = intent.getIntExtra("nowait", 0) != 0;
201 mReqRebootInterval = intent.getIntExtra("interval", -1);
202 mReqRebootStartTime = intent.getIntExtra("startTime", -1);
203 mReqRebootWindow = intent.getIntExtra("window", -1);
204 mReqMinScreenOff = intent.getIntExtra("minScreenOff", -1);
205 mReqMinNextAlarm = intent.getIntExtra("minNextAlarm", -1);
206 mReqRecheckInterval = intent.getIntExtra("recheckInterval", -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800207 EventLog.writeEvent(EventLogTags.WATCHDOG_REQUESTED_REBOOT,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 mReqRebootNoWait ? 1 : 0, mReqRebootInterval,
209 mReqRecheckInterval, mReqRebootStartTime,
210 mReqRebootWindow, mReqMinScreenOff, mReqMinNextAlarm);
211 checkReboot(true);
212 }
213 }
214
215 public interface Monitor {
216 void monitor();
217 }
218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 public static Watchdog getInstance() {
220 if (sWatchdog == null) {
221 sWatchdog = new Watchdog();
222 }
223
224 return sWatchdog;
225 }
226
227 private Watchdog() {
228 super("watchdog");
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700229 // Initialize handler checkers for each common thread we want to check. Note
230 // that we are not currently checking the background thread, since it can
231 // potentially hold longer running operations with no guarantees about the timeliness
232 // of operations there.
233
234 // The shared foreground thread is the main checker. It is where we
235 // will also dispatch monitor checks and do other work.
236 mMonitorChecker = new HandlerChecker(FgThread.getHandler(), "foreground thread", true);
237 mHandlerCheckers.add(mMonitorChecker);
238 // Add checker for main thread. We only do a quick check since there
239 // can be UI running on the thread.
240 mHandlerCheckers.add(new HandlerChecker(new Handler(Looper.getMainLooper()),
241 "main thread", false));
242 // Add checker for shared UI thread.
243 mHandlerCheckers.add(new HandlerChecker(UiThread.getHandler(), "ui thread", false));
244 // And also check IO thread.
245 mHandlerCheckers.add(new HandlerChecker(IoThread.getHandler(), "i/o thread", false));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 }
247
248 public void init(Context context, BatteryService battery,
249 PowerManagerService power, AlarmManagerService alarm,
250 ActivityManagerService activity) {
251 mResolver = context.getContentResolver();
252 mBattery = battery;
253 mPower = power;
254 mAlarm = alarm;
255 mActivity = activity;
256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 context.registerReceiver(new RebootReceiver(),
258 new IntentFilter(REBOOT_ACTION));
259 mRebootIntent = PendingIntent.getBroadcast(context,
260 0, new Intent(REBOOT_ACTION), 0);
261
262 context.registerReceiver(new RebootRequestReceiver(),
263 new IntentFilter(Intent.ACTION_REBOOT),
264 android.Manifest.permission.REBOOT, null);
265
266 mBootTime = System.currentTimeMillis();
267 }
268
Christopher Tatec27181c2010-06-30 14:41:09 -0700269 public void processStarted(String name, int pid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 synchronized (this) {
271 if ("com.android.phone".equals(name)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 mPhonePid = pid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 }
274 }
275 }
276
Dianne Hackborn5b88a2f2013-05-03 16:25:11 -0700277 public void setActivityController(IActivityController controller) {
278 synchronized (this) {
279 mController = controller;
280 }
281 }
282
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700283 public void setAllowRestart(boolean allowRestart) {
284 synchronized (this) {
285 mAllowRestart = allowRestart;
286 }
287 }
288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 public void addMonitor(Monitor monitor) {
290 synchronized (this) {
291 if (isAlive()) {
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700292 throw new RuntimeException("Monitors can't be added once the Watchdog is running");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 }
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700294 mMonitorChecker.addMonitor(monitor);
295 }
296 }
297
298 public void addThread(Handler thread, String name) {
299 synchronized (this) {
300 if (isAlive()) {
301 throw new RuntimeException("Threads can't be added once the Watchdog is running");
302 }
303 mHandlerCheckers.add(new HandlerChecker(thread, name, false));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 }
305 }
306
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 void checkReboot(boolean fromAlarm) {
308 int rebootInterval = mReqRebootInterval >= 0 ? mReqRebootInterval
Jeff Sharkey4de99362012-09-26 17:58:19 -0700309 : REBOOT_DEFAULT_INTERVAL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 mRebootInterval = rebootInterval;
311 if (rebootInterval <= 0) {
312 // No reboot interval requested.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800313 if (localLOGV) Slog.v(TAG, "No need to schedule a reboot alarm!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 mAlarm.remove(mRebootIntent);
315 return;
316 }
317
318 long rebootStartTime = mReqRebootStartTime >= 0 ? mReqRebootStartTime
Jeff Sharkey4de99362012-09-26 17:58:19 -0700319 : REBOOT_DEFAULT_START_TIME;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 long rebootWindowMillis = (mReqRebootWindow >= 0 ? mReqRebootWindow
Jeff Sharkey4de99362012-09-26 17:58:19 -0700321 : REBOOT_DEFAULT_WINDOW) * 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322 long recheckInterval = (mReqRecheckInterval >= 0 ? mReqRecheckInterval
Jeff Sharkey4de99362012-09-26 17:58:19 -0700323 : MEMCHECK_DEFAULT_RECHECK_INTERVAL) * 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324
325 retrieveBrutalityAmount();
326
327 long realStartTime;
328 long now;
329
330 synchronized (this) {
331 now = System.currentTimeMillis();
332 realStartTime = computeCalendarTime(mCalendar, now,
333 rebootStartTime);
334
335 long rebootIntervalMillis = rebootInterval*24*60*60*1000;
336 if (DB || mReqRebootNoWait ||
337 (now-mBootTime) >= (rebootIntervalMillis-rebootWindowMillis)) {
338 if (fromAlarm && rebootWindowMillis <= 0) {
339 // No reboot window -- just immediately reboot.
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800340 EventLog.writeEvent(EventLogTags.WATCHDOG_SCHEDULED_REBOOT, now,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 (int)rebootIntervalMillis, (int)rebootStartTime*1000,
342 (int)rebootWindowMillis, "");
343 rebootSystem("Checkin scheduled forced");
344 return;
345 }
346
347 // Are we within the reboot window?
348 if (now < realStartTime) {
349 // Schedule alarm for next check interval.
350 realStartTime = computeCalendarTime(mCalendar,
351 now, rebootStartTime);
352 } else if (now < (realStartTime+rebootWindowMillis)) {
353 String doit = shouldWeBeBrutalLocked(now);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800354 EventLog.writeEvent(EventLogTags.WATCHDOG_SCHEDULED_REBOOT, now,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 (int)rebootInterval, (int)rebootStartTime*1000,
356 (int)rebootWindowMillis, doit != null ? doit : "");
357 if (doit == null) {
358 rebootSystem("Checked scheduled range");
359 return;
360 }
361
362 // Schedule next alarm either within the window or in the
363 // next interval.
364 if ((now+recheckInterval) >= (realStartTime+rebootWindowMillis)) {
365 realStartTime = computeCalendarTime(mCalendar,
366 now + rebootIntervalMillis, rebootStartTime);
367 } else {
368 realStartTime = now + recheckInterval;
369 }
370 } else {
371 // Schedule alarm for next check interval.
372 realStartTime = computeCalendarTime(mCalendar,
373 now + rebootIntervalMillis, rebootStartTime);
374 }
375 }
376 }
377
Joe Onorato8a9b2202010-02-26 18:56:32 -0800378 if (localLOGV) Slog.v(TAG, "Scheduling next reboot alarm for "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 + ((realStartTime-now)/1000/60) + "m from now");
380 mAlarm.remove(mRebootIntent);
381 mAlarm.set(AlarmManager.RTC_WAKEUP, realStartTime, mRebootIntent);
382 }
383
384 /**
385 * Perform a full reboot of the system.
386 */
387 void rebootSystem(String reason) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800388 Slog.i(TAG, "Rebooting system because: " + reason);
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800389 PowerManagerService pms = (PowerManagerService) ServiceManager.getService("power");
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700390 pms.reboot(false, reason, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 }
392
393 /**
394 * Load the current Gservices settings for when
395 * {@link #shouldWeBeBrutalLocked} will allow the brutality to happen.
396 * Must not be called with the lock held.
397 */
398 void retrieveBrutalityAmount() {
399 mMinScreenOff = (mReqMinScreenOff >= 0 ? mReqMinScreenOff
Jeff Sharkey4de99362012-09-26 17:58:19 -0700400 : MEMCHECK_DEFAULT_MIN_SCREEN_OFF) * 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 mMinAlarm = (mReqMinNextAlarm >= 0 ? mReqMinNextAlarm
Jeff Sharkey4de99362012-09-26 17:58:19 -0700402 : MEMCHECK_DEFAULT_MIN_ALARM) * 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 }
404
405 /**
406 * Determine whether it is a good time to kill, crash, or otherwise
407 * plunder the current situation for the overall long-term benefit of
408 * the world.
409 *
410 * @param curTime The current system time.
411 * @return Returns null if this is a good time, else a String with the
412 * text of why it is not a good time.
413 */
414 String shouldWeBeBrutalLocked(long curTime) {
Jeff Browna4d82042012-10-02 19:11:19 -0700415 if (mBattery == null || !mBattery.isPowered(BatteryManager.BATTERY_PLUGGED_ANY)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 return "battery";
417 }
418
419 if (mMinScreenOff >= 0 && (mPower == null ||
Jeff Brown96307042012-07-27 15:51:34 -0700420 mPower.timeSinceScreenWasLastOn() < mMinScreenOff)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 return "screen";
422 }
423
424 if (mMinAlarm >= 0 && (mAlarm == null ||
425 mAlarm.timeToNextAlarm() < mMinAlarm)) {
426 return "alarm";
427 }
428
429 return null;
430 }
431
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 static long computeCalendarTime(Calendar c, long curTime,
433 long secondsSinceMidnight) {
434
435 // start with now
436 c.setTimeInMillis(curTime);
437
438 int val = (int)secondsSinceMidnight / (60*60);
439 c.set(Calendar.HOUR_OF_DAY, val);
440 secondsSinceMidnight -= val * (60*60);
441 val = (int)secondsSinceMidnight / 60;
442 c.set(Calendar.MINUTE, val);
443 c.set(Calendar.SECOND, (int)secondsSinceMidnight - (val*60));
444 c.set(Calendar.MILLISECOND, 0);
445
446 long newTime = c.getTimeInMillis();
447 if (newTime < curTime) {
448 // The given time (in seconds since midnight) has already passed for today, so advance
449 // by one day (due to daylight savings, etc., the delta may differ from 24 hours).
450 c.add(Calendar.DAY_OF_MONTH, 1);
451 newTime = c.getTimeInMillis();
452 }
453
454 return newTime;
455 }
456
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700457 private boolean haveAllCheckersCompletedLocked() {
458 for (int i=0; i<mHandlerCheckers.size(); i++) {
459 HandlerChecker hc = mHandlerCheckers.get(i);
460 if (!hc.isCompletedLocked()) {
461 return false;
462 }
463 }
464 return true;
465 }
466
467 private String describeBlockedCheckersLocked() {
468 StringBuilder builder = new StringBuilder(128);
469 for (int i=0; i<mHandlerCheckers.size(); i++) {
470 HandlerChecker hc = mHandlerCheckers.get(i);
471 if (!hc.isCompletedLocked()) {
472 if (builder.length() > 0) {
473 builder.append(", ");
474 }
475 builder.append(hc.describeBlockedStateLocked());
476 }
477 }
478 return builder.toString();
479 }
480
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481 @Override
482 public void run() {
Christopher Tate6ee412d2010-05-28 12:01:56 -0700483 boolean waitedHalf = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484 while (true) {
Michael Wright8fa56f62013-04-01 16:36:05 -0700485 final String name;
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700486 final boolean allowRestart;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 synchronized (this) {
488 long timeout = TIME_TO_WAIT;
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700489 if (!waitedHalf) {
490 // If we are not at the half-point of waiting, perform a
491 // new set of checks. Otherwise we are still waiting for a previous set.
492 for (int i=0; i<mHandlerCheckers.size(); i++) {
493 HandlerChecker hc = mHandlerCheckers.get(i);
494 hc.scheduleCheckLocked();
495 }
496 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497
498 // NOTE: We use uptimeMillis() here because we do not want to increment the time we
499 // wait while asleep. If the device is asleep then the thing that we are waiting
Christopher Tate6ee412d2010-05-28 12:01:56 -0700500 // to timeout on is asleep as well and won't have a chance to run, causing a false
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 // positive on when to kill things.
502 long start = SystemClock.uptimeMillis();
Michael Wright8fa56f62013-04-01 16:36:05 -0700503 while (timeout > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 try {
Michael Wright8fa56f62013-04-01 16:36:05 -0700505 wait(timeout);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 } catch (InterruptedException e) {
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800507 Log.wtf(TAG, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 }
509 timeout = TIME_TO_WAIT - (SystemClock.uptimeMillis() - start);
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800510 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700512 if (haveAllCheckersCompletedLocked()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 // The monitors have returned.
Christopher Tate6ee412d2010-05-28 12:01:56 -0700514 waitedHalf = false;
515 continue;
516 }
517
518 if (!waitedHalf) {
519 // We've waited half the deadlock-detection interval. Pull a stack
520 // trace and wait another half.
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -0700521 ArrayList<Integer> pids = new ArrayList<Integer>();
Christopher Tate6ee412d2010-05-28 12:01:56 -0700522 pids.add(Process.myPid());
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700523 ActivityManagerService.dumpStackTraces(true, pids, null, null,
524 NATIVE_STACKS_OF_INTEREST);
Christopher Tate6ee412d2010-05-28 12:01:56 -0700525 waitedHalf = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 continue;
527 }
Michael Wright8fa56f62013-04-01 16:36:05 -0700528
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700529 name = describeBlockedCheckersLocked();
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700530 allowRestart = mAllowRestart;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 }
532
533 // If we got here, that means that the system is most likely hung.
Jean-Baptiste Queru784827b2012-09-04 13:35:12 -0700534 // First collect stack traces from all threads of the system process.
535 // Then kill this process so that the system will restart.
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800536 EventLog.writeEvent(EventLogTags.WATCHDOG, name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800537
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -0700538 ArrayList<Integer> pids = new ArrayList<Integer>();
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800539 pids.add(Process.myPid());
Dan Egnor4bded072010-03-11 22:00:47 -0800540 if (mPhonePid > 0) pids.add(mPhonePid);
Christopher Tate6ee412d2010-05-28 12:01:56 -0700541 // Pass !waitedHalf so that just in case we somehow wind up here without having
542 // dumped the halfway stacks, we properly re-initialize the trace file.
Brad Fitzpatrick9765c722011-01-14 11:28:22 -0800543 final File stack = ActivityManagerService.dumpStackTraces(
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700544 !waitedHalf, pids, null, null, NATIVE_STACKS_OF_INTEREST);
Dan Egnor4bded072010-03-11 22:00:47 -0800545
546 // Give some extra time to make sure the stack traces get written.
547 // The system's been hanging for a minute, another second or two won't hurt much.
548 SystemClock.sleep(2000);
549
Christopher Tateecaa7b42010-06-04 14:55:02 -0700550 // Pull our own kernel thread stacks as well if we're configured for that
551 if (RECORD_KERNEL_THREADS) {
552 dumpKernelStackTraces();
553 }
554
Colin Cross5df1d872012-11-29 11:42:11 -0800555 // Trigger the kernel to dump all blocked threads to the kernel log
556 try {
557 FileWriter sysrq_trigger = new FileWriter("/proc/sysrq-trigger");
558 sysrq_trigger.write("w");
559 sysrq_trigger.close();
560 } catch (IOException e) {
561 Slog.e(TAG, "Failed to write to /proc/sysrq-trigger");
562 Slog.e(TAG, e.getMessage());
563 }
564
Brad Fitzpatrick9765c722011-01-14 11:28:22 -0800565 // Try to add the error to the dropbox, but assuming that the ActivityManager
566 // itself may be deadlocked. (which has happened, causing this statement to
567 // deadlock and the watchdog as a whole to be ineffective)
568 Thread dropboxThread = new Thread("watchdogWriteToDropbox") {
569 public void run() {
570 mActivity.addErrorToDropBox(
Jeff Sharkeya353d262011-10-28 11:12:06 -0700571 "watchdog", null, "system_server", null, null,
572 name, null, stack, null);
Brad Fitzpatrick9765c722011-01-14 11:28:22 -0800573 }
574 };
575 dropboxThread.start();
576 try {
577 dropboxThread.join(2000); // wait up to 2 seconds for it to return.
578 } catch (InterruptedException ignored) {}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579
Dianne Hackborn5b88a2f2013-05-03 16:25:11 -0700580 IActivityController controller;
581 synchronized (this) {
582 controller = mController;
583 }
584 if (controller != null) {
585 Slog.i(TAG, "Reporting stuck state to activity controller");
586 try {
587 Binder.setDumpDisabled("Service dumps disabled due to hung system process.");
588 // 1 = keep waiting, -1 = kill system
589 int res = controller.systemNotResponding(name);
590 if (res >= 0) {
591 Slog.i(TAG, "Activity controller requested to coninue to wait");
592 waitedHalf = false;
593 continue;
594 }
595 } catch (RemoteException e) {
596 }
597 }
598
Jean-Baptiste Queru784827b2012-09-04 13:35:12 -0700599 // Only kill the process if the debugger is not attached.
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700600 if (Debug.isDebuggerConnected()) {
601 Slog.w(TAG, "Debugger connected: Watchdog is *not* killing the system process");
602 } else if (!allowRestart) {
603 Slog.w(TAG, "Restart not allowed: Watchdog is *not* killing the system process");
604 } else {
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800605 Slog.w(TAG, "*** WATCHDOG KILLING SYSTEM PROCESS: " + name);
Michael Wright56a6c662013-04-30 20:13:07 -0700606 Slog.w(TAG, "Main thread stack trace:");
Dianne Hackborn98eb06a2013-05-02 19:50:00 -0700607 StackTraceElement[] stackTrace = Looper.getMainLooper().getThread().getStackTrace();
Michael Wright56a6c662013-04-30 20:13:07 -0700608 for (StackTraceElement element: stackTrace) {
609 Slog.w(TAG, "\tat " + element);
610 }
611 Slog.w(TAG, "<End of main thread stack trace>");
Jean-Baptiste Queru784827b2012-09-04 13:35:12 -0700612 Process.killProcess(Process.myPid());
613 System.exit(10);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 }
Christopher Tate6ee412d2010-05-28 12:01:56 -0700615
616 waitedHalf = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 }
618 }
Christopher Tateecaa7b42010-06-04 14:55:02 -0700619
620 private File dumpKernelStackTraces() {
621 String tracesPath = SystemProperties.get("dalvik.vm.stack-trace-file", null);
622 if (tracesPath == null || tracesPath.length() == 0) {
623 return null;
624 }
625
626 native_dumpKernelStacks(tracesPath);
627 return new File(tracesPath);
628 }
629
630 private native void native_dumpKernelStacks(String tracesPath);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631}