Perfetto 抓取Android设备启动trace

从Android 13开始, perfetto能够被配置成设备启动时自动抓取,这对于分析设备启动性能非常有帮助。

操作步骤

  1. 创建配置文件
# One buffer allocated within the central tracing binary for the entire trace,
# shared by the two data sources below.
buffers {
  size_kb: 32768
  fill_policy: DISCARD
}

# Ftrace data from the kernel, mainly the process scheduling events.
data_sources {
  config {
    name: "linux.ftrace"
    target_buffer: 0
    ftrace_config {
      ftrace_events: "sched_switch"
      ftrace_events: "sched_waking"
      ftrace_events: "sched_wakeup_new"

      ftrace_events: "task_newtask"
      ftrace_events: "task_rename"

      ftrace_events: "sched_process_exec"
      ftrace_events: "sched_process_exit"
      ftrace_events: "sched_process_fork"
      ftrace_events: "sched_process_free"
      ftrace_events: "sched_process_hang"
      ftrace_events: "sched_process_wait"
    }
  }
}

# Resolve process commandlines and parent/child relationships, to better
# interpret the ftrace events, which are in terms of pids.
data_sources {
  config {
    name: "linux.process_stats"
    target_buffer: 0
  }
}

# 10s trace, but can be stopped prematurely via `adb shell pkill -u perfetto`.
duration_ms: 10000
  1. 将配置文件push到 /data/misc/perfetto-configs/boottrace.pbtxt
adb push <yourfile> /data/misc/perfetto-configs/boottrace.pbtxt
  1. 使能 perfetto_trace_on_boot 服务
adb shell setprop persist.debug.perfetto.boottrace 1

这个property在设备启动时会被reset,因此这里设置是为下一次设备启动生效的。

  1. 重启设备

  2. perfetto配置的抓取时间到后,trace被放在 /data/misc/perfetto-traces/boottrace.perfetto-trace

adb pull /data/misc/perfetto-traces/boottrace.perfetto-trace