查询 ProfilingManager 配置文件与查询常规 Perfetto 配置文件类似。因此,请参阅 PerfettoSQL 入门,了解如何查询性能剖析文件。
常规 Perfetto 轨迹与 ProfilingManager 轨迹之间的一个重要区别是,ProfilingManager 轨迹会通过轨迹编辑工具。此编辑工具会出于隐私保护方面的原因移除与您的应用无关的其他进程的相关信息。
Perfetto 标准库中的某些查询无法在已编校的轨迹中使用。这是因为 ProfilingManager 仅收集应用的分析数据,而不收集其他进程的分析数据。因此,您可以使用 ProfilingManager 的查询比使用本地 Perfetto 记录的完整系统配置文件的查询少。
虽然查询空间有所缩小,但您仍然可以按原样使用 Perfetto 标准库中的许多 PerfettoSQL 查询和表,因此我们建议您尝试一下。
我们还建议您查看分析 Android 轨迹,以找到无需修改即可提供实用性能数据的现成查询。
ProfilingManager 示例查询
为了简化查询过程,本部分提供了一系列适用于 ProfilingManager 的查询。您可以直接使用这些查询,也可以将其作为构建其他查询的示例。
查找重复次数最多的切片
此查询会在轨迹中查找重复的切片,并按出现频率对其进行排序,首先显示重复次数最多的切片。
查找重复工作是查找轨迹中不必要工作的常用方法。
-- You only need to call this once in the session to create the function
DROP TABLE IF EXISTS find_duplicates;
CREATE PERFETTO FUNCTION find_duplicates(pattern STRING) RETURNS
TABLE(name STRING, count_slice LONG) AS SELECT name, COUNT(dur) as count_slice FROM slice WHERE name GLOB $pattern GROUP BY name HAVING COUNT(name) >= 2 ORDER BY count_slice DESC;
-- Subsequent calls can just use the function to find dupes
SELECT * FROM find_duplicates('*Text*')
卡顿查询
查找慢帧
此查询会查找应用生成帧的时间过长的帧,假设预期帧速率为 60 Hz(16.6 毫秒)。dur 设置为 16,660,000,因为 Perfetto 表中的切片时长以纳秒为单位存储。
INCLUDE PERFETTO module android.frames.timeline;
SELECT * FROM android_frames WHERE dur > 16660000;
查找导致卡顿的帧
INCLUDE PERFETTO module android.frames.timeline;
SELECT * FROM actual_frame_timeline_slice WHERE jank_type = 'App Deadline Missed';
此查询有助于查找轨迹中发生卡顿的位置,因为应用生成帧的时间过长。这意味着界面线程未能生成帧。在极端情况下,这可能先于 ANR 发生。
查找重复次数最多的对象
您还可以查询与内存相关的配置文件(例如堆转储),以执行更复杂的内存分析。
INCLUDE PERFETTO MODULE android.memory.heap_graph.heap_graph_class_aggregation;
SELECT * FROM android_heap_graph_class_aggregation WHERE obj_count >= 2
ORDER BY obj_count DESC LIMIT 100
此查询会返回前 100 个重复对象。这有助于您查找多次实例化的对象,从而发现缓存这些对象的机会或识别意外的重复项。
冷启动延迟时间
您还可以查询初创公司。本部分提供了一个更复杂的查询,用于估计轨迹中的冷启动时间。
-- This function finds slices that match the given GLOB $pattern
CREATE OR REPLACE FUNCTION find_slices(pattern STRING) RETURNS
TABLE (name STRING, ts LONG, dur LONG) AS
SELECT name,ts,dur FROM slice WHERE name GLOB $pattern;
-- This function generates a slice that starts at $startSlicePattern and finishes at the slice matched by $endSlicePattern. If $inclusive is true, then the end slice dur will be added, otherwise, the end slice start time will be used.
CREATE OR REPLACE PERFETTO FUNCTION generate_start_to_end_slices(startSlicePattern STRING, endSlicePattern STRING, inclusive BOOL) RETURNS
TABLE(name STRING, ts LONG, dur LONG) AS
SELECT name, ts, MIN(startToEndDur) as dur
FROM
(SELECT S.name as name, S.ts as ts, E.ts + IIF($inclusive, E.dur, 0) - S.ts as startToEndDur
FROM find_slices($startSlicePattern) as S CROSS JOIN find_slices($endSlicePattern) as E
WHERE startToEndDur > 0)
GROUP BY name, ts;
-- Using these functions we can estimate cold startup time by generating a slice between bindApplication and first frame.
SELECT * from generate_start_to_end_slices('bindApplication','*Choreographer#doFrame [0-9]*', true)
此查询会生成一个切片,用于表示定义启动时间的两个切片(bindApplication [通常在冷启动应用时找到] 和第一个 Choreographer#doFrame 切片 [生成的第一个帧])之间的时间。此指标可有效估计冷启动 TTFF(首次定位时间)。