trace_processor: migrate cpu stack profile table to db tables

Context: go/perfetto-tp-refactor
Bug: 135177627
Change-Id: I43702bd9a8e5f3198cf8e8cc8767bb3145172e7a
diff --git a/src/trace_processor/BUILD.gn b/src/trace_processor/BUILD.gn
index cb2f5cf..ee7a309 100644
--- a/src/trace_processor/BUILD.gn
+++ b/src/trace_processor/BUILD.gn
@@ -301,8 +301,6 @@
       "android_logs_table.h",
       "args_table.cc",
       "args_table.h",
-      "cpu_profile_stack_sample_table.cc",
-      "cpu_profile_stack_sample_table.h",
       "filtered_row_index.cc",
       "filtered_row_index.h",
       "gfp_flags.cc",
diff --git a/src/trace_processor/cpu_profile_stack_sample_table.cc b/src/trace_processor/cpu_profile_stack_sample_table.cc
deleted file mode 100644
index 1226020..0000000
--- a/src/trace_processor/cpu_profile_stack_sample_table.cc
+++ /dev/null
@@ -1,55 +0,0 @@
-/*
- * Copyright (C) 2019 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "src/trace_processor/cpu_profile_stack_sample_table.h"
-
-namespace perfetto {
-namespace trace_processor {
-
-CpuProfileStackSampleTable::CpuProfileStackSampleTable(
-    sqlite3*,
-    const TraceStorage* storage)
-    : storage_(storage) {}
-
-void CpuProfileStackSampleTable::RegisterTable(sqlite3* db,
-                                               const TraceStorage* storage) {
-  SqliteTable::Register<CpuProfileStackSampleTable>(db, storage,
-                                                    "cpu_profile_stack_sample");
-}
-
-StorageSchema CpuProfileStackSampleTable::CreateStorageSchema() {
-  const auto& allocs = storage_->cpu_profile_stack_samples();
-  return StorageSchema::Builder()
-      .AddGenericNumericColumn("id", RowAccessor())
-      .AddOrderedNumericColumn("ts", &allocs.timestamps())
-      .AddNumericColumn("callsite_id", &allocs.callsite_ids())
-      .AddNumericColumn("utid", &allocs.utids())
-      .Build({"id"});
-}
-
-uint32_t CpuProfileStackSampleTable::RowCount() {
-  return storage_->cpu_profile_stack_samples().size();
-}
-
-int CpuProfileStackSampleTable::BestIndex(const QueryConstraints& qc,
-                                          BestIndexInfo* info) {
-  info->sqlite_omit_order_by = true;
-  info->estimated_cost = HasEqConstraint(qc, "id") ? 1 : RowCount();
-  return SQLITE_OK;
-}
-
-}  // namespace trace_processor
-}  // namespace perfetto
diff --git a/src/trace_processor/cpu_profile_stack_sample_table.h b/src/trace_processor/cpu_profile_stack_sample_table.h
deleted file mode 100644
index e422d75..0000000
--- a/src/trace_processor/cpu_profile_stack_sample_table.h
+++ /dev/null
@@ -1,43 +0,0 @@
-/*
- * Copyright (C) 2019 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef SRC_TRACE_PROCESSOR_CPU_PROFILE_STACK_SAMPLE_TABLE_H_
-#define SRC_TRACE_PROCESSOR_CPU_PROFILE_STACK_SAMPLE_TABLE_H_
-
-#include "src/trace_processor/storage_table.h"
-
-namespace perfetto {
-namespace trace_processor {
-
-class CpuProfileStackSampleTable : public StorageTable {
- public:
-  static void RegisterTable(sqlite3* db, const TraceStorage* storage);
-
-  CpuProfileStackSampleTable(sqlite3*, const TraceStorage*);
-
-  // StorageTable implementation.
-  StorageSchema CreateStorageSchema() override;
-  uint32_t RowCount() override;
-  int BestIndex(const QueryConstraints&, BestIndexInfo*) override;
-
- private:
-  const TraceStorage* const storage_;
-};
-
-}  // namespace trace_processor
-}  // namespace perfetto
-
-#endif  // SRC_TRACE_PROCESSOR_CPU_PROFILE_STACK_SAMPLE_TABLE_H_
diff --git a/src/trace_processor/export_json.cc b/src/trace_processor/export_json.cc
index 9ac286e..cba20d5 100644
--- a/src/trace_processor/export_json.cc
+++ b/src/trace_processor/export_json.cc
@@ -811,13 +811,13 @@
 
 util::Status ExportCpuProfileSamples(const TraceStorage* storage,
                                      TraceFormatWriter* writer) {
-  const TraceStorage::CpuProfileStackSamples& samples =
-      storage->cpu_profile_stack_samples();
-  for (uint32_t i = 0; i < samples.size(); ++i) {
+  const tables::CpuProfileStackSampleTable& samples =
+      storage->cpu_profile_stack_sample_table();
+  for (uint32_t i = 0; i < samples.row_count(); ++i) {
     Json::Value event;
-    event["ts"] = Json::Int64(samples.timestamps()[i] / 1000);
+    event["ts"] = Json::Int64(samples.timestamp()[i] / 1000);
 
-    UniqueTid utid = static_cast<UniqueTid>(samples.utids()[i]);
+    UniqueTid utid = static_cast<UniqueTid>(samples.utid()[i]);
     auto thread = storage->GetThread(utid);
     event["tid"] = static_cast<int32_t>(thread.tid);
     if (thread.upid) {
@@ -843,7 +843,7 @@
 
     std::vector<std::string> callstack;
     const auto& callsites = storage->stack_profile_callsite_table();
-    int64_t maybe_callsite_id = samples.callsite_ids()[i];
+    int64_t maybe_callsite_id = samples.callsite_id()[i];
     PERFETTO_DCHECK(maybe_callsite_id >= 0 &&
                     maybe_callsite_id < callsites.row_count());
     while (maybe_callsite_id >= 0) {
diff --git a/src/trace_processor/export_json_unittest.cc b/src/trace_processor/export_json_unittest.cc
index 4d72fcc..f087228 100644
--- a/src/trace_processor/export_json_unittest.cc
+++ b/src/trace_processor/export_json_unittest.cc
@@ -1174,7 +1174,7 @@
       storage->mutable_stack_profile_callsite_table()->Insert(
           {1, frame_callsite_id_1, frame_row_id_2});
 
-  storage->mutable_cpu_profile_stack_samples()->Insert(
+  storage->mutable_cpu_profile_stack_sample_table()->Insert(
       {kTimestamp, frame_callsite_id_2, utid});
 
   base::TempFile temp_file = base::TempFile::Create();
diff --git a/src/trace_processor/importers/proto/proto_trace_parser.cc b/src/trace_processor/importers/proto/proto_trace_parser.cc
index 01c0156..0788267 100644
--- a/src/trace_processor/importers/proto/proto_trace_parser.cc
+++ b/src/trace_processor/importers/proto/proto_trace_parser.cc
@@ -443,10 +443,10 @@
 
     int64_t callstack_id = *maybe_callstack_id;
 
-    TraceStorage::CpuProfileStackSamples::Row sample_row{
+    tables::CpuProfileStackSampleTable::Row sample_row{
         sequence_state->IncrementAndGetTrackEventTimeNs(*timestamp_it),
         callstack_id, utid};
-    storage->mutable_cpu_profile_stack_samples()->Insert(sample_row);
+    storage->mutable_cpu_profile_stack_sample_table()->Insert(sample_row);
   }
 }
 
diff --git a/src/trace_processor/importers/proto/proto_trace_parser_unittest.cc b/src/trace_processor/importers/proto/proto_trace_parser_unittest.cc
index a2455ce..6939a37 100644
--- a/src/trace_processor/importers/proto/proto_trace_parser_unittest.cc
+++ b/src/trace_processor/importers/proto/proto_trace_parser_unittest.cc
@@ -2581,20 +2581,20 @@
   Tokenize();
 
   // Verify cpu_profile_samples.
-  const auto& samples = storage_->cpu_profile_stack_samples();
-  EXPECT_EQ(samples.size(), 3u);
+  const auto& samples = storage_->cpu_profile_stack_sample_table();
+  EXPECT_EQ(samples.row_count(), 3u);
 
-  EXPECT_EQ(samples.timestamps()[0], 1010);
-  EXPECT_EQ(samples.callsite_ids()[0], 0);
-  EXPECT_EQ(samples.utids()[0], 1u);
+  EXPECT_EQ(samples.timestamp()[0], 1010);
+  EXPECT_EQ(samples.callsite_id()[0], 0);
+  EXPECT_EQ(samples.utid()[0], 1u);
 
-  EXPECT_EQ(samples.timestamps()[1], 1025);
-  EXPECT_EQ(samples.callsite_ids()[1], 1);
-  EXPECT_EQ(samples.utids()[1], 1u);
+  EXPECT_EQ(samples.timestamp()[1], 1025);
+  EXPECT_EQ(samples.callsite_id()[1], 1);
+  EXPECT_EQ(samples.utid()[1], 1u);
 
-  EXPECT_EQ(samples.timestamps()[2], 1067);
-  EXPECT_EQ(samples.callsite_ids()[2], 0);
-  EXPECT_EQ(samples.utids()[2], 1u);
+  EXPECT_EQ(samples.timestamp()[2], 1067);
+  EXPECT_EQ(samples.callsite_id()[2], 0);
+  EXPECT_EQ(samples.utid()[2], 1u);
 }
 
 }  // namespace
diff --git a/src/trace_processor/tables/profiler_tables.h b/src/trace_processor/tables/profiler_tables.h
index d00c5e7..f471954 100644
--- a/src/trace_processor/tables/profiler_tables.h
+++ b/src/trace_processor/tables/profiler_tables.h
@@ -32,6 +32,15 @@
 
 PERFETTO_TP_TABLE(PERFETTO_TP_STACK_PROFILE_CALLSITE_DEF);
 
+#define PERFETTO_TP_CPU_PROFILE_STACK_SAMPLE_DEF(NAME, PARENT, C) \
+  NAME(CpuProfileStackSampleTable, "cpu_profile_stack_sample")    \
+  PERFETTO_TP_ROOT_TABLE(PARENT, C)                               \
+  C(int64_t, timestamp)                                           \
+  C(int64_t, callsite_id)                                         \
+  C(uint32_t, utid)
+
+PERFETTO_TP_TABLE(PERFETTO_TP_CPU_PROFILE_STACK_SAMPLE_DEF);
+
 #define PERFETTO_TP_SYMBOL_DEF(NAME, PARENT, C) \
   NAME(SymbolTable, "stack_profile_symbol")     \
   PERFETTO_TP_ROOT_TABLE(PARENT, C)             \
diff --git a/src/trace_processor/trace_processor_impl.cc b/src/trace_processor/trace_processor_impl.cc
index 039b1bd..b220ac2 100644
--- a/src/trace_processor/trace_processor_impl.cc
+++ b/src/trace_processor/trace_processor_impl.cc
@@ -25,7 +25,6 @@
 #include "perfetto/ext/base/string_utils.h"
 #include "src/trace_processor/android_logs_table.h"
 #include "src/trace_processor/args_table.h"
-#include "src/trace_processor/cpu_profile_stack_sample_table.h"
 #include "src/trace_processor/instants_table.h"
 #include "src/trace_processor/metadata_table.h"
 #include "src/trace_processor/process_table.h"
@@ -381,7 +380,6 @@
   StatsTable::RegisterTable(*db_, context_.storage.get());
   AndroidLogsTable::RegisterTable(*db_, context_.storage.get());
   RawTable::RegisterTable(*db_, context_.storage.get());
-  CpuProfileStackSampleTable::RegisterTable(*db_, context_.storage.get());
   StackProfileFrameTable::RegisterTable(*db_, context_.storage.get());
   StackProfileMappingTable::RegisterTable(*db_, context_.storage.get());
   MetadataTable::RegisterTable(*db_, context_.storage.get());
@@ -436,6 +434,9 @@
       *db_, &storage->heap_profile_allocation_table(),
       storage->heap_profile_allocation_table().table_name());
   DbSqliteTable::RegisterTable(
+      *db_, &storage->cpu_profile_stack_sample_table(),
+      storage->cpu_profile_stack_sample_table().table_name());
+  DbSqliteTable::RegisterTable(
       *db_, &storage->stack_profile_callsite_table(),
       storage->stack_profile_callsite_table().table_name());
 
diff --git a/src/trace_processor/trace_storage.h b/src/trace_processor/trace_storage.h
index b27d5f1..0e9e827 100644
--- a/src/trace_processor/trace_storage.h
+++ b/src/trace_processor/trace_storage.h
@@ -773,32 +773,6 @@
         index_;
   };
 
-  class CpuProfileStackSamples {
-   public:
-    struct Row {
-      int64_t timestamp;
-      int64_t callsite_id;
-      UniqueTid utid;
-    };
-
-    uint32_t size() const { return static_cast<uint32_t>(timestamps_.size()); }
-
-    void Insert(const Row& row) {
-      timestamps_.emplace_back(row.timestamp);
-      callsite_ids_.emplace_back(row.callsite_id);
-      utids_.emplace_back(row.utid);
-    }
-
-    const std::deque<int64_t>& timestamps() const { return timestamps_; }
-    const std::deque<int64_t>& callsite_ids() const { return callsite_ids_; }
-    const std::deque<UniqueTid>& utids() const { return utids_; }
-
-   private:
-    std::deque<int64_t> timestamps_;
-    std::deque<int64_t> callsite_ids_;
-    std::deque<UniqueTid> utids_;
-  };
-
   UniqueTid AddEmptyThread(uint32_t tid) {
     unique_threads_.emplace_back(tid);
     return static_cast<UniqueTid>(unique_threads_.size() - 1);
@@ -1079,11 +1053,12 @@
   tables::HeapProfileAllocationTable* mutable_heap_profile_allocation_table() {
     return &heap_profile_allocation_table_;
   }
-  const CpuProfileStackSamples& cpu_profile_stack_samples() const {
-    return cpu_profile_stack_samples_;
+  const tables::CpuProfileStackSampleTable& cpu_profile_stack_sample_table()
+      const {
+    return cpu_profile_stack_sample_table_;
   }
-  CpuProfileStackSamples* mutable_cpu_profile_stack_samples() {
-    return &cpu_profile_stack_samples_;
+  tables::CpuProfileStackSampleTable* mutable_cpu_profile_stack_sample_table() {
+    return &cpu_profile_stack_sample_table_;
   }
 
   const tables::SymbolTable& symbol_table() const { return symbol_table_; }
@@ -1236,7 +1211,8 @@
                                                                   nullptr};
   tables::HeapProfileAllocationTable heap_profile_allocation_table_{
       &string_pool_, nullptr};
-  CpuProfileStackSamples cpu_profile_stack_samples_;
+  tables::CpuProfileStackSampleTable cpu_profile_stack_sample_table_{
+      &string_pool_, nullptr};
 
   // Symbol tables (mappings from frames to symbol names)
   tables::SymbolTable symbol_table_{&string_pool_, nullptr};