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

Commit 6d421ef4 authored by Peter Kalauskas's avatar Peter Kalauskas Committed by Android (Google) Code Review
Browse files

Merge changes from topic "tracinglib-split" into main

* changes:
  Split tracinglib into API variants (2/2)
  Split tracinglib into API variants (1/2)
parents 0d67f91f 342bfc43
Loading
Loading
Loading
Loading
+26 −5
Original line number Diff line number Diff line
@@ -17,14 +17,35 @@ package {
}

java_library {
    name: "tracinglib",
    name: "tracinglib-platform",
    defaults: ["tracinglib-defaults"],
    srcs: ["src/platform/kotlin/**/*.kt"],
    platform_apis: true,
}

java_library {
    name: "tracinglib-androidx",
    defaults: ["tracinglib-defaults"],
    srcs: ["src/androidx/kotlin/**/*.kt"],
    sdk_version: "31",
    min_sdk_version: "19",
    static_libs: [
        "androidx.tracing_tracing",
    ],
}

java_defaults {
    name: "tracinglib-defaults",
    common_srcs: ["src/common/kotlin/**/*.kt"],
    static_libs: [
        "kotlinx_coroutines_android",
        "com_android_systemui_flags_lib",
    ],
    srcs: [
        "src/**/*.kt"
    kotlincflags: [
        "-Xjvm-default=all",
        "-Xmulti-platform",
        "-opt-in=kotlin.ExperimentalStdlibApi",
        "-opt-in=kotlinx.coroutines.DelicateCoroutinesApi",
        "-opt-in=kotlinx.coroutines.ExperimentalCoroutinesApi",
    ],
    kotlincflags: ["-Xjvm-default=all"],
    platform_apis: true,
}
+72 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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

import androidx.tracing.Trace
import kotlin.random.Random

internal actual fun traceCounter(counterName: String, counterValue: Int) {
    Trace.setCounter(counterName, counterValue)
}

internal actual fun traceBegin(methodName: String) {
    Trace.beginSection(methodName)
}

internal actual fun traceEnd() {
    Trace.endSection()
}

internal actual fun asyncTraceBegin(methodName: String, cookie: Int) {
    Trace.beginAsyncSection(methodName, cookie)
}

internal actual fun asyncTraceEnd(methodName: String, cookie: Int) {
    Trace.endAsyncSection(methodName, cookie)
}

private fun namedSlice(trackName: String, methodName: String) = "$trackName:$methodName"

@PublishedApi
internal actual fun asyncTraceForTrackBegin(trackName: String, methodName: String, cookie: Int) {
    if (isEnabled()) {
        asyncTraceBegin(namedSlice(trackName, methodName), cookie)
    }
}

@PublishedApi
internal actual fun asyncTraceForTrackEnd(trackName: String, methodName: String, cookie: Int) {
    if (isEnabled()) {
        asyncTraceEnd(namedSlice(trackName, methodName), cookie)
    }
}

internal actual fun instant(eventName: String) {
    if (isEnabled()) {
        traceBegin("instant:$eventName")
        traceEnd()
    }
}

internal actual fun instantForTrack(trackName: String, eventName: String) {
    if (Trace.isEnabled()) {
        val cookie = Random.nextInt()
        val name = "instant:${namedSlice(trackName,eventName)}"
        asyncTraceBegin(name, cookie)
        asyncTraceEnd(name, cookie)
    }
}
+1 −2
Original line number Diff line number Diff line
@@ -15,7 +15,6 @@
 */
package com.android.app.tracing

import android.os.Trace
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.onEach

@@ -39,7 +38,7 @@ object FlowTracing {
        var count = 0
        return onEach {
            count += 1
            Trace.traceCounter(Trace.TRACE_TAG_APP, trackName, count)
            traceCounter(trackName, count)
        }
    }
}
+60 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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

internal expect fun traceCounter(counterName: String, counterValue: Int)

internal expect fun traceBegin(methodName: String)

internal expect fun traceEnd()

internal expect fun asyncTraceBegin(methodName: String, cookie: Int)

internal expect fun asyncTraceEnd(methodName: String, cookie: Int)

internal expect fun asyncTraceForTrackBegin(trackName: String, methodName: String, cookie: Int)

internal expect fun asyncTraceForTrackEnd(trackName: String, methodName: String, cookie: Int)

/**
 * Writes a trace message indicating that an instant event occurred on the current thread. Unlike
 * slices, instant events have no duration and do not need to be matched with another call. Perfetto
 * will display instant events using an arrow pointing to the timestamp they occurred:
 * ```
 * Thread #1 | [==============]               [======]
 *           |     [====]                        ^
 *           |        ^
 * ```
 *
 * @param eventName The name of the event to appear in the trace.
 */
internal expect fun instant(eventName: String)

/**
 * Writes a trace message indicating that an instant event occurred on the given track. Unlike
 * slices, instant events have no duration and do not need to be matched with another call. Perfetto
 * will display instant events using an arrow pointing to the timestamp they occurred:
 * ```
 * Async  | [==============]               [======]
 *  Track |     [====]                        ^
 *   Name |        ^
 * ```
 *
 * @param trackName The track where the event should appear in the trace.
 * @param eventName The name of the event to appear in the trace.
 */
internal expect fun instantForTrack(trackName: String, eventName: String)
Loading