Android APP性能调优工具 Systrace 的使用

2024-02-17 08:08

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

http://blog.csdn.net/whorus1/article/details/51210573

1 Systrace 简介

Android 应用启动速度优化方式有很多方法,不过在优化之前,需要找到应用启动速度的瓶颈,找到关键点之后,再去优化,则可以达到事半功倍的效果。 Google 提供了很多 Debug 工具来帮助我们优化应用,这其中就包括 Systrace 工具。

Systrace 是 Android 4.1 中新增的性能数据采样和分析工具。它可帮助开发者收集 Android 关键子系统(如 surfaceflinger、WindowManagerService 等 Framework 部分关键模块、服务,View 系统等)的运行信息,从而帮助开发者更直观的分析系统瓶颈,改进性能。

Systrace 的功能包括跟踪系统的 I/O 操作、内核工作队列、CPU 负载以及 Android 各个子系统的运行状况等。在 Android 平台中,它主要由三部分组成:

  • 内核部分

    Systrace 利用了Linux Kernel 中的 ftrace 功能。所以,如果要使用 Systrace 的话,必须开启 kernel 中和 ftrace 相关的模块。

  • 数据采集部分

    Android 定义了一个 Trace 类。应用程序可利用该类把统计信息输出给 ftrace。同时,Android 还有一个 atrace 程序,它可以从 ftrace 中读取统计信息然后交给数据分析工具来处理。

  • 数据分析工具

    Android 提供一个 systrace.py(Python脚本文件,位于Android SDK 目录 /tools/systrace 中,其内部将调用 atrace 程序)用来配置数据采集的方式(如采集数据的标签、输出文件名等)和收集 ftrace 统计数据并生成一个结果网页文件供用户查看。

从本质上说,Systrace 是对 Linux Kernel 中 ftrace 的封装。应用进程需要利用 Android 提供的 Trace 类来使用 Systrace。

关于 Systrace 的官方介绍和使用可以看这里:Systrace 官方文档。

2 Systrace 用法

使用 Systrace前,要先了解一下 Systrace 在各个平台上的使用方法,鉴于大家使用 Eclipse 和 Android Studio 的居多,所以直接摘抄官网关于这个的使用方法,不过不管是什么工具,流程是一样的:

  • 手机准备好你要进行抓取的界面
  • 点击开始抓取(命令行的话就是开始执行命令)
  • 手机上开始操作
  • 设定好的时间到了之后,会将生成 Trace 文件,使用 Chrome 将这个文件打开进行分析

下面介绍了四种使用工具抓取 Systrace 的方法:

2.1 使用 Eclipse

  1. In Eclipse, open an Android application project.
  2. Switch to the DDMS perspective, by selecting Window > Perspectives > DDMS.
  3. In the Devices tab, select the device on which to run a trace. If no devices are listed, make sure your device is connected via USB cable and that debugging is enabled on the device.
  4. Click the Systrace icon at the top of the Devices panel to configure tracing.
  5. Set the tracing options and click OK to start the trace.

2.2 使用 Android Studio

  1. In Android Studio, open an Android application project.
  2. Open the Device Monitor by selecting Tools > Android > Monitor.
  3. In the Devices tab, select the device on which to run a trace. If no devices are listed, make sure your device is connected via USB cable and that debugging is enabled on the device.
  4. Click the Systrace icon at the top of the Devices panel to configure tracing.
  5. Set the tracing options and click OK to start the trace.

2.3 使用 Device Monitor

  1. Navigate to your SDK tools/ directory.
  2. Run the monitor program.
  3. In the Devices tab, select the device on which to run a trace. If no devices are listed, make sure your device is connected via USB cable and that debugging is enabled on the device.
  4. Click the Systrace icon at the top of the Devices panel to configure tracing.
  5. Set the tracing options and click OK to start the trace.

2.4 使用命令行工具(强烈推荐)

命令行形式比较灵活,速度也比较快,一次性配置好之后,以后再使用的时候就会很快就出结果.

  
  1. $ cd android-sdk/platform-tools/systrace
  2. $ python systrace.py --time=10 -o mynewtrace.trace sched gfx view wm

