OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #if defined(OS_WIN) | 5 #if defined(OS_WIN) |
6 #include <windows.h> | 6 #include <windows.h> |
7 #endif | 7 #endif |
8 | 8 |
9 #include "content/gpu/gpu_watchdog_thread.h" | 9 #include "content/gpu/gpu_watchdog_thread.h" |
10 | 10 |
11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
| 12 #include "base/process.h" |
| 13 #include "base/process_util.h" |
12 #include "build/build_config.h" | 14 #include "build/build_config.h" |
| 15 #include "content/common/result_codes.h" |
13 | 16 |
14 namespace { | 17 namespace { |
15 const int64 kCheckPeriod = 2000; | 18 const int64 kCheckPeriod = 2000; |
16 | 19 |
17 void DoNothing() { | 20 void DoNothing() { |
18 } | 21 } |
19 } | 22 } |
20 | 23 |
21 GpuWatchdogThread::GpuWatchdogThread(int timeout) | 24 GpuWatchdogThread::GpuWatchdogThread(int timeout) |
22 : base::Thread("Watchdog"), | 25 : base::Thread("Watchdog"), |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 // also wake up the observer. This simply ensures there is at least one. | 182 // also wake up the observer. This simply ensures there is at least one. |
180 watched_message_loop_->PostTask( | 183 watched_message_loop_->PostTask( |
181 FROM_HERE, | 184 FROM_HERE, |
182 NewRunnableFunction(DoNothing)); | 185 NewRunnableFunction(DoNothing)); |
183 | 186 |
184 // Post a task to the watchdog thread to exit if the monitored thread does | 187 // Post a task to the watchdog thread to exit if the monitored thread does |
185 // not respond in time. | 188 // not respond in time. |
186 message_loop()->PostDelayedTask( | 189 message_loop()->PostDelayedTask( |
187 FROM_HERE, | 190 FROM_HERE, |
188 method_factory_->NewRunnableMethod( | 191 method_factory_->NewRunnableMethod( |
189 &GpuWatchdogThread::DeliberatelyCrashingToRecoverFromHang), | 192 &GpuWatchdogThread::DeliberatelyTerminateToRecoverFromHang), |
190 timeout_); | 193 timeout_); |
191 } | 194 } |
192 | 195 |
193 // Use the --disable-gpu-watchdog command line switch to disable this. | 196 // Use the --disable-gpu-watchdog command line switch to disable this. |
194 void GpuWatchdogThread::DeliberatelyCrashingToRecoverFromHang() { | 197 void GpuWatchdogThread::DeliberatelyTerminateToRecoverFromHang() { |
195 #if defined(OS_WIN) | 198 #if defined(OS_WIN) |
196 // Defer termination until a certain amount of CPU time has elapsed on the | 199 // Defer termination until a certain amount of CPU time has elapsed on the |
197 // watched thread. | 200 // watched thread. |
198 int64 time_since_arm = GetWatchedThreadTime() - arm_cpu_time_; | 201 int64 time_since_arm = GetWatchedThreadTime() - arm_cpu_time_; |
199 if (time_since_arm < timeout_) { | 202 if (time_since_arm < timeout_) { |
200 message_loop()->PostDelayedTask( | 203 message_loop()->PostDelayedTask( |
201 FROM_HERE, | 204 FROM_HERE, |
202 method_factory_->NewRunnableMethod( | 205 method_factory_->NewRunnableMethod( |
203 &GpuWatchdogThread::DeliberatelyCrashingToRecoverFromHang), | 206 &GpuWatchdogThread::DeliberatelyTerminateToRecoverFromHang), |
204 timeout_ - time_since_arm); | 207 timeout_ - time_since_arm); |
205 return; | 208 return; |
206 } | 209 } |
207 #endif | 210 #endif |
208 | 211 |
209 // If the watchdog woke up significantly behind schedule, disarm and reset | 212 // If the watchdog woke up significantly behind schedule, disarm and reset |
210 // the watchdog check. This is to prevent the watchdog thread from terminating | 213 // the watchdog check. This is to prevent the watchdog thread from terminating |
211 // when a machine wakes up from sleep or hibernation, which would otherwise | 214 // when a machine wakes up from sleep or hibernation, which would otherwise |
212 // appear to be a hang. | 215 // appear to be a hang. |
213 if ((base::Time::Now() - arm_absolute_time_).InMilliseconds() > | 216 if ((base::Time::Now() - arm_absolute_time_).InMilliseconds() > |
214 timeout_ * 2) { | 217 timeout_ * 2) { |
215 armed_ = false; | 218 armed_ = false; |
216 OnCheck(); | 219 OnCheck(); |
217 return; | 220 return; |
218 } | 221 } |
219 | 222 |
220 // Make sure the timeout period is on the stack before crashing. | 223 // For minimal developer annoyance, don't keep terminating. You need to skip |
221 volatile int timeout = timeout_; | 224 // the call to base::Process::Terminate below in a debugger for this to be |
222 | 225 // useful. |
223 // For minimal developer annoyance, don't keep crashing. | 226 static bool terminated = false; |
224 static bool crashed = false; | 227 if (terminated) |
225 if (crashed) | |
226 return; | 228 return; |
227 | 229 |
228 #if defined(OS_WIN) | 230 #if defined(OS_WIN) |
229 if (IsDebuggerPresent()) | 231 if (IsDebuggerPresent()) |
230 return; | 232 return; |
231 #endif | 233 #endif |
232 | 234 |
233 LOG(ERROR) << "The GPU process hung. Terminating after " | 235 LOG(ERROR) << "The GPU process hung. Terminating after " |
234 << timeout_ << " ms."; | 236 << timeout_ << " ms."; |
235 | 237 |
236 volatile int* null_pointer = NULL; | 238 base::Process current_process(base::GetCurrentProcessHandle()); |
237 *null_pointer = timeout; | 239 current_process.Terminate(content::RESULT_CODE_HUNG); |
238 | 240 |
239 crashed = true; | 241 terminated = true; |
240 } | 242 } |
OLD | NEW |