(两百五十六)学习perfetto(一)

学习https://www.bilibili.com/video/av77424850/

竟然发现Google中国在哔哩哔哩也有账号,i了i了=-=

 

trace分析对应的UI网址

https://ui.perfetto.dev/

官网:

https://perfetto.dev/

 

1.简介

https://perfetto.dev/docs/

Perfetto is a production-grade open-source stack for performance instrumentation and trace analysis. It offers services and libraries and for recording system-level and app-level traces, native + java heap profiling, a library for analyzing traces using SQL and a web-based UI to visualize and explore multi-GB traces.

简单来说就是性能检测和trace分析,了解到的是systrace的升级版

System-wide tracing on Android and Linux

On Linux and Anroid, Perfetto bundles a number of data sources that are able to gather detailed performance data from different system interfaces. For the full sets and details see the Data Sources section of the documentation. Same examples:

  • Kernel tracing: Perfetto integrates with Linux's ftrace and allows to record kernel events (e.g scheduling events, syscalls) into the trace.

  • /proc and /sys pollers, which allow to sample the state of process-wide or system-wide cpu and memory counters over time.

  • Integration with Android HALs modules for recording battery and energy-usage counters.

  • Native heap profiling: a low-overhead heap profiler for hooking malloc/free/new/delete and associating memory to callstacks, based on out-of-process unwinding, configurable sampling, attachable to already running processes.

  • Java heap profiling: an out-of-process profiler tightly integrated with the Android RunTime that allows to get full snapshots of the managed heap retention graph (types, field names, retained size and references to other objects) without, however, dumping the full heap contents (strings and bitmaps) and hence reducing the serialization time and output file size.

On Android, Perfetto is the next-generation system tracing system and replaces the chromium-based systrace. ATrace-based intstrumentation remains fully supported. See Android developer docs for more details.

简要说来举例说支持如下5个功能点

  • 内核跟踪
  • cpu和内存计数器跟踪
  • 电量使用情况跟踪
  • native heap分析
  • java heap 分析

提及说是systrace的下一代版本

Trace visualization

Perfetto provides also a brand new trace visualizer for opening and querying hours-long traces, available at ui.perfetto.dev. The new visualizer takes advantage of modern web platform technolgies. Its multi-threading design based WebWorkers keeps the UI always responsive; the analytical power of Trace Processor and SQLite is fully available in-browser through WebAssembly.

The Perfetto UI works fully offline after it has been opened once. Traces opened with the UI are processed locally by the browser and do not require any server-side interaction.

提及了ui.perfetto.dev是基于web的可视化工具,一次打开后就可以脱机工作,不与服务器做交互

 

2.Get Started

perfetto允许您从各种数据源(通过ftrace进行内核调度,通过atrace进行用户空间检测以及此站点中列出的所有其他数据源)收集来自Android设备的系统范围的性能跟踪。

Starting the tracing services

Due to Perfetto's service-based architecture , the traced and traced_probes services need to be running to record traces.

These services are shipped on Android system images by default since Android 9 (Pie) but are not always enabled by default. On Android 9 (P) and 10 (Q) those services are enabled by default only on Pixel phones and must be manually enabled on other phones. Since Android 11 (R), perfetto services are enabled by default on most devices.

To enable perfetto services run:

# Will start both traced and traced_probes.

adb shell setprop persist.traced.enable 1

简单来说就是要启动一下perfetto服务,我是从开发者选项中打开对应“系统跟踪”在systemUi的快捷键的,看了下这个值已经是true了

Recording a trace

You can collect a trace in the following ways:

记录trace方式一共有两种,UI和命令行

 

3.UI

可以看到Ui能进行更详细的设置,比如CPU,比如对应用等等,应用下面的用户注解ctrl+点击可以多选categories

浏览器连接adb需要支持https

测试了火狐和360都不行,Google浏览器可以adb连接上~

但是权限被拒绝了。。。打开了usb调试下面的3个开关都不行,后面试下native版本有没有用。。。

 

4. Perfetto CLI

https://perfetto.dev/docs/reference/perfetto-cli

https://developer.android.google.cn/studio/command-line/perfetto?hl=zh_cn

https://developer.android.google.cn/studio/command-line/perfetto?hl=en

This section describes how to use the perfetto commandline binary to capture traces. Examples are given in terms of an Android device connected over ADB.

perfetto has two modes for configuring the tracing session (i.e. what and how to collect):

  • lightweight mode: all config options are supplied as commandline flags, but the available data sources are restricted to ftrace and atrace. This mode is similar to systrace.
  • normal mode: the configuration is specified in a protocol buffer. This allows for full customisation of collected traces.

General options

The following table lists the available options when using perfetto in either mode.

OptionDescription
--background | -dPerfetto immediately exits the command-line interface and continues recording your trace in background.
--out OUT_FILE | -o OUT_FILESpecifies the desired path to the output trace file, or - for stdout. perfetto writes the output to the file described in the flags above. The output format compiles with the format defined in AOSP trace.proto.
--dropbox TAGUploads your trace via the DropBoxManager API using the tag you specify.
--no-guardrailsDisables protections against excessive resource usage when enabling the --dropbox flag during testing.
--reset-guardrailsResets the persistent state of the guardrails and exits (for testing).
--queryQueries the service state and prints it as human-readable text.
--query-rawSimilar to --query, but prints raw proto-encoded bytes of tracing_service_state.proto.
--help | -hPrints out help text for the perfetto tool.

Lightweight mode

