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

Commit 3b9dbb08 authored by Peter Kalauskas's avatar Peter Kalauskas Committed by Android (Google) Code Review
Browse files

Merge "Trace coroutine section name generation" into main

parents d7b663b5 7459e822
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"