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

Commit 72cbd036 authored by Peter Kalauskas's avatar Peter Kalauskas
Browse files

Updates to coroutine tracing

 - Read aconfig flag in tracinglib directly

 - Move coroutine tracing utils to separate package to making
   extension functions easier to find. This way, the extensions
   for kotlinx.courtines.* can be found under
   com.android.app.tracing.coroutines.*. Similarly, in the future,
   extensions for flows will be under
   com.android.app.tracing.coroutines.flow.*

 - Hide implementation details, change visibility to internal or
   private on various members.

 - Replace Lazy<String> with lambda to simplify bytecode

 - Use sealed class to simplify conditional logic for when coroutine
   tracing is not enabled or available in the current coroutine context.

 - Simplify TraceUtils object declaration (it doesn't need to be a
   companion object)

 - Use random numbers for trace cookies. Generating a random number
   should be faster than using atomic integers. Cookie collisions would
   be extremely rare, and if they do happen, the worst that would
   happen is a momentarily malformed trace.

Flag: ACONFIG com.android.systemui.coroutine_tracing DEVELOPMENT
Bug: 289353932
Test: SystemUITests
Test: capture trace on phone, async traces work as expected
Change-Id: Ie240a125893282bb6d79614bdd1d980aaad82645
parent b87b35a2
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ java_library {
    name: "tracinglib",
    static_libs: [
        "kotlinx_coroutines_android",
        "com_android_systemui_flags_lib",
    ],
    srcs: [
        "src/**/*.kt"
+162 −394

File changed.

Preview size limit exceeded, changes collapsed.

+234 −0

File added.

Preview size limit exceeded, changes collapsed.

+16 −6
Original line number Diff line number Diff line
@@ -14,16 +14,25 @@
 * limitations under the License.
 */

package com.android.app.tracing
package com.android.app.tracing.coroutines

import com.android.app.tracing.TraceUtils.Companion.instant
import com.android.app.tracing.TraceUtils.Companion.traceCoroutine
import com.android.app.tracing.TraceUtils.instant
import com.android.systemui.Flags.coroutineTracing
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext
import kotlinx.coroutines.CopyableThreadContextElement
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.ExperimentalCoroutinesApi

/**
 * Returns a new [CoroutineContext] used for tracing. Used to hide internal implementation details.
 */
@OptIn(ExperimentalCoroutinesApi::class)
fun createCoroutineTracingContext(): CoroutineContext {
    return if (coroutineTracing()) TraceContextElement() else EmptyCoroutineContext
}

/**
 * Used for safely persisting [TraceData] state when coroutines are suspended and resumed.
 *
@@ -34,12 +43,13 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi
 */
@OptIn(DelicateCoroutinesApi::class)
@ExperimentalCoroutinesApi
class TraceContextElement(private val traceData: TraceData = TraceData()) :
internal class TraceContextElement(private val traceData: TraceData = TraceData()) :
    CopyableThreadContextElement<TraceData?> {

    companion object Key : CoroutineContext.Key<TraceContextElement>
    internal companion object Key : CoroutineContext.Key<TraceContextElement>

    override val key: CoroutineContext.Key<TraceContextElement> = Key
    override val key: CoroutineContext.Key<*>
        get() = Key

    @OptIn(ExperimentalStdlibApi::class)
    override fun updateThreadContext(context: CoroutineContext): TraceData? {
Loading