시스템 추적은 프로세스에 관한 정보를 시스템 수준에서만 보여주므로, 시스템 이벤트와 관련하여 특정 시간에 앱이나 게임의 어느 메서드가 실행 중이었는지 인식하기 어려울 때가 있습니다.
Jetpack은 코드의 특정 섹션에 라벨을 지정하는 데 사용할 수 있는 추적 API를 제공합니다. 이 정보는 이후 기기에서 캡처된 트레이스에 보고됩니다. Macrobenchmark는 맞춤 트레이스 포인트로 트레이스를 자동으로 캡처합니다.
systrace 명령줄 도구를 사용하여 트레이스를 캡처하는 경우 -a
옵션이 필요합니다. 이 옵션이 없으면 앱의 메서드가 시스템 트레이스 보고서에 표시되지 않습니다.
Kotlin
class MyAdapter : RecyclerView.Adapter<MyViewHolder>() { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder { trace("MyAdapter.onCreateViewHolder") { MyViewHolder.newInstance(parent) } } override fun onBindViewHolder(holder: MyViewHolder, position: Int) { trace("MyAdapter.onBindViewHolder") { trace("MyAdapter.queryDatabase") val rowItem = queryDatabase(position) dataset.add(rowItem) } holder.bind(dataset[position]) } } }
자바
Trace
클래스를 사용하세요.
참고: beginSection()
을 여러 번 호출하는 경우 endSection()
을 호출할 때 최근에 호출된 beginSection()
메서드만 종료됩니다. 따라서 중첩 호출의 경우 다음 스니펫과 같이 각 beginSection()
호출이 endSection()
호출과 올바르게 일치하는지 확인하세요.
또한 하나의 스레드에서 beginSection()
을 호출하고 다른 스레드에서 종료할 수 없습니다. 두 메서드를 모두 동일한 스레드에서 호출해야 합니다.
public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> { @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { Trace.beginSection("MyAdapter.onCreateViewHolder"); MyViewHolder myViewHolder; try { myViewHolder = MyViewHolder.newInstance(parent); } finally { // In try and catch statements, always call "endSection()" in a // "finally" block. That way, the method is invoked even when an // exception occurs. Trace.endSection(); } return myViewHolder; } @Override public void onBindViewHolder(MyViewHolder holder, int position) { Trace.beginSection("MyAdapter.onBindViewHolder"); try { try { Trace.beginSection("MyAdapter.queryDatabase"); RowItem rowItem = queryDatabase(position); dataset.add(rowItem); } finally { Trace.endSection(); } holder.bind(dataset.get(position)); } finally { Trace.endSection(); } } }
맞춤 트레이스 이벤트를 위한 NDK API도 있습니다. 네이티브 코드에 이 API를 사용하는 방법에 관한 자세한 내용은 네이티브 코드의 맞춤 트레이스 이벤트 문서를 참고하세요.