1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
9 #include "content/gpu/gpu_watchdog_thread.h"
11 #include "base/bind.h"
12 #include "base/bind_helpers.h"
13 #include "base/command_line.h"
14 #include "base/compiler_specific.h"
15 #include "base/process_util.h"
16 #include "base/process.h"
17 #include "build/build_config.h"
18 #include "content/public/common/content_switches.h"
19 #include "content/public/common/result_codes.h"
23 const int64 kCheckPeriodMs
= 2000;
26 GpuWatchdogThread::GpuWatchdogThread(int timeout
)
27 : base::Thread("Watchdog"),
28 watched_message_loop_(MessageLoop::current()),
29 timeout_(base::TimeDelta::FromMilliseconds(timeout
)),
32 watched_thread_handle_(0),
35 ALLOW_THIS_IN_INITIALIZER_LIST(task_observer_(this)),
36 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {
40 // GetCurrentThread returns a pseudo-handle that cannot be used by one thread
41 // to identify another. DuplicateHandle creates a "real" handle that can be
42 // used for this purpose.
43 BOOL result
= DuplicateHandle(GetCurrentProcess(),
46 &watched_thread_handle_
,
47 THREAD_QUERY_INFORMATION
,
53 watched_message_loop_
->AddTaskObserver(&task_observer_
);
56 void GpuWatchdogThread::PostAcknowledge() {
57 // Called on the monitored thread. Responds with OnAcknowledge. Cannot use
58 // the method factory. Rely on reference counting instead.
59 message_loop()->PostTask(
61 base::Bind(&GpuWatchdogThread::OnAcknowledge
, this));
64 void GpuWatchdogThread::CheckArmed() {
65 // Acknowledge the watchdog if it has armed itself. The watchdog will not
66 // change its armed state until it is acknowledged.
72 void GpuWatchdogThread::Init() {
73 // Schedule the first check.
77 void GpuWatchdogThread::CleanUp() {
78 weak_factory_
.InvalidateWeakPtrs();
81 GpuWatchdogThread::GpuWatchdogTaskObserver::GpuWatchdogTaskObserver(
82 GpuWatchdogThread
* watchdog
)
83 : watchdog_(watchdog
) {
86 GpuWatchdogThread::GpuWatchdogTaskObserver::~GpuWatchdogTaskObserver() {
89 void GpuWatchdogThread::GpuWatchdogTaskObserver::WillProcessTask(
90 base::TimeTicks time_posted
) {
91 watchdog_
->CheckArmed();
94 void GpuWatchdogThread::GpuWatchdogTaskObserver::DidProcessTask(
95 base::TimeTicks time_posted
) {
96 watchdog_
->CheckArmed();
99 GpuWatchdogThread::~GpuWatchdogThread() {
100 // Verify that the thread was explicitly stopped. If the thread is stopped
101 // implicitly by the destructor, CleanUp() will not be called.
102 DCHECK(!weak_factory_
.HasWeakPtrs());
105 CloseHandle(watched_thread_handle_
);
108 watched_message_loop_
->RemoveTaskObserver(&task_observer_
);
111 void GpuWatchdogThread::OnAcknowledge() {
112 // The check has already been acknowledged and another has already been
113 // scheduled by a previous call to OnAcknowledge. It is normal for a
114 // watched thread to see armed_ being true multiple times before
115 // the OnAcknowledge task is run on the watchdog thread.
119 // Revoke any pending hang termination.
120 weak_factory_
.InvalidateWeakPtrs();
123 // The monitored thread has responded. Post a task to check it again.
124 message_loop()->PostDelayedTask(
126 base::Bind(&GpuWatchdogThread::OnCheck
, weak_factory_
.GetWeakPtr()),
127 base::TimeDelta::FromMilliseconds(kCheckPeriodMs
));
130 void GpuWatchdogThread::OnCheck() {
134 // Must set armed before posting the task. This task might be the only task
135 // that will activate the TaskObserver on the watched thread and it must not
136 // miss the false -> true transition.
140 arm_cpu_time_
= GetWatchedThreadTime();
143 arm_absolute_time_
= base::Time::Now();
145 // Post a task to the monitored thread that does nothing but wake up the
146 // TaskObserver. Any other tasks that are pending on the watched thread will
147 // also wake up the observer. This simply ensures there is at least one.
148 watched_message_loop_
->PostTask(
150 base::Bind(&base::DoNothing
));
152 // Post a task to the watchdog thread to exit if the monitored thread does
153 // not respond in time.
154 message_loop()->PostDelayedTask(
157 &GpuWatchdogThread::DeliberatelyTerminateToRecoverFromHang
,
158 weak_factory_
.GetWeakPtr()),
162 // Use the --disable-gpu-watchdog command line switch to disable this.
163 void GpuWatchdogThread::DeliberatelyTerminateToRecoverFromHang() {
165 // Defer termination until a certain amount of CPU time has elapsed on the
167 base::TimeDelta time_since_arm
= GetWatchedThreadTime() - arm_cpu_time_
;
168 if (time_since_arm
< timeout_
) {
169 message_loop()->PostDelayedTask(
172 &GpuWatchdogThread::DeliberatelyTerminateToRecoverFromHang
,
173 weak_factory_
.GetWeakPtr()),
174 timeout_
- time_since_arm
);
179 // If the watchdog woke up significantly behind schedule, disarm and reset
180 // the watchdog check. This is to prevent the watchdog thread from terminating
181 // when a machine wakes up from sleep or hibernation, which would otherwise
182 // appear to be a hang.
183 if (base::Time::Now() - arm_absolute_time_
> timeout_
* 2) {
189 // For minimal developer annoyance, don't keep terminating. You need to skip
190 // the call to base::Process::Terminate below in a debugger for this to be
192 static bool terminated
= false;
197 if (IsDebuggerPresent())
201 LOG(ERROR
) << "The GPU process hung. Terminating after "
202 << timeout_
.InMilliseconds() << " ms.";
205 CommandLine::ForCurrentProcess()->HasSwitch(switches::kCrashOnGpuHang
);
212 // Deliberately crash the process to create a crash dump.
213 *((volatile int*)0) = 0x1337;
215 base::Process
current_process(base::GetCurrentProcessHandle());
216 current_process
.Terminate(RESULT_CODE_HUNG
);
223 base::TimeDelta
GpuWatchdogThread::GetWatchedThreadTime() {
224 FILETIME creation_time
;
227 FILETIME kernel_time
;
228 BOOL result
= GetThreadTimes(watched_thread_handle_
,
235 ULARGE_INTEGER user_time64
;
236 user_time64
.HighPart
= user_time
.dwHighDateTime
;
237 user_time64
.LowPart
= user_time
.dwLowDateTime
;
239 ULARGE_INTEGER kernel_time64
;
240 kernel_time64
.HighPart
= kernel_time
.dwHighDateTime
;
241 kernel_time64
.LowPart
= kernel_time
.dwLowDateTime
;
243 // Time is reported in units of 100 nanoseconds. Kernel and user time are
244 // summed to deal with to kinds of hangs. One is where the GPU process is
245 // stuck in user level, never calling into the kernel and kernel time is
246 // not increasing. The other is where either the kernel hangs and never
247 // returns to user level or where user level code
248 // calls into kernel level repeatedly, giving up its quanta before it is
249 // tracked, for example a loop that repeatedly Sleeps.
250 return base::TimeDelta::FromMilliseconds(static_cast<int64
>(
251 (user_time64
.QuadPart
+ kernel_time64
.QuadPart
) / 10000));
255 } // namespace content