Systrace 是研发人员用来分析手机卡顿等性能问题的Log,那么如何在手机端抓取Systrace Log呢?
1.手机端抓取Systrace 的方法 System Traceing 打开方法一进入Settings
>> System
>> Developer options
>> System Traceing
点击即可。
在Settings
界面直接搜索System Traceing
也可以。
点击开始记录trace,复现问题,然后点击关闭trace,这样trace 文件就会保存在/data/local/traces
目录下,然后pull 出来 ,使用举例如下:
C:\Users\Administrator>adb pull /data/local/traces .
/data/local/traces/: 1 file pulled. 21.6 MB/s (5962270 bytes in 0.263s)
C:\Users\Administrator>
三、Systrace 的打开方法
导出的***.cstrace文件可以通过perfetto网站:https://ui.perfetto.dev/,点击 open with legacy UI打开。
使用 systrace.py 脚本既可以将手机抓取的Systrace 转换成Html 文件。
1.systrace.py 文件目录如下:
sdk\platform-tools\systrace\systrace.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())
四、System Traceing的主要功能
System Traceing的主要有以下功能
-
记录trace
-
Debug app trace
-
配置Trace 抓取的组件内容
-
恢复系统默认trace配置
-
trace 默认缓存大小配置
-
清除之前已经保存的trace
-
在SystemUI 快速设置中显示
点击开始后手机会有通知提示,然后我们复现问题,问题复现结束后,关闭trace 即可。
此功能可以抓取 app 运行缓慢以及丢帧等问题的trace log。
3. 配置Trace 抓取的组件内容我们可以根据不同的情况,配置抓取不同的trace 信息。详细配置信息如下:
此功能主要是为了恢复默认的Trace 配置,因为假如抓取配置的组件信息过多,我们的缓存大小又有现在,抓取trace时间长的话,之前的trace会被冲掉,导致可能抓取的trace 被冲掉,抓取无效。
5. trace 默认缓存大小配置trace 默认缓存大小 主要有:4M
、8M
、16M
、32M
、64M
清除trace 会清空 /data/local/traces
下所有的trace 文件,请谨慎操作。
开启在SystemUI 快速设置中显示,可以实现在SystemUI 设置栏中快速开始关闭抓取Trace 方法。