Skip to content

Commit 341dbd4

Browse files
Add profiling manager snippets for how-to-capture DAC docs (#568)
*Adding code snippets for how-to-capture section of ProfilingManager docs Co-authored-by: edgararriagag <[email protected]>
1 parent 33c4bc2 commit 341dbd4

File tree

4 files changed

+163
-2
lines changed

4 files changed

+163
-2
lines changed

gradle/libs.versions.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,14 @@ material3-adaptive = "1.1.0"
6262
material3-adaptive-navigation-suite = "1.3.2"
6363
media3 = "1.7.1"
6464
# @keep
65-
minSdk = "21"
65+
minSdk = "35"
6666
okHttp = "4.12.0"
6767
playServicesWearable = "19.0.0"
6868
protolayout = "1.3.0"
6969
recyclerview = "1.4.0"
70-
targetSdk = "34"
70+
targetSdk = "35"
7171
tiles = "1.5.0"
72+
tracing = "1.3.0"
7273
validatorPush = "1.0.0-alpha03"
7374
version-catalog-update = "1.0.0"
7475
wear = "1.3.0"
@@ -152,6 +153,7 @@ androidx-tiles-renderer = { module = "androidx.wear.tiles:tiles-renderer", versi
152153
androidx-tiles-testing = { module = "androidx.wear.tiles:tiles-testing", version.ref = "tiles" }
153154
androidx-tiles-tooling = { module = "androidx.wear.tiles:tiles-tooling", version.ref = "tiles" }
154155
androidx-tiles-tooling-preview = { module = "androidx.wear.tiles:tiles-tooling-preview", version.ref = "tiles" }
156+
androidx-tracing = { group = "androidx.tracing", name = "tracing", version.ref = "tracing" }
155157
androidx-wear = { module = "androidx.wear:wear", version.ref = "wear" }
156158
androidx-wear-ongoing = { module = "androidx.wear:wear-ongoing", version.ref = "wearOngoing" }
157159
androidx-wear-tooling-preview = { module = "androidx.wear:wear-tooling-preview", version.ref = "wearToolingPreview" }

misc/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ dependencies {
5757
implementation(libs.androidx.compose.ui.util)
5858
implementation(libs.androidx.compose.ui.tooling.preview)
5959
implementation(libs.androidx.compose.material3)
60+
implementation(libs.androidx.tracing)
6061

6162
implementation(libs.hilt.android)
6263
implementation(libs.androidx.hilt.navigation.compose)
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.example.snippets.profiling;
2+
3+
import android.app.Activity;
4+
import android.os.Bundle;
5+
import android.util.Log;
6+
import java.util.function.Consumer;
7+
import java.util.concurrent.Executor;
8+
import android.os.ProfilingResult;
9+
import java.util.concurrent.Executors;
10+
import android.os.CancellationSignal;
11+
import androidx.tracing.Trace;
12+
import androidx.core.os.Profiling;
13+
import androidx.core.os.SystemTraceRequestBuilder;
14+
import androidx.core.os.BufferFillPolicy;
15+
16+
public class ProfilingManagerJavaSnippets {
17+
public class MainActivityJava extends Activity {
18+
19+
@Override
20+
public void onCreate(Bundle savedInstanceState) {
21+
super.onCreate(savedInstanceState);
22+
sampleRecordSystemTrace();
23+
}
24+
25+
// [START android_profiling_manager_record_system_trace_java]
26+
void heavyOperation() {
27+
// Computations you want to profile
28+
}
29+
30+
void sampleRecordSystemTrace() {
31+
Executor mainExecutor = Executors.newSingleThreadExecutor();
32+
Consumer<ProfilingResult> resultCallback =
33+
new Consumer<ProfilingResult>() {
34+
@Override
35+
public void accept(ProfilingResult profilingResult) {
36+
if (profilingResult.getErrorCode() == ProfilingResult.ERROR_NONE) {
37+
Log.d(
38+
"ProfileTest",
39+
"Received profiling result file=" + profilingResult.getResultFilePath());
40+
} else {
41+
Log.e(
42+
"ProfileTest",
43+
"Profiling failed errorcode="
44+
45+
+ profilingResult.getErrorCode()
46+
+ " errormsg="
47+
+ profilingResult.getErrorMessage());
48+
}
49+
}
50+
};
51+
CancellationSignal stopSignal = new CancellationSignal();
52+
53+
SystemTraceRequestBuilder requestBuilder = new SystemTraceRequestBuilder();
54+
requestBuilder.setCancellationSignal(stopSignal);
55+
requestBuilder.setTag("FOO");
56+
requestBuilder.setDurationMs(60000);
57+
requestBuilder.setBufferFillPolicy(BufferFillPolicy.RING_BUFFER);
58+
requestBuilder.setBufferSizeKb(20971520);
59+
Profiling.requestProfiling(getApplicationContext(), requestBuilder.build(), mainExecutor,
60+
resultCallback);
61+
62+
// Wait some time for profiling to start.
63+
64+
Trace.beginSection("MyApp:HeavyOperation");
65+
heavyOperation();
66+
Trace.endSection();
67+
68+
// Once the interesting code section is profiled, stop profile
69+
stopSignal.cancel();
70+
}
71+
// [END android_profiling_manager_record_system_trace_java]
72+
}
73+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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

Comments
 (0)