1 /*
<lambda>null2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.example.tracing.demo.experiments
17 
18 import android.os.HandlerThread
19 import android.os.Trace
20 import com.android.app.tracing.coroutines.traceCoroutine
21 import kotlin.coroutines.resume
22 import kotlin.random.Random
23 import kotlinx.coroutines.delay
24 import kotlinx.coroutines.flow.flow
25 import kotlinx.coroutines.suspendCancellableCoroutine
26 
27 fun coldCounterFlow(name: String, maxCount: Int = Int.MAX_VALUE) = flow {
28     for (n in 0..maxCount) {
29         emit(n)
30         forceSuspend("coldCounterFlow:$name:$n", 25)
31     }
32 }
33 
<lambda>null34 private val delayHandler by lazy { startThreadWithLooper("Thread:forceSuspend").threadHandler }
35 
36 /** Like [delay], but naively implemented so that it always suspends. */
forceSuspendnull37 suspend fun forceSuspend(traceName: String, timeMillis: Long) {
38     val traceMessage = "forceSuspend:$traceName"
39     return traceCoroutine(traceMessage) {
40         val cookie = Random.nextInt()
41         suspendCancellableCoroutine { continuation ->
42             Trace.asyncTraceForTrackBegin(Trace.TRACE_TAG_APP, TRACK_NAME, traceMessage, cookie)
43             Trace.instant(Trace.TRACE_TAG_APP, "will resume in ${timeMillis}ms")
44             continuation.invokeOnCancellation { cause ->
45                 Trace.instant(
46                     Trace.TRACE_TAG_APP,
47                     "forceSuspend:$traceName, cancelled due to ${cause?.javaClass}",
48                 )
49                 Trace.asyncTraceForTrackEnd(Trace.TRACE_TAG_APP, TRACK_NAME, cookie)
50             }
51             delayHandler.postDelayed(
52                 {
53                     Trace.asyncTraceForTrackEnd(Trace.TRACE_TAG_APP, TRACK_NAME, cookie)
54                     Trace.traceBegin(Trace.TRACE_TAG_APP, "resume")
55                     try {
56                         continuation.resume(Unit)
57                     } finally {
58                         Trace.traceEnd(Trace.TRACE_TAG_APP)
59                     }
60                 },
61                 timeMillis,
62             )
63         }
64     }
65 }
66 
startThreadWithLoopernull67 fun startThreadWithLooper(name: String): HandlerThread {
68     val thread = HandlerThread(name)
69     thread.start()
70     val looper = thread.looper
71     looper.setTraceTag(Trace.TRACE_TAG_APP)
72     return thread
73 }
74 
75 const val TRACK_NAME = "Async events"
76