[sql] Integrate SQLite recover.c module.
[chromium-blink-merge.git] / content / gpu / gpu_watchdog_thread.cc
blob09e710eba76cdb177e114f48fa0be059ad611caa
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_(base::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 task_observer_(this),
36 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(false);
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());
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 // 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(
129 FROM_HERE,
130 base::Bind(&GpuWatchdogThread::OnCheck, weak_factory_.GetWeakPtr(),
131 was_suspended),
132 base::TimeDelta::FromMilliseconds(kCheckPeriodMs));
135 void GpuWatchdogThread::OnCheck(bool after_suspend) {
136 if (armed_)
137 return;
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.
142 armed_ = true;
144 #if defined(OS_WIN)
145 arm_cpu_time_ = GetWatchedThreadTime();
146 #endif
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(
157 FROM_HERE,
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(
163 FROM_HERE,
164 base::Bind(
165 &GpuWatchdogThread::DeliberatelyTerminateToRecoverFromHang,
166 weak_factory_.GetWeakPtr()),
167 timeout);
170 // Use the --disable-gpu-watchdog command line switch to disable this.
171 void GpuWatchdogThread::DeliberatelyTerminateToRecoverFromHang() {
172 #if defined(OS_WIN)
173 // Defer termination until a certain amount of CPU time has elapsed on the
174 // watched thread.
175 base::TimeDelta time_since_arm = GetWatchedThreadTime() - arm_cpu_time_;
176 if (time_since_arm < timeout_) {
177 message_loop()->PostDelayedTask(
178 FROM_HERE,
179 base::Bind(
180 &GpuWatchdogThread::DeliberatelyTerminateToRecoverFromHang,
181 weak_factory_.GetWeakPtr()),
182 timeout_ - time_since_arm);
183 return;
185 #endif
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_) {
192 armed_ = false;
193 OnCheck(true);
194 return;
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
199 // useful.
200 static bool terminated = false;
201 if (terminated)
202 return;
204 #if defined(OS_WIN)
205 if (IsDebuggerPresent())
206 return;
207 #endif
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;
215 terminated = true;
218 #if defined(OS_WIN)
219 base::TimeDelta GpuWatchdogThread::GetWatchedThreadTime() {
220 FILETIME creation_time;
221 FILETIME exit_time;
222 FILETIME user_time;
223 FILETIME kernel_time;
224 BOOL result = GetThreadTimes(watched_thread_handle_,
225 &creation_time,
226 &exit_time,
227 &kernel_time,
228 &user_time);
229 DCHECK(result);
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));
249 #endif
251 } // namespace content