如何用Android手机抓取Systrace

admin 2023年3月16日13:07:58评论19 views字数 2796阅读9分19秒阅读模式

经典好文推荐,通过阅读本文,您将收获以下知识点:

一、手机端打开Systrace方法
二、System Traceing的主要功能
三、Systrace 的抓取方法
四、Systrace 的打开方法
五、Sytrace 转换成html 的方法

一、手机端打开Systrace方法

Systrace 是研发人员用来分析手机卡顿等性能问题的Log,那么如何在手机端抓取Systrace Log呢?

1.手机端抓取Systrace 的方法

System Traceing 打开方法一

进入Settings >> System >> Developer options >> System Traceing点击即可。

如何用Android手机抓取Systrace

System Traceing 打开方法一

System Traceing 打开方法二

Settings 界面直接搜索System Traceing也可以。

如何用Android手机抓取Systrace

System Traceing 打开方法二

二、System Traceing的主要功能

System Traceing的主要有以下功能

  1. 记录trace

  2. Debug app trace

  3. 配置Trace 抓取的组件内容

  4. 恢复系统默认trace配置

  5. trace 默认缓存大小配置

  6. 清除之前已经保存的trace

  7. 在SystemUI 快速设置中显示

如何用Android手机抓取Systrace

System Traceing 的主要功能

1. 记录trace

点击开始后手机会有通知提示,然后我们复现问题,问题复现结束后,关闭trace 即可。

如何用Android手机抓取Systrace

会在traces文件夹下生成某某.ctrace举例

2. Debug app trace

此功能可以抓取 app 运行缓慢以及丢帧等问题的trace log。

3. 配置Trace 抓取的组件内容

我们可以根据不同的情况,配置抓取不同的trace 信息。详细配置信息如下:


如何用Android手机抓取Systrace

配置Trace 抓取的组件内容

4. 恢复系统默认trace配置

此功能主要是为了恢复默认的Trace 配置,因为假如抓取配置的组件信息过多,我们的缓存大小又有现在,抓取trace时间长的话,之前的trace会被冲掉,导致可能抓取的trace 被冲掉,抓取无效。

5. trace 默认缓存大小配置

trace 默认缓存大小 主要有:4M8M16M32M64M

如何用Android手机抓取Systrace

trace 默认缓存大小配置

6. 清除之前已经保存的trace

清除trace 会清空 /data/local/traces 下所有的trace 文件,请谨慎操作。

如何用Android手机抓取Systrace

清除保存在/data/local/traces

7. 在SystemUI 快速设置中显示

开启在SystemUI 快速设置中显示,可以实现在SystemUI 设置栏中快速开始关闭抓取Trace 方法。

如何用Android手机抓取Systrace

在SystemUI 快速设置中显示

三、Systrace 的抓取方法

点击开始记录trace,复现问题,然后点击关闭trace,这样trace 文件就会保存在/data/local/traces目录下,然后pull 出来 ,使用举例如下:

C:UsersAdministrator>adb pull /data/local/traces .
/data/local/traces/: 1 file pulled. 21.6 MB/s (5962270 bytes in 0.263s)

C:UsersAdministrator>

四、Systrace 的打开方法

导出的***.cstrace文件可以通过perfetto网站:https://ui.perfetto.dev/,点击 open with legacy UI打开。

如何用Android手机抓取Systrace


如何用Android手机抓取Systrace

打开后的内容

五、Sytrace 转换成html 的方法

使用 systrace.py 脚本既可以将手机抓取的Systrace 转换成Html 文件。

1.systrace.py 文件目录如下:

sdkplatform-toolssystracesystrace.py
2.systrace.py 转换命令如下:

systrace.py --from-file=input.ctrace -o output.html
3.systrace.py 源文件如下:

#!/usr/bin/env python


# Copyright (c) 2015 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import os
import sys

version = sys.version_info[:2]
if version != (2, 7):
sys.stderr.write('Systrace does not support Python %d.%d. '
'Please use Python 2.7.n' % version)
sys.exit(1)

systrace_dir = os.path.abspath(
os.path.join(os.path.dirname(__file__), 'catapult', 'systrace'))
sys.path.insert(0, systrace_dir)

def RemoveAllStalePycFiles(base_dir):
"""Scan directories for old .pyc files without a .py file and delete them."""
for dirname, _, filenames in os.walk(base_dir):
if '.git' in dirname:
continue
for filename in filenames:
root, ext = os.path.splitext(filename)
if ext != '.pyc':
continue

pyc_path = os.path.join(dirname, filename)
py_path = os.path.join(dirname, root + '.py')

try:
if not os.path.exists(py_path):
os.remove(pyc_path)
except OSError:
# Wrap OS calls in try/except in case another process touched this file.
pass

try:
os.removedirs(dirname)
except OSError:
# Wrap OS calls in try/except in case another process touched this dir.
pass

if __name__ == '__main__':
RemoveAllStalePycFiles(os.path.dirname(__file__))
from systrace import run_systrace
sys.exit(run_systrace.main())

推荐阅读

Android Framework学习路线

Android Framework基础到深入篇

原文始发于微信公众号(哆啦安全):如何用Android手机抓取Systrace

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2023年3月16日13:07:58
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   如何用Android手机抓取Systracehttp://cn-sec.com/archives/1607475.html

发表评论

匿名网友 填写信息