Lalit Maganti | 2d3e71d | 2018-09-24 14:13:27 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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/span_operator_table.h" |
| 18 | |
| 19 | #include <sqlite3.h> |
| 20 | #include <string.h> |
| 21 | #include <algorithm> |
| 22 | #include <set> |
| 23 | |
| 24 | #include "perfetto/base/logging.h" |
Lalit Maganti | d1ee86b | 2018-09-24 17:13:01 +0100 | [diff] [blame] | 25 | #include "src/trace_processor/sqlite_utils.h" |
Lalit Maganti | 2d3e71d | 2018-09-24 14:13:27 +0100 | [diff] [blame] | 26 | |
| 27 | namespace perfetto { |
| 28 | namespace trace_processor { |
| 29 | |
| 30 | namespace { |
| 31 | |
Lalit Maganti | d1ee86b | 2018-09-24 17:13:01 +0100 | [diff] [blame] | 32 | using namespace sqlite_utils; |
| 33 | |
Lalit Maganti | 2d3e71d | 2018-09-24 14:13:27 +0100 | [diff] [blame] | 34 | constexpr uint64_t kU64Max = std::numeric_limits<uint64_t>::max(); |
| 35 | |
Lalit Maganti | a270b65 | 2018-09-26 11:54:33 +0100 | [diff] [blame] | 36 | std::vector<Table::Column> GetColumnsForTable( |
Lalit Maganti | 2d3e71d | 2018-09-24 14:13:27 +0100 | [diff] [blame] | 37 | sqlite3* db, |
| 38 | const std::string& raw_table_name) { |
| 39 | char sql[1024]; |
| 40 | const char kRawSql[] = "SELECT name, type from pragma_table_info(\"%s\")"; |
| 41 | |
| 42 | // Support names which are table valued functions with arguments. |
| 43 | std::string table_name = raw_table_name.substr(0, raw_table_name.find('(')); |
| 44 | int n = snprintf(sql, sizeof(sql), kRawSql, table_name.c_str()); |
| 45 | PERFETTO_DCHECK(n >= 0 || static_cast<size_t>(n) < sizeof(sql)); |
| 46 | |
| 47 | sqlite3_stmt* raw_stmt = nullptr; |
| 48 | int err = sqlite3_prepare_v2(db, sql, n, &raw_stmt, nullptr); |
| 49 | |
| 50 | ScopedStmt stmt(raw_stmt); |
| 51 | PERFETTO_DCHECK(sqlite3_column_count(*stmt) == 2); |
| 52 | |
Lalit Maganti | a270b65 | 2018-09-26 11:54:33 +0100 | [diff] [blame] | 53 | std::vector<Table::Column> columns; |
Lalit Maganti | 2d3e71d | 2018-09-24 14:13:27 +0100 | [diff] [blame] | 54 | while (true) { |
| 55 | err = sqlite3_step(raw_stmt); |
| 56 | if (err == SQLITE_DONE) |
| 57 | break; |
| 58 | if (err != SQLITE_ROW) { |
| 59 | PERFETTO_ELOG("Querying schema of table failed"); |
| 60 | return {}; |
| 61 | } |
| 62 | |
| 63 | const char* name = |
| 64 | reinterpret_cast<const char*>(sqlite3_column_text(*stmt, 0)); |
Lalit Maganti | a270b65 | 2018-09-26 11:54:33 +0100 | [diff] [blame] | 65 | const char* raw_type = |
Lalit Maganti | 2d3e71d | 2018-09-24 14:13:27 +0100 | [diff] [blame] | 66 | reinterpret_cast<const char*>(sqlite3_column_text(*stmt, 1)); |
Lalit Maganti | a270b65 | 2018-09-26 11:54:33 +0100 | [diff] [blame] | 67 | if (!name || !raw_type || !*name || !*raw_type) { |
Lalit Maganti | 2d3e71d | 2018-09-24 14:13:27 +0100 | [diff] [blame] | 68 | PERFETTO_ELOG("Schema has invalid column values"); |
| 69 | return {}; |
| 70 | } |
| 71 | |
Lalit Maganti | a270b65 | 2018-09-26 11:54:33 +0100 | [diff] [blame] | 72 | Table::ColumnType type; |
| 73 | if (strcmp(raw_type, "UNSIGNED BIG INT") == 0) { |
| 74 | type = Table::ColumnType::kUlong; |
| 75 | } else if (strcmp(raw_type, "UNSIGNED INT") == 0) { |
| 76 | type = Table::ColumnType::kUint; |
| 77 | } else if (strcmp(raw_type, "STRING") == 0) { |
| 78 | type = Table::ColumnType::kString; |
Lalit Maganti | 2d3e71d | 2018-09-24 14:13:27 +0100 | [diff] [blame] | 79 | } else { |
| 80 | PERFETTO_FATAL("Unknown column type on table %s", raw_table_name.c_str()); |
| 81 | } |
Lalit Maganti | a270b65 | 2018-09-26 11:54:33 +0100 | [diff] [blame] | 82 | columns.emplace_back(columns.size(), name, type); |
Lalit Maganti | 2d3e71d | 2018-09-24 14:13:27 +0100 | [diff] [blame] | 83 | } |
| 84 | return columns; |
| 85 | } |
| 86 | |
| 87 | } // namespace |
| 88 | |
| 89 | SpanOperatorTable::SpanOperatorTable(sqlite3* db, const TraceStorage*) |
| 90 | : db_(db) {} |
| 91 | |
| 92 | void SpanOperatorTable::RegisterTable(sqlite3* db, |
| 93 | const TraceStorage* storage) { |
| 94 | Table::Register<SpanOperatorTable>(db, storage, "span"); |
| 95 | } |
| 96 | |
Lalit Maganti | a270b65 | 2018-09-26 11:54:33 +0100 | [diff] [blame] | 97 | Table::Schema SpanOperatorTable::CreateSchema(int argc, |
| 98 | const char* const* argv) { |
Lalit Maganti | 2d3e71d | 2018-09-24 14:13:27 +0100 | [diff] [blame] | 99 | // argv[0] - argv[2] are SQLite populated fields which are always present. |
| 100 | if (argc < 6) { |
Lalit Maganti | b29d0cb | 2018-09-24 16:31:44 +0100 | [diff] [blame] | 101 | PERFETTO_ELOG("SPAN JOIN expected at least 3 args, received %d", argc - 3); |
Lalit Maganti | a270b65 | 2018-09-26 11:54:33 +0100 | [diff] [blame] | 102 | return Table::Schema({}, {}); |
Lalit Maganti | 2d3e71d | 2018-09-24 14:13:27 +0100 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | // The order arguments is (t1_name, t2_name, join_col). |
Lalit Maganti | b29d0cb | 2018-09-24 16:31:44 +0100 | [diff] [blame] | 106 | t1_defn_.name = reinterpret_cast<const char*>(argv[3]); |
| 107 | t1_defn_.cols = GetColumnsForTable(db_, t1_defn_.name); |
Lalit Maganti | 2d3e71d | 2018-09-24 14:13:27 +0100 | [diff] [blame] | 108 | |
Lalit Maganti | b29d0cb | 2018-09-24 16:31:44 +0100 | [diff] [blame] | 109 | t2_defn_.name = reinterpret_cast<const char*>(argv[4]); |
| 110 | t2_defn_.cols = GetColumnsForTable(db_, t2_defn_.name); |
Lalit Maganti | 2d3e71d | 2018-09-24 14:13:27 +0100 | [diff] [blame] | 111 | |
| 112 | join_col_ = reinterpret_cast<const char*>(argv[5]); |
Lalit Maganti | 2d3e71d | 2018-09-24 14:13:27 +0100 | [diff] [blame] | 113 | |
| 114 | // TODO(lalitm): add logic to ensure that the tables that are being joined |
| 115 | // are actually valid to be joined i.e. they have the ts and dur columns and |
| 116 | // have the join column. |
| 117 | |
Lalit Maganti | a270b65 | 2018-09-26 11:54:33 +0100 | [diff] [blame] | 118 | auto filter_fn = [this](const Table::Column& it) { |
| 119 | return it.name() == "ts" || it.name() == "dur" || it.name() == join_col_; |
Lalit Maganti | b29d0cb | 2018-09-24 16:31:44 +0100 | [diff] [blame] | 120 | }; |
| 121 | auto t1_remove_it = |
| 122 | std::remove_if(t1_defn_.cols.begin(), t1_defn_.cols.end(), filter_fn); |
| 123 | t1_defn_.cols.erase(t1_remove_it, t1_defn_.cols.end()); |
| 124 | auto t2_remove_it = |
| 125 | std::remove_if(t2_defn_.cols.begin(), t2_defn_.cols.end(), filter_fn); |
| 126 | t2_defn_.cols.erase(t2_remove_it, t2_defn_.cols.end()); |
Lalit Maganti | 2d3e71d | 2018-09-24 14:13:27 +0100 | [diff] [blame] | 127 | |
Lalit Maganti | a270b65 | 2018-09-26 11:54:33 +0100 | [diff] [blame] | 128 | std::vector<Table::Column> columns = { |
| 129 | Table::Column(Column::kTimestamp, "ts", ColumnType::kUlong), |
| 130 | Table::Column(Column::kDuration, "dur", ColumnType::kUlong), |
| 131 | Table::Column(Column::kJoinValue, join_col_, ColumnType::kUlong), |
| 132 | }; |
| 133 | size_t index = kReservedColumns; |
Lalit Maganti | b29d0cb | 2018-09-24 16:31:44 +0100 | [diff] [blame] | 134 | for (const auto& col : t1_defn_.cols) { |
Lalit Maganti | a270b65 | 2018-09-26 11:54:33 +0100 | [diff] [blame] | 135 | columns.emplace_back(index++, col.name(), col.type()); |
Lalit Maganti | 2d3e71d | 2018-09-24 14:13:27 +0100 | [diff] [blame] | 136 | } |
Lalit Maganti | b29d0cb | 2018-09-24 16:31:44 +0100 | [diff] [blame] | 137 | for (const auto& col : t2_defn_.cols) { |
Lalit Maganti | a270b65 | 2018-09-26 11:54:33 +0100 | [diff] [blame] | 138 | columns.emplace_back(index++, col.name(), col.type()); |
Lalit Maganti | 2d3e71d | 2018-09-24 14:13:27 +0100 | [diff] [blame] | 139 | } |
Lalit Maganti | a270b65 | 2018-09-26 11:54:33 +0100 | [diff] [blame] | 140 | return Schema(columns, {Column::kTimestamp, kJoinValue}); |
Lalit Maganti | 2d3e71d | 2018-09-24 14:13:27 +0100 | [diff] [blame] | 141 | } |
| 142 | |
Lalit Maganti | f0b4e17 | 2018-09-27 13:43:26 +0100 | [diff] [blame^] | 143 | std::unique_ptr<Table::Cursor> SpanOperatorTable::CreateCursor( |
| 144 | const QueryConstraints& qc, |
| 145 | sqlite3_value** argv) { |
| 146 | auto cursor = std::unique_ptr<SpanOperatorTable::Cursor>( |
Lalit Maganti | 2d3e71d | 2018-09-24 14:13:27 +0100 | [diff] [blame] | 147 | new SpanOperatorTable::Cursor(this, db_)); |
Lalit Maganti | f0b4e17 | 2018-09-27 13:43:26 +0100 | [diff] [blame^] | 148 | int value = cursor->Initialize(qc, argv); |
| 149 | return value != SQLITE_OK ? nullptr : std::move(cursor); |
Lalit Maganti | 2d3e71d | 2018-09-24 14:13:27 +0100 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | int SpanOperatorTable::BestIndex(const QueryConstraints&, BestIndexInfo*) { |
| 153 | // TODO(lalitm): figure out cost estimation. |
| 154 | return SQLITE_OK; |
| 155 | } |
| 156 | |
Lalit Maganti | d1ee86b | 2018-09-24 17:13:01 +0100 | [diff] [blame] | 157 | std::pair<bool, size_t> SpanOperatorTable::GetTableAndColumnIndex( |
| 158 | int joined_column_idx) { |
| 159 | PERFETTO_CHECK(joined_column_idx >= kReservedColumns); |
| 160 | |
| 161 | size_t table_1_col = |
| 162 | static_cast<size_t>(joined_column_idx - kReservedColumns); |
| 163 | if (table_1_col < t1_defn_.cols.size()) { |
| 164 | return std::make_pair(true, table_1_col); |
| 165 | } |
| 166 | size_t table_2_col = table_1_col - t1_defn_.cols.size(); |
| 167 | PERFETTO_CHECK(table_2_col < t2_defn_.cols.size()); |
| 168 | return std::make_pair(false, table_2_col); |
| 169 | } |
| 170 | |
Lalit Maganti | 2d3e71d | 2018-09-24 14:13:27 +0100 | [diff] [blame] | 171 | SpanOperatorTable::Cursor::Cursor(SpanOperatorTable* table, sqlite3* db) |
| 172 | : db_(db), table_(table) {} |
| 173 | |
Lalit Maganti | f0b4e17 | 2018-09-27 13:43:26 +0100 | [diff] [blame^] | 174 | int SpanOperatorTable::Cursor::Initialize(const QueryConstraints& qc, |
| 175 | sqlite3_value** argv) { |
| 176 | sqlite3_stmt* t1_raw = nullptr; |
| 177 | int err = PrepareRawStmt(qc, argv, table_->t1_defn_, true, &t1_raw); |
| 178 | t1_.stmt.reset(t1_raw); |
| 179 | if (err != SQLITE_OK) |
| 180 | return err; |
| 181 | |
| 182 | sqlite3_stmt* t2_raw = nullptr; |
| 183 | err = PrepareRawStmt(qc, argv, table_->t2_defn_, false, &t2_raw); |
| 184 | t2_.stmt.reset(t2_raw); |
| 185 | if (err != SQLITE_OK) |
| 186 | return err; |
| 187 | |
| 188 | err = sqlite3_step(t1_.stmt.get()); |
| 189 | if (err != SQLITE_DONE) { |
| 190 | if (err != SQLITE_ROW) |
| 191 | return SQLITE_ERROR; |
| 192 | int64_t ts = sqlite3_column_int64(t1_.stmt.get(), Column::kTimestamp); |
| 193 | t1_.latest_ts = static_cast<uint64_t>(ts); |
| 194 | t1_.col_count = static_cast<size_t>(sqlite3_column_count(t1_.stmt.get())); |
| 195 | } |
| 196 | |
| 197 | err = sqlite3_step(t2_.stmt.get()); |
| 198 | if (err != SQLITE_DONE) { |
| 199 | if (err != SQLITE_ROW) |
| 200 | return SQLITE_ERROR; |
| 201 | int64_t ts = sqlite3_column_int64(t2_.stmt.get(), Column::kTimestamp); |
| 202 | t2_.latest_ts = static_cast<uint64_t>(ts); |
| 203 | t2_.col_count = static_cast<size_t>(sqlite3_column_count(t2_.stmt.get())); |
| 204 | } |
| 205 | return Next(); |
| 206 | } |
| 207 | |
Lalit Maganti | 2d3e71d | 2018-09-24 14:13:27 +0100 | [diff] [blame] | 208 | SpanOperatorTable::Cursor::~Cursor() {} |
| 209 | |
Lalit Maganti | d1ee86b | 2018-09-24 17:13:01 +0100 | [diff] [blame] | 210 | int SpanOperatorTable::Cursor::PrepareRawStmt(const QueryConstraints& qc, |
| 211 | sqlite3_value** argv, |
| 212 | const TableDefinition& def, |
| 213 | bool is_t1, |
Lalit Maganti | b29d0cb | 2018-09-24 16:31:44 +0100 | [diff] [blame] | 214 | sqlite3_stmt** stmt) { |
Lalit Maganti | 2d3e71d | 2018-09-24 14:13:27 +0100 | [diff] [blame] | 215 | // TODO(lalitm): pass through constraints on other tables to those tables. |
Lalit Maganti | d1ee86b | 2018-09-24 17:13:01 +0100 | [diff] [blame] | 216 | std::string sql; |
| 217 | sql += "SELECT ts, dur, " + table_->join_col_; |
Lalit Maganti | b29d0cb | 2018-09-24 16:31:44 +0100 | [diff] [blame] | 218 | for (const auto& col : def.cols) { |
Lalit Maganti | a270b65 | 2018-09-26 11:54:33 +0100 | [diff] [blame] | 219 | sql += ", " + col.name(); |
Lalit Maganti | 2d3e71d | 2018-09-24 14:13:27 +0100 | [diff] [blame] | 220 | } |
Lalit Maganti | d1ee86b | 2018-09-24 17:13:01 +0100 | [diff] [blame] | 221 | sql += " FROM " + def.name; |
| 222 | sql += " WHERE 1"; |
| 223 | |
| 224 | for (size_t i = 0; i < qc.constraints().size(); i++) { |
| 225 | const auto& constraint = qc.constraints()[i]; |
| 226 | int c = constraint.iColumn; |
| 227 | std::string col_name; |
| 228 | if (c == Column::kTimestamp) { |
| 229 | col_name = "ts"; |
| 230 | } else if (c == Column::kDuration) { |
| 231 | col_name = "dur"; |
| 232 | } else if (c == Column::kJoinValue) { |
| 233 | col_name = table_->join_col_; |
| 234 | } else { |
| 235 | auto index_pair = table_->GetTableAndColumnIndex(c); |
| 236 | bool is_constraint_in_current_table = index_pair.first == is_t1; |
| 237 | if (is_constraint_in_current_table) { |
Lalit Maganti | a270b65 | 2018-09-26 11:54:33 +0100 | [diff] [blame] | 238 | col_name = def.cols[index_pair.second].name(); |
Lalit Maganti | d1ee86b | 2018-09-24 17:13:01 +0100 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | |
| 242 | if (!col_name.empty()) { |
| 243 | sql += " AND " + col_name + OpToString(constraint.op) + |
| 244 | reinterpret_cast<const char*>(sqlite3_value_text(argv[i])); |
| 245 | } |
| 246 | } |
| 247 | sql += " ORDER BY ts;"; |
| 248 | |
| 249 | PERFETTO_DLOG("%s", sql.c_str()); |
| 250 | int t1_size = static_cast<int>(sql.size()); |
| 251 | return sqlite3_prepare_v2(db_, sql.c_str(), t1_size, stmt, nullptr); |
Lalit Maganti | b29d0cb | 2018-09-24 16:31:44 +0100 | [diff] [blame] | 252 | } |
Lalit Maganti | 2d3e71d | 2018-09-24 14:13:27 +0100 | [diff] [blame] | 253 | |
Lalit Maganti | 2d3e71d | 2018-09-24 14:13:27 +0100 | [diff] [blame] | 254 | int SpanOperatorTable::Cursor::Next() { |
Lalit Maganti | b29d0cb | 2018-09-24 16:31:44 +0100 | [diff] [blame] | 255 | PERFETTO_DCHECK(!intersecting_spans_.empty() || children_have_more_); |
| 256 | |
| 257 | // If there are no more rows to be added from the child tables, simply pop the |
| 258 | // the front of the queue and return. |
| 259 | if (!children_have_more_) { |
| 260 | intersecting_spans_.pop_front(); |
| 261 | return SQLITE_OK; |
| 262 | } |
| 263 | |
| 264 | // Remove the previously returned span but also try and find more |
| 265 | // intersections. |
| 266 | if (!intersecting_spans_.empty()) |
| 267 | intersecting_spans_.pop_front(); |
Lalit Maganti | 2d3e71d | 2018-09-24 14:13:27 +0100 | [diff] [blame] | 268 | |
| 269 | // Pull from whichever cursor has the earlier timestamp and return if there |
Lalit Maganti | b29d0cb | 2018-09-24 16:31:44 +0100 | [diff] [blame] | 270 | // is a valid span. |
Lalit Maganti | 2d3e71d | 2018-09-24 14:13:27 +0100 | [diff] [blame] | 271 | while (t1_.latest_ts < kU64Max || t2_.latest_ts < kU64Max) { |
| 272 | int err = ExtractNext(t1_.latest_ts <= t2_.latest_ts); |
| 273 | if (err == SQLITE_ROW) { |
| 274 | return SQLITE_OK; |
| 275 | } else if (err != SQLITE_DONE) { |
| 276 | return err; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | // Once both cursors are completely exhausted, do one last pass through the |
Lalit Maganti | b29d0cb | 2018-09-24 16:31:44 +0100 | [diff] [blame] | 281 | // tables and return any final intersecting spans. |
| 282 | for (auto it = t1_.spans.begin(); it != t1_.spans.end(); it++) { |
| 283 | auto join_val = it->first; |
| 284 | auto t2_it = t2_.spans.find(join_val); |
| 285 | if (t2_it == t2_.spans.end()) |
| 286 | continue; |
| 287 | MaybeAddIntersectingSpan(join_val, std::move(it->second), |
| 288 | std::move(t2_it->second)); |
Lalit Maganti | 2d3e71d | 2018-09-24 14:13:27 +0100 | [diff] [blame] | 289 | } |
| 290 | |
Lalit Maganti | b29d0cb | 2018-09-24 16:31:44 +0100 | [diff] [blame] | 291 | // We don't have any more items to yield. |
| 292 | children_have_more_ = false; |
Lalit Maganti | 2d3e71d | 2018-09-24 14:13:27 +0100 | [diff] [blame] | 293 | return SQLITE_OK; |
| 294 | } |
| 295 | |
Lalit Maganti | f0b4e17 | 2018-09-27 13:43:26 +0100 | [diff] [blame^] | 296 | PERFETTO_ALWAYS_INLINE int SpanOperatorTable::Cursor::ExtractNext( |
Lalit Maganti | 2d3e71d | 2018-09-24 14:13:27 +0100 | [diff] [blame] | 297 | bool pull_t1) { |
| 298 | // Decide which table we will be retrieving a row from. |
| 299 | TableState* pull_table = pull_t1 ? &t1_ : &t2_; |
| 300 | |
| 301 | // Extract the timestamp, duration and join value from that table. |
| 302 | sqlite3_stmt* stmt = pull_table->stmt.get(); |
| 303 | int64_t ts = sqlite3_column_int64(stmt, Column::kTimestamp); |
| 304 | int64_t dur = sqlite3_column_int64(stmt, Column::kDuration); |
Lalit Maganti | b29d0cb | 2018-09-24 16:31:44 +0100 | [diff] [blame] | 305 | int64_t join_val = sqlite3_column_int64(stmt, Column::kJoinValue); |
Lalit Maganti | 2d3e71d | 2018-09-24 14:13:27 +0100 | [diff] [blame] | 306 | |
| 307 | // Extract the actual row from the state. |
Lalit Maganti | b29d0cb | 2018-09-24 16:31:44 +0100 | [diff] [blame] | 308 | auto* pull_span = &pull_table->spans[join_val]; |
Lalit Maganti | 2d3e71d | 2018-09-24 14:13:27 +0100 | [diff] [blame] | 309 | |
Lalit Maganti | b29d0cb | 2018-09-24 16:31:44 +0100 | [diff] [blame] | 310 | // Save the old span (to allow us to return it) and then update the data in |
| 311 | // the span. |
| 312 | Span saved_span = std::move(*pull_span); |
| 313 | pull_span->ts = static_cast<uint64_t>(ts); |
| 314 | pull_span->dur = static_cast<uint64_t>(dur); |
| 315 | pull_span->values.resize(pull_table->col_count - kReservedColumns); |
Lalit Maganti | 2d3e71d | 2018-09-24 14:13:27 +0100 | [diff] [blame] | 316 | |
| 317 | // Update all other columns. |
Lalit Maganti | b29d0cb | 2018-09-24 16:31:44 +0100 | [diff] [blame] | 318 | const auto& table_desc = pull_t1 ? table_->t1_defn_ : table_->t2_defn_; |
Lalit Maganti | 2d3e71d | 2018-09-24 14:13:27 +0100 | [diff] [blame] | 319 | int col_count = static_cast<int>(pull_table->col_count); |
| 320 | for (int i = kReservedColumns; i < col_count; i++) { |
| 321 | size_t off = static_cast<size_t>(i - kReservedColumns); |
| 322 | |
Lalit Maganti | b29d0cb | 2018-09-24 16:31:44 +0100 | [diff] [blame] | 323 | Value* value = &pull_span->values[off]; |
Lalit Maganti | a270b65 | 2018-09-26 11:54:33 +0100 | [diff] [blame] | 324 | value->type = table_desc.cols[off].type(); |
Lalit Maganti | 2d3e71d | 2018-09-24 14:13:27 +0100 | [diff] [blame] | 325 | switch (value->type) { |
Lalit Maganti | a270b65 | 2018-09-26 11:54:33 +0100 | [diff] [blame] | 326 | case Table::ColumnType::kUlong: |
Lalit Maganti | 2d3e71d | 2018-09-24 14:13:27 +0100 | [diff] [blame] | 327 | value->ulong_value = |
| 328 | static_cast<uint64_t>(sqlite3_column_int64(stmt, i)); |
| 329 | break; |
Lalit Maganti | a270b65 | 2018-09-26 11:54:33 +0100 | [diff] [blame] | 330 | case Table::ColumnType::kUint: |
Lalit Maganti | 2d3e71d | 2018-09-24 14:13:27 +0100 | [diff] [blame] | 331 | value->uint_value = static_cast<uint32_t>(sqlite3_column_int(stmt, i)); |
| 332 | break; |
Lalit Maganti | a270b65 | 2018-09-26 11:54:33 +0100 | [diff] [blame] | 333 | case Table::ColumnType::kString: |
Lalit Maganti | 2d3e71d | 2018-09-24 14:13:27 +0100 | [diff] [blame] | 334 | value->text_value = |
| 335 | reinterpret_cast<const char*>(sqlite3_column_text(stmt, i)); |
| 336 | break; |
Lalit Maganti | a270b65 | 2018-09-26 11:54:33 +0100 | [diff] [blame] | 337 | case Table::ColumnType::kInt: |
| 338 | PERFETTO_CHECK(false); |
Lalit Maganti | 2d3e71d | 2018-09-24 14:13:27 +0100 | [diff] [blame] | 339 | } |
| 340 | } |
| 341 | |
Lalit Maganti | b29d0cb | 2018-09-24 16:31:44 +0100 | [diff] [blame] | 342 | // Get the next value from whichever table we just updated. |
Lalit Maganti | 2d3e71d | 2018-09-24 14:13:27 +0100 | [diff] [blame] | 343 | int err = sqlite3_step(stmt); |
Lalit Maganti | b29d0cb | 2018-09-24 16:31:44 +0100 | [diff] [blame] | 344 | switch (err) { |
| 345 | case SQLITE_DONE: |
| 346 | pull_table->latest_ts = kU64Max; |
| 347 | break; |
| 348 | case SQLITE_ROW: |
| 349 | pull_table->latest_ts = |
| 350 | static_cast<uint64_t>(sqlite3_column_int64(stmt, Column::kTimestamp)); |
| 351 | break; |
| 352 | default: |
| 353 | return err; |
Lalit Maganti | 2d3e71d | 2018-09-24 14:13:27 +0100 | [diff] [blame] | 354 | } |
| 355 | |
Lalit Maganti | b29d0cb | 2018-09-24 16:31:44 +0100 | [diff] [blame] | 356 | // Create copies of the spans we want to intersect then perform the intersect. |
| 357 | auto t1_span = pull_t1 ? std::move(saved_span) : t1_.spans[join_val]; |
| 358 | auto t2_span = pull_t1 ? t2_.spans[join_val] : std::move(saved_span); |
| 359 | bool span_added = MaybeAddIntersectingSpan(join_val, t1_span, t2_span); |
| 360 | return span_added ? SQLITE_ROW : SQLITE_DONE; |
Lalit Maganti | 2d3e71d | 2018-09-24 14:13:27 +0100 | [diff] [blame] | 361 | } |
| 362 | |
Lalit Maganti | f0b4e17 | 2018-09-27 13:43:26 +0100 | [diff] [blame^] | 363 | bool SpanOperatorTable::Cursor::MaybeAddIntersectingSpan(int64_t join_value, |
| 364 | Span t1_span, |
| 365 | Span t2_span) { |
Lalit Maganti | b29d0cb | 2018-09-24 16:31:44 +0100 | [diff] [blame] | 366 | uint64_t t1_end = t1_span.ts + t1_span.dur; |
| 367 | uint64_t t2_end = t2_span.ts + t2_span.dur; |
Lalit Maganti | 2d3e71d | 2018-09-24 14:13:27 +0100 | [diff] [blame] | 368 | |
| 369 | // If there is no overlap between the two spans, don't return anything. |
Lalit Maganti | b29d0cb | 2018-09-24 16:31:44 +0100 | [diff] [blame] | 370 | if (t1_end == 0 || t2_end == 0 || t2_end < t1_span.ts || t1_end < t2_span.ts) |
Lalit Maganti | 2d3e71d | 2018-09-24 14:13:27 +0100 | [diff] [blame] | 371 | return false; |
| 372 | |
Lalit Maganti | b29d0cb | 2018-09-24 16:31:44 +0100 | [diff] [blame] | 373 | IntersectingSpan value; |
| 374 | value.ts = std::max(t1_span.ts, t2_span.ts); |
| 375 | value.dur = std::min(t1_end, t2_end) - value.ts; |
| 376 | value.join_val = join_value; |
| 377 | value.t1_span = std::move(t1_span); |
| 378 | value.t2_span = std::move(t2_span); |
| 379 | intersecting_spans_.emplace_back(std::move(value)); |
Lalit Maganti | 2d3e71d | 2018-09-24 14:13:27 +0100 | [diff] [blame] | 380 | |
| 381 | return true; |
| 382 | } |
| 383 | |
Lalit Maganti | f0b4e17 | 2018-09-27 13:43:26 +0100 | [diff] [blame^] | 384 | int SpanOperatorTable::Cursor::Eof() { |
Lalit Maganti | b29d0cb | 2018-09-24 16:31:44 +0100 | [diff] [blame] | 385 | return intersecting_spans_.empty() && !children_have_more_; |
Lalit Maganti | 2d3e71d | 2018-09-24 14:13:27 +0100 | [diff] [blame] | 386 | } |
| 387 | |
Lalit Maganti | f0b4e17 | 2018-09-27 13:43:26 +0100 | [diff] [blame^] | 388 | int SpanOperatorTable::Cursor::Column(sqlite3_context* context, int N) { |
Lalit Maganti | b29d0cb | 2018-09-24 16:31:44 +0100 | [diff] [blame] | 389 | const auto& ret = intersecting_spans_.front(); |
Lalit Maganti | 2d3e71d | 2018-09-24 14:13:27 +0100 | [diff] [blame] | 390 | switch (N) { |
| 391 | case Column::kTimestamp: |
Lalit Maganti | b29d0cb | 2018-09-24 16:31:44 +0100 | [diff] [blame] | 392 | sqlite3_result_int64(context, static_cast<sqlite3_int64>(ret.ts)); |
Lalit Maganti | 2d3e71d | 2018-09-24 14:13:27 +0100 | [diff] [blame] | 393 | break; |
| 394 | case Column::kDuration: |
Lalit Maganti | b29d0cb | 2018-09-24 16:31:44 +0100 | [diff] [blame] | 395 | sqlite3_result_int64(context, static_cast<sqlite3_int64>(ret.dur)); |
Lalit Maganti | 2d3e71d | 2018-09-24 14:13:27 +0100 | [diff] [blame] | 396 | break; |
| 397 | case Column::kJoinValue: |
Lalit Maganti | b29d0cb | 2018-09-24 16:31:44 +0100 | [diff] [blame] | 398 | sqlite3_result_int64(context, static_cast<sqlite3_int64>(ret.join_val)); |
Lalit Maganti | 2d3e71d | 2018-09-24 14:13:27 +0100 | [diff] [blame] | 399 | break; |
| 400 | default: { |
Lalit Maganti | d1ee86b | 2018-09-24 17:13:01 +0100 | [diff] [blame] | 401 | auto index_pair = table_->GetTableAndColumnIndex(N); |
| 402 | const auto& row = index_pair.first ? ret.t1_span : ret.t2_span; |
| 403 | ReportSqliteResult(context, row.values[index_pair.second]); |
Lalit Maganti | 2d3e71d | 2018-09-24 14:13:27 +0100 | [diff] [blame] | 404 | } |
| 405 | } |
| 406 | return SQLITE_OK; |
| 407 | } |
| 408 | |
Lalit Maganti | f0b4e17 | 2018-09-27 13:43:26 +0100 | [diff] [blame^] | 409 | PERFETTO_ALWAYS_INLINE void SpanOperatorTable::Cursor::ReportSqliteResult( |
Lalit Maganti | 2d3e71d | 2018-09-24 14:13:27 +0100 | [diff] [blame] | 410 | sqlite3_context* context, |
| 411 | SpanOperatorTable::Value value) { |
| 412 | switch (value.type) { |
Lalit Maganti | a270b65 | 2018-09-26 11:54:33 +0100 | [diff] [blame] | 413 | case Table::ColumnType::kUint: |
Lalit Maganti | 2d3e71d | 2018-09-24 14:13:27 +0100 | [diff] [blame] | 414 | sqlite3_result_int(context, static_cast<int>(value.uint_value)); |
| 415 | break; |
Lalit Maganti | a270b65 | 2018-09-26 11:54:33 +0100 | [diff] [blame] | 416 | case Table::ColumnType::kUlong: |
Lalit Maganti | 2d3e71d | 2018-09-24 14:13:27 +0100 | [diff] [blame] | 417 | sqlite3_result_int64(context, |
| 418 | static_cast<sqlite3_int64>(value.ulong_value)); |
| 419 | break; |
Lalit Maganti | a270b65 | 2018-09-26 11:54:33 +0100 | [diff] [blame] | 420 | case Table::ColumnType::kString: { |
Lalit Maganti | 2d3e71d | 2018-09-24 14:13:27 +0100 | [diff] [blame] | 421 | // Note: If you could guarantee that you never sqlite3_step() the cursor |
| 422 | // before accessing the values here, you could avoid string copies and |
| 423 | // pass through the const char* obtained in ExtractNext |
| 424 | const auto kSqliteTransient = |
| 425 | reinterpret_cast<sqlite3_destructor_type>(-1); |
| 426 | sqlite3_result_text(context, value.text_value.c_str(), -1, |
| 427 | kSqliteTransient); |
| 428 | break; |
Lalit Maganti | a270b65 | 2018-09-26 11:54:33 +0100 | [diff] [blame] | 429 | } |
| 430 | case Table::ColumnType::kInt: |
| 431 | PERFETTO_CHECK(false); |
Lalit Maganti | 2d3e71d | 2018-09-24 14:13:27 +0100 | [diff] [blame] | 432 | } |
| 433 | } |
| 434 | |
| 435 | } // namespace trace_processor |
| 436 | } // namespace perfetto |