| 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 |
| 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/location.h" | |
| 17 #include "base/power_monitor/power_monitor.h" | 16 #include "base/power_monitor/power_monitor.h" |
| 18 #include "base/process/process.h" | 17 #include "base/process/process.h" |
| 19 #include "base/single_thread_task_runner.h" | |
| 20 #include "build/build_config.h" | 18 #include "build/build_config.h" |
| 21 #include "content/public/common/content_switches.h" | 19 #include "content/public/common/content_switches.h" |
| 22 #include "content/public/common/result_codes.h" | 20 #include "content/public/common/result_codes.h" |
| 23 | 21 |
| 24 namespace content { | 22 namespace content { |
| 25 namespace { | 23 namespace { |
| 26 #if defined(OS_CHROMEOS) | 24 #if defined(OS_CHROMEOS) |
| 27 const base::FilePath::CharType | 25 const base::FilePath::CharType |
| 28 kTtyFilePath[] = FILE_PATH_LITERAL("/sys/class/tty/tty0/active"); | 26 kTtyFilePath[] = FILE_PATH_LITERAL("/sys/class/tty/tty0/active"); |
| 29 #endif | 27 #endif |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 #endif | 68 #endif |
| 71 #if defined(USE_X11) | 69 #if defined(USE_X11) |
| 72 SetupXServer(); | 70 SetupXServer(); |
| 73 #endif | 71 #endif |
| 74 watched_message_loop_->AddTaskObserver(&task_observer_); | 72 watched_message_loop_->AddTaskObserver(&task_observer_); |
| 75 } | 73 } |
| 76 | 74 |
| 77 void GpuWatchdogThread::PostAcknowledge() { | 75 void GpuWatchdogThread::PostAcknowledge() { |
| 78 // Called on the monitored thread. Responds with OnAcknowledge. Cannot use | 76 // Called on the monitored thread. Responds with OnAcknowledge. Cannot use |
| 79 // the method factory. Rely on reference counting instead. | 77 // the method factory. Rely on reference counting instead. |
| 80 task_runner()->PostTask(FROM_HERE, | 78 message_loop()->PostTask( |
| 81 base::Bind(&GpuWatchdogThread::OnAcknowledge, this)); | 79 FROM_HERE, |
| 80 base::Bind(&GpuWatchdogThread::OnAcknowledge, this)); |
| 82 } | 81 } |
| 83 | 82 |
| 84 void GpuWatchdogThread::CheckArmed() { | 83 void GpuWatchdogThread::CheckArmed() { |
| 85 // Acknowledge the watchdog if it has armed itself. The watchdog will not | 84 // Acknowledge the watchdog if it has armed itself. The watchdog will not |
| 86 // change its armed state until it is acknowledged. | 85 // change its armed state until it is acknowledged. |
| 87 if (armed()) { | 86 if (armed()) { |
| 88 PostAcknowledge(); | 87 PostAcknowledge(); |
| 89 } | 88 } |
| 90 } | 89 } |
| 91 | 90 |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 armed_ = false; | 155 armed_ = false; |
| 157 | 156 |
| 158 if (suspended_) | 157 if (suspended_) |
| 159 return; | 158 return; |
| 160 | 159 |
| 161 // If it took a long time for the acknowledgement, assume the computer was | 160 // If it took a long time for the acknowledgement, assume the computer was |
| 162 // recently suspended. | 161 // recently suspended. |
| 163 bool was_suspended = (base::Time::Now() > suspension_timeout_); | 162 bool was_suspended = (base::Time::Now() > suspension_timeout_); |
| 164 | 163 |
| 165 // The monitored thread has responded. Post a task to check it again. | 164 // The monitored thread has responded. Post a task to check it again. |
| 166 task_runner()->PostDelayedTask( | 165 message_loop()->PostDelayedTask( |
| 167 FROM_HERE, base::Bind(&GpuWatchdogThread::OnCheck, | 166 FROM_HERE, |
| 168 weak_factory_.GetWeakPtr(), was_suspended), | 167 base::Bind(&GpuWatchdogThread::OnCheck, weak_factory_.GetWeakPtr(), |
| 168 was_suspended), |
| 169 0.5 * timeout_); | 169 0.5 * timeout_); |
| 170 } | 170 } |
| 171 | 171 |
| 172 void GpuWatchdogThread::OnCheck(bool after_suspend) { | 172 void GpuWatchdogThread::OnCheck(bool after_suspend) { |
| 173 CHECK(base::PlatformThread::CurrentId() == thread_id()); | 173 CHECK(base::PlatformThread::CurrentId() == thread_id()); |
| 174 | 174 |
| 175 // Do not create any new termination tasks if one has already been created | 175 // Do not create any new termination tasks if one has already been created |
| 176 // or the system is suspended. | 176 // or the system is suspended. |
| 177 if (armed_ || suspended_) | 177 if (armed_ || suspended_) |
| 178 return; | 178 return; |
| 179 | 179 |
| 180 // Must set armed before posting the task. This task might be the only task | 180 // Must set armed before posting the task. This task might be the only task |
| 181 // that will activate the TaskObserver on the watched thread and it must not | 181 // that will activate the TaskObserver on the watched thread and it must not |
| 182 // miss the false -> true transition. | 182 // miss the false -> true transition. |
| 183 armed_ = true; | 183 armed_ = true; |
| 184 | 184 |
| 185 #if defined(OS_WIN) | 185 #if defined(OS_WIN) |
| 186 arm_cpu_time_ = GetWatchedThreadTime(); | 186 arm_cpu_time_ = GetWatchedThreadTime(); |
| 187 #endif | 187 #endif |
| 188 | 188 |
| 189 // Immediately after the computer is woken up from being suspended it might | 189 // Immediately after the computer is woken up from being suspended it might |
| 190 // be pretty sluggish, so allow some extra time before the next timeout. | 190 // be pretty sluggish, so allow some extra time before the next timeout. |
| 191 base::TimeDelta timeout = timeout_ * (after_suspend ? 3 : 1); | 191 base::TimeDelta timeout = timeout_ * (after_suspend ? 3 : 1); |
| 192 suspension_timeout_ = base::Time::Now() + timeout * 2; | 192 suspension_timeout_ = base::Time::Now() + timeout * 2; |
| 193 | 193 |
| 194 // Post a task to the monitored thread that does nothing but wake up the | 194 // Post a task to the monitored thread that does nothing but wake up the |
| 195 // TaskObserver. Any other tasks that are pending on the watched thread will | 195 // TaskObserver. Any other tasks that are pending on the watched thread will |
| 196 // also wake up the observer. This simply ensures there is at least one. | 196 // also wake up the observer. This simply ensures there is at least one. |
| 197 watched_message_loop_->task_runner()->PostTask(FROM_HERE, | 197 watched_message_loop_->PostTask( |
| 198 base::Bind(&base::DoNothing)); | 198 FROM_HERE, |
| 199 base::Bind(&base::DoNothing)); |
| 199 | 200 |
| 200 // Post a task to the watchdog thread to exit if the monitored thread does | 201 // Post a task to the watchdog thread to exit if the monitored thread does |
| 201 // not respond in time. | 202 // not respond in time. |
| 202 task_runner()->PostDelayedTask( | 203 message_loop()->PostDelayedTask( |
| 203 FROM_HERE, | 204 FROM_HERE, |
| 204 base::Bind(&GpuWatchdogThread::DeliberatelyTerminateToRecoverFromHang, | 205 base::Bind(&GpuWatchdogThread::DeliberatelyTerminateToRecoverFromHang, |
| 205 weak_factory_.GetWeakPtr()), | 206 weak_factory_.GetWeakPtr()), |
| 206 timeout); | 207 timeout); |
| 207 } | 208 } |
| 208 | 209 |
| 209 // Use the --disable-gpu-watchdog command line switch to disable this. | 210 // Use the --disable-gpu-watchdog command line switch to disable this. |
| 210 void GpuWatchdogThread::DeliberatelyTerminateToRecoverFromHang() { | 211 void GpuWatchdogThread::DeliberatelyTerminateToRecoverFromHang() { |
| 211 // Should not get here while the system is suspended. | 212 // Should not get here while the system is suspended. |
| 212 DCHECK(!suspended_); | 213 DCHECK(!suspended_); |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 333 bool GpuWatchdogThread::MatchXEventAtom(XEvent* event) { | 334 bool GpuWatchdogThread::MatchXEventAtom(XEvent* event) { |
| 334 if (event->xproperty.window == window_ && event->type == PropertyNotify && | 335 if (event->xproperty.window == window_ && event->type == PropertyNotify && |
| 335 event->xproperty.atom == atom_) | 336 event->xproperty.atom == atom_) |
| 336 return true; | 337 return true; |
| 337 | 338 |
| 338 return false; | 339 return false; |
| 339 } | 340 } |
| 340 | 341 |
| 341 #endif | 342 #endif |
| 342 void GpuWatchdogThread::AddPowerObserver() { | 343 void GpuWatchdogThread::AddPowerObserver() { |
| 343 task_runner()->PostTask( | 344 message_loop()->PostTask( |
| 344 FROM_HERE, base::Bind(&GpuWatchdogThread::OnAddPowerObserver, this)); | 345 FROM_HERE, |
| 346 base::Bind(&GpuWatchdogThread::OnAddPowerObserver, this)); |
| 345 } | 347 } |
| 346 | 348 |
| 347 void GpuWatchdogThread::OnAddPowerObserver() { | 349 void GpuWatchdogThread::OnAddPowerObserver() { |
| 348 base::PowerMonitor* power_monitor = base::PowerMonitor::Get(); | 350 base::PowerMonitor* power_monitor = base::PowerMonitor::Get(); |
| 349 DCHECK(power_monitor); | 351 DCHECK(power_monitor); |
| 350 power_monitor->AddObserver(this); | 352 power_monitor->AddObserver(this); |
| 351 } | 353 } |
| 352 | 354 |
| 353 void GpuWatchdogThread::OnSuspend() { | 355 void GpuWatchdogThread::OnSuspend() { |
| 354 suspended_ = true; | 356 suspended_ = true; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 393 // not increasing. The other is where either the kernel hangs and never | 395 // not increasing. The other is where either the kernel hangs and never |
| 394 // returns to user level or where user level code | 396 // returns to user level or where user level code |
| 395 // calls into kernel level repeatedly, giving up its quanta before it is | 397 // calls into kernel level repeatedly, giving up its quanta before it is |
| 396 // tracked, for example a loop that repeatedly Sleeps. | 398 // tracked, for example a loop that repeatedly Sleeps. |
| 397 return base::TimeDelta::FromMilliseconds(static_cast<int64>( | 399 return base::TimeDelta::FromMilliseconds(static_cast<int64>( |
| 398 (user_time64.QuadPart + kernel_time64.QuadPart) / 10000)); | 400 (user_time64.QuadPart + kernel_time64.QuadPart) / 10000)); |
| 399 } | 401 } |
| 400 #endif | 402 #endif |
| 401 | 403 |
| 402 } // namespace content | 404 } // namespace content |
| OLD | NEW |