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 17-- For a given counter timeline (e.g. a single counter track), returns 18-- intervals of time where the counter has the same value. 19-- 20-- Intervals are computed in a "forward-looking" way. That is, if a counter 21-- changes value at some timestamp, it's assumed it *just* reached that 22-- value and it should continue to have that value until the next 23-- value change. The final value is assumed to hold until the very end of 24-- the trace. 25-- 26-- For example, suppose we have the following data: 27-- ``` 28-- ts=0, value=10, track_id=1 29-- ts=0, value=10, track_id=2 30-- ts=10, value=10, track_id=1 31-- ts=10, value=20, track_id=2 32-- ts=20, value=30, track_id=1 33-- [end of trace at ts = 40] 34-- ``` 35-- 36-- Then this macro will generate the following intervals: 37-- ``` 38-- ts=0, dur=20, value=10, track_id=1 39-- ts=20, dur=10, value=30, track_id=1 40-- ts=0, dur=10, value=10, track_id=2 41-- ts=10, dur=30, value=20, track_id=2 42-- ``` 43CREATE PERFETTO MACRO counter_leading_intervals( 44 -- A table/view/subquery corresponding to a "counter-like" table. 45 -- This table must have the columns "id" and "ts" and "track_id" and "value" corresponding 46 -- to an id, timestamp, counter track_id and associated counter value. 47 counter_table TableOrSubquery 48) 49-- Table with the schema (id LONG, ts TIMESTAMP, dur DURATION, track_id JOINID(track.id), 50-- value DOUBLE, next_value DOUBLE, delta_value DOUBLE). 51RETURNS TableOrSubquery AS 52( 53 SELECT 54 c0 AS id, 55 c1 AS ts, 56 c2 AS dur, 57 c3 AS track_id, 58 c4 AS value, 59 c5 AS next_value, 60 c6 AS delta_value 61 FROM __intrinsic_table_ptr( 62 __intrinsic_counter_intervals( 63 "leading", 64 trace_end(), 65 ( 66 SELECT 67 __intrinsic_counter_per_track_agg(input.id, input.ts, input.track_id, input.value) 68 FROM ( 69 SELECT 70 * 71 FROM $counter_table 72 ORDER BY 73 ts 74 ) AS input 75 ) 76 ) 77 ) 78 WHERE 79 __intrinsic_table_ptr_bind(c0, 'id') 80 AND __intrinsic_table_ptr_bind(c1, 'ts') 81 AND __intrinsic_table_ptr_bind(c2, 'dur') 82 AND __intrinsic_table_ptr_bind(c3, 'track_id') 83 AND __intrinsic_table_ptr_bind(c4, 'value') 84 AND __intrinsic_table_ptr_bind(c5, 'next_value') 85 AND __intrinsic_table_ptr_bind(c6, 'delta_value') 86); 87