Commit e46117cb authored by Eric Seckler's avatar Eric Seckler Committed by Commit Bot

tracing: Enable startup tracing on Android only after mojo is inited

We're refactoring startup tracing in Chrome, see design doc[1]. With
the new startup tracing architecture, each process needs to allocate a
shared memory buffer when enabling startup tracing. This is only
supported after mojo was initialized for the process.

On Android, we were previously enabling startup tracing right after the
native library is loaded. At this point, mojo is not ready yet. To
prepare for the startup tracing refactor, this patch moves startup
tracing initialization on android to ContentMainRunnerImpl::Initialize,
which is called after mojo was initialized by Main() in
//services/service_manager/embedder/main.cc.

We should only lose minimal tracing data due to this, since
ContentMainRunnerImpl::Initialize is still very early during startup,
and this doesn't affect recording of even earlier java events.

[1] https://docs.google.com/document/d/1FygJQbD29vMkfVfT7m0Lb1u1zZZU1VgXAPi_R0uBoTw/edit?usp=sharing

Bug: 1006753
Change-Id: I89aa292b3010bb501b42bd0bd735f8df75cea557
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2036188Reviewed-by: default avatarKen Rockot <rockot@google.com>
Reviewed-by: default avatarScott Violet <sky@chromium.org>
Reviewed-by: default avatarSami Kyöstilä <skyostil@chromium.org>
Commit-Queue: Eric Seckler <eseckler@chromium.org>
Cr-Commit-Position: refs/heads/master@{#738881}
parent d6f26ce4
...@@ -23,9 +23,6 @@ bool LibraryLoaded(JNIEnv* env, ...@@ -23,9 +23,6 @@ bool LibraryLoaded(JNIEnv* env,
base::android::InitReachedCodeProfilerAtStartup(library_process_type); base::android::InitReachedCodeProfilerAtStartup(library_process_type);
} }
// Enable startup tracing asap to avoid early TRACE_EVENT calls being ignored.
tracing::EnableStartupTracingIfNeeded();
// Android's main browser loop is custom so we set the browser name here as // Android's main browser loop is custom so we set the browser name here as
// early as possible if this is the browser process or main webview process. // early as possible if this is the browser process or main webview process.
if (library_process_type == if (library_process_type ==
...@@ -37,8 +34,8 @@ bool LibraryLoaded(JNIEnv* env, ...@@ -37,8 +34,8 @@ bool LibraryLoaded(JNIEnv* env,
base::trace_event::TraceLog::GetInstance()->SetProcessSortIndex( base::trace_event::TraceLog::GetInstance()->SetProcessSortIndex(
kTraceEventBrowserProcessSortIndex); kTraceEventBrowserProcessSortIndex);
// Can only use event tracing after setting up the command line. // Tracing itself can only be enabled after mojo is initialized, we do so in
TRACE_EVENT0("jni", "JNI_OnLoad continuation"); // ContentMainRunnerImpl::Initialize.
logging::LoggingSettings settings; logging::LoggingSettings settings;
settings.logging_dest = settings.logging_dest =
......
...@@ -602,9 +602,13 @@ int ContentMainRunnerImpl::Initialize(const ContentMainParams& params) { ...@@ -602,9 +602,13 @@ int ContentMainRunnerImpl::Initialize(const ContentMainParams& params) {
#endif // defined(OS_MACOSX) #endif // defined(OS_MACOSX)
#if defined(OS_ANDROID) #if defined(OS_ANDROID)
// See note at the initialization of ExitManager, below; basically, // Now that mojo's core is initialized (by service manager's Main()), we can
// only Android builds have the ctor/dtor handlers set up to use // enable tracing. Note that only Android builds have the ctor/dtor handlers
// TRACE_EVENT right away. // set up to use trace events at this point (because AtExitManager is already
// set up when the library is loaded). Other platforms enable tracing below,
// after the initialization of AtExitManager.
tracing::EnableStartupTracingIfNeeded();
TRACE_EVENT0("startup,benchmark,rail", "ContentMainRunnerImpl::Initialize"); TRACE_EVENT0("startup,benchmark,rail", "ContentMainRunnerImpl::Initialize");
#endif // OS_ANDROID #endif // OS_ANDROID
...@@ -672,25 +676,23 @@ int ContentMainRunnerImpl::Initialize(const ContentMainParams& params) { ...@@ -672,25 +676,23 @@ int ContentMainRunnerImpl::Initialize(const ContentMainParams& params) {
ContentClientInitializer::Set(process_type, delegate_); ContentClientInitializer::Set(process_type, delegate_);
#if !defined(OS_ANDROID) #if !defined(OS_ANDROID)
// Enable startup tracing asap to avoid early TRACE_EVENT calls being // Enable startup tracing asap to avoid early TRACE_EVENT calls being
// ignored. For Android, startup tracing is enabled in an even earlier place // ignored. For Android, startup tracing is enabled in an even earlier place
// content/app/android/library_loader_hooks.cc. // above.
// //
// Startup tracing flags are not (and should not) passed to Zygote // Startup tracing flags are not (and should not be) passed to Zygote
// processes. We will enable tracing when forked, if needed. // processes. We will enable tracing when forked, if needed.
if (process_type != service_manager::switches::kZygoteProcess) if (process_type != service_manager::switches::kZygoteProcess)
tracing::EnableStartupTracingIfNeeded(); tracing::EnableStartupTracingIfNeeded();
#endif // !OS_ANDROID
#if defined(OS_WIN) #if defined(OS_WIN)
base::trace_event::TraceEventETWExport::EnableETWExport(); base::trace_event::TraceEventETWExport::EnableETWExport();
#endif // OS_WIN #endif // OS_WIN
#if !defined(OS_ANDROID) // Android tracing started at the beginning of the method.
// Android tracing started at the beginning of the method. // Other OSes have to wait till we get here in order for all the memory
// Other OSes have to wait till we get here in order for all the memory // management setup to be completed.
// management setup to be completed. TRACE_EVENT0("startup,benchmark,rail", "ContentMainRunnerImpl::Initialize");
TRACE_EVENT0("startup,benchmark,rail", "ContentMainRunnerImpl::Initialize");
#endif // !OS_ANDROID #endif // !OS_ANDROID
// If we are on a platform where the default allocator is overridden (shim // If we are on a platform where the default allocator is overridden (shim
......
...@@ -51,14 +51,6 @@ class CommandlineStartupTracingTest : public ContentBrowserTest { ...@@ -51,14 +51,6 @@ class CommandlineStartupTracingTest : public ContentBrowserTest {
command_line->AppendSwitchASCII(switches::kTraceStartupDuration, "3"); command_line->AppendSwitchASCII(switches::kTraceStartupDuration, "3");
command_line->AppendSwitchASCII(switches::kTraceStartupFile, command_line->AppendSwitchASCII(switches::kTraceStartupFile,
temp_file_path_.AsUTF8Unsafe()); temp_file_path_.AsUTF8Unsafe());
#if defined(OS_ANDROID)
// On Android the startup tracing is initialized as soon as library load
// time, earlier than this point. So, reset the config and enable startup
// tracing here.
tracing::TraceStartupConfig::GetInstance()->EnableFromCommandLine();
tracing::EnableStartupTracingIfNeeded();
#endif
} }
void PreRunTestOnMainThread() override { void PreRunTestOnMainThread() override {
...@@ -185,7 +177,6 @@ class BackgroundStartupTracingTest : public ContentBrowserTest { ...@@ -185,7 +177,6 @@ class BackgroundStartupTracingTest : public ContentBrowserTest {
startup_config->enable_background_tracing_for_testing_ = true; startup_config->enable_background_tracing_for_testing_ = true;
startup_config->EnableFromBackgroundTracing(); startup_config->EnableFromBackgroundTracing();
startup_config->startup_duration_in_seconds_ = 3; startup_config->startup_duration_in_seconds_ = 3;
tracing::EnableStartupTracingIfNeeded();
command_line->AppendSwitchASCII(switches::kPerfettoOutputFile, command_line->AppendSwitchASCII(switches::kPerfettoOutputFile,
temp_file_path_.AsUTF8Unsafe()); temp_file_path_.AsUTF8Unsafe());
} }
......
...@@ -404,6 +404,9 @@ void BrowserTestBase::SetUp() { ...@@ -404,6 +404,9 @@ void BrowserTestBase::SetUp() {
InitializeMojo(); InitializeMojo();
// We can only setup startup tracing after mojo is initialized above.
tracing::EnableStartupTracingIfNeeded();
{ {
SetBrowserClientForTesting(delegate->CreateContentBrowserClient()); SetBrowserClientForTesting(delegate->CreateContentBrowserClient());
if (command_line->HasSwitch(switches::kSingleProcess)) if (command_line->HasSwitch(switches::kSingleProcess))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment