diff options
author | 2025-03-13 11:31:08 +0000 | |
---|---|---|
committer | 2025-03-18 06:30:24 -0700 | |
commit | 029c7a04cc36484fe7c0d69c75b29bd9eff1e3a6 (patch) | |
tree | 06808e98aa7cc6cc34518484bda113431de77752 | |
parent | 04b421bcce5e0ccf25c9a8f1caf26d1131242514 (diff) |
Added a no-op PerfettoTrackEvent extra builder
This improves performance when tracing is disabled
because all the no-op calls can use one statically
initialized dummy builder object.
Newly added perf test to measure unregistered
category peformance shows a 2x improvement when
tracing a proto. 37ns to 17ns on oriole.
Test: atest PerfettoTraceTest
Test: atest TracePerfTest
Bug: 303199244
Bug: 400826148
Flag: android.os.perfetto_sdk_tracing_v2
Change-Id: If8f663d72b8283265a5f1b7fc75f5af2ecd2e37d
-rw-r--r-- | apct-tests/perftests/core/src/android/os/TracePerfTest.java | 25 | ||||
-rw-r--r-- | core/java/android/os/PerfettoTrace.java | 15 | ||||
-rw-r--r-- | core/java/android/os/PerfettoTrackEventExtra.java | 67 |
3 files changed, 74 insertions, 33 deletions
diff --git a/apct-tests/perftests/core/src/android/os/TracePerfTest.java b/apct-tests/perftests/core/src/android/os/TracePerfTest.java index 00e1c1fdbf4b..3df708d1a5cd 100644 --- a/apct-tests/perftests/core/src/android/os/TracePerfTest.java +++ b/apct-tests/perftests/core/src/android/os/TracePerfTest.java @@ -46,6 +46,7 @@ public class TracePerfTest { private static final String FOO = "foo"; private static final Category FOO_CATEGORY = new Category(FOO); + private static final Category UNREGISTERED_CATEGORY = new Category("unregistered"); private static PerfettoTrace.Session sPerfettoSession; @BeforeClass @@ -163,6 +164,30 @@ public class TracePerfTest { } } + @Test + public void testInstantPerfettoWithProtoUnregistered() { + PerfettoTrace.begin(UNREGISTERED_CATEGORY, "message_queue_receive") + .beginProto() + .beginNested(2004 /* message_queue */) + .addField(1 /* sending_thread_name */, "foo") + .endNested() + .endProto() + .setTerminatingFlow(5) + .emit(); + + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + PerfettoTrace.begin(UNREGISTERED_CATEGORY, "message_queue_receive") + .beginProto() + .beginNested(2004 /* message_queue */) + .addField(1 /* sending_thread_name */, "foo") + .endNested() + .endProto() + .setTerminatingFlow(5) + .emit(); + } + } + private static TraceConfig getTraceConfig(String cat) { BufferConfig bufferConfig = BufferConfig.newBuilder().setSizeKb(1024).build(); TrackEventConfig trackEventConfig = TrackEventConfig diff --git a/core/java/android/os/PerfettoTrace.java b/core/java/android/os/PerfettoTrace.java index 932836f8a050..8b96b89c949a 100644 --- a/core/java/android/os/PerfettoTrace.java +++ b/core/java/android/os/PerfettoTrace.java @@ -232,7 +232,8 @@ public final class PerfettoTrace { * @param eventName The event name to appear in the trace. */ public static PerfettoTrackEventExtra.Builder instant(Category category, String eventName) { - return PerfettoTrackEventExtra.builder().init(PERFETTO_TE_TYPE_INSTANT, category) + return PerfettoTrackEventExtra.builder(category.isEnabled()) + .init(PERFETTO_TE_TYPE_INSTANT, category) .setEventName(eventName); } @@ -243,7 +244,8 @@ public final class PerfettoTrace { * @param eventName The event name to appear in the trace. */ public static PerfettoTrackEventExtra.Builder begin(Category category, String eventName) { - return PerfettoTrackEventExtra.builder().init(PERFETTO_TE_TYPE_SLICE_BEGIN, category) + return PerfettoTrackEventExtra.builder(category.isEnabled()) + .init(PERFETTO_TE_TYPE_SLICE_BEGIN, category) .setEventName(eventName); } @@ -253,7 +255,8 @@ public final class PerfettoTrace { * @param category The perfetto category. */ public static PerfettoTrackEventExtra.Builder end(Category category) { - return PerfettoTrackEventExtra.builder().init(PERFETTO_TE_TYPE_SLICE_END, category); + return PerfettoTrackEventExtra.builder(category.isEnabled()) + .init(PERFETTO_TE_TYPE_SLICE_END, category); } /** @@ -263,7 +266,8 @@ public final class PerfettoTrace { * @param value The value of the counter. */ public static PerfettoTrackEventExtra.Builder counter(Category category, long value) { - return PerfettoTrackEventExtra.builder().init(PERFETTO_TE_TYPE_COUNTER, category) + return PerfettoTrackEventExtra.builder(category.isEnabled()) + .init(PERFETTO_TE_TYPE_COUNTER, category) .setCounter(value); } @@ -286,7 +290,8 @@ public final class PerfettoTrace { * @param value The value of the counter. */ public static PerfettoTrackEventExtra.Builder counter(Category category, double value) { - return PerfettoTrackEventExtra.builder().init(PERFETTO_TE_TYPE_COUNTER, category) + return PerfettoTrackEventExtra.builder(category.isEnabled()) + .init(PERFETTO_TE_TYPE_COUNTER, category) .setCounter(value); } diff --git a/core/java/android/os/PerfettoTrackEventExtra.java b/core/java/android/os/PerfettoTrackEventExtra.java index 07b44a87ef88..5fc7cf7be246 100644 --- a/core/java/android/os/PerfettoTrackEventExtra.java +++ b/core/java/android/os/PerfettoTrackEventExtra.java @@ -37,6 +37,7 @@ import java.util.function.Supplier; public final class PerfettoTrackEventExtra { private static final boolean DEBUG = false; private static final int DEFAULT_EXTRA_CACHE_SIZE = 5; + private static final Builder NO_OP_BUILDER = new Builder(/* extra= */ null, /* isCategoryEnabled= */ false); private static final ThreadLocal<PerfettoTrackEventExtra> sTrackEventExtra = new ThreadLocal<PerfettoTrackEventExtra>() { @Override @@ -153,8 +154,8 @@ public final class PerfettoTrackEventExtra { private Builder mParent; private FieldContainer mCurrentContainer; - private boolean mIsCategoryEnabled; + private final boolean mIsCategoryEnabled; private final CounterInt64 mCounterInt64; private final CounterDouble mCounterDouble; private final Proto mProto; @@ -175,24 +176,29 @@ public final class PerfettoTrackEventExtra { private final Pool<Builder> mBuilderCache; private Builder() { - mExtra = sTrackEventExtra.get(); - mNamedTrackCache = mExtra.mNamedTrackCache; - mCounterTrackCache = mExtra.mCounterTrackCache; - mArgInt64Cache = mExtra.mArgInt64Cache; - mArgDoubleCache = mExtra.mArgDoubleCache; - mArgBoolCache = mExtra.mArgBoolCache; - mArgStringCache = mExtra.mArgStringCache; - mFieldInt64Cache = mExtra.mFieldInt64Cache; - mFieldDoubleCache = mExtra.mFieldDoubleCache; - mFieldStringCache = mExtra.mFieldStringCache; - mFieldNestedCache = mExtra.mFieldNestedCache; - mBuilderCache = mExtra.mBuilderCache; - - mCounterInt64 = mExtra.getCounterInt64(); - mCounterDouble = mExtra.getCounterDouble(); - mProto = mExtra.getProto(); - mFlow = mExtra.getFlow(); - mTerminatingFlow = mExtra.getTerminatingFlow(); + this(sTrackEventExtra.get(), true); + } + + public Builder(PerfettoTrackEventExtra extra, boolean isCategoryEnabled) { + mIsCategoryEnabled = isCategoryEnabled; + mExtra = extra; + mNamedTrackCache = mExtra == null ? null : mExtra.mNamedTrackCache; + mCounterTrackCache = mExtra == null ? null : mExtra.mCounterTrackCache; + mArgInt64Cache = mExtra == null ? null : mExtra.mArgInt64Cache; + mArgDoubleCache = mExtra == null ? null : mExtra.mArgDoubleCache; + mArgBoolCache = mExtra == null ? null : mExtra.mArgBoolCache; + mArgStringCache = mExtra == null ? null : mExtra.mArgStringCache; + mFieldInt64Cache = mExtra == null ? null : mExtra.mFieldInt64Cache; + mFieldDoubleCache = mExtra == null ? null : mExtra.mFieldDoubleCache; + mFieldStringCache = mExtra == null ? null : mExtra.mFieldStringCache; + mFieldNestedCache = mExtra == null ? null : mExtra.mFieldNestedCache; + mBuilderCache = mExtra == null ? null : mExtra.mBuilderCache; + + mCounterInt64 = mExtra == null ? null : mExtra.getCounterInt64(); + mCounterDouble = mExtra == null ? null : mExtra.getCounterDouble(); + mProto = mExtra == null ? null : mExtra.getProto(); + mFlow = mExtra == null ? null : mExtra.getFlow(); + mTerminatingFlow = mExtra == null ? null : mExtra.getTerminatingFlow(); } /** @@ -214,6 +220,10 @@ public final class PerfettoTrackEventExtra { * Initialize the builder for a new trace event. */ public Builder init(int traceType, PerfettoTrace.Category category) { + if (!mIsCategoryEnabled) { + return this; + } + mTraceType = traceType; mCategory = category; mEventName = ""; @@ -225,7 +235,7 @@ public final class PerfettoTrackEventExtra { mExtra.reset(); // Reset after on init in case the thread created builders without calling emit - return initInternal(this, null, category.isEnabled()); + return initInternal(this, null); } /** @@ -529,7 +539,7 @@ public final class PerfettoTrackEventExtra { } mProto.clearFields(); mExtra.addPerfettoPointer(mProto); - return mBuilderCache.get(sBuilderSupplier).initInternal(this, mProto, true); + return mBuilderCache.get(sBuilderSupplier).initInternal(this, mProto); } /** @@ -560,7 +570,7 @@ public final class PerfettoTrackEventExtra { FieldNested field = mFieldNestedCache.get(sFieldNestedSupplier); field.setId(id); mExtra.addPerfettoPointer(mCurrentContainer, field); - return mBuilderCache.get(sBuilderSupplier).initInternal(this, field, true); + return mBuilderCache.get(sBuilderSupplier).initInternal(this, field); } /** @@ -577,11 +587,9 @@ public final class PerfettoTrackEventExtra { } - private Builder initInternal(Builder parent, FieldContainer field, - boolean isCategoryEnabled) { + private Builder initInternal(Builder parent, FieldContainer field) { mParent = parent; mCurrentContainer = field; - mIsCategoryEnabled = isCategoryEnabled; mIsBuilt = false; return this; @@ -613,9 +621,12 @@ public final class PerfettoTrackEventExtra { /** * Start a {@link Builder} to build a {@link PerfettoTrackEventExtra}. */ - public static Builder builder() { - return sTrackEventExtra.get().mBuilderCache.get(sBuilderSupplier).initInternal(null, null, - false); + public static Builder builder(boolean isCategoryEnabled) { + if (isCategoryEnabled) { + return sTrackEventExtra.get().mBuilderCache.get(sBuilderSupplier) + .initInternal(null, null); + } + return NO_OP_BUILDER; } private final RingBuffer<NamedTrack> mNamedTrackCache = |