---
id: native-perf-tracing-systrace
title: Native Perf Tracing — Systrace / Instruments / Perfetto
category: Coding
status: draft
source_trust_level: B
verification_status: conceptual
created_at: 2026-05-09
updated_at: 2026-05-09
tags: [native, performance, systrace, perfetto, instruments, vibe-coding]
tech_stack: { language: "Swift / Kotlin", applicable_to: ["iOS", "Android"] }
applied_in: []
aliases: [Systrace, Perfetto, Instruments Time Profiler, frame timing, trace]
---

# Native Perf Tracing

> Frame drop / startup 느림 / scroll jank 분석 = trace tool. **Android: Perfetto / Systrace**. **iOS: Instruments (Time Profiler / SwiftUI / Animation Hitches)**. Custom span 으로 본인 코드도 표시.

## 📖 핵심 개념
- Frame budget: 60fps = 16.6ms / 120fps = 8.3ms.
- Jank: budget 초과 = 사용자 보임.
- Trace: 시간축 위 함수 실행 그래프.
- Custom span: 라이브러리 / 본인 함수 표시.

## 💻 코드 패턴

### Android — Perfetto (modern)
```bash
# Android Studio: Profiler → System Trace
# 또는 명령행:
adb shell perfetto --txt -c - --out /data/misc/perfetto-traces/trace -t 10s sched freq idle am wm gfx view binder_driver hal dalvik camera input res memory <<< ''
```

### Custom trace section (Android)
```kotlin
import androidx.tracing.Trace

Trace.beginSection("loadHomeFeed")
val items = repo.loadFeed()
Trace.endSection()

// 또는 inline
Trace.beginAsyncSection("download:image:42", 42)
download(url) { Trace.endAsyncSection("download:image:42", 42) }
```

### iOS — Instruments
```
Xcode → Product → Profile (⌘I) →
- Time Profiler: 시간 소요 함수
- Animation Hitches: 60fps 깬 frame
- SwiftUI: View 재계산
- Network: 요청 latency
- Allocations: 메모리
```

### iOS — os_signpost (custom span)
```swift
import os.signpost

let log = OSLog(subsystem: "com.app", category: .pointsOfInterest)
let id = OSSignpostID(log: log)

os_signpost(.begin, log: log, name: "loadFeed", signpostID: id)
let items = await loadFeed()
os_signpost(.end, log: log, name: "loadFeed", signpostID: id)
```

Instruments → os_signpost instrument 추가하면 timeline 에 보임.

### Frame timing (iOS)
```swift
let displayLink = CADisplayLink(target: self, selector: #selector(tick))
displayLink.add(to: .main, forMode: .common)

@objc func tick(_ dl: CADisplayLink) {
    let frameDuration = dl.targetTimestamp - dl.timestamp
    if frameDuration > 1.0 / 60.0 + 0.005 { print("hitch") }
}
```

### Frame timing (Android)
```kotlin
Choreographer.getInstance().postFrameCallback(object : FrameCallback {
    var last = 0L
    override fun doFrame(now: Long) {
        if (last > 0) {
            val ms = (now - last) / 1_000_000
            if (ms > 16) Log.d("frame", "jank $ms ms")
        }
        last = now
        Choreographer.getInstance().postFrameCallback(this)
    }
})
```

### App startup tracing
```kotlin
// AndroidX Startup Initializer 또는 manual
class App : Application() {
    override fun onCreate() {
        Trace.beginSection("App.onCreate")
        super.onCreate()
        // ...
        Trace.endSection()
    }
}
```

## 🤔 의사결정 기준
| 분석 대상 | 도구 |
|---|---|
| Scroll jank | Systrace/Perfetto + Choreographer (Android) / Instruments Animation Hitches (iOS) |
| Cold startup | Systrace boot to firstFrame / iOS Launch Time |
| 함수 hot path | Time Profiler (iOS) / CPU Profiler (Android) |
| 사용자 본인 함수 표시 | Trace.beginSection / os_signpost |
| 네트워크 latency | Charles / 본인 SDK observability |

## ❌ 안티패턴
- **Trace section 안 닫음**: timeline 깨짐.
- **Profile build 안 씀**: debug 는 느림 — release 또는 profile build 사용.
- **profile 상태 의존**: profiler attach 자체가 영향. 상대 비교만.
- **시작 측정 — emulator 만**: 실기와 다름.
- **로그로 시간 측정**: I/O 자체가 시간. 표시 적은 trace API.
- **첫 측정만 보기**: warm-up 필요. 5번 평균.

## 🤖 LLM 활용 힌트
- iOS = os_signpost + Instruments. Android = Trace + Perfetto.
- Frame budget 16.6ms 기준 비교.

## 🔗 관련 문서
- [[Native_Memory_Profiling]]
- [[Native_ANR_Freeze_Debugging]]
- [[Native_Battery_Network_Profiling]]
