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_DFS_WEIGHT_BOUNDED_H_
18 #define SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_TABLE_FUNCTIONS_DFS_WEIGHT_BOUNDED_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 performs a weight bounded DFS from a set of start
34 // nodes in a graph and returns all the nodes which are reachable from each
35 // start node independently.
36 //
37 // Arguments:
38 //  1) |source_node_ids|: RepeatedBuilderResult proto containing a column of
39 //     uint32 values corresponding to the source of edges.
40 //  2) |dest_node_ids|:  RepeatedBuilderResult proto containing a column of
41 //     uint32 values corresponding to the destination of edges. This number of
42 //     values should be the same as |source_node_ids| with each index in
43 //     |source_node_ids| acting as the source for the corresponding index in
44 //     |dest_node_ids|.
45 //  3) |edge_weights|:  RepeatedBuilderResult proto containing a column of
46 //     uint32 values corresponding to the weight of edges. This number of
47 //     values should be the same as |source_node_ids| with each index in
48 //     |source_node_ids| acting as the source for the corresponding index in
49 //     |edge_weights|.
50 //  4) |root_node_ids|: RepeatedBuilderResult proto containing a column of
51 //     uint32 values corresponding to the ID of the start nodes in the graph
52 //     from which reachability should be computed.
53 //  5) |root_max_weights|: RepeatedBuilderResult proto containing a column of
54 //     uint32 values corresponding to the max sum of edge weights inclusive,
55 //     at which point the DFS from the |root_node_ids| stops. This number of
56 //     values should be the same as |root_node_ids|.
57 //  6) |is_target_weight_floor|: Whether the target_weight is a floor weight or
58 //  ceiling weight.
59 //     If it's floor, the search stops right after we exceed the target weight,
60 //     and we include the node that pushed just passed the target. If ceiling,
61 //     the search stops right before the target weight and the node that would
62 //     have pushed us passed the target is not included.
63 
64 //
65 // Returns:
66 //  A table with the nodes reachable from the start node, their "parent" in
67 //  the tree generated by the DFS and the starting node itself "root". The
68 //  schema of the table is (root_node_id uint32_t, node_id uint32_t,
69 //  parent_node_id optional<uint32_t>).
70 //
71 // Note: this function is not intended to be used directly from SQL: instead
72 // macros exist in the standard library, wrapping it and making it
73 // user-friendly.
74 class DfsWeightBounded : public StaticTableFunction {
75  public:
76   explicit DfsWeightBounded(StringPool*);
77   virtual ~DfsWeightBounded() override;
78 
79   // StaticTableFunction implementation.
80   Table::Schema CreateSchema() override;
81   std::string TableName() override;
82   uint32_t EstimateRowCount() override;
83   base::StatusOr<std::unique_ptr<Table>> ComputeTable(
84       const std::vector<SqlValue>& arguments) override;
85 
86  private:
87   StringPool* pool_ = nullptr;
88 };
89 
90 }  // namespace perfetto::trace_processor
91 
92 #endif  // SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_TABLE_FUNCTIONS_DFS_WEIGHT_BOUNDED_H_
93