1 #ifndef SRC_TRACE_PROCESSOR_TABLES_TRACE_PROTO_TABLES_PY_H_
2 #define SRC_TRACE_PROCESSOR_TABLES_TRACE_PROTO_TABLES_PY_H_
3 
4 #include <array>
5 #include <cstddef>
6 #include <cstdint>
7 #include <memory>
8 #include <optional>
9 #include <type_traits>
10 #include <utility>
11 #include <vector>
12 
13 #include "perfetto/base/logging.h"
14 #include "perfetto/trace_processor/basic_types.h"
15 #include "perfetto/trace_processor/ref_counted.h"
16 #include "src/trace_processor/containers/bit_vector.h"
17 #include "src/trace_processor/containers/row_map.h"
18 #include "src/trace_processor/containers/string_pool.h"
19 #include "src/trace_processor/db/column/arrangement_overlay.h"
20 #include "src/trace_processor/db/column/data_layer.h"
21 #include "src/trace_processor/db/column/dense_null_overlay.h"
22 #include "src/trace_processor/db/column/numeric_storage.h"
23 #include "src/trace_processor/db/column/id_storage.h"
24 #include "src/trace_processor/db/column/null_overlay.h"
25 #include "src/trace_processor/db/column/range_overlay.h"
26 #include "src/trace_processor/db/column/selector_overlay.h"
27 #include "src/trace_processor/db/column/set_id_storage.h"
28 #include "src/trace_processor/db/column/string_storage.h"
29 #include "src/trace_processor/db/column/types.h"
30 #include "src/trace_processor/db/column_storage.h"
31 #include "src/trace_processor/db/column.h"
32 #include "src/trace_processor/db/table.h"
33 #include "src/trace_processor/db/typed_column.h"
34 #include "src/trace_processor/db/typed_column_internal.h"
35 #include "src/trace_processor/tables/macros_internal.h"
36 
37 
38 
39 namespace perfetto::trace_processor::tables {
40 
41 class ExperimentalProtoPathTable : public macros_internal::MacroTable {
42  public:
43   static constexpr uint32_t kColumnCount = 6;
44 
45   struct Id : public BaseId {
46     Id() = default;
IdId47     explicit constexpr Id(uint32_t v) : BaseId(v) {}
48   };
49   static_assert(std::is_trivially_destructible_v<Id>,
50                 "Inheritance used without trivial destruction");
51 
52   struct ColumnIndex {
53     static constexpr uint32_t id = 0;
54     static constexpr uint32_t type = 1;
55     static constexpr uint32_t parent_id = 2;
56     static constexpr uint32_t field_type = 3;
57     static constexpr uint32_t field_name = 4;
58     static constexpr uint32_t arg_set_id = 5;
59   };
60   struct ColumnType {
61     using id = IdColumn<ExperimentalProtoPathTable::Id>;
62     using type = TypedColumn<StringPool::Id>;
63     using parent_id = TypedColumn<std::optional<ExperimentalProtoPathTable::Id>>;
64     using field_type = TypedColumn<StringPool::Id>;
65     using field_name = TypedColumn<std::optional<StringPool::Id>>;
66     using arg_set_id = TypedColumn<std::optional<uint32_t>>;
67   };
68   struct Row : public macros_internal::RootParentTable::Row {
69     Row(std::optional<ExperimentalProtoPathTable::Id> in_parent_id = {},
70         StringPool::Id in_field_type = {},
71         std::optional<StringPool::Id> in_field_name = {},
72         std::optional<uint32_t> in_arg_set_id = {},
73         std::nullptr_t = nullptr)
RowRow74         : macros_internal::RootParentTable::Row(),
75           parent_id(in_parent_id),
76           field_type(in_field_type),
77           field_name(in_field_name),
78           arg_set_id(in_arg_set_id) {
79       type_ = "experimental_proto_path";
80     }
81     std::optional<ExperimentalProtoPathTable::Id> parent_id;
82     StringPool::Id field_type;
83     std::optional<StringPool::Id> field_name;
84     std::optional<uint32_t> arg_set_id;
85 
86     bool operator==(const ExperimentalProtoPathTable::Row& other) const {
87       return type() == other.type() && ColumnType::parent_id::Equals(parent_id, other.parent_id) &&
88        ColumnType::field_type::Equals(field_type, other.field_type) &&
89        ColumnType::field_name::Equals(field_name, other.field_name) &&
90        ColumnType::arg_set_id::Equals(arg_set_id, other.arg_set_id);
91     }
92   };
93   struct ColumnFlag {
94     static constexpr uint32_t parent_id = ColumnType::parent_id::default_flags();
95     static constexpr uint32_t field_type = ColumnType::field_type::default_flags();
96     static constexpr uint32_t field_name = ColumnType::field_name::default_flags();
97     static constexpr uint32_t arg_set_id = ColumnType::arg_set_id::default_flags();
98   };
99 
100   class RowNumber;
101   class ConstRowReference;
102   class RowReference;
103 
104   class RowNumber : public macros_internal::AbstractRowNumber<
105       ExperimentalProtoPathTable, ConstRowReference, RowReference> {
106    public:
RowNumber(uint32_t row_number)107     explicit RowNumber(uint32_t row_number)
108         : AbstractRowNumber(row_number) {}
109   };
110   static_assert(std::is_trivially_destructible_v<RowNumber>,
111                 "Inheritance used without trivial destruction");
112 
113   class ConstRowReference : public macros_internal::AbstractConstRowReference<
114     ExperimentalProtoPathTable, RowNumber> {
115    public:
ConstRowReference(const ExperimentalProtoPathTable * table,uint32_t row_number)116     ConstRowReference(const ExperimentalProtoPathTable* table, uint32_t row_number)
117         : AbstractConstRowReference(table, row_number) {}
118 
id()119     ColumnType::id::type id() const {
120       return table()->id()[row_number_];
121     }
type()122     ColumnType::type::type type() const {
123       return table()->type()[row_number_];
124     }
parent_id()125     ColumnType::parent_id::type parent_id() const {
126       return table()->parent_id()[row_number_];
127     }
field_type()128     ColumnType::field_type::type field_type() const {
129       return table()->field_type()[row_number_];
130     }
field_name()131     ColumnType::field_name::type field_name() const {
132       return table()->field_name()[row_number_];
133     }
arg_set_id()134     ColumnType::arg_set_id::type arg_set_id() const {
135       return table()->arg_set_id()[row_number_];
136     }
137   };
138   static_assert(std::is_trivially_destructible_v<ConstRowReference>,
139                 "Inheritance used without trivial destruction");
140   class RowReference : public ConstRowReference {
141    public:
RowReference(const ExperimentalProtoPathTable * table,uint32_t row_number)142     RowReference(const ExperimentalProtoPathTable* table, uint32_t row_number)
143         : ConstRowReference(table, row_number) {}
144 
set_parent_id(ColumnType::parent_id::non_optional_type v)145     void set_parent_id(
146         ColumnType::parent_id::non_optional_type v) {
147       return mutable_table()->mutable_parent_id()->Set(row_number_, v);
148     }
set_field_type(ColumnType::field_type::non_optional_type v)149     void set_field_type(
150         ColumnType::field_type::non_optional_type v) {
151       return mutable_table()->mutable_field_type()->Set(row_number_, v);
152     }
set_field_name(ColumnType::field_name::non_optional_type v)153     void set_field_name(
154         ColumnType::field_name::non_optional_type v) {
155       return mutable_table()->mutable_field_name()->Set(row_number_, v);
156     }
set_arg_set_id(ColumnType::arg_set_id::non_optional_type v)157     void set_arg_set_id(
158         ColumnType::arg_set_id::non_optional_type v) {
159       return mutable_table()->mutable_arg_set_id()->Set(row_number_, v);
160     }
161 
162    private:
mutable_table()163     ExperimentalProtoPathTable* mutable_table() const {
164       return const_cast<ExperimentalProtoPathTable*>(table());
165     }
166   };
167   static_assert(std::is_trivially_destructible_v<RowReference>,
168                 "Inheritance used without trivial destruction");
169 
170   class ConstIterator;
171   class ConstIterator : public macros_internal::AbstractConstIterator<
172     ConstIterator, ExperimentalProtoPathTable, RowNumber, ConstRowReference> {
173    public:
id()174     ColumnType::id::type id() const {
175       const auto& col = table()->id();
176       return col.GetAtIdx(
177         iterator_.StorageIndexForColumn(col.index_in_table()));
178     }
type()179     ColumnType::type::type type() const {
180       const auto& col = table()->type();
181       return col.GetAtIdx(
182         iterator_.StorageIndexForColumn(col.index_in_table()));
183     }
parent_id()184     ColumnType::parent_id::type parent_id() const {
185       const auto& col = table()->parent_id();
186       return col.GetAtIdx(
187         iterator_.StorageIndexForColumn(col.index_in_table()));
188     }
field_type()189     ColumnType::field_type::type field_type() const {
190       const auto& col = table()->field_type();
191       return col.GetAtIdx(
192         iterator_.StorageIndexForColumn(col.index_in_table()));
193     }
field_name()194     ColumnType::field_name::type field_name() const {
195       const auto& col = table()->field_name();
196       return col.GetAtIdx(
197         iterator_.StorageIndexForColumn(col.index_in_table()));
198     }
arg_set_id()199     ColumnType::arg_set_id::type arg_set_id() const {
200       const auto& col = table()->arg_set_id();
201       return col.GetAtIdx(
202         iterator_.StorageIndexForColumn(col.index_in_table()));
203     }
204 
205    protected:
ConstIterator(const ExperimentalProtoPathTable * table,Table::Iterator iterator)206     explicit ConstIterator(const ExperimentalProtoPathTable* table,
207                            Table::Iterator iterator)
208         : AbstractConstIterator(table, std::move(iterator)) {}
209 
CurrentRowNumber()210     uint32_t CurrentRowNumber() const {
211       return iterator_.StorageIndexForLastOverlay();
212     }
213 
214    private:
215     friend class ExperimentalProtoPathTable;
216     friend class macros_internal::AbstractConstIterator<
217       ConstIterator, ExperimentalProtoPathTable, RowNumber, ConstRowReference>;
218   };
219   class Iterator : public ConstIterator {
220     public:
row_reference()221      RowReference row_reference() const {
222        return {const_cast<ExperimentalProtoPathTable*>(table()), CurrentRowNumber()};
223      }
224 
225     private:
226      friend class ExperimentalProtoPathTable;
227 
Iterator(ExperimentalProtoPathTable * table,Table::Iterator iterator)228      explicit Iterator(ExperimentalProtoPathTable* table, Table::Iterator iterator)
229         : ConstIterator(table, std::move(iterator)) {}
230   };
231 
232   struct IdAndRow {
233     Id id;
234     uint32_t row;
235     RowReference row_reference;
236     RowNumber row_number;
237   };
238 
GetColumns(ExperimentalProtoPathTable * self,const macros_internal::MacroTable * parent)239   static std::vector<ColumnLegacy> GetColumns(
240       ExperimentalProtoPathTable* self,
241       const macros_internal::MacroTable* parent) {
242     std::vector<ColumnLegacy> columns =
243         CopyColumnsFromParentOrAddRootColumns(self, parent);
244     uint32_t olay_idx = OverlayCount(parent);
245     AddColumnToVector(columns, "parent_id", &self->parent_id_, ColumnFlag::parent_id,
246                       static_cast<uint32_t>(columns.size()), olay_idx);
247     AddColumnToVector(columns, "field_type", &self->field_type_, ColumnFlag::field_type,
248                       static_cast<uint32_t>(columns.size()), olay_idx);
249     AddColumnToVector(columns, "field_name", &self->field_name_, ColumnFlag::field_name,
250                       static_cast<uint32_t>(columns.size()), olay_idx);
251     AddColumnToVector(columns, "arg_set_id", &self->arg_set_id_, ColumnFlag::arg_set_id,
252                       static_cast<uint32_t>(columns.size()), olay_idx);
253     return columns;
254   }
255 
ExperimentalProtoPathTable(StringPool * pool)256   PERFETTO_NO_INLINE explicit ExperimentalProtoPathTable(StringPool* pool)
257       : macros_internal::MacroTable(
258           pool,
259           GetColumns(this, nullptr),
260           nullptr),
261         parent_id_(ColumnStorage<ColumnType::parent_id::stored_type>::Create<false>()),
262         field_type_(ColumnStorage<ColumnType::field_type::stored_type>::Create<false>()),
263         field_name_(ColumnStorage<ColumnType::field_name::stored_type>::Create<false>()),
264         arg_set_id_(ColumnStorage<ColumnType::arg_set_id::stored_type>::Create<false>())
265 ,
266         id_storage_layer_(new column::IdStorage()),
267         type_storage_layer_(
268           new column::StringStorage(string_pool(), &type_.vector())),
269         parent_id_storage_layer_(
270           new column::NumericStorage<ColumnType::parent_id::non_optional_stored_type>(
271             &parent_id_.non_null_vector(),
272             ColumnTypeHelper<ColumnType::parent_id::stored_type>::ToColumnType(),
273             false)),
274         field_type_storage_layer_(
275           new column::StringStorage(string_pool(), &field_type_.vector())),
276         field_name_storage_layer_(
277           new column::StringStorage(string_pool(), &field_name_.vector())),
278         arg_set_id_storage_layer_(
279           new column::NumericStorage<ColumnType::arg_set_id::non_optional_stored_type>(
280             &arg_set_id_.non_null_vector(),
281             ColumnTypeHelper<ColumnType::arg_set_id::stored_type>::ToColumnType(),
282             false))
283 ,
284         parent_id_null_layer_(new column::NullOverlay(parent_id_.bv())),
285         arg_set_id_null_layer_(new column::NullOverlay(arg_set_id_.bv())) {
286     static_assert(
287         ColumnLegacy::IsFlagsAndTypeValid<ColumnType::parent_id::stored_type>(
288           ColumnFlag::parent_id),
289         "Column type and flag combination is not valid");
290       static_assert(
291         ColumnLegacy::IsFlagsAndTypeValid<ColumnType::field_type::stored_type>(
292           ColumnFlag::field_type),
293         "Column type and flag combination is not valid");
294       static_assert(
295         ColumnLegacy::IsFlagsAndTypeValid<ColumnType::field_name::stored_type>(
296           ColumnFlag::field_name),
297         "Column type and flag combination is not valid");
298       static_assert(
299         ColumnLegacy::IsFlagsAndTypeValid<ColumnType::arg_set_id::stored_type>(
300           ColumnFlag::arg_set_id),
301         "Column type and flag combination is not valid");
302     OnConstructionCompletedRegularConstructor(
303       {id_storage_layer_,type_storage_layer_,parent_id_storage_layer_,field_type_storage_layer_,field_name_storage_layer_,arg_set_id_storage_layer_},
304       {{},{},parent_id_null_layer_,{},{},arg_set_id_null_layer_});
305   }
306   ~ExperimentalProtoPathTable() override;
307 
Name()308   static const char* Name() { return "experimental_proto_path"; }
309 
ComputeStaticSchema()310   static Table::Schema ComputeStaticSchema() {
311     Table::Schema schema;
312     schema.columns.emplace_back(Table::Schema::Column{
313         "id", SqlValue::Type::kLong, true, true, false, false});
314     schema.columns.emplace_back(Table::Schema::Column{
315         "type", SqlValue::Type::kString, false, false, false, false});
316     schema.columns.emplace_back(Table::Schema::Column{
317         "parent_id", ColumnType::parent_id::SqlValueType(), false,
318         false,
319         false,
320         false});
321     schema.columns.emplace_back(Table::Schema::Column{
322         "field_type", ColumnType::field_type::SqlValueType(), false,
323         false,
324         false,
325         false});
326     schema.columns.emplace_back(Table::Schema::Column{
327         "field_name", ColumnType::field_name::SqlValueType(), false,
328         false,
329         false,
330         false});
331     schema.columns.emplace_back(Table::Schema::Column{
332         "arg_set_id", ColumnType::arg_set_id::SqlValueType(), false,
333         false,
334         false,
335         false});
336     return schema;
337   }
338 
IterateRows()339   ConstIterator IterateRows() const {
340     return ConstIterator(this, Table::IterateRows());
341   }
342 
IterateRows()343   Iterator IterateRows() { return Iterator(this, Table::IterateRows()); }
344 
FilterToIterator(const Query & q)345   ConstIterator FilterToIterator(const Query& q) const {
346     return ConstIterator(this, QueryToIterator(q));
347   }
348 
FilterToIterator(const Query & q)349   Iterator FilterToIterator(const Query& q) {
350     return Iterator(this, QueryToIterator(q));
351   }
352 
ShrinkToFit()353   void ShrinkToFit() {
354     type_.ShrinkToFit();
355     parent_id_.ShrinkToFit();
356     field_type_.ShrinkToFit();
357     field_name_.ShrinkToFit();
358     arg_set_id_.ShrinkToFit();
359   }
360 
361   ConstRowReference operator[](uint32_t r) const {
362     return ConstRowReference(this, r);
363   }
364   RowReference operator[](uint32_t r) { return RowReference(this, r); }
365   ConstRowReference operator[](RowNumber r) const {
366     return ConstRowReference(this, r.row_number());
367   }
368   RowReference operator[](RowNumber r) {
369     return RowReference(this, r.row_number());
370   }
371 
FindById(Id find_id)372   std::optional<ConstRowReference> FindById(Id find_id) const {
373     std::optional<uint32_t> row = id().IndexOf(find_id);
374     return row ? std::make_optional(ConstRowReference(this, *row))
375                : std::nullopt;
376   }
377 
FindById(Id find_id)378   std::optional<RowReference> FindById(Id find_id) {
379     std::optional<uint32_t> row = id().IndexOf(find_id);
380     return row ? std::make_optional(RowReference(this, *row)) : std::nullopt;
381   }
382 
Insert(const Row & row)383   IdAndRow Insert(const Row& row) {
384     uint32_t row_number = row_count();
385     Id id = Id{row_number};
386     type_.Append(string_pool()->InternString(row.type()));
387     mutable_parent_id()->Append(row.parent_id);
388     mutable_field_type()->Append(row.field_type);
389     mutable_field_name()->Append(row.field_name);
390     mutable_arg_set_id()->Append(row.arg_set_id);
391     UpdateSelfOverlayAfterInsert();
392     return IdAndRow{id, row_number, RowReference(this, row_number),
393                      RowNumber(row_number)};
394   }
395 
396 
397 
id()398   const IdColumn<ExperimentalProtoPathTable::Id>& id() const {
399     return static_cast<const ColumnType::id&>(columns()[ColumnIndex::id]);
400   }
type()401   const TypedColumn<StringPool::Id>& type() const {
402     return static_cast<const ColumnType::type&>(columns()[ColumnIndex::type]);
403   }
parent_id()404   const TypedColumn<std::optional<ExperimentalProtoPathTable::Id>>& parent_id() const {
405     return static_cast<const ColumnType::parent_id&>(columns()[ColumnIndex::parent_id]);
406   }
field_type()407   const TypedColumn<StringPool::Id>& field_type() const {
408     return static_cast<const ColumnType::field_type&>(columns()[ColumnIndex::field_type]);
409   }
field_name()410   const TypedColumn<std::optional<StringPool::Id>>& field_name() const {
411     return static_cast<const ColumnType::field_name&>(columns()[ColumnIndex::field_name]);
412   }
arg_set_id()413   const TypedColumn<std::optional<uint32_t>>& arg_set_id() const {
414     return static_cast<const ColumnType::arg_set_id&>(columns()[ColumnIndex::arg_set_id]);
415   }
416 
mutable_parent_id()417   TypedColumn<std::optional<ExperimentalProtoPathTable::Id>>* mutable_parent_id() {
418     return static_cast<ColumnType::parent_id*>(
419         GetColumn(ColumnIndex::parent_id));
420   }
mutable_field_type()421   TypedColumn<StringPool::Id>* mutable_field_type() {
422     return static_cast<ColumnType::field_type*>(
423         GetColumn(ColumnIndex::field_type));
424   }
mutable_field_name()425   TypedColumn<std::optional<StringPool::Id>>* mutable_field_name() {
426     return static_cast<ColumnType::field_name*>(
427         GetColumn(ColumnIndex::field_name));
428   }
mutable_arg_set_id()429   TypedColumn<std::optional<uint32_t>>* mutable_arg_set_id() {
430     return static_cast<ColumnType::arg_set_id*>(
431         GetColumn(ColumnIndex::arg_set_id));
432   }
433 
434  private:
435 
436 
437   ColumnStorage<ColumnType::parent_id::stored_type> parent_id_;
438   ColumnStorage<ColumnType::field_type::stored_type> field_type_;
439   ColumnStorage<ColumnType::field_name::stored_type> field_name_;
440   ColumnStorage<ColumnType::arg_set_id::stored_type> arg_set_id_;
441 
442   RefPtr<column::StorageLayer> id_storage_layer_;
443   RefPtr<column::StorageLayer> type_storage_layer_;
444   RefPtr<column::StorageLayer> parent_id_storage_layer_;
445   RefPtr<column::StorageLayer> field_type_storage_layer_;
446   RefPtr<column::StorageLayer> field_name_storage_layer_;
447   RefPtr<column::StorageLayer> arg_set_id_storage_layer_;
448 
449   RefPtr<column::OverlayLayer> parent_id_null_layer_;
450   RefPtr<column::OverlayLayer> arg_set_id_null_layer_;
451 };
452 
453 
454 class ExperimentalProtoContentTable : public macros_internal::MacroTable {
455  public:
456   static constexpr uint32_t kColumnCount = 7;
457 
458   struct Id : public BaseId {
459     Id() = default;
IdId460     explicit constexpr Id(uint32_t v) : BaseId(v) {}
461   };
462   static_assert(std::is_trivially_destructible_v<Id>,
463                 "Inheritance used without trivial destruction");
464 
465   struct ColumnIndex {
466     static constexpr uint32_t id = 0;
467     static constexpr uint32_t type = 1;
468     static constexpr uint32_t path = 2;
469     static constexpr uint32_t path_id = 3;
470     static constexpr uint32_t total_size = 4;
471     static constexpr uint32_t size = 5;
472     static constexpr uint32_t count = 6;
473   };
474   struct ColumnType {
475     using id = IdColumn<ExperimentalProtoContentTable::Id>;
476     using type = TypedColumn<StringPool::Id>;
477     using path = TypedColumn<StringPool::Id>;
478     using path_id = TypedColumn<ExperimentalProtoPathTable::Id>;
479     using total_size = TypedColumn<int64_t>;
480     using size = TypedColumn<int64_t>;
481     using count = TypedColumn<int64_t>;
482   };
483   struct Row : public macros_internal::RootParentTable::Row {
484     Row(StringPool::Id in_path = {},
485         ExperimentalProtoPathTable::Id in_path_id = {},
486         int64_t in_total_size = {},
487         int64_t in_size = {},
488         int64_t in_count = {},
489         std::nullptr_t = nullptr)
RowRow490         : macros_internal::RootParentTable::Row(),
491           path(in_path),
492           path_id(in_path_id),
493           total_size(in_total_size),
494           size(in_size),
495           count(in_count) {
496       type_ = "experimental_proto_content";
497     }
498     StringPool::Id path;
499     ExperimentalProtoPathTable::Id path_id;
500     int64_t total_size;
501     int64_t size;
502     int64_t count;
503 
504     bool operator==(const ExperimentalProtoContentTable::Row& other) const {
505       return type() == other.type() && ColumnType::path::Equals(path, other.path) &&
506        ColumnType::path_id::Equals(path_id, other.path_id) &&
507        ColumnType::total_size::Equals(total_size, other.total_size) &&
508        ColumnType::size::Equals(size, other.size) &&
509        ColumnType::count::Equals(count, other.count);
510     }
511   };
512   struct ColumnFlag {
513     static constexpr uint32_t path = ColumnType::path::default_flags();
514     static constexpr uint32_t path_id = ColumnType::path_id::default_flags();
515     static constexpr uint32_t total_size = ColumnType::total_size::default_flags();
516     static constexpr uint32_t size = ColumnType::size::default_flags();
517     static constexpr uint32_t count = ColumnType::count::default_flags();
518   };
519 
520   class RowNumber;
521   class ConstRowReference;
522   class RowReference;
523 
524   class RowNumber : public macros_internal::AbstractRowNumber<
525       ExperimentalProtoContentTable, ConstRowReference, RowReference> {
526    public:
RowNumber(uint32_t row_number)527     explicit RowNumber(uint32_t row_number)
528         : AbstractRowNumber(row_number) {}
529   };
530   static_assert(std::is_trivially_destructible_v<RowNumber>,
531                 "Inheritance used without trivial destruction");
532 
533   class ConstRowReference : public macros_internal::AbstractConstRowReference<
534     ExperimentalProtoContentTable, RowNumber> {
535    public:
ConstRowReference(const ExperimentalProtoContentTable * table,uint32_t row_number)536     ConstRowReference(const ExperimentalProtoContentTable* table, uint32_t row_number)
537         : AbstractConstRowReference(table, row_number) {}
538 
id()539     ColumnType::id::type id() const {
540       return table()->id()[row_number_];
541     }
type()542     ColumnType::type::type type() const {
543       return table()->type()[row_number_];
544     }
path()545     ColumnType::path::type path() const {
546       return table()->path()[row_number_];
547     }
path_id()548     ColumnType::path_id::type path_id() const {
549       return table()->path_id()[row_number_];
550     }
total_size()551     ColumnType::total_size::type total_size() const {
552       return table()->total_size()[row_number_];
553     }
size()554     ColumnType::size::type size() const {
555       return table()->size()[row_number_];
556     }
count()557     ColumnType::count::type count() const {
558       return table()->count()[row_number_];
559     }
560   };
561   static_assert(std::is_trivially_destructible_v<ConstRowReference>,
562                 "Inheritance used without trivial destruction");
563   class RowReference : public ConstRowReference {
564    public:
RowReference(const ExperimentalProtoContentTable * table,uint32_t row_number)565     RowReference(const ExperimentalProtoContentTable* table, uint32_t row_number)
566         : ConstRowReference(table, row_number) {}
567 
set_path(ColumnType::path::non_optional_type v)568     void set_path(
569         ColumnType::path::non_optional_type v) {
570       return mutable_table()->mutable_path()->Set(row_number_, v);
571     }
set_path_id(ColumnType::path_id::non_optional_type v)572     void set_path_id(
573         ColumnType::path_id::non_optional_type v) {
574       return mutable_table()->mutable_path_id()->Set(row_number_, v);
575     }
set_total_size(ColumnType::total_size::non_optional_type v)576     void set_total_size(
577         ColumnType::total_size::non_optional_type v) {
578       return mutable_table()->mutable_total_size()->Set(row_number_, v);
579     }
set_size(ColumnType::size::non_optional_type v)580     void set_size(
581         ColumnType::size::non_optional_type v) {
582       return mutable_table()->mutable_size()->Set(row_number_, v);
583     }
set_count(ColumnType::count::non_optional_type v)584     void set_count(
585         ColumnType::count::non_optional_type v) {
586       return mutable_table()->mutable_count()->Set(row_number_, v);
587     }
588 
589    private:
mutable_table()590     ExperimentalProtoContentTable* mutable_table() const {
591       return const_cast<ExperimentalProtoContentTable*>(table());
592     }
593   };
594   static_assert(std::is_trivially_destructible_v<RowReference>,
595                 "Inheritance used without trivial destruction");
596 
597   class ConstIterator;
598   class ConstIterator : public macros_internal::AbstractConstIterator<
599     ConstIterator, ExperimentalProtoContentTable, RowNumber, ConstRowReference> {
600    public:
id()601     ColumnType::id::type id() const {
602       const auto& col = table()->id();
603       return col.GetAtIdx(
604         iterator_.StorageIndexForColumn(col.index_in_table()));
605     }
type()606     ColumnType::type::type type() const {
607       const auto& col = table()->type();
608       return col.GetAtIdx(
609         iterator_.StorageIndexForColumn(col.index_in_table()));
610     }
path()611     ColumnType::path::type path() const {
612       const auto& col = table()->path();
613       return col.GetAtIdx(
614         iterator_.StorageIndexForColumn(col.index_in_table()));
615     }
path_id()616     ColumnType::path_id::type path_id() const {
617       const auto& col = table()->path_id();
618       return col.GetAtIdx(
619         iterator_.StorageIndexForColumn(col.index_in_table()));
620     }
total_size()621     ColumnType::total_size::type total_size() const {
622       const auto& col = table()->total_size();
623       return col.GetAtIdx(
624         iterator_.StorageIndexForColumn(col.index_in_table()));
625     }
size()626     ColumnType::size::type size() const {
627       const auto& col = table()->size();
628       return col.GetAtIdx(
629         iterator_.StorageIndexForColumn(col.index_in_table()));
630     }
count()631     ColumnType::count::type count() const {
632       const auto& col = table()->count();
633       return col.GetAtIdx(
634         iterator_.StorageIndexForColumn(col.index_in_table()));
635     }
636 
637    protected:
ConstIterator(const ExperimentalProtoContentTable * table,Table::Iterator iterator)638     explicit ConstIterator(const ExperimentalProtoContentTable* table,
639                            Table::Iterator iterator)
640         : AbstractConstIterator(table, std::move(iterator)) {}
641 
CurrentRowNumber()642     uint32_t CurrentRowNumber() const {
643       return iterator_.StorageIndexForLastOverlay();
644     }
645 
646    private:
647     friend class ExperimentalProtoContentTable;
648     friend class macros_internal::AbstractConstIterator<
649       ConstIterator, ExperimentalProtoContentTable, RowNumber, ConstRowReference>;
650   };
651   class Iterator : public ConstIterator {
652     public:
row_reference()653      RowReference row_reference() const {
654        return {const_cast<ExperimentalProtoContentTable*>(table()), CurrentRowNumber()};
655      }
656 
657     private:
658      friend class ExperimentalProtoContentTable;
659 
Iterator(ExperimentalProtoContentTable * table,Table::Iterator iterator)660      explicit Iterator(ExperimentalProtoContentTable* table, Table::Iterator iterator)
661         : ConstIterator(table, std::move(iterator)) {}
662   };
663 
664   struct IdAndRow {
665     Id id;
666     uint32_t row;
667     RowReference row_reference;
668     RowNumber row_number;
669   };
670 
GetColumns(ExperimentalProtoContentTable * self,const macros_internal::MacroTable * parent)671   static std::vector<ColumnLegacy> GetColumns(
672       ExperimentalProtoContentTable* self,
673       const macros_internal::MacroTable* parent) {
674     std::vector<ColumnLegacy> columns =
675         CopyColumnsFromParentOrAddRootColumns(self, parent);
676     uint32_t olay_idx = OverlayCount(parent);
677     AddColumnToVector(columns, "path", &self->path_, ColumnFlag::path,
678                       static_cast<uint32_t>(columns.size()), olay_idx);
679     AddColumnToVector(columns, "path_id", &self->path_id_, ColumnFlag::path_id,
680                       static_cast<uint32_t>(columns.size()), olay_idx);
681     AddColumnToVector(columns, "total_size", &self->total_size_, ColumnFlag::total_size,
682                       static_cast<uint32_t>(columns.size()), olay_idx);
683     AddColumnToVector(columns, "size", &self->size_, ColumnFlag::size,
684                       static_cast<uint32_t>(columns.size()), olay_idx);
685     AddColumnToVector(columns, "count", &self->count_, ColumnFlag::count,
686                       static_cast<uint32_t>(columns.size()), olay_idx);
687     return columns;
688   }
689 
ExperimentalProtoContentTable(StringPool * pool)690   PERFETTO_NO_INLINE explicit ExperimentalProtoContentTable(StringPool* pool)
691       : macros_internal::MacroTable(
692           pool,
693           GetColumns(this, nullptr),
694           nullptr),
695         path_(ColumnStorage<ColumnType::path::stored_type>::Create<false>()),
696         path_id_(ColumnStorage<ColumnType::path_id::stored_type>::Create<false>()),
697         total_size_(ColumnStorage<ColumnType::total_size::stored_type>::Create<false>()),
698         size_(ColumnStorage<ColumnType::size::stored_type>::Create<false>()),
699         count_(ColumnStorage<ColumnType::count::stored_type>::Create<false>())
700 ,
701         id_storage_layer_(new column::IdStorage()),
702         type_storage_layer_(
703           new column::StringStorage(string_pool(), &type_.vector())),
704         path_storage_layer_(
705           new column::StringStorage(string_pool(), &path_.vector())),
706         path_id_storage_layer_(
707         new column::NumericStorage<ColumnType::path_id::non_optional_stored_type>(
708           &path_id_.vector(),
709           ColumnTypeHelper<ColumnType::path_id::stored_type>::ToColumnType(),
710           false)),
711         total_size_storage_layer_(
712         new column::NumericStorage<ColumnType::total_size::non_optional_stored_type>(
713           &total_size_.vector(),
714           ColumnTypeHelper<ColumnType::total_size::stored_type>::ToColumnType(),
715           false)),
716         size_storage_layer_(
717         new column::NumericStorage<ColumnType::size::non_optional_stored_type>(
718           &size_.vector(),
719           ColumnTypeHelper<ColumnType::size::stored_type>::ToColumnType(),
720           false)),
721         count_storage_layer_(
722         new column::NumericStorage<ColumnType::count::non_optional_stored_type>(
723           &count_.vector(),
724           ColumnTypeHelper<ColumnType::count::stored_type>::ToColumnType(),
725           false))
726          {
727     static_assert(
728         ColumnLegacy::IsFlagsAndTypeValid<ColumnType::path::stored_type>(
729           ColumnFlag::path),
730         "Column type and flag combination is not valid");
731       static_assert(
732         ColumnLegacy::IsFlagsAndTypeValid<ColumnType::path_id::stored_type>(
733           ColumnFlag::path_id),
734         "Column type and flag combination is not valid");
735       static_assert(
736         ColumnLegacy::IsFlagsAndTypeValid<ColumnType::total_size::stored_type>(
737           ColumnFlag::total_size),
738         "Column type and flag combination is not valid");
739       static_assert(
740         ColumnLegacy::IsFlagsAndTypeValid<ColumnType::size::stored_type>(
741           ColumnFlag::size),
742         "Column type and flag combination is not valid");
743       static_assert(
744         ColumnLegacy::IsFlagsAndTypeValid<ColumnType::count::stored_type>(
745           ColumnFlag::count),
746         "Column type and flag combination is not valid");
747     OnConstructionCompletedRegularConstructor(
748       {id_storage_layer_,type_storage_layer_,path_storage_layer_,path_id_storage_layer_,total_size_storage_layer_,size_storage_layer_,count_storage_layer_},
749       {{},{},{},{},{},{},{}});
750   }
751   ~ExperimentalProtoContentTable() override;
752 
Name()753   static const char* Name() { return "experimental_proto_content"; }
754 
ComputeStaticSchema()755   static Table::Schema ComputeStaticSchema() {
756     Table::Schema schema;
757     schema.columns.emplace_back(Table::Schema::Column{
758         "id", SqlValue::Type::kLong, true, true, false, false});
759     schema.columns.emplace_back(Table::Schema::Column{
760         "type", SqlValue::Type::kString, false, false, false, false});
761     schema.columns.emplace_back(Table::Schema::Column{
762         "path", ColumnType::path::SqlValueType(), false,
763         false,
764         false,
765         false});
766     schema.columns.emplace_back(Table::Schema::Column{
767         "path_id", ColumnType::path_id::SqlValueType(), false,
768         false,
769         false,
770         false});
771     schema.columns.emplace_back(Table::Schema::Column{
772         "total_size", ColumnType::total_size::SqlValueType(), false,
773         false,
774         false,
775         false});
776     schema.columns.emplace_back(Table::Schema::Column{
777         "size", ColumnType::size::SqlValueType(), false,
778         false,
779         false,
780         false});
781     schema.columns.emplace_back(Table::Schema::Column{
782         "count", ColumnType::count::SqlValueType(), false,
783         false,
784         false,
785         false});
786     return schema;
787   }
788 
IterateRows()789   ConstIterator IterateRows() const {
790     return ConstIterator(this, Table::IterateRows());
791   }
792 
IterateRows()793   Iterator IterateRows() { return Iterator(this, Table::IterateRows()); }
794 
FilterToIterator(const Query & q)795   ConstIterator FilterToIterator(const Query& q) const {
796     return ConstIterator(this, QueryToIterator(q));
797   }
798 
FilterToIterator(const Query & q)799   Iterator FilterToIterator(const Query& q) {
800     return Iterator(this, QueryToIterator(q));
801   }
802 
ShrinkToFit()803   void ShrinkToFit() {
804     type_.ShrinkToFit();
805     path_.ShrinkToFit();
806     path_id_.ShrinkToFit();
807     total_size_.ShrinkToFit();
808     size_.ShrinkToFit();
809     count_.ShrinkToFit();
810   }
811 
812   ConstRowReference operator[](uint32_t r) const {
813     return ConstRowReference(this, r);
814   }
815   RowReference operator[](uint32_t r) { return RowReference(this, r); }
816   ConstRowReference operator[](RowNumber r) const {
817     return ConstRowReference(this, r.row_number());
818   }
819   RowReference operator[](RowNumber r) {
820     return RowReference(this, r.row_number());
821   }
822 
FindById(Id find_id)823   std::optional<ConstRowReference> FindById(Id find_id) const {
824     std::optional<uint32_t> row = id().IndexOf(find_id);
825     return row ? std::make_optional(ConstRowReference(this, *row))
826                : std::nullopt;
827   }
828 
FindById(Id find_id)829   std::optional<RowReference> FindById(Id find_id) {
830     std::optional<uint32_t> row = id().IndexOf(find_id);
831     return row ? std::make_optional(RowReference(this, *row)) : std::nullopt;
832   }
833 
Insert(const Row & row)834   IdAndRow Insert(const Row& row) {
835     uint32_t row_number = row_count();
836     Id id = Id{row_number};
837     type_.Append(string_pool()->InternString(row.type()));
838     mutable_path()->Append(row.path);
839     mutable_path_id()->Append(row.path_id);
840     mutable_total_size()->Append(row.total_size);
841     mutable_size()->Append(row.size);
842     mutable_count()->Append(row.count);
843     UpdateSelfOverlayAfterInsert();
844     return IdAndRow{id, row_number, RowReference(this, row_number),
845                      RowNumber(row_number)};
846   }
847 
848 
849 
id()850   const IdColumn<ExperimentalProtoContentTable::Id>& id() const {
851     return static_cast<const ColumnType::id&>(columns()[ColumnIndex::id]);
852   }
type()853   const TypedColumn<StringPool::Id>& type() const {
854     return static_cast<const ColumnType::type&>(columns()[ColumnIndex::type]);
855   }
path()856   const TypedColumn<StringPool::Id>& path() const {
857     return static_cast<const ColumnType::path&>(columns()[ColumnIndex::path]);
858   }
path_id()859   const TypedColumn<ExperimentalProtoPathTable::Id>& path_id() const {
860     return static_cast<const ColumnType::path_id&>(columns()[ColumnIndex::path_id]);
861   }
total_size()862   const TypedColumn<int64_t>& total_size() const {
863     return static_cast<const ColumnType::total_size&>(columns()[ColumnIndex::total_size]);
864   }
size()865   const TypedColumn<int64_t>& size() const {
866     return static_cast<const ColumnType::size&>(columns()[ColumnIndex::size]);
867   }
count()868   const TypedColumn<int64_t>& count() const {
869     return static_cast<const ColumnType::count&>(columns()[ColumnIndex::count]);
870   }
871 
mutable_path()872   TypedColumn<StringPool::Id>* mutable_path() {
873     return static_cast<ColumnType::path*>(
874         GetColumn(ColumnIndex::path));
875   }
mutable_path_id()876   TypedColumn<ExperimentalProtoPathTable::Id>* mutable_path_id() {
877     return static_cast<ColumnType::path_id*>(
878         GetColumn(ColumnIndex::path_id));
879   }
mutable_total_size()880   TypedColumn<int64_t>* mutable_total_size() {
881     return static_cast<ColumnType::total_size*>(
882         GetColumn(ColumnIndex::total_size));
883   }
mutable_size()884   TypedColumn<int64_t>* mutable_size() {
885     return static_cast<ColumnType::size*>(
886         GetColumn(ColumnIndex::size));
887   }
mutable_count()888   TypedColumn<int64_t>* mutable_count() {
889     return static_cast<ColumnType::count*>(
890         GetColumn(ColumnIndex::count));
891   }
892 
893  private:
894 
895 
896   ColumnStorage<ColumnType::path::stored_type> path_;
897   ColumnStorage<ColumnType::path_id::stored_type> path_id_;
898   ColumnStorage<ColumnType::total_size::stored_type> total_size_;
899   ColumnStorage<ColumnType::size::stored_type> size_;
900   ColumnStorage<ColumnType::count::stored_type> count_;
901 
902   RefPtr<column::StorageLayer> id_storage_layer_;
903   RefPtr<column::StorageLayer> type_storage_layer_;
904   RefPtr<column::StorageLayer> path_storage_layer_;
905   RefPtr<column::StorageLayer> path_id_storage_layer_;
906   RefPtr<column::StorageLayer> total_size_storage_layer_;
907   RefPtr<column::StorageLayer> size_storage_layer_;
908   RefPtr<column::StorageLayer> count_storage_layer_;
909 
910 
911 };
912 
913 }  // namespace perfetto
914 
915 #endif  // SRC_TRACE_PROCESSOR_TABLES_TRACE_PROTO_TABLES_PY_H_
916