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 16INCLUDE PERFETTO MODULE callstacks.stack_profile; 17 18CREATE PERFETTO TABLE _android_heap_profile_raw_callstacks AS 19WITH 20 metrics AS MATERIALIZED ( 21 SELECT 22 callsite_id, 23 sum(size) AS self_size, 24 sum(max(size, 0)) AS self_alloc_size 25 FROM heap_profile_allocation 26 GROUP BY 27 callsite_id 28 ) 29SELECT 30 c.id, 31 c.parent_id, 32 c.name, 33 c.mapping_name, 34 c.source_file, 35 c.line_number, 36 coalesce(m.self_size, 0) AS self_size, 37 coalesce(m.self_alloc_size, 0) AS self_alloc_size 38FROM _callstacks_for_stack_profile_samples!(metrics) AS c 39LEFT JOIN metrics AS m 40 USING (callsite_id); 41 42CREATE PERFETTO TABLE _android_heap_profile_cumulatives AS 43SELECT 44 a.* 45FROM _graph_aggregating_scan!( 46 ( 47 SELECT id AS source_node_id, parent_id AS dest_node_id 48 FROM _android_heap_profile_raw_callstacks 49 WHERE parent_id IS NOT NULL 50 ), 51 ( 52 SELECT 53 p.id, 54 p.self_size AS cumulative_size, 55 p.self_alloc_size AS cumulative_alloc_size 56 FROM _android_heap_profile_raw_callstacks p 57 LEFT JOIN _android_heap_profile_raw_callstacks c ON c.parent_id = p.id 58 WHERE c.id IS NULL 59 ), 60 (cumulative_size, cumulative_alloc_size), 61 ( 62 WITH agg AS ( 63 SELECT 64 t.id, 65 SUM(t.cumulative_size) AS child_size, 66 SUM(t.cumulative_alloc_size) AS child_alloc_size 67 FROM $table t 68 GROUP BY t.id 69 ) 70 SELECT 71 a.id, 72 a.child_size + r.self_size as cumulative_size, 73 a.child_alloc_size + r.self_alloc_size AS cumulative_alloc_size 74 FROM agg a 75 JOIN _android_heap_profile_raw_callstacks r USING (id) 76 ) 77) AS a; 78 79-- Table summarising the amount of memory allocated by each 80-- callstack as seen by Android native heap profiling (i.e. 81-- profiling information collected by heapprofd). 82-- 83-- Note: this table collapses data from all processes together 84-- into a single table. 85CREATE PERFETTO TABLE android_heap_profile_summary_tree ( 86 -- The id of the callstack. A callstack in this context 87 -- is a unique set of frames up to the root. 88 id LONG, 89 -- The id of the parent callstack for this callstack. 90 parent_id LONG, 91 -- The function name of the frame for this callstack. 92 name STRING, 93 -- The name of the mapping containing the frame. This 94 -- can be a native binary, library, JAR or APK. 95 mapping_name STRING, 96 -- The name of the file containing the function. 97 source_file STRING, 98 -- The line number in the file the function is located at. 99 line_number LONG, 100 -- The amount of memory allocated and *not freed* with this 101 -- function as the leaf frame. 102 self_size LONG, 103 -- The amount of memory allocated and *not freed* with this 104 -- function appearing anywhere on the callstack. 105 cumulative_size LONG, 106 -- The amount of memory allocated with this function as the leaf 107 -- frame. This may include memory which was later freed. 108 self_alloc_size LONG, 109 -- The amount of memory allocated with this function appearing 110 -- anywhere on the callstack. This may include memory which was 111 -- later freed. 112 cumulative_alloc_size LONG 113) AS 114SELECT 115 id, 116 parent_id, 117 name, 118 mapping_name, 119 source_file, 120 line_number, 121 self_size, 122 cumulative_size, 123 self_alloc_size, 124 cumulative_alloc_size 125FROM _android_heap_profile_raw_callstacks AS r 126JOIN _android_heap_profile_cumulatives AS a 127 USING (id); 128