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 #include "src/trace_processor/perfetto_sql/intrinsics/table_functions/descendant.h"
18
19 #include <algorithm>
20 #include <cinttypes>
21 #include <cstdint>
22 #include <memory>
23 #include <optional>
24 #include <string>
25 #include <utility>
26 #include <vector>
27
28 #include "perfetto/base/logging.h"
29 #include "perfetto/base/status.h"
30 #include "perfetto/ext/base/status_or.h"
31 #include "perfetto/trace_processor/basic_types.h"
32 #include "src/trace_processor/db/column/types.h"
33 #include "src/trace_processor/db/column_storage.h"
34 #include "src/trace_processor/db/table.h"
35 #include "src/trace_processor/db/typed_column.h"
36 #include "src/trace_processor/perfetto_sql/intrinsics/table_functions/tables_py.h"
37 #include "src/trace_processor/storage/trace_storage.h"
38 #include "src/trace_processor/tables/slice_tables_py.h"
39 #include "src/trace_processor/types/trace_processor_context.h"
40 #include "src/trace_processor/util/status_macros.h"
41
42 namespace perfetto::trace_processor {
43 namespace tables {
44
45 DescendantSliceTable::~DescendantSliceTable() = default;
46 DescendantSliceByStackTable::~DescendantSliceByStackTable() = default;
47
48 } // namespace tables
49
50 namespace {
51
52 template <typename ChildTable, typename ParentTable, typename ConstraintType>
ExtendWithStartId(ConstraintType constraint_id,const ParentTable & table,std::vector<typename ParentTable::RowNumber> parent_rows)53 std::unique_ptr<Table> ExtendWithStartId(
54 ConstraintType constraint_id,
55 const ParentTable& table,
56 std::vector<typename ParentTable::RowNumber> parent_rows) {
57 ColumnStorage<ConstraintType> start_ids;
58 for (uint32_t i = 0; i < parent_rows.size(); ++i)
59 start_ids.Append(constraint_id);
60 return ChildTable::SelectAndExtendParent(table, std::move(parent_rows),
61 std::move(start_ids));
62 }
63
GetDescendants(const tables::SliceTable & slices,SliceId starting_id,std::vector<tables::SliceTable::RowNumber> & row_numbers_accumulator)64 base::Status GetDescendants(
65 const tables::SliceTable& slices,
66 SliceId starting_id,
67 std::vector<tables::SliceTable::RowNumber>& row_numbers_accumulator) {
68 auto start_ref = slices.FindById(starting_id);
69 // The query gave an invalid ID that doesn't exist in the slice table.
70 if (!start_ref) {
71 return base::ErrStatus("no row with id %" PRIu32 "",
72 static_cast<uint32_t>(starting_id.value));
73 }
74
75 // As an optimization, for any finished slices, we only need to consider
76 // slices which started before the end of this slice (because slices on a
77 // track are always perfectly stacked).
78 // For unfinshed slices (i.e. -1 dur), we need to consider until the end of
79 // the trace so we cannot add any similar constraint.
80 Query q;
81 if (start_ref->dur() >= 0) {
82 q.constraints.emplace_back(
83 slices.ts().le(start_ref->ts() + start_ref->dur()));
84 }
85
86 // All nested descendents must be on the same track, with a ts greater than
87 // |start_ref.ts| and whose depth is larger than |start_ref|'s.
88 q.constraints.emplace_back(slices.ts().ge(start_ref->ts()));
89 q.constraints.emplace_back(slices.track_id().eq(start_ref->track_id().value));
90 q.constraints.emplace_back(slices.depth().gt(start_ref->depth()));
91
92 // It's important we insert directly into |row_numbers_accumulator| and not
93 // overwrite it because we expect the existing elements in
94 // |row_numbers_accumulator| to be preserved.
95
96 for (auto it = slices.FilterToIterator(q); it; ++it) {
97 row_numbers_accumulator.emplace_back(it.row_number());
98 }
99 return base::OkStatus();
100 }
101
102 } // namespace
103
Descendant(Type type,const TraceStorage * storage)104 Descendant::Descendant(Type type, const TraceStorage* storage)
105 : type_(type), storage_(storage) {}
106
ComputeTable(const std::vector<SqlValue> & arguments)107 base::StatusOr<std::unique_ptr<Table>> Descendant::ComputeTable(
108 const std::vector<SqlValue>& arguments) {
109 PERFETTO_CHECK(arguments.size() == 1);
110
111 const auto& slices = storage_->slice_table();
112 if (arguments[0].type == SqlValue::Type::kNull) {
113 // Nothing matches a null id so return an empty table.
114 switch (type_) {
115 case Type::kSlice:
116 return std::unique_ptr<Table>(
117 tables::DescendantSliceTable::SelectAndExtendParent(
118 storage_->slice_table(), {}, {}));
119 case Type::kSliceByStack:
120 return std::unique_ptr<Table>(
121 tables::DescendantSliceByStackTable::SelectAndExtendParent(
122 storage_->slice_table(), {}, {}));
123 }
124 PERFETTO_FATAL("For GCC");
125 }
126 if (arguments[0].type != SqlValue::Type::kLong) {
127 return base::ErrStatus("start id should be an integer.");
128 }
129
130 int64_t start_id = arguments[0].AsLong();
131 std::vector<tables::SliceTable::RowNumber> descendants;
132 switch (type_) {
133 case Type::kSlice: {
134 // Build up all the children row ids.
135 uint32_t start_id_uint = static_cast<uint32_t>(start_id);
136 RETURN_IF_ERROR(GetDescendants(
137 slices, tables::SliceTable::Id(start_id_uint), descendants));
138 return ExtendWithStartId<tables::DescendantSliceTable>(
139 start_id_uint, slices, std::move(descendants));
140 }
141 case Type::kSliceByStack: {
142 Query q;
143 q.constraints = {slices.stack_id().eq(start_id)};
144 for (auto it = slices.FilterToIterator(q); it; ++it) {
145 RETURN_IF_ERROR(GetDescendants(slices, it.id(), descendants));
146 }
147 return ExtendWithStartId<tables::DescendantSliceByStackTable>(
148 start_id, slices, std::move(descendants));
149 }
150 }
151 PERFETTO_FATAL("For GCC");
152 }
153
CreateSchema()154 Table::Schema Descendant::CreateSchema() {
155 switch (type_) {
156 case Type::kSlice:
157 return tables::DescendantSliceTable::ComputeStaticSchema();
158 case Type::kSliceByStack:
159 return tables::DescendantSliceByStackTable::ComputeStaticSchema();
160 }
161 PERFETTO_FATAL("For GCC");
162 }
163
TableName()164 std::string Descendant::TableName() {
165 switch (type_) {
166 case Type::kSlice:
167 return tables::DescendantSliceTable::Name();
168 case Type::kSliceByStack:
169 return tables::DescendantSliceByStackTable::Name();
170 }
171 PERFETTO_FATAL("For GCC");
172 }
173
EstimateRowCount()174 uint32_t Descendant::EstimateRowCount() {
175 return 1;
176 }
177
178 // static
179 std::optional<std::vector<tables::SliceTable::RowNumber>>
GetDescendantSlices(const tables::SliceTable & slices,SliceId slice_id)180 Descendant::GetDescendantSlices(const tables::SliceTable& slices,
181 SliceId slice_id) {
182 std::vector<tables::SliceTable::RowNumber> ret;
183 auto status = GetDescendants(slices, slice_id, ret);
184 if (!status.ok())
185 return std::nullopt;
186 return std::move(ret);
187 }
188
189 } // namespace perfetto::trace_processor
190