1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef GPU_IPC_SERVICE_GPU_WATCHDOG_THREAD_H_
#define GPU_IPC_SERVICE_GPU_WATCHDOG_THREAD_H_
#include "base/atomicops.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/power_monitor/power_observer.h"
#include "base/threading/thread.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "gpu/command_buffer/service/progress_reporter.h"
#include "gpu/gpu_export.h"
#include "ui/gfx/native_widget_types.h"
#if defined(USE_X11)
extern "C" {
#include <X11/Xlib.h>
#include <X11/Xatom.h>
}
#include <sys/poll.h>
#include "ui/base/x/x11_util.h" // nogncheck
#include "ui/gfx/x/x11_types.h" // nogncheck
#endif // defined(USE_X11)
namespace gpu {
// A thread that intermitently sends tasks to a group of watched message loops
// and deliberately crashes if one of them does not respond after a timeout.
class GPU_EXPORT GpuWatchdogThread : public base::Thread,
public base::PowerObserver,
public gles2::ProgressReporter {
public:
~GpuWatchdogThread() override;
static std::unique_ptr<GpuWatchdogThread> Create();
void CheckArmed();
// Must be called after a PowerMonitor has been created. Can be called from
// any thread.
void AddPowerObserver();
// gles2::ProgressReporter implementation:
void ReportProgress() override;
protected:
void Init() override;
void CleanUp() override;
private:
// An object of this type intercepts the reception and completion of all tasks
// on the watched thread and checks whether the watchdog is armed.
class GpuWatchdogTaskObserver : public base::MessageLoop::TaskObserver {
public:
explicit GpuWatchdogTaskObserver(GpuWatchdogThread* watchdog);
~GpuWatchdogTaskObserver() override;
// Implements MessageLoop::TaskObserver.
void WillProcessTask(const base::PendingTask& pending_task) override;
void DidProcessTask(const base::PendingTask& pending_task) override;
private:
GpuWatchdogThread* watchdog_;
};
GpuWatchdogThread();
void OnAcknowledge();
void OnCheck(bool after_suspend);
void DeliberatelyTerminateToRecoverFromHang();
#if defined(USE_X11)
void SetupXServer();
void SetupXChangeProp();
bool MatchXEventAtom(XEvent* event);
#endif
void OnAddPowerObserver();
// Implement PowerObserver.
void OnSuspend() override;
void OnResume() override;
#if defined(OS_WIN)
base::ThreadTicks GetWatchedThreadTime();
#endif
#if defined(USE_X11)
int GetActiveTTY() const;
#endif
base::MessageLoop* watched_message_loop_;
base::TimeDelta timeout_;
bool armed_;
GpuWatchdogTaskObserver task_observer_;
// |awaiting_acknowledge_| is only ever read on the watched thread, but may
// be modified on either the watched or watchdog thread. Reads/writes should
// be careful to ensure that appropriate synchronization is used.
base::subtle::Atomic32 awaiting_acknowledge_;
// True if the watchdog should wait for a certain amount of CPU to be used
// before killing the process.
bool use_thread_cpu_time_;
// The number of consecutive acknowledgements that had a latency less than
// 50ms.
int responsive_acknowledge_count_;
#if defined(OS_WIN)
void* watched_thread_handle_;
base::ThreadTicks arm_cpu_time_;
// This measures the time that the system has been running, in units of 100
// ns.
ULONGLONG arm_interrupt_time_;
#endif
// Time after which it's assumed that the computer has been suspended since
// the task was posted.
base::Time suspension_timeout_;
bool suspended_;
// The time the last OnSuspend and OnResume was called.
base::Time suspend_time_;
base::Time resume_time_;
// This is the time the last check was sent.
base::Time check_time_;
base::TimeTicks check_timeticks_;
#if defined(USE_X11)
XDisplay* display_;
gfx::AcceleratedWidget window_;
XAtom atom_;
FILE* tty_file_;
int host_tty_;
#endif
base::WeakPtrFactory<GpuWatchdogThread> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(GpuWatchdogThread);
};
} // namespace gpu
#endif // GPU_IPC_SERVICE_GPU_WATCHDOG_THREAD_H_
|