[content shell] hook up testRunner.dumpEditingCallbacks
[chromium-blink-merge.git] / content / gpu / gpu_watchdog_thread.cc
blob19b0fd71cbfadcddc696aea45d10d56115a19350
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.
5 #if defined(OS_WIN)
6 #include <windows.h>
7 #endif
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"
21 namespace content {
22 namespace {
23 const int64 kCheckPeriodMs = 2000;
24 } // namespace
26 GpuWatchdogThread::GpuWatchdogThread(int timeout)
27 : base::Thread("Watchdog"),
28 watched_message_loop_(MessageLoop::current()),
29 timeout_(base::TimeDelta::FromMilliseconds(timeout)),
30 armed_(false),
31 #if defined(OS_WIN)
32 watched_thread_handle_(0),
33 arm_cpu_time_(),
34 #endif
35 ALLOW_THIS_IN_INITIALIZER_LIST(task_observer_(this)),
36 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {
37 DCHECK(timeout >= 0);
39 #if defined(OS_WIN)
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(),
44 GetCurrentThread(),
45 GetCurrentProcess(),
46 &watched_thread_handle_,
47 THREAD_QUERY_INFORMATION,
48 FALSE,
49 0);
50 DCHECK(result);
51 #endif
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(
60 FROM_HERE,
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.
67 if (armed()) {
68 PostAcknowledge();
72 void GpuWatchdogThread::Init() {
73 // Schedule the first check.
74 OnCheck();
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());
104 #if defined(OS_WIN)
105 CloseHandle(watched_thread_handle_);
106 #endif
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.
116 if (!armed_)
117 return;
119 // Revoke any pending hang termination.
120 weak_factory_.InvalidateWeakPtrs();
121 armed_ = false;
123 // The monitored thread has responded. Post a task to check it again.
124 message_loop()->PostDelayedTask(
125 FROM_HERE,
126 base::Bind(&GpuWatchdogThread::OnCheck, weak_factory_.GetWeakPtr()),
127 base::TimeDelta::FromMilliseconds(kCheckPeriodMs));
130 void GpuWatchdogThread::OnCheck() {
131 if (armed_)
132 return;
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.
137 armed_ = true;
139 #if defined(OS_WIN)
140 arm_cpu_time_ = GetWatchedThreadTime();
141 #endif
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(
149 FROM_HERE,
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(
155 FROM_HERE,
156 base::Bind(
157 &GpuWatchdogThread::DeliberatelyTerminateToRecoverFromHang,
158 weak_factory_.GetWeakPtr()),
159 timeout_);
162 // Use the --disable-gpu-watchdog command line switch to disable this.
163 void GpuWatchdogThread::DeliberatelyTerminateToRecoverFromHang() {
164 #if defined(OS_WIN)
165 // Defer termination until a certain amount of CPU time has elapsed on the
166 // watched thread.
167 base::TimeDelta time_since_arm = GetWatchedThreadTime() - arm_cpu_time_;
168 if (time_since_arm < timeout_) {
169 message_loop()->PostDelayedTask(
170 FROM_HERE,
171 base::Bind(
172 &GpuWatchdogThread::DeliberatelyTerminateToRecoverFromHang,
173 weak_factory_.GetWeakPtr()),
174 timeout_ - time_since_arm);
175 return;
177 #endif
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) {
184 armed_ = false;
185 OnCheck();
186 return;
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
191 // useful.
192 static bool terminated = false;
193 if (terminated)
194 return;
196 #if defined(OS_WIN)
197 if (IsDebuggerPresent())
198 return;
199 #endif
201 LOG(ERROR) << "The GPU process hung. Terminating after "
202 << timeout_.InMilliseconds() << " ms.";
204 bool should_crash =
205 CommandLine::ForCurrentProcess()->HasSwitch(switches::kCrashOnGpuHang);
207 #if defined(OS_WIN)
208 should_crash = true;
209 #endif
211 if (should_crash) {
212 // Deliberately crash the process to create a crash dump.
213 *((volatile int*)0) = 0x1337;
214 } else {
215 base::Process current_process(base::GetCurrentProcessHandle());
216 current_process.Terminate(RESULT_CODE_HUNG);
219 terminated = true;
222 #if defined(OS_WIN)
223 base::TimeDelta GpuWatchdogThread::GetWatchedThreadTime() {
224 FILETIME creation_time;
225 FILETIME exit_time;
226 FILETIME user_time;
227 FILETIME kernel_time;
228 BOOL result = GetThreadTimes(watched_thread_handle_,
229 &creation_time,
230 &exit_time,
231 &kernel_time,
232 &user_time);
233 DCHECK(result);
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));
253 #endif
255 } // namespace content