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

Commit a3c49884 authored by Android Build Coastguard Worker's avatar Android Build Coastguard Worker
Browse files

Snap for 11248037 from 6bf95c50 to 24Q2-release

Change-Id: I22b8c47eeadcada6cd0b735cdc9714b0c4b08d5a
parents 3f712669 6bf95c50
Loading
Loading
Loading
Loading

PREUPLOAD.cfg

0 → 100644
+12 −0
Original line number Diff line number Diff line
[Builtin Hooks]
bpfmt = true

[Builtin Hooks Options]
bpfmt = -d

[Hook Scripts]
checkstyle_hook = ${REPO_ROOT}/prebuilts/checkstyle/checkstyle.py --sha ${PREUPLOAD_COMMIT}

ktfmt_hook = ${REPO_ROOT}/external/ktfmt/ktfmt.py --check -i ${REPO_ROOT}/frameworks/libs/systemui/ktfmt_includes.txt ${PREUPLOAD_FILES}

ktlint_hook = ${REPO_ROOT}/prebuilts/ktlint/ktlint.py --no-verify-format -f ${PREUPLOAD_FILES}

ktfmt_includes.txt

0 → 100644
+80 −0
Original line number Diff line number Diff line
+
-animationlib/tests/robolectric/src/com/android/app/animation/robolectric/ShadowAnimationUtils2.kt
-animationlib/tests/src/com/android/app/animation/InterpolatorResourcesTest.kt
-animationlib/tests/src/com/android/app/animation/InterpolatorsAndroidXTest.kt
-motiontoollib/src/com/android/app/motiontool/DdmHandleMotionTool.kt
-motiontoollib/src/com/android/app/motiontool/MotionToolManager.kt
-motiontoollib/tests/com/android/app/motiontool/DdmHandleMotionToolTest.kt
-motiontoollib/tests/com/android/app/motiontool/MotionToolManagerTest.kt
-toruslib/torus-core/src/main/java/com/google/android/torus/core/activity/TorusViewerActivity.kt
-toruslib/torus-core/src/main/java/com/google/android/torus/core/app/KeyguardLockController.kt
-toruslib/torus-core/src/main/java/com/google/android/torus/core/content/ConfigurationChangeListener.kt
-toruslib/torus-core/src/main/java/com/google/android/torus/core/engine/TorusEngine.kt
-toruslib/torus-core/src/main/java/com/google/android/torus/core/engine/listener/TorusTouchListener.kt
-toruslib/torus-core/src/main/java/com/google/android/torus/core/extensions/ConfigurationExt.kt
-toruslib/torus-core/src/main/java/com/google/android/torus/core/geometry/Vertex.kt
-toruslib/torus-core/src/main/java/com/google/android/torus/core/power/FpsThrottler.kt
-toruslib/torus-core/src/main/java/com/google/android/torus/core/time/TimeController.kt
-toruslib/torus-core/src/main/java/com/google/android/torus/core/wallpaper/LiveWallpaper.kt
-toruslib/torus-core/src/main/java/com/google/android/torus/core/wallpaper/listener/LiveWallpaperEventListener.kt
-toruslib/torus-framework-canvas/src/main/java/com/google/android/torus/canvas/engine/CanvasWallpaperEngine.kt
-toruslib/torus-math/src/main/java/com/google/android/torus/math/AffineTransform.kt
-toruslib/torus-math/src/main/java/com/google/android/torus/math/MathUtils.kt
-toruslib/torus-math/src/main/java/com/google/android/torus/math/MatrixTransform.kt
-toruslib/torus-math/src/main/java/com/google/android/torus/math/RotationQuaternion.kt
-toruslib/torus-math/src/main/java/com/google/android/torus/math/SphericalTransform.kt
-toruslib/torus-math/src/main/java/com/google/android/torus/math/Vector2.kt
-toruslib/torus-math/src/main/java/com/google/android/torus/math/Vector3.kt
-toruslib/torus-utils/src/main/java/com/google/android/torus/utils/BitmapUtils.kt
-toruslib/torus-utils/src/main/java/com/google/android/torus/utils/animation/EasingUtils.kt
-toruslib/torus-utils/src/main/java/com/google/android/torus/utils/broadcast/BroadcastEventController.kt
-toruslib/torus-utils/src/main/java/com/google/android/torus/utils/broadcast/PowerSaveController.kt
-toruslib/torus-utils/src/main/java/com/google/android/torus/utils/content/ResourcesManager.kt
-toruslib/torus-utils/src/main/java/com/google/android/torus/utils/display/DisplayOrientationController.kt
-toruslib/torus-utils/src/main/java/com/google/android/torus/utils/display/DisplaySizeType.kt
-toruslib/torus-utils/src/main/java/com/google/android/torus/utils/display/DisplayUtils.kt
-toruslib/torus-utils/src/main/java/com/google/android/torus/utils/extensions/ActivityExt.kt
-toruslib/torus-utils/src/main/java/com/google/android/torus/utils/extensions/AssetManagerExt.kt
-toruslib/torus-utils/src/main/java/com/google/android/torus/utils/extensions/SizeExt.kt
-toruslib/torus-utils/src/main/java/com/google/android/torus/utils/interaction/Gyro2dController.kt
-toruslib/torus-utils/src/main/java/com/google/android/torus/utils/interaction/HingeController.kt
-toruslib/torus-utils/src/main/java/com/google/android/torus/utils/wallpaper/WallpaperUtils.kt
-toruslib/torus-wallpaper-settings/src/main/java/com/google/android/torus/settings/inlinecontrol/BaseSliceConfigProvider.kt
-toruslib/torus-wallpaper-settings/src/main/java/com/google/android/torus/settings/inlinecontrol/ColorChipsRowBuilder.kt
-toruslib/torus-wallpaper-settings/src/main/java/com/google/android/torus/settings/inlinecontrol/InputRangeRowBuilder.kt
-toruslib/torus-wallpaper-settings/src/main/java/com/google/android/torus/settings/inlinecontrol/SingleSelectionRowConfigProvider.kt
-toruslib/torus-wallpaper-settings/src/main/java/com/google/android/torus/settings/inlinecontrol/SliceConfigController.kt
-toruslib/torus-wallpaper-settings/src/main/java/com/google/android/torus/settings/storage/CustomizedSharedPreferences.kt
-viewcapturelib/src/com/android/app/viewcapture/NoOpViewCapture.kt
-viewcapturelib/src/com/android/app/viewcapture/SettingsAwareViewCapture.kt
-viewcapturelib/src/com/android/app/viewcapture/SimpleViewCapture.kt
-viewcapturelib/tests/com/android/app/viewcapture/SettingsAwareViewCaptureTest.kt
-viewcapturelib/tests/com/android/app/viewcapture/TestActivity.kt
-viewcapturelib/tests/com/android/app/viewcapture/ViewCaptureTest.kt
-weathereffects/debug/src/com/google/android/wallpaper/weathereffects/WallpaperEffectsDebugActivity.kt
-weathereffects/debug/src/com/google/android/wallpaper/weathereffects/WallpaperEffectsDebugApplication.kt
-weathereffects/debug/src/com/google/android/wallpaper/weathereffects/dagger/DebugApplicationComponent.kt
-weathereffects/src/com/google/android/wallpaper/weathereffects/WeatherEffect.kt
-weathereffects/src/com/google/android/wallpaper/weathereffects/WeatherEngine.kt
-weathereffects/src/com/google/android/wallpaper/weathereffects/WeatherWallpaperService.kt
-weathereffects/src/com/google/android/wallpaper/weathereffects/dagger/ApplicationComponent.kt
-weathereffects/src/com/google/android/wallpaper/weathereffects/dagger/DependencyProvider.kt
-weathereffects/src/com/google/android/wallpaper/weathereffects/dagger/Qualifiers.kt
-weathereffects/src/com/google/android/wallpaper/weathereffects/data/repository/WallpaperFileUtils.kt
-weathereffects/src/com/google/android/wallpaper/weathereffects/data/repository/WeatherEffectsRepository.kt
-weathereffects/src/com/google/android/wallpaper/weathereffects/domain/WeatherEffectsInteractor.kt
-weathereffects/src/com/google/android/wallpaper/weathereffects/fog/FogEffect.kt
-weathereffects/src/com/google/android/wallpaper/weathereffects/fog/FogEffectConfig.kt
-weathereffects/src/com/google/android/wallpaper/weathereffects/graphics/FrameBuffer.kt
-weathereffects/src/com/google/android/wallpaper/weathereffects/none/NoEffect.kt
-weathereffects/src/com/google/android/wallpaper/weathereffects/provider/WallpaperInfoContract.kt
-weathereffects/src/com/google/android/wallpaper/weathereffects/provider/WeatherEffectsContentProvider.kt
-weathereffects/src/com/google/android/wallpaper/weathereffects/rain/RainEffect.kt
-weathereffects/src/com/google/android/wallpaper/weathereffects/rain/RainEffectConfig.kt
-weathereffects/src/com/google/android/wallpaper/weathereffects/shared/model/WallpaperFileModel.kt
-weathereffects/src/com/google/android/wallpaper/weathereffects/snow/SnowEffect.kt
-weathereffects/src/com/google/android/wallpaper/weathereffects/snow/SnowEffectConfig.kt
-weathereffects/src/com/google/android/wallpaper/weathereffects/utils/GraphicsUtils.kt
-weathereffects/src/com/google/android/wallpaper/weathereffects/utils/ImageCrop.kt
-weathereffects/src/com/google/android/wallpaper/weathereffects/utils/MatrixUtils.kt
-weathereffects/tests/src/com/google/android/wallpaper/weathereffects/provider/WeatherEffectsContentProviderTest.kt
+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
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.app.tracing.coroutines

