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

Commit 9bbafa88 authored by Peter Kalauskas's avatar Peter Kalauskas Committed by Android Build Cherrypicker Worker
Browse files

tracinglib: enable strict mode

Enable strict API mode, requiring visibility and return types to be
specified explicitly.

Also, adjust Soong build rules to make use of internal visibility,
despite lack of friends support.

Also, fix tests:

 - Remove unused and unnecessary test, testHandlerDispatcher

 - Fix collectTraced12_badTransform, which had not way to confirm if
   expected exception was through

 - Fix flakes in unconfinedLaunch test

Also, use removeLast() method now that it is available again.

Test: atest tracinglib-robo-test
Flag: com.android.systemui.coroutine_tracing
Bug: 289353932
(cherry picked from https://googleplex-android-review.googlesource.com/q/commit:39d41fc302d096b0149968401ec0743684e79992)
Merged-In: I3acfe48c706aaa2119c0565d2544803d6128781c
Change-Id: I3acfe48c706aaa2119c0565d2544803d6128781c
parent 1668e68c
Loading
Loading
Loading
Loading
+6 −3
Original line number Diff line number Diff line
@@ -20,7 +20,10 @@ package {
android_test {
    name: "tracinglib-benchmark",

    srcs: ["src/**/*.kt"],
    srcs: [
        "src/**/*.kt",
        ":tracinglib-core-srcs",
    ],

    static_libs: [
        "androidx.annotation_annotation",
@@ -35,12 +38,12 @@ android_test {
        "flag-junit",
        "kotlinx_coroutines_android",
        "platform-test-rules",
        "tracinglib-platform",
        "com_android_systemui_flags_lib",
    ],

    data: [":perfetto_artifacts"],

    sdk_version: "current",
    platform_apis: true,
    certificate: "platform",
    use_resource_processor: true,

+9 −4
Original line number Diff line number Diff line
@@ -16,20 +16,25 @@ package {
    default_applicable_licenses: ["Android-Apache-2.0"],
}

// TODO(b/240432457): Replace with library once `-Xfriend-paths` is supported by Soong
filegroup {
    name: "tracinglib-core-srcs",
    srcs: ["src/**/*.kt"],
    visibility: ["//frameworks/libs/systemui/tracinglib:__subpackages__"],
}

java_library {
    name: "tracinglib-platform",
    static_libs: [
        "kotlinx_coroutines_android",
        "com_android_systemui_flags_lib",
    ],
    libs: [
        "androidx.annotation_annotation",
    ],
    kotlincflags: [
        "-Xjvm-default=all",
        "-opt-in=kotlin.ExperimentalStdlibApi",
        "-opt-in=kotlinx.coroutines.DelicateCoroutinesApi",
        "-opt-in=kotlinx.coroutines.ExperimentalCoroutinesApi",
        "-Xexplicit-api=strict",
    ],
    srcs: ["src/**/*.kt"],
    srcs: [":tracinglib-core-srcs"],
}
+10 −7
Original line number Diff line number Diff line
@@ -29,14 +29,14 @@ import kotlinx.coroutines.flow.conflate
import kotlinx.coroutines.flow.onEach

/** Utilities to trace Flows */
object FlowTracing {
public object FlowTracing {

    private const val TAG = "FlowTracing"
    private const val DEFAULT_ASYNC_TRACK_NAME = TAG
    private val counter = AtomicInteger(0)

    /** Logs each flow element to a trace. */
    inline fun <T> Flow<T>.traceEach(
    public inline fun <T> Flow<T>.traceEach(
        flowName: String,
        logcat: Boolean = false,
        traceEmissionCount: Boolean = false,
@@ -48,7 +48,7 @@ object FlowTracing {
    }

    /** Records value of a given numeric flow as a counter track in traces. */
    fun <T : Number> Flow<T>.traceAsCounter(
    public fun <T : Number> Flow<T>.traceAsCounter(
        counterName: String,
        traceEmissionCount: Boolean = false,
        valueToInt: (T) -> Int = { it.toInt() },
@@ -62,7 +62,10 @@ object FlowTracing {
    }

    /** Adds a counter track to monitor emissions from a specific flow.] */
    fun <T> Flow<T>.traceEmissionCount(flowName: String, uniqueSuffix: Boolean = false): Flow<T> {
    public fun <T> Flow<T>.traceEmissionCount(
        flowName: String,
        uniqueSuffix: Boolean = false,
    ): Flow<T> {
        val trackName by lazy {
            "$flowName#emissionCount" + if (uniqueSuffix) "\$${counter.addAndGet(1)}" else ""
        }
@@ -78,7 +81,7 @@ object FlowTracing {
     *
     * [flowName] is lazy: it would be computed only if tracing is enabled and only the first time.
     */
    fun <T> Flow<T>.traceEmissionCount(
    public fun <T> Flow<T>.traceEmissionCount(
        flowName: () -> String,
        uniqueSuffix: Boolean = false,
    ): Flow<T> {
@@ -103,7 +106,7 @@ object FlowTracing {
     *
     * This allows to easily have visibility into what's happening in awaitClose.
     */
    suspend fun ProducerScope<*>.tracedAwaitClose(name: String, block: () -> Unit = {}) {
    public suspend fun ProducerScope<*>.tracedAwaitClose(name: String, block: () -> Unit = {}) {
        awaitClose {
            val traceName = { "$name#TracedAwaitClose" }
            traceAsync(DEFAULT_ASYNC_TRACK_NAME, traceName) { traceSection(traceName) { block() } }
@@ -119,7 +122,7 @@ object FlowTracing {
     *
     * Should be used with [tracedAwaitClose] (when needed).
     */
    fun <T> tracedConflatedCallbackFlow(
    public fun <T> tracedConflatedCallbackFlow(
        name: String,
        @BuilderInference block: suspend ProducerScope<T>.() -> Unit,
    ): Flow<T> {
+2 −2
Original line number Diff line number Diff line
@@ -17,7 +17,7 @@
package com.android.app.tracing

/** Utilities to trace automatically computations happening for each element of a list. */
object ListenersTracing {
public object ListenersTracing {

    /**
     * Like [forEach], but outputs a trace for each element.
@@ -33,7 +33,7 @@ object ListenersTracing {
     * listeners.forEachTraced(TAG) { it.dispatch(state) }
     * ```
     */
    inline fun <T : Any> List<T>.forEachTraced(tag: String = "", f: (T) -> Unit) {
    public inline fun <T : Any> List<T>.forEachTraced(tag: String = "", f: (T) -> Unit) {
        forEach { traceSection({ "$tag#${it::javaClass.get().name}" }) { f(it) } }
    }
}
+2 −2
Original line number Diff line number Diff line
@@ -35,7 +35,7 @@ import android.util.Log
 * This creates a new slice in a perfetto trace only if the state is different than the previous
 * one.
 */
class TraceStateLogger
public class TraceStateLogger
@JvmOverloads
constructor(
    private val trackName: String,
@@ -47,7 +47,7 @@ constructor(
    private var previousValue: String? = null

    /** If needed, logs the value to a track with name [trackName]. */
    fun log(newValue: String) {
    public fun log(newValue: String) {
        if (instantEvent) {
            Trace.instantForTrack(Trace.TRACE_TAG_APP, trackName, newValue)
        }
Loading