查询 ProfilingManager 配置文件类似于查询常规 Perfetto 配置文件。因此,请查看 PerfettoSQL 入门,了解
如何查询配置文件。
常规 Perfetto 跟踪记录与 ProfilingManager 跟踪记录之间的一个重要区别是,ProfilingManager 跟踪记录会通过跟踪记录编辑器。出于隐私原因,此编辑器会移除与您的应用无关的其他进程的相关信息。
Perfetto 标准库中的某些查询无法在经过编辑的跟踪记录中使用。这是因为 ProfilingManager 仅收集您应用的分析数据,而不收集其他进程的分析数据。因此,您可以使用 ProfilingManager 的查询比使用本地 Perfetto 记录的完整系统配置文件的查询少。
即使查询空间减少了,您仍然可以按原样使用 PerfettoSQL 查询和 Perfetto 标准库中的表,因此我们 建议您尝试使用它们。
我们还建议您查看 分析 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 ms)。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(首帧时间)。