| 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" |
| 16 #include "base/power_monitor/power_monitor.h" | 17 #include "base/power_monitor/power_monitor.h" |
| 17 #include "base/process/process.h" | 18 #include "base/process/process.h" |
| 19 #include "base/single_thread_task_runner.h" |
| 18 #include "build/build_config.h" | 20 #include "build/build_config.h" |
| 19 #include "content/public/common/content_switches.h" | 21 #include "content/public/common/content_switches.h" |
| 20 #include "content/public/common/result_codes.h" | 22 #include "content/public/common/result_codes.h" |
| 21 | 23 |
| 22 namespace content { | 24 namespace content { |
| 23 namespace { | 25 namespace { |
| 24 #if defined(OS_CHROMEOS) | 26 #if defined(OS_CHROMEOS) |
| 25 const base::FilePath::CharType | 27 const base::FilePath::CharType |
| 26 kTtyFilePath[] = FILE_PATH_LITERAL("/sys/class/tty/tty0/active"); | 28 kTtyFilePath[] = FILE_PATH_LITERAL("/sys/class/tty/tty0/active"); |
| 27 #endif | 29 #endif |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 #endif | 70 #endif |
| 69 #if defined(USE_X11) | 71 #if defined(USE_X11) |
| 70 SetupXServer(); | 72 SetupXServer(); |
| 71 #endif | 73 #endif |
| 72 watched_message_loop_->AddTaskObserver(&task_observer_); | 74 watched_message_loop_->AddTaskObserver(&task_observer_); |
| 73 } | 75 } |
| 74 | 76 |
| 75 void GpuWatchdogThread::PostAcknowledge() { | 77 void GpuWatchdogThread::PostAcknowledge() { |
| 76 // Called on the monitored thread. Responds with OnAcknowledge. Cannot use | 78 // Called on the monitored thread. Responds with OnAcknowledge. Cannot use |
| 77 // the method factory. Rely on reference counting instead. | 79 // the method factory. Rely on reference counting instead. |
| 78 message_loop()->PostTask( | 80 task_runner()->PostTask(FROM_HERE, |
| 79 FROM_HERE, | 81 base::Bind(&GpuWatchdogThread::OnAcknowledge, this)); |
| 80 base::Bind(&GpuWatchdogThread::OnAcknowledge, this)); | |
| 81 } | 82 } |
| 82 | 83 |
| 83 void GpuWatchdogThread::CheckArmed() { | 84 void GpuWatchdogThread::CheckArmed() { |
| 84 // Acknowledge the watchdog if it has armed itself. The watchdog will not | 85 // Acknowledge the watchdog if it has armed itself. The watchdog will not |
| 85 // change its armed state until it is acknowledged. | 86 // change its armed state until it is acknowledged. |
| 86 if (armed()) { | 87 if (armed()) { |
| 87 PostAcknowledge(); | 88 PostAcknowledge(); |
| 88 } | 89 } |
| 89 } | 90 } |
| 90 | 91 |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 armed_ = false; | 156 armed_ = false; |
| 156 | 157 |
| 157 if (suspended_) | 158 if (suspended_) |
| 158 return; | 159 return; |
| 159 | 160 |
| 160 // If it took a long time for the acknowledgement, assume the computer was | 161 // If it took a long time for the acknowledgement, assume the computer was |
| 161 // recently suspended. | 162 // recently suspended. |
| 162 bool was_suspended = (base::Time::Now() > suspension_timeout_); | 163 bool was_suspended = (base::Time::Now() > suspension_timeout_); |
| 163 | 164 |
| 164 // The monitored thread has responded. Post a task to check it again. | 165 // The monitored thread has responded. Post a task to check it again. |
| 165 message_loop()->PostDelayedTask( | 166 task_runner()->PostDelayedTask( |
| 166 FROM_HERE, | 167 FROM_HERE, base::Bind(&GpuWatchdogThread::OnCheck, |
| 167 base::Bind(&GpuWatchdogThread::OnCheck, weak_factory_.GetWeakPtr(), | 168 weak_factory_.GetWeakPtr(), was_suspended), |
| 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_->PostTask( | 197 watched_message_loop_->task_runner()->PostTask(FROM_HERE, |
| 198 FROM_HERE, | 198 base::Bind(&base::DoNothing)); |
| 199 base::Bind(&base::DoNothing)); | |
| 200 | 199 |
| 201 // Post a task to the watchdog thread to exit if the monitored thread does | 200 // Post a task to the watchdog thread to exit if the monitored thread does |
| 202 // not respond in time. | 201 // not respond in time. |
| 203 message_loop()->PostDelayedTask( | 202 task_runner()->PostDelayedTask( |
| 204 FROM_HERE, | 203 FROM_HERE, |
| 205 base::Bind(&GpuWatchdogThread::DeliberatelyTerminateToRecoverFromHang, | 204 base::Bind(&GpuWatchdogThread::DeliberatelyTerminateToRecoverFromHang, |
| 206 weak_factory_.GetWeakPtr()), | 205 weak_factory_.GetWeakPtr()), |
| 207 timeout); | 206 timeout); |
| 208 } | 207 } |
| 209 | 208 |
| 210 // Use the --disable-gpu-watchdog command line switch to disable this. | 209 // Use the --disable-gpu-watchdog command line switch to disable this. |
| 211 void GpuWatchdogThread::DeliberatelyTerminateToRecoverFromHang() { | 210 void GpuWatchdogThread::DeliberatelyTerminateToRecoverFromHang() { |
| 212 // Should not get here while the system is suspended. | 211 // Should not get here while the system is suspended. |
| 213 DCHECK(!suspended_); | 212 DCHECK(!suspended_); |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 334 bool GpuWatchdogThread::MatchXEventAtom(XEvent* event) { | 333 bool GpuWatchdogThread::MatchXEventAtom(XEvent* event) { |
| 335 if (event->xproperty.window == window_ && event->type == PropertyNotify && | 334 if (event->xproperty.window == window_ && event->type == PropertyNotify && |
| 336 event->xproperty.atom == atom_) | 335 event->xproperty.atom == atom_) |
| 337 return true; | 336 return true; |
| 338 | 337 |
| 339 return false; | 338 return false; |
| 340 } | 339 } |
| 341 | 340 |
| 342 #endif | 341 #endif |
| 343 void GpuWatchdogThread::AddPowerObserver() { | 342 void GpuWatchdogThread::AddPowerObserver() { |
| 344 message_loop()->PostTask( | 343 task_runner()->PostTask( |
| 345 FROM_HERE, | 344 FROM_HERE, base::Bind(&GpuWatchdogThread::OnAddPowerObserver, this)); |
| 346 base::Bind(&GpuWatchdogThread::OnAddPowerObserver, this)); | |
| 347 } | 345 } |
| 348 | 346 |
| 349 void GpuWatchdogThread::OnAddPowerObserver() { | 347 void GpuWatchdogThread::OnAddPowerObserver() { |
| 350 base::PowerMonitor* power_monitor = base::PowerMonitor::Get(); | 348 base::PowerMonitor* power_monitor = base::PowerMonitor::Get(); |
| 351 DCHECK(power_monitor); | 349 DCHECK(power_monitor); |
| 352 power_monitor->AddObserver(this); | 350 power_monitor->AddObserver(this); |
| 353 } | 351 } |
| 354 | 352 |
| 355 void GpuWatchdogThread::OnSuspend() { | 353 void GpuWatchdogThread::OnSuspend() { |
| 356 suspended_ = true; | 354 suspended_ = true; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 395 // not increasing. The other is where either the kernel hangs and never | 393 // not increasing. The other is where either the kernel hangs and never |
| 396 // returns to user level or where user level code | 394 // returns to user level or where user level code |
| 397 // calls into kernel level repeatedly, giving up its quanta before it is | 395 // calls into kernel level repeatedly, giving up its quanta before it is |
| 398 // tracked, for example a loop that repeatedly Sleeps. | 396 // tracked, for example a loop that repeatedly Sleeps. |
| 399 return base::TimeDelta::FromMilliseconds(static_cast<int64>( | 397 return base::TimeDelta::FromMilliseconds(static_cast<int64>( |
| 400 (user_time64.QuadPart + kernel_time64.QuadPart) / 10000)); | 398 (user_time64.QuadPart + kernel_time64.QuadPart) / 10000)); |
| 401 } | 399 } |
| 402 #endif | 400 #endif |
| 403 | 401 |
| 404 } // namespace content | 402 } // namespace content |
| OLD | NEW |