Android: Add preference enabling for startup tracing
This CL sets up a preference for enabling startup tracing if needed. Early java tracing is enabled if the preference is set. This will be used by background tracing to setup startup tracing. BUG=859260 Change-Id: Idf2c6cbfb8c2a4d5e1765a1d2c3256abe69368fe Reviewed-on: https://chromium-review.googlesource.com/1159116 Commit-Queue: Siddhartha S <ssid@chromium.org> Reviewed-by: Tommy Nyquist <nyquist@chromium.org> Cr-Commit-Position: refs/heads/master@{#583963}
This commit is contained in:
base
BUILD.gn
android
@@ -181,6 +181,7 @@ jumbo_component("base") {
|
|||||||
"android/content_uri_utils.h",
|
"android/content_uri_utils.h",
|
||||||
"android/cpu_features.cc",
|
"android/cpu_features.cc",
|
||||||
"android/early_trace_event_binding.cc",
|
"android/early_trace_event_binding.cc",
|
||||||
|
"android/early_trace_event_binding.h",
|
||||||
"android/event_log.cc",
|
"android/event_log.cc",
|
||||||
"android/event_log.h",
|
"android/event_log.h",
|
||||||
"android/field_trial_list.cc",
|
"android/field_trial_list.cc",
|
||||||
|
@@ -2,6 +2,8 @@
|
|||||||
// Use of this source code is governed by a BSD-style license that can be
|
// Use of this source code is governed by a BSD-style license that can be
|
||||||
// found in the LICENSE file.
|
// found in the LICENSE file.
|
||||||
|
|
||||||
|
#include "base/android/early_trace_event_binding.h"
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
#include "base/android/jni_string.h"
|
#include "base/android/jni_string.h"
|
||||||
@@ -63,5 +65,17 @@ static void JNI_EarlyTraceEvent_RecordEarlyFinishAsyncEvent(
|
|||||||
base::TimeTicks() + base::TimeDelta::FromMicroseconds(timestamp_us));
|
base::TimeTicks() + base::TimeDelta::FromMicroseconds(timestamp_us));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool GetBackgroundStartupTracingFlag() {
|
||||||
|
JNIEnv* env = base::android::AttachCurrentThread();
|
||||||
|
return base::android::Java_EarlyTraceEvent_getBackgroundStartupTracingFlag(
|
||||||
|
env);
|
||||||
|
}
|
||||||
|
|
||||||
|
void SetBackgroundStartupTracingFlag(bool enabled) {
|
||||||
|
JNIEnv* env = base::android::AttachCurrentThread();
|
||||||
|
base::android::Java_EarlyTraceEvent_setBackgroundStartupTracingFlag(env,
|
||||||
|
enabled);
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace android
|
} // namespace android
|
||||||
} // namespace base
|
} // namespace base
|
||||||
|
24
base/android/early_trace_event_binding.h
Normal file
24
base/android/early_trace_event_binding.h
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
// Copyright 2018 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 BASE_ANDROID_EARLY_TRACE_EVENT_BINDING_H_
|
||||||
|
#define BASE_ANDROID_EARLY_TRACE_EVENT_BINDING_H_
|
||||||
|
|
||||||
|
#include "base/base_export.h"
|
||||||
|
|
||||||
|
namespace base {
|
||||||
|
namespace android {
|
||||||
|
|
||||||
|
// Returns true if background startup tracing flag was set on the previous
|
||||||
|
// startup.
|
||||||
|
BASE_EXPORT bool GetBackgroundStartupTracingFlag();
|
||||||
|
|
||||||
|
// Sets a flag to chrome application preferences to enable startup tracing next
|
||||||
|
// time the app is started.
|
||||||
|
BASE_EXPORT void SetBackgroundStartupTracingFlag(bool enabled);
|
||||||
|
|
||||||
|
} // namespace android
|
||||||
|
} // namespace base
|
||||||
|
|
||||||
|
#endif // BASE_ANDROID_EARLY_TRACE_EVENT_BINDING_H_
|
@@ -10,6 +10,7 @@ import android.os.Process;
|
|||||||
import android.os.StrictMode;
|
import android.os.StrictMode;
|
||||||
import android.os.SystemClock;
|
import android.os.SystemClock;
|
||||||
|
|
||||||
|
import org.chromium.base.annotations.CalledByNative;
|
||||||
import org.chromium.base.annotations.JNINamespace;
|
import org.chromium.base.annotations.JNINamespace;
|
||||||
import org.chromium.base.annotations.MainDex;
|
import org.chromium.base.annotations.MainDex;
|
||||||
|
|
||||||
@@ -100,6 +101,9 @@ public class EarlyTraceEvent {
|
|||||||
@VisibleForTesting static final int STATE_FINISHING = 2;
|
@VisibleForTesting static final int STATE_FINISHING = 2;
|
||||||
@VisibleForTesting static final int STATE_FINISHED = 3;
|
@VisibleForTesting static final int STATE_FINISHED = 3;
|
||||||
|
|
||||||
|
private static final String BACKGROUND_STARTUP_TRACING_ENABLED_KEY = "bg_startup_tracing";
|
||||||
|
private static boolean sCachedBackgroundStartupTracingFlag = false;
|
||||||
|
|
||||||
// Locks the fields below.
|
// Locks the fields below.
|
||||||
private static final Object sLock = new Object();
|
private static final Object sLock = new Object();
|
||||||
|
|
||||||
@@ -115,6 +119,7 @@ public class EarlyTraceEvent {
|
|||||||
*/
|
*/
|
||||||
static void maybeEnable() {
|
static void maybeEnable() {
|
||||||
ThreadUtils.assertOnUiThread();
|
ThreadUtils.assertOnUiThread();
|
||||||
|
if (sState != STATE_DISABLED) return;
|
||||||
boolean shouldEnable = false;
|
boolean shouldEnable = false;
|
||||||
// Checking for the trace config filename touches the disk.
|
// Checking for the trace config filename touches the disk.
|
||||||
StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads();
|
StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads();
|
||||||
@@ -128,6 +133,18 @@ public class EarlyTraceEvent {
|
|||||||
// Access denied, not enabled.
|
// Access denied, not enabled.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (ContextUtils.getAppSharedPreferences().getBoolean(
|
||||||
|
BACKGROUND_STARTUP_TRACING_ENABLED_KEY, false)) {
|
||||||
|
if (shouldEnable) {
|
||||||
|
// If user has enabled tracing, then force disable background tracing for this
|
||||||
|
// session.
|
||||||
|
setBackgroundStartupTracingFlag(false);
|
||||||
|
sCachedBackgroundStartupTracingFlag = false;
|
||||||
|
} else {
|
||||||
|
sCachedBackgroundStartupTracingFlag = true;
|
||||||
|
shouldEnable = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
} finally {
|
} finally {
|
||||||
StrictMode.setThreadPolicy(oldPolicy);
|
StrictMode.setThreadPolicy(oldPolicy);
|
||||||
}
|
}
|
||||||
@@ -175,6 +192,28 @@ public class EarlyTraceEvent {
|
|||||||
return sState == STATE_ENABLED;
|
return sState == STATE_ENABLED;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the background startup tracing enabled in app preferences for next startup.
|
||||||
|
*/
|
||||||
|
@CalledByNative
|
||||||
|
static void setBackgroundStartupTracingFlag(boolean enabled) {
|
||||||
|
ContextUtils.getAppSharedPreferences()
|
||||||
|
.edit()
|
||||||
|
.putBoolean(BACKGROUND_STARTUP_TRACING_ENABLED_KEY, enabled)
|
||||||
|
.apply();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if the background startup tracing flag is set.
|
||||||
|
*
|
||||||
|
* This does not return the correct value if called before maybeEnable() was called. But that is
|
||||||
|
* called really early in startup.
|
||||||
|
*/
|
||||||
|
@CalledByNative
|
||||||
|
public static boolean getBackgroundStartupTracingFlag() {
|
||||||
|
return sCachedBackgroundStartupTracingFlag;
|
||||||
|
}
|
||||||
|
|
||||||
/** @see {@link TraceEvent#begin()}. */
|
/** @see {@link TraceEvent#begin()}. */
|
||||||
public static void begin(String name) {
|
public static void begin(String name) {
|
||||||
// begin() and end() are going to be called once per TraceEvent, this avoids entering a
|
// begin() and end() are going to be called once per TraceEvent, this avoids entering a
|
||||||
|
@@ -269,4 +269,37 @@ public class EarlyTraceEventTest {
|
|||||||
EarlyTraceEvent.Event event = EarlyTraceEvent.sCompletedEvents.get(0);
|
EarlyTraceEvent.Event event = EarlyTraceEvent.sCompletedEvents.get(0);
|
||||||
Assert.assertEquals(threadId[0], event.mThreadId);
|
Assert.assertEquals(threadId[0], event.mThreadId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@SmallTest
|
||||||
|
@Feature({"Android-AppBase"})
|
||||||
|
public void testEnableAtStartup() {
|
||||||
|
ThreadUtils.setThreadAssertsDisabledForTesting(true);
|
||||||
|
EarlyTraceEvent.maybeEnable();
|
||||||
|
Assert.assertFalse(EarlyTraceEvent.enabled());
|
||||||
|
EarlyTraceEvent.setBackgroundStartupTracingFlag(false);
|
||||||
|
Assert.assertFalse(EarlyTraceEvent.enabled());
|
||||||
|
|
||||||
|
EarlyTraceEvent.setBackgroundStartupTracingFlag(true);
|
||||||
|
EarlyTraceEvent.maybeEnable();
|
||||||
|
Assert.assertTrue(EarlyTraceEvent.getBackgroundStartupTracingFlag());
|
||||||
|
Assert.assertTrue(EarlyTraceEvent.enabled());
|
||||||
|
EarlyTraceEvent.disable();
|
||||||
|
EarlyTraceEvent.setBackgroundStartupTracingFlag(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@SmallTest
|
||||||
|
@Feature({"Android-AppBase"})
|
||||||
|
public void testUserOverrideBackgroundTracing() {
|
||||||
|
ThreadUtils.setThreadAssertsDisabledForTesting(true);
|
||||||
|
// Setting command line should disable the background tracing flag.
|
||||||
|
CommandLine.getInstance().appendSwitch("trace-startup");
|
||||||
|
EarlyTraceEvent.setBackgroundStartupTracingFlag(true);
|
||||||
|
EarlyTraceEvent.maybeEnable();
|
||||||
|
Assert.assertFalse(EarlyTraceEvent.getBackgroundStartupTracingFlag());
|
||||||
|
Assert.assertTrue(EarlyTraceEvent.enabled());
|
||||||
|
EarlyTraceEvent.disable();
|
||||||
|
EarlyTraceEvent.setBackgroundStartupTracingFlag(false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user