The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server; |
| 18 | |
Dianne Hackborn | 5b88a2f | 2013-05-03 16:25:11 -0700 | [diff] [blame] | 19 | import android.app.IActivityController; |
| 20 | import android.os.Binder; |
| 21 | import android.os.RemoteException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | import com.android.server.am.ActivityManagerService; |
Jeff Brown | 4f8ecd8 | 2012-06-18 18:29:13 -0700 | [diff] [blame] | 23 | import com.android.server.power.PowerManagerService; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | |
| 25 | import android.app.AlarmManager; |
| 26 | import android.app.PendingIntent; |
| 27 | import android.content.BroadcastReceiver; |
| 28 | import android.content.ContentResolver; |
| 29 | import android.content.Context; |
| 30 | import android.content.Intent; |
| 31 | import android.content.IntentFilter; |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 32 | import android.os.BatteryManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | import android.os.Debug; |
| 34 | import android.os.Handler; |
John Michelau | 1164152 | 2013-03-18 18:28:23 -0500 | [diff] [blame] | 35 | import android.os.Looper; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | import android.os.Process; |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 37 | import android.os.ServiceManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | import android.os.SystemClock; |
| 39 | import android.os.SystemProperties; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | import android.util.EventLog; |
Dan Egnor | 9bdc94b | 2010-03-04 14:20:31 -0800 | [diff] [blame] | 41 | import android.util.Log; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 42 | import android.util.Slog; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | |
Dan Egnor | 9bdc94b | 2010-03-04 14:20:31 -0800 | [diff] [blame] | 44 | import java.io.File; |
Colin Cross | 5df1d87 | 2012-11-29 11:42:11 -0800 | [diff] [blame] | 45 | import java.io.FileWriter; |
| 46 | import java.io.IOException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | import java.util.ArrayList; |
| 48 | import java.util.Calendar; |
| 49 | |
| 50 | /** This class calls its monitor every minute. Killing this process if they don't return **/ |
| 51 | public class Watchdog extends Thread { |
| 52 | static final String TAG = "Watchdog"; |
Joe Onorato | 43a1765 | 2011-04-06 19:22:23 -0700 | [diff] [blame] | 53 | static final boolean localLOGV = false || false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | |
| 55 | // Set this to true to use debug default values. |
| 56 | static final boolean DB = false; |
| 57 | |
Christopher Tate | ecaa7b4 | 2010-06-04 14:55:02 -0700 | [diff] [blame] | 58 | // 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | static final int MONITOR = 2718; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | |
Mathias Agopian | cf2317e | 2011-08-25 17:12:37 -0700 | [diff] [blame] | 63 | static final int TIME_TO_RESTART = DB ? 15*1000 : 60*1000; |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 64 | static final int TIME_TO_WAIT = TIME_TO_RESTART / 2; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | static final String REBOOT_ACTION = "com.android.service.Watchdog.REBOOT"; |
| 75 | |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 76 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | static Watchdog sWatchdog; |
| 83 | |
| 84 | /* This handler will be used to post message back onto the main thread */ |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 85 | final ArrayList<HandlerChecker> mHandlerCheckers = new ArrayList<HandlerChecker>(); |
| 86 | final HandlerChecker mMonitorChecker; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | ContentResolver mResolver; |
| 88 | BatteryService mBattery; |
| 89 | PowerManagerService mPower; |
| 90 | AlarmManagerService mAlarm; |
| 91 | ActivityManagerService mActivity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | int mPhonePid; |
Dianne Hackborn | 5b88a2f | 2013-05-03 16:25:11 -0700 | [diff] [blame] | 94 | IActivityController mController; |
Dianne Hackborn | 8bd64df | 2013-05-06 16:07:26 -0700 | [diff] [blame] | 95 | boolean mAllowRestart = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | |
| 97 | final Calendar mCalendar = Calendar.getInstance(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | 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 Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 116 | * Used for checking status of handle threads and scheduling monitor callbacks. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | */ |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 118 | 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 Hackborn | efa92b2 | 2013-05-03 14:11:43 -0700 | [diff] [blame^] | 137 | 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 Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 147 | 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 Michelau | 1164152 | 2013-03-18 18:28:23 -0500 | [diff] [blame] | 158 | } |
| 159 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 160 | @Override |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 161 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 173 | |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 174 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 181 | |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 182 | synchronized (Watchdog.this) { |
| 183 | mCompleted = true; |
| 184 | mCurrentMonitor = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 189 | final class RebootReceiver extends BroadcastReceiver { |
| 190 | @Override |
| 191 | public void onReceive(Context c, Intent intent) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 192 | if (localLOGV) Slog.v(TAG, "Alarm went off, checking reboot."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 193 | 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 Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 207 | EventLog.writeEvent(EventLogTags.WATCHDOG_REQUESTED_REBOOT, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 208 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 219 | 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 Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 229 | // 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 246 | } |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 257 | 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 Tate | c27181c | 2010-06-30 14:41:09 -0700 | [diff] [blame] | 269 | public void processStarted(String name, int pid) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 270 | synchronized (this) { |
| 271 | if ("com.android.phone".equals(name)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 272 | mPhonePid = pid; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | } |
| 276 | |
Dianne Hackborn | 5b88a2f | 2013-05-03 16:25:11 -0700 | [diff] [blame] | 277 | public void setActivityController(IActivityController controller) { |
| 278 | synchronized (this) { |
| 279 | mController = controller; |
| 280 | } |
| 281 | } |
| 282 | |
Dianne Hackborn | 8bd64df | 2013-05-06 16:07:26 -0700 | [diff] [blame] | 283 | public void setAllowRestart(boolean allowRestart) { |
| 284 | synchronized (this) { |
| 285 | mAllowRestart = allowRestart; |
| 286 | } |
| 287 | } |
| 288 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 289 | public void addMonitor(Monitor monitor) { |
| 290 | synchronized (this) { |
| 291 | if (isAlive()) { |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 292 | throw new RuntimeException("Monitors can't be added once the Watchdog is running"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 293 | } |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 294 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 304 | } |
| 305 | } |
| 306 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 307 | void checkReboot(boolean fromAlarm) { |
| 308 | int rebootInterval = mReqRebootInterval >= 0 ? mReqRebootInterval |
Jeff Sharkey | 4de9936 | 2012-09-26 17:58:19 -0700 | [diff] [blame] | 309 | : REBOOT_DEFAULT_INTERVAL; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 310 | mRebootInterval = rebootInterval; |
| 311 | if (rebootInterval <= 0) { |
| 312 | // No reboot interval requested. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 313 | if (localLOGV) Slog.v(TAG, "No need to schedule a reboot alarm!"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 314 | mAlarm.remove(mRebootIntent); |
| 315 | return; |
| 316 | } |
| 317 | |
| 318 | long rebootStartTime = mReqRebootStartTime >= 0 ? mReqRebootStartTime |
Jeff Sharkey | 4de9936 | 2012-09-26 17:58:19 -0700 | [diff] [blame] | 319 | : REBOOT_DEFAULT_START_TIME; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 320 | long rebootWindowMillis = (mReqRebootWindow >= 0 ? mReqRebootWindow |
Jeff Sharkey | 4de9936 | 2012-09-26 17:58:19 -0700 | [diff] [blame] | 321 | : REBOOT_DEFAULT_WINDOW) * 1000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 322 | long recheckInterval = (mReqRecheckInterval >= 0 ? mReqRecheckInterval |
Jeff Sharkey | 4de9936 | 2012-09-26 17:58:19 -0700 | [diff] [blame] | 323 | : MEMCHECK_DEFAULT_RECHECK_INTERVAL) * 1000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 324 | |
| 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 Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 340 | EventLog.writeEvent(EventLogTags.WATCHDOG_SCHEDULED_REBOOT, now, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 341 | (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 Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 354 | EventLog.writeEvent(EventLogTags.WATCHDOG_SCHEDULED_REBOOT, now, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 355 | (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 Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 378 | if (localLOGV) Slog.v(TAG, "Scheduling next reboot alarm for " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 379 | + ((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 Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 388 | Slog.i(TAG, "Rebooting system because: " + reason); |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 389 | PowerManagerService pms = (PowerManagerService) ServiceManager.getService("power"); |
Dianne Hackborn | c428aae | 2012-10-03 16:38:22 -0700 | [diff] [blame] | 390 | pms.reboot(false, reason, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 391 | } |
| 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 Sharkey | 4de9936 | 2012-09-26 17:58:19 -0700 | [diff] [blame] | 400 | : MEMCHECK_DEFAULT_MIN_SCREEN_OFF) * 1000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 401 | mMinAlarm = (mReqMinNextAlarm >= 0 ? mReqMinNextAlarm |
Jeff Sharkey | 4de9936 | 2012-09-26 17:58:19 -0700 | [diff] [blame] | 402 | : MEMCHECK_DEFAULT_MIN_ALARM) * 1000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 403 | } |
| 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 Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 415 | if (mBattery == null || !mBattery.isPowered(BatteryManager.BATTERY_PLUGGED_ANY)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 416 | return "battery"; |
| 417 | } |
| 418 | |
| 419 | if (mMinScreenOff >= 0 && (mPower == null || |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 420 | mPower.timeSinceScreenWasLastOn() < mMinScreenOff)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 421 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 432 | 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 Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 457 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 481 | @Override |
| 482 | public void run() { |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 483 | boolean waitedHalf = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 484 | while (true) { |
Michael Wright | 8fa56f6 | 2013-04-01 16:36:05 -0700 | [diff] [blame] | 485 | final String name; |
Dianne Hackborn | 8bd64df | 2013-05-06 16:07:26 -0700 | [diff] [blame] | 486 | final boolean allowRestart; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 487 | synchronized (this) { |
| 488 | long timeout = TIME_TO_WAIT; |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 489 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 497 | |
| 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 Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 500 | // to timeout on is asleep as well and won't have a chance to run, causing a false |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 501 | // positive on when to kill things. |
| 502 | long start = SystemClock.uptimeMillis(); |
Michael Wright | 8fa56f6 | 2013-04-01 16:36:05 -0700 | [diff] [blame] | 503 | while (timeout > 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 504 | try { |
Michael Wright | 8fa56f6 | 2013-04-01 16:36:05 -0700 | [diff] [blame] | 505 | wait(timeout); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 506 | } catch (InterruptedException e) { |
Dan Egnor | 9bdc94b | 2010-03-04 14:20:31 -0800 | [diff] [blame] | 507 | Log.wtf(TAG, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 508 | } |
| 509 | timeout = TIME_TO_WAIT - (SystemClock.uptimeMillis() - start); |
Dan Egnor | 9bdc94b | 2010-03-04 14:20:31 -0800 | [diff] [blame] | 510 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 511 | |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 512 | if (haveAllCheckersCompletedLocked()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 513 | // The monitors have returned. |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 514 | 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 Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 521 | ArrayList<Integer> pids = new ArrayList<Integer>(); |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 522 | pids.add(Process.myPid()); |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 523 | ActivityManagerService.dumpStackTraces(true, pids, null, null, |
| 524 | NATIVE_STACKS_OF_INTEREST); |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 525 | waitedHalf = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 526 | continue; |
| 527 | } |
Michael Wright | 8fa56f6 | 2013-04-01 16:36:05 -0700 | [diff] [blame] | 528 | |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 529 | name = describeBlockedCheckersLocked(); |
Dianne Hackborn | 8bd64df | 2013-05-06 16:07:26 -0700 | [diff] [blame] | 530 | allowRestart = mAllowRestart; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | // If we got here, that means that the system is most likely hung. |
Jean-Baptiste Queru | 784827b | 2012-09-04 13:35:12 -0700 | [diff] [blame] | 534 | // First collect stack traces from all threads of the system process. |
| 535 | // Then kill this process so that the system will restart. |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 536 | EventLog.writeEvent(EventLogTags.WATCHDOG, name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 537 | |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 538 | ArrayList<Integer> pids = new ArrayList<Integer>(); |
Dan Egnor | 9bdc94b | 2010-03-04 14:20:31 -0800 | [diff] [blame] | 539 | pids.add(Process.myPid()); |
Dan Egnor | 4bded07 | 2010-03-11 22:00:47 -0800 | [diff] [blame] | 540 | if (mPhonePid > 0) pids.add(mPhonePid); |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 541 | // 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 Fitzpatrick | 9765c72 | 2011-01-14 11:28:22 -0800 | [diff] [blame] | 543 | final File stack = ActivityManagerService.dumpStackTraces( |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 544 | !waitedHalf, pids, null, null, NATIVE_STACKS_OF_INTEREST); |
Dan Egnor | 4bded07 | 2010-03-11 22:00:47 -0800 | [diff] [blame] | 545 | |
| 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 Tate | ecaa7b4 | 2010-06-04 14:55:02 -0700 | [diff] [blame] | 550 | // Pull our own kernel thread stacks as well if we're configured for that |
| 551 | if (RECORD_KERNEL_THREADS) { |
| 552 | dumpKernelStackTraces(); |
| 553 | } |
| 554 | |
Colin Cross | 5df1d87 | 2012-11-29 11:42:11 -0800 | [diff] [blame] | 555 | // 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 Fitzpatrick | 9765c72 | 2011-01-14 11:28:22 -0800 | [diff] [blame] | 565 | // 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 Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 571 | "watchdog", null, "system_server", null, null, |
| 572 | name, null, stack, null); |
Brad Fitzpatrick | 9765c72 | 2011-01-14 11:28:22 -0800 | [diff] [blame] | 573 | } |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 579 | |
Dianne Hackborn | 5b88a2f | 2013-05-03 16:25:11 -0700 | [diff] [blame] | 580 | 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 Queru | 784827b | 2012-09-04 13:35:12 -0700 | [diff] [blame] | 599 | // Only kill the process if the debugger is not attached. |
Dianne Hackborn | 8bd64df | 2013-05-06 16:07:26 -0700 | [diff] [blame] | 600 | 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 Egnor | 9bdc94b | 2010-03-04 14:20:31 -0800 | [diff] [blame] | 605 | Slog.w(TAG, "*** WATCHDOG KILLING SYSTEM PROCESS: " + name); |
Michael Wright | 56a6c66 | 2013-04-30 20:13:07 -0700 | [diff] [blame] | 606 | Slog.w(TAG, "Main thread stack trace:"); |
Dianne Hackborn | 98eb06a | 2013-05-02 19:50:00 -0700 | [diff] [blame] | 607 | StackTraceElement[] stackTrace = Looper.getMainLooper().getThread().getStackTrace(); |
Michael Wright | 56a6c66 | 2013-04-30 20:13:07 -0700 | [diff] [blame] | 608 | for (StackTraceElement element: stackTrace) { |
| 609 | Slog.w(TAG, "\tat " + element); |
| 610 | } |
| 611 | Slog.w(TAG, "<End of main thread stack trace>"); |
Jean-Baptiste Queru | 784827b | 2012-09-04 13:35:12 -0700 | [diff] [blame] | 612 | Process.killProcess(Process.myPid()); |
| 613 | System.exit(10); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 614 | } |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 615 | |
| 616 | waitedHalf = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 617 | } |
| 618 | } |
Christopher Tate | ecaa7b4 | 2010-06-04 14:55:02 -0700 | [diff] [blame] | 619 | |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 631 | } |