1 /*
2  * Copyright (C) 2021 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_DESCENDANT_H_
18 #define SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_TABLE_FUNCTIONS_DESCENDANT_H_
19 
20 #include <cstdint>
21 #include <memory>
22 #include <optional>
23 #include <string>
24 #include <vector>
25 
26 #include "perfetto/ext/base/status_or.h"
27 #include "perfetto/trace_processor/basic_types.h"
28 #include "src/trace_processor/db/table.h"
29 #include "src/trace_processor/perfetto_sql/intrinsics/table_functions/static_table_function.h"
30 #include "src/trace_processor/storage/trace_storage.h"
31 #include "src/trace_processor/tables/slice_tables_py.h"
32 
33 namespace perfetto::trace_processor {
34 
35 class TraceProcessorContext;
36 
37 // Implements the following dynamic tables:
38 // * descendant_slice
39 // * descendant_slice_by_stack
40 //
41 // See docs/analysis/trace-processor for usage.
42 class Descendant : public StaticTableFunction {
43  public:
44   enum class Type { kSlice = 1, kSliceByStack = 2 };
45 
46   Descendant(Type type, const TraceStorage*);
47 
48   Table::Schema CreateSchema() override;
49   std::string TableName() override;
50   uint32_t EstimateRowCount() override;
51   base::StatusOr<std::unique_ptr<Table>> ComputeTable(
52       const std::vector<SqlValue>& arguments) override;
53 
54   // Returns a vector of slice rows which are descendants of |slice_id|. Returns
55   // std::nullopt if an invalid |slice_id| is given. This is used by
56   // ConnectedFlow to traverse flow indirectly connected flow events.
57   static std::optional<std::vector<tables::SliceTable::RowNumber>>
58   GetDescendantSlices(const tables::SliceTable& slices, SliceId slice_id);
59 
60  private:
61   Type type_;
62   const TraceStorage* storage_ = nullptr;
63 };
64 
65 }  // namespace perfetto::trace_processor
66 
67 #endif  // SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_TABLE_FUNCTIONS_DESCENDANT_H_
68