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