序言
最近在研究性能問題需要抓取trace文件。于是寫了個腳本
使用
雙擊運行批處理文件,可以開始記錄trace。而且以當前文件夾下面的。config.pbtx 作為配置文件。
(pbtx就是一個json文件。配置了需要抓取那些參數,可以通過https://ui.perfetto.dev/中的Record new trace (左側)的菜單,在右側設置最近要記錄什么內容。來生成配置文件。可以參考
Android Perfetto 系列 2:Perfetto Trace 抓取
最后的文件在Cmdline instructions菜單中顯示。
右邊下方的內容拷貝到。當前目錄,并命名為config.pbtx
記得把配置中的時間去除,這樣可以一直記錄。
在當前文件夾下新建批處理
@echo off
chcp 65001 >nul@echo off
setlocal enabledelayedexpansion:: 獲取當前目錄
echo 當前目錄: %cd%:: 使用 PowerShell 獲取時間戳
for /f "delims=" %%a in ('powershell.exe -Command "Get-Date -Format yyyyMMdd_HHmmss"') do set timestamp=%%a
echo 時間戳: %timestamp%:: 刪除舊 trace 文件
adb shell rm -rf /data/misc/perfetto-traces/trace.perfetto
timeout /t 1 >nul:: 推送配置文件
adb push config.pbtx /data/misc/perfetto-configs/config.pbtx
timeout /t 1 >nul:: 開始錄制
echo 開始記錄 trace,請按 Ctrl+C 結束錄制...
adb shell -tt perfetto --txt -c /data/misc/perfetto-configs/config.pbtx -o /data/misc/perfetto-traces/trace.perfetto:: 導出 trace 到當前目錄
adb pull /data/misc/perfetto-traces/trace.perfetto .\trace_%timestamp%.perfettoif exist trace_%timestamp%.perfetto (echo ? 文件已成功導出到當前目錄:echo trace_%timestamp%.perfettoecho.echo 🔗 正在打開 Perfetto 官方網站,請稍后手動上傳 trace 文件。echo 1. 瀏覽器將自動打開 Perfetto UI。echo 2. 點擊左上角 ^> Open trace file^> 按鈕。echo 3. 選擇剛剛保存的 trace_%timestamp%.perfetto 文件。echo.:: 打開默認瀏覽器訪問 Perfetto UIstart "" https://ui.perfetto.dev/) else (echo ? 文件未找到,請檢查設備路徑或配置。
)pause
雙擊bat運行,要結束錄制的話,只需要按下 Ctrl+C 就可以了。
最后文件會自動保存到當前文件夾下,以時間命名。
最后還會自動打開分析網站。只需要把日志文件拖入瀏覽器就可以打開。
附帶配置
這是我的配置文件,可以記錄logcat
buffers {size_kb: 65536fill_policy: DISCARD
}
buffers {size_kb: 4096fill_policy: DISCARD
}
data_sources {config {name: "linux.ftrace"ftrace_config {ftrace_events: "sched/sched_process_exit"ftrace_events: "sched/sched_process_free"ftrace_events: "task/task_newtask"ftrace_events: "task/task_rename"ftrace_events: "binder/*"ftrace_events: "block/*"ftrace_events: "clk/*"ftrace_events: "devfreq/*"ftrace_events: "ext4/*"ftrace_events: "f2fs/*"ftrace_events: "i2c/*"ftrace_events: "irq/*"ftrace_events: "kmem/*"ftrace_events: "memory_bus/*"ftrace_events: "mmc/*"ftrace_events: "oom/*"ftrace_events: "power/*"ftrace_events: "regulator/*"ftrace_events: "sched/*"ftrace_events: "sync/*"ftrace_events: "task/*"ftrace_events: "vmscan/*"ftrace_events: "fastrpc/*"ftrace_events: "sched/sched_switch"ftrace_events: "power/suspend_resume"ftrace_events: "sched/sched_blocked_reason"ftrace_events: "sched/sched_wakeup"ftrace_events: "sched/sched_wakeup_new"ftrace_events: "sched/sched_waking"ftrace_events: "sched/sched_process_exit"ftrace_events: "sched/sched_process_free"ftrace_events: "task/task_newtask"ftrace_events: "task/task_rename"ftrace_events: "power/cpu_frequency"ftrace_events: "power/cpu_idle"ftrace_events: "power/suspend_resume"ftrace_events: "raw_syscalls/sys_enter"ftrace_events: "raw_syscalls/sys_exit"ftrace_events: "power/gpu_frequency"ftrace_events: "gpu_mem/gpu_mem_total"ftrace_events: "power/gpu_work_period"ftrace_events: "ftrace/print"atrace_categories: "adb"atrace_categories: "aidl"atrace_categories: "am"atrace_categories: "audio"atrace_categories: "binder_driver"atrace_categories: "binder_lock"atrace_categories: "bionic"atrace_categories: "camera"atrace_categories: "dalvik"atrace_categories: "database"atrace_categories: "gfx"atrace_categories: "hal"atrace_categories: "input"atrace_categories: "network"atrace_categories: "nnapi"atrace_categories: "pm"atrace_categories: "power"atrace_categories: "res"atrace_categories: "rro"atrace_categories: "rs"atrace_categories: "sm"atrace_categories: "ss"atrace_categories: "vibrator"atrace_categories: "video"atrace_categories: "view"atrace_categories: "webview"atrace_categories: "wm"symbolize_ksyms: truedisable_generic_events: true}}
}
data_sources {config {name: "linux.process_stats"process_stats_config {scan_all_processes_on_start: true}}
}
data_sources {config {name: "linux.sys_stats"sys_stats_config {meminfo_period_ms: 5000stat_period_ms: 250stat_counters: STAT_CPU_TIMESstat_counters: STAT_FORK_COUNTcpufreq_period_ms: 250}}
}
data_sources {config {name: "android.gpu.memory"}
}
data_sources {config {name: "android.java_hprof"java_hprof_config {}}
}
data_sources {config {name: "android.log"android_log_config {log_ids: LID_CRASHlog_ids: LID_DEFAULTlog_ids: LID_EVENTSlog_ids: LID_KERNELlog_ids: LID_RADIOlog_ids: LID_SECURITYlog_ids: LID_STATSlog_ids: LID_SYSTEM}}
}
data_sources {config {name: "android.surfaceflinger.frametimeline"}
}
data_sources {config {name: "android.game_interventions"}
}
data_sources {config {name: "android.network_packets"network_packet_trace_config {poll_ms: 1000}}
}
data_sources {config {name: "android.packages_list"}
}
data_sources {config {name: "android.statsd"statsd_tracing_config {push_atom_id: ATOM_PROCESS_STATE_CHANGEDpush_atom_id: ATOM_BLE_SCAN_RESULT_RECEIVEDpush_atom_id: ATOM_SENSOR_STATE_CHANGEDpush_atom_id: ATOM_GPS_SCAN_STATE_CHANGED}}
}
data_sources {config {name: "linux.perf"perf_event_config {timebase {frequency: 100timestamp_clock: PERF_CLOCK_MONOTONIC}callstack_sampling {}}}
}