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

Commit 8d024f8e authored by Nicolò Mazzucato's avatar Nicolò Mazzucato Committed by Android (Google) Code Review
Browse files

Merge "Add instantForGroup utils to TrackTracer" into main

parents b9dc4ca3 ab8561a3
Loading
Loading
Loading
Loading
+31 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.app.tracing

public object TrackGroupUtils {
    /**
     * Generates a track name in a way that perfetto can group tracks together.
     *
     * This leverages the "Create process workspace" perfetto plugin. This plugins parses all the
     * tracks that follow the "groupName##trackName" format, nesting "trackName" under "groupName".
     *
     * This allows to easily group tracks that are related under a single summary track (e.g. all
     * "shade" related tracks will appear together, under the "shade" track in the process
     * workspace).
     */
    public fun trackGroup(groupName: String, trackName: String): String = "$groupName##$trackName"
}
+33 −1
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.app.tracing.coroutines

import android.os.Trace
import com.android.app.tracing.TraceUtils
import com.android.app.tracing.TrackGroupUtils.trackGroup
import java.io.Closeable
import java.util.concurrent.ThreadLocalRandom
import kotlin.contracts.ExperimentalContracts
@@ -41,9 +42,13 @@ import kotlin.contracts.contract
 */
@OptIn(ExperimentalContracts::class)
public class TrackTracer(
    public val trackName: String,
    trackName: String,
    public val traceTag: Long = Trace.TRACE_TAG_APP,
    public val trackGroup: String? = null,
) {
    public val trackName: String =
        if (trackGroup != null) trackGroup(trackGroup, trackName) else trackName

    /** See [Trace.instantForTrack]. */
    public inline fun instant(s: () -> String) {
        if (!Trace.isEnabled()) return
@@ -71,4 +76,31 @@ public class TrackTracer(
        Trace.asyncTraceForTrackBegin(traceTag, trackName, sliceName, cookie)
        return Closeable { Trace.asyncTraceForTrackEnd(traceTag, trackName, cookie) }
    }

    public companion object {
        /**
         * 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, i: Int) {
            Trace.traceCounter(Trace.TRACE_TAG_APP, trackGroup(groupName, trackName), i)
        }

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

        /**
         * Creates an instant event, see [instantForGroup], converting [i] to an int by multiplying
         * it by 100.
         */
        @JvmStatic
        public fun instantForGroup(groupName: String, trackName: String, i: Float) {
            instantForGroup(groupName, trackName, (i * 100).toInt())
        }
    }
}