Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8601e96a authored by Zimuzo Ezeozue's avatar Zimuzo Ezeozue
Browse files

Remove unnecessary interface from Perfetto APIs

1. Removed the Builder interfaces from PerfettoTrackEventExtra
to improve performance. The interface was needed for a
NoOp impl when tracing was disabled. Instead, we just store
a boolean in the builder impl representing whether
the category is enabled or not, all the builder methods
return early if false. This saves an additional dependent load
instruction and an adrp + ldr sequence. This improves the
benchmark by a few nanos which adds up for each builder method
executed.

2. Moved the check* calls behind a DEBUG flag. This
elided the function calls entirely which were also getting inlined
and likely spilling the instruction cache. It's a debugging check
when generating protos. Besides, proto generation code is likely
to change in coming cls.

3. Added a setFlow/setTerminatingFlow call. This limits flows that
can be added to an event to at most 1 flow and terminating flow.
This is the most common case in practice anyways and we can avoid
the expensive indirection to get a flow via the Pool class.

godbolt playground to analyze the generated dex2oat code:
https://godbolt.org/z/Er9Gjvhfd

In a follow up, I'll remove addFlow/addTerminatingFlow

Test: atest PerfettoTest
Test: atest TracePerfTest
Bug: 303199244
Flag: android.os.perfetto_sdk_tracing_v2
Change-Id: I38f9853567734425ef8721dbbd93df8d83d7e257
parent 6b3d33e2
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -127,14 +127,14 @@ public class TracePerfTest {
    public void testInstantPerfettoWithArgs() {
        PerfettoTrace.instant(FOO_CATEGORY, "testInstantP")
                .addArg("foo", "bar")
                .addFlow(1)
                .setFlow(1)
                .emit();

        BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        while (state.keepRunning()) {
            PerfettoTrace.instant(FOO_CATEGORY, "testInstantP")
                    .addArg("foo", "bar")
                    .addFlow(1)
                    .setFlow(1)
                    .emit();
        }
    }
+6 −2
Original line number Diff line number Diff line
@@ -45,11 +45,15 @@ HSPLandroid/os/MessageQueue$OnFileDescriptorEventListener;->*
HSPLandroid/os/MessageQueue$StackNodeType;->*
HSPLandroid/os/MessageQueue$StateNode;->*
HSPLandroid/os/MessageQueue$TimedParkStateNode;->*

# For now, compile all methods in PerfettoTrace and PerfettoTrackEventExtra.
# Similar to the existing Trace APIs, these new APIs can impact the performance
# of many subsystems including MessageQueue. This also keeps benchmark
# comparisons between both APIs fair.
HSPLandroid/os/PerfettoTrace$Category;->*
HSPLandroid/os/PerfettoTrace;->*
HSPLandroid/os/PerfettoTrackEventExtra;->*
HSPLandroid/os/PerfettoTrackEventExtra$BuilderImpl;->*
HSPLandroid/os/PerfettoTrackEventExtra$NoOpBuilder;->*
HSPLandroid/os/PerfettoTrackEventExtra$Builder;->*
HSPLandroid/os/PerfettoTrackEventExtra$ArgBool;->*
HSPLandroid/os/PerfettoTrackEventExtra$ArgInt64;->*
HSPLandroid/os/PerfettoTrackEventExtra$ArgDouble;->*
+0 −20
Original line number Diff line number Diff line
@@ -232,10 +232,6 @@ public final class PerfettoTrace {
     * @param eventName The event name to appear in the trace.
     */
    public static PerfettoTrackEventExtra.Builder instant(Category category, String eventName) {
        if (!category.isEnabled()) {
            return PerfettoTrackEventExtra.noOpBuilder();
        }

        return PerfettoTrackEventExtra.builder().init(PERFETTO_TE_TYPE_INSTANT, category)
            .setEventName(eventName);
    }
@@ -247,10 +243,6 @@ public final class PerfettoTrace {
     * @param eventName The event name to appear in the trace.
     */
    public static PerfettoTrackEventExtra.Builder begin(Category category, String eventName) {
        if (!category.isEnabled()) {
            return PerfettoTrackEventExtra.noOpBuilder();
        }

        return PerfettoTrackEventExtra.builder().init(PERFETTO_TE_TYPE_SLICE_BEGIN, category)
            .setEventName(eventName);
    }
@@ -261,10 +253,6 @@ public final class PerfettoTrace {
     * @param category The perfetto category.
     */
    public static PerfettoTrackEventExtra.Builder end(Category category) {
        if (!category.isEnabled()) {
            return PerfettoTrackEventExtra.noOpBuilder();
        }

        return PerfettoTrackEventExtra.builder().init(PERFETTO_TE_TYPE_SLICE_END, category);
    }

@@ -275,10 +263,6 @@ public final class PerfettoTrace {
     * @param value The value of the counter.
     */
    public static PerfettoTrackEventExtra.Builder counter(Category category, long value) {
        if (!category.isEnabled()) {
            return PerfettoTrackEventExtra.noOpBuilder();
        }

        return PerfettoTrackEventExtra.builder().init(PERFETTO_TE_TYPE_COUNTER, category)
            .setCounter(value);
    }
@@ -302,10 +286,6 @@ public final class PerfettoTrace {
     * @param value The value of the counter.
     */
    public static PerfettoTrackEventExtra.Builder counter(Category category, double value) {
        if (!category.isEnabled()) {
            return PerfettoTrackEventExtra.noOpBuilder();
        }

        return PerfettoTrackEventExtra.builder().init(PERFETTO_TE_TYPE_COUNTER, category)
            .setCounter(value);
    }
+302 −297

File changed.

Preview size limit exceeded, changes collapsed.

+2 −2
Original line number Diff line number Diff line
@@ -108,8 +108,8 @@ public class PerfettoTraceTest {
        PerfettoTrace.Session session = new PerfettoTrace.Session(true, traceConfig.toByteArray());

        PerfettoTrace.instant(FOO_CATEGORY, "event")
                .addFlow(2)
                .addTerminatingFlow(3)
                .setFlow(2)
                .setTerminatingFlow(3)
                .addArg("long_val", 10000000000L)
                .addArg("bool_val", true)
                .addArg("double_val", 3.14)