이 페이지에서는 ProfilingManager
API를 사용하여 시스템 트레이스를 기록하는 방법을 보여줍니다.
종속 항목 추가
ProfilingManager
API를 최대한 활용하려면 build.gradle.kts
파일에 다음 Jetpack 라이브러리를 추가하세요.
Kotlin
dependencies { implementation("androidx.tracing:tracing:1.3.0") implementation("androidx.core:core:1.16.0") }
Groovy
dependencies { implementation 'androidx.tracing:tracing:1.3.0' implementation 'androidx.core:core:1.16.0' }
시스템 트레이스 기록
필요한 종속 항목을 추가한 후 다음 코드를 사용하여 시스템 트레이스를 기록합니다. 이 예에서는 Activity
내에서 프로파일링 세션을 시작하고 관리하는 기본 설정을 보여줍니다.
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 }
자바
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(); }
샘플 코드는 다음 단계를 거쳐 프로파일링 세션을 설정하고 관리합니다.
실행기 설정 프로파일링 결과를 수신할 스레드를 정의하는
Executor
를 만듭니다. 프로파일링은 백그라운드에서 이루어집니다. UI가 아닌 스레드 실행기를 사용하면 나중에 콜백에 처리를 추가할 경우 애플리케이션 응답 없음 (ANR) 오류를 방지할 수 있습니다.프로파일링 결과 처리
Consumer<ProfilingResult>
객체를 만듭니다. 시스템은 이 객체를 사용하여ProfilingManager
의 프로파일링 결과를 앱에 다시 전송합니다.프로파일링 요청을 빌드합니다.
SystemTraceRequestBuilder
를 만들어 프로파일링 세션을 설정합니다. 이 빌더를 사용하면ProfilingManager
추적 설정을 맞춤설정할 수 있습니다. 빌더 맞춤설정은 선택사항입니다. 맞춤설정하지 않으면 시스템에서 기본 설정을 사용합니다.- 태그를 정의합니다.
setTag()
을 사용하여 추적 이름에 태그를 추가합니다. 이 태그는 트레이스를 식별하는 데 도움이 됩니다. - 선택사항: 기간 설정
setDurationMs()
를 사용하여 프로파일링할 시간을 밀리초 단위로 지정합니다. 예를 들어60000
는 60초 추적을 설정합니다. 지정된 기간 전에CancellationSignal
가 트리거되지 않으면 추적이 자동으로 종료됩니다. - 버퍼 정책을 선택합니다.
setBufferFillPolicy()
를 사용하여 추적 데이터가 저장되는 방식을 정의합니다.BufferFillPolicy.RING_BUFFER
는 버퍼가 가득 차면 새 데이터가 가장 오래된 데이터를 덮어쓰므로 최근 활동의 연속 기록이 유지된다는 의미입니다. - 버퍼 크기를 설정합니다.
setBufferSizeKb()
를 사용하여 추적의 버퍼 크기를 지정합니다. 이를 사용하여 출력 추적 파일의 크기를 제어할 수 있습니다.
- 태그를 정의합니다.
선택사항: 세션 수명 주기 관리
CancellationSignal
를 만듭니다. 이 객체를 사용하면 원하는 때 언제든지 프로파일링 세션을 중지하여 길이를 정확하게 제어할 수 있습니다.시작하고 결과를 수신합니다.
requestProfiling()
를 호출하면ProfilingManager
가 백그라운드에서 프로파일링 세션을 시작합니다. 프로파일링이 완료되면ProfilingResult
가resultCallback#accept
메서드로 전송됩니다. 프로파일링이 성공적으로 완료되면ProfilingResult
는ProfilingResult#getResultFilePath
를 통해 기기에 트레이스가 저장된 경로를 제공합니다. 이 파일은 프로그래매틱 방식으로 가져오거나 로컬 프로파일링의 경우 컴퓨터에서adb pull <trace_path>
를 실행하여 가져올 수 있습니다.맞춤 추적 포인트 추가 앱 코드에 맞춤 추적 포인트를 추가할 수 있습니다. 이전 코드 예시에서는
Trace.beginSection()
및Trace.endSection()
를 사용하여MyApp:HeavyOperation
라는 트레이스 슬라이스가 추가됩니다. 이 맞춤 슬라이스는 생성된 프로필에 표시되어 앱 내의 특정 작업을 강조 표시합니다.