import android.os.Trace
import android.util.Log
import com.android.app.tracing.TraceUtils
import com.android.systemui.Flags.coroutineTracing
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext
import kotlin.coroutines.coroutineContext
import kotlin.random.Random
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.Job
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext

@PublishedApi internal const val DEBUG_COROUTINE_TRACING = false

/**
 * Convenience function for calling [CoroutineScope.launch] with [traceCoroutine] to enable tracing.
 *
 * @see traceCoroutine
 */
inline fun CoroutineScope.launch(
    crossinline spanName: () -> String,
    context: CoroutineContext = EmptyCoroutineContext,
    // TODO(b/306457056): DO NOT pass CoroutineStart; doing so will regress .odex size
    crossinline block: suspend CoroutineScope.() -> Unit
): Job = launch(context) { traceCoroutine(spanName) { block() } }

/**
 * Convenience function for calling [CoroutineScope.launch] with [traceCoroutine] to enable tracing.
 *
 * @see traceCoroutine
 */
inline fun CoroutineScope.launch(
    spanName: String,
    context: CoroutineContext = EmptyCoroutineContext,
    // TODO(b/306457056): DO NOT pass CoroutineStart; doing so will regress .odex size
    crossinline block: suspend CoroutineScope.() -> Unit
): Job = launch(context) { traceCoroutine(spanName) { block() } }

