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