Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(107)

Side by Side Diff: content/gpu/gpu_watchdog_thread.cc

Issue 836473003: Avoid gpu watchdog crash on timeout if X is un-responsive. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: review comments addressed. Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « content/gpu/gpu_watchdog_thread.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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/bind.h" 11 #include "base/bind.h"
12 #include "base/bind_helpers.h" 12 #include "base/bind_helpers.h"
13 #include "base/command_line.h" 13 #include "base/command_line.h"
14 #include "base/compiler_specific.h" 14 #include "base/compiler_specific.h"
15 #include "base/files/file_util.h" 15 #include "base/files/file_util.h"
16 #include "base/power_monitor/power_monitor.h" 16 #include "base/power_monitor/power_monitor.h"
17 #include "base/process/process.h" 17 #include "base/process/process.h"
18 #include "build/build_config.h" 18 #include "build/build_config.h"
19 #include "content/public/common/content_switches.h" 19 #include "content/public/common/content_switches.h"
20 #include "content/public/common/result_codes.h" 20 #include "content/public/common/result_codes.h"
21 21
22 namespace content { 22 namespace content {
23 namespace { 23 namespace {
24 const int64 kCheckPeriodMs = 2000; 24 const int64 kCheckPeriodMs = 2000;
25 #if defined(OS_CHROMEOS) 25 #if defined(OS_CHROMEOS)
26 const base::FilePath::CharType 26 const base::FilePath::CharType
27 kTtyFilePath[] = FILE_PATH_LITERAL("/sys/class/tty/tty0/active"); 27 kTtyFilePath[] = FILE_PATH_LITERAL("/sys/class/tty/tty0/active");
28 #endif 28 #endif
29 #if defined(USE_X11)
30 const unsigned char text[20] = "check";
31 #endif
29 } // namespace 32 } // namespace
30 33
31 GpuWatchdogThread::GpuWatchdogThread(int timeout) 34 GpuWatchdogThread::GpuWatchdogThread(int timeout)
32 : base::Thread("Watchdog"), 35 : base::Thread("Watchdog"),
33 watched_message_loop_(base::MessageLoop::current()), 36 watched_message_loop_(base::MessageLoop::current()),
34 timeout_(base::TimeDelta::FromMilliseconds(timeout)), 37 timeout_(base::TimeDelta::FromMilliseconds(timeout)),
35 armed_(false), 38 armed_(false),
36 #if defined(OS_WIN) 39 #if defined(OS_WIN)
37 watched_thread_handle_(0), 40 watched_thread_handle_(0),
38 arm_cpu_time_(), 41 arm_cpu_time_(),
39 #endif 42 #endif
40 task_observer_(this), 43 task_observer_(this),
41 suspended_(false), 44 suspended_(false),
45 #if defined(USE_X11)
46 display_(NULL),
47 window_(0),
48 atom_(None),
49 #endif
42 weak_factory_(this) { 50 weak_factory_(this) {
43 DCHECK(timeout >= 0); 51 DCHECK(timeout >= 0);
44 52
45 #if defined(OS_WIN) 53 #if defined(OS_WIN)
46 // GetCurrentThread returns a pseudo-handle that cannot be used by one thread 54 // GetCurrentThread returns a pseudo-handle that cannot be used by one thread
47 // to identify another. DuplicateHandle creates a "real" handle that can be 55 // to identify another. DuplicateHandle creates a "real" handle that can be
48 // used for this purpose. 56 // used for this purpose.
49 BOOL result = DuplicateHandle(GetCurrentProcess(), 57 BOOL result = DuplicateHandle(GetCurrentProcess(),
50 GetCurrentThread(), 58 GetCurrentThread(),
51 GetCurrentProcess(), 59 GetCurrentProcess(),
52 &watched_thread_handle_, 60 &watched_thread_handle_,
53 THREAD_QUERY_INFORMATION, 61 THREAD_QUERY_INFORMATION,
54 FALSE, 62 FALSE,
55 0); 63 0);
56 DCHECK(result); 64 DCHECK(result);
57 #endif 65 #endif
58 66
59 #if defined(OS_CHROMEOS) 67 #if defined(OS_CHROMEOS)
60 tty_file_ = base::OpenFile(base::FilePath(kTtyFilePath), "r"); 68 tty_file_ = base::OpenFile(base::FilePath(kTtyFilePath), "r");
61 #endif 69 #endif
70 #if defined(USE_X11)
71 SetupXServer();
72 #endif
62 watched_message_loop_->AddTaskObserver(&task_observer_); 73 watched_message_loop_->AddTaskObserver(&task_observer_);
63 } 74 }
64 75
65 void GpuWatchdogThread::PostAcknowledge() { 76 void GpuWatchdogThread::PostAcknowledge() {
66 // Called on the monitored thread. Responds with OnAcknowledge. Cannot use 77 // Called on the monitored thread. Responds with OnAcknowledge. Cannot use
67 // the method factory. Rely on reference counting instead. 78 // the method factory. Rely on reference counting instead.
68 message_loop()->PostTask( 79 message_loop()->PostTask(
69 FROM_HERE, 80 FROM_HERE,
70 base::Bind(&GpuWatchdogThread::OnAcknowledge, this)); 81 base::Bind(&GpuWatchdogThread::OnAcknowledge, this));
71 } 82 }
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
116 127
117 base::PowerMonitor* power_monitor = base::PowerMonitor::Get(); 128 base::PowerMonitor* power_monitor = base::PowerMonitor::Get();
118 if (power_monitor) 129 if (power_monitor)
119 power_monitor->RemoveObserver(this); 130 power_monitor->RemoveObserver(this);
120 131
121 #if defined(OS_CHROMEOS) 132 #if defined(OS_CHROMEOS)
122 if (tty_file_) 133 if (tty_file_)
123 fclose(tty_file_); 134 fclose(tty_file_);
124 #endif 135 #endif
125 136
137 #if defined(USE_X11)
138 XDestroyWindow(display_, window_);
139 XCloseDisplay(display_);
140 #endif
141
126 watched_message_loop_->RemoveTaskObserver(&task_observer_); 142 watched_message_loop_->RemoveTaskObserver(&task_observer_);
127 } 143 }
128 144
129 void GpuWatchdogThread::OnAcknowledge() { 145 void GpuWatchdogThread::OnAcknowledge() {
130 CHECK(base::PlatformThread::CurrentId() == thread_id()); 146 CHECK(base::PlatformThread::CurrentId() == thread_id());
131 147
132 // The check has already been acknowledged and another has already been 148 // The check has already been acknowledged and another has already been
133 // scheduled by a previous call to OnAcknowledge. It is normal for a 149 // scheduled by a previous call to OnAcknowledge. It is normal for a
134 // watched thread to see armed_ being true multiple times before 150 // watched thread to see armed_ being true multiple times before
135 // the OnAcknowledge task is run on the watchdog thread. 151 // the OnAcknowledge task is run on the watchdog thread.
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
181 // TaskObserver. Any other tasks that are pending on the watched thread will 197 // TaskObserver. Any other tasks that are pending on the watched thread will
182 // also wake up the observer. This simply ensures there is at least one. 198 // also wake up the observer. This simply ensures there is at least one.
183 watched_message_loop_->PostTask( 199 watched_message_loop_->PostTask(
184 FROM_HERE, 200 FROM_HERE,
185 base::Bind(&base::DoNothing)); 201 base::Bind(&base::DoNothing));
186 202
187 // Post a task to the watchdog thread to exit if the monitored thread does 203 // Post a task to the watchdog thread to exit if the monitored thread does
188 // not respond in time. 204 // not respond in time.
189 message_loop()->PostDelayedTask( 205 message_loop()->PostDelayedTask(
190 FROM_HERE, 206 FROM_HERE,
191 base::Bind( 207 base::Bind(&GpuWatchdogThread::DeliberatelyTerminateToRecoverFromHang,
192 &GpuWatchdogThread::DeliberatelyTerminateToRecoverFromHang, 208 weak_factory_.GetWeakPtr()),
193 weak_factory_.GetWeakPtr()),
194 timeout); 209 timeout);
195 } 210 }
196 211
197 // Use the --disable-gpu-watchdog command line switch to disable this. 212 // Use the --disable-gpu-watchdog command line switch to disable this.
198 void GpuWatchdogThread::DeliberatelyTerminateToRecoverFromHang() { 213 void GpuWatchdogThread::DeliberatelyTerminateToRecoverFromHang() {
199 // Should not get here while the system is suspended. 214 // Should not get here while the system is suspended.
200 DCHECK(!suspended_); 215 DCHECK(!suspended_);
201 216
202 #if defined(OS_WIN) 217 #if defined(OS_WIN)
203 // Defer termination until a certain amount of CPU time has elapsed on the 218 // Defer termination until a certain amount of CPU time has elapsed on the
(...skipping 13 matching lines...) Expand all
217 // If the watchdog woke up significantly behind schedule, disarm and reset 232 // If the watchdog woke up significantly behind schedule, disarm and reset
218 // the watchdog check. This is to prevent the watchdog thread from terminating 233 // the watchdog check. This is to prevent the watchdog thread from terminating
219 // when a machine wakes up from sleep or hibernation, which would otherwise 234 // when a machine wakes up from sleep or hibernation, which would otherwise
220 // appear to be a hang. 235 // appear to be a hang.
221 if (base::Time::Now() > suspension_timeout_) { 236 if (base::Time::Now() > suspension_timeout_) {
222 armed_ = false; 237 armed_ = false;
223 OnCheck(true); 238 OnCheck(true);
224 return; 239 return;
225 } 240 }
226 241
242 #if defined(USE_X11)
243 XWindowAttributes attributes;
244 XGetWindowAttributes(display_, window_, &attributes);
245
246 XSelectInput(display_, window_, PropertyChangeMask);
247 SetupXChangeProp();
248
249 XFlush(display_);
250
251 // We wait for the property change event with a timeout. If it arrives we know
252 // that X is responsive and is not the cause of the watchdog trigger, so we
253 // should
254 // terminate. If it times out, it may be due to X taking a long time, but
255 // terminating won't help, so ignore the watchdog trigger.
256 XEvent event_return;
257 base::TimeTicks deadline = base::TimeTicks::Now() + timeout_;
258 while (true) {
259 base::TimeDelta delta = deadline - base::TimeTicks::Now();
260 if (delta < base::TimeDelta()) {
261 return;
262 } else {
263 while (XCheckWindowEvent(display_, window_, PropertyChangeMask,
264 &event_return)) {
265 if (MatchXEventAtom(&event_return))
266 break;
267 }
268 struct pollfd fds[1];
269 fds[0].fd = XConnectionNumber(display_);
270 fds[0].events = POLLIN;
271 int status = poll(fds, 1, delta.InMilliseconds());
272 if (status == -1) {
273 if (errno == EINTR) {
274 continue;
275 } else {
276 LOG(FATAL) << "Lost X connection, aborting.";
277 break;
278 }
279 } else if (status == 0) {
280 return;
281 } else {
282 continue;
283 }
284 }
285 }
286 #endif
287
227 // For minimal developer annoyance, don't keep terminating. You need to skip 288 // For minimal developer annoyance, don't keep terminating. You need to skip
228 // the call to base::Process::Terminate below in a debugger for this to be 289 // the call to base::Process::Terminate below in a debugger for this to be
229 // useful. 290 // useful.
230 static bool terminated = false; 291 static bool terminated = false;
231 if (terminated) 292 if (terminated)
232 return; 293 return;
233 294
234 #if defined(OS_WIN) 295 #if defined(OS_WIN)
235 if (IsDebuggerPresent()) 296 if (IsDebuggerPresent())
236 return; 297 return;
(...skipping 15 matching lines...) Expand all
252 313
253 LOG(ERROR) << "The GPU process hung. Terminating after " 314 LOG(ERROR) << "The GPU process hung. Terminating after "
254 << timeout_.InMilliseconds() << " ms."; 315 << timeout_.InMilliseconds() << " ms.";
255 316
256 // Deliberately crash the process to create a crash dump. 317 // Deliberately crash the process to create a crash dump.
257 *((volatile int*)0) = 0x1337; 318 *((volatile int*)0) = 0x1337;
258 319
259 terminated = true; 320 terminated = true;
260 } 321 }
261 322
323 #if defined(USE_X11)
324 void GpuWatchdogThread::SetupXServer() {
325 display_ = XOpenDisplay(NULL);
326 window_ = XCreateWindow(display_, DefaultRootWindow(display_), 0, 0, 1, 1, 0,
327 CopyFromParent, InputOutput, CopyFromParent, 0, NULL);
328 atom_ = XInternAtom(display_, "CHECK", False);
329 }
330
331 void GpuWatchdogThread::SetupXChangeProp() {
332 XChangeProperty(display_, window_, atom_, XA_STRING, 8, PropModeReplace, text,
333 (arraysize(text) - 1));
334 }
335
336 bool GpuWatchdogThread::MatchXEventAtom(XEvent* event) {
337 if (event->xproperty.window == window_ && event->type == PropertyNotify &&
338 event->xproperty.atom == atom_)
339 return true;
340
341 return false;
342 }
343
344 #endif
262 void GpuWatchdogThread::AddPowerObserver() { 345 void GpuWatchdogThread::AddPowerObserver() {
263 message_loop()->PostTask( 346 message_loop()->PostTask(
264 FROM_HERE, 347 FROM_HERE,
265 base::Bind(&GpuWatchdogThread::OnAddPowerObserver, this)); 348 base::Bind(&GpuWatchdogThread::OnAddPowerObserver, this));
266 } 349 }
267 350
268 void GpuWatchdogThread::OnAddPowerObserver() { 351 void GpuWatchdogThread::OnAddPowerObserver() {
269 base::PowerMonitor* power_monitor = base::PowerMonitor::Get(); 352 base::PowerMonitor* power_monitor = base::PowerMonitor::Get();
270 DCHECK(power_monitor); 353 DCHECK(power_monitor);
271 power_monitor->AddObserver(this); 354 power_monitor->AddObserver(this);
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
314 // not increasing. The other is where either the kernel hangs and never 397 // not increasing. The other is where either the kernel hangs and never
315 // returns to user level or where user level code 398 // returns to user level or where user level code
316 // calls into kernel level repeatedly, giving up its quanta before it is 399 // calls into kernel level repeatedly, giving up its quanta before it is
317 // tracked, for example a loop that repeatedly Sleeps. 400 // tracked, for example a loop that repeatedly Sleeps.
318 return base::TimeDelta::FromMilliseconds(static_cast<int64>( 401 return base::TimeDelta::FromMilliseconds(static_cast<int64>(
319 (user_time64.QuadPart + kernel_time64.QuadPart) / 10000)); 402 (user_time64.QuadPart + kernel_time64.QuadPart) / 10000));
320 } 403 }
321 #endif 404 #endif
322 405
323 } // namespace content 406 } // namespace content
OLDNEW
« no previous file with comments | « content/gpu/gpu_watchdog_thread.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698