OLD | NEW |
---|---|
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 |
(...skipping 10 matching lines...) Expand all Loading... | |
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 } // namespace | 29 } // namespace |
30 | 30 |
31 #if defined(USE_X11) | |
32 const unsigned char text[20] = "check"; | |
33 #endif | |
piman
2015/02/13 22:28:38
nit: move into anonymous namespace above.
| |
34 | |
31 GpuWatchdogThread::GpuWatchdogThread(int timeout) | 35 GpuWatchdogThread::GpuWatchdogThread(int timeout) |
32 : base::Thread("Watchdog"), | 36 : base::Thread("Watchdog"), |
33 watched_message_loop_(base::MessageLoop::current()), | 37 watched_message_loop_(base::MessageLoop::current()), |
34 timeout_(base::TimeDelta::FromMilliseconds(timeout)), | 38 timeout_(base::TimeDelta::FromMilliseconds(timeout)), |
35 armed_(false), | 39 armed_(false), |
36 #if defined(OS_WIN) | 40 #if defined(OS_WIN) |
37 watched_thread_handle_(0), | 41 watched_thread_handle_(0), |
38 arm_cpu_time_(), | 42 arm_cpu_time_(), |
39 #endif | 43 #endif |
40 task_observer_(this), | 44 task_observer_(this), |
41 suspended_(false), | 45 suspended_(false), |
46 #if defined(USE_X11) | |
47 display_(NULL), | |
48 window_(0), | |
49 atom_(None), | |
50 #endif | |
42 weak_factory_(this) { | 51 weak_factory_(this) { |
43 DCHECK(timeout >= 0); | 52 DCHECK(timeout >= 0); |
44 | 53 |
45 #if defined(OS_WIN) | 54 #if defined(OS_WIN) |
46 // GetCurrentThread returns a pseudo-handle that cannot be used by one thread | 55 // GetCurrentThread returns a pseudo-handle that cannot be used by one thread |
47 // to identify another. DuplicateHandle creates a "real" handle that can be | 56 // to identify another. DuplicateHandle creates a "real" handle that can be |
48 // used for this purpose. | 57 // used for this purpose. |
49 BOOL result = DuplicateHandle(GetCurrentProcess(), | 58 BOOL result = DuplicateHandle(GetCurrentProcess(), |
50 GetCurrentThread(), | 59 GetCurrentThread(), |
51 GetCurrentProcess(), | 60 GetCurrentProcess(), |
52 &watched_thread_handle_, | 61 &watched_thread_handle_, |
53 THREAD_QUERY_INFORMATION, | 62 THREAD_QUERY_INFORMATION, |
54 FALSE, | 63 FALSE, |
55 0); | 64 0); |
56 DCHECK(result); | 65 DCHECK(result); |
57 #endif | 66 #endif |
58 | 67 |
59 #if defined(OS_CHROMEOS) | 68 #if defined(OS_CHROMEOS) |
60 tty_file_ = base::OpenFile(base::FilePath(kTtyFilePath), "r"); | 69 tty_file_ = base::OpenFile(base::FilePath(kTtyFilePath), "r"); |
61 #endif | 70 #endif |
71 #if defined(USE_X11) | |
72 SetupXServer(); | |
73 #endif | |
62 watched_message_loop_->AddTaskObserver(&task_observer_); | 74 watched_message_loop_->AddTaskObserver(&task_observer_); |
63 } | 75 } |
64 | 76 |
65 void GpuWatchdogThread::PostAcknowledge() { | 77 void GpuWatchdogThread::PostAcknowledge() { |
66 // Called on the monitored thread. Responds with OnAcknowledge. Cannot use | 78 // Called on the monitored thread. Responds with OnAcknowledge. Cannot use |
67 // the method factory. Rely on reference counting instead. | 79 // the method factory. Rely on reference counting instead. |
68 message_loop()->PostTask( | 80 message_loop()->PostTask( |
69 FROM_HERE, | 81 FROM_HERE, |
70 base::Bind(&GpuWatchdogThread::OnAcknowledge, this)); | 82 base::Bind(&GpuWatchdogThread::OnAcknowledge, this)); |
71 } | 83 } |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
116 | 128 |
117 base::PowerMonitor* power_monitor = base::PowerMonitor::Get(); | 129 base::PowerMonitor* power_monitor = base::PowerMonitor::Get(); |
118 if (power_monitor) | 130 if (power_monitor) |
119 power_monitor->RemoveObserver(this); | 131 power_monitor->RemoveObserver(this); |
120 | 132 |
121 #if defined(OS_CHROMEOS) | 133 #if defined(OS_CHROMEOS) |
122 if (tty_file_) | 134 if (tty_file_) |
123 fclose(tty_file_); | 135 fclose(tty_file_); |
124 #endif | 136 #endif |
125 | 137 |
138 #if defined(USE_X11) | |
139 XDestroyWindow(display_, window_); | |
140 XCloseDisplay(display_); | |
141 #endif | |
142 | |
126 watched_message_loop_->RemoveTaskObserver(&task_observer_); | 143 watched_message_loop_->RemoveTaskObserver(&task_observer_); |
127 } | 144 } |
128 | 145 |
129 void GpuWatchdogThread::OnAcknowledge() { | 146 void GpuWatchdogThread::OnAcknowledge() { |
130 CHECK(base::PlatformThread::CurrentId() == thread_id()); | 147 CHECK(base::PlatformThread::CurrentId() == thread_id()); |
131 | 148 |
132 // The check has already been acknowledged and another has already been | 149 // The check has already been acknowledged and another has already been |
133 // scheduled by a previous call to OnAcknowledge. It is normal for a | 150 // scheduled by a previous call to OnAcknowledge. It is normal for a |
134 // watched thread to see armed_ being true multiple times before | 151 // watched thread to see armed_ being true multiple times before |
135 // the OnAcknowledge task is run on the watchdog thread. | 152 // the OnAcknowledge task is run on the watchdog thread. |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
181 // TaskObserver. Any other tasks that are pending on the watched thread will | 198 // 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. | 199 // also wake up the observer. This simply ensures there is at least one. |
183 watched_message_loop_->PostTask( | 200 watched_message_loop_->PostTask( |
184 FROM_HERE, | 201 FROM_HERE, |
185 base::Bind(&base::DoNothing)); | 202 base::Bind(&base::DoNothing)); |
186 | 203 |
187 // Post a task to the watchdog thread to exit if the monitored thread does | 204 // Post a task to the watchdog thread to exit if the monitored thread does |
188 // not respond in time. | 205 // not respond in time. |
189 message_loop()->PostDelayedTask( | 206 message_loop()->PostDelayedTask( |
190 FROM_HERE, | 207 FROM_HERE, |
191 base::Bind( | 208 base::Bind(&GpuWatchdogThread::DeliberatelyTerminateToRecoverFromHang, |
192 &GpuWatchdogThread::DeliberatelyTerminateToRecoverFromHang, | 209 weak_factory_.GetWeakPtr()), |
193 weak_factory_.GetWeakPtr()), | |
194 timeout); | 210 timeout); |
195 } | 211 } |
196 | 212 |
197 // Use the --disable-gpu-watchdog command line switch to disable this. | 213 // Use the --disable-gpu-watchdog command line switch to disable this. |
198 void GpuWatchdogThread::DeliberatelyTerminateToRecoverFromHang() { | 214 void GpuWatchdogThread::DeliberatelyTerminateToRecoverFromHang() { |
199 // Should not get here while the system is suspended. | 215 // Should not get here while the system is suspended. |
200 DCHECK(!suspended_); | 216 DCHECK(!suspended_); |
201 | 217 |
202 #if defined(OS_WIN) | 218 #if defined(OS_WIN) |
203 // Defer termination until a certain amount of CPU time has elapsed on the | 219 // Defer termination until a certain amount of CPU time has elapsed on the |
(...skipping 13 matching lines...) Expand all Loading... | |
217 // If the watchdog woke up significantly behind schedule, disarm and reset | 233 // If the watchdog woke up significantly behind schedule, disarm and reset |
218 // the watchdog check. This is to prevent the watchdog thread from terminating | 234 // 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 | 235 // when a machine wakes up from sleep or hibernation, which would otherwise |
220 // appear to be a hang. | 236 // appear to be a hang. |
221 if (base::Time::Now() > suspension_timeout_) { | 237 if (base::Time::Now() > suspension_timeout_) { |
222 armed_ = false; | 238 armed_ = false; |
223 OnCheck(true); | 239 OnCheck(true); |
224 return; | 240 return; |
225 } | 241 } |
226 | 242 |
243 #if defined(USE_X11) | |
244 XWindowAttributes attributes; | |
245 XGetWindowAttributes(display_, window_, &attributes); | |
246 | |
247 XSelectInput(display_, window_, PropertyChangeMask); | |
248 SetupXChangeProp(); | |
249 | |
250 XFlush(display_); | |
251 | |
252 XEvent event_return; | |
253 base::TimeTicks deadline = base::TimeTicks::Now() + timeout_; | |
254 while (true) { | |
255 base::TimeDelta delta = deadline - base::TimeTicks::Now(); | |
256 if (delta < base::TimeDelta()) { | |
257 // timeout | |
piman
2015/02/13 22:28:38
It would be more useful to have a comment describi
sohanjg
2015/02/16 09:44:49
Done.
| |
258 return; | |
259 } else { | |
260 if (XCheckWindowEvent(display_, window_, PropertyChangeMask, | |
261 &event_return)) { | |
262 // got property | |
piman
2015/02/13 22:28:38
nit: remove comment
sohanjg
2015/02/16 09:44:49
Done.
| |
263 if (MatchXWindowProp()) | |
264 break; | |
265 } | |
266 struct pollfd fds[1]; | |
267 fds[0].fd = XConnectionNumber(display_); | |
268 fds[0].events = POLLIN; | |
269 int status = poll(fds, 1, delta.InMilliseconds()); | |
270 if (status == -1) { | |
271 if (errno == EINTR) | |
272 // restart | |
piman
2015/02/13 22:28:38
nit: remove comment
sohanjg
2015/02/16 09:44:49
Done.
| |
273 continue; | |
274 else | |
275 // error, abort | |
piman
2015/02/13 22:28:38
nit: remove comment
sohanjg
2015/02/16 09:44:49
Done.
| |
276 break; | |
piman
2015/02/13 22:28:38
In this case, we should LOG(FATAL) << "Lost X conn
sohanjg
2015/02/16 09:44:49
Done.
| |
277 } else if (status == 0) { | |
278 // timeout | |
piman
2015/02/13 22:28:38
nit: remove comment
| |
279 return; | |
280 } else { | |
281 while (true) { | |
piman
2015/02/13 22:28:38
I don't think this second block makes sense. Can w
sohanjg
2015/02/16 09:44:49
Done.
| |
282 base::TimeDelta delta2 = deadline - base::TimeTicks::Now(); | |
283 if (delta2 < base::TimeDelta()) | |
284 // timeout | |
285 return; | |
286 if (XCheckWindowEvent(display_, window_, PropertyChangeMask, | |
287 &event_return)) { | |
288 if (MatchXWindowProp()) | |
289 break; | |
290 } else | |
291 continue; | |
292 } | |
293 } | |
294 } | |
295 } | |
296 #endif | |
297 | |
227 // For minimal developer annoyance, don't keep terminating. You need to skip | 298 // 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 | 299 // the call to base::Process::Terminate below in a debugger for this to be |
229 // useful. | 300 // useful. |
230 static bool terminated = false; | 301 static bool terminated = false; |
231 if (terminated) | 302 if (terminated) |
232 return; | 303 return; |
233 | 304 |
234 #if defined(OS_WIN) | 305 #if defined(OS_WIN) |
235 if (IsDebuggerPresent()) | 306 if (IsDebuggerPresent()) |
236 return; | 307 return; |
(...skipping 15 matching lines...) Expand all Loading... | |
252 | 323 |
253 LOG(ERROR) << "The GPU process hung. Terminating after " | 324 LOG(ERROR) << "The GPU process hung. Terminating after " |
254 << timeout_.InMilliseconds() << " ms."; | 325 << timeout_.InMilliseconds() << " ms."; |
255 | 326 |
256 // Deliberately crash the process to create a crash dump. | 327 // Deliberately crash the process to create a crash dump. |
257 *((volatile int*)0) = 0x1337; | 328 *((volatile int*)0) = 0x1337; |
258 | 329 |
259 terminated = true; | 330 terminated = true; |
260 } | 331 } |
261 | 332 |
333 #if defined(USE_X11) | |
334 void GpuWatchdogThread::SetupXServer() { | |
335 display_ = XOpenDisplay(NULL); | |
336 window_ = XCreateWindow(display_, DefaultRootWindow(display_), 0, 0, 1, 1, 0, | |
337 CopyFromParent, InputOutput, CopyFromParent, 0, NULL); | |
338 atom_ = XInternAtom(display_, "CHECK", False); | |
339 } | |
340 | |
341 void GpuWatchdogThread::SetupXChangeProp() { | |
342 XChangeProperty(display_, window_, atom_, XA_STRING, 8, PropModeReplace, text, | |
343 strlen((char*)text)); | |
piman
2015/02/13 22:28:38
nit: no c-style cast.
You should be able to use ar
sohanjg
2015/02/16 09:44:49
Done.
| |
344 } | |
345 | |
346 bool GpuWatchdogThread::MatchXWindowProp() { | |
347 unsigned long nitems = 0; | |
348 unsigned long nbytes = 0; | |
349 XAtom prop_type = None; | |
350 int prop_format = 0; | |
351 unsigned char* property_data = NULL; | |
352 if (XGetWindowProperty(display_, window_, atom_, 0, 65535, False, | |
piman
2015/02/13 22:28:38
I was more imagining that we'd check the X event,
sohanjg
2015/02/16 09:44:49
Done.
| |
353 AnyPropertyType, &prop_type, &prop_format, &nitems, | |
354 &nbytes, &property_data) == Success) { | |
355 if (prop_type == XA_STRING && prop_format == 8 && | |
356 nitems == strlen((char*)text)) | |
357 return true; | |
358 } | |
359 return false; | |
360 } | |
361 | |
362 #endif | |
262 void GpuWatchdogThread::AddPowerObserver() { | 363 void GpuWatchdogThread::AddPowerObserver() { |
263 message_loop()->PostTask( | 364 message_loop()->PostTask( |
264 FROM_HERE, | 365 FROM_HERE, |
265 base::Bind(&GpuWatchdogThread::OnAddPowerObserver, this)); | 366 base::Bind(&GpuWatchdogThread::OnAddPowerObserver, this)); |
266 } | 367 } |
267 | 368 |
268 void GpuWatchdogThread::OnAddPowerObserver() { | 369 void GpuWatchdogThread::OnAddPowerObserver() { |
269 base::PowerMonitor* power_monitor = base::PowerMonitor::Get(); | 370 base::PowerMonitor* power_monitor = base::PowerMonitor::Get(); |
270 DCHECK(power_monitor); | 371 DCHECK(power_monitor); |
271 power_monitor->AddObserver(this); | 372 power_monitor->AddObserver(this); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
314 // not increasing. The other is where either the kernel hangs and never | 415 // not increasing. The other is where either the kernel hangs and never |
315 // returns to user level or where user level code | 416 // returns to user level or where user level code |
316 // calls into kernel level repeatedly, giving up its quanta before it is | 417 // calls into kernel level repeatedly, giving up its quanta before it is |
317 // tracked, for example a loop that repeatedly Sleeps. | 418 // tracked, for example a loop that repeatedly Sleeps. |
318 return base::TimeDelta::FromMilliseconds(static_cast<int64>( | 419 return base::TimeDelta::FromMilliseconds(static_cast<int64>( |
319 (user_time64.QuadPart + kernel_time64.QuadPart) / 10000)); | 420 (user_time64.QuadPart + kernel_time64.QuadPart) / 10000)); |
320 } | 421 } |
321 #endif | 422 #endif |
322 | 423 |
323 } // namespace content | 424 } // namespace content |
OLD | NEW |