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

Commit 342bfc43 authored by Peter Kalauskas's avatar Peter Kalauskas
Browse files

Split tracinglib into API variants (2/2)

Build two variants of the tracinglib project: one that uses public APIs,
and another that uses platform APIs.

Use expact/actual functions in TraceProxy.kt for routing API calls.
Use src/androidx and src/platform for the public and platform API
variants.

Use --opt-in kotlinc flags instead of @OptIn annotations.

Flag: ACONFIG com.android.systemui.coroutine_tracing DISABLED
Bug: 289353932
Test: m tracinglib ; m tracinglib-platform
Change-Id: Iec0ecd30372c2fe97894dd421c7443cacefa4ec8
parent 1beae4ee
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)
+3 −4
Original line number Diff line number Diff line
@@ -16,7 +16,6 @@

package com.android.app.tracing

import android.os.Trace
import android.util.Log

/**
@@ -47,11 +46,11 @@ class TraceStateLogger(
    /** If needed, logs the value to a track with name [trackName]. */
    fun log(newValue: String) {
        if (instantEvent) {
            Trace.instantForTrack(Trace.TRACE_TAG_APP, trackName, newValue)
            instantForTrack(trackName, newValue)
        }
        if (logOnlyIfDifferent && previousValue == newValue) return
        Trace.asyncTraceForTrackEnd(Trace.TRACE_TAG_APP, trackName, 0)
        Trace.asyncTraceForTrackBegin(Trace.TRACE_TAG_APP, trackName, newValue, 0)
        previousValue?.let { asyncTraceForTrackEnd(trackName, it, 0) }
        asyncTraceForTrackBegin(trackName, newValue, 0)
        if (logcat) {
            Log.d(trackName, "newValue: $newValue")
        }
Loading