Android Perfetto 性能分析

2024-02-08 05:36
文章标签 分析 android 性能 perfetto

本文主要是介绍Android Perfetto 性能分析,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Perfetto是Android为我们提供的性能分析工具,网上已经有很多文章来介绍这个工具了,这里就不再赘述,仅贴几篇我觉得有用的文章/网站在此记录:

  • systrace/perfetto中需要actrace打tag相关方法
  • 常用的SQL查询
  • perfetto ui

perfetto命令可能用到的参数可以用 -h 查询到,我们常用的是 -o-t-b

$ perfetto -h
[307.548] perfetto_cmd.cc:187
Usage: perfetto--background     -d      : Exits immediately and continues tracing inbackground--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 insteadTAG 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 ashuman-readable text.--query-raw              : Like --query, but prints raw proto-encoded bytesof tracing_service_state.proto.--help           -hlight 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).

常用的抓 perfetto 的命令如下:

$ adb shell perfetto -o /data/misc/perfetto-traces/trace.file -t 30s am adb aidl dalvik audio binder_lock binder_driver bionic camera database gfx hal input network nnapi pm power rs res rro sm ss vibrator video webview wm sched freq dalvik

命令后面跟着的 am、adb、aidl、dalvik,查询网上的资料我们可以知道这些都是ATRACE_CAT

我们自己在添加 trace point时,需要先添加ATRACE_TAG ,例如:

#define ATRACE_TAG ATRACE_TAG_GRAPHICS

我理解的是这个文件中所有的 trace 都属于 GRAPHIC 组,那么到底有哪些组呢?

参考 system/core/libcutils/include/cutils/trace.h

#define ATRACE_TAG_NEVER            0       // This tag is never enabled.
#define ATRACE_TAG_ALWAYS           (1<<0)  // This tag is always enabled.
#define ATRACE_TAG_GRAPHICS         (1<<1)
#define ATRACE_TAG_INPUT            (1<<2)
#define ATRACE_TAG_VIEW             (1<<3)
#define ATRACE_TAG_WEBVIEW          (1<<4)
#define ATRACE_TAG_WINDOW_MANAGER   (1<<5)
#define ATRACE_TAG_ACTIVITY_MANAGER (1<<6)
#define ATRACE_TAG_SYNC_MANAGER     (1<<7)
#define ATRACE_TAG_AUDIO            (1<<8)
#define ATRACE_TAG_VIDEO            (1<<9)
#define ATRACE_TAG_CAMERA           (1<<10)
#define ATRACE_TAG_HAL              (1<<11)
#define ATRACE_TAG_APP              (1<<12)
#define ATRACE_TAG_RESOURCES        (1<<13)
#define ATRACE_TAG_DALVIK           (1<<14)
#define ATRACE_TAG_RS               (1<<15)
#define ATRACE_TAG_BIONIC           (1<<16)
#define ATRACE_TAG_POWER            (1<<17)
#define ATRACE_TAG_PACKAGE_MANAGER  (1<<18)
#define ATRACE_TAG_SYSTEM_SERVER    (1<<19)
#define ATRACE_TAG_DATABASE         (1<<20)
#define ATRACE_TAG_NETWORK          (1<<21)
#define ATRACE_TAG_ADB              (1<<22)
#define ATRACE_TAG_VIBRATOR         (1<<23)
#define ATRACE_TAG_AIDL             (1<<24)
#define ATRACE_TAG_NNAPI            (1<<25)
#define ATRACE_TAG_RRO              (1<<26)
#define ATRACE_TAG_LAST             ATRACE_TAG_RRO

添加 ATRACE_TAG 后我们要如何使用命令抓到这个TAG呢?我们需要在命令中指定组才能抓到相关的信息,那么在命令中我们应该如何指定呢?

可以参考 frameworks/native/cmds/atrace/atrace.cpp,里面定义有所有的 ATRACE_CAT,前面的name也就是我们要在cmd中指定的:

