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_(base::MessageLoop::current()),
29 timeout_(base::TimeDelta::FromMilliseconds(timeout
)),
32 watched_thread_handle_(0),
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 const base::PendingTask
& pending_task
) {
91 watchdog_
->CheckArmed();
94 void GpuWatchdogThread::GpuWatchdogTaskObserver::DidProcessTask(
95 const base::PendingTask
& pending_task
) {
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 // If it took a long time for the acknowledgement, assume the computer was
124 // recently suspended.
125 bool was_suspended
= (base::Time::Now() > suspension_timeout_
);
127 // The monitored thread has responded. Post a task to check it again.
128 message_loop()->PostDelayedTask(
130 base::Bind(&GpuWatchdogThread::OnCheck
, weak_factory_
.GetWeakPtr(),
132 base::TimeDelta::FromMilliseconds(kCheckPeriodMs
));
135 void GpuWatchdogThread::OnCheck(bool after_suspend
) {
139 // Must set armed before posting the task. This task might be the only task
140 // that will activate the TaskObserver on the watched thread and it must not
141 // miss the false -> true transition.
145 arm_cpu_time_
= GetWatchedThreadTime();
148 // Immediately after the computer is woken up from being suspended it might
149 // be pretty sluggish, so allow some extra time before the next timeout.
150 base::TimeDelta timeout
= timeout_
* (after_suspend
? 3 : 1);
151 suspension_timeout_
= base::Time::Now() + timeout
* 2;
153 // Post a task to the monitored thread that does nothing but wake up the
154 // TaskObserver. Any other tasks that are pending on the watched thread will
155 // also wake up the observer. This simply ensures there is at least one.
156 watched_message_loop_
->PostTask(
158 base::Bind(&base::DoNothing
));
160 // Post a task to the watchdog thread to exit if the monitored thread does
161 // not respond in time.
162 message_loop()->PostDelayedTask(
165 &GpuWatchdogThread::DeliberatelyTerminateToRecoverFromHang
,
166 weak_factory_
.GetWeakPtr()),
170 // Use the --disable-gpu-watchdog command line switch to disable this.
171 void GpuWatchdogThread::DeliberatelyTerminateToRecoverFromHang() {
173 // Defer termination until a certain amount of CPU time has elapsed on the
175 base::TimeDelta time_since_arm
= GetWatchedThreadTime() - arm_cpu_time_
;
176 if (time_since_arm
< timeout_
) {
177 message_loop()->PostDelayedTask(
180 &GpuWatchdogThread::DeliberatelyTerminateToRecoverFromHang
,
181 weak_factory_
.GetWeakPtr()),
182 timeout_
- time_since_arm
);
187 // If the watchdog woke up significantly behind schedule, disarm and reset
188 // the watchdog check. This is to prevent the watchdog thread from terminating
189 // when a machine wakes up from sleep or hibernation, which would otherwise
190 // appear to be a hang.
191 if (base::Time::Now() > suspension_timeout_
) {
197 // For minimal developer annoyance, don't keep terminating. You need to skip
198 // the call to base::Process::Terminate below in a debugger for this to be
200 static bool terminated
= false;
205 if (IsDebuggerPresent())
209 LOG(ERROR
) << "The GPU process hung. Terminating after "
210 << timeout_
.InMilliseconds() << " ms.";
212 // Deliberately crash the process to create a crash dump.
213 *((volatile int*)0) = 0x1337;
219 base::TimeDelta
GpuWatchdogThread::GetWatchedThreadTime() {
220 FILETIME creation_time
;
223 FILETIME kernel_time
;
224 BOOL result
= GetThreadTimes(watched_thread_handle_
,
231 ULARGE_INTEGER user_time64
;
232 user_time64
.HighPart
= user_time
.dwHighDateTime
;
233 user_time64
.LowPart
= user_time
.dwLowDateTime
;
235 ULARGE_INTEGER kernel_time64
;
236 kernel_time64
.HighPart
= kernel_time
.dwHighDateTime
;
237 kernel_time64
.LowPart
= kernel_time
.dwLowDateTime
;
239 // Time is reported in units of 100 nanoseconds. Kernel and user time are
240 // summed to deal with to kinds of hangs. One is where the GPU process is
241 // stuck in user level, never calling into the kernel and kernel time is
242 // not increasing. The other is where either the kernel hangs and never
243 // returns to user level or where user level code
244 // calls into kernel level repeatedly, giving up its quanta before it is
245 // tracked, for example a loop that repeatedly Sleeps.
246 return base::TimeDelta::FromMilliseconds(static_cast<int64
>(
247 (user_time64
.QuadPart
+ kernel_time64
.QuadPart
) / 10000));
251 } // namespace content