From 0a72a6db31de82243d400fe6b2bdf5d2aa81c6c2 Mon Sep 17 00:00:00 2001 From: Nicolo' Mazzucato Date: Tue, 4 Jul 2023 12:46:00 +0000 Subject: [PATCH] Add traceAsync to TraceUtils This utility is useful to trace coroutine code, as the beginning and the end of the event might end up in different threads. Note that stacks are maintained as long as the same trackName is used. When no stackName is used, "AsyncTraces" default is used. Test: New track appearing in a perfetto trace Bug: 289353932 Change-Id: I0fbf745d058e6a329d6b2229178a52af21e84033 --- .../com/android/systemui/util/TraceUtils.kt | 54 +++++++++++++++---- .../KeyguardQuickAffordanceInteractor.kt | 7 ++- 2 files changed, 49 insertions(+), 12 deletions(-) diff --git a/packages/SystemUI/shared/src/com/android/systemui/util/TraceUtils.kt b/packages/SystemUI/shared/src/com/android/systemui/util/TraceUtils.kt index 64234c205617a..e02e592c32cc0 100644 --- a/packages/SystemUI/shared/src/com/android/systemui/util/TraceUtils.kt +++ b/packages/SystemUI/shared/src/com/android/systemui/util/TraceUtils.kt @@ -18,22 +18,23 @@ package com.android.systemui.util import android.os.Trace import android.os.TraceNameSupplier +import java.util.concurrent.atomic.AtomicInteger /** - * Run a block within a [Trace] section. - * Calls [Trace.beginSection] before and [Trace.endSection] after the passed block. + * Run a block within a [Trace] section. Calls [Trace.beginSection] before and [Trace.endSection] + * after the passed block. */ inline fun traceSection(tag: String, block: () -> T): T = - if (Trace.isTagEnabled(Trace.TRACE_TAG_APP)) { - Trace.traceBegin(Trace.TRACE_TAG_APP, tag) - try { - block() - } finally { - Trace.traceEnd(Trace.TRACE_TAG_APP) - } - } else { + if (Trace.isTagEnabled(Trace.TRACE_TAG_APP)) { + Trace.traceBegin(Trace.TRACE_TAG_APP, tag) + try { block() + } finally { + Trace.traceEnd(Trace.TRACE_TAG_APP) } + } else { + block() + } class TraceUtils { companion object { @@ -43,6 +44,7 @@ class TraceUtils { /** * Helper function for creating a Runnable object that implements TraceNameSupplier. + * * This is useful for posting Runnables to Handlers with meaningful names. */ inline fun namedRunnable(tag: String, crossinline block: () -> Unit): Runnable { @@ -51,5 +53,37 @@ class TraceUtils { override fun run() = block() } } + + /** + * Cookie used for async traces. Shouldn't be public, but to use it inside inline methods + * there is no other way around. + */ + val lastCookie = AtomicInteger(0) + + /** + * Creates an async slice in a track called "AsyncTraces". + * + * This can be used to trace coroutine code. Note that all usages of this method will appear + * under a single track. + */ + inline fun traceAsync(method: String, block: () -> T): T = + traceAsync(method, "AsyncTraces", block) + + /** + * Creates an async slice in a track with [trackName] while [block] runs. + * + * This can be used to trace coroutine code. [method] will be the name of the slice, + * [trackName] of the track. The track is one of the rows visible in a perfetto trace inside + * SystemUI process. + */ + inline fun traceAsync(method: String, trackName: String, block: () -> T): T { + val cookie = lastCookie.incrementAndGet() + Trace.asyncTraceForTrackBegin(Trace.TRACE_TAG_APP, trackName, method, cookie) + try { + return block() + } finally { + Trace.asyncTraceForTrackEnd(Trace.TRACE_TAG_APP, trackName, cookie) + } + } } } diff --git a/packages/SystemUI/src/com/android/systemui/keyguard/domain/interactor/KeyguardQuickAffordanceInteractor.kt b/packages/SystemUI/src/com/android/systemui/keyguard/domain/interactor/KeyguardQuickAffordanceInteractor.kt index d1ac49bd44099..f692a390c8477 100644 --- a/packages/SystemUI/src/com/android/systemui/keyguard/domain/interactor/KeyguardQuickAffordanceInteractor.kt +++ b/packages/SystemUI/src/com/android/systemui/keyguard/domain/interactor/KeyguardQuickAffordanceInteractor.kt @@ -49,6 +49,7 @@ import com.android.systemui.settings.UserTracker import com.android.systemui.shared.customization.data.content.CustomizationProviderContract as Contract import com.android.systemui.statusbar.phone.SystemUIDialog import com.android.systemui.statusbar.policy.KeyguardStateController +import com.android.systemui.util.TraceUtils.Companion.traceAsync import dagger.Lazy import javax.inject.Inject import kotlinx.coroutines.CoroutineDispatcher @@ -442,8 +443,10 @@ constructor( } private suspend fun isFeatureDisabledByDevicePolicy(): Boolean = - withContext(backgroundDispatcher) { - devicePolicyManager.areKeyguardShortcutsDisabled(userId = userTracker.userId) + traceAsync("isFeatureDisabledByDevicePolicy", TAG) { + withContext(backgroundDispatcher) { + devicePolicyManager.areKeyguardShortcutsDisabled(userId = userTracker.userId) + } } companion object {