Android性能优化--Perfetto抓取trace

Android性能优化–Perfetto抓取trace

本文首发地址 https://blog.csdn.net/CSqingchen/article/details/128900541
最新更新地址 https://gitee.com/chenjim/chenjimblog
Perfetto 官方链接地址 https://github.com/google/perfetto/

介绍

Perfetto 是基于 Android 的系统追踪服务, Android的trace跟踪服务在 Android11® 之后是默认打开的,但是如果你是 Android 9 ( P ) 或者 10 ( Q ) ,那么就需要手动设置一下相应的 prop 属性。
adb shell setprop persist.traced.enable 1


使用 adb 抓取

adb shell perfetto -o /data/misc/perfetto-traces/trace_file.perfetto-trace -t 60s sched freq idle am wm gfx view binder_driver hal dalvik camera input res memory
-o /data/misc/perfetto-traces/trace_file.perfetto-trace 输出trace的路径
-t 60s 最多抓取时长,可以Ctrl+C停止
sched ... memory 要抓取相关模块
更多配置说明,参见文档:https://perfetto.dev/docs/concepts/config
使用配置文件,通过adb抓取,参考如下
adb push atrace.cfg /data/local/tmp/atrace.cfg
adb shell "cat /data/local/tmp/atrace.cfg | perfetto --txt -c - -o /data/misc/perfetto-traces/trace.perfetto-trace"
atrace.cfg 是配置信息,默认最多只抓取10秒,可以修改更长
可以Ctrl+C停止,需执行adb shell
再执行 cat /data/local/tmp/atrace.cfg | perfetto --txt -c - -o /data/misc/perfetto-traces/trace.perfetto-trace
更多配置参见 https://github.com/google/perfetto/blob/master/test/configs
然后 adb pull /data/misc/perfetto-traces/trace.perfetto-trace ,在 https://perfetto.dev/ 导入文件分析


通过 perfetto 网页抓取

打开 https://ui.perfetto.dev/#!/record
“Add ADB Device” 选择手机设备
如下图,有一些参数配置,根据自己的需要添加修改

最后右上角 “Start Recording”


直接在手机上抓取

开启开发者模式:系统设置–关于手机–连续点击。部分机型可网上搜索开启方式。
进入开发者模式,选择"系统跟踪",里面有一些相关设置类别,可以选择要抓取的数据种类
抓取的trace信息会直接存储在手机 /data/local/traces,“查看跟踪文件”,可以分享
此方式比较适合没有电脑,随时抓取


使用 record_android_trace 抓取

执行如下命令,会自动抓取,并打开 https://perfetto.dev/ 分析
python record_android_trace -c atrace.cfg -o out.perfetto.trace
record_android_trace 是 perfetto 中文件,具体参见以上命令的链接
atrace.cfg 是配置信息,更多配置说明参见文档:https://perfetto.dev/docs/concepts/config
默认只抓取10秒,可以修改更长,可以Ctrl+C停止,更多配置参见 https://github.com/google/perfetto/blob/master/test/configs
首次运行会从 googleapis.com 下载文件,如果遇到超时,可能需要修改如下
Windows可参考 Android基于perfetto分析native内存泄露 放文件

diff --git a/tools/heap_profile b/tools/heap_profile
@@ -243,8 +243,9 @@ def download_or_get_cached(file_name, url, sha256):
	if needs_download:
		# Either the filed doesn't exist or the SHA256 doesn't match.
		tmp_path = bin_path + '.tmp'
+    proxy_7890='http://127.0.0.1:7890'
		print('Downloading ' + url)
-    subprocess.check_call(['curl', '-f', '-L', '-#', '-o', tmp_path, url])
+    subprocess.check_call(['curl', '-f', '-L', '-#','-x',proxy_7890, '-o', tmp_path, url])
		with open(tmp_path, 'rb') as fd:
		actual_sha256 = hashlib.sha256(fd.read()).hexdigest()
		if actual_sha256 != sha256:

熟悉 perfetto 快捷键,会有事半功倍效果


