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

Commit be71bc4c authored by Pablo Gamito's avatar Pablo Gamito
Browse files

Dump jank CUJ events in another thread

To avoid any latency caused by event logging in SysUI

Bug: 267482772

Test: atest FlickerLibTest
Change-Id: Ia855fb09b19aa006c77b2ec3f52fabbc011089b6
parent 86b4e5ad
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -3,8 +3,8 @@
option java_package com.android.internal.jank;

# Marks a request to start tracing a CUJ. Doesn't mean the request was executed.
37001 jank_cuj_events_begin_request (CUJ Type|1|5),(Elapsed Time Ns|2|3),(Uptime Ns|2|3)
37001 jank_cuj_events_begin_request (CUJ Type|1|5),(Unix Time Ns|2|3),(Elapsed Time Ns|2|3),(Uptime Ns|2|3)
# Marks a request to end tracing a CUJ. Doesn't mean the request was executed.
37002 jank_cuj_events_end_request (CUJ Type|1|5),(Elapsed Time Ns|2|3),(Uptime Time Ns|2|3)
37002 jank_cuj_events_end_request (CUJ Type|1|5),(Unix Time Ns|2|3),(Elapsed Time Ns|2|3),(Uptime Time Ns|2|3)
# Marks a request to cancel tracing a CUJ. Doesn't mean the request was executed.
37003 jank_cuj_events_cancel_request (CUJ Type|1|5),(Elapsed Time Ns|2|3),(Uptime Time Ns|2|3)
37003 jank_cuj_events_cancel_request (CUJ Type|1|5),(Unix Time Ns|2|3),(Elapsed Time Ns|2|3),(Uptime Time Ns|2|3)
+30 −6
Original line number Diff line number Diff line
@@ -128,6 +128,7 @@ import com.android.internal.util.PerfettoTrigger;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.time.Instant;
import java.util.Locale;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
@@ -578,8 +579,10 @@ public class InteractionJankMonitor {
    public boolean begin(@NonNull Configuration.Builder builder) {
        try {
            final Configuration config = builder.build();
            postEventLogToWorkerThread((unixNanos, elapsedNanos, realtimeNanos) -> {
                EventLogTags.writeJankCujEventsBeginRequest(
                    config.mCujType, SystemClock.elapsedRealtimeNanos(), SystemClock.uptimeNanos());
                        config.mCujType, unixNanos, elapsedNanos, realtimeNanos);
            });
            final TrackerResult result = new TrackerResult();
            final boolean success = config.getHandler().runWithScissors(
                    () -> result.mResult = beginInternal(config), EXECUTOR_TASK_TIMEOUT);
@@ -653,8 +656,10 @@ public class InteractionJankMonitor {
     * @return boolean true if the tracker is ended successfully, false otherwise.
     */
    public boolean end(@CujType int cujType) {
        EventLogTags.writeJankCujEventsEndRequest(cujType, SystemClock.elapsedRealtimeNanos(),
                SystemClock.uptimeNanos());
        postEventLogToWorkerThread((unixNanos, elapsedNanos, realtimeNanos) -> {
            EventLogTags.writeJankCujEventsEndRequest(
                    cujType, unixNanos, elapsedNanos, realtimeNanos);
        });
        FrameTracker tracker = getTracker(cujType);
        // Skip this call since we haven't started a trace yet.
        if (tracker == null) return false;
@@ -692,8 +697,10 @@ public class InteractionJankMonitor {
     * @return boolean true if the tracker is cancelled successfully, false otherwise.
     */
    public boolean cancel(@CujType int cujType) {
        EventLogTags.writeJankCujEventsCancelRequest(cujType, SystemClock.elapsedRealtimeNanos(),
                SystemClock.uptimeNanos());
        postEventLogToWorkerThread((unixNanos, elapsedNanos, realtimeNanos) -> {
            EventLogTags.writeJankCujEventsCancelRequest(
                    cujType, unixNanos, elapsedNanos, realtimeNanos);
        });
        return cancel(cujType, REASON_CANCEL_NORMAL);
    }

@@ -1284,4 +1291,21 @@ public class InteractionJankMonitor {
            return mReason;
        }
    }

    @FunctionalInterface
    private interface TimeFunction {
        void invoke(long unixNanos, long elapsedNanos, long realtimeNanos);
    }

    private void postEventLogToWorkerThread(TimeFunction logFunction) {
        final Instant now = Instant.now();
        final long unixNanos = TimeUnit.NANOSECONDS.convert(now.getEpochSecond(), TimeUnit.SECONDS)
                + now.getNano();
        final long elapsedNanos = SystemClock.elapsedRealtimeNanos();
        final long realtimeNanos = SystemClock.uptimeNanos();

        mWorker.getThreadHandler().post(() -> {
            logFunction.invoke(unixNanos, elapsedNanos, realtimeNanos);
        });
    }
}