1-- 2-- Copyright 2022 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-- Trace bounds 18-- 19 20-- Fetch start of the trace. 21-- @ret LONG Start of the trace in nanoseconds. 22SELECT CREATE_FUNCTION( 23 'TRACE_START()', 24 'LONG', 25 'SELECT start_ts FROM trace_bounds;' 26); 27 28-- Fetch end of the trace. 29-- @ret LONG End of the trace in nanoseconds. 30SELECT CREATE_FUNCTION( 31 'TRACE_END()', 32 'LONG', 33 'SELECT end_ts FROM trace_bounds;' 34); 35 36-- Fetch duration of the trace. 37-- @ret LONG Duration of the trace in nanoseconds. 38SELECT CREATE_FUNCTION( 39 'TRACE_DUR()', 40 'LONG', 41 'SELECT TRACE_END() - TRACE_START();' 42); 43 44-- Checks whether two spans are overlapping. 45-- 46-- @arg ts1 LONG Start of first span. 47-- @arg ts_end1 LONG End of first span. 48-- @arg ts2 LONG Start of second span. 49-- @arg ts_end2 LONG End of second span. 50-- @ret BOOL Whether two spans are overlapping. 51SELECT CREATE_FUNCTION( 52 'IS_SPANS_OVERLAPPING(ts1 LONG, ts_end1 LONG, ts2 LONG, ts_end2 LONG)', 53 'BOOL', 54 ' 55 SELECT (IIF($ts1 < $ts2, $ts2, $ts1) 56 < IIF($ts_end1 < $ts_end2, $ts_end1, $ts_end2)) 57 ' 58); 59 60--Return the overlapping duration between two spans. 61--If either duration is less than 0 or there's no intersection, 0 is returned 62-- 63-- @arg ts1 LONG Timestamp of first slice start. 64-- @arg dur1 LONG Duration of first slice. 65-- @arg ts2 LONG Timestamp of second slice start. 66-- @arg dur2 LONG Duration of second slice. 67-- @ret INT Overlapping duration 68SELECT CREATE_FUNCTION( 69 'SPANS_OVERLAPPING_DUR(ts1 LONG, dur1 LONG, ts2 LONG, dur2 LONG)', 70 'INT', 71 ' 72 SELECT 73 CASE 74 WHEN $dur1 = -1 OR $dur2 = -1 THEN 0 75 WHEN $ts1 + $dur1 < $ts2 OR $ts2 + $dur2 < $ts1 THEN 0 76 WHEN ($ts1 >= $ts2) AND ($ts1 + $dur1 <= $ts2 + $dur2) THEN $dur1 77 WHEN ($ts1 < $ts2) AND ($ts1 + $dur1 < $ts2 + $dur2) THEN $ts1 + $dur1 - $ts2 78 WHEN ($ts1 > $ts2) AND ($ts1 + $dur1 > $ts2 + $dur2) THEN $ts2 + $dur2 - $ts1 79 ELSE $dur2 80 END 81 ' 82); 83