Android 性能优化

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

2020-07-09  本文已影响0人  科技猿人

小酌鸡汤

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

本文来源《Android 性能优化 全家桶》

什么是Perfetto?

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

Perfetto和Systrace的关系?

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

Perfetto的 文件生成 实操开始吧~

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

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:\>

现在动手练习起来吧

小编的扩展链接

参考链接

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

举手之劳,赞有余香! ❤ 比心 ❤

上一篇 下一篇

猜你喜欢

热点阅读