从上面的命令可以看到 Systrace 工具的位置,只需要在 Bash 中配置好对应的路径和 Alias,使用起来还是很快速的。另外需要注意的是: User版本是不可以抓 Trace 的,只有 ENG 版本或者 Userdebug 版本才可以,是不是又多了一个买 Nexus5 的理由 ^_^。

抓取结束后,会生成对应的 Trace 文件,注意这个文件只能被 Chrome 打开。关于如何分析 Trace 文件,我们下面的章节会讲。

3 Systrace 命令参数

不论使用那种工具,在抓取之前都会让选择参数,下面说一下这些参数的意思:

  • -h, –help Show the help message.(帮助)
  • -o <FILE> Write the HTML trace report to the specified file.(即输出文件名,)
  • -t N, –time=N Trace activity for N seconds. The default value is 5 seconds. (Trace抓取的时间,一般是 : -t 8)
  • -b N, –buf-size=N Use a trace buffer size of N kilobytes. This option lets you limit the total size of the data collected during a trace.
  • -k <KFUNCS>
  • —ktrace=<KFUNCS> Trace the activity of specific kernel functions, specified in a comma-separated list.
  • -l, –list-categories List the available tracing category tags. The available tags are(下面的参数不用翻译了估计大家也看得懂,贴官方的解释也会比较权威,后面分析的时候我们会看到这些参数的作用的):

    • gfx – Graphics
    • input – Input
    • view – View
    • webview – WebView
    • wm – Window Manager
    • am – Activity Manager
    • audio – Audio
    • video – Video
    • camera – Camera
    • hal – Hardware Modules
    • res – Resource Loading
    • dalvik – Dalvik VM
    • rs – RenderScript
    • sched – CPU Scheduling
    • freq – CPU Frequency
    • membus – Memory Bus Utilization
    • idle – CPU Idle
    • disk – Disk input and output
    • load – CPU Load
    • sync – Synchronization Manager
    • workq – Kernel Workqueues Note: Some trace categories are not supported on all devices. Tip: If you want to see the names of tasks in the trace output, you must include the sched category in your command parameters.
  • -a  <APP_NAME>

  • —app=<APP_NAME> Enable tracing for applications, specified as a comma-separated list of package names. The apps must contain tracing instrumentation calls from the Trace class. For more information, see Analyzing Display and Performance.
  • —link-assets Link to the original CSS or JavaScript resources instead of embedding them in the HTML trace report.
  • —from-file=<FROM_FILE> Create the interactive Systrace report from a file, instead of running a live trace.
  • —asset-dir=<ASSET_DIR> Specify a directory for the trace report assets. This option is useful for maintaining a single set of assets for multiple Systrace reports.
  • -e <DEVICE_SERIAL>
  • —serial=<DEVICE_SERIAL> Conduct the trace on a specific connected device, identified by its device serial number.

上面的参数虽然比较多,但使用工具的时候不需考虑这么多,在对应的项目前打钩即可,命令行的时候才会去手动加参数:

我们一般会把这个命令配置成Alias,配置如下:

  
  1. alias st-start='python /path/to/android-studio/sdk/platform-tools/systrace/systrace.py'
  2. alias st-start-gfx = 'st-start -t 8 gfx input view sched freq wm am hwui workq res dalvik sync disk load perf hal rs idle mmc'

这样在使用的时候,可以直接敲 st-start-gfx 即可,当然为了区分和保持各个文件,还需要加上 -o xxx.Trace。上面的命令和参数不必一次就理解,只需要记住如何简单使用即可,在分析的过程中,这些东西都会慢慢熟悉的。

4 Systrace 结果截图

Systrace 抓取结束后,会生成一个文件,这个文件必须使用 Chrome 打开。下面的图是一张典型的应用启动时候的 Systrace 图:

Systrace

0

这篇关于Android APP性能调优工具 Systrace 的使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Spring IoC 容器的使用详解(最新整理)