注意事项

  • 最好先执行adb shell,然后执行perfetto相关命令,因为在某些情况 Ctrl+C不通过ADB传播而无法停止.
  • 在 Android 12 之前的非 root 设备上,由于过度限制的 SELinux 规则,配置只能通过 (cat config | adb shell perfetto -c -) 传递 。由于 Android 12 /data/misc/perfetto-configs可用于存储配置。
  • 在 Android 10 之前的设备上,adb 无法直接拉取 /data/misc/perfetto-traces. 可以用 adb shell cat /data/misc/perfetto-traces/trace > trace变通。
  • 当捕获较长的跟踪时,例如在基准测试或 CI 的上下文中,使用 PID=$(perfetto --background)然后kill $PID停止。


  • 一个参考配置示例如下
    更多可参见 https://github.com/google/perfetto/blob/master/test/configs
    buffers: {
        size_kb: 707200
        fill_policy: RING_BUFFER
    }
    buffers: {
        size_kb: 707200
        fill_policy: RING_BUFFER
    }
    data_sources: {
        config {
            name: "linux.process_stats"
            target_buffer: 1
            process_stats_config {
                scan_all_processes_on_start: true
                proc_stats_poll_ms: 1000
            }
        }
    }
    data_sources: {
        config {
            name: "android.log"
            android_log_config {
            }
        }
    }
    data_sources: {
        config {
            name: "android.surfaceflinger.frametimeline"
        }
    }
    data_sources: {
        config {
            name: "linux.sys_stats"
            sys_stats_config {
                meminfo_period_ms: 1000
                vmstat_period_ms: 1000
                stat_period_ms: 1000
                stat_counters: STAT_CPU_TIMES
                stat_counters: STAT_FORK_COUNT
            }
        }
    }
    data_sources: {
        config {
            name: "android.heapprofd"
            target_buffer: 0
            heapprofd_config {
                sampling_interval_bytes: 4096
                shmem_size_bytes: 8388608
                block_client: true
            }
        }
    }
    data_sources: {
        config {
            name: "android.java_hprof"
            target_buffer: 0
            java_hprof_config {
            }
        }
    }
    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: "sched/sched_process_exit"
                ftrace_events: "sched/sched_process_free"
                ftrace_events: "task/task_newtask"
                ftrace_events: "task/task_rename"
                ftrace_events: "lowmemorykiller/lowmemory_kill"
                ftrace_events: "oom/oom_score_adj_update"
                ftrace_events: "ftrace/print"
                ftrace_events: "binder/*"
                atrace_categories: "input"
                atrace_categories: "gfx"
                atrace_categories: "view"
                atrace_categories: "webview"
                atrace_categories: "camera"
                atrace_categories: "dalvik"
                atrace_categories: "power"
                atrace_categories: "wm"
                atrace_categories: "am"
                atrace_categories: "ss"
                atrace_categories: "sched"
                atrace_categories: "freq"
                atrace_categories: "binder_driver"
                atrace_categories: "aidl"
                atrace_categories: "binder_lock"
                atrace_apps: "*"
            }
        }
    }
    duration_ms: 300000
    flush_period_ms: 30000
    incremental_state_config {
        clear_period_ms: 5000
    }
    write_into_file: true
    

相关文章连接
Android性能优化–Perfetto抓取trace
Android性能优化–perfetto分析native内存泄露
Android性能优化–Perfetto用SQL性能分析

<think>好的,我现在需要帮助用户修正adb shell perfetto命令的参数错误,特别是关于duration和输出文件trace.perfetto的问题。首先,我需要回忆一下perfetto命令的正确语法和参数用法。 根据之前的引用资料[^2]和[^3],用户提到perfetto可以通过配置文件或直接命令行参数来抓取trace。用户可能在使用adb shell perfetto时参数顺序或格式有误。常见的参数包括-o指定输出文件,-c指定配置文件,还有时间参数可能有不同的写法。 我记得perfetto的duration参数应该以时间单位结尾,比如10s表示10秒。另外,输出文件需要使用-o参数来指定。用户可能错误地将参数顺序弄反了,或者漏掉了必要的参数。 接下来,我需要检查用户可能的错误命令。例如,用户可能输入了类似: adb shell perfetto duration 10s trace.perfetto 但正确的应该是: adb shell perfetto -o /data/misc/perfetto-traces/trace.perfetto --txt -c /dev/stdin <<EOF duration_ms: 10000 ... EOF 或者使用简化的命令行参数,如: adb shell perfetto -o /data/misc/perfetto-traces/trace.perfetto --time 10s 另外,需要注意输出文件的路径是否有写入权限,通常/data/misc/perfetto-traces是推荐的目录。用户可能直接指定了trace.perfetto而没有路径,这可能导致权限错误。 还需要确认Android版本是否支持命令行参数,或者是否需要使用配置文件。在较新版本中,可能可以直接使用--time指定持续时间,而旧版本可能需要配置文件。 总结可能的错误点: 1. duration参数没有正确指定时间单位或位置。 2. 输出文件路径不正确或缺少-o参数。 3. 缺少必要的参数如--txt或-c配置。 因此,正确的命令应该包含-o指定输出路径,--time指定持续时间,或者使用配置文件来定义duration_ms。需要根据用户的具体使用场景来推荐合适的命令格式,并提醒权限和路径的问题。</think>### 修正说明 `adb shell perfetto` 命令参数错误主要涉及**持续时间(duration)**和**输出文件路径**的格式问题。正确语法需要结合参数顺序、权限控制和配置文件使用[^3]。 #### 修正前错误示例 ```bash adb shell perfetto duration 10s trace.perfetto ``` 错误原因:缺少输出参数`-o`,未指定配置文件或时间参数格式错误 #### 修正后正确用法 **方法1:使用命令行参数(需Android 12+)** ```bash adb shell perfetto -o /data/misc/perfetto-traces/trace.perfetto \ --time 10s \ --config 'buffers: {size_kb: 10000} data_sources: {config {name: "linux.process_stats"}}' ``` **方法2:使用配置文件(兼容Android 9-11)** ```bash adb shell perfetto -o /data/misc/perfetto-traces/trace.perfetto \ --txt -c /dev/stdin <<EOF duration_ms: 10000 buffers { size_kb: 10000 } data_sources { config { name: "linux.process_stats" } } EOF ``` ### 关键参数说明 1. **-o**:指定输出文件路径,必须使用系统可写目录 2. **--time**:持续时间(格式:`数字+单位`,如`10s`/`5m`) 3. **-c**:配置文件输入(支持stdin管道) 4. **--txt**:当使用文本格式配置文件时必选 ### 验证命令 ```bash adb pull /data/misc/perfetto-traces/trace.perfetto # 使用 https://ui.perfetto.dev 打开验证 ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

清霜辰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值