• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1--
2-- Copyright 2024 The Android Open Source Project
3--
4-- Licensed under the Apache License, Version 2.0 (the "License");
5-- you may not use this file except in compliance with the License.
6-- You may obtain a copy of the License at
7--
8--     https://www.apache.org/licenses/LICENSE-2.0
9--
10-- Unless required by applicable law or agreed to in writing, software
11-- distributed under the License is distributed on an "AS IS" BASIS,
12-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-- See the License for the specific language governing permissions and
14-- limitations under the License.
15--
16
17SELECT RUN_METRIC('android/process_metadata.sql');
18
19INCLUDE PERFETTO MODULE memory.heap_graph_dominator_tree;
20INCLUDE PERFETTO MODULE graphs.partition;
21
22DROP TABLE IF EXISTS _heap_graph_dominator_tree_for_partition;
23CREATE PERFETTO TABLE _heap_graph_dominator_tree_for_partition AS
24SELECT
25  tree.id,
26  tree.idom_id as parent_id,
27  obj.type_id as group_key
28FROM memory_heap_graph_dominator_tree tree
29JOIN heap_graph_object obj USING(id)
30UNION ALL
31-- provide a single root required by tree partition if heap graph exists.
32SELECT
33  memory_heap_graph_super_root_fn() AS id,
34  NULL AS parent_id,
35  (SELECT MAX(id) + 1 FROM heap_graph_class) AS group_key
36WHERE memory_heap_graph_super_root_fn() IS NOT NULL;
37
38DROP TABLE IF EXISTS _heap_object_marked_for_dominated_stats;
39CREATE PERFETTO TABLE _heap_object_marked_for_dominated_stats AS
40SELECT
41  id,
42  IIF(parent_id IS NULL, 1, 0) as marked
43FROM tree_structural_partition_by_group!(_heap_graph_dominator_tree_for_partition)
44ORDER BY id;
45
46DROP TABLE IF EXISTS _heap_class_stats;
47CREATE PERFETTO TABLE _heap_class_stats AS
48SELECT
49  obj.upid,
50  obj.graph_sample_ts,
51  obj.type_id,
52  COUNT(1) AS obj_count,
53  SUM(self_size) AS size_bytes,
54  SUM(native_size) AS native_size_bytes,
55  SUM(IIF(obj.reachable, 1, 0)) AS reachable_obj_count,
56  SUM(IIF(obj.reachable, self_size, 0)) AS reachable_size_bytes,
57  SUM(IIF(obj.reachable, native_size, 0)) AS reachable_native_size_bytes,
58  SUM(IIF(marked, dominated_obj_count, 0)) AS dominated_obj_count,
59  SUM(IIF(marked, dominated_size_bytes, 0)) AS dominated_size_bytes,
60  SUM(IIF(marked, dominated_native_size_bytes, 0)) AS dominated_native_size_bytes
61FROM heap_graph_object obj
62-- Left joins to preserve unreachable objects.
63LEFT JOIN _heap_object_marked_for_dominated_stats USING(id)
64LEFT JOIN memory_heap_graph_dominator_tree USING(id)
65GROUP BY 1, 2, 3
66ORDER BY 1, 2, 3;
67
68DROP VIEW IF EXISTS java_heap_class_stats_output;
69CREATE PERFETTO VIEW java_heap_class_stats_output AS
70WITH
71-- Group by to build the repeated field by upid, ts
72heap_class_stats_count_protos AS (
73  SELECT
74    upid,
75    graph_sample_ts,
76    RepeatedField(JavaHeapClassStats_TypeCount(
77      'type_name', IFNULL(c.deobfuscated_name, c.name),
78      'obj_count', obj_count,
79      'size_bytes', size_bytes,
80      'native_size_bytes', native_size_bytes,
81      'reachable_obj_count', reachable_obj_count,
82      'reachable_size_bytes', reachable_size_bytes,
83      'reachable_native_size_bytes', reachable_native_size_bytes,
84      'dominated_obj_count', dominated_obj_count,
85      'dominated_size_bytes', dominated_size_bytes,
86      'dominated_native_size_bytes', dominated_native_size_bytes
87    )) AS count_protos
88  FROM _heap_class_stats s
89  JOIN heap_graph_class c ON s.type_id = c.id
90  GROUP BY 1, 2
91),
92-- Group by to build the repeated field by upid
93heap_class_stats_sample_protos AS (
94  SELECT
95    upid,
96    RepeatedField(JavaHeapClassStats_Sample(
97      'ts', graph_sample_ts,
98      'type_count', count_protos
99    )) AS sample_protos
100  FROM heap_class_stats_count_protos
101  GROUP BY 1
102)
103SELECT JavaHeapClassStats(
104  'instance_stats', RepeatedField(JavaHeapClassStats_InstanceStats(
105    'upid', upid,
106    'process', process_metadata.metadata,
107    'samples', sample_protos
108  )))
109FROM heap_class_stats_sample_protos JOIN process_metadata USING (upid);
110