从 Perfetto 转换为其他 trace 格式

Perfetto 的原生 protobuf trace 格式可以使用 traceconv 工具转换为其他格式。traceconv 同时也是一个用于 trace 符号化/反混淆以及一些小型 trace 编辑工具的工具包。

前提条件

traceconv 有三组模式:

使用方法

使用最新的二进制文件:

curl -LO https://get.perfetto.dev/traceconv chmod +x traceconv ./traceconv MODE [OPTIONS] [input_file] [output_file]

curl.exe -LO https://get.perfetto.dev/traceconv python traceconv MODE [OPTIONS] [input_file] [output_file]

traceconv 脚本是一个轻量级 Python 包装器,首次使用时会在 ~/.local/share/perfetto/prebuilts 下下载并缓存适合你平台的原生二进制文件(包括 Windows 上的 traceconv.exe)。

当省略输入或输出路径(或传递 -)时,traceconv 从 stdin 读取并写入 stdout。运行 ./traceconv 不带参数可打印你的版本支持的所有模式和选项的完整列表。

格式转换

模式 输出
text protobuf 文本格式 — proto 的文本表示
json Chrome JSON 格式,可在 chrome://tracing 中查看
systrace Android systrace 使用的 ftrace 文本/HTML 格式
ctrace 压缩的 systrace 格式
profile 聚合的 pprof profile(heapprofd、perf、Java heap 图)
firefox Firefox profiler 格式

示例:

./traceconv json trace.perfetto-trace trace.json ./traceconv systrace trace.perfetto-trace trace.html ./traceconv text trace.perfetto-trace trace.textproto

profile 将一个或多个 .pb 文件写入目录(默认为随机临时目录)而不是单个输出文件,因此请使用 --output-dir 而不是位置输出路径:

./traceconv profile --output-dir ./profiles trace.perfetto-trace ./traceconv profile --java-heap --pid 1234 --output-dir ./profiles trace.perfetto-trace ./traceconv profile --perf --timestamps 1000000,2000000 --output-dir ./profiles trace.perfetto-trace

常用选项:

符号化和反混淆

这些模式使用原生符号和/或 ProGuard/R8 反混淆映射来丰富 trace。有关 Perfetto 如何发现符号文件和映射文件的背景信息,请参阅符号化参考。

bundle(推荐)

bundle 是符号化和反混淆的推荐入口点。它将 trace 及其原生符号和 ProGuard/R8 映射打包成一个独立的 TAR,这是与队友分享、附加到 bug 或归档以供后续分析的正确产物。除非有特定原因,否则请优先使用 bundle 而不是 symbolize/deobfuscate

./traceconv bundle trace.perfetto-trace trace.bundle.tar # 提供额外的符号搜索路径或显式的 ProGuard 映射: ./traceconv bundle \ --symbol-paths /path/to/symbols1,/path/to/symbols2 \ --proguard-map com.example.app=/path/to/mapping.txt \ trace.perfetto-trace trace.bundle.tar

bundle 专用选项:

NOTE: bundle 需要输入和输出的真实文件路径 — 它不接受 stdin/stdout。

symbolize / deobfuscate(高级)

用于无法使用 bundle 的流水线的底层构建块。每个都将 Packet 流(符号或反混淆映射)发送到单独的输出文件:

./traceconv symbolize trace.perfetto-trace symbols.pb ./traceconv deobfuscate trace.perfetto-trace mappings.pb

请优先使用 bundle — 它生成一个独立的 TAR,而不是留下松散的附属文件需要管理。

实用工具

模式 功能
binary 将 protobuf 文本格式的 trace 转换回二进制形式。
decompress_packets 解压缩 trace 中压缩的 TracePacket
./traceconv binary trace.textproto trace.perfetto-trace ./traceconv decompress_packets trace.perfetto-trace trace.decompressed

在旧版 systrace UI 中打开

如果你只想使用旧版(Catapult)trace 查看器打开 Perfetto trace,可以直接导航到 ui.perfetto.dev,并使用 "Open with legacy UI" 链接。这会在浏览器中使用 WebAssembly 运行 traceconv,并将转换后的 trace 无缝传递给 chrome://tracing。