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 18-- Table containing all the timestamped samples of CPU profiling which occurred 19-- during the trace. 20-- 21-- Currently, this table is backed by the following data sources: 22-- * Linux perf 23-- * macOS instruments 24-- * Chrome CPU profiling 25-- * Legacy V8 CPU profiling 26-- * Profiling data in Gecko traces 27CREATE PERFETTO TABLE cpu_profiling_samples ( 28 -- The id of the sample. 29 id LONG, 30 -- The timestamp of the sample. 31 ts TIMESTAMP, 32 -- The utid of the thread of the sample, if available. 33 utid JOINID(thread.id), 34 -- The tid of the sample, if available. 35 tid LONG, 36 -- The thread name of thread of the sample, if available. 37 thread_name STRING, 38 -- The ucpu of the sample, if available. 39 ucpu LONG, 40 -- The cpu of the sample, if available. 41 cpu LONG, 42 -- The callsite id of the sample. 43 callsite_id LONG 44) AS 45WITH 46 raw_samples AS ( 47 -- Linux perf samples. 48 SELECT 49 p.ts, 50 p.utid, 51 p.cpu AS ucpu, 52 p.callsite_id 53 FROM perf_sample AS p 54 UNION ALL 55 -- Instruments samples. 56 SELECT 57 p.ts, 58 p.utid, 59 p.cpu AS ucpu, 60 p.callsite_id 61 FROM instruments_sample AS p 62 UNION ALL 63 -- All other CPU profiling. 64 SELECT 65 s.ts, 66 s.utid, 67 NULL AS ucpu, 68 s.callsite_id 69 FROM cpu_profile_stack_sample AS s 70 ) 71SELECT 72 row_number() OVER (ORDER BY ts) AS id, 73 r.*, 74 t.tid, 75 t.name AS thread_name, 76 c.cpu 77FROM raw_samples AS r 78LEFT JOIN thread AS t 79 USING (utid) 80LEFT JOIN cpu AS c 81 USING (ucpu) 82ORDER BY 83 ts; 84 85CREATE PERFETTO TABLE _cpu_profiling_self_callsites AS 86SELECT 87 * 88FROM _callstacks_for_callsites!(( 89 SELECT callsite_id 90 FROM cpu_profiling_samples 91)) 92ORDER BY 93 id; 94 95-- Table summarising the callstacks captured during any CPU profiling which 96-- occurred during the trace. 97-- 98-- Specifically, this table returns a tree containing all the callstacks seen 99-- during the trace with `self_count` equal to the number of samples with that 100-- frame as the leaf and `cumulative_count` equal to the number of samples with 101-- the frame anywhere in the tree. 102-- 103-- The data sources supported are the same as the `cpu_profiling_samples` table. 104CREATE PERFETTO TABLE cpu_profiling_summary_tree ( 105 -- The id of the callstack; by callstack we mean a unique set of frames up to 106 -- the root frame. 107 id LONG, 108 -- The id of the parent callstack for this callstack. NULL if this is root. 109 parent_id LONG, 110 -- The function name of the frame for this callstack. 111 name STRING, 112 -- The name of the mapping containing the frame. This can be a native binary, 113 -- library, JAR or APK. 114 mapping_name STRING, 115 -- The name of the file containing the function. 116 source_file STRING, 117 -- The line number in the file the function is located at. 118 line_number LONG, 119 -- The number of samples with this function as the leaf frame. 120 self_count LONG, 121 -- The number of samples with this function appearing anywhere on the 122 -- callstack. 123 cumulative_count LONG 124) AS 125SELECT 126 id, 127 parent_id, 128 name, 129 mapping_name, 130 source_file, 131 line_number, 132 sum(self_count) AS self_count, 133 sum(cumulative_count) AS cumulative_count 134FROM ( 135 SELECT 136 r.*, 137 a.cumulative_count 138 FROM _cpu_profiling_self_callsites AS r 139 JOIN _callstacks_self_to_cumulative!(( 140 SELECT id, parent_id, self_count 141 FROM _cpu_profiling_self_callsites 142 )) AS a 143 USING (id) 144) 145GROUP BY 146 id; 147