(七) 上篇 Android 性能优化 Perfetto 文件生成

献赋今未售,读书凡几秋。

什么是 Perfetto ?

 Perfetto 是 Android 10 中引入的全新平台级跟踪工具。这是适用于 Android、Linux 和 Chrome 的更加通用和复杂的开源跟踪项目。与 Systrace 不同,它提供数据源超集,可让您以 protobuf 编码的二进制流形式记录任意长度的跟踪记录。您可以在 Perfetto 界面中打开这些跟踪记录。

Perfetto 和 Systrace 的关系?

  • 在 Perfetto 界面中打开 Perfetto 文件和 Systrace 文件。在 Perfetto 界面中使用旧版 Systrace 查看器打开 Systrace 文件(使用 Open with legacy UI 链接)。
  • 使用 traceconv 工具将 Perfetto 跟踪记录转换为旧版 Systrace 文本格式。
  • Systrace 和 Perfetto 不会收集有关应用进程中代码执行情况的详细信息。如需详细了解您的应用正在执行哪些方法及其占用了多少 CPU 资源,请使用CPU Profiler

Perfetto 收集性能数据来源有哪些?

  • 使用 ftrace 收集内核信息
  • 使用 atrace 收集服务和应用中的用户空间注释
  • 使用 heapprofd 收集服务和应用的本地内存使用情况信息

实操体验

(1)配置选项 – 数据源选择

perfetto 包含以下两种模式,可确定用于记录跟踪数据的数据源:

  • 轻量模式:只能选择一部分数据源,具体来说就是 atrace 和 ftrace。但此模式可提供类似于 systrace的接口。
  • 普通模式:从协议缓冲区获取其配置,并且可以让您使用 atrace 和 ftrace 之外的数据源,从而更加充分地利用 perfetto 的功能。

(2)轻量模式

在轻量模式下使用 perfetto 的一般语法如下:

//轻量模式语法:
adb shell perfetto [ --time <记录时间> ] [ --buffer <缓冲区空间大小> ] [ --size <文件大小上限> ]
                 [ <atrace类别>| <ftrace事件> | <记录群组中的所有事件> ]...

//如何你是小白,不用关注那些繁琐的参数,看一个简单实操栗子:
//(1)--time 5s <记录5秒钟>
//(2)--out /data/misc/perfetto-traces/trace <录制文件输出到手机指定位置,然后adb pull出来即可>
adb shell perfetto --time 5s --out /data/misc/perfetto-traces/trace 

//adb pull出来到当前路径.
adb pull /data/misc/perfetto-traces/trace .

//如果不你不是小白,请移步到文章结尾,去官网查看
复制代码

(3)普通模式

在普通模式下使用 perfetto 的一般语法如下:

/普通模式语法:
adb shell perfetto [ --txt <pbtxt标志(可忽略)> ] --config CONFIG_FILE <指定配置文件的路径,一般使用默认test配置即可>
//举个栗子
adb shell perfetto --config :test --out /data/misc/perfetto-traces/trace 
复制代码

(4)PerfettoUI 跟踪文件

PerfettoUI打开跟踪文件预览

 怎么样是不是画风清爽,功能多样,激动不已,亲测好用,极力推荐。

(5)会查字典

 在操作过程中,肯定会有操作失误的时候,这个时候,切莫着急,打开新华字典 (adb shell perfetto -h),然后对比错误提示,就可以解决大部分的问题了。

E:\>adb shell perfetto -h
perfetto_cmd.cc:205
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
  --dropbox           TAG  : Upload trace into DropBox using tag TAG
  --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).

E:\>
复制代码


现在动手练习起来吧……

小编的扩展链接

参考链接

远处蔚蓝天空下,涌动着金色的麦浪


 ❤ 比心 ❤

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享