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 | |
| 19 | import com.android.server.am.ActivityManagerService; |
Jeff Brown | 4f8ecd8 | 2012-06-18 18:29:13 -0700 | [diff] [blame] | 20 | import com.android.server.power.PowerManagerService; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | |
| 22 | import android.app.AlarmManager; |
| 23 | import android.app.PendingIntent; |
| 24 | import android.content.BroadcastReceiver; |
| 25 | import android.content.ContentResolver; |
| 26 | import android.content.Context; |
| 27 | import android.content.Intent; |
| 28 | import android.content.IntentFilter; |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 29 | import android.os.BatteryManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | import android.os.Debug; |
| 31 | import android.os.Handler; |
John Michelau | 1164152 | 2013-03-18 18:28:23 -0500 | [diff] [blame] | 32 | import android.os.Looper; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | import android.os.Message; |
| 34 | import android.os.Process; |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 35 | import android.os.ServiceManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | import android.os.SystemClock; |
| 37 | import android.os.SystemProperties; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | import android.util.EventLog; |
Dan Egnor | 9bdc94b | 2010-03-04 14:20:31 -0800 | [diff] [blame] | 39 | import android.util.Log; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 40 | import android.util.Slog; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | |
Dan Egnor | 9bdc94b | 2010-03-04 14:20:31 -0800 | [diff] [blame] | 42 | import java.io.File; |
Colin Cross | 5df1d87 | 2012-11-29 11:42:11 -0800 | [diff] [blame] | 43 | import java.io.FileWriter; |
| 44 | import java.io.IOException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | import java.util.ArrayList; |
| 46 | import java.util.Calendar; |
| 47 | |
| 48 | /** This class calls its monitor every minute. Killing this process if they don't return **/ |
| 49 | public class Watchdog extends Thread { |
| 50 | static final String TAG = "Watchdog"; |
Joe Onorato | 43a1765 | 2011-04-06 19:22:23 -0700 | [diff] [blame] | 51 | static final boolean localLOGV = false || false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | |
| 53 | // Set this to true to use debug default values. |
| 54 | static final boolean DB = false; |
| 55 | |
Christopher Tate | ecaa7b4 | 2010-06-04 14:55:02 -0700 | [diff] [blame] | 56 | // Set this to true to have the watchdog record kernel thread stacks when it fires |
| 57 | static final boolean RECORD_KERNEL_THREADS = true; |
| 58 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | static final int MONITOR = 2718; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | |
Mathias Agopian | cf2317e | 2011-08-25 17:12:37 -0700 | [diff] [blame] | 61 | static final int TIME_TO_RESTART = DB ? 15*1000 : 60*1000; |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 62 | 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] | 63 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | static final int MEMCHECK_DEFAULT_MIN_SCREEN_OFF = DB ? 1*60 : 5*60; // 5 minutes |
| 65 | static final int MEMCHECK_DEFAULT_MIN_ALARM = DB ? 1*60 : 3*60; // 3 minutes |
| 66 | static final int MEMCHECK_DEFAULT_RECHECK_INTERVAL = DB ? 1*60 : 5*60; // 5 minutes |
| 67 | |
| 68 | static final int REBOOT_DEFAULT_INTERVAL = DB ? 1 : 0; // never force reboot |
| 69 | static final int REBOOT_DEFAULT_START_TIME = 3*60*60; // 3:00am |
| 70 | static final int REBOOT_DEFAULT_WINDOW = 60*60; // within 1 hour |
| 71 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | static final String REBOOT_ACTION = "com.android.service.Watchdog.REBOOT"; |
| 73 | |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 74 | static final String[] NATIVE_STACKS_OF_INTEREST = new String[] { |
| 75 | "/system/bin/mediaserver", |
| 76 | "/system/bin/sdcard", |
| 77 | "/system/bin/surfaceflinger" |
| 78 | }; |
| 79 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | static Watchdog sWatchdog; |
| 81 | |
| 82 | /* 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^] | 83 | final ArrayList<HandlerChecker> mHandlerCheckers = new ArrayList<HandlerChecker>(); |
| 84 | final HandlerChecker mMonitorChecker; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | ContentResolver mResolver; |
| 86 | BatteryService mBattery; |
| 87 | PowerManagerService mPower; |
| 88 | AlarmManagerService mAlarm; |
| 89 | ActivityManagerService mActivity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | int mPhonePid; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | |
| 93 | final Calendar mCalendar = Calendar.getInstance(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | int mMinScreenOff = MEMCHECK_DEFAULT_MIN_SCREEN_OFF; |
| 95 | int mMinAlarm = MEMCHECK_DEFAULT_MIN_ALARM; |
| 96 | boolean mNeedScheduledCheck; |
| 97 | PendingIntent mCheckupIntent; |
| 98 | PendingIntent mRebootIntent; |
| 99 | |
| 100 | long mBootTime; |
| 101 | int mRebootInterval; |
| 102 | |
| 103 | boolean mReqRebootNoWait; // should wait for one interval before reboot? |
| 104 | int mReqRebootInterval = -1; // >= 0 if a reboot has been requested |
| 105 | int mReqRebootStartTime = -1; // >= 0 if a specific start time has been requested |
| 106 | int mReqRebootWindow = -1; // >= 0 if a specific window has been requested |
| 107 | int mReqMinScreenOff = -1; // >= 0 if a specific screen off time has been requested |
| 108 | int mReqMinNextAlarm = -1; // >= 0 if specific time to next alarm has been requested |
| 109 | int mReqRecheckInterval= -1; // >= 0 if a specific recheck interval has been requested |
| 110 | |
| 111 | /** |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame^] | 112 | * 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] | 113 | */ |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame^] | 114 | public final class HandlerChecker implements Runnable { |
| 115 | private final Handler mHandler; |
| 116 | private final String mName; |
| 117 | private final ArrayList<Monitor> mMonitors = new ArrayList<Monitor>(); |
| 118 | private final boolean mCheckReboot; |
| 119 | private boolean mCompleted; |
| 120 | private Monitor mCurrentMonitor; |
| 121 | |
| 122 | HandlerChecker(Handler handler, String name, boolean checkReboot) { |
| 123 | mHandler = handler; |
| 124 | mName = name; |
| 125 | mCheckReboot = checkReboot; |
| 126 | } |
| 127 | |
| 128 | public void addMonitor(Monitor monitor) { |
| 129 | mMonitors.add(monitor); |
| 130 | } |
| 131 | |
| 132 | public void scheduleCheckLocked() { |
| 133 | mCompleted = false; |
| 134 | mCurrentMonitor = null; |
| 135 | mHandler.postAtFrontOfQueue(this); |
| 136 | } |
| 137 | |
| 138 | public boolean isCompletedLocked() { |
| 139 | return mCompleted; |
| 140 | } |
| 141 | |
| 142 | public String describeBlockedStateLocked() { |
| 143 | return mCurrentMonitor == null ? mName : mCurrentMonitor.getClass().getName(); |
John Michelau | 1164152 | 2013-03-18 18:28:23 -0500 | [diff] [blame] | 144 | } |
| 145 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 146 | @Override |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame^] | 147 | public void run() { |
| 148 | // See if we should force a reboot. |
| 149 | if (mCheckReboot) { |
| 150 | int rebootInterval = mReqRebootInterval >= 0 |
| 151 | ? mReqRebootInterval : REBOOT_DEFAULT_INTERVAL; |
| 152 | if (mRebootInterval != rebootInterval) { |
| 153 | mRebootInterval = rebootInterval; |
| 154 | // We have been running long enough that a reboot can |
| 155 | // be considered... |
| 156 | checkReboot(false); |
| 157 | } |
| 158 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame^] | 160 | final int size = mMonitors.size(); |
| 161 | for (int i = 0 ; i < size ; i++) { |
| 162 | synchronized (Watchdog.this) { |
| 163 | mCurrentMonitor = mMonitors.get(i); |
| 164 | } |
| 165 | mCurrentMonitor.monitor(); |
| 166 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 167 | |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame^] | 168 | synchronized (Watchdog.this) { |
| 169 | mCompleted = true; |
| 170 | mCurrentMonitor = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 175 | final class RebootReceiver extends BroadcastReceiver { |
| 176 | @Override |
| 177 | public void onReceive(Context c, Intent intent) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 178 | 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] | 179 | checkReboot(true); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | final class RebootRequestReceiver extends BroadcastReceiver { |
| 184 | @Override |
| 185 | public void onReceive(Context c, Intent intent) { |
| 186 | mReqRebootNoWait = intent.getIntExtra("nowait", 0) != 0; |
| 187 | mReqRebootInterval = intent.getIntExtra("interval", -1); |
| 188 | mReqRebootStartTime = intent.getIntExtra("startTime", -1); |
| 189 | mReqRebootWindow = intent.getIntExtra("window", -1); |
| 190 | mReqMinScreenOff = intent.getIntExtra("minScreenOff", -1); |
| 191 | mReqMinNextAlarm = intent.getIntExtra("minNextAlarm", -1); |
| 192 | mReqRecheckInterval = intent.getIntExtra("recheckInterval", -1); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 193 | EventLog.writeEvent(EventLogTags.WATCHDOG_REQUESTED_REBOOT, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 194 | mReqRebootNoWait ? 1 : 0, mReqRebootInterval, |
| 195 | mReqRecheckInterval, mReqRebootStartTime, |
| 196 | mReqRebootWindow, mReqMinScreenOff, mReqMinNextAlarm); |
| 197 | checkReboot(true); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | public interface Monitor { |
| 202 | void monitor(); |
| 203 | } |
| 204 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 205 | public static Watchdog getInstance() { |
| 206 | if (sWatchdog == null) { |
| 207 | sWatchdog = new Watchdog(); |
| 208 | } |
| 209 | |
| 210 | return sWatchdog; |
| 211 | } |
| 212 | |
| 213 | private Watchdog() { |
| 214 | super("watchdog"); |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame^] | 215 | // Initialize handler checkers for each common thread we want to check. Note |
| 216 | // that we are not currently checking the background thread, since it can |
| 217 | // potentially hold longer running operations with no guarantees about the timeliness |
| 218 | // of operations there. |
| 219 | |
| 220 | // The shared foreground thread is the main checker. It is where we |
| 221 | // will also dispatch monitor checks and do other work. |
| 222 | mMonitorChecker = new HandlerChecker(FgThread.getHandler(), "foreground thread", true); |
| 223 | mHandlerCheckers.add(mMonitorChecker); |
| 224 | // Add checker for main thread. We only do a quick check since there |
| 225 | // can be UI running on the thread. |
| 226 | mHandlerCheckers.add(new HandlerChecker(new Handler(Looper.getMainLooper()), |
| 227 | "main thread", false)); |
| 228 | // Add checker for shared UI thread. |
| 229 | mHandlerCheckers.add(new HandlerChecker(UiThread.getHandler(), "ui thread", false)); |
| 230 | // And also check IO thread. |
| 231 | 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] | 232 | } |
| 233 | |
| 234 | public void init(Context context, BatteryService battery, |
| 235 | PowerManagerService power, AlarmManagerService alarm, |
| 236 | ActivityManagerService activity) { |
| 237 | mResolver = context.getContentResolver(); |
| 238 | mBattery = battery; |
| 239 | mPower = power; |
| 240 | mAlarm = alarm; |
| 241 | mActivity = activity; |
| 242 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 243 | context.registerReceiver(new RebootReceiver(), |
| 244 | new IntentFilter(REBOOT_ACTION)); |
| 245 | mRebootIntent = PendingIntent.getBroadcast(context, |
| 246 | 0, new Intent(REBOOT_ACTION), 0); |
| 247 | |
| 248 | context.registerReceiver(new RebootRequestReceiver(), |
| 249 | new IntentFilter(Intent.ACTION_REBOOT), |
| 250 | android.Manifest.permission.REBOOT, null); |
| 251 | |
| 252 | mBootTime = System.currentTimeMillis(); |
| 253 | } |
| 254 | |
Christopher Tate | c27181c | 2010-06-30 14:41:09 -0700 | [diff] [blame] | 255 | public void processStarted(String name, int pid) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 256 | synchronized (this) { |
| 257 | if ("com.android.phone".equals(name)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 258 | mPhonePid = pid; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | public void addMonitor(Monitor monitor) { |
| 264 | synchronized (this) { |
| 265 | if (isAlive()) { |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame^] | 266 | 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] | 267 | } |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame^] | 268 | mMonitorChecker.addMonitor(monitor); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | public void addThread(Handler thread, String name) { |
| 273 | synchronized (this) { |
| 274 | if (isAlive()) { |
| 275 | throw new RuntimeException("Threads can't be added once the Watchdog is running"); |
| 276 | } |
| 277 | mHandlerCheckers.add(new HandlerChecker(thread, name, false)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 281 | void checkReboot(boolean fromAlarm) { |
| 282 | int rebootInterval = mReqRebootInterval >= 0 ? mReqRebootInterval |
Jeff Sharkey | 4de9936 | 2012-09-26 17:58:19 -0700 | [diff] [blame] | 283 | : REBOOT_DEFAULT_INTERVAL; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 284 | mRebootInterval = rebootInterval; |
| 285 | if (rebootInterval <= 0) { |
| 286 | // No reboot interval requested. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 287 | 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] | 288 | mAlarm.remove(mRebootIntent); |
| 289 | return; |
| 290 | } |
| 291 | |
| 292 | long rebootStartTime = mReqRebootStartTime >= 0 ? mReqRebootStartTime |
Jeff Sharkey | 4de9936 | 2012-09-26 17:58:19 -0700 | [diff] [blame] | 293 | : REBOOT_DEFAULT_START_TIME; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 294 | long rebootWindowMillis = (mReqRebootWindow >= 0 ? mReqRebootWindow |
Jeff Sharkey | 4de9936 | 2012-09-26 17:58:19 -0700 | [diff] [blame] | 295 | : REBOOT_DEFAULT_WINDOW) * 1000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 296 | long recheckInterval = (mReqRecheckInterval >= 0 ? mReqRecheckInterval |
Jeff Sharkey | 4de9936 | 2012-09-26 17:58:19 -0700 | [diff] [blame] | 297 | : MEMCHECK_DEFAULT_RECHECK_INTERVAL) * 1000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 298 | |
| 299 | retrieveBrutalityAmount(); |
| 300 | |
| 301 | long realStartTime; |
| 302 | long now; |
| 303 | |
| 304 | synchronized (this) { |
| 305 | now = System.currentTimeMillis(); |
| 306 | realStartTime = computeCalendarTime(mCalendar, now, |
| 307 | rebootStartTime); |
| 308 | |
| 309 | long rebootIntervalMillis = rebootInterval*24*60*60*1000; |
| 310 | if (DB || mReqRebootNoWait || |
| 311 | (now-mBootTime) >= (rebootIntervalMillis-rebootWindowMillis)) { |
| 312 | if (fromAlarm && rebootWindowMillis <= 0) { |
| 313 | // No reboot window -- just immediately reboot. |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 314 | EventLog.writeEvent(EventLogTags.WATCHDOG_SCHEDULED_REBOOT, now, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 315 | (int)rebootIntervalMillis, (int)rebootStartTime*1000, |
| 316 | (int)rebootWindowMillis, ""); |
| 317 | rebootSystem("Checkin scheduled forced"); |
| 318 | return; |
| 319 | } |
| 320 | |
| 321 | // Are we within the reboot window? |
| 322 | if (now < realStartTime) { |
| 323 | // Schedule alarm for next check interval. |
| 324 | realStartTime = computeCalendarTime(mCalendar, |
| 325 | now, rebootStartTime); |
| 326 | } else if (now < (realStartTime+rebootWindowMillis)) { |
| 327 | String doit = shouldWeBeBrutalLocked(now); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 328 | EventLog.writeEvent(EventLogTags.WATCHDOG_SCHEDULED_REBOOT, now, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 329 | (int)rebootInterval, (int)rebootStartTime*1000, |
| 330 | (int)rebootWindowMillis, doit != null ? doit : ""); |
| 331 | if (doit == null) { |
| 332 | rebootSystem("Checked scheduled range"); |
| 333 | return; |
| 334 | } |
| 335 | |
| 336 | // Schedule next alarm either within the window or in the |
| 337 | // next interval. |
| 338 | if ((now+recheckInterval) >= (realStartTime+rebootWindowMillis)) { |
| 339 | realStartTime = computeCalendarTime(mCalendar, |
| 340 | now + rebootIntervalMillis, rebootStartTime); |
| 341 | } else { |
| 342 | realStartTime = now + recheckInterval; |
| 343 | } |
| 344 | } else { |
| 345 | // Schedule alarm for next check interval. |
| 346 | realStartTime = computeCalendarTime(mCalendar, |
| 347 | now + rebootIntervalMillis, rebootStartTime); |
| 348 | } |
| 349 | } |
| 350 | } |
| 351 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 352 | 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] | 353 | + ((realStartTime-now)/1000/60) + "m from now"); |
| 354 | mAlarm.remove(mRebootIntent); |
| 355 | mAlarm.set(AlarmManager.RTC_WAKEUP, realStartTime, mRebootIntent); |
| 356 | } |
| 357 | |
| 358 | /** |
| 359 | * Perform a full reboot of the system. |
| 360 | */ |
| 361 | void rebootSystem(String reason) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 362 | Slog.i(TAG, "Rebooting system because: " + reason); |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 363 | PowerManagerService pms = (PowerManagerService) ServiceManager.getService("power"); |
Dianne Hackborn | c428aae | 2012-10-03 16:38:22 -0700 | [diff] [blame] | 364 | pms.reboot(false, reason, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | /** |
| 368 | * Load the current Gservices settings for when |
| 369 | * {@link #shouldWeBeBrutalLocked} will allow the brutality to happen. |
| 370 | * Must not be called with the lock held. |
| 371 | */ |
| 372 | void retrieveBrutalityAmount() { |
| 373 | mMinScreenOff = (mReqMinScreenOff >= 0 ? mReqMinScreenOff |
Jeff Sharkey | 4de9936 | 2012-09-26 17:58:19 -0700 | [diff] [blame] | 374 | : MEMCHECK_DEFAULT_MIN_SCREEN_OFF) * 1000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 375 | mMinAlarm = (mReqMinNextAlarm >= 0 ? mReqMinNextAlarm |
Jeff Sharkey | 4de9936 | 2012-09-26 17:58:19 -0700 | [diff] [blame] | 376 | : MEMCHECK_DEFAULT_MIN_ALARM) * 1000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | /** |
| 380 | * Determine whether it is a good time to kill, crash, or otherwise |
| 381 | * plunder the current situation for the overall long-term benefit of |
| 382 | * the world. |
| 383 | * |
| 384 | * @param curTime The current system time. |
| 385 | * @return Returns null if this is a good time, else a String with the |
| 386 | * text of why it is not a good time. |
| 387 | */ |
| 388 | String shouldWeBeBrutalLocked(long curTime) { |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 389 | if (mBattery == null || !mBattery.isPowered(BatteryManager.BATTERY_PLUGGED_ANY)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 390 | return "battery"; |
| 391 | } |
| 392 | |
| 393 | if (mMinScreenOff >= 0 && (mPower == null || |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 394 | mPower.timeSinceScreenWasLastOn() < mMinScreenOff)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 395 | return "screen"; |
| 396 | } |
| 397 | |
| 398 | if (mMinAlarm >= 0 && (mAlarm == null || |
| 399 | mAlarm.timeToNextAlarm() < mMinAlarm)) { |
| 400 | return "alarm"; |
| 401 | } |
| 402 | |
| 403 | return null; |
| 404 | } |
| 405 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 406 | static long computeCalendarTime(Calendar c, long curTime, |
| 407 | long secondsSinceMidnight) { |
| 408 | |
| 409 | // start with now |
| 410 | c.setTimeInMillis(curTime); |
| 411 | |
| 412 | int val = (int)secondsSinceMidnight / (60*60); |
| 413 | c.set(Calendar.HOUR_OF_DAY, val); |
| 414 | secondsSinceMidnight -= val * (60*60); |
| 415 | val = (int)secondsSinceMidnight / 60; |
| 416 | c.set(Calendar.MINUTE, val); |
| 417 | c.set(Calendar.SECOND, (int)secondsSinceMidnight - (val*60)); |
| 418 | c.set(Calendar.MILLISECOND, 0); |
| 419 | |
| 420 | long newTime = c.getTimeInMillis(); |
| 421 | if (newTime < curTime) { |
| 422 | // The given time (in seconds since midnight) has already passed for today, so advance |
| 423 | // by one day (due to daylight savings, etc., the delta may differ from 24 hours). |
| 424 | c.add(Calendar.DAY_OF_MONTH, 1); |
| 425 | newTime = c.getTimeInMillis(); |
| 426 | } |
| 427 | |
| 428 | return newTime; |
| 429 | } |
| 430 | |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame^] | 431 | private boolean haveAllCheckersCompletedLocked() { |
| 432 | for (int i=0; i<mHandlerCheckers.size(); i++) { |
| 433 | HandlerChecker hc = mHandlerCheckers.get(i); |
| 434 | if (!hc.isCompletedLocked()) { |
| 435 | return false; |
| 436 | } |
| 437 | } |
| 438 | return true; |
| 439 | } |
| 440 | |
| 441 | private String describeBlockedCheckersLocked() { |
| 442 | StringBuilder builder = new StringBuilder(128); |
| 443 | for (int i=0; i<mHandlerCheckers.size(); i++) { |
| 444 | HandlerChecker hc = mHandlerCheckers.get(i); |
| 445 | if (!hc.isCompletedLocked()) { |
| 446 | if (builder.length() > 0) { |
| 447 | builder.append(", "); |
| 448 | } |
| 449 | builder.append(hc.describeBlockedStateLocked()); |
| 450 | } |
| 451 | } |
| 452 | return builder.toString(); |
| 453 | } |
| 454 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 455 | @Override |
| 456 | public void run() { |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 457 | boolean waitedHalf = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 458 | while (true) { |
Michael Wright | 8fa56f6 | 2013-04-01 16:36:05 -0700 | [diff] [blame] | 459 | final String name; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 460 | synchronized (this) { |
| 461 | long timeout = TIME_TO_WAIT; |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame^] | 462 | if (!waitedHalf) { |
| 463 | // If we are not at the half-point of waiting, perform a |
| 464 | // new set of checks. Otherwise we are still waiting for a previous set. |
| 465 | for (int i=0; i<mHandlerCheckers.size(); i++) { |
| 466 | HandlerChecker hc = mHandlerCheckers.get(i); |
| 467 | hc.scheduleCheckLocked(); |
| 468 | } |
| 469 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 470 | |
| 471 | // NOTE: We use uptimeMillis() here because we do not want to increment the time we |
| 472 | // 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] | 473 | // 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] | 474 | // positive on when to kill things. |
| 475 | long start = SystemClock.uptimeMillis(); |
Michael Wright | 8fa56f6 | 2013-04-01 16:36:05 -0700 | [diff] [blame] | 476 | while (timeout > 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 477 | try { |
Michael Wright | 8fa56f6 | 2013-04-01 16:36:05 -0700 | [diff] [blame] | 478 | wait(timeout); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 479 | } catch (InterruptedException e) { |
Dan Egnor | 9bdc94b | 2010-03-04 14:20:31 -0800 | [diff] [blame] | 480 | Log.wtf(TAG, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 481 | } |
| 482 | timeout = TIME_TO_WAIT - (SystemClock.uptimeMillis() - start); |
Dan Egnor | 9bdc94b | 2010-03-04 14:20:31 -0800 | [diff] [blame] | 483 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 484 | |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame^] | 485 | if (haveAllCheckersCompletedLocked()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 486 | // The monitors have returned. |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 487 | waitedHalf = false; |
| 488 | continue; |
| 489 | } |
| 490 | |
| 491 | if (!waitedHalf) { |
| 492 | // We've waited half the deadlock-detection interval. Pull a stack |
| 493 | // trace and wait another half. |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 494 | ArrayList<Integer> pids = new ArrayList<Integer>(); |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 495 | pids.add(Process.myPid()); |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 496 | ActivityManagerService.dumpStackTraces(true, pids, null, null, |
| 497 | NATIVE_STACKS_OF_INTEREST); |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 498 | waitedHalf = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 499 | continue; |
| 500 | } |
Michael Wright | 8fa56f6 | 2013-04-01 16:36:05 -0700 | [diff] [blame] | 501 | |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame^] | 502 | name = describeBlockedCheckersLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | // 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] | 506 | // First collect stack traces from all threads of the system process. |
| 507 | // Then kill this process so that the system will restart. |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 508 | EventLog.writeEvent(EventLogTags.WATCHDOG, name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 509 | |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 510 | ArrayList<Integer> pids = new ArrayList<Integer>(); |
Dan Egnor | 9bdc94b | 2010-03-04 14:20:31 -0800 | [diff] [blame] | 511 | pids.add(Process.myPid()); |
Dan Egnor | 4bded07 | 2010-03-11 22:00:47 -0800 | [diff] [blame] | 512 | if (mPhonePid > 0) pids.add(mPhonePid); |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 513 | // Pass !waitedHalf so that just in case we somehow wind up here without having |
| 514 | // dumped the halfway stacks, we properly re-initialize the trace file. |
Brad Fitzpatrick | 9765c72 | 2011-01-14 11:28:22 -0800 | [diff] [blame] | 515 | final File stack = ActivityManagerService.dumpStackTraces( |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 516 | !waitedHalf, pids, null, null, NATIVE_STACKS_OF_INTEREST); |
Dan Egnor | 4bded07 | 2010-03-11 22:00:47 -0800 | [diff] [blame] | 517 | |
| 518 | // Give some extra time to make sure the stack traces get written. |
| 519 | // The system's been hanging for a minute, another second or two won't hurt much. |
| 520 | SystemClock.sleep(2000); |
| 521 | |
Christopher Tate | ecaa7b4 | 2010-06-04 14:55:02 -0700 | [diff] [blame] | 522 | // Pull our own kernel thread stacks as well if we're configured for that |
| 523 | if (RECORD_KERNEL_THREADS) { |
| 524 | dumpKernelStackTraces(); |
| 525 | } |
| 526 | |
Colin Cross | 5df1d87 | 2012-11-29 11:42:11 -0800 | [diff] [blame] | 527 | // Trigger the kernel to dump all blocked threads to the kernel log |
| 528 | try { |
| 529 | FileWriter sysrq_trigger = new FileWriter("/proc/sysrq-trigger"); |
| 530 | sysrq_trigger.write("w"); |
| 531 | sysrq_trigger.close(); |
| 532 | } catch (IOException e) { |
| 533 | Slog.e(TAG, "Failed to write to /proc/sysrq-trigger"); |
| 534 | Slog.e(TAG, e.getMessage()); |
| 535 | } |
| 536 | |
Brad Fitzpatrick | 9765c72 | 2011-01-14 11:28:22 -0800 | [diff] [blame] | 537 | // Try to add the error to the dropbox, but assuming that the ActivityManager |
| 538 | // itself may be deadlocked. (which has happened, causing this statement to |
| 539 | // deadlock and the watchdog as a whole to be ineffective) |
| 540 | Thread dropboxThread = new Thread("watchdogWriteToDropbox") { |
| 541 | public void run() { |
| 542 | mActivity.addErrorToDropBox( |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 543 | "watchdog", null, "system_server", null, null, |
| 544 | name, null, stack, null); |
Brad Fitzpatrick | 9765c72 | 2011-01-14 11:28:22 -0800 | [diff] [blame] | 545 | } |
| 546 | }; |
| 547 | dropboxThread.start(); |
| 548 | try { |
| 549 | dropboxThread.join(2000); // wait up to 2 seconds for it to return. |
| 550 | } catch (InterruptedException ignored) {} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 551 | |
Jean-Baptiste Queru | 784827b | 2012-09-04 13:35:12 -0700 | [diff] [blame] | 552 | // Only kill the process if the debugger is not attached. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 553 | if (!Debug.isDebuggerConnected()) { |
Dan Egnor | 9bdc94b | 2010-03-04 14:20:31 -0800 | [diff] [blame] | 554 | Slog.w(TAG, "*** WATCHDOG KILLING SYSTEM PROCESS: " + name); |
Jean-Baptiste Queru | 784827b | 2012-09-04 13:35:12 -0700 | [diff] [blame] | 555 | Process.killProcess(Process.myPid()); |
| 556 | System.exit(10); |
Dan Egnor | 9bdc94b | 2010-03-04 14:20:31 -0800 | [diff] [blame] | 557 | } else { |
| 558 | Slog.w(TAG, "Debugger connected: Watchdog is *not* killing the system process"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 559 | } |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 560 | |
| 561 | waitedHalf = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 562 | } |
| 563 | } |
Christopher Tate | ecaa7b4 | 2010-06-04 14:55:02 -0700 | [diff] [blame] | 564 | |
| 565 | private File dumpKernelStackTraces() { |
| 566 | String tracesPath = SystemProperties.get("dalvik.vm.stack-trace-file", null); |
| 567 | if (tracesPath == null || tracesPath.length() == 0) { |
| 568 | return null; |
| 569 | } |
| 570 | |
| 571 | native_dumpKernelStacks(tracesPath); |
| 572 | return new File(tracesPath); |
| 573 | } |
| 574 | |
| 575 | private native void native_dumpKernelStacks(String tracesPath); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 576 | } |