跳转至

Binder 事务分析

Binder IPC 事务性能分析。

概述

Binder 是 Android 的进程间通信机制。缓慢的 Binder 事务会阻塞调用线程,导致 ANR 和卡顿。perfetto-cli 分析 Binder 调用以识别延迟问题。

命令

binder profile

分析 Binder 事务性能。

perfetto-cli -t trace.perfetto-trace -p com.example.app binder profile

选项:

选项 说明 默认值
--min-latency <ms> 最小事务延迟 10
--group-by <field> 分组方式:aidl / server / none none

示例:

# 仅显示慢事务(>20ms)
perfetto-cli -t trace.perfetto-trace -p com.example.app binder profile --min-latency 20

# 按 AIDL 接口分组
perfetto-cli -t trace.perfetto-trace -p com.example.app binder profile --group-by aidl

# 按服务进程分组
perfetto-cli -t trace.perfetto-trace -p com.example.app binder profile --group-by server

工作流程

  1. 运行 binder profile 查看所有慢事务
  2. 使用 --group-by aidl 识别最慢的接口
  3. 对服务进程使用 cpu utilization 了解原因

提示

  • 主线程上 >16ms 的 Binder 调用会导致掉帧
  • 使用 --group-by server 识别慢系统服务
  • 结合 ANR 分析 — Binder 超时是常见的 ANR 原因