#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/compiler_specific.h"
#include "base/process_util.h"
#include "base/process.h"
#include "build/build_config.h"
#include "content/public/common/result_codes.h"
namespace {
const int64 kCheckPeriodMs = 2000;
}
GpuWatchdogThread::GpuWatchdogThread(int timeout)
: base::Thread("Watchdog"),
watched_message_loop_(MessageLoop::current()),
timeout_(base::TimeDelta::FromMilliseconds(timeout)),
armed_(false),
#if defined(OS_WIN)
watched_thread_handle_(0),
arm_cpu_time_(),
#endif
ALLOW_THIS_IN_INITIALIZER_LIST(task_observer_(this)),
ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {
DCHECK(timeout >= 0);
#if defined(OS_WIN)
BOOL result = DuplicateHandle(GetCurrentProcess(),
GetCurrentThread(),
GetCurrentProcess(),
&watched_thread_handle_,
THREAD_QUERY_INFORMATION,
FALSE,
0);
DCHECK(result);
#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();
}
void GpuWatchdogThread::CleanUp() {
weak_factory_.InvalidateWeakPtrs();
}
GpuWatchdogThread::GpuWatchdogTaskObserver::GpuWatchdogTaskObserver(
GpuWatchdogThread* watchdog)
: watchdog_(watchdog) {
}
GpuWatchdogThread::GpuWatchdogTaskObserver::~GpuWatchdogTaskObserver() {
}
void GpuWatchdogThread::GpuWatchdogTaskObserver::WillProcessTask(
base::TimeTicks time_posted) {
watchdog_->CheckArmed();
}
void GpuWatchdogThread::GpuWatchdogTaskObserver::DidProcessTask(
base::TimeTicks time_posted) {
watchdog_->CheckArmed();
}
GpuWatchdogThread::~GpuWatchdogThread() {
DCHECK(!weak_factory_.HasWeakPtrs());
#if defined(OS_WIN)
CloseHandle(watched_thread_handle_);
#endif
watched_message_loop_->RemoveTaskObserver(&task_observer_);
}
void GpuWatchdogThread::OnAcknowledge() {
if (!armed_)
return;
weak_factory_.InvalidateWeakPtrs();
armed_ = false;
message_loop()->PostDelayedTask(
FROM_HERE,
base::Bind(&GpuWatchdogThread::OnCheck, weak_factory_.GetWeakPtr()),
base::TimeDelta::FromMilliseconds(kCheckPeriodMs));
}
void GpuWatchdogThread::OnCheck() {
if (armed_)
return;
armed_ = true;
#if defined(OS_WIN)
arm_cpu_time_ = GetWatchedThreadTime();
#endif
arm_absolute_time_ = base::Time::Now();
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() {
#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() - arm_absolute_time_ > timeout_ * 2) {
armed_ = false;
OnCheck();
return;
}
static bool terminated = false;
if (terminated)
return;
#if defined(OS_WIN)
if (IsDebuggerPresent())
return;
#endif
LOG(ERROR) << "The GPU process hung. Terminating after "
<< timeout_.InMilliseconds() << " ms.";
base::Process current_process(base::GetCurrentProcessHandle());
current_process.Terminate(content::RESULT_CODE_HUNG);
terminated = 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