इस पेज पर, ProfilingManager एपीआई का इस्तेमाल करके सिस्टम ट्रेस रिकॉर्ड करने का तरीका बताया गया है.
ProfilingManager अन्य तरह की प्रोफ़ाइलों को भी रिकॉर्ड कर सकता है. यह प्रोसेस, सिस्टम ट्रेस रिकॉर्ड करने जैसी ही होती है. हालांकि, हर टाइप के लिए अलग-अलग बिल्डर का इस्तेमाल किया जाता है. इनके साथ काम करने वाली प्रोफ़ाइलें और बिल्डर ये हैं:
सिस्टम ट्रेस: इन्हें
SystemTraceRequestBuilderका इस्तेमाल करके रिकॉर्ड किया जाता है. ये लेटेन्सी का विश्लेषण करने और परफ़ॉर्मेंस से जुड़ी सामान्य गड़बड़ियों को ठीक करने के लिए काम आते हैं.हीप डंप: इन्हें
JavaHeapDumpRequestBuilderका इस्तेमाल करके रिकॉर्ड किया जाता है. ये मेमोरी लीक का पता लगाने और उसे ऑप्टिमाइज़ करने में मददगार होते हैं.हीप प्रोफ़ाइलें: इन्हें
HeapProfileRequestBuilderका इस्तेमाल करके रिकॉर्ड किया जाता है. ये मेमोरी को ऑप्टिमाइज़ करने के लिए काम की होती हैं.कॉल स्टैक प्रोफ़ाइलें: इन्हें
StackSamplingRequestBuilderका इस्तेमाल करके रिकॉर्ड किया जाता है. ये कोड एक्ज़ीक्यूशन और लेटेन्सी विश्लेषण को समझने के लिए काम की होती हैं.
डिपेंडेंसी जोड़ें
ProfilingManager एपीआई का बेहतर तरीके से इस्तेमाल करने के लिए, अपनी build.gradle.kts फ़ाइल में ये Jetpack लाइब्रेरी जोड़ें.
Kotlin
dependencies { implementation("androidx.tracing:tracing-ktx:2.0.2") implementation("androidx.core:core:1.19.0") }
Groovy
dependencies { implementation 'androidx.tracing:tracing:2.0.2' implementation 'androidx.core:core:1.19.0' }
सिस्टम ट्रेस रिकॉर्ड करना
ज़रूरी डिपेंडेंसी जोड़ने के बाद, सिस्टम ट्रेस रिकॉर्ड करने के लिए इस कोड का इस्तेमाल करें. इस उदाहरण में, मुख्य थ्रेड से बाहर लंबी प्रोसेस को सुरक्षित तरीके से मैनेज करते हुए, कंपोज़ेबल से प्रोफ़ाइलिंग सेशन शुरू करने का तरीका बताया गया है.
Kotlin
@RequiresApi(Build.VERSION_CODES.VANILLA_ICE_CREAM)
@Composable
fun ProfiledScreen(modifier: Modifier = Modifier) {
// Use the application context: requestProfiling resolves the ProfilingManager
// system service from it, so there's no reason to hand it a short-lived Activity.
val appContext = LocalContext.current.applicationContext
val scope = rememberCoroutineScope()
Button(
onClick = {
// Run the orchestration off the main thread. Profiling a heavy operation
// on the UI thread would freeze the UI (ANR) and distort the very metrics
// you're trying to capture.
//
// Note: this scope is tied to composition. If the user leaves this screen
// mid-session, the coroutine is cancelled and stopSignal.cancel() might not
// run, but setDurationMs() acts as a safety net and ends the trace.
scope.launch(Dispatchers.Default) {
val callbackExecutor = Dispatchers.IO.asExecutor()
val resultCallback = Consumer<ProfilingResult> { profilingResult ->
if (profilingResult.errorCode == ProfilingResult.ERROR_NONE) {
Log.d("ProfileTest", "Result file: ${profilingResult.resultFilePath}")
} else {
// errorMessage explains the failure (e.g., rate limiting); keep it.
Log.e(
"ProfileTest",
"Profiling failed errorCode=${profilingResult.errorCode} " +
"errorMessage=${profilingResult.errorMessage}"
)
}
}
val stopSignal = CancellationSignal()
val requestBuilder = SystemTraceRequestBuilder().apply {
setCancellationSignal(stopSignal)
setTag("FOO") // Caller-supplied tag for identification.
setDurationMs(60000) // Hard cap: ends the session if cancel() never fires.
setBufferFillPolicy(BufferFillPolicy.RING_BUFFER)
setBufferSizeKb(32768)
}
// 1. Start the session. This is asynchronous system IPC. The tracing
// engine takes a moment to start and allocate buffers.
requestProfiling(appContext, requestBuilder.build(), callbackExecutor, resultCallback)
// 2. The API exposes no "profiling started" signal, so pad with a short,
// best-effort delay before running the code you care about. This is
// approximate. Increase it on slower or heavily loaded devices.
delay(STARTUP_PADDING_MS)
// 3. The session is already recording every thread in your app. This slice
// doesn't scope what's captured. It just labels this region of the
// timeline so heavyOperation() is easier to find. trace { } closes the
// section even if the block throws.
trace("MyApp:HeavyOperation") {
heavyOperation()
}
// 4. Stop recording. Until this fires or the setDurationMs() cap is
// reached (whichever comes first), the session keeps capturing app-wide
// activity.
stopSignal.cancel()
}
}
) {
Text("Run & Profile Heavy Operation")
}
}
// Best-effort wait for the system trace engine to initialize before profiling.
// There is no deterministic start callback; tune this for your target devices.
private const val STARTUP_PADDING_MS = 100L
fun heavyOperation() {
// Background computations to profile.
}
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());
setupProfileUploadWorker(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(32768);
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();
}
यह सैंपल कोड, प्रोफ़ाइलिंग सेशन को सेट अप और मैनेज करता है. इसके लिए, यह तरीका अपनाता है:
एक्ज़ीक्यूटर सेट अप करें. प्रोफ़ाइलिंग के नतीजे पाने वाले थ्रेड को तय करने के लिए,
Executorबनाएं. प्रोफ़ाइलिंग बैकग्राउंड में होती है. अगर आपको बाद में कॉलबैक में ज़्यादा प्रोसेसिंग जोड़नी है, तो यूज़र इंटरफ़ेस (यूआई) थ्रेड एक्ज़ीक्यूटर के बजाय किसी दूसरे थ्रेड एक्ज़ीक्यूटर का इस्तेमाल करें. इससे ऐप्लिकेशन काम नहीं कर रहा है (एएनआर) वाली गड़बड़ियों को रोकने में मदद मिलती है.प्रोफ़ाइलिंग के नतीजों को मैनेज करना.
Consumer<ProfilingResult>ऑब्जेक्ट बनाएं. सिस्टम इस ऑब्जेक्ट का इस्तेमाल करके,ProfilingManagerसे प्रोफ़ाइलिंग के नतीजे आपके ऐप्लिकेशन को भेजता है.प्रोफ़ाइलिंग का अनुरोध तैयार करें. प्रोफ़ाइलिंग सेशन सेट अप करने के लिए,
SystemTraceRequestBuilderबनाएं. इस बिल्डर की मदद से,ProfilingManagerकी ट्रेस सेटिंग को अपनी पसंद के मुताबिक बनाया जा सकता है. बिल्डर को पसंद के मुताबिक बनाना ज़रूरी नहीं है. अगर ऐसा नहीं किया जाता है, तो सिस्टम डिफ़ॉल्ट सेटिंग का इस्तेमाल करता है.- टैग तय करें. ट्रेस के नाम में टैग जोड़ने के लिए,
setTag()का इस्तेमाल करें. इस टैग से, आपको ट्रेस की पहचान करने में मदद मिलती है. - ज़रूरी नहीं: अवधि सेट करें.
setDurationMs()का इस्तेमाल करके, यह तय करें कि प्रोफ़ाइलिंग कितने समय तक करनी है. यह समय मिलीसेकंड में होना चाहिए. उदाहरण के लिए,6000060 सेकंड का ट्रेस सेट करता है. अगर तय की गई अवधि से पहलेCancellationSignalट्रिगर नहीं होता है, तो ट्रेस अपने-आप खत्म हो जाता है. - बफ़रिंग से जुड़ी कोई नीति चुनें.
setBufferFillPolicy()का इस्तेमाल करके यह तय करें कि ट्रेस डेटा को कैसे सेव किया जाए.BufferFillPolicy.RING_BUFFERका मतलब है कि जब बफ़र पूरा हो जाता है, तो नया डेटा सबसे पुराने डेटा को बदल देता है. इससे हाल की गतिविधि का लगातार रिकॉर्ड बना रहता है. - बफ़र का साइज़ सेट करें.
setBufferSizeKb()का इस्तेमाल करके, ट्रेसिंग के लिए बफ़र साइज़ तय करें. इसका इस्तेमाल करके, आउटपुट ट्रेस फ़ाइल के साइज़ को कंट्रोल किया जा सकता है.
- टैग तय करें. ट्रेस के नाम में टैग जोड़ने के लिए,
ज़रूरी नहीं: सेशन के लाइफ़साइकल को मैनेज करें.
CancellationSignalबनाएं. इस ऑब्जेक्ट की मदद से, प्रोफ़ाइलिंग सेशन को कभी भी रोका जा सकता है. इससे आपको सेशन की अवधि को सटीक तरीके से कंट्रोल करने का विकल्प मिलता है.शुरू करें और नतीजे पाएं.
requestProfiling()पर कॉल करने पर,ProfilingManagerबैकग्राउंड में प्रोफ़ाइलिंग सेशन शुरू करता है. प्रोफ़ाइलिंग हो जाने के बाद, यह आपकेresultCallback#acceptतरीके कोProfilingResultभेजता है. प्रोफ़ाइलिंग पूरी होने के बाद,ProfilingResultआपके डिवाइस पर उस पाथ की जानकारी देता है जहां ट्रेस कोProfilingResult#getResultFilePathके ज़रिए सेव किया गया था. इस फ़ाइल को प्रोग्राम के ज़रिए या लोकल प्रोफ़ाइलिंग के लिए, अपने कंप्यूटर परadb pull <trace_path>चलाकर हासिल किया जा सकता है.कस्टम ट्रेस पॉइंट जोड़ें. अपने ऐप्लिकेशन के कोड में, कस्टम ट्रेस पॉइंट जोड़े जा सकते हैं. कोड के पिछले उदाहरण में,
trace("MyApp:HeavyOperation") { ... }ब्लॉक जनरेट की गई प्रोफ़ाइल में कस्टम स्लाइस बनाता है.