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
diff --git a/packages/SystemUI/src/com/android/systemui/lifecycle/RepeatWhenAttached.kt b/packages/SystemUI/src/com/android/systemui/lifecycle/RepeatWhenAttached.kt
index 5dafd94..c997617 100644
--- a/packages/SystemUI/src/com/android/systemui/lifecycle/RepeatWhenAttached.kt
+++ b/packages/SystemUI/src/com/android/systemui/lifecycle/RepeatWhenAttached.kt
@@ -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 @@
Dispatchers.Main + createCoroutineTracingContext() + coroutineContext
val traceName =
if (Compile.IS_DEBUG && coroutineTracing()) {
- traceSectionName()
+ inferTraceSectionName()
} else {
DEFAULT_TRACE_NAME
}
@@ -197,16 +198,21 @@
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 {
- val interestingFrame =
- StackWalker.getInstance().walk { stream ->
- stream.filter(::isFrameInteresting).limit(5).findFirst()
+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 f = interestingFrame.get()
+ return "${f.className}#${f.methodName}:${f.lineNumber} [$DEFAULT_TRACE_NAME]"
+ } else {
+ return DEFAULT_TRACE_NAME
}
- if (interestingFrame.isPresent) {
- val frame = interestingFrame.get()
- return "${frame.className}#${frame.methodName}:${frame.lineNumber} [$DEFAULT_TRACE_NAME]"
- } else {
- return DEFAULT_TRACE_NAME
+ } finally {
+ Trace.traceEnd(Trace.TRACE_TAG_APP)
}
}