Android atrace/systrace的使用

5,852 阅读4分钟
1、atrace/systrace介绍

Systrace是Android系统中性能分析的主要工具。它记录一段时间内的设备活动,并产生一个HTML格式的Systrace报告。它收集了CPU调度、磁盘操作、应用线程等信息,可以帮助开发者分析系统瓶颈,改进性能。

Systrace实际上是一些工具的集合,在设备端使用atrace来控制用户空间的tracing和设置ftrace,kernel中使用tracing机制进行数据采集。Systrace最终读取ftrace的buffer,然后将数据打包以HTML文件的方式呈现。

1642246221.png

2、设备端使用atrace设置ftrace
console:/ # atrace --help
usage: atrace [options] [categories...]
options include:
  -a appname      enable app-level tracing for a comma separated list of cmdlines; 
                  * is a wildcard matching any process
  -b N            use a trace buffer size of N KB
  -c              trace into a circular buffer
  -f filename     use the categories written in a file as space-separated
                    values in a line
  -k fname,...    trace the listed kernel functions
  -n              ignore signals
  -s N            sleep for N seconds before tracing [default 0]
  -t N            trace for N seconds [default 5]
  -z              compress the trace dump
  --async_start   start circular trace and return immediately
  --async_dump    dump the current contents of circular trace buffer
  --async_stop    stop tracing and dump the current contents of circular
                    trace buffer
  --stream        stream trace to stdout as it enters the trace buffer
                    Note: this can take significant CPU time, and is best
                    used for measuring things that are not affected by
                    CPU performance, like pagecache usage.
  --list_categories
                  list the available tracing categories
 -o filename      write the trace to the specified file instead
                    of stdout.

执行指令,-z表示压缩数据,-b表示buffer为4000KB,-t设置15秒钟trace抓取时间,其中包含很多categories,为需要Tracing的模块。

atrace -z -b 4000 gfx input view webview wm am camera hal res audio video dalvik rs ss sched freq idle disk mmc -t 15 > /data/trace_output

其中categories含义为:

atrace  --list_categories
         gfx - Graphics
       input - Input
        view - View System
     webview - WebView
          wm - Window Manager
          am - Activity Manager
          sm - Sync Manager
       audio - Audio
       video - Video
      camera - Camera
         hal - Hardware Modules
         res - Resource Loading
      dalvik - Dalvik VM
          rs - RenderScript
      bionic - Bionic C Library
       power - Power Management
          pm - Package Manager
          ss - System Server
    database - Database
     network - Network
         adb - ADB
    vibrator - Vibrator
        aidl - AIDL calls
       nnapi - NNAPI
         rro - Runtime Resource Overlay
         pdx - PDX services
       sched - CPU Scheduling
         irq - IRQ Events
         i2c - I2C Events
        freq - CPU Frequency
        idle - CPU Idle
        disk - Disk I/O
         mmc - eMMC commands
        sync - Synchronization
       workq - Kernel Workqueues
  memreclaim - Kernel Memory Reclaim
  regulators - Voltage and Current Regulators
  binder_driver - Binder Kernel driver
  binder_lock - Binder global lock trace
   pagecache - Page cache
      memory - Memory
         gfx - Graphics (HAL)
         ion - ION allocation (HAL)
3、复现现象,即需要在这段时间进行监听的问题

执行指令:ps -A | grep atrace,查看atrace进程是否结束抓取并退出

4、抓取完成后,取出trace文件,使用systrace转成html格式
systrace转换脚本源代码路径: android/external/chromium-trace/systrace.py

./systrace.py --from-file trace_output -o trace_output.html
5、用谷歌浏览器打开进行分析

在chrome浏览器输入:chrome://tracing/,然后点击load加载trace html文件trace_output.html,你tracing的模块将以图形化的方式展示出来。通过查看Systrace视图报告,可以查找并修复性能问题。

按键操作       作用
w             放大,[+shift]速度更快
s             缩小,[+shift]速度更快
a             左移,[+shift]速度更快
d             右移,[+shift]速度更快
 
f             放大当前选定区域
m             标记当前选定区域
v             高亮VSync
g             切换是否显示60hz的网格线
0             恢复trace到初始态,这里是数字0而非字母o

h             切换是否显示详情
/             搜索关键字
enter      显示搜索结果,可通过← →定位搜索结果
`             显示/隐藏脚本控制台
?             显示帮助功能
6、SystemStrace报告解析

报告提供了 Android 设备在给定时间段内的系统进程的总体情况,还会检查所捕获的跟踪信息,以突出显示它所观察到的问题(例如界面卡顿或耗电量高)。

1642246452.png

  • 颜色块: 每块颜色占据的长度即为该系统或者自定义trace等执行所占据的时间长度;
  • Alerts: 含有三角状的圆圈图标,对应出现警告的位置,点击可以在右边栏Alerts查看具体警告内容; 警告会告诉你可能丢帧或者卡顿等的原因;
  • Frame: 含有F字母的圆圈图标,对应每一帧开始的位置,不同颜色有不同意义; 绿色表示正常,当颜色为橙色或者红色时,意味着这一帧超过16.6ms(即发现丢帧);

6.1 CPU 活动

显示了表示每个 CPU 中的线程活动的条形图。这些条形会显示所有应用(包括你的应用或游戏)中的 CPU 活动。

1642246552.png

6.2 UI线程

1642246570.png

6.3 app信息

1642246601.png

6.4 线程信息

1642246633.png

每个条形堆上方的多色线条表示特定线程随时间变化的一组状态。每段线条可以包含以下颜色之一:

  • 绿色:正在运行,线程正在完成与某个进程相关的工作或正在响应中断。
  • 蓝色:可运行,线程可以运行但目前未进行调度。
  • 白色:休眠,线程没有可执行的任务,可能是因为线程在遇到斥锁定时被阻止。
  • 红色:Uninterruptible sleep,不可中断的休眠状态,通常处于内核中休眠锁定状态(generally sleeping on a lock in the kernel),一般是正在进行 I/O 操作,红色状态时通常对于性能调试非常有用
  • 橙色:Uninterruptible sleep due to I/O load,由于 I/O load 而不可中断的休眠状态