[sql] Record SQLite memory usage after a week.
[chromium-blink-merge.git] / content / gpu / gpu_main.cc
blob8606afe82cfcceec27584151bfc436b8bab76956
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.
5 #include <stdlib.h>
7 #if defined(OS_WIN)
8 #include <dwmapi.h>
9 #include <windows.h>
10 #endif
12 #include "base/lazy_instance.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/metrics/histogram.h"
15 #include "base/metrics/statistics_recorder.h"
16 #include "base/rand_util.h"
17 #include "base/strings/string_number_conversions.h"
18 #include "base/strings/stringprintf.h"
19 #include "base/third_party/dynamic_annotations/dynamic_annotations.h"
20 #include "base/threading/platform_thread.h"
21 #include "base/trace_event/trace_event.h"
22 #include "build/build_config.h"
23 #include "content/child/child_process.h"
24 #include "content/common/content_constants_internal.h"
25 #include "content/common/gpu/gpu_config.h"
26 #include "content/common/gpu/gpu_memory_buffer_factory.h"
27 #include "content/common/gpu/gpu_messages.h"
28 #include "content/common/gpu/media/gpu_jpeg_decode_accelerator.h"
29 #include "content/common/gpu/media/gpu_video_decode_accelerator.h"
30 #include "content/common/gpu/media/gpu_video_encode_accelerator.h"
31 #include "content/common/sandbox_linux/sandbox_linux.h"
32 #include "content/gpu/gpu_child_thread.h"
33 #include "content/gpu/gpu_process.h"
34 #include "content/gpu/gpu_watchdog_thread.h"
35 #include "content/public/common/content_client.h"
36 #include "content/public/common/content_switches.h"
37 #include "content/public/common/main_function_params.h"
38 #include "gpu/command_buffer/service/gpu_switches.h"
39 #include "gpu/command_buffer/service/sync_point_manager.h"
40 #include "gpu/config/gpu_info_collector.h"
41 #include "gpu/config/gpu_switches.h"
42 #include "gpu/config/gpu_util.h"
43 #include "ui/events/platform/platform_event_source.h"
44 #include "ui/gl/gl_context.h"
45 #include "ui/gl/gl_implementation.h"
46 #include "ui/gl/gl_surface.h"
47 #include "ui/gl/gl_switches.h"
48 #include "ui/gl/gpu_switching_manager.h"
50 #if defined(OS_WIN)
51 #include "base/win/windows_version.h"
52 #include "base/win/scoped_com_initializer.h"
53 #include "sandbox/win/src/sandbox.h"
54 #endif
56 #if defined(USE_X11)
57 #include "ui/base/x/x11_util.h"
58 #endif
60 #if defined(OS_LINUX)
61 #include "content/public/common/sandbox_init.h"
62 #endif
64 #if defined(OS_MACOSX)
65 #include "base/message_loop/message_pump_mac.h"
66 #include "content/common/sandbox_mac.h"
67 #endif
69 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_X86_FAMILY)
70 #include "content/common/gpu/media/vaapi_wrapper.h"
71 #endif
73 #if defined(SANITIZER_COVERAGE)
74 #include <sanitizer/common_interface_defs.h>
75 #include <sanitizer/coverage_interface.h>
76 #endif
78 const int kGpuTimeout = 10000;
80 namespace content {
82 namespace {
84 void GetGpuInfoFromCommandLine(gpu::GPUInfo& gpu_info,
85 const base::CommandLine& command_line);
86 bool WarmUpSandbox(const base::CommandLine& command_line);
88 #if !defined(OS_MACOSX)
89 bool CollectGraphicsInfo(gpu::GPUInfo& gpu_info);
90 #endif
92 #if defined(OS_LINUX)
93 #if !defined(OS_CHROMEOS)
94 bool CanAccessNvidiaDeviceFile();
95 #endif
96 bool StartSandboxLinux(const gpu::GPUInfo&, GpuWatchdogThread*, bool);
97 #elif defined(OS_WIN)
98 bool StartSandboxWindows(const sandbox::SandboxInterfaceInfo*);
99 #endif
101 base::LazyInstance<GpuChildThread::DeferredMessages> deferred_messages =
102 LAZY_INSTANCE_INITIALIZER;
104 bool GpuProcessLogMessageHandler(int severity,
105 const char* file, int line,
106 size_t message_start,
107 const std::string& str) {
108 std::string header = str.substr(0, message_start);
109 std::string message = str.substr(message_start);
110 deferred_messages.Get().push(new GpuHostMsg_OnLogMessage(
111 severity, header, message));
112 return false;
115 } // namespace anonymous
117 // Main function for starting the Gpu process.
118 int GpuMain(const MainFunctionParams& parameters) {
119 TRACE_EVENT0("gpu", "GpuMain");
120 base::trace_event::TraceLog::GetInstance()->SetProcessName("GPU Process");
121 base::trace_event::TraceLog::GetInstance()->SetProcessSortIndex(
122 kTraceEventGpuProcessSortIndex);
124 const base::CommandLine& command_line = parameters.command_line;
125 if (command_line.HasSwitch(switches::kGpuStartupDialog)) {
126 ChildProcess::WaitForDebugger("Gpu");
129 base::Time start_time = base::Time::Now();
131 #if defined(OS_WIN)
132 // Prevent Windows from displaying a modal dialog on failures like not being
133 // able to load a DLL.
134 SetErrorMode(
135 SEM_FAILCRITICALERRORS |
136 SEM_NOGPFAULTERRORBOX |
137 SEM_NOOPENFILEERRORBOX);
138 #elif defined(USE_X11)
139 ui::SetDefaultX11ErrorHandlers();
140 #endif
142 logging::SetLogMessageHandler(GpuProcessLogMessageHandler);
144 if (command_line.HasSwitch(switches::kSupportsDualGpus)) {
145 std::string types = command_line.GetSwitchValueASCII(
146 switches::kGpuDriverBugWorkarounds);
147 std::set<int> workarounds;
148 gpu::StringToFeatureSet(types, &workarounds);
149 if (workarounds.count(gpu::FORCE_DISCRETE_GPU) == 1)
150 ui::GpuSwitchingManager::GetInstance()->ForceUseOfDiscreteGpu();
151 else if (workarounds.count(gpu::FORCE_INTEGRATED_GPU) == 1)
152 ui::GpuSwitchingManager::GetInstance()->ForceUseOfIntegratedGpu();
155 // Initialization of the OpenGL bindings may fail, in which case we
156 // will need to tear down this process. However, we can not do so
157 // safely until the IPC channel is set up, because the detection of
158 // early return of a child process is implemented using an IPC
159 // channel error. If the IPC channel is not fully set up between the
160 // browser and GPU process, and the GPU process crashes or exits
161 // early, the browser process will never detect it. For this reason
162 // we defer tearing down the GPU process until receiving the
163 // GpuMsg_Initialize message from the browser.
164 bool dead_on_arrival = false;
166 #if defined(OS_WIN)
167 // Use a UI message loop because ANGLE and the desktop GL platform can
168 // create child windows to render to.
169 base::MessageLoop main_message_loop(base::MessageLoop::TYPE_UI);
170 #elif defined(OS_LINUX) && defined(USE_X11)
171 // We need a UI loop so that we can grab the Expose events. See GLSurfaceGLX
172 // and https://crbug.com/326995.
173 base::MessageLoop main_message_loop(base::MessageLoop::TYPE_UI);
174 scoped_ptr<ui::PlatformEventSource> event_source =
175 ui::PlatformEventSource::CreateDefault();
176 #elif defined(OS_LINUX)
177 base::MessageLoop main_message_loop(base::MessageLoop::TYPE_DEFAULT);
178 #elif defined(OS_MACOSX)
179 // This is necessary for CoreAnimation layers hosted in the GPU process to be
180 // drawn. See http://crbug.com/312462.
181 scoped_ptr<base::MessagePump> pump(new base::MessagePumpCFRunLoop());
182 base::MessageLoop main_message_loop(pump.Pass());
183 #else
184 base::MessageLoop main_message_loop(base::MessageLoop::TYPE_IO);
185 #endif
187 base::PlatformThread::SetName("CrGpuMain");
189 // In addition to disabling the watchdog if the command line switch is
190 // present, disable the watchdog on valgrind because the code is expected
191 // to run slowly in that case.
192 bool enable_watchdog =
193 !command_line.HasSwitch(switches::kDisableGpuWatchdog) &&
194 !RunningOnValgrind();
196 // Disable the watchdog in debug builds because they tend to only be run by
197 // developers who will not appreciate the watchdog killing the GPU process.
198 #ifndef NDEBUG
199 enable_watchdog = false;
200 #endif
202 bool delayed_watchdog_enable = false;
204 #if defined(OS_CHROMEOS)
205 // Don't start watchdog immediately, to allow developers to switch to VT2 on
206 // startup.
207 delayed_watchdog_enable = true;
208 #endif
210 scoped_refptr<GpuWatchdogThread> watchdog_thread;
212 // Start the GPU watchdog only after anything that is expected to be time
213 // consuming has completed, otherwise the process is liable to be aborted.
214 if (enable_watchdog && !delayed_watchdog_enable) {
215 watchdog_thread = new GpuWatchdogThread(kGpuTimeout);
216 base::Thread::Options options;
217 options.timer_slack = base::TIMER_SLACK_MAXIMUM;
218 watchdog_thread->StartWithOptions(options);
221 // Initializes StatisticsRecorder which tracks UMA histograms.
222 base::StatisticsRecorder::Initialize();
224 gpu::GPUInfo gpu_info;
225 // Get vendor_id, device_id, driver_version from browser process through
226 // commandline switches.
227 GetGpuInfoFromCommandLine(gpu_info, command_line);
228 gpu_info.in_process_gpu = false;
230 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_X86_FAMILY)
231 VaapiWrapper::PreSandboxInitialization();
232 #endif
234 // Warm up resources that don't need access to GPUInfo.
235 if (WarmUpSandbox(command_line)) {
236 #if defined(OS_LINUX)
237 bool initialized_sandbox = false;
238 bool initialized_gl_context = false;
239 bool should_initialize_gl_context = false;
240 // On Chrome OS ARM Mali, GPU driver userspace creates threads when
241 // initializing a GL context, so start the sandbox early.
242 if (command_line.HasSwitch(switches::kGpuSandboxStartEarly)) {
243 gpu_info.sandboxed = StartSandboxLinux(
244 gpu_info, watchdog_thread.get(), should_initialize_gl_context);
245 initialized_sandbox = true;
247 #endif // defined(OS_LINUX)
249 base::TimeTicks before_initialize_one_off = base::TimeTicks::Now();
251 // Determine if we need to initialize GL here or it has already been done.
252 bool gl_already_initialized = false;
253 #if defined(OS_MACOSX)
254 if (!command_line.HasSwitch(switches::kNoSandbox)) {
255 // On Mac, if the sandbox is enabled, then GLSurface::InitializeOneOff()
256 // is called from the sandbox warmup code before getting here.
257 gl_already_initialized = true;
259 #endif
260 if (command_line.HasSwitch(switches::kInProcessGPU)) {
261 // With in-process GPU, GLSurface::InitializeOneOff() is called from
262 // GpuChildThread before getting here.
263 gl_already_initialized = true;
266 // Load and initialize the GL implementation and locate the GL entry points.
267 bool gl_initialized =
268 gl_already_initialized
269 ? gfx::GetGLImplementation() != gfx::kGLImplementationNone
270 : gfx::GLSurface::InitializeOneOff();
271 if (gl_initialized) {
272 // We need to collect GL strings (VENDOR, RENDERER) for blacklisting
273 // purposes. However, on Mac we don't actually use them. As documented in
274 // crbug.com/222934, due to some driver issues, glGetString could take
275 // multiple seconds to finish, which in turn cause the GPU process to
276 // crash.
277 // By skipping the following code on Mac, we don't really lose anything,
278 // because the basic GPU information is passed down from browser process
279 // and we already registered them through SetGpuInfo() above.
280 base::TimeTicks before_collect_context_graphics_info =
281 base::TimeTicks::Now();
282 #if !defined(OS_MACOSX)
283 if (!CollectGraphicsInfo(gpu_info))
284 dead_on_arrival = true;
286 #if defined(OS_CHROMEOS) || defined(OS_ANDROID)
287 // Recompute gpu driver bug workarounds - this is specifically useful
288 // on systems where vendor_id/device_id aren't available.
289 if (!command_line.HasSwitch(switches::kDisableGpuDriverBugWorkarounds)) {
290 gpu::ApplyGpuDriverBugWorkarounds(
291 gpu_info, const_cast<base::CommandLine*>(&command_line));
293 #endif
295 #if defined(OS_LINUX)
296 initialized_gl_context = true;
297 #if !defined(OS_CHROMEOS)
298 if (gpu_info.gpu.vendor_id == 0x10de && // NVIDIA
299 gpu_info.driver_vendor == "NVIDIA" &&
300 !CanAccessNvidiaDeviceFile())
301 dead_on_arrival = true;
302 #endif // !defined(OS_CHROMEOS)
303 #endif // defined(OS_LINUX)
304 #endif // !defined(OS_MACOSX)
305 base::TimeDelta collect_context_time =
306 base::TimeTicks::Now() - before_collect_context_graphics_info;
307 UMA_HISTOGRAM_TIMES("GPU.CollectContextGraphicsInfo",
308 collect_context_time);
309 } else { // gl_initialized
310 VLOG(1) << "gfx::GLSurface::InitializeOneOff failed";
311 dead_on_arrival = true;
314 base::TimeDelta initialize_one_off_time =
315 base::TimeTicks::Now() - before_initialize_one_off;
316 UMA_HISTOGRAM_MEDIUM_TIMES("GPU.InitializeOneOffMediumTime",
317 initialize_one_off_time);
319 if (enable_watchdog && delayed_watchdog_enable) {
320 watchdog_thread = new GpuWatchdogThread(kGpuTimeout);
321 base::Thread::Options options;
322 options.timer_slack = base::TIMER_SLACK_MAXIMUM;
323 watchdog_thread->StartWithOptions(options);
326 // OSMesa is expected to run very slowly, so disable the watchdog in that
327 // case.
328 if (enable_watchdog &&
329 gfx::GetGLImplementation() == gfx::kGLImplementationOSMesaGL) {
330 watchdog_thread->Stop();
331 watchdog_thread = NULL;
334 #if defined(OS_LINUX)
335 should_initialize_gl_context = !initialized_gl_context &&
336 !dead_on_arrival;
338 if (!initialized_sandbox) {
339 gpu_info.sandboxed = StartSandboxLinux(gpu_info, watchdog_thread.get(),
340 should_initialize_gl_context);
342 #elif defined(OS_WIN)
343 gpu_info.sandboxed = StartSandboxWindows(parameters.sandbox_info);
344 #elif defined(OS_MACOSX)
345 gpu_info.sandboxed = Sandbox::SandboxIsCurrentlyActive();
346 #endif
348 gpu_info.video_decode_accelerator_supported_profiles =
349 content::GpuVideoDecodeAccelerator::GetSupportedProfiles();
350 gpu_info.video_encode_accelerator_supported_profiles =
351 content::GpuVideoEncodeAccelerator::GetSupportedProfiles();
352 gpu_info.jpeg_decode_accelerator_supported =
353 content::GpuJpegDecodeAccelerator::IsSupported();
354 } else {
355 dead_on_arrival = true;
358 logging::SetLogMessageHandler(NULL);
360 scoped_ptr<GpuMemoryBufferFactory> gpu_memory_buffer_factory =
361 GpuMemoryBufferFactory::Create(
362 GpuChildThread::GetGpuMemoryBufferFactoryType());
363 gpu::SyncPointManager sync_point_manager(false);
365 GpuProcess gpu_process;
367 GpuChildThread* child_thread = new GpuChildThread(
368 watchdog_thread.get(), dead_on_arrival, gpu_info, deferred_messages.Get(),
369 gpu_memory_buffer_factory.get(),
370 &sync_point_manager);
371 while (!deferred_messages.Get().empty())
372 deferred_messages.Get().pop();
374 child_thread->Init(start_time);
376 gpu_process.set_main_thread(child_thread);
378 if (watchdog_thread.get())
379 watchdog_thread->AddPowerObserver();
382 TRACE_EVENT0("gpu", "Run Message Loop");
383 main_message_loop.Run();
386 child_thread->StopWatchdog();
388 return 0;
391 namespace {
393 void GetGpuInfoFromCommandLine(gpu::GPUInfo& gpu_info,
394 const base::CommandLine& command_line) {
395 DCHECK(command_line.HasSwitch(switches::kGpuVendorID) &&
396 command_line.HasSwitch(switches::kGpuDeviceID) &&
397 command_line.HasSwitch(switches::kGpuDriverVersion));
398 bool success = base::HexStringToUInt(
399 command_line.GetSwitchValueASCII(switches::kGpuVendorID),
400 &gpu_info.gpu.vendor_id);
401 DCHECK(success);
402 success = base::HexStringToUInt(
403 command_line.GetSwitchValueASCII(switches::kGpuDeviceID),
404 &gpu_info.gpu.device_id);
405 DCHECK(success);
406 gpu_info.driver_vendor =
407 command_line.GetSwitchValueASCII(switches::kGpuDriverVendor);
408 gpu_info.driver_version =
409 command_line.GetSwitchValueASCII(switches::kGpuDriverVersion);
410 GetContentClient()->SetGpuInfo(gpu_info);
413 bool WarmUpSandbox(const base::CommandLine& command_line) {
415 TRACE_EVENT0("gpu", "Warm up rand");
416 // Warm up the random subsystem, which needs to be done pre-sandbox on all
417 // platforms.
418 (void) base::RandUint64();
420 return true;
423 #if !defined(OS_MACOSX)
424 bool CollectGraphicsInfo(gpu::GPUInfo& gpu_info) {
425 bool res = true;
426 gpu::CollectInfoResult result = gpu::CollectContextGraphicsInfo(&gpu_info);
427 switch (result) {
428 case gpu::kCollectInfoFatalFailure:
429 LOG(ERROR) << "gpu::CollectGraphicsInfo failed (fatal).";
430 res = false;
431 break;
432 case gpu::kCollectInfoNonFatalFailure:
433 DVLOG(1) << "gpu::CollectGraphicsInfo failed (non-fatal).";
434 break;
435 case gpu::kCollectInfoNone:
436 NOTREACHED();
437 break;
438 case gpu::kCollectInfoSuccess:
439 break;
441 GetContentClient()->SetGpuInfo(gpu_info);
442 return res;
444 #endif
446 #if defined(OS_LINUX)
447 #if !defined(OS_CHROMEOS)
448 bool CanAccessNvidiaDeviceFile() {
449 bool res = true;
450 base::ThreadRestrictions::AssertIOAllowed();
451 if (access("/dev/nvidiactl", R_OK) != 0) {
452 DVLOG(1) << "NVIDIA device file /dev/nvidiactl access denied";
453 res = false;
455 return res;
457 #endif
459 void CreateDummyGlContext() {
460 scoped_refptr<gfx::GLSurface> surface(
461 gfx::GLSurface::CreateOffscreenGLSurface(gfx::Size()));
462 if (!surface.get()) {
463 DVLOG(1) << "gfx::GLSurface::CreateOffscreenGLSurface failed";
464 return;
467 // On Linux, this is needed to make sure /dev/nvidiactl has
468 // been opened and its descriptor cached.
469 scoped_refptr<gfx::GLContext> context(gfx::GLContext::CreateGLContext(
470 NULL, surface.get(), gfx::PreferDiscreteGpu));
471 if (!context.get()) {
472 DVLOG(1) << "gfx::GLContext::CreateGLContext failed";
473 return;
476 // Similarly, this is needed for /dev/nvidia0.
477 if (context->MakeCurrent(surface.get())) {
478 context->ReleaseCurrent(surface.get());
479 } else {
480 DVLOG(1) << "gfx::GLContext::MakeCurrent failed";
484 void WarmUpSandboxNvidia(const gpu::GPUInfo& gpu_info,
485 bool should_initialize_gl_context) {
486 // We special case Optimus since the vendor_id we see may not be Nvidia.
487 bool uses_nvidia_driver = (gpu_info.gpu.vendor_id == 0x10de && // NVIDIA.
488 gpu_info.driver_vendor == "NVIDIA") ||
489 gpu_info.optimus;
490 if (uses_nvidia_driver && should_initialize_gl_context) {
491 // We need this on Nvidia to pre-open /dev/nvidiactl and /dev/nvidia0.
492 CreateDummyGlContext();
496 bool StartSandboxLinux(const gpu::GPUInfo& gpu_info,
497 GpuWatchdogThread* watchdog_thread,
498 bool should_initialize_gl_context) {
499 TRACE_EVENT0("gpu", "Initialize sandbox");
501 bool res = false;
503 WarmUpSandboxNvidia(gpu_info, should_initialize_gl_context);
505 if (watchdog_thread) {
506 // LinuxSandbox needs to be able to ensure that the thread
507 // has really been stopped.
508 LinuxSandbox::StopThread(watchdog_thread);
511 #if defined(SANITIZER_COVERAGE)
512 const std::string sancov_file_name =
513 "gpu." + base::Uint64ToString(base::RandUint64());
514 LinuxSandbox* linux_sandbox = LinuxSandbox::GetInstance();
515 linux_sandbox->sanitizer_args()->coverage_sandboxed = 1;
516 linux_sandbox->sanitizer_args()->coverage_fd =
517 __sanitizer_maybe_open_cov_file(sancov_file_name.c_str());
518 linux_sandbox->sanitizer_args()->coverage_max_block_size = 0;
519 #endif
521 // LinuxSandbox::InitializeSandbox() must always be called
522 // with only one thread.
523 res = LinuxSandbox::InitializeSandbox();
524 if (watchdog_thread) {
525 base::Thread::Options options;
526 options.timer_slack = base::TIMER_SLACK_MAXIMUM;
527 watchdog_thread->StartWithOptions(options);
530 return res;
532 #endif // defined(OS_LINUX)
534 #if defined(OS_WIN)
535 bool StartSandboxWindows(const sandbox::SandboxInterfaceInfo* sandbox_info) {
536 TRACE_EVENT0("gpu", "Lower token");
538 // For Windows, if the target_services interface is not zero, the process
539 // is sandboxed and we must call LowerToken() before rendering untrusted
540 // content.
541 sandbox::TargetServices* target_services = sandbox_info->target_services;
542 if (target_services) {
543 target_services->LowerToken();
544 return true;
547 return false;
549 #endif // defined(OS_WIN)
551 } // namespace.
553 } // namespace content