(两百五十七)学习perfetto(二)——生成perfetto trace

继续学习

https://perfetto.dev/docs/quickstart/android-tracing

https://perfetto.dev/docs/concepts/config

之前看的https://ui.perfetto.dev/#!/record?p=instructions

其实就是开发者文档里的config文件

 

1.理论准备

perfetto普通模式的命令行

普通模式

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

     adb shell perfetto [ --txt ] --config CONFIG_FILE
    

下表列出了在普通模式下使用 perfetto 的可用选项:

选项说明
--config CONFIG_FILE | -c CONFIG_FILE指定配置文件的路径。在普通模式下,某些配置可能会在配置协议缓冲区中进行编码。此文件必须符合 AOSP trace_config.proto 中定义的协议缓冲区架构。

根据 AOSP data_source_config.proto 中的定义,您可以使用 TraceConfigDataSourceConfig 成员来选择和配置数据源。

--txt指示 perfetto 将配置文件解析为 pbtxt。此标记为实验性标记,不建议您在正式版中启用此标记。

这两个标记是用来解析config文件的

至于加不加--txt参考

https://perfetto.dev/docs/concepts/config

PBTX vs binary format

There are two ways to pass the trace config when using the perfetto cmdline client format:

Text format

It is the preferred format for human-driven workflows and exploration. It allows to pass directly the text file in the PBTX (ProtoBuf TeXtual representation) syntax, for the schema defined in the trace_config.proto (see reference docs)

When using this mode pass the --txt flag to perfetto to indicate the config should be interpreted as a PBTX file:

perfetto -c /path/to/config.pbtx --txt -o trace_file.pftrace

NOTE: The --txt option has been introduced only in Android 10 (Q). Older versions support only the binary format.

WARNING: Do not use the text format for machine-to-machine interaction benchmark, scripts and tools) as it's more prone to breakages (e.g. if a field is renamed or an enum is turned into an integer)

Binary format

It is the preferred format for machine-to-machine (M2M) interaction. It involves passing the protobuf-encoded binary of the TraceConfig message. This can be obtained passing the PBTX in input to the protobuf's protoc compiler (which can be downloaded here).

cd ~/code/perfetto  # external/perfetto in the Android tree.

protoc --encode=perfetto.protos.TraceConfig \
        -I. protos/perfetto/config/perfetto_config.proto \
        < config.txpb \
        > config.bin
 

and then passing it to perfetto as follows, without the --txt argument:

perfetto -c config.bin -o trace_file.pftrace

步骤参考

https://perfetto.dev/docs/quickstart/android-tracing

adb push config.txt /data/local/tmp/trace_config.txt abb shell 'perfetto --txt -c - -o /data/misc/perfetto-traces/trace < /data/local/tmp/trace_config.txt'

NOTE: because of strict SELinux rules, on versions of older than Android 11 (R) passing directly the file path as -c /data/local/tmp/config might fail, hence the -c - + stdin piping above.

Pull the file using adb pull /data/misc/perfetto-traces/trace ~/trace.pftrace and upload to the Perfetto UI.

The full reference for the perfetto cmdline interface can be found here.

至于config文件可以在UI界面选择需要trace的categories后看https://ui.perfetto.dev/#!/record?p=instructions

比如我需要加一个am的categories

然后点击instructions

可以看到instructions里多了个am

config文件截取中间的一段就可以了

buffers: {
    size_kb: 8960
    fill_policy: DISCARD
}
buffers: {
    size_kb: 1280
    fill_policy: DISCARD
}
data_sources: {
    config {
        name: "linux.ftrace"
        ftrace_config {
            ftrace_events: "ftrace/print"
            atrace_categories: "am"
        }
    }
}
duration_ms: 10000

比如我如下执行结果

config源文件

buffers: {
    size_kb: 8960
    fill_policy: DISCARD
}
buffers: {
    size_kb: 1280
    fill_policy: DISCARD
}
data_sources: {
    config {
        name: "linux.sys_stats"
        sys_stats_config {
            stat_period_ms: 1000
            stat_counters: STAT_CPU_TIMES
            stat_counters: STAT_FORK_COUNT
        }
    }
}
data_sources: {
    config {
        name: "linux.ftrace"
        ftrace_config {
            ftrace_events: "ftrace/print"
            atrace_categories: "gfx"
            atrace_categories: "view"
            atrace_categories: "wm"
            atrace_categories: "am"
            atrace_categories: "rs"
            atrace_categories: "pm"
            atrace_categories: "ss"
        }
    }
}
duration_ms: 10000

执行命令

F:\learn\systrace\bilibili>adb push perfetto_config.pbtx /data/misc/perfetto-traces/
perfetto_config.pbtx: 1 file pushed. 0.1 MB/s (784 bytes in 0.006s)

F:\learn\systrace\bilibili>adb shell perfetto --txt --config /data/misc/perfetto-traces/perfetto_config.pbtx --out /data/misc/perfetto-traces/jiatai.perfetto-trace
perfetto_cmd.cc:604      Connected to the Perfetto traced service, starting tracing for 10000 ms
perfetto_cmd.cc:677      Wrote 2475170 bytes into /data/misc/perfetto-traces/jiatai.perfetto-trace

F:\learn\systrace\bilibili>adb pull /data/misc/perfetto-traces/jiatai.perfetto-trace ./
/data/misc/perfetto-traces/jiatai.perfetto-trace: 1 file pulled. 27.4 MB/s (2475170 bytes in 0.086s)

用官方的把adb shell和子命令分开也是可以的

F:\learn\systrace\bilibili>adb shell
raphael:/ # perfetto --txt -c - -o /data/misc/perfetto-traces/trace < /data/misc/perfetto-traces/perfetto_config.pbtx
perfetto_cmd.cc:604      Connected to the Perfetto traced service, starting tracing for 10000 ms
perfetto_cmd.cc:677      Wrote 14014 bytes into /data/misc/perfetto-traces/trace
ata/misc/perfetto-traces/111.trace < /data/misc/perfetto-traces/perfetto_config.pbtx                                  <
perfetto_cmd.cc:604      Connected to the Perfetto traced service, starting tracing for 10000 ms
perfetto_cmd.cc:677      Wrote 2755679 bytes into /data/misc/perfetto-traces/111.trace
raphael:/ # exit

F:\learn\systrace\bilibili>adb pull /data/misc/perfetto-traces/111.trace ./
/data/misc/perfetto-traces/111.trace: 1 file pulled. 26.4 MB/s (2755679 bytes in 0.099s)

 

用perfetto打开

 

2.开发者选项

https://developer.android.com/topic/performance/tracing/on-device

没打得开,应该说的就是开发者选项的系统监控

https://developer.android.google.cn/studio/profile/systrace/on-device

        启用开发者选项(如果尚未启用此选项)。
        打开开发者选项设置屏幕。
        在调试部分中,选择系统跟踪。此时会打开“系统跟踪”应用,其中显示了应用菜单。

        在应用菜单中,启用显示“快捷设置”图块,如图 2 所示。系统会将系统跟踪图块图块添加到快捷设置面板中,如图 1 所示:

导出trace

    cd /path-to-traces-on-my-dev-machine && \
          adb pull /data/local/traces/ .

 

3.脚本

If you are running on a Mac or Linux host, or are using a bash-based terminal on Windows, you can use the following:

adb shell perfetto \
  -c - --txt \
  -o /data/misc/perfetto-traces/trace \
<<EOF
duration_ms: 10000

buffers: {
    size_kb: 8960
    fill_policy: DISCARD
}
buffers: {
    size_kb: 1280
    fill_policy: DISCARD
}
data_sources: {
    config {
        name: "linux.ftrace"
        ftrace_config {
            ftrace_events: "sched/sched_switch"
            ftrace_events: "power/suspend_resume"
            ftrace_events: "sched/sched_process_exit"
            ftrace_events: "sched/sched_process_free"
            ftrace_events: "task/task_newtask"
            ftrace_events: "task/task_rename"
            ftrace_events: "ftrace/print"
            atrace_categories: "gfx"
            atrace_categories: "view"
            atrace_categories: "webview"
            atrace_categories: "camera"
            atrace_categories: "dalvik"
            atrace_categories: "power"
        }
    }
}
data_sources: {
    config {
        name: "linux.process_stats"
        target_buffer: 1
        process_stats_config {
            scan_all_processes_on_start: true
        }
    }
}

EOF

window下执行有点问题,后面ubuntu下试试,我自己改文件为bat文件了。。。

F:\learn\systrace\bilibili>bash.bat

F:\learn\systrace\bilibili>adb shell perfetto \
perfetto_cmd.cc:441      The TraceConfig is empty

F:\learn\systrace\bilibili>-c - --txt \
'-c' 不是内部或外部命令,也不是可运行的程序
或批处理文件。

F:\learn\systrace\bilibili>-o /data/misc/perfetto-traces/trace \
'-o' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
此时不应有 <<。

F:\learn\systrace\bilibili><<EOF

ubuntu试了下可以的,直接把上面的命令保存成.sh执行就可以了

可以正常打开

 

4.网页端抓取

https://ui.perfetto.dev/#!/

注意ubuntu下使用Google浏览器可以,windows下面试了不行。。。

点击左侧栏Record new trace,然后详细设置选一些自己感兴趣的,最后点击Start Recording

点击开始录制,然后会弹出进度条

录制完成后会自动打开

我设置了20s 50M google浏览器打开竟然很卡。。。。是Ubuntu配置了太低了么。。

过会就好了,可以看到生命周期啥的都有

 

5.总结

抓trace的方法系统提供了3个,脚本应该是最方便的,其次是开发者选项里的,最差的是config,操作有些复杂

漏了一个 https://ui.perfetto.dev/#!/

大多对Ubuntu支持的很好,windows支持的不怎么样。

  • 5
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值