|
| 1 | +/* |
| 2 | + * Copyright 2025 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 | + * https://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 com.example.snippets.profiling |
| 18 | + |
| 19 | +import android.app.Activity |
| 20 | +import android.os.Build |
| 21 | +import android.os.Bundle |
| 22 | +import android.os.CancellationSignal |
| 23 | +import android.os.ProfilingResult |
| 24 | +import android.util.Log |
| 25 | +import androidx.annotation.RequiresApi |
| 26 | +import androidx.core.os.BufferFillPolicy |
| 27 | +import androidx.core.os.SystemTraceRequestBuilder |
| 28 | +import androidx.core.os.requestProfiling |
| 29 | +import androidx.tracing.Trace |
| 30 | +import java.util.concurrent.Executor |
| 31 | +import java.util.function.Consumer |
| 32 | +import kotlinx.coroutines.Dispatchers |
| 33 | +import kotlinx.coroutines.asExecutor |
| 34 | + |
| 35 | +class ProfilingManagerKotlinSnippets { |
| 36 | + class MainActivity : Activity() { |
| 37 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 38 | + super.onCreate(savedInstanceState) |
| 39 | + sampleRecordSystemTrace() |
| 40 | + } |
| 41 | + |
| 42 | + // [START android_profiling_manager_record_system_trace_kotlin] |
| 43 | + @RequiresApi(Build.VERSION_CODES.VANILLA_ICE_CREAM) |
| 44 | + fun sampleRecordSystemTrace() { |
| 45 | + val mainExecutor: Executor = |
| 46 | + Dispatchers.IO.asExecutor() // Your choice of executor for the callback to occur on. |
| 47 | + val resultCallback = Consumer<ProfilingResult> { profilingResult -> |
| 48 | + if (profilingResult.errorCode == ProfilingResult.ERROR_NONE) { |
| 49 | + Log.d( |
| 50 | + "ProfileTest", |
| 51 | + "Received profiling result file=" + profilingResult.resultFilePath |
| 52 | + ) |
| 53 | + } else { |
| 54 | + Log.e( |
| 55 | + "ProfileTest", |
| 56 | + "Profiling failed errorcode=" + profilingResult.errorCode + " errormsg=" + profilingResult.errorMessage |
| 57 | + ) |
| 58 | + } |
| 59 | + } |
| 60 | + val stopSignal = CancellationSignal() |
| 61 | + |
| 62 | + val requestBuilder = SystemTraceRequestBuilder() |
| 63 | + requestBuilder.setCancellationSignal(stopSignal) |
| 64 | + requestBuilder.setTag("FOO") // Caller supplied tag for identification |
| 65 | + requestBuilder.setDurationMs(60000) |
| 66 | + requestBuilder.setBufferFillPolicy(BufferFillPolicy.RING_BUFFER) |
| 67 | + requestBuilder.setBufferSizeKb(20971520) |
| 68 | + requestProfiling(applicationContext, requestBuilder.build(), mainExecutor, resultCallback) |
| 69 | + |
| 70 | + // Wait some time for profiling to start. |
| 71 | + |
| 72 | + Trace.beginSection("MyApp:HeavyOperation") |
| 73 | + heavyOperation() |
| 74 | + Trace.endSection() |
| 75 | + |
| 76 | + // Once the interesting code section is profiled, stop profile |
| 77 | + stopSignal.cancel() |
| 78 | + } |
| 79 | + |
| 80 | + fun heavyOperation() { |
| 81 | + // Computations you want to profile |
| 82 | + } |
| 83 | + // [END android_profiling_manager_record_system_trace_kotlin] |
| 84 | + } |
| 85 | +} |
0 commit comments