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

Commit a9df4a50 authored by Nicolo' Mazzucato's avatar Nicolo' Mazzucato
Browse files

Add traceSyncAndAsync and traceAsyncClosable

traceSyncAndAsync allows to add a slice both in
the thread the call is done from, and in a separate track.

traceAsyncClosable allows to create a slice in a
specific track and finish it in a nicer way than using the raw api

Bug: 362719719
Test: Perfetto trace (with topic cl)
Flag: NONE - logging method addition to lib
Change-Id: Idd4a4b71b16b6d20c406fe7c64786e54ff75f22c
parent 66f97f4a
Loading
Loading
Loading
Loading
+28 −0
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.app.tracing

import android.annotation.SuppressLint
import android.os.Trace
import com.android.app.tracing.TrackGroupUtils.trackGroup
import com.android.app.tracing.coroutines.traceCoroutine
import java.util.concurrent.ThreadLocalRandom
import kotlin.contracts.ExperimentalContracts
@@ -238,4 +239,31 @@ public object TraceUtils {
            block()
        }
    }

    /** Starts an async slice, and returns a runnable that stops the slice. */
    @JvmStatic
    public fun traceAsyncClosable(
        traceTag: Long = Trace.TRACE_TAG_APP,
        trackName: String,
        sliceName: String,
    ): () -> Unit {
        val cookie = ThreadLocalRandom.current().nextInt()
        Trace.asyncTraceForTrackBegin(traceTag, trackName, sliceName, cookie)
        return { Trace.asyncTraceForTrackEnd(traceTag, trackName, cookie) }
    }

    /** Starts an async slice, and returns a runnable that stops the slice. */
    @JvmStatic
    @JvmOverloads
    public fun traceAsyncClosable(
        traceTag: Long = Trace.TRACE_TAG_APP,
        trackGroupName: String,
        trackName: String,
        sliceName: String,
    ): () -> Unit {
        val groupedTrackName = trackGroup(trackGroupName, trackName)
        val cookie = ThreadLocalRandom.current().nextInt()
        Trace.asyncTraceForTrackBegin(traceTag, groupedTrackName, sliceName, cookie)
        return { Trace.asyncTraceForTrackEnd(traceTag, groupedTrackName, cookie) }
    }
}
+1 −0
Original line number Diff line number Diff line
@@ -27,5 +27,6 @@ public object TrackGroupUtils {
     * "shade" related tracks will appear together, under the "shade" track in the process
     * workspace).
     */
    @JvmStatic
    public fun trackGroup(groupName: String, trackName: String): String = "$groupName##$trackName"
}
+24 −0
Original line number Diff line number Diff line
@@ -77,6 +77,20 @@ public class TrackTracer(
        return Closeable { Trace.asyncTraceForTrackEnd(traceTag, trackName, cookie) }
    }

    /** Traces [block] both sync and async. */
    public fun traceSyncAndAsync(sliceName: () -> String, block: () -> Unit) {
        contract {
            callsInPlace(block, InvocationKind.EXACTLY_ONCE)
            callsInPlace(sliceName, InvocationKind.AT_MOST_ONCE)
        }
        if (Trace.isEnabled()) {
            val name = sliceName()
            TraceUtils.trace(name) { traceAsync(name, block) }
        } else {
            block()
        }
    }

    public companion object {
        /**
         * Creates an instant event for a track called [trackName] inside [groupName]. See
@@ -87,6 +101,16 @@ public class TrackTracer(
            Trace.traceCounter(Trace.TRACE_TAG_APP, trackGroup(groupName, trackName), i)
        }

        /**
         * Creates an instant event for a track called [trackName] inside [groupName]. See
         * [trackGroup] for details on how the rendering in groups works.
         */
        @JvmStatic
        public fun instantForGroup(groupName: String, trackName: String, event: () -> String) {
            if (!Trace.isEnabled()) return
            Trace.instantForTrack(Trace.TRACE_TAG_APP, trackGroup(groupName, trackName), event())
        }

        /** Creates an instant event for [groupName] grgorp, see [instantForGroup]. */
        @JvmStatic
        public inline fun instantForGroup(groupName: String, trackName: () -> String, i: Int) {