Skip to content

Add profiling manager snippets for how-to-capture DAC docs #568

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jul 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,14 @@ material3-adaptive = "1.1.0"
material3-adaptive-navigation-suite = "1.3.2"
media3 = "1.7.1"
# @keep
minSdk = "21"
minSdk = "35"
okHttp = "4.12.0"
playServicesWearable = "19.0.0"
protolayout = "1.3.0"
recyclerview = "1.4.0"
targetSdk = "34"
targetSdk = "35"
tiles = "1.5.0"
tracing = "1.3.0"
validatorPush = "1.0.0-alpha03"
version-catalog-update = "1.0.0"
wear = "1.3.0"
Expand Down Expand Up @@ -152,6 +153,7 @@ androidx-tiles-renderer = { module = "androidx.wear.tiles:tiles-renderer", versi
androidx-tiles-testing = { module = "androidx.wear.tiles:tiles-testing", version.ref = "tiles" }
androidx-tiles-tooling = { module = "androidx.wear.tiles:tiles-tooling", version.ref = "tiles" }
androidx-tiles-tooling-preview = { module = "androidx.wear.tiles:tiles-tooling-preview", version.ref = "tiles" }
androidx-tracing = { group = "androidx.tracing", name = "tracing", version.ref = "tracing" }
androidx-wear = { module = "androidx.wear:wear", version.ref = "wear" }
androidx-wear-ongoing = { module = "androidx.wear:wear-ongoing", version.ref = "wearOngoing" }
androidx-wear-tooling-preview = { module = "androidx.wear:wear-tooling-preview", version.ref = "wearToolingPreview" }
Expand Down
1 change: 1 addition & 0 deletions misc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ dependencies {
implementation(libs.androidx.compose.ui.util)
implementation(libs.androidx.compose.ui.tooling.preview)
implementation(libs.androidx.compose.material3)
implementation(libs.androidx.tracing)

implementation(libs.hilt.android)
implementation(libs.androidx.hilt.navigation.compose)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.example.snippets.profiling;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import java.util.function.Consumer;
import java.util.concurrent.Executor;
import android.os.ProfilingResult;
import java.util.concurrent.Executors;
import android.os.CancellationSignal;
import androidx.tracing.Trace;
import androidx.core.os.Profiling;
import androidx.core.os.SystemTraceRequestBuilder;
import androidx.core.os.BufferFillPolicy;

public class ProfilingManagerJavaSnippets {
public class MainActivityJava extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sampleRecordSystemTrace();
}

// [START android_profiling_manager_record_system_trace_java]
void heavyOperation() {
// Computations you want to profile
}

void sampleRecordSystemTrace() {
Executor mainExecutor = Executors.newSingleThreadExecutor();
Consumer<ProfilingResult> resultCallback =
new Consumer<ProfilingResult>() {
@Override
public void accept(ProfilingResult profilingResult) {
if (profilingResult.getErrorCode() == ProfilingResult.ERROR_NONE) {
Log.d(
"ProfileTest",
"Received profiling result file=" + profilingResult.getResultFilePath());
} else {
Log.e(
"ProfileTest",
"Profiling failed errorcode="

+ profilingResult.getErrorCode()
+ " errormsg="
+ profilingResult.getErrorMessage());
}
}
};
CancellationSignal stopSignal = new CancellationSignal();

SystemTraceRequestBuilder requestBuilder = new SystemTraceRequestBuilder();
requestBuilder.setCancellationSignal(stopSignal);
requestBuilder.setTag("FOO");
requestBuilder.setDurationMs(60000);
requestBuilder.setBufferFillPolicy(BufferFillPolicy.RING_BUFFER);
requestBuilder.setBufferSizeKb(20971520);
Profiling.requestProfiling(getApplicationContext(), requestBuilder.build(), mainExecutor,
resultCallback);

// Wait some time for profiling to start.

Trace.beginSection("MyApp:HeavyOperation");
heavyOperation();
Trace.endSection();

// Once the interesting code section is profiled, stop profile
stopSignal.cancel();
}
// [END android_profiling_manager_record_system_trace_java]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright 2025 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
*
* https://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.example.snippets.profiling

import android.app.Activity
import android.os.Build
import android.os.Bundle
import android.os.CancellationSignal
import android.os.ProfilingResult
import android.util.Log
import androidx.annotation.RequiresApi
import androidx.core.os.BufferFillPolicy
import androidx.core.os.SystemTraceRequestBuilder
import androidx.core.os.requestProfiling
import androidx.tracing.Trace
import java.util.concurrent.Executor
import java.util.function.Consumer
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.asExecutor

class ProfilingManagerKotlinSnippets {
class MainActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
sampleRecordSystemTrace()
}

// [START android_profiling_manager_record_system_trace_kotlin]
@RequiresApi(Build.VERSION_CODES.VANILLA_ICE_CREAM)
fun sampleRecordSystemTrace() {
val mainExecutor: Executor =
Dispatchers.IO.asExecutor() // Your choice of executor for the callback to occur on.
val resultCallback = Consumer<ProfilingResult> { profilingResult ->
if (profilingResult.errorCode == ProfilingResult.ERROR_NONE) {
Log.d(
"ProfileTest",
"Received profiling result file=" + profilingResult.resultFilePath
)
} else {
Log.e(
"ProfileTest",
"Profiling failed errorcode=" + profilingResult.errorCode + " errormsg=" + profilingResult.errorMessage
)
}
}
val stopSignal = CancellationSignal()

val requestBuilder = SystemTraceRequestBuilder()
requestBuilder.setCancellationSignal(stopSignal)
requestBuilder.setTag("FOO") // Caller supplied tag for identification
requestBuilder.setDurationMs(60000)
requestBuilder.setBufferFillPolicy(BufferFillPolicy.RING_BUFFER)
requestBuilder.setBufferSizeKb(20971520)
requestProfiling(applicationContext, requestBuilder.build(), mainExecutor, resultCallback)

// Wait some time for profiling to start.

Trace.beginSection("MyApp:HeavyOperation")
heavyOperation()
Trace.endSection()

// Once the interesting code section is profiled, stop profile
stopSignal.cancel()
}

fun heavyOperation() {
// Computations you want to profile
}
// [END android_profiling_manager_record_system_trace_kotlin]
}
}