1-- 2-- Copyright 2023 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 deprecated.v42.common.timestamps; 17 18-- Timestamp of first counter value in a counter. 19CREATE PERFETTO FUNCTION earliest_timestamp_for_counter_track( 20 -- Id of a counter track with a counter. 21 counter_track_id INT) 22-- Timestamp of first counter value. Null if doesn't exist. 23RETURNS LONG AS 24SELECT MIN(ts) FROM counter WHERE counter.track_id = $counter_track_id; 25 26-- Counter values with details of counter track with calculated duration of each counter value. 27-- Duration is calculated as time from counter to the next counter. 28CREATE PERFETTO FUNCTION counter_with_dur_for_track( 29 -- Id of track counter track. 30 counter_track_id INT) 31RETURNS TABLE( 32 -- Timestamp of the counter value. 33 ts LONG, 34 -- Duration of the counter value. 35 dur LONG, 36 -- Counter value. 37 value DOUBLE, 38 -- Id of the counter track. 39 track_id INT, 40 -- Name of the counter track. 41 track_name STRING, 42 -- Counter track set id. 43 track_arg_set_id INT, 44 -- Counter arg set id. 45 arg_set_id INT 46) AS 47SELECT 48 ts, 49 LEAD(ts, 1, trace_end()) OVER(ORDER BY ts) - ts AS dur, 50 value, 51 track.id AS track_id, 52 track.name AS track_name, 53 track.source_arg_set_id AS track_arg_set_id, 54 counter.arg_set_id AS arg_set_id 55FROM counter 56JOIN counter_track track ON track.id = counter.track_id 57WHERE track.id = $counter_track_id; 58 59-- COUNTER_WITH_DUR_FOR_TRACK but in a specified time. 60-- Does calculation over the table ends - creates an artificial counter value at 61-- the start if needed and chops the duration of the last timestamps in range. 62CREATE PERFETTO FUNCTION counter_for_time_range( 63 -- Id of track counter track. 64 counter_track_id INT, 65 -- Timestamp of the timerange start. 66 -- Can be earlier than the first counter value. 67 start_ts LONG, 68 -- Timestamp of the timerange end. 69 end_ts LONG) 70RETURNS TABLE( 71 -- Timestamp of the counter value. 72 ts LONG, 73 -- Duration of the counter value. 74 dur LONG, 75 -- Counter value. 76 value DOUBLE, 77 -- If of the counter track. 78 track_id INT, 79 -- Name of the counter track. 80 track_name STRING, 81 -- Counter track set id. 82 track_arg_set_id INT, 83 -- Counter arg set id. 84 arg_set_id INT 85) AS 86SELECT 87 IIF(ts < $start_ts, $start_ts, ts) AS ts, 88 IIF( 89 ts < $start_ts, 90 dur - ($start_ts - ts), 91 IIF(ts + dur > $end_ts, $end_ts - ts, dur)) AS dur, 92 value, 93 track_id, 94 track_name, 95 track_arg_set_id, 96 arg_set_id 97FROM counter_with_dur_for_track($counter_track_id) 98WHERE TRUE 99 AND ts + dur >= $start_ts 100 AND ts < $end_ts 101ORDER BY ts ASC; 102