创作不易,希望能一键三连哦~点赞👍、收藏🌟、加关注➕,随用随学不迷路~
Perfetto 是 Google 开源的一款 Android & Linux 性能追踪与分析工具。它可以帮助开发者分析 App 的性能表现,找到性能瓶颈,并进行优化。
- Perfetto 官方源码链接地址 github.com/google/perf…
- Perfetto 在线分析工具 ui.perfetto.dev/
- 也可用Android Studio自带工具Profiler进行分析(有🤏一点点卡,但方便不依赖网络)
Perfetto 的主要功能包括:
- 收集跟踪和性能数据: 它可以trace Android平台上的各种系统和应用组件,获取详细的执行信息和性能数据。
- 实时分析和查看数据: 在数据收集过程中,可以实时生成交互式的分析报告,帮助开发者快速找到性能问题。
- 保存和导出数据: 收集的数据可以保存到本地文件或上传到 Perfetto 分析服务,方便分享和长期分析。
- 定制跟踪配置: 开发者可以自定义跟踪配置,选择自己关注的系统与应用组件进行跟踪,过滤不需要的数据。
使用perfetto主要有以下步骤:
打开perfetto
- perfetto 的命令行工具是这个配置在 Android11® 之后是默认打开的,但是如果你是 Android 9 ( P ) 或者 10 ( Q ) ,那么就需要手动设置一下相应的 prop 属性,用于收集和分析跟踪数据。
# 1-打开,0-关闭
adb shell setprop persist.traced.enable 1
配置跟踪信息
- 配置跟踪,选择要跟踪的数据源和时间范围等。配置信息写入文本文件并传给perfetto命令行工具,如:config.pbtx,可借助Perfetto 在线分析工具中的⏺️Record new trace进行手动勾选想要录制的内容,它会帮你生成配置文件命令,在Recording command,如下图所示。
- 你可以复制Recording command中生成好的指令到cmd终端进行执行,等待对应的时间即可完成录制trace。
- 你也可以将Recording command中生成好的指令,复制两个EOF标签之间的指令到config.pbtx配置文件中。并将配置文件上传到Android设备中,如:
adb push config.pbtx /data/local/tmp/config.pbtx
duration_ms: 10000
buffers: {
size_kb: 522240
fill_policy: DISCARD
}
buffers: {
size_kb: 2048
fill_policy: DISCARD
}
data_sources: {
config {
name: "android.gpu.memory"
}
}
data_sources: {
config {
name: "linux.process_stats"
target_buffer: 1
process_stats_config {
scan_all_processes_on_start: true
proc_stats_poll_ms: 1000
}
}
}
data_sources: {
config {
name: "android.log"
android_log_config {
}
}
}
data_sources: {
config {
name: "android.surfaceflinger.frametimeline"
}
}
data_sources: {
config {
name: "android.game_interventions"
}
}
data_sources: {
config {
name: "linux.sys_stats"
sys_stats_config {
meminfo_period_ms: 1000
vmstat_period_ms: 1000
}
}
}
data_sources: {
config {
name: "android.heapprofd"
target_buffer: 0
heapprofd_config {
sampling_interval_bytes: 4096
shmem_size_bytes: 8388608
block_client: true
all_heaps: true
}
}
}
data_sources: {
config {
name: "android.java_hprof"
target_buffer: 0
java_hprof_config {
}
}
}
data_sources: {
config {
name: "linux.ftrace"
ftrace_config {
ftrace_events: "sched/sched_switch"
ftrace_events: "power/suspend_resume"
ftrace_events: "sched/sched_wakeup"
ftrace_events: "sched/sched_wakeup_new"
ftrace_events: "sched/sched_waking"
ftrace_events: "power/cpu_frequency"
ftrace_events: "power/cpu_idle"
ftrace_events: "power/gpu_frequency"
ftrace_events: "gpu_mem/gpu_mem_total"
ftrace_events: "raw_syscalls/sys_enter"
ftrace_events: "raw_syscalls/sys_exit"
ftrace_events: "mm_event/mm_event_record"
ftrace_events: "kmem/rss_stat"
ftrace_events: "ion/ion_stat"
ftrace_events: "dmabuf_heap/dma_heap_stat"
ftrace_events: "kmem/ion_heap_grow"
ftrace_events: "kmem/ion_heap_shrink"
ftrace_events: "sched/sched_process_exit"
ftrace_events: "sched/sched_process_free"
ftrace_events: "task/task_newtask"
ftrace_events: "task/task_rename"
ftrace_events: "lowmemorykiller/lowmemory_kill"
ftrace_events: "oom/oom_score_adj_update"
ftrace_events: "sched/sched_blocked_reason"
ftrace_events: "ftrace/print"
atrace_apps: "lmkd"
symbolize_ksyms: true
}
}
}
运行perfetto trace
- 运行perfetto trace命令进行跟踪数据收集。
adb shell 'cat /data/local/tmp/config.pbtx | perfetto --txt -c - -o /data/misc/perfetto-traces/trace.perfetto-trace'
- 上面配置为10秒采集,等待10秒即可看到终端提示执行记录完毕
导出 .perfetto-trace 记录文件
- 接下来就是从设备将
/data/misc/perfetto-traces/trace.perfetto-trace
路径下的文件下载到电脑中,指令如下:
adb pull /data/misc/perfetto-traces/trace_file.perfetto-trace ./traces/
至此,如何从设备中录制trace文件的工作就完成了,接下来就是如何进行分析trace来有针对的进行APP的性能优化了。
创作不易,希望能一键三连哦~点赞👍、收藏🌟、加关注➕,随用随学不迷路~