This source file includes following definitions.
- suspended_
- PostAcknowledge
- CheckArmed
- Init
- CleanUp
- WillProcessTask
- DidProcessTask
- OnAcknowledge
- OnCheck
- DeliberatelyTerminateToRecoverFromHang
- AddPowerObserver
- OnAddPowerObserver
- OnSuspend
- OnResume
- GetWatchedThreadTime
#if defined(OS_WIN)
#include <windows.h>
#endif
#include "content/gpu/gpu_watchdog_thread.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/command_line.h"
#include "base/compiler_specific.h"
#include "base/file_util.h"
#include "base/power_monitor/power_monitor.h"
#include "base/process/process.h"
#include "build/build_config.h"
#include "content/public/common/content_switches.h"
#include "content/public/common/result_codes.h"
namespace content {
namespace {
const int64 kCheckPeriodMs = 2000;
#if defined(OS_CHROMEOS)
const base::FilePath::CharType
kTtyFilePath[] = FILE_PATH_LITERAL("/sys/class/tty/tty0/active");
#endif
}
GpuWatchdogThread::GpuWatchdogThread(int timeout)
: base::Thread("Watchdog"),
watched_message_loop_(base::MessageLoop::current()),
timeout_(base::TimeDelta::FromMilliseconds(timeout)),
armed_(false),
#if defined(OS_WIN)
watched_thread_handle_(0),
arm_cpu_time_(),
#endif
task_observer_(this),
weak_factory_(this),
suspended_(false) {
DCHECK(timeout >= 0);
#if defined(OS_WIN)
BOOL result = DuplicateHandle(GetCurrentProcess(),
GetCurrentThread(),
GetCurrentProcess(),
&watched_thread_handle_,
THREAD_QUERY_INFORMATION,
FALSE,
0);
DCHECK(result);
#endif
#if defined(OS_CHROMEOS)
tty_file_ = base::OpenFile(base::FilePath(kTtyFilePath), "r");
#endif
watched_message_loop_->AddTaskObserver(&task_observer_);
}
void GpuWatchdogThread::PostAcknowledge() {
message_loop()->PostTask(
FROM_HERE,
base::Bind(&GpuWatchdogThread::OnAcknowledge, this));
}
void GpuWatchdogThread::CheckArmed() {
if (armed()) {
PostAcknowledge();
}
}
void GpuWatchdogThread::Init() {
OnCheck(false);
}
void GpuWatchdogThread::CleanUp() {
weak_factory_.InvalidateWeakPtrs();
}
GpuWatchdogThread::GpuWatchdogTaskObserver::GpuWatchdogTaskObserver(
GpuWatchdogThread* watchdog)
: watchdog_(watchdog) {
}
GpuWatchdogThread::GpuWatchdogTaskObserver::~GpuWatchdogTaskObserver() {
}
void GpuWatchdogThread::GpuWatchdogTaskObserver::WillProcessTask(
const base::PendingTask& pending_task) {
watchdog_->CheckArmed();
}
void GpuWatchdogThread::GpuWatchdogTaskObserver::DidProcessTask(
const base::PendingTask& pending_task) {
watchdog_->CheckArmed();
}
GpuWatchdogThread::~GpuWatchdogThread() {
DCHECK(!weak_factory_.HasWeakPtrs());
#if defined(OS_WIN)
CloseHandle(watched_thread_handle_);
#endif
base::PowerMonitor* power_monitor = base::PowerMonitor::Get();
if (power_monitor)
power_monitor->RemoveObserver(this);
#if defined(OS_CHROMEOS)
if (tty_file_)
fclose(tty_file_);
#endif
watched_message_loop_->RemoveTaskObserver(&task_observer_);
}
void GpuWatchdogThread::OnAcknowledge() {
CHECK(base::PlatformThread::CurrentId() == thread_id());
if (!armed_)
return;
weak_factory_.InvalidateWeakPtrs();
armed_ = false;
if (suspended_)
return;
bool was_suspended = (base::Time::Now() > suspension_timeout_);
message_loop()->PostDelayedTask(
FROM_HERE,
base::Bind(&GpuWatchdogThread::OnCheck, weak_factory_.GetWeakPtr(),
was_suspended),
base::TimeDelta::FromMilliseconds(kCheckPeriodMs));
}
void GpuWatchdogThread::OnCheck(bool after_suspend) {
CHECK(base::PlatformThread::CurrentId() == thread_id());
if (armed_ || suspended_)
return;
armed_ = true;
#if defined(OS_WIN)
arm_cpu_time_ = GetWatchedThreadTime();
#endif
base::TimeDelta timeout = timeout_ * (after_suspend ? 3 : 1);
suspension_timeout_ = base::Time::Now() + timeout * 2;
watched_message_loop_->PostTask(
FROM_HERE,
base::Bind(&base::DoNothing));
message_loop()->PostDelayedTask(
FROM_HERE,
base::Bind(
&GpuWatchdogThread::DeliberatelyTerminateToRecoverFromHang,
weak_factory_.GetWeakPtr()),
timeout);
}
void GpuWatchdogThread::DeliberatelyTerminateToRecoverFromHang() {
DCHECK(!suspended_);
#if defined(OS_WIN)
base::TimeDelta time_since_arm = GetWatchedThreadTime() - arm_cpu_time_;
if (time_since_arm < timeout_) {
message_loop()->PostDelayedTask(
FROM_HERE,
base::Bind(
&GpuWatchdogThread::DeliberatelyTerminateToRecoverFromHang,
weak_factory_.GetWeakPtr()),
timeout_ - time_since_arm);
return;
}
#endif
if (base::Time::Now() > suspension_timeout_) {
armed_ = false;
OnCheck(true);
return;
}
static bool terminated = false;
if (terminated)
return;
#if defined(OS_WIN)
if (IsDebuggerPresent())
return;
#endif
#if defined(OS_CHROMEOS)
char tty_string[8] = {0};
if (tty_file_ &&
!fseek(tty_file_, 0, SEEK_SET) &&
fread(tty_string, 1, 7, tty_file_)) {
int tty_number = -1;
int num_res = sscanf(tty_string, "tty%d", &tty_number);
if (num_res == 1 && tty_number != 1)
return;
}
#endif
LOG(ERROR) << "The GPU process hung. Terminating after "
<< timeout_.InMilliseconds() << " ms.";
*((volatile int*)0) = 0x1337;
terminated = true;
}
void GpuWatchdogThread::AddPowerObserver() {
message_loop()->PostTask(
FROM_HERE,
base::Bind(&GpuWatchdogThread::OnAddPowerObserver, this));
}
void GpuWatchdogThread::OnAddPowerObserver() {
base::PowerMonitor* power_monitor = base::PowerMonitor::Get();
DCHECK(power_monitor);
power_monitor->AddObserver(this);
}
void GpuWatchdogThread::OnSuspend() {
suspended_ = true;
OnAcknowledge();
}
void GpuWatchdogThread::OnResume() {
suspended_ = false;
armed_ = false;
OnCheck(true);
}
#if defined(OS_WIN)
base::TimeDelta GpuWatchdogThread::GetWatchedThreadTime() {
FILETIME creation_time;
FILETIME exit_time;
FILETIME user_time;
FILETIME kernel_time;
BOOL result = GetThreadTimes(watched_thread_handle_,
&creation_time,
&exit_time,
&kernel_time,
&user_time);
DCHECK(result);
ULARGE_INTEGER user_time64;
user_time64.HighPart = user_time.dwHighDateTime;
user_time64.LowPart = user_time.dwLowDateTime;
ULARGE_INTEGER kernel_time64;
kernel_time64.HighPart = kernel_time.dwHighDateTime;
kernel_time64.LowPart = kernel_time.dwLowDateTime;
return base::TimeDelta::FromMilliseconds(static_cast<int64>(
(user_time64.QuadPart + kernel_time64.QuadPart) / 10000));
}
#endif
}