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

Commit 95f4abe5 authored by Peter Kalauskas's avatar Peter Kalauskas
Browse files

tracinglib: remove nameCoroutine()

Function was deprecated, and improper usage of it would result in
missing trace sections. Remove it to avoid confusion.

Test: tracinglib-robo-test
Bug: 289353932
Flag: EXEMPT refactor
Change-Id: Ib46c8f4604a4aa7e401b0e22bdc3f8a2a67e9a9f
parent c32cb42c
Loading
Loading
Loading
Loading
+4 −5
Original line number Diff line number Diff line
@@ -24,11 +24,10 @@ import android.platform.test.rule.EnsureDeviceSettingsRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.app.tracing.coroutines.createCoroutineTracingContext
import com.android.app.tracing.coroutines.nameCoroutine
import com.android.app.tracing.coroutines.launchTraced
import com.android.app.tracing.coroutines.traceCoroutine
import com.android.systemui.Flags.FLAG_COROUTINE_TRACING
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import kotlinx.coroutines.yield
@@ -81,7 +80,7 @@ class TraceContextMicroBenchmark {
        val state = perfStatusReporter.benchmarkState

        val context1 = createCoroutineTracingContext("scope1")
        val context2 = nameCoroutine("scope2")
        val context2 = createCoroutineTracingContext("scope2")
        runBlocking {
            while (state.keepRunning()) {
                withContext(context1) {
@@ -108,7 +107,7 @@ class TraceContextMicroBenchmark {

        runBlocking(createCoroutineTracingContext("root")) {
            val job1 =
                launch(nameCoroutine("scope1")) {
                launchTraced("scope1") {
                    while (true) {
                        traceCoroutine("hello") {
                            traceCoroutine("world") { yield() }
@@ -117,7 +116,7 @@ class TraceContextMicroBenchmark {
                    }
                }
            val job2 =
                launch(nameCoroutine("scope2")) {
                launchTraced("scope2") {
                    while (true) {
                        traceCoroutine("hallo") {
                            traceCoroutine("welt") { yield() }
+0 −30
Original line number Diff line number Diff line
@@ -138,36 +138,6 @@ public fun createCoroutineTracingContext(
    }
}

/**
 * Returns a new [CoroutineTraceName] (or [EmptyCoroutineContext] if `coroutine_tracing` feature is
 * flagged off). When the current [CoroutineScope] has a [TraceContextElement] installed,
 * [CoroutineTraceName] can be used to name the child scope under construction.
 *
 * [TraceContextElement] should be installed on the root, and [CoroutineTraceName] on the children.
 */
@Deprecated("Use .launchInTraced, .launchTraced, .shareInTraced, etc.")
public fun nameCoroutine(name: String): CoroutineContext = nameCoroutine { name }

/**
 * Returns a new [CoroutineTraceName] (or [EmptyCoroutineContext] if `coroutine_tracing` feature is
 * flagged off). When the current [CoroutineScope] has a [TraceContextElement] installed,
 * [CoroutineTraceName] can be used to name the child scope under construction.
 *
 * [TraceContextElement] should be installed on the root, and [CoroutineTraceName] on the children.
 *
 * @param name lazy string to only be called if feature is enabled
 */
@OptIn(ExperimentalContracts::class)
@Deprecated("Use .launchInTraced, .launchTraced, .shareInTraced, etc.")
public inline fun nameCoroutine(name: () -> String): CoroutineContext {
    contract { callsInPlace(name, InvocationKind.AT_MOST_ONCE) }
    return if (com.android.systemui.Flags.coroutineTracing()) {
        CoroutineTraceName(name())
    } else {
        EmptyCoroutineContext
    }
}

private object PerfettoTraceConfig {
    // cc = coroutine continuations
    @JvmField val COROUTINE_CATEGORY: PerfettoTrace.Category = PerfettoTrace.Category("cc")
+2 −2
Original line number Diff line number Diff line
@@ -15,7 +15,7 @@
 */
package com.example.tracing.demo.experiments

import com.android.app.tracing.coroutines.nameCoroutine
import com.android.app.tracing.coroutines.asyncTraced
import com.android.app.tracing.coroutines.traceCoroutine
import com.android.app.tracing.traceSection
import com.example.tracing.demo.FixedThread1
@@ -76,7 +76,7 @@ constructor(
        // no dispatcher specified, so will inherit dispatcher from whoever called
        // run(), meaning the main thread
        val deferredE =
            async(nameCoroutine("overridden-scope-name-for-deferredE")) {
            asyncTraced("overridden-scope-name-for-deferredE") {
                traceCoroutine("async#E") { forceSuspend("deferredE", 25) }
            }

+1 −1
Original line number Diff line number Diff line
@@ -128,7 +128,7 @@ constructor(
    //                // Alternatively, call `.flowName("hello-cold")` before or after `flowOn`
    // changes
    //                // the dispatcher.
    //                .flowOn(nameCoroutine("hello-cold") + dispatcherA)
    //                .flowOn(CoroutineTraceName("hello-cold") + dispatcherA)
    //                .map { it * 2 }
    //                .collectTraced("my-collector") { forceSuspend("A:$it", 1) }
    //        }
+3 −4
Original line number Diff line number Diff line
@@ -23,7 +23,6 @@ import com.android.app.tracing.coroutines.CoroutineTraceName
import com.android.app.tracing.coroutines.TraceContextElement
import com.android.app.tracing.coroutines.coroutineScopeTraced
import com.android.app.tracing.coroutines.launchTraced
import com.android.app.tracing.coroutines.nameCoroutine
import com.android.app.tracing.coroutines.traceCoroutine
import com.android.app.tracing.coroutines.withContextTraced
import com.android.systemui.Flags.FLAG_COROUTINE_TRACING
@@ -172,8 +171,8 @@ class CoroutineTracingTest : TestBase() {
        runTest(finalEvent = 4) {
            assertTrue(coroutineContext[CoroutineTraceName] is TraceContextElement)
            expect(1, "1^main")
            withContext(nameCoroutine("inside-withContext")) { // <-- BAD, DON'T DO THIS
                // This is why nameCoroutine() should not be used this way, it overwrites the
            withContext(CoroutineTraceName("inside-withContext")) { // <-- BAD, DON'T DO THIS
                // This is why CoroutineTraceName() should not be used this way, it overwrites the
                // TraceContextElement. Because it is not a CopyableThreadContextElement, it is
                // not given opportunity to merge with the parent trace context.
                // While we could make CoroutineTraceName a CopyableThreadContextElement, it would
@@ -305,7 +304,7 @@ class CoroutineTracingTest : TestBase() {
            expect(1, "1^main")
            delay(1)
            expect(2, "1^main")
            val reusedNameContext = nameCoroutine("my-coroutine")
            val reusedNameContext = CoroutineTraceName("my-coroutine")
            launch(reusedNameContext) {
                expect(3, "1^main:1^my-coroutine")
                delay(1)
Loading