1 /* 2 * Copyright (C) 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 * http://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 #ifndef SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_TABLE_FUNCTIONS_INTERVAL_INTERSECT_H_ 18 #define SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_TABLE_FUNCTIONS_INTERVAL_INTERSECT_H_ 19 20 #include <cstdint> 21 #include <memory> 22 #include <string> 23 #include <vector> 24 25 #include "perfetto/ext/base/status_or.h" 26 #include "perfetto/trace_processor/basic_types.h" 27 #include "src/trace_processor/containers/string_pool.h" 28 #include "src/trace_processor/db/table.h" 29 #include "src/trace_processor/perfetto_sql/intrinsics/table_functions/static_table_function.h" 30 31 namespace perfetto::trace_processor { 32 33 // An SQL table-function which computes the intersection of intervals from two 34 // tables. 35 // 36 // Given two sets of sorted non-overlapping intervals (with id, timestamp and 37 // duration) returns intervals that are intersections between those two sets, 38 // with ids to state what intervals are intersected. 39 // 40 // LEFT . - - - - - - . 41 // RIGHT - - . - - . - - 42 // Intersection . - . - - . - . 43 // 44 // Arguments are RepeatedBuilderResult protos containing a column of 45 // numerics values: 46 // 1) |in_left_ids|(uint32_t): Ids from the left table. 47 // 2) |in_left_tses|(uint64_t): Timestamps (starts) of intervals from 48 // the left table. 49 // 3) |in_left_durs|(uint64_t): Durations of intervals 50 // from the left table. 51 // 4) |in_right_ids|(uint32_t): Ids from the right table. 52 // 5) |in_right_tses|(uint64_t): Timestamps (starts) of intervals 53 // from the right table. 54 // 6) |in_right_durs|(uint64_t): Durations of intervals from the right table. 55 // 56 // NOTES: 57 // - The first 3 arguments have to have the same number of values. 58 // - Timestamps in left and right columns have to be sorted. 59 // 60 // Returns: 61 // 1) |ts|: Start of the intersection. 62 // 2) |dur|: Duration of the intersection. 63 // 3) |left_id|: Id of the slice that was intersected in the first table. 64 // 4) |right_id|: Id of the slice that was intersected in the second table. 65 class IntervalIntersect : public StaticTableFunction { 66 public: 67 explicit IntervalIntersect(StringPool*); 68 virtual ~IntervalIntersect() override; 69 70 // StaticTableFunction implementation. 71 Table::Schema CreateSchema() override; 72 std::string TableName() override; 73 uint32_t EstimateRowCount() override; 74 base::StatusOr<std::unique_ptr<Table>> ComputeTable( 75 const std::vector<SqlValue>& arguments) override; 76 77 private: 78 StringPool* pool_; 79 }; 80 81 } // namespace perfetto::trace_processor 82 83 #endif // SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_TABLE_FUNCTIONS_INTERVAL_INTERSECT_H_ 84