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 | |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 61 | static final long DEFAULT_TIMEOUT = DB ? 10*1000 : 60*1000; |
| 62 | static final long CHECK_INTERVAL = DEFAULT_TIMEOUT / 2; |
| 63 | |
| 64 | // These are temporally ordered: larger values as lateness increases |
| 65 | static final int COMPLETED = 0; |
| 66 | static final int WAITING = 1; |
| 67 | static final int WAITED_HALF = 2; |
| 68 | static final int OVERDUE = 3; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | |
Igor Murashkin | 44d04aa | 2013-10-23 10:56:02 -0700 | [diff] [blame] | 70 | // Which native processes to dump into dropbox's stack traces |
| 71 | public static final String[] NATIVE_STACKS_OF_INTEREST = new String[] { |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 72 | "/system/bin/mediaserver", |
| 73 | "/system/bin/sdcard", |
| 74 | "/system/bin/surfaceflinger" |
| 75 | }; |
| 76 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | static Watchdog sWatchdog; |
| 78 | |
| 79 | /* 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] | 80 | final ArrayList<HandlerChecker> mHandlerCheckers = new ArrayList<HandlerChecker>(); |
| 81 | final HandlerChecker mMonitorChecker; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | ContentResolver mResolver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | ActivityManagerService mActivity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | int mPhonePid; |
Dianne Hackborn | 5b88a2f | 2013-05-03 16:25:11 -0700 | [diff] [blame] | 86 | IActivityController mController; |
Dianne Hackborn | 8bd64df | 2013-05-06 16:07:26 -0700 | [diff] [blame] | 87 | boolean mAllowRestart = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 89 | /** |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 90 | * 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] | 91 | */ |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 92 | public final class HandlerChecker implements Runnable { |
| 93 | private final Handler mHandler; |
| 94 | private final String mName; |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 95 | private final long mWaitMax; |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 96 | private final ArrayList<Monitor> mMonitors = new ArrayList<Monitor>(); |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 97 | private boolean mCompleted; |
| 98 | private Monitor mCurrentMonitor; |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 99 | private long mStartTime; |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 100 | |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 101 | HandlerChecker(Handler handler, String name, long waitMaxMillis) { |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 102 | mHandler = handler; |
| 103 | mName = name; |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 104 | mWaitMax = waitMaxMillis; |
| 105 | mCompleted = true; |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | public void addMonitor(Monitor monitor) { |
| 109 | mMonitors.add(monitor); |
| 110 | } |
| 111 | |
| 112 | public void scheduleCheckLocked() { |
Dianne Hackborn | f6438b1 | 2013-05-09 18:53:48 -0700 | [diff] [blame] | 113 | if (mMonitors.size() == 0 && mHandler.getLooper().isIdling()) { |
Dianne Hackborn | efa92b2 | 2013-05-03 14:11:43 -0700 | [diff] [blame] | 114 | // If the target looper is or just recently was idling, then |
| 115 | // there is no reason to enqueue our checker on it since that |
| 116 | // is as good as it not being deadlocked. This avoid having |
| 117 | // to do a context switch to check the thread. Note that we |
| 118 | // only do this if mCheckReboot is false and we have no |
| 119 | // monitors, since those would need to be executed at this point. |
| 120 | mCompleted = true; |
| 121 | return; |
| 122 | } |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 123 | |
| 124 | if (!mCompleted) { |
| 125 | // we already have a check in flight, so no need |
| 126 | return; |
| 127 | } |
| 128 | |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 129 | mCompleted = false; |
| 130 | mCurrentMonitor = null; |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 131 | mStartTime = SystemClock.uptimeMillis(); |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 132 | mHandler.postAtFrontOfQueue(this); |
| 133 | } |
| 134 | |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 135 | public boolean isOverdueLocked() { |
| 136 | return (!mCompleted) && (SystemClock.uptimeMillis() > mStartTime + mWaitMax); |
| 137 | } |
| 138 | |
| 139 | public int getCompletionStateLocked() { |
| 140 | if (mCompleted) { |
| 141 | return COMPLETED; |
| 142 | } else { |
| 143 | long latency = SystemClock.uptimeMillis() - mStartTime; |
| 144 | if (latency < mWaitMax/2) { |
| 145 | return WAITING; |
| 146 | } else if (latency < mWaitMax) { |
| 147 | return WAITED_HALF; |
| 148 | } |
| 149 | } |
| 150 | return OVERDUE; |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Dianne Hackborn | fa012b3 | 2013-05-10 15:23:28 -0700 | [diff] [blame] | 153 | public Thread getThread() { |
| 154 | return mHandler.getLooper().getThread(); |
| 155 | } |
| 156 | |
| 157 | public String getName() { |
| 158 | return mName; |
| 159 | } |
| 160 | |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 161 | public String describeBlockedStateLocked() { |
Jeff Brown | 7dd2d19 | 2013-09-06 15:05:23 -0700 | [diff] [blame] | 162 | if (mCurrentMonitor == null) { |
| 163 | return "Blocked in handler on " + mName + " (" + getThread().getName() + ")"; |
| 164 | } else { |
| 165 | return "Blocked in monitor " + mCurrentMonitor.getClass().getName() |
| 166 | + " on " + mName + " (" + getThread().getName() + ")"; |
| 167 | } |
John Michelau | 1164152 | 2013-03-18 18:28:23 -0500 | [diff] [blame] | 168 | } |
| 169 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 170 | @Override |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 171 | public void run() { |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 172 | final int size = mMonitors.size(); |
| 173 | for (int i = 0 ; i < size ; i++) { |
| 174 | synchronized (Watchdog.this) { |
| 175 | mCurrentMonitor = mMonitors.get(i); |
| 176 | } |
| 177 | mCurrentMonitor.monitor(); |
| 178 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 179 | |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 180 | synchronized (Watchdog.this) { |
| 181 | mCompleted = true; |
| 182 | mCurrentMonitor = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 187 | final class RebootRequestReceiver extends BroadcastReceiver { |
| 188 | @Override |
| 189 | public void onReceive(Context c, Intent intent) { |
Dianne Hackborn | f6438b1 | 2013-05-09 18:53:48 -0700 | [diff] [blame] | 190 | if (intent.getIntExtra("nowait", 0) != 0) { |
| 191 | rebootSystem("Received ACTION_REBOOT broadcast"); |
| 192 | return; |
| 193 | } |
| 194 | Slog.w(TAG, "Unsupported ACTION_REBOOT broadcast: " + intent); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | |
| 198 | public interface Monitor { |
| 199 | void monitor(); |
| 200 | } |
| 201 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 202 | public static Watchdog getInstance() { |
| 203 | if (sWatchdog == null) { |
| 204 | sWatchdog = new Watchdog(); |
| 205 | } |
| 206 | |
| 207 | return sWatchdog; |
| 208 | } |
| 209 | |
| 210 | private Watchdog() { |
| 211 | super("watchdog"); |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 212 | // Initialize handler checkers for each common thread we want to check. Note |
| 213 | // that we are not currently checking the background thread, since it can |
| 214 | // potentially hold longer running operations with no guarantees about the timeliness |
| 215 | // of operations there. |
| 216 | |
| 217 | // The shared foreground thread is the main checker. It is where we |
| 218 | // will also dispatch monitor checks and do other work. |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 219 | mMonitorChecker = new HandlerChecker(FgThread.getHandler(), |
| 220 | "foreground thread", DEFAULT_TIMEOUT); |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 221 | mHandlerCheckers.add(mMonitorChecker); |
| 222 | // Add checker for main thread. We only do a quick check since there |
| 223 | // can be UI running on the thread. |
| 224 | mHandlerCheckers.add(new HandlerChecker(new Handler(Looper.getMainLooper()), |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 225 | "main thread", DEFAULT_TIMEOUT)); |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 226 | // Add checker for shared UI thread. |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 227 | mHandlerCheckers.add(new HandlerChecker(UiThread.getHandler(), |
| 228 | "ui thread", DEFAULT_TIMEOUT)); |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 229 | // And also check IO thread. |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 230 | mHandlerCheckers.add(new HandlerChecker(IoThread.getHandler(), |
| 231 | "i/o thread", DEFAULT_TIMEOUT)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 232 | } |
| 233 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame^] | 234 | public void init(Context context, ActivityManagerService activity) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 235 | mResolver = context.getContentResolver(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 236 | mActivity = activity; |
| 237 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 238 | context.registerReceiver(new RebootRequestReceiver(), |
| 239 | new IntentFilter(Intent.ACTION_REBOOT), |
| 240 | android.Manifest.permission.REBOOT, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 241 | } |
| 242 | |
Christopher Tate | c27181c | 2010-06-30 14:41:09 -0700 | [diff] [blame] | 243 | public void processStarted(String name, int pid) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 244 | synchronized (this) { |
| 245 | if ("com.android.phone".equals(name)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 246 | mPhonePid = pid; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | } |
| 250 | |
Dianne Hackborn | 5b88a2f | 2013-05-03 16:25:11 -0700 | [diff] [blame] | 251 | public void setActivityController(IActivityController controller) { |
| 252 | synchronized (this) { |
| 253 | mController = controller; |
| 254 | } |
| 255 | } |
| 256 | |
Dianne Hackborn | 8bd64df | 2013-05-06 16:07:26 -0700 | [diff] [blame] | 257 | public void setAllowRestart(boolean allowRestart) { |
| 258 | synchronized (this) { |
| 259 | mAllowRestart = allowRestart; |
| 260 | } |
| 261 | } |
| 262 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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) { |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 273 | addThread(thread, name, DEFAULT_TIMEOUT); |
| 274 | } |
| 275 | |
| 276 | public void addThread(Handler thread, String name, long timeoutMillis) { |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 277 | synchronized (this) { |
| 278 | if (isAlive()) { |
| 279 | throw new RuntimeException("Threads can't be added once the Watchdog is running"); |
| 280 | } |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 281 | mHandlerCheckers.add(new HandlerChecker(thread, name, timeoutMillis)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 285 | /** |
| 286 | * Perform a full reboot of the system. |
| 287 | */ |
| 288 | void rebootSystem(String reason) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 289 | Slog.i(TAG, "Rebooting system because: " + reason); |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 290 | PowerManagerService pms = (PowerManagerService) ServiceManager.getService("power"); |
Dianne Hackborn | c428aae | 2012-10-03 16:38:22 -0700 | [diff] [blame] | 291 | pms.reboot(false, reason, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 292 | } |
| 293 | |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 294 | private int evaluateCheckerCompletionLocked() { |
| 295 | int state = COMPLETED; |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 296 | for (int i=0; i<mHandlerCheckers.size(); i++) { |
| 297 | HandlerChecker hc = mHandlerCheckers.get(i); |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 298 | state = Math.max(state, hc.getCompletionStateLocked()); |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 299 | } |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 300 | return state; |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 301 | } |
| 302 | |
Dianne Hackborn | fa012b3 | 2013-05-10 15:23:28 -0700 | [diff] [blame] | 303 | private ArrayList<HandlerChecker> getBlockedCheckersLocked() { |
| 304 | ArrayList<HandlerChecker> checkers = new ArrayList<HandlerChecker>(); |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 305 | for (int i=0; i<mHandlerCheckers.size(); i++) { |
| 306 | HandlerChecker hc = mHandlerCheckers.get(i); |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 307 | if (hc.isOverdueLocked()) { |
Dianne Hackborn | fa012b3 | 2013-05-10 15:23:28 -0700 | [diff] [blame] | 308 | checkers.add(hc); |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 309 | } |
| 310 | } |
Dianne Hackborn | fa012b3 | 2013-05-10 15:23:28 -0700 | [diff] [blame] | 311 | return checkers; |
| 312 | } |
| 313 | |
| 314 | private String describeCheckersLocked(ArrayList<HandlerChecker> checkers) { |
| 315 | StringBuilder builder = new StringBuilder(128); |
| 316 | for (int i=0; i<checkers.size(); i++) { |
| 317 | if (builder.length() > 0) { |
| 318 | builder.append(", "); |
| 319 | } |
| 320 | builder.append(checkers.get(i).describeBlockedStateLocked()); |
| 321 | } |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 322 | return builder.toString(); |
| 323 | } |
| 324 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 325 | @Override |
| 326 | public void run() { |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 327 | boolean waitedHalf = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 328 | while (true) { |
Dianne Hackborn | fa012b3 | 2013-05-10 15:23:28 -0700 | [diff] [blame] | 329 | final ArrayList<HandlerChecker> blockedCheckers; |
Jeff Brown | 7dd2d19 | 2013-09-06 15:05:23 -0700 | [diff] [blame] | 330 | final String subject; |
Dianne Hackborn | 8bd64df | 2013-05-06 16:07:26 -0700 | [diff] [blame] | 331 | final boolean allowRestart; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 332 | synchronized (this) { |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 333 | long timeout = CHECK_INTERVAL; |
| 334 | // Make sure we (re)spin the checkers that have become idle within |
| 335 | // this wait-and-check interval |
| 336 | for (int i=0; i<mHandlerCheckers.size(); i++) { |
| 337 | HandlerChecker hc = mHandlerCheckers.get(i); |
| 338 | hc.scheduleCheckLocked(); |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 339 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 340 | |
| 341 | // NOTE: We use uptimeMillis() here because we do not want to increment the time we |
| 342 | // 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] | 343 | // 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] | 344 | // positive on when to kill things. |
| 345 | long start = SystemClock.uptimeMillis(); |
Michael Wright | 8fa56f6 | 2013-04-01 16:36:05 -0700 | [diff] [blame] | 346 | while (timeout > 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 347 | try { |
Michael Wright | 8fa56f6 | 2013-04-01 16:36:05 -0700 | [diff] [blame] | 348 | wait(timeout); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 349 | } catch (InterruptedException e) { |
Dan Egnor | 9bdc94b | 2010-03-04 14:20:31 -0800 | [diff] [blame] | 350 | Log.wtf(TAG, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 351 | } |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 352 | timeout = CHECK_INTERVAL - (SystemClock.uptimeMillis() - start); |
Dan Egnor | 9bdc94b | 2010-03-04 14:20:31 -0800 | [diff] [blame] | 353 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 354 | |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 355 | final int waitState = evaluateCheckerCompletionLocked(); |
| 356 | if (waitState == COMPLETED) { |
| 357 | // The monitors have returned; reset |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 358 | waitedHalf = false; |
| 359 | continue; |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 360 | } else if (waitState == WAITING) { |
| 361 | // still waiting but within their configured intervals; back off and recheck |
| 362 | continue; |
| 363 | } else if (waitState == WAITED_HALF) { |
| 364 | if (!waitedHalf) { |
| 365 | // We've waited half the deadlock-detection interval. Pull a stack |
| 366 | // trace and wait another half. |
| 367 | ArrayList<Integer> pids = new ArrayList<Integer>(); |
| 368 | pids.add(Process.myPid()); |
| 369 | ActivityManagerService.dumpStackTraces(true, pids, null, null, |
| 370 | NATIVE_STACKS_OF_INTEREST); |
| 371 | waitedHalf = true; |
| 372 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 373 | continue; |
| 374 | } |
Michael Wright | 8fa56f6 | 2013-04-01 16:36:05 -0700 | [diff] [blame] | 375 | |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 376 | // something is overdue! |
Dianne Hackborn | fa012b3 | 2013-05-10 15:23:28 -0700 | [diff] [blame] | 377 | blockedCheckers = getBlockedCheckersLocked(); |
Jeff Brown | 7dd2d19 | 2013-09-06 15:05:23 -0700 | [diff] [blame] | 378 | subject = describeCheckersLocked(blockedCheckers); |
Dianne Hackborn | 8bd64df | 2013-05-06 16:07:26 -0700 | [diff] [blame] | 379 | allowRestart = mAllowRestart; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | // 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] | 383 | // First collect stack traces from all threads of the system process. |
| 384 | // Then kill this process so that the system will restart. |
Jeff Brown | 7dd2d19 | 2013-09-06 15:05:23 -0700 | [diff] [blame] | 385 | EventLog.writeEvent(EventLogTags.WATCHDOG, subject); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 386 | |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 387 | ArrayList<Integer> pids = new ArrayList<Integer>(); |
Dan Egnor | 9bdc94b | 2010-03-04 14:20:31 -0800 | [diff] [blame] | 388 | pids.add(Process.myPid()); |
Dan Egnor | 4bded07 | 2010-03-11 22:00:47 -0800 | [diff] [blame] | 389 | if (mPhonePid > 0) pids.add(mPhonePid); |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 390 | // Pass !waitedHalf so that just in case we somehow wind up here without having |
| 391 | // dumped the halfway stacks, we properly re-initialize the trace file. |
Brad Fitzpatrick | 9765c72 | 2011-01-14 11:28:22 -0800 | [diff] [blame] | 392 | final File stack = ActivityManagerService.dumpStackTraces( |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 393 | !waitedHalf, pids, null, null, NATIVE_STACKS_OF_INTEREST); |
Dan Egnor | 4bded07 | 2010-03-11 22:00:47 -0800 | [diff] [blame] | 394 | |
| 395 | // Give some extra time to make sure the stack traces get written. |
| 396 | // The system's been hanging for a minute, another second or two won't hurt much. |
| 397 | SystemClock.sleep(2000); |
| 398 | |
Christopher Tate | ecaa7b4 | 2010-06-04 14:55:02 -0700 | [diff] [blame] | 399 | // Pull our own kernel thread stacks as well if we're configured for that |
| 400 | if (RECORD_KERNEL_THREADS) { |
| 401 | dumpKernelStackTraces(); |
| 402 | } |
| 403 | |
Colin Cross | 5df1d87 | 2012-11-29 11:42:11 -0800 | [diff] [blame] | 404 | // Trigger the kernel to dump all blocked threads to the kernel log |
| 405 | try { |
| 406 | FileWriter sysrq_trigger = new FileWriter("/proc/sysrq-trigger"); |
| 407 | sysrq_trigger.write("w"); |
| 408 | sysrq_trigger.close(); |
| 409 | } catch (IOException e) { |
| 410 | Slog.e(TAG, "Failed to write to /proc/sysrq-trigger"); |
| 411 | Slog.e(TAG, e.getMessage()); |
| 412 | } |
| 413 | |
Brad Fitzpatrick | 9765c72 | 2011-01-14 11:28:22 -0800 | [diff] [blame] | 414 | // Try to add the error to the dropbox, but assuming that the ActivityManager |
| 415 | // itself may be deadlocked. (which has happened, causing this statement to |
| 416 | // deadlock and the watchdog as a whole to be ineffective) |
| 417 | Thread dropboxThread = new Thread("watchdogWriteToDropbox") { |
| 418 | public void run() { |
| 419 | mActivity.addErrorToDropBox( |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 420 | "watchdog", null, "system_server", null, null, |
Jeff Brown | 7dd2d19 | 2013-09-06 15:05:23 -0700 | [diff] [blame] | 421 | subject, null, stack, null); |
Brad Fitzpatrick | 9765c72 | 2011-01-14 11:28:22 -0800 | [diff] [blame] | 422 | } |
| 423 | }; |
| 424 | dropboxThread.start(); |
| 425 | try { |
| 426 | dropboxThread.join(2000); // wait up to 2 seconds for it to return. |
| 427 | } catch (InterruptedException ignored) {} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 428 | |
Dianne Hackborn | 5b88a2f | 2013-05-03 16:25:11 -0700 | [diff] [blame] | 429 | IActivityController controller; |
| 430 | synchronized (this) { |
| 431 | controller = mController; |
| 432 | } |
| 433 | if (controller != null) { |
| 434 | Slog.i(TAG, "Reporting stuck state to activity controller"); |
| 435 | try { |
| 436 | Binder.setDumpDisabled("Service dumps disabled due to hung system process."); |
| 437 | // 1 = keep waiting, -1 = kill system |
Jeff Brown | 7dd2d19 | 2013-09-06 15:05:23 -0700 | [diff] [blame] | 438 | int res = controller.systemNotResponding(subject); |
Dianne Hackborn | 5b88a2f | 2013-05-03 16:25:11 -0700 | [diff] [blame] | 439 | if (res >= 0) { |
| 440 | Slog.i(TAG, "Activity controller requested to coninue to wait"); |
| 441 | waitedHalf = false; |
| 442 | continue; |
| 443 | } |
| 444 | } catch (RemoteException e) { |
| 445 | } |
| 446 | } |
| 447 | |
Jean-Baptiste Queru | 784827b | 2012-09-04 13:35:12 -0700 | [diff] [blame] | 448 | // Only kill the process if the debugger is not attached. |
Dianne Hackborn | 8bd64df | 2013-05-06 16:07:26 -0700 | [diff] [blame] | 449 | if (Debug.isDebuggerConnected()) { |
| 450 | Slog.w(TAG, "Debugger connected: Watchdog is *not* killing the system process"); |
| 451 | } else if (!allowRestart) { |
| 452 | Slog.w(TAG, "Restart not allowed: Watchdog is *not* killing the system process"); |
| 453 | } else { |
Jeff Brown | 7dd2d19 | 2013-09-06 15:05:23 -0700 | [diff] [blame] | 454 | Slog.w(TAG, "*** WATCHDOG KILLING SYSTEM PROCESS: " + subject); |
Dianne Hackborn | fa012b3 | 2013-05-10 15:23:28 -0700 | [diff] [blame] | 455 | for (int i=0; i<blockedCheckers.size(); i++) { |
| 456 | Slog.w(TAG, blockedCheckers.get(i).getName() + " stack trace:"); |
| 457 | StackTraceElement[] stackTrace |
| 458 | = blockedCheckers.get(i).getThread().getStackTrace(); |
| 459 | for (StackTraceElement element: stackTrace) { |
| 460 | Slog.w(TAG, " at " + element); |
| 461 | } |
Michael Wright | 56a6c66 | 2013-04-30 20:13:07 -0700 | [diff] [blame] | 462 | } |
Dianne Hackborn | fa012b3 | 2013-05-10 15:23:28 -0700 | [diff] [blame] | 463 | Slog.w(TAG, "*** GOODBYE!"); |
Jean-Baptiste Queru | 784827b | 2012-09-04 13:35:12 -0700 | [diff] [blame] | 464 | Process.killProcess(Process.myPid()); |
| 465 | System.exit(10); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 466 | } |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 467 | |
| 468 | waitedHalf = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 469 | } |
| 470 | } |
Christopher Tate | ecaa7b4 | 2010-06-04 14:55:02 -0700 | [diff] [blame] | 471 | |
| 472 | private File dumpKernelStackTraces() { |
| 473 | String tracesPath = SystemProperties.get("dalvik.vm.stack-trace-file", null); |
| 474 | if (tracesPath == null || tracesPath.length() == 0) { |
| 475 | return null; |
| 476 | } |
| 477 | |
| 478 | native_dumpKernelStacks(tracesPath); |
| 479 | return new File(tracesPath); |
| 480 | } |
| 481 | |
| 482 | private native void native_dumpKernelStacks(String tracesPath); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 483 | } |