/**
 * Convenience function for calling [CoroutineScope.async] with [traceCoroutine] enable tracing
 *
 * @see traceCoroutine
 */
inline fun <T> CoroutineScope.async(
    crossinline spanName: () -> String,
    context: CoroutineContext = EmptyCoroutineContext,
    // TODO(b/306457056): DO NOT pass CoroutineStart; doing so will regress .odex size
    crossinline block: suspend CoroutineScope.() -> T
): Deferred<T> = async(context) { traceCoroutine(spanName) { block() } }

/**
 * Convenience function for calling [CoroutineScope.async] with [traceCoroutine] enable tracing.
 *
 * @see traceCoroutine
 */
inline fun <T> CoroutineScope.async(
    spanName: String,
    context: CoroutineContext = EmptyCoroutineContext,
    // TODO(b/306457056): DO NOT pass CoroutineStart; doing so will regress .odex size
    crossinline block: suspend CoroutineScope.() -> T
): Deferred<T> = async(context) { traceCoroutine(spanName) { block() } }

/**
 * Convenience function for calling [runBlocking] with [traceCoroutine] to enable tracing.
 *
 * @see traceCoroutine
 */
inline fun <T> runBlocking(
    crossinline spanName: () -> String,
    context: CoroutineContext,
    crossinline block: suspend () -> T
): T = runBlocking(context) { traceCoroutine(spanName) { block() } }

/**
 * Convenience function for calling [runBlocking] with [traceCoroutine] to enable tracing.
 *
 * @see traceCoroutine
 */
inline fun <T> runBlocking(
    spanName: String,
    context: CoroutineContext,
    crossinline block: suspend CoroutineScope.() -> T
): T = runBlocking(context) { traceCoroutine(spanName) { block() } }

/**
 * Convenience function for calling [withContext] with [traceCoroutine] to enable tracing.
 *
 * @see traceCoroutine
 */
suspend inline fun <T> withContext(
    spanName: String,
    context: CoroutineContext,
    crossinline block: suspend CoroutineScope.() -> T
): T = withContext(context) { traceCoroutine(spanName) { block() } }

/**
 * Convenience function for calling [withContext] with [traceCoroutine] to enable tracing.
 *
 * @see traceCoroutine
 */
suspend inline fun <T> withContext(
    crossinline spanName: () -> String,
    context: CoroutineContext,
    crossinline block: suspend CoroutineScope.() -> T
): T = withContext(context) { traceCoroutine(spanName) { block() } }