static const TracingCategory k_categories[] = {{ "gfx",        "Graphics",                 ATRACE_TAG_GRAPHICS, { } },{ "input",      "Input",                    ATRACE_TAG_INPUT, { } },{ "view",       "View System",              ATRACE_TAG_VIEW, { } },{ "webview",    "WebView",                  ATRACE_TAG_WEBVIEW, { } },{ "wm",         "Window Manager",           ATRACE_TAG_WINDOW_MANAGER, { } },{ "am",         "Activity Manager",         ATRACE_TAG_ACTIVITY_MANAGER, { } },{ "sm",         "Sync Manager",             ATRACE_TAG_SYNC_MANAGER, { } },{ "audio",      "Audio",                    ATRACE_TAG_AUDIO, { } },{ "video",      "Video",                    ATRACE_TAG_VIDEO, { } },{ "camera",     "Camera",                   ATRACE_TAG_CAMERA, { } },{ "hal",        "Hardware Modules",         ATRACE_TAG_HAL, { } },{ "res",        "Resource Loading",         ATRACE_TAG_RESOURCES, { } },{ "dalvik",     "Dalvik VM",                ATRACE_TAG_DALVIK, { } },{ "rs",         "RenderScript",             ATRACE_TAG_RS, { } },{ "bionic",     "Bionic C Library",         ATRACE_TAG_BIONIC, { } },{ "power",      "Power Management",         ATRACE_TAG_POWER, { } },{ "pm",         "Package Manager",          ATRACE_TAG_PACKAGE_MANAGER, { } },{ "ss",         "System Server",            ATRACE_TAG_SYSTEM_SERVER, { } },{ "database",   "Database",                 ATRACE_TAG_DATABASE, { } },{ "network",    "Network",                  ATRACE_TAG_NETWORK, { } },{ "adb",        "ADB",                      ATRACE_TAG_ADB, { } },{ "vibrator",   "Vibrator",                 ATRACE_TAG_VIBRATOR, { } },{ "aidl",       "AIDL calls",               ATRACE_TAG_AIDL, { } },{ "nnapi",      "NNAPI",                    ATRACE_TAG_NNAPI, { } },{ "rro",        "Runtime Resource Overlay", ATRACE_TAG_RRO, { } },{ k_coreServiceCategory, "Core services", 0, { } },{ k_pdxServiceCategory, "PDX services", 0, { } },{ "sched",      "CPU Scheduling",   0, {{ REQ,      "events/sched/sched_switch/enable" },{ REQ,      "events/sched/sched_wakeup/enable" },{ OPT,      "events/sched/sched_waking/enable" },{ OPT,      "events/sched/sched_blocked_reason/enable" },{ OPT,      "events/sched/sched_cpu_hotplug/enable" },{ OPT,      "events/sched/sched_pi_setprio/enable" },{ OPT,      "events/sched/sched_process_exit/enable" },{ OPT,      "events/cgroup/enable" },{ OPT,      "events/oom/oom_score_adj_update/enable" },{ OPT,      "events/task/task_rename/enable" },{ OPT,      "events/task/task_newtask/enable" },} },{ "irq",        "IRQ Events",   0, {{ REQ,      "events/irq/enable" },{ OPT,      "events/ipi/enable" },} },{ "irqoff",     "IRQ-disabled code section tracing", 0, {{ REQ,      "events/preemptirq/irq_enable/enable" },{ REQ,      "events/preemptirq/irq_disable/enable" },} },{ "preemptoff", "Preempt-disabled code section tracing", 0, {{ REQ,      "events/preemptirq/preempt_enable/enable" },{ REQ,      "events/preemptirq/preempt_disable/enable" },} },{ "i2c",        "I2C Events",   0, {{ REQ,      "events/i2c/enable" },{ REQ,      "events/i2c/i2c_read/enable" },{ REQ,      "events/i2c/i2c_write/enable" },{ REQ,      "events/i2c/i2c_result/enable" },{ REQ,      "events/i2c/i2c_reply/enable" },{ OPT,      "events/i2c/smbus_read/enable" },{ OPT,      "events/i2c/smbus_write/enable" },{ OPT,      "events/i2c/smbus_result/enable" },{ OPT,      "events/i2c/smbus_reply/enable" },} },{ "freq",       "CPU Frequency",    0, {{ REQ,      "events/power/cpu_frequency/enable" },{ OPT,      "events/power/clock_set_rate/enable" },{ OPT,      "events/power/clock_disable/enable" },{ OPT,      "events/power/clock_enable/enable" },{ OPT,      "events/clk/clk_set_rate/enable" },{ OPT,      "events/clk/clk_disable/enable" },{ OPT,      "events/clk/clk_enable/enable" },{ OPT,      "events/power/cpu_frequency_limits/enable" },{ OPT,      "events/power/suspend_resume/enable" },} },{ "membus",     "Memory Bus Utilization", 0, {{ REQ,      "events/memory_bus/enable" },} },{ "idle",       "CPU Idle",         0, {{ REQ,      "events/power/cpu_idle/enable" },} },{ "disk",       "Disk I/O",         0, {{ OPT,      "events/f2fs/f2fs_sync_file_enter/enable" },{ OPT,      "events/f2fs/f2fs_sync_file_exit/enable" },{ OPT,      "events/f2fs/f2fs_write_begin/enable" },{ OPT,      "events/f2fs/f2fs_write_end/enable" },{ OPT,      "events/ext4/ext4_da_write_begin/enable" },{ OPT,      "events/ext4/ext4_da_write_end/enable" },{ OPT,      "events/ext4/ext4_sync_file_enter/enable" },{ OPT,      "events/ext4/ext4_sync_file_exit/enable" },{ REQ,      "events/block/block_rq_issue/enable" },{ REQ,      "events/block/block_rq_complete/enable" },} },{ "mmc",        "eMMC commands",    0, {{ REQ,      "events/mmc/enable" },} },{ "load",       "CPU Load",         0, {{ REQ,      "events/cpufreq_interactive/enable" },} },{ "sync",       "Synchronization",  0, {// linux kernel < 4.9{ OPT,      "events/sync/enable" },// linux kernel == 4.9.x{ OPT,      "events/fence/enable" },// linux kernel > 4.9{ OPT,      "events/dma_fence/enable" },} },{ "workq",      "Kernel Workqueues", 0, {{ REQ,      "events/workqueue/enable" },} },{ "memreclaim", "Kernel Memory Reclaim", 0, {{ REQ,      "events/vmscan/mm_vmscan_direct_reclaim_begin/enable" },{ REQ,      "events/vmscan/mm_vmscan_direct_reclaim_end/enable" },{ REQ,      "events/vmscan/mm_vmscan_kswapd_wake/enable" },{ REQ,      "events/vmscan/mm_vmscan_kswapd_sleep/enable" },{ OPT,      "events/lowmemorykiller/enable" },} },{ "regulators",  "Voltage and Current Regulators", 0, {{ REQ,      "events/regulator/enable" },} },{ "binder_driver", "Binder Kernel driver", 0, {{ REQ,      "events/binder/binder_transaction/enable" },{ REQ,      "events/binder/binder_transaction_received/enable" },{ REQ,      "events/binder/binder_transaction_alloc_buf/enable" },{ OPT,      "events/binder/binder_set_priority/enable" },} },{ "binder_lock", "Binder global lock trace", 0, {{ OPT,      "events/binder/binder_lock/enable" },{ OPT,      "events/binder/binder_locked/enable" },{ OPT,      "events/binder/binder_unlock/enable" },} },{ "pagecache",  "Page cache", 0, {{ REQ,      "events/filemap/enable" },} },{ "memory",  "Memory", 0, {{ OPT,      "events/mm_event/mm_event_record/enable" },{ OPT,      "events/kmem/rss_stat/enable" },{ OPT,      "events/kmem/ion_heap_grow/enable" },{ OPT,      "events/kmem/ion_heap_shrink/enable" },{ OPT,      "events/ion/ion_stat/enable" },} },
};

这篇先到这里,perfetto 文件如何分析,我们后面再聊。

这篇关于Android Perfetto 性能分析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/690040

相关文章

Redis主从复制实现原理分析

《Redis主从复制实现原理分析》Redis主从复制通过Sync和CommandPropagate阶段实现数据同步,2.8版本后引入Psync指令,根据复制偏移量进行全量或部分同步,优化了数据传输效率... 目录Redis主DodMIK从复制实现原理实现原理Psync: 2.8版本后总结Redis主从复制实

锐捷和腾达哪个好? 两个品牌路由器对比分析

《锐捷和腾达哪个好?两个品牌路由器对比分析》在选择路由器时,Tenda和锐捷都是备受关注的品牌,各自有独特的产品特点和市场定位,选择哪个品牌的路由器更合适,实际上取决于你的具体需求和使用场景,我们从... 在选购路由器时,锐捷和腾达都是市场上备受关注的品牌,但它们的定位和特点却有所不同。锐捷更偏向企业级和专

Android数据库Room的实际使用过程总结

《Android数据库Room的实际使用过程总结》这篇文章主要给大家介绍了关于Android数据库Room的实际使用过程,详细介绍了如何创建实体类、数据访问对象(DAO)和数据库抽象类,需要的朋友可以... 目录前言一、Room的基本使用1.项目配置2.创建实体类(Entity)3.创建数据访问对象(DAO

Spring中Bean有关NullPointerException异常的原因分析

《Spring中Bean有关NullPointerException异常的原因分析》在Spring中使用@Autowired注解注入的bean不能在静态上下文中访问,否则会导致NullPointerE... 目录Spring中Bean有关NullPointerException异常的原因问题描述解决方案总结

正则表达式高级应用与性能优化记录

《正则表达式高级应用与性能优化记录》本文介绍了正则表达式的高级应用和性能优化技巧,包括文本拆分、合并、XML/HTML解析、数据分析、以及性能优化方法,通过这些技巧,可以更高效地利用正则表达式进行复杂... 目录第6章:正则表达式的高级应用6.1 模式匹配与文本处理6.1.1 文本拆分6.1.2 文本合并6

python中的与时间相关的模块应用场景分析

《python中的与时间相关的模块应用场景分析》本文介绍了Python中与时间相关的几个重要模块:`time`、`datetime`、`calendar`、`timeit`、`pytz`和`dateu... 目录1. time 模块2. datetime 模块3. calendar 模块4. timeit

python-nmap实现python利用nmap进行扫描分析

《python-nmap实现python利用nmap进行扫描分析》Nmap是一个非常用的网络/端口扫描工具,如果想将nmap集成进你的工具里,可以使用python-nmap这个python库,它提供了... 目录前言python-nmap的基本使用PortScanner扫描PortScannerAsync异

Android WebView的加载超时处理方案

《AndroidWebView的加载超时处理方案》在Android开发中,WebView是一个常用的组件,用于在应用中嵌入网页,然而,当网络状况不佳或页面加载过慢时,用户可能会遇到加载超时的问题,本... 目录引言一、WebView加载超时的原因二、加载超时处理方案1. 使用Handler和Timer进行超

Oracle数据库执行计划的查看与分析技巧

《Oracle数据库执行计划的查看与分析技巧》在Oracle数据库中,执行计划能够帮助我们深入了解SQL语句在数据库内部的执行细节,进而优化查询性能、提升系统效率,执行计划是Oracle数据库优化器为... 目录一、什么是执行计划二、查看执行计划的方法(一)使用 EXPLAIN PLAN 命令(二)通过 S

Vue3 的 shallowRef 和 shallowReactive:优化性能

大家对 Vue3 的 ref 和 reactive 都很熟悉,那么对 shallowRef 和 shallowReactive 是否了解呢? 在编程和数据结构中,“shallow”(浅层)通常指对数据结构的最外层进行操作,而不递归地处理其内部或嵌套的数据。这种处理方式关注的是数据结构的第一层属性或元素,而忽略更深层次的嵌套内容。 1. 浅层与深层的对比 1.1 浅层(Shallow) 定义