1 /*
2  * 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 
17 package android.tools.flicker
18 
19 import android.tools.FLICKER_TAG
20 import android.tools.Scenario
21 import android.tools.flicker.Utils.ALL_MONITORS
22 import android.tools.io.Reader
23 import android.tools.io.TraceType
24 import android.tools.traces.SERVICE_TRACE_CONFIG
25 import android.tools.traces.io.ResultReaderWithLru
26 import android.tools.traces.io.ResultWriter
27 import android.util.Log
28 import java.io.File
29 import kotlin.io.path.createTempDirectory
30 
31 class FlickerServiceTracesCollector(
32     private val outputDir: File = createTempDirectory().toFile(),
33 ) : TracesCollector {
34     private var scenario: Scenario? = null
35 
<lambda>null36     private val traceMonitors = ALL_MONITORS.filter { it.traceType != TraceType.SCREEN_RECORDING }
37 
startnull38     override fun start(scenario: Scenario) {
39         reportErrorsBlock("Failed to start traces") {
40             require(this.scenario == null) { "Trace still running" }
41             traceMonitors.forEach { it.start() }
42             this.scenario = scenario
43         }
44     }
45 
stopnull46     override fun stop(): Reader {
47         return reportErrorsBlock("Failed to stop traces") {
48             val scenario = this.scenario
49             require(scenario != null) { "Scenario not set - make sure trace was started properly" }
50 
51             Log.v(LOG_TAG, "Creating output directory for trace files")
52             outputDir.mkdirs()
53 
54             Log.v(LOG_TAG, "Stopping trace monitors")
55             val writer = ResultWriter().forScenario(scenario).withOutputDir(outputDir)
56             traceMonitors.reversed().forEach { it.stop(writer) }
57             this.scenario = null
58             val result = writer.write()
59 
60             ResultReaderWithLru(result, SERVICE_TRACE_CONFIG)
61         }
62     }
63 
cleanupnull64     override fun cleanup() {
65         if (outputDir.exists()) {
66             outputDir.deleteRecursively()
67         }
68     }
69 
reportErrorsBlocknull70     private fun <T : Any> reportErrorsBlock(msg: String, block: () -> T): T {
71         try {
72             return block()
73         } catch (e: Throwable) {
74             Log.e(LOG_TAG, msg, e)
75             throw e
76         }
77     }
78 
79     companion object {
80         private const val LOG_TAG = "$FLICKER_TAG-Collector"
81     }
82 }
83