/**
 * Traces a section of work of a `suspend` [block]. The trace sections will appear on the thread
 * that is currently executing the [block] of work. If the [block] is suspended, all trace sections
 * added using this API will end until the [block] is resumed, which could happen either on this
 * thread or on another thread. If a child coroutine is started, it will inherit the trace sections
 * of its parent. The child will continue to print these trace sections whether or not the parent
 * coroutine is still running them.
 *
 * The current [CoroutineContext] must have a [TraceContextElement] for this API to work. Otherwise,
 * the trace sections will be dropped.
 *
 * For example, in the following trace, Thread #1 ran some work, suspended, then continued working
 * on Thread #2. Meanwhile, Thread #2 created a new child coroutine which inherited its trace
 * sections. Then, the original coroutine resumed on Thread #1 before ending. Meanwhile Thread #3 is
 * still printing trace sections from its parent because they were copied when it was created. There
 * is no way for the parent to communicate to the child that it marked these slices as completed.
 * While this might seem counterintuitive, it allows us to pinpoint the origin of the child
 * coroutine's work.
 *
 * ```
 * Thread #1 | [==== Slice A ====]                        [==== Slice A ====]
 *           |       [==== B ====]                        [=== B ===]
 * --------------------------------------------------------------------------------------
 * Thread #2 |                    [====== Slice A ======]
 *           |                    [========= B =========]
 *           |                        [===== C ======]
 * --------------------------------------------------------------------------------------
 * Thread #3 |                            [== Slice A ==]                [== Slice A ==]
 *           |                            [===== B =====]                [===== B =====]
 *           |                            [===== C =====]                [===== C =====]
 *           |                                                               [=== D ===]
 * ```
 *
 * @param name The name of the code section to appear in the trace
 * @see endSlice
 * @see traceCoroutine
 */
@OptIn(ExperimentalCoroutinesApi::class)
suspend inline fun <T> traceCoroutine(spanName: () -> String, block: () -> T): T {
    // For coroutine tracing to work, trace spans must be added and removed even when
    // tracing is not active (i.e. when TRACE_TAG_APP is disabled). Otherwise, when the
    // coroutine resumes when tracing is active, we won't know its name.
    val tracer = getTraceData()
    var spanString = "<none>"
    var coroutineSpanCookie = TraceData.INVALID_SPAN
    when (tracer) {
        is MissingTraceData -> logVerbose(tracer.message, spanName)
        is TraceData -> {
            spanString = spanName()
            coroutineSpanCookie = tracer.beginSpan(spanString)
        }
    }

    // For now, also trace to "AsyncTraces". This will allow us to verify the correctness
    // of the COROUTINE_TRACING feature flag.
    val asyncTraceCookie =
        if (Trace.isTagEnabled(Trace.TRACE_TAG_APP))
            Random.nextInt(TraceData.FIRST_VALID_SPAN, Int.MAX_VALUE)
        else TraceData.INVALID_SPAN
    if (asyncTraceCookie != TraceData.INVALID_SPAN) {
        Trace.asyncTraceForTrackBegin(
            Trace.TRACE_TAG_APP,
            TraceUtils.DEFAULT_TRACK_NAME,
            spanString,
            asyncTraceCookie
        )
    }
    try {
        return block()
    } finally {
        if (asyncTraceCookie != TraceData.INVALID_SPAN) {
            Trace.asyncTraceForTrackEnd(
                Trace.TRACE_TAG_APP,
                TraceUtils.DEFAULT_TRACK_NAME,
                asyncTraceCookie
            )
        }
        if (tracer is TraceData) {
            tracer.endSpan(coroutineSpanCookie)
        }
    }
}

/** @see traceCoroutine */
suspend inline fun <T> traceCoroutine(spanName: String, block: () -> T): T =
    traceCoroutine({ spanName }, block)

@PublishedApi
@OptIn(ExperimentalCoroutinesApi::class)
internal suspend fun getTraceData(): TraceStatus {
    return if (!coroutineTracing()) {
        MissingTraceData("Experimental flag COROUTINE_TRACING is off")
    } else if (coroutineContext[TraceContextElement] == null) {
        MissingTraceData("Current CoroutineContext is missing TraceContextElement")
    } else {
        threadLocalTrace.get() ?: MissingTraceData("ThreadLocal TraceData is null")
    }
}

@PublishedApi
internal inline fun logVerbose(logMessage: String, spanName: () -> String) {
    if (DEBUG_COROUTINE_TRACING && Log.isLoggable(TraceUtils.TAG, Log.VERBOSE)) {
        Log.v(TraceUtils.TAG, "$logMessage. Dropping trace section: \"${spanName()}\"")
    }
}
Loading