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

Commit 7459e822 authored by Peter Kalauskas's avatar Peter Kalauskas
Browse files

Trace coroutine section name generation

To assess whether coroutine tracing is slow due to context switching,
or if it is slow due to stack unwinding to generate default sections
names, create a new trace section so it can be measured.

Flag: ACONFIG com.android.systemui.coroutine_tracing DISABLED
Bug: 289353932
Test: capture and inspect perfetto trace
Change-Id: I77dcdab40574f1e339c570f6d9984a1bc6aa0644
parent d739277b
Loading
Loading
Loading
Loading
+16 −10
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@

package com.android.systemui.lifecycle

import android.os.Trace
import android.view.View
import android.view.ViewTreeObserver
import androidx.annotation.MainThread
@@ -73,7 +74,7 @@ fun View.repeatWhenAttached(
        Dispatchers.Main + createCoroutineTracingContext() + coroutineContext
    val traceName =
        if (Compile.IS_DEBUG && coroutineTracing()) {
            traceSectionName()
            inferTraceSectionName()
        } else {
            DEFAULT_TRACE_NAME
        }
@@ -197,17 +198,22 @@ private fun isFrameInteresting(frame: StackWalker.StackFrame): Boolean =
    frame.className != CURRENT_CLASS_NAME && frame.className != JAVA_ADAPTER_CLASS_NAME

/** Get a name for the trace section include the name of the call site. */
private fun traceSectionName(): String {
private fun inferTraceSectionName(): String {
    try {
        Trace.traceBegin(Trace.TRACE_TAG_APP, "RepeatWhenAttachedKt#inferTraceSectionName")
        val interestingFrame =
            StackWalker.getInstance().walk { stream ->
                stream.filter(::isFrameInteresting).limit(5).findFirst()
            }
        if (interestingFrame.isPresent) {
        val frame = interestingFrame.get()
        return "${frame.className}#${frame.methodName}:${frame.lineNumber} [$DEFAULT_TRACE_NAME]"
            val f = interestingFrame.get()
            return "${f.className}#${f.methodName}:${f.lineNumber} [$DEFAULT_TRACE_NAME]"
        } else {
            return DEFAULT_TRACE_NAME
        }
    } finally {
        Trace.traceEnd(Trace.TRACE_TAG_APP)
    }
}

private const val DEFAULT_TRACE_NAME = "repeatWhenAttached"