《SpringIoC容器的使用详解(最新整理)》文章介绍了Spring框架中的应用分层思想与IoC容器原理,通过分层解耦业务逻辑、数据访问等模块,IoC容器利用@Component注解管理Bean... 目录1. 应用分层2. IoC 的介绍3. IoC 容器的使用3.1. bean 的存储3.2. 方法注

Python内置函数之classmethod函数使用详解

《Python内置函数之classmethod函数使用详解》:本文主要介绍Python内置函数之classmethod函数使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 类方法定义与基本语法2. 类方法 vs 实例方法 vs 静态方法3. 核心特性与用法(1编程客

Linux中压缩、网络传输与系统监控工具的使用完整指南

《Linux中压缩、网络传输与系统监控工具的使用完整指南》在Linux系统管理中,压缩与传输工具是数据备份和远程协作的桥梁,而系统监控工具则是保障服务器稳定运行的眼睛,下面小编就来和大家详细介绍一下它... 目录引言一、压缩与解压:数据存储与传输的优化核心1. zip/unzip:通用压缩格式的便捷操作2.

使用Python实现可恢复式多线程下载器

《使用Python实现可恢复式多线程下载器》在数字时代,大文件下载已成为日常操作,本文将手把手教你用Python打造专业级下载器,实现断点续传,多线程加速,速度限制等功能,感兴趣的小伙伴可以了解下... 目录一、智能续传:从崩溃边缘抢救进度二、多线程加速:榨干网络带宽三、速度控制:做网络的好邻居四、终端交互

Python中注释使用方法举例详解

《Python中注释使用方法举例详解》在Python编程语言中注释是必不可少的一部分,它有助于提高代码的可读性和维护性,:本文主要介绍Python中注释使用方法的相关资料,需要的朋友可以参考下... 目录一、前言二、什么是注释?示例:三、单行注释语法:以 China编程# 开头,后面的内容为注释内容示例:示例:四

Go语言数据库编程GORM 的基本使用详解

《Go语言数据库编程GORM的基本使用详解》GORM是Go语言流行的ORM框架,封装database/sql,支持自动迁移、关联、事务等,提供CRUD、条件查询、钩子函数、日志等功能,简化数据库操作... 目录一、安装与初始化1. 安装 GORM 及数据库驱动2. 建立数据库连接二、定义模型结构体三、自动迁

ModelMapper基本使用和常见场景示例详解

《ModelMapper基本使用和常见场景示例详解》ModelMapper是Java对象映射库,支持自动映射、自定义规则、集合转换及高级配置(如匹配策略、转换器),可集成SpringBoot,减少样板... 目录1. 添加依赖2. 基本用法示例:简单对象映射3. 自定义映射规则4. 集合映射5. 高级配置匹

Spring 框架之Springfox使用详解

《Spring框架之Springfox使用详解》Springfox是Spring框架的API文档工具,集成Swagger规范,自动生成文档并支持多语言/版本,模块化设计便于扩展,但存在版本兼容性、性... 目录核心功能工作原理模块化设计使用示例注意事项优缺点优点缺点总结适用场景建议总结Springfox 是

嵌入式数据库SQLite 3配置使用讲解

《嵌入式数据库SQLite3配置使用讲解》本文强调嵌入式项目中SQLite3数据库的重要性,因其零配置、轻量级、跨平台及事务处理特性,可保障数据溯源与责任明确,详细讲解安装配置、基础语法及SQLit... 目录0、惨痛教训1、SQLite3环境配置(1)、下载安装SQLite库(2)、解压下载的文件(3)、

sqlite3 命令行工具使用指南

《sqlite3命令行工具使用指南》本文系统介绍sqlite3CLI的启动、数据库操作、元数据查询、数据导入导出及输出格式化命令,涵盖文件管理、备份恢复、性能统计等实用功能,并说明命令分类、SQL语... 目录一、启动与退出二、数据库与文件操作三、元数据查询四、数据操作与导入导出五、查询输出格式化六、实用功