Android perfetto的使用

4,695 阅读2分钟

perfetto 是一种用于从Android系统上获取性能跟踪数据的工具。是Android下一代全新的统一的trace收集和分析框架,可以抓取平台和app的trace信息。

1、perfetto的使用帮助
console:/ # perfetto -h
[344.726] perfetto_cmd.cc:187
Usage: perfetto
  --background     -d      : Exits immediately and continues tracing in
                             background
  --config         -c      : /path/to/trace/config/file or - for stdin
  --out            -o      : /path/to/out/trace/file or - for stdout
  --upload                 : Upload field trace (Android only)
  --dropbox        TAG     : DEPRECATED: Use --upload instead
                             TAG should always be set to 'perfetto'
  --no-guardrails          : Ignore guardrails triggered when using --dropbox
                             (for testing).
  --txt                    : Parse config as pbtxt. Not for production use.
                             Not a stable API.
  --reset-guardrails       : Resets the state of the guardails and exits
                             (for testing).
  --query                  : Queries the service state and prints it as
                             human-readable text.
  --query-raw              : Like --query, but prints raw proto-encoded bytes
                             of tracing_service_state.proto.
  --help           -h


light configuration flags: (only when NOT using -c/--config)
  --time           -t      : Trace duration N[s,m,h] (default: 10s)
  --buffer         -b      : Ring buffer size N[mb,gb] (default: 32mb)
  --size           -s      : Max file size N[mb,gb] (default: in-memory ring-buffer only)
  ATRACE_CAT               : Record ATRACE_CAT (e.g. wm)
  FTRACE_GROUP/FTRACE_NAME : Record ftrace event (e.g. sched/sched_switch)
  FTRACE_GROUP/*           : Record all events in group (e.g. sched/*)


statsd-specific flags:
  --alert-id           : ID of the alert that triggered this trace.
  --config-id          : ID of the triggering config.
  --config-uid         : UID of app which registered the config.
  --subscription-id    : ID of the subscription that triggered this trace.

Detach mode. DISCOURAGED, read https://docs.perfetto.dev/#/detached-mode :
  --detach=key          : Detach from the tracing session with the given key.
  --attach=key [--stop] : Re-attach to the session (optionally stop tracing once reattached).
  --is_detached=key     : Check if the session can be re-attached (0:Yes, 2:No, 1:Error).
2、生成配置文件

打开ui.perfetto.dev/, 点击Record new trace,选择Recording settings、CPU、GPU、Powe、Memery等等的配置,然后在Recording command拷贝生成的配置,如下红框部分,保存到文件perfetto_config中。

1642246997.png

3、抓取信息

将perfetto_config拷贝到目标板卡,使用命令perfetto --txt -c perfetto_config -o perfetto_out(如果有报错,不识别的配置可删掉,如下symbolize_ksyms: true),会生成一个抓取的文件perfetto_out,从目标板卡拷贝到PC电脑上。

1642247033.png

4、数据解析

打开ui.perfetto.dev/, 点击Open trace file导入生成的perfetto_out文件,就可以生成如下的可视化分析图形。

1642247116.png

5、参考文档