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 #include "src/trace_processor/tables/macros_internal.h"
18
19 #include <cstdint>
20 #include <initializer_list>
21 #include <type_traits>
22 #include <utility>
23 #include <vector>
24
25 #include "perfetto/base/logging.h"
26 #include "perfetto/public/compiler.h"
27 #include "perfetto/trace_processor/ref_counted.h"
28 #include "src/trace_processor/containers/bit_vector.h"
29 #include "src/trace_processor/containers/row_map.h"
30 #include "src/trace_processor/containers/string_pool.h"
31 #include "src/trace_processor/db/column.h"
32 #include "src/trace_processor/db/column/overlay_layer.h"
33 #include "src/trace_processor/db/column/selector_overlay.h"
34 #include "src/trace_processor/db/column/storage_layer.h"
35 #include "src/trace_processor/db/column_storage_overlay.h"
36
37 namespace perfetto::trace_processor::macros_internal {
38
MacroTable(StringPool * pool,std::vector<ColumnLegacy> columns,const MacroTable * parent)39 PERFETTO_NO_INLINE MacroTable::MacroTable(StringPool* pool,
40 std::vector<ColumnLegacy> columns,
41 const MacroTable* parent)
42 : Table(pool, 0u, std::move(columns), EmptyOverlaysFromParent(parent)),
43 allow_inserts_(true),
44 parent_(parent) {}
45
MacroTable(StringPool * pool,std::vector<ColumnLegacy> columns,const MacroTable & parent,const RowMap & parent_overlay)46 PERFETTO_NO_INLINE MacroTable::MacroTable(StringPool* pool,
47 std::vector<ColumnLegacy> columns,
48 const MacroTable& parent,
49 const RowMap& parent_overlay)
50 : Table(pool,
51 parent_overlay.size(),
52 std::move(columns),
53 SelectedOverlaysFromParent(parent, parent_overlay)),
54 allow_inserts_(false),
55 parent_(&parent) {}
56
UpdateOverlaysAfterParentInsert()57 PERFETTO_NO_INLINE void MacroTable::UpdateOverlaysAfterParentInsert() {
58 CopyLastInsertFrom(parent_->overlays());
59 }
60
UpdateSelfOverlayAfterInsert()61 PERFETTO_NO_INLINE void MacroTable::UpdateSelfOverlayAfterInsert() {
62 IncrementRowCountAndAddToLastOverlay();
63 }
64
65 PERFETTO_NO_INLINE std::vector<ColumnLegacy>
CopyColumnsFromParentOrAddRootColumns(const MacroTable * parent)66 MacroTable::CopyColumnsFromParentOrAddRootColumns(const MacroTable* parent) {
67 std::vector<ColumnLegacy> columns;
68 if (parent) {
69 for (const ColumnLegacy& col : parent->columns()) {
70 columns.emplace_back(col, col.index_in_table(), col.overlay_index());
71 }
72 } else {
73 columns.emplace_back(ColumnLegacy::IdColumn(0, 0));
74 }
75 return columns;
76 }
77
OnConstructionCompletedRegularConstructor(std::initializer_list<RefPtr<column::StorageLayer>> storage_layers,std::initializer_list<RefPtr<column::OverlayLayer>> null_layers)78 PERFETTO_NO_INLINE void MacroTable::OnConstructionCompletedRegularConstructor(
79 std::initializer_list<RefPtr<column::StorageLayer>> storage_layers,
80 std::initializer_list<RefPtr<column::OverlayLayer>> null_layers) {
81 std::vector<RefPtr<column::OverlayLayer>> overlay_layers(
82 OverlayCount(parent_) + 1);
83 for (uint32_t i = 0; i < overlay_layers.size() - 1; ++i) {
84 PERFETTO_CHECK(overlays()[i].row_map().IsBitVector());
85 overlay_layers[i].reset(
86 new column::SelectorOverlay(overlays()[i].row_map().GetIfBitVector()));
87 }
88 Table::OnConstructionCompleted(storage_layers, null_layers,
89 std::move(overlay_layers));
90 }
91
92 PERFETTO_NO_INLINE std::vector<ColumnStorageOverlay>
EmptyOverlaysFromParent(const MacroTable * parent)93 MacroTable::EmptyOverlaysFromParent(const MacroTable* parent) {
94 std::vector<ColumnStorageOverlay> overlays(parent ? parent->overlays().size()
95 : 0);
96 for (auto& overlay : overlays) {
97 overlay = ColumnStorageOverlay(BitVector());
98 }
99 overlays.emplace_back();
100 return overlays;
101 }
102
103 PERFETTO_NO_INLINE std::vector<ColumnStorageOverlay>
SelectedOverlaysFromParent(const macros_internal::MacroTable & parent,const RowMap & rm)104 MacroTable::SelectedOverlaysFromParent(
105 const macros_internal::MacroTable& parent,
106 const RowMap& rm) {
107 std::vector<ColumnStorageOverlay> overlays;
108 for (const auto& overlay : parent.overlays()) {
109 overlays.emplace_back(overlay.SelectRows(rm));
110 PERFETTO_DCHECK(overlays.back().size() == rm.size());
111 }
112 overlays.emplace_back(rm.size());
113 return overlays;
114 }
115
BaseConstIterator(const MacroTable * table,Table::Iterator iterator)116 BaseConstIterator::BaseConstIterator(const MacroTable* table,
117 Table::Iterator iterator)
118 : iterator_(std::move(iterator)), table_(table) {
119 static_assert(std::is_base_of<Table, MacroTable>::value,
120 "Template param should be a subclass of Table.");
121 }
122
operator bool() const123 BaseConstIterator::operator bool() const {
124 return bool(iterator_);
125 }
126
operator ++()127 BaseConstIterator& BaseConstIterator::operator++() {
128 ++iterator_;
129 return *this;
130 }
131
BaseRowReference(const MacroTable * table,uint32_t row_number)132 BaseRowReference::BaseRowReference(const MacroTable* table, uint32_t row_number)
133 : table_(table), row_number_(row_number) {}
134
135 } // namespace perfetto::trace_processor::macros_internal
136