The general syntax for using perfetto in lightweight mode is as follows:

 adb shell perfetto [ --time TIMESPEC ] [ --buffer SIZE ] [ --size SIZE ]
           [ ATRACE_CAT | FTRACE_GROUP/FTRACE_NAME | FTRACE_GROUP/* ]...

The following table lists the available options when using perfetto in lightweight mode.

OptionDescription
--time TIME[s|m|h] | -t TIME[s|m|h]Specifies the trace duration in seconds, minutes, or hours. For example, --time 1m specifies a trace duration of 1 minute. The default duration is 10 seconds.
--buffer SIZE[mb|gb] | -b SIZE[mb|gb]Specifies the ring buffer size in megabytes (mb) or gigabytes (gb). The default parameter is --buffer 32mb.
--size SIZE[mb|gb] | -s SIZE[mb|gb]Specifies the max file size in megabytes (mb) or gigabytes (gb). By default perfetto uses only in-memory ring-buffer.

This is followed by a list of event specifiers:

EventDescription
ATRACE_CATSpecifies the atrace categories you want to record a trace for. For example, the following command traces Window Manager using atrace: adb shell perfetto --out FILE wm. To record other categories, see this list of atrace categories.
FTRACE_GROUP/FTRACE_NAMESpecifies the ftrace events you want to record a trace for. For example, the following command traces sched/sched_switch events: adb shell perfetto --out FILE sched/sched_switch
FTRACE_GROUP/*Record all events in group (e.g. sched/*). Specifies the group of ftrace events you want to record a trace for. For example, the following command traces sched/* events: adb shell perfetto --out FILE 'sched/*'

Normal mode

The general syntax for using perfetto in normal mode is as follows:

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

The following table lists the available options when using perfetto in normal mode.

OptionDescription
--config CONFIG_FILE | -c CONFIG_FILESpecifies the path to a configuration file. In normal mode, some configurations may be encoded in a configuration protocol buffer. This file must comply with the protocol buffer schema defined in AOSP trace_config.proto. You select and configure the data sources using the DataSourceConfig member of the TraceConfig, as defined in AOSP data_source_config.proto.
--txtInstructs perfetto to parse the config file as pbtxt. This flag is experimental, and it's not recommended that you enable it for production.

命令行有的连接点击不进去,我们可以退而求其次,我们看到Ui界面也有个命令行,这应该是将用户的UI选择换成命令行了

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

buffers: {
    size_kb: 8960
    fill_policy: DISCARD
}
buffers: {
    size_kb: 1280
    fill_policy: DISCARD
}
data_sources: {
    config {
        name: "linux.process_stats"
        target_buffer: 1
        process_stats_config {
            scan_all_processes_on_start: true
        }
    }
}
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: "sched/sched_switch"
            ftrace_events: "power/suspend_resume"
            ftrace_events: "sched/sched_wakeup"
            ftrace_events: "sched/sched_wakeup_new"
            ftrace_events: "sched/sched_waking"
            ftrace_events: "power/cpu_frequency"
            ftrace_events: "power/cpu_idle"
            ftrace_events: "power/gpu_frequency"
            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: "wm"
            atrace_categories: "am"
            atrace_categories: "rs"
            atrace_categories: "pm"
            atrace_categories: "ss"
        }
    }
}
duration_ms: 10000

EOF

normal的config不知道怎么写,用lightweight mode来吧

自己写了个类似的命令行

adb shell perfetto -t 30s -b 1gb -s 1gb --out /data/misc/perfetto-traces/trace.perfetto-trace wm gfx view am rs pm ss sched/* task/* ftrace/print

大概是30s抓一些设定了atrace和ftrace,如果不知道对应的categories,可以在ui上选好了,命令行那边会显示出来的

用perfetto可以看到哔哩哔哩的UI线程绘制流程了~

对比系统默认抓的perfetto,信息量少得可怜

补充下系统抓perfetto的方法

Supported data sources

This section describes the different sources that perfetto uses to generate your trace.

FTrace

The ftrace data source allows perfetto to get events from the kernel.

You can enable this source by setting ftrace_config in the DataSourceConfig.

The events that can be enabled include:

  • Scheduling activity:

    • sched/sched_switch
    • sched/sched_wakeup
    • sched/sched_wakeup_new
    • sched/sched_process_exec
    • sched/sched_process_exit
    • sched/sched_process_fork
    • sched/sched_process_free
    • sched/sched_process_hang
    • sched/sched_process_wait
  • Filesystem events:

  • ATrace events

Depending on your device, OS version, and kernel, more events might be available. For more information, refer to the config protos.

Process Stats

The process stats data source allows you to get polled counters about the system and individual processes.

You can enable this source by setting process_stats_config and sys_stats_config in the DataSourceConfig.

The data that profetto generates includes:

Depending on your device, OS version, and kernel, more events might be available. To learn more, refer to the config protos for sys_stats and process_stats.

Heapprofd

Heapprofd allows you to sample the causes of native memory use.

You can enable this source by setting heapprofd_config in the DataSourceConfig.

This produces ProfilePackets. These include the Java frames of the callstack.

Additional information on how to use heapprofd can be found at perfetto.dev.

Other sources

Depending on your device, OS version, and kernel, more data-sources might be available. To learn more, refer to the data source config protos.

Additional information about perfetto can be found at perfetto.dev.

另外系统抓perfetto的方法

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/ .

多选些atrace和ftrace,信息比默认的多好多了

 

总结

抓perfetto命令方法

  • ui(还有点问题,用native + google浏览器试试)
  • 命令行

adb shell perfetto -t 30s -b 1gb -s 1gb --out /data/misc/perfetto-traces/trace.perfetto-trace wm gfx view am rs pm ss sched/* task/* ftrace/print

具体atrace和ftrace 的 categories可以参考perfetto的https://ui.perfetto.dev/#!/record?p=instructions

 

待续

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

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

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

其实就是开发者文档里的config文件,继续学习

 

 

 

相关推荐
©️2020 CSDN 皮肤主题: 鲸 设计师:meimeiellie 返回首页