[email protected] | f091824 | 2012-02-18 00:30:50 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | c0fc094 | 2010-01-13 00:55:37 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 5 | #include "content/gpu/gpu_child_thread.h" |
[email protected] | c0fc094 | 2010-01-13 00:55:37 | [diff] [blame] | 6 | |
[email protected] | f658af8b | 2010-06-11 01:56:27 | [diff] [blame] | 7 | #include <string> |
| 8 | #include <vector> |
[email protected] | 28638b2 | 2010-03-31 17:55:57 | [diff] [blame] | 9 | |
[email protected] | 8075105 | 2011-11-12 17:10:58 | [diff] [blame] | 10 | #include "base/bind.h" |
[email protected] | 28638b2 | 2010-03-31 17:55:57 | [diff] [blame] | 11 | #include "base/command_line.h" |
[email protected] | ac9ba8fe | 2010-12-30 18:08:36 | [diff] [blame] | 12 | #include "base/threading/worker_pool.h" |
[email protected] | 274aa588 | 2010-07-15 21:12:23 | [diff] [blame] | 13 | #include "build/build_config.h" |
[email protected] | 7ef40ffe1 | 2011-03-08 05:05:28 | [diff] [blame] | 14 | #include "content/common/child_process.h" |
[email protected] | 202b54ff | 2011-04-22 21:36:38 | [diff] [blame] | 15 | #include "content/common/gpu/gpu_messages.h" |
[email protected] | 623c0bd | 2011-03-12 01:00:41 | [diff] [blame] | 16 | #include "content/gpu/gpu_info_collector.h" |
| 17 | #include "content/gpu/gpu_watchdog_thread.h" |
[email protected] | 085170ca | 2012-05-17 20:27:28 | [diff] [blame] | 18 | #include "content/public/common/content_client.h" |
[email protected] | c9e2cbbb | 2012-05-12 21:17:27 | [diff] [blame] | 19 | #include "content/public/common/content_switches.h" |
[email protected] | 939856a | 2010-08-24 20:29:02 | [diff] [blame] | 20 | #include "ipc/ipc_channel_handle.h" |
[email protected] | 8c81e9ecc | 2012-04-25 00:42:27 | [diff] [blame] | 21 | #include "ipc/ipc_sync_message_filter.h" |
[email protected] | c9e2cbbb | 2012-05-12 21:17:27 | [diff] [blame] | 22 | #include "ui/gl/gl_implementation.h" |
[email protected] | 7709e71 | 2011-01-07 17:57:31 | [diff] [blame] | 23 | |
| 24 | const int kGpuTimeout = 10000; |
| 25 | |
| 26 | namespace { |
| 27 | |
[email protected] | 9158f2f | 2011-01-27 21:08:57 | [diff] [blame] | 28 | bool GpuProcessLogMessageHandler(int severity, |
| 29 | const char* file, int line, |
| 30 | size_t message_start, |
| 31 | const std::string& str) { |
[email protected] | e6ff5a3 | 2011-01-31 20:36:22 | [diff] [blame] | 32 | std::string header = str.substr(0, message_start); |
[email protected] | 9158f2f | 2011-01-27 21:08:57 | [diff] [blame] | 33 | std::string message = str.substr(message_start); |
[email protected] | 8c81e9ecc | 2012-04-25 00:42:27 | [diff] [blame] | 34 | |
| 35 | // If we are not on main thread in child process, send through |
| 36 | // the sync_message_filter; otherwise send directly. |
| 37 | if (MessageLoop::current() != |
| 38 | ChildProcess::current()->main_thread()->message_loop()) { |
| 39 | ChildProcess::current()->main_thread()->sync_message_filter()->Send( |
| 40 | new GpuHostMsg_OnLogMessage(severity, header, message)); |
| 41 | } else { |
| 42 | ChildThread::current()->Send(new GpuHostMsg_OnLogMessage(severity, header, |
| 43 | message)); |
| 44 | } |
| 45 | |
[email protected] | 9158f2f | 2011-01-27 21:08:57 | [diff] [blame] | 46 | return false; |
| 47 | } |
| 48 | |
[email protected] | e6ff5a3 | 2011-01-31 20:36:22 | [diff] [blame] | 49 | } // namespace |
[email protected] | 9158f2f | 2011-01-27 21:08:57 | [diff] [blame] | 50 | |
[email protected] | f091824 | 2012-02-18 00:30:50 | [diff] [blame] | 51 | GpuChildThread::GpuChildThread(bool dead_on_arrival, |
| 52 | const content::GPUInfo& gpu_info) |
| 53 | : dead_on_arrival_(dead_on_arrival), |
| 54 | gpu_info_(gpu_info) { |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 55 | #if defined(OS_WIN) |
[email protected] | 5abe630 | 2011-12-20 23:44:32 | [diff] [blame] | 56 | target_services_ = NULL; |
[email protected] | 0b2cec6 | 2011-07-22 18:13:28 | [diff] [blame] | 57 | collecting_dx_diagnostics_ = false; |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 58 | #endif |
[email protected] | 0b2cec6 | 2011-07-22 18:13:28 | [diff] [blame] | 59 | } |
[email protected] | 9158f2f | 2011-01-27 21:08:57 | [diff] [blame] | 60 | |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 61 | GpuChildThread::GpuChildThread(const std::string& channel_id) |
[email protected] | 0b2cec6 | 2011-07-22 18:13:28 | [diff] [blame] | 62 | : ChildThread(channel_id), |
| 63 | dead_on_arrival_(false) { |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 64 | #if defined(OS_WIN) |
| 65 | target_services_ = NULL; |
[email protected] | 5ea8881 | 2011-03-28 10:45:39 | [diff] [blame] | 66 | collecting_dx_diagnostics_ = false; |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 67 | #endif |
[email protected] | 5750c52 | 2012-06-04 06:35:25 | [diff] [blame] | 68 | if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kSingleProcess) || |
| 69 | CommandLine::ForCurrentProcess()->HasSwitch(switches::kInProcessGPU)) { |
| 70 | // For single process and in-process GPU mode, we need to load and |
| 71 | // initialize the GL implementation and locate the GL entry points here. |
| 72 | if (!gfx::GLSurface::InitializeOneOff()) { |
| 73 | VLOG(1) << "gfx::GLSurface::InitializeOneOff()"; |
| 74 | } |
| 75 | } |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | |
| 79 | GpuChildThread::~GpuChildThread() { |
| 80 | logging::SetLogMessageHandler(NULL); |
| 81 | } |
| 82 | |
| 83 | void GpuChildThread::Init(const base::Time& process_start_time) { |
| 84 | process_start_time_ = process_start_time; |
| 85 | } |
| 86 | |
| 87 | bool GpuChildThread::Send(IPC::Message* msg) { |
| 88 | // The GPU process must never send a synchronous IPC message to the browser |
[email protected] | 4c0e289d | 2011-06-01 03:45:51 | [diff] [blame] | 89 | // process. This could result in deadlock. |
| 90 | DCHECK(!msg->is_sync()); |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 91 | |
| 92 | return ChildThread::Send(msg); |
| 93 | } |
| 94 | |
| 95 | bool GpuChildThread::OnControlMessageReceived(const IPC::Message& msg) { |
| 96 | bool msg_is_ok = true; |
| 97 | bool handled = true; |
| 98 | IPC_BEGIN_MESSAGE_MAP_EX(GpuChildThread, msg, msg_is_ok) |
| 99 | IPC_MESSAGE_HANDLER(GpuMsg_Initialize, OnInitialize) |
| 100 | IPC_MESSAGE_HANDLER(GpuMsg_CollectGraphicsInfo, OnCollectGraphicsInfo) |
[email protected] | fb246af | 2012-08-18 03:11:41 | [diff] [blame] | 101 | IPC_MESSAGE_HANDLER(GpuMsg_GetVideoMemoryUsageStats, |
| 102 | OnGetVideoMemoryUsageStats) |
[email protected] | 184e3f380 | 2011-05-04 20:48:52 | [diff] [blame] | 103 | IPC_MESSAGE_HANDLER(GpuMsg_Clean, OnClean) |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 104 | IPC_MESSAGE_HANDLER(GpuMsg_Crash, OnCrash) |
| 105 | IPC_MESSAGE_HANDLER(GpuMsg_Hang, OnHang) |
| 106 | IPC_MESSAGE_UNHANDLED(handled = false) |
| 107 | IPC_END_MESSAGE_MAP_EX() |
| 108 | |
| 109 | if (handled) |
| 110 | return true; |
| 111 | |
[email protected] | f24a1e2b | 2011-04-08 01:48:48 | [diff] [blame] | 112 | return gpu_channel_manager_.get() && |
| 113 | gpu_channel_manager_->OnMessageReceived(msg); |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | void GpuChildThread::OnInitialize() { |
[email protected] | 9665973 | 2012-08-24 01:55:17 | [diff] [blame] | 117 | Send(new GpuHostMsg_Initialized(!dead_on_arrival_)); |
| 118 | |
[email protected] | 0b2cec6 | 2011-07-22 18:13:28 | [diff] [blame] | 119 | if (dead_on_arrival_) { |
[email protected] | 085170ca | 2012-05-17 20:27:28 | [diff] [blame] | 120 | VLOG(1) << "Exiting GPU process due to errors during initialization"; |
[email protected] | 37cfa2a | 2012-08-20 20:29:39 | [diff] [blame] | 121 | MessageLoop::current()->Quit(); |
| 122 | return; |
[email protected] | 0b2cec6 | 2011-07-22 18:13:28 | [diff] [blame] | 123 | } |
| 124 | |
[email protected] | 2d9de411 | 2011-05-27 03:18:36 | [diff] [blame] | 125 | // We don't need to pipe log messages if we are running the GPU thread in |
| 126 | // the browser process. |
| 127 | if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kSingleProcess) && |
| 128 | !CommandLine::ForCurrentProcess()->HasSwitch(switches::kInProcessGPU)) |
| 129 | logging::SetLogMessageHandler(GpuProcessLogMessageHandler); |
[email protected] | 602cf9fda | 2011-04-22 16:53:30 | [diff] [blame] | 130 | |
[email protected] | e09cee4 | 2010-11-09 01:50:08 | [diff] [blame] | 131 | // Record initialization only after collecting the GPU info because that can |
| 132 | // take a significant amount of time. |
[email protected] | a61508e5 | 2011-03-08 17:59:42 | [diff] [blame] | 133 | gpu_info_.initialization_time = base::Time::Now() - process_start_time_; |
[email protected] | 35312bf2 | 2011-01-06 23:07:20 | [diff] [blame] | 134 | |
[email protected] | 7709e71 | 2011-01-07 17:57:31 | [diff] [blame] | 135 | // In addition to disabling the watchdog if the command line switch is |
| 136 | // present, disable it in two other cases. OSMesa is expected to run very |
| 137 | // slowly. Also disable the watchdog on valgrind because the code is expected |
| 138 | // to run slowly in that case. |
| 139 | bool enable_watchdog = |
[email protected] | 630c1f3 | 2011-01-13 21:30:56 | [diff] [blame] | 140 | !CommandLine::ForCurrentProcess()->HasSwitch( |
| 141 | switches::kDisableGpuWatchdog) && |
[email protected] | 7709e71 | 2011-01-07 17:57:31 | [diff] [blame] | 142 | gfx::GetGLImplementation() != gfx::kGLImplementationOSMesaGL && |
| 143 | !RunningOnValgrind(); |
| 144 | |
| 145 | // Disable the watchdog in debug builds because they tend to only be run by |
| 146 | // developers who will not appreciate the watchdog killing the GPU process. |
| 147 | #ifndef NDEBUG |
| 148 | enable_watchdog = false; |
[email protected] | a363c7a | 2011-01-06 23:25:50 | [diff] [blame] | 149 | #endif |
[email protected] | 7709e71 | 2011-01-07 17:57:31 | [diff] [blame] | 150 | |
[email protected] | 7709e71 | 2011-01-07 17:57:31 | [diff] [blame] | 151 | // Start the GPU watchdog only after anything that is expected to be time |
| 152 | // consuming has completed, otherwise the process is liable to be aborted. |
| 153 | if (enable_watchdog) { |
| 154 | watchdog_thread_ = new GpuWatchdogThread(kGpuTimeout); |
| 155 | watchdog_thread_->Start(); |
| 156 | } |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 157 | |
| 158 | // Defer creation of the render thread. This is to prevent it from handling |
| 159 | // IPC messages before the sandbox has been enabled and all other necessary |
| 160 | // initialization has succeeded. |
[email protected] | f24a1e2b | 2011-04-08 01:48:48 | [diff] [blame] | 161 | gpu_channel_manager_.reset(new GpuChannelManager( |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 162 | this, |
[email protected] | 808f7fe7 | 2011-03-23 03:49:02 | [diff] [blame] | 163 | watchdog_thread_, |
[email protected] | 92bf906 | 2011-05-02 18:00:49 | [diff] [blame] | 164 | ChildProcess::current()->io_message_loop_proxy(), |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 165 | ChildProcess::current()->GetShutDownEvent())); |
| 166 | |
[email protected] | 085170ca | 2012-05-17 20:27:28 | [diff] [blame] | 167 | #if defined(OS_LINUX) |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 168 | // Ensure the browser process receives the GPU info before a reply to any |
| 169 | // subsequent IPC it might send. |
[email protected] | 198e0c44 | 2011-12-02 19:45:06 | [diff] [blame] | 170 | Send(new GpuHostMsg_GraphicsInfoCollected(gpu_info_)); |
[email protected] | 085170ca | 2012-05-17 20:27:28 | [diff] [blame] | 171 | #endif |
[email protected] | 7709e71 | 2011-01-07 17:57:31 | [diff] [blame] | 172 | } |
| 173 | |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 174 | void GpuChildThread::StopWatchdog() { |
[email protected] | 7709e71 | 2011-01-07 17:57:31 | [diff] [blame] | 175 | if (watchdog_thread_.get()) { |
| 176 | watchdog_thread_->Stop(); |
| 177 | } |
[email protected] | c0fc094 | 2010-01-13 00:55:37 | [diff] [blame] | 178 | } |
| 179 | |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 180 | void GpuChildThread::OnCollectGraphicsInfo() { |
[email protected] | 085170ca | 2012-05-17 20:27:28 | [diff] [blame] | 181 | if (!gpu_info_.finalized && |
[email protected] | 5750c52 | 2012-06-04 06:35:25 | [diff] [blame] | 182 | (CommandLine::ForCurrentProcess()->HasSwitch( |
| 183 | switches::kDisableGpuSandbox) || |
| 184 | CommandLine::ForCurrentProcess()->HasSwitch(switches::kSingleProcess) || |
| 185 | CommandLine::ForCurrentProcess()->HasSwitch(switches::kInProcessGPU))) { |
| 186 | // GPU full info collection should only happen on un-sandboxed GPU process |
| 187 | // or single process/in-process gpu mode. |
[email protected] | f3f79b10 | 2011-01-27 00:19:37 | [diff] [blame] | 188 | |
[email protected] | 085170ca | 2012-05-17 20:27:28 | [diff] [blame] | 189 | if (!gpu_info_collector::CollectGraphicsInfo(&gpu_info_)) |
| 190 | VLOG(1) << "gpu_info_collector::CollectGraphicsInfo failed"; |
| 191 | content::GetContentClient()->SetGpuInfo(gpu_info_); |
| 192 | |
| 193 | #if defined(OS_WIN) |
| 194 | if (!collecting_dx_diagnostics_) { |
| 195 | // Prevent concurrent collection of DirectX diagnostics. |
| 196 | collecting_dx_diagnostics_ = true; |
| 197 | |
[email protected] | 6f1485ee | 2012-01-28 00:29:19 | [diff] [blame] | 198 | // Asynchronously collect the DirectX diagnostics because this can take a |
| 199 | // couple of seconds. |
| 200 | if (!base::WorkerPool::PostTask( |
| 201 | FROM_HERE, base::Bind(&GpuChildThread::CollectDxDiagnostics, this), |
| 202 | true)) { |
| 203 | // Flag GPU info as complete if the DirectX diagnostics cannot be |
| 204 | // collected. |
| 205 | collecting_dx_diagnostics_ = false; |
| 206 | gpu_info_.finalized = true; |
[email protected] | 6f1485ee | 2012-01-28 00:29:19 | [diff] [blame] | 207 | } |
[email protected] | f3f79b10 | 2011-01-27 00:19:37 | [diff] [blame] | 208 | } |
[email protected] | f3f79b10 | 2011-01-27 00:19:37 | [diff] [blame] | 209 | #endif |
[email protected] | 085170ca | 2012-05-17 20:27:28 | [diff] [blame] | 210 | } |
[email protected] | e09cee4 | 2010-11-09 01:50:08 | [diff] [blame] | 211 | Send(new GpuHostMsg_GraphicsInfoCollected(gpu_info_)); |
[email protected] | cadc050 | 2010-09-10 20:27:13 | [diff] [blame] | 212 | } |
[email protected] | 43ed628 | 2010-09-15 20:07:19 | [diff] [blame] | 213 | |
[email protected] | fb246af | 2012-08-18 03:11:41 | [diff] [blame] | 214 | void GpuChildThread::OnGetVideoMemoryUsageStats() { |
| 215 | content::GPUVideoMemoryUsageStats video_memory_usage_stats; |
| 216 | if (gpu_channel_manager_.get()) |
| 217 | gpu_channel_manager_->gpu_memory_manager()->GetVideoMemoryUsageStats( |
| 218 | video_memory_usage_stats); |
| 219 | Send(new GpuHostMsg_VideoMemoryUsageStats(video_memory_usage_stats)); |
| 220 | } |
| 221 | |
[email protected] | 184e3f380 | 2011-05-04 20:48:52 | [diff] [blame] | 222 | void GpuChildThread::OnClean() { |
[email protected] | 085170ca | 2012-05-17 20:27:28 | [diff] [blame] | 223 | VLOG(1) << "GPU: Removing all contexts"; |
[email protected] | 184e3f380 | 2011-05-04 20:48:52 | [diff] [blame] | 224 | if (gpu_channel_manager_.get()) |
| 225 | gpu_channel_manager_->LoseAllContexts(); |
| 226 | } |
| 227 | |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 228 | void GpuChildThread::OnCrash() { |
[email protected] | 085170ca | 2012-05-17 20:27:28 | [diff] [blame] | 229 | VLOG(1) << "GPU: Simulating GPU crash"; |
[email protected] | 43ed628 | 2010-09-15 20:07:19 | [diff] [blame] | 230 | // Good bye, cruel world. |
| 231 | volatile int* it_s_the_end_of_the_world_as_we_know_it = NULL; |
| 232 | *it_s_the_end_of_the_world_as_we_know_it = 0xdead; |
| 233 | } |
[email protected] | 259750d | 2010-09-24 06:14:00 | [diff] [blame] | 234 | |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 235 | void GpuChildThread::OnHang() { |
[email protected] | 085170ca | 2012-05-17 20:27:28 | [diff] [blame] | 236 | VLOG(1) << "GPU: Simulating GPU hang"; |
[email protected] | 981c1c5 | 2010-12-01 20:09:24 | [diff] [blame] | 237 | for (;;) { |
| 238 | // Do not sleep here. The GPU watchdog timer tracks the amount of user |
| 239 | // time this thread is using and it doesn't use much while calling Sleep. |
| 240 | } |
[email protected] | 259750d | 2010-09-24 06:14:00 | [diff] [blame] | 241 | } |
[email protected] | 41579ae | 2010-11-15 22:31:26 | [diff] [blame] | 242 | |
| 243 | #if defined(OS_WIN) |
| 244 | |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 245 | // Runs on a worker thread. The GPU process never terminates voluntarily so |
| 246 | // it is safe to assume that its message loop is valid. |
| 247 | void GpuChildThread::CollectDxDiagnostics(GpuChildThread* thread) { |
[email protected] | a80f5ece | 2011-10-20 23:56:55 | [diff] [blame] | 248 | content::DxDiagNode node; |
[email protected] | 41579ae | 2010-11-15 22:31:26 | [diff] [blame] | 249 | gpu_info_collector::GetDxDiagnostics(&node); |
| 250 | |
| 251 | thread->message_loop()->PostTask( |
[email protected] | 8075105 | 2011-11-12 17:10:58 | [diff] [blame] | 252 | FROM_HERE, base::Bind(&GpuChildThread::SetDxDiagnostics, thread, node)); |
[email protected] | 41579ae | 2010-11-15 22:31:26 | [diff] [blame] | 253 | } |
| 254 | |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 255 | // Runs on the main thread. |
| 256 | void GpuChildThread::SetDxDiagnostics(GpuChildThread* thread, |
[email protected] | a80f5ece | 2011-10-20 23:56:55 | [diff] [blame] | 257 | const content::DxDiagNode& node) { |
[email protected] | a61508e5 | 2011-03-08 17:59:42 | [diff] [blame] | 258 | thread->gpu_info_.dx_diagnostics = node; |
[email protected] | 6cc8ece6 | 2011-03-14 20:05:47 | [diff] [blame] | 259 | thread->gpu_info_.finalized = true; |
| 260 | thread->collecting_dx_diagnostics_ = false; |
[email protected] | dfb2643 | 2011-02-24 21:03:03 | [diff] [blame] | 261 | thread->Send(new GpuHostMsg_GraphicsInfoCollected(thread->gpu_info_)); |
[email protected] | 41579ae | 2010-11-15 22:31:26 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | #endif |