tp: Use track_tracker to intern/create tracks in {track_event|async}_track_trackers



Change-Id: If602491affd1c219573b6e2e3d7412125b3dd9b8
diff --git a/src/trace_processor/export_json_unittest.cc b/src/trace_processor/export_json_unittest.cc
index 8bb4e5f..25c7a82 100644
--- a/src/trace_processor/export_json_unittest.cc
+++ b/src/trace_processor/export_json_unittest.cc
@@ -251,8 +251,8 @@
 }
 
 TEST_F(ExportJsonTest, SystemEventsIgnored) {
-  TrackId track = context_.track_tracker->LegacyCreateProcessAsyncTrack(
-      /*name=*/kNullStringId, /*upid=*/0, /*source=*/kNullStringId);
+  TrackId track = context_.track_tracker->InternProcessTrack(
+      TrackTracker::TrackClassification::kUnknown, 0);
   context_.args_tracker->Flush();  // Flush track args.
 
   // System events have no category.
diff --git a/src/trace_processor/importers/common/async_track_set_tracker.cc b/src/trace_processor/importers/common/async_track_set_tracker.cc
index 5b1005e..17085b5 100644
--- a/src/trace_processor/importers/common/async_track_set_tracker.cc
+++ b/src/trace_processor/importers/common/async_track_set_tracker.cc
@@ -16,6 +16,7 @@
 
 #include "src/trace_processor/importers/common/async_track_set_tracker.h"
 
+#include "src/trace_processor/importers/common/process_track_translation_table.h"
 #include "src/trace_processor/importers/common/track_tracker.h"
 #include "src/trace_processor/storage/trace_storage.h"
 #include "src/trace_processor/types/trace_processor_context.h"
@@ -181,11 +182,16 @@
 
 TrackId AsyncTrackSetTracker::CreateTrackForSet(const TrackSet& set) {
   switch (set.scope) {
-    case TrackSetScope::kGlobal:
-      // TODO(lalitm): propogate source from callers rather than just passing
-      // kNullStringId here.
-      return context_->track_tracker->LegacyCreateGlobalAsyncTrack(
-          set.global_track_name, kNullStringId);
+    case TrackSetScope::kGlobal: {
+      // TODO(lalitm): propogate source from callers as a dimension
+      TrackTracker::DimensionsBuilder builder =
+          context_->track_tracker->CreateDimensionsBuilder();
+      builder.AppendName(set.global_track_name);
+
+      return context_->track_tracker->CreateTrack(
+          TrackTracker::TrackClassification::kAsync, std::move(builder).Build(),
+          set.global_track_name);
+    }
     case TrackSetScope::kProcess:
       // TODO(lalitm): propogate source from callers rather than just passing
       // kNullStringId here.
@@ -193,9 +199,27 @@
           set.nesting_behaviour == NestingBehaviour::kLegacySaturatingUnnestable
               ? android_source_
               : kNullStringId;
-      return context_->track_tracker->LegacyCreateProcessAsyncTrack(
-          set.process_tuple.name, set.process_tuple.upid, source);
+
+      const StringId name =
+          context_->process_track_translation_table->TranslateName(
+              set.process_tuple.name);
+      TrackTracker::DimensionsBuilder dims_builder =
+          context_->track_tracker->CreateDimensionsBuilder();
+      dims_builder.AppendName(name);
+      dims_builder.AppendUpid(set.process_tuple.upid);
+      TrackTracker::Dimensions dims_id = std::move(dims_builder).Build();
+
+      TrackId id = context_->track_tracker->CreateProcessTrack(
+          TrackTracker::TrackClassification::kAsync, set.process_tuple.upid,
+          dims_id, name);
+
+      if (!source.is_null()) {
+        context_->args_tracker->AddArgsTo(id).AddArg(source,
+                                                     Variadic::String(source));
+      }
+      return id;
   }
+
   PERFETTO_FATAL("For GCC");
 }
 
diff --git a/src/trace_processor/importers/common/track_tracker.cc b/src/trace_processor/importers/common/track_tracker.cc
index 5f08fe1..2386453 100644
--- a/src/trace_processor/importers/common/track_tracker.cc
+++ b/src/trace_processor/importers/common/track_tracker.cc
@@ -143,13 +143,9 @@
       name_id_(context->storage->InternString("name")),
       context_(context) {}
 
-TrackId TrackTracker::InternTrack(TrackClassification classification,
+TrackId TrackTracker::CreateTrack(TrackClassification classification,
                                   std::optional<Dimensions> dimensions,
                                   StringId name) {
-  auto it = tracks_.Find({classification, dimensions});
-  if (it)
-    return *it;
-
   tables::TrackTable::Row row;
   row.classification = context_->storage->InternString(
       std::string(GetClassification(classification)).c_str());
@@ -159,38 +155,30 @@
   row.machine_id = context_->machine_id();
   row.name = name;
 
-  TrackId id = context_->storage->mutable_track_table()->Insert(row).id;
-  tracks_[{classification, dimensions}] = id;
-  return id;
+  return context_->storage->mutable_track_table()->Insert(row).id;
 }
 
-TrackId TrackTracker::CreateAsyncTrack(std::optional<Dimensions> dimensions,
-                                       StringId name) {
-  tables::TrackTable::Row row;
+TrackId TrackTracker::CreateCounterTrack(TrackClassification classification,
+                                         std::optional<Dimensions> dimensions,
+                                         StringId name) {
+  tables::CounterTrackTable::Row row;
   row.classification = context_->storage->InternString(
-      std::string(GetClassification(TrackClassification::kAsync)).c_str());
+      std::string(GetClassification(classification)).c_str());
   if (dimensions) {
     row.dimensions = dimensions->arg_set_id;
   }
   row.machine_id = context_->machine_id();
   row.name = name;
 
-  TrackId id = context_->storage->mutable_track_table()->Insert(row).id;
-  tracks_[{TrackTracker::TrackClassification::kAsync, dimensions}] = id;
-  return id;
+  return context_->storage->mutable_counter_track_table()->Insert(row).id;
 }
 
-TrackId TrackTracker::InternProcessTrack(
-    TrackClassification classification,
-    UniquePid upid,
-    std::optional<Dimensions> use_other_dimension,
-    StringId name) {
-  Dimensions dims_id = use_other_dimension.has_value()
-                           ? use_other_dimension.value()
-                           : SingleDimension(upid_id_, Variadic::Integer(upid));
-  auto it = tracks_.Find({classification, dims_id});
-  if (it)
-    return *it;
+TrackId TrackTracker::CreateProcessTrack(TrackClassification classification,
+                                         UniquePid upid,
+                                         std::optional<Dimensions> dims,
+                                         StringId name) {
+  Dimensions dims_id =
+      dims ? *dims : SingleDimension(upid_id_, Variadic::Integer(upid));
 
   tables::ProcessTrackTable::Row row(name);
   row.upid = upid;
@@ -199,48 +187,178 @@
       GetClassification(classification).c_str());
   row.machine_id = context_->machine_id();
 
-  auto track_id =
-      context_->storage->mutable_process_track_table()->Insert(row).id;
-  tracks_[{classification, dims_id}] = track_id;
-  return track_id;
+  return context_->storage->mutable_process_track_table()->Insert(row).id;
 }
 
-TrackId TrackTracker::InternThreadTrack(UniqueTid utid) {
-  Dimensions dims_id = SingleDimension(utid_id_, Variadic::Integer(utid));
+TrackId TrackTracker::CreateProcessCounterTrack(
+    TrackClassification classification,
+    UniquePid upid,
+    std::optional<Dimensions> dims) {
+  Dimensions dims_id =
+      dims ? *dims : SingleDimension(upid_id_, Variadic::Integer(upid));
 
-  auto it = tracks_.Find({TrackClassification::kThread, dims_id});
-  if (it)
-    return *it;
+  tables::ProcessCounterTrackTable::Row row;
+  row.upid = upid;
+  row.machine_id = context_->machine_id();
+  row.dimensions = dims_id.arg_set_id;
+  row.classification = context_->storage->InternString(
+      GetClassification(classification).c_str());
+
+  return context_->storage->mutable_process_counter_track_table()
+      ->Insert(row)
+      .id;
+}
+
+TrackId TrackTracker::CreateThreadTrack(TrackClassification classification,
+                                        UniqueTid utid,
+                                        std::optional<Dimensions> dims) {
+  Dimensions dims_id =
+      dims ? *dims : SingleDimension(utid_id_, Variadic::Integer(utid));
 
   tables::ThreadTrackTable::Row row;
   row.utid = utid;
   row.classification = context_->storage->InternString(
-      GetClassification(TrackClassification::kThread).c_str());
+      GetClassification(classification).c_str());
   row.dimensions = dims_id.arg_set_id;
   row.machine_id = context_->machine_id();
 
-  auto track_id =
-      context_->storage->mutable_thread_track_table()->Insert(row).id;
+  return context_->storage->mutable_thread_track_table()->Insert(row).id;
+}
+
+TrackId TrackTracker::CreateThreadCounterTrack(
+    TrackClassification classification,
+    StringId name,
+    UniqueTid utid,
+    std::optional<Dimensions> dims) {
+  Dimensions dims_id =
+      dims ? *dims : SingleDimension(utid_id_, Variadic::Integer(utid));
+
+  tables::ThreadCounterTrackTable::Row row(name);
+  row.utid = utid;
+  row.machine_id = context_->machine_id();
+  row.dimensions = dims_id.arg_set_id;
+  row.classification = context_->storage->InternString(
+      GetClassification(classification).c_str());
+
+  return context_->storage->mutable_thread_counter_track_table()
+      ->Insert(row)
+      .id;
+}
+
+TrackId TrackTracker::InternTrack(TrackClassification classification,
+                                  std::optional<Dimensions> dimensions,
+                                  StringId name) {
+  auto it = tracks_.Find({classification, dimensions});
+  if (it)
+    return *it;
+
+  TrackId id = CreateTrack(classification, dimensions, name);
+  tracks_[{classification, dimensions}] = id;
+  return id;
+}
+
+TrackId TrackTracker::InternProcessTrack(TrackClassification classification,
+                                         UniquePid upid,
+                                         std::optional<Dimensions> dims,
+                                         StringId name) {
+  Dimensions dims_id =
+      dims ? *dims : SingleDimension(upid_id_, Variadic::Integer(upid));
+
+  auto it = tracks_.Find({classification, dims_id});
+  if (it)
+    return *it;
+
+  TrackId track_id = CreateProcessTrack(classification, upid, dims, name);
+  tracks_[{classification, dims_id}] = track_id;
+  return track_id;
+}
+
+TrackId TrackTracker::InternProcessTrack(UniquePid upid,
+                                         std::optional<Dimensions> dims) {
+  return InternProcessTrack(TrackTracker::TrackClassification::kProcess, upid,
+                            dims);
+}
+
+TrackId TrackTracker::InternProcessCounterTrack(
+    UniquePid upid,
+    std::optional<Dimensions> dims) {
+  Dimensions dims_id =
+      dims ? *dims : SingleDimension(upid_id_, Variadic::Integer(upid));
+
+  auto it = tracks_.Find({TrackClassification::kUnknown, dims_id});
+  if (it) {
+    return *it;
+  }
+
+  TrackId track_id =
+      CreateProcessCounterTrack(TrackClassification::kUnknown, upid, dims_id);
+  tracks_[{TrackTracker::TrackClassification::kUnknown, dims_id}] = track_id;
+  return track_id;
+}
+
+TrackId TrackTracker::InternProcessCounterTrack(StringId raw_name,
+                                                UniquePid upid,
+                                                StringId unit,
+                                                StringId description) {
+  const StringId name =
+      context_->process_track_translation_table->TranslateName(raw_name);
+
+  TrackMapKey key;
+  key.classification = TrackClassification::kUnknown;
+  key.name = name;
+  key.dimensions = SingleDimension(upid_id_, Variadic::Integer(upid));
+
+  auto it = tracks_.Find(key);
+  if (it) {
+    return *it;
+  }
+
+  tables::ProcessCounterTrackTable::Row row(name);
+  row.upid = upid;
+  row.unit = unit;
+  row.description = description;
+  row.machine_id = context_->machine_id();
+  row.classification = context_->storage->InternString(
+      GetClassification(key.classification).c_str());
+  row.dimensions = key.dimensions->arg_set_id;
+  TrackId track_id =
+      context_->storage->mutable_process_counter_track_table()->Insert(row).id;
+
+  tracks_[key] = track_id;
+  return track_id;
+}
+
+TrackId TrackTracker::InternThreadTrack(UniqueTid utid,
+                                        std::optional<Dimensions> dims) {
+  Dimensions dims_id =
+      dims ? *dims : SingleDimension(utid_id_, Variadic::Integer(utid));
+
+  auto it = tracks_.Find({TrackClassification::kThread, dims_id});
+  if (it)
+    return *it;
+  TrackId track_id =
+      CreateThreadTrack(TrackClassification::kThread, utid, dims_id);
   tracks_[{TrackClassification::kThread, dims_id}] = track_id;
   return track_id;
 }
 
-TrackId TrackTracker::InternProcessTrack(UniquePid upid) {
-  Dimensions dims_id = SingleDimension(upid_id_, Variadic::Integer(upid));
-  auto it = tracks_.Find({TrackClassification::kProcess, dims_id});
-  if (it)
+TrackId TrackTracker::InternThreadCounterTrack(StringId name,
+                                               UniqueTid utid,
+                                               std::optional<Dimensions> dims) {
+  TrackMapKey key;
+  key.classification = TrackClassification::kUnknown;
+  key.name = name;
+  key.dimensions =
+      dims ? *dims : SingleDimension(utid_id_, Variadic::Integer(utid));
+
+  auto it = tracks_.Find(key);
+  if (it) {
     return *it;
+  }
 
-  tables::ProcessTrackTable::Row row;
-  row.upid = upid;
-  row.dimensions = dims_id.arg_set_id;
-  row.classification = context_->storage->InternString(
-      GetClassification(TrackClassification::kProcess).c_str());
-  row.machine_id = context_->machine_id();
-
-  auto track_id =
-      context_->storage->mutable_process_track_table()->Insert(row).id;
-  tracks_[{TrackClassification::kProcess, dims_id}] = track_id;
+  TrackId track_id = CreateThreadCounterTrack(TrackClassification::kUnknown,
+                                              name, utid, key.dimensions);
+  tracks_[key] = track_id;
   return track_id;
 }
 
@@ -263,10 +381,10 @@
     row.name = context_->storage->InternString(track_name.value().c_str());
   }
 
-  auto id = context_->storage->mutable_cpu_track_table()->Insert(row).id;
-
-  tracks_[{type, dims_id}] = id;
-  return id;
+  TrackId track_id =
+      context_->storage->mutable_cpu_track_table()->Insert(row).id;
+  tracks_[{type, dims_id}] = track_id;
+  return track_id;
 }
 
 TrackId TrackTracker::InternGlobalTrack(TrackClassification type) {
@@ -291,13 +409,6 @@
   return track_id;
 }
 
-TrackId TrackTracker::LegacyInternFuchsiaAsyncTrack(StringId name,
-                                                    uint32_t upid,
-                                                    int64_t correlation_id) {
-  return LegacyInternLegacyChromeAsyncTrack(name, upid, correlation_id, false,
-                                            StringId());
-}
-
 TrackId TrackTracker::InternGpuTrack(const tables::GpuTrackTable::Row& row) {
   DimensionsBuilder dims_builder = CreateDimensionsBuilder();
   dims_builder.AppendGpu(row.context_id.value_or(0));
@@ -322,141 +433,9 @@
   row_copy.dimensions = dims_id.arg_set_id;
   row_copy.machine_id = context_->machine_id();
 
-  auto id = context_->storage->mutable_gpu_track_table()->Insert(row_copy).id;
-  tracks_[key] = id;
-  return id;
-}
-
-TrackId TrackTracker::LegacyInternGpuWorkPeriodTrack(
-    const tables::GpuWorkPeriodTrackTable::Row& row) {
-  DimensionsBuilder dims_builder = CreateDimensionsBuilder();
-  dims_builder.AppendGpu(row.gpu_id);
-  dims_builder.AppendUid(row.uid);
-  Dimensions dims_id = std::move(dims_builder).Build();
-
-  TrackMapKey key;
-  key.classification = TrackClassification::kUnknown;
-  key.dimensions = dims_id;
-  key.name = row.name;
-
-  auto it = tracks_.Find(key);
-  if (it)
-    return *it;
-
-  tables::GpuWorkPeriodTrackTable::Row row_copy = row;
-  row_copy.classification = context_->storage->InternString(
-      GetClassification(TrackClassification::kUnknown).c_str());
-  row_copy.dimensions = dims_id.arg_set_id;
-  row_copy.machine_id = context_->machine_id();
-
-  auto id = context_->storage->mutable_gpu_work_period_track_table()
-                ->Insert(row_copy)
-                .id;
-  tracks_[key] = id;
-  return id;
-}
-
-TrackId TrackTracker::LegacyInternLegacyChromeAsyncTrack(
-    StringId raw_name,
-    uint32_t upid,
-    int64_t trace_id,
-    bool trace_id_is_process_scoped,
-    StringId source_scope) {
-  DimensionsBuilder dims_builder = CreateDimensionsBuilder();
-  dims_builder.AppendDimension(context_->storage->InternString("scope"),
-                               Variadic::String(source_scope));
-  if (trace_id_is_process_scoped) {
-    dims_builder.AppendUpid(upid);
-  }
-  Dimensions dims_id = std::move(dims_builder).Build();
-
-  const StringId name =
-      context_->process_track_translation_table->TranslateName(raw_name);
-
-  TrackMapKey key;
-  key.classification = TrackClassification::kUnknown;
-  key.cookie = trace_id;
-  key.dimensions = dims_id;
-
-  auto it = tracks_.Find(key);
-  if (it) {
-    if (name != kNullStringId) {
-      // The track may have been created for an end event without name. In
-      // that case, update it with this event's name.
-      auto& tracks = *context_->storage->mutable_track_table();
-      auto rr = *tracks.FindById(*it);
-      if (rr.name() == kNullStringId) {
-        rr.set_name(name);
-      }
-    }
-    return *it;
-  }
-
-  // Legacy async tracks are always drawn in the context of a process, even if
-  // the ID's scope is global.
-  tables::ProcessTrackTable::Row track(name);
-  track.upid = upid;
-  track.classification = context_->storage->InternString(
-      GetClassification(TrackClassification::kUnknown).c_str());
-  track.dimensions = dims_id.arg_set_id;
-  track.machine_id = context_->machine_id();
-
-  TrackId id =
-      context_->storage->mutable_process_track_table()->Insert(track).id;
-  tracks_[key] = id;
-
-  context_->args_tracker->AddArgsTo(id)
-      .AddArg(source_key_, Variadic::String(chrome_source_))
-      .AddArg(trace_id_key_, Variadic::Integer(trace_id))
-      .AddArg(trace_id_is_process_scoped_key_,
-              Variadic::Boolean(trace_id_is_process_scoped))
-      .AddArg(source_scope_key_, Variadic::String(source_scope));
-
-  return id;
-}
-
-TrackId TrackTracker::LegacyCreateGlobalAsyncTrack(StringId name,
-                                                   StringId source) {
-  Dimensions dims_id = SingleDimension(name_id_, Variadic::String(name));
-  TrackId track_id = CreateAsyncTrack(dims_id, name);
-  if (!source.is_null()) {
-    context_->args_tracker->AddArgsTo(track_id).AddArg(
-        source_key_, Variadic::String(source));
-  }
-  return track_id;
-}
-
-TrackId TrackTracker::LegacyCreateProcessAsyncTrack(StringId raw_name,
-                                                    UniquePid upid,
-                                                    StringId source) {
-  const StringId name =
-      context_->process_track_translation_table->TranslateName(raw_name);
-  DimensionsBuilder dims_builder = CreateDimensionsBuilder();
-  dims_builder.AppendName(name);
-  dims_builder.AppendUpid(upid);
-  Dimensions dims_id = std::move(dims_builder).Build();
-
-  tables::ProcessTrackTable::Row row(name);
-  row.upid = upid;
-  row.classification = context_->storage->InternString(
-      GetClassification(TrackClassification::kAsync).c_str());
-  row.machine_id = context_->machine_id();
-  row.dimensions = dims_id.arg_set_id;
-
-  auto id = context_->storage->mutable_process_track_table()->Insert(row).id;
-  if (!source.is_null()) {
-    context_->args_tracker->AddArgsTo(id).AddArg(source_key_,
-                                                 Variadic::String(source));
-  }
-  return id;
-}
-
-TrackId TrackTracker::InternLegacyChromeProcessInstantTrack(UniquePid upid) {
-  TrackId track_id = InternProcessTrack(
-      TrackTracker::TrackClassification::kChromeProcessInstant, upid);
-  context_->args_tracker->AddArgsTo(track_id).AddArg(
-      source_key_, Variadic::String(chrome_source_));
-
+  TrackId track_id =
+      context_->storage->mutable_gpu_track_table()->Insert(row_copy).id;
+  tracks_[key] = track_id;
   return track_id;
 }
 
@@ -525,7 +504,6 @@
 
   TrackId track_id =
       context_->storage->mutable_cpu_counter_track_table()->Insert(row).id;
-
   tracks_[key] = track_id;
   return track_id;
 }
@@ -560,69 +538,10 @@
 
   TrackId track_id =
       context_->storage->mutable_cpu_counter_track_table()->Insert(row).id;
-
   tracks_[{classification, dims_id}] = track_id;
   return track_id;
 }
 
-TrackId TrackTracker::InternThreadCounterTrack(StringId name, UniqueTid utid) {
-  TrackMapKey key;
-  key.classification = TrackClassification::kUnknown;
-  key.name = name;
-  key.dimensions = SingleDimension(utid_id_, Variadic::Integer(utid));
-
-  auto it = tracks_.Find(key);
-  if (it) {
-    return *it;
-  }
-
-  tables::ThreadCounterTrackTable::Row row(name);
-  row.utid = utid;
-  row.machine_id = context_->machine_id();
-  row.dimensions = key.dimensions->arg_set_id;
-  row.classification = context_->storage->InternString(
-      GetClassification(key.classification).c_str());
-
-  TrackId track_id =
-      context_->storage->mutable_thread_counter_track_table()->Insert(row).id;
-
-  tracks_[key] = track_id;
-  return track_id;
-}
-
-TrackId TrackTracker::InternProcessCounterTrack(StringId raw_name,
-                                                UniquePid upid,
-                                                StringId unit,
-                                                StringId description) {
-  const StringId name =
-      context_->process_track_translation_table->TranslateName(raw_name);
-
-  TrackMapKey key;
-  key.classification = TrackClassification::kUnknown;
-  key.name = name;
-  key.dimensions = SingleDimension(upid_id_, Variadic::Integer(upid));
-
-  auto it = tracks_.Find(key);
-  if (it) {
-    return *it;
-  }
-
-  tables::ProcessCounterTrackTable::Row row(name);
-  row.upid = upid;
-  row.unit = unit;
-  row.description = description;
-  row.machine_id = context_->machine_id();
-  row.classification = context_->storage->InternString(
-      GetClassification(key.classification).c_str());
-  row.dimensions = key.dimensions->arg_set_id;
-
-  TrackId track_id =
-      context_->storage->mutable_process_counter_track_table()->Insert(row).id;
-
-  tracks_[key] = track_id;
-  return track_id;
-}
-
 TrackId TrackTracker::LegacyInternIrqCounterTrack(TrackClassification type,
                                                   int32_t irq) {
   Dimensions dims_id = SingleDimension(context_->storage->InternString("irq"),
@@ -645,9 +564,7 @@
 
   TrackId track_id =
       context_->storage->mutable_irq_counter_track_table()->Insert(row).id;
-
   tracks_[{type, dims_id}] = track_id;
-
   return track_id;
 }
 
@@ -672,8 +589,8 @@
 
   TrackId track_id =
       context_->storage->mutable_softirq_counter_track_table()->Insert(row).id;
-  tracks_[{type, dims_id}] = track_id;
 
+  tracks_[{type, dims_id}] = track_id;
   return track_id;
 }
 
@@ -729,10 +646,11 @@
   row.classification = context_->storage->InternString(
       GetClassification(TrackClassification::kUnknown).c_str());
 
-  TrackId track =
+  TrackId track_id =
       context_->storage->mutable_energy_counter_track_table()->Insert(row).id;
-  tracks_[key] = track;
-  return track;
+
+  tracks_[key] = track_id;
+  return track_id;
 }
 
 TrackId TrackTracker::LegacyInternLegacyEnergyPerUidCounterTrack(
@@ -764,12 +682,13 @@
   row.classification = context_->storage->InternString(
       GetClassification(TrackClassification::kUnknown).c_str());
 
-  TrackId track =
+  TrackId track_id =
       context_->storage->mutable_energy_per_uid_counter_track_table()
           ->Insert(row)
           .id;
-  tracks_[key] = track;
-  return track;
+
+  tracks_[key] = track_id;
+  return track_id;
 }
 
 TrackId TrackTracker::LegacyInternLinuxDeviceTrack(StringId name) {
@@ -785,10 +704,10 @@
   row.classification = context_->storage->InternString(
       GetClassification(TrackClassification::kLinuxDevice).c_str());
 
-  TrackId track =
+  TrackId track_id =
       context_->storage->mutable_linux_device_track_table()->Insert(row).id;
-  tracks_[{TrackClassification::kLinuxDevice, dims_id}] = track;
-  return track;
+  tracks_[{TrackClassification::kLinuxDevice, dims_id}] = track_id;
+  return track_id;
 }
 
 TrackId TrackTracker::LegacyCreateGpuCounterTrack(StringId name,
@@ -845,5 +764,100 @@
   return track_id;
 }
 
+TrackId TrackTracker::LegacyInternGpuWorkPeriodTrack(
+    const tables::GpuWorkPeriodTrackTable::Row& row) {
+  DimensionsBuilder dims_builder = CreateDimensionsBuilder();
+  dims_builder.AppendGpu(row.gpu_id);
+  dims_builder.AppendUid(row.uid);
+  Dimensions dims_id = std::move(dims_builder).Build();
+
+  TrackMapKey key;
+  key.classification = TrackClassification::kUnknown;
+  key.dimensions = dims_id;
+  key.name = row.name;
+
+  auto it = tracks_.Find(key);
+  if (it)
+    return *it;
+
+  tables::GpuWorkPeriodTrackTable::Row row_copy = row;
+  row_copy.classification = context_->storage->InternString(
+      GetClassification(TrackClassification::kUnknown).c_str());
+  row_copy.dimensions = dims_id.arg_set_id;
+  row_copy.machine_id = context_->machine_id();
+
+  TrackId track_id = context_->storage->mutable_gpu_work_period_track_table()
+                         ->Insert(row_copy)
+                         .id;
+  tracks_[key] = track_id;
+  return track_id;
+}
+
+TrackId TrackTracker::LegacyInternFuchsiaAsyncTrack(StringId name,
+                                                    uint32_t upid,
+                                                    int64_t correlation_id) {
+  return LegacyInternLegacyChromeAsyncTrack(name, upid, correlation_id, false,
+                                            StringId());
+}
+
+TrackId TrackTracker::LegacyInternLegacyChromeAsyncTrack(
+    StringId raw_name,
+    uint32_t upid,
+    int64_t trace_id,
+    bool trace_id_is_process_scoped,
+    StringId source_scope) {
+  DimensionsBuilder dims_builder = CreateDimensionsBuilder();
+  dims_builder.AppendDimension(context_->storage->InternString("scope"),
+                               Variadic::String(source_scope));
+  if (trace_id_is_process_scoped) {
+    dims_builder.AppendUpid(upid);
+  }
+  Dimensions dims_id = std::move(dims_builder).Build();
+
+  const StringId name =
+      context_->process_track_translation_table->TranslateName(raw_name);
+
+  TrackMapKey key;
+  key.classification = TrackClassification::kUnknown;
+  key.cookie = trace_id;
+  key.dimensions = dims_id;
+
+  auto it = tracks_.Find(key);
+  if (it) {
+    if (name != kNullStringId) {
+      // The track may have been created for an end event without name. In
+      // that case, update it with this event's name.
+      auto& tracks = *context_->storage->mutable_track_table();
+      auto rr = *tracks.FindById(*it);
+      if (rr.name() == kNullStringId) {
+        rr.set_name(name);
+      }
+    }
+    return *it;
+  }
+
+  // Legacy async tracks are always drawn in the context of a process, even if
+  // the ID's scope is global.
+  tables::ProcessTrackTable::Row track(name);
+  track.upid = upid;
+  track.classification = context_->storage->InternString(
+      GetClassification(TrackClassification::kUnknown).c_str());
+  track.dimensions = dims_id.arg_set_id;
+  track.machine_id = context_->machine_id();
+
+  TrackId id =
+      context_->storage->mutable_process_track_table()->Insert(track).id;
+  tracks_[key] = id;
+
+  context_->args_tracker->AddArgsTo(id)
+      .AddArg(source_key_, Variadic::String(chrome_source_))
+      .AddArg(trace_id_key_, Variadic::Integer(trace_id))
+      .AddArg(trace_id_is_process_scoped_key_,
+              Variadic::Boolean(trace_id_is_process_scoped))
+      .AddArg(source_scope_key_, Variadic::String(source_scope));
+
+  return id;
+}
+
 }  // namespace trace_processor
 }  // namespace perfetto
diff --git a/src/trace_processor/importers/common/track_tracker.h b/src/trace_processor/importers/common/track_tracker.h
index cb8906a..a2bd592 100644
--- a/src/trace_processor/importers/common/track_tracker.h
+++ b/src/trace_processor/importers/common/track_tracker.h
@@ -134,6 +134,7 @@
     kLinuxDevice,
     kChromeProcessInstant,
     kAsync,
+    kTrackEvent,
 
     // Irq tracks.
     kIrqCount,
@@ -209,7 +210,7 @@
   // Interns track into TrackTable. If the track created with below arguments
   // already exists, returns the TrackTable::Id of the track.
   TrackId InternTrack(TrackClassification,
-                      std::optional<Dimensions> dimensions,
+                      std::optional<Dimensions>,
                       StringId name);
 
   // Interns a unique track into the storage.
@@ -225,15 +226,28 @@
                                    StringId description = kNullStringId);
 
   // Interns a thread track into the storage.
-  TrackId InternThreadTrack(UniqueTid);
+  TrackId InternThreadTrack(UniqueTid,
+                            std::optional<Dimensions> = std::nullopt);
 
   // Interns a counter track associated with a thread into the storage.
   // TODO(mayzner): Cleanup the arguments to be consistent with other Intern
   // functions.
-  TrackId InternThreadCounterTrack(StringId name, UniqueTid);
+  TrackId InternThreadCounterTrack(StringId name,
+                                   UniqueTid,
+                                   std::optional<Dimensions> = std::nullopt);
+
+  TrackId InternProcessTrack(
+      TrackClassification,
+      UniquePid,
+      std::optional<Dimensions> use_other_dimension = std::nullopt,
+      StringId name = kNullStringId);
 
   // Interns a process track into the storage.
-  TrackId InternProcessTrack(UniquePid);
+  TrackId InternProcessTrack(UniquePid,
+                             std::optional<Dimensions> = std::nullopt);
+
+  TrackId InternProcessCounterTrack(UniquePid,
+                                    std::optional<Dimensions> = std::nullopt);
 
   // Interns a counter track associated with a process into the storage.
   // TODO(mayzner): Cleanup the arguments to be consistent with other Intern
@@ -257,13 +271,6 @@
   // Interns a counter track associated with a GPU into the storage.
   TrackId InternGpuCounterTrack(TrackClassification, uint32_t gpu_id);
 
-  // Pushes async track into TrackTable. Returns the TrackTable::Id of the newly
-  // created track.
-  // TODO(mayzner): make name optional and later remove the option of adding it
-  // fully. Those changes can be made when we add support for autogenerating
-  // names.
-  TrackId CreateAsyncTrack(std::optional<Dimensions> dimensions, StringId name);
-
   // Interns a GPU work period track into the storage.
   // TODO(mayzner): Remove when all usages migrated to new track design.
   TrackId LegacyInternGpuWorkPeriodTrack(
@@ -277,11 +284,6 @@
                                              bool trace_id_is_process_scoped,
                                              StringId source_scope);
 
-  // Interns a track for legacy Chrome process-scoped instant events into the
-  // storage.
-  // TODO(mayzner): Remove when all usages migrated to new track design.
-  TrackId InternLegacyChromeProcessInstantTrack(UniquePid);
-
   // Interns a counter track associated with a cpu into the storage.
   // TODO(mayzner): Remove when all usages migrated to new track design.
   TrackId LegacyInternCpuIdleStateTrack(uint32_t cpu, StringId state);
@@ -335,20 +337,10 @@
                                         uint32_t upid,
                                         int64_t correlation_id);
 
-  // NOTE:
-  // The below method should only be called by AsyncTrackSetTracker
-
-  // Creates and inserts a global async track into the storage.
-  // TODO(mayzner): Remove when all usages migrated to new track design.
-  TrackId LegacyCreateGlobalAsyncTrack(StringId name, StringId source);
-
-  // Creates and inserts a Android async track into the storage.
-  // TODO(mayzner): Remove when all usages migrated to new track design.
-  TrackId LegacyCreateProcessAsyncTrack(StringId name,
-                                        UniquePid upid,
-                                        StringId source);
-
  private:
+  friend class AsyncTrackSetTracker;
+  friend class TrackEventTracker;
+
   struct TrackMapKey {
     TrackClassification classification;
     std::optional<Dimensions> dimensions;
@@ -424,6 +416,8 @@
         return "linux_device";
       case TrackClassification::kAsync:
         return "async";
+      case TrackClassification::kTrackEvent:
+        return "track_event";
       case TrackClassification::kIrqCount:
         return "irq_count_num";
       case TrackClassification::kSoftirqCount:
@@ -485,13 +479,33 @@
     PERFETTO_FATAL("For GCC");
   }
 
-  TrackId InternTrackForGroup(Group);
+  TrackId CreateTrack(TrackClassification,
+                      std::optional<Dimensions>,
+                      StringId name);
 
-  TrackId InternProcessTrack(
-      TrackClassification,
-      UniquePid,
-      std::optional<Dimensions> use_other_dimension = std::nullopt,
-      StringId name = kNullStringId);
+  TrackId CreateCounterTrack(TrackClassification,
+                             std::optional<Dimensions>,
+                             StringId name);
+
+  TrackId CreateThreadTrack(TrackClassification,
+                            UniqueTid,
+                            std::optional<Dimensions> = std::nullopt);
+
+  TrackId CreateThreadCounterTrack(TrackClassification,
+                                   StringId name,
+                                   UniqueTid,
+                                   std::optional<Dimensions> = std::nullopt);
+
+  TrackId CreateProcessTrack(TrackClassification,
+                             UniquePid,
+                             std::optional<Dimensions> = std::nullopt,
+                             StringId name = kNullStringId);
+
+  TrackId CreateProcessCounterTrack(TrackClassification,
+                                    UniquePid,
+                                    std::optional<Dimensions> = std::nullopt);
+
+  TrackId InternTrackForGroup(Group);
 
   std::array<std::optional<TrackId>, kGroupCount> group_track_ids_;
 
diff --git a/src/trace_processor/importers/json/json_trace_parser_impl.cc b/src/trace_processor/importers/json/json_trace_parser_impl.cc
index ba2e08a..617fd3e 100644
--- a/src/trace_processor/importers/json/json_trace_parser_impl.cc
+++ b/src/trace_processor/importers/json/json_trace_parser_impl.cc
@@ -296,9 +296,11 @@
           break;
         }
         UniquePid upid = context_->process_tracker->GetOrCreateProcess(pid);
-        track_id =
-            context_->track_tracker->InternLegacyChromeProcessInstantTrack(
-                upid);
+        track_id = context_->track_tracker->InternProcessTrack(
+            TrackTracker::TrackClassification::kChromeProcessInstant, upid);
+        context_->args_tracker->AddArgsTo(track_id).AddArg(
+            context_->storage->InternString("source"),
+            Variadic::String(context_->storage->InternString("chrome")));
       } else if (scope == "t" || scope.data() == nullptr) {
         if (!opt_tid) {
           context_->storage->IncrementStats(stats::json_parser_failure);
diff --git a/src/trace_processor/importers/proto/track_event_parser.cc b/src/trace_processor/importers/proto/track_event_parser.cc
index afb0244..b73b47a 100644
--- a/src/trace_processor/importers/proto/track_event_parser.cc
+++ b/src/trace_processor/importers/proto/track_event_parser.cc
@@ -525,9 +525,12 @@
                   "Process-scoped instant event without process association");
             }
 
-            track_id_ =
-                context_->track_tracker->InternLegacyChromeProcessInstantTrack(
-                    *upid_);
+            track_id_ = context_->track_tracker->InternProcessTrack(
+                TrackTracker::TrackClassification::kChromeProcessInstant,
+                *upid_);
+            context_->args_tracker->AddArgsTo(track_id_).AddArg(
+                context_->storage->InternString("source"),
+                Variadic::String(context_->storage->InternString("chrome")));
             legacy_passthrough_utid_ = utid_;
             utid_ = std::nullopt;
             break;
diff --git a/src/trace_processor/importers/proto/track_event_tracker.cc b/src/trace_processor/importers/proto/track_event_tracker.cc
index a336b60..0252bee 100644
--- a/src/trace_processor/importers/proto/track_event_tracker.cc
+++ b/src/trace_processor/importers/proto/track_event_tracker.cc
@@ -34,7 +34,6 @@
 #include "src/trace_processor/importers/common/track_tracker.h"
 #include "src/trace_processor/storage/stats.h"
 #include "src/trace_processor/storage/trace_storage.h"
-#include "src/trace_processor/tables/track_tables_py.h"
 #include "src/trace_processor/types/trace_processor_context.h"
 #include "src/trace_processor/types/variadic.h"
 
@@ -175,22 +174,6 @@
   context_->storage->IncrementStats(stats::track_event_tokenizer_errors);
 }
 
-TrackId TrackEventTracker::InsertThreadTrack(UniqueTid utid) {
-  tables::ThreadTrackTable::Row row;
-  row.utid = utid;
-  row.machine_id = context_->machine_id();
-  auto* thread_tracks = context_->storage->mutable_thread_track_table();
-  return thread_tracks->Insert(row).id;
-}
-
-TrackId TrackEventTracker::InternThreadTrack(UniqueTid utid) {
-  auto it = thread_tracks_.find(utid);
-  if (it != thread_tracks_.end()) {
-    return it->second;
-  }
-  return thread_tracks_[utid] = InsertThreadTrack(utid);
-}
-
 std::optional<TrackId> TrackEventTracker::GetDescriptorTrack(
     uint64_t uuid,
     StringId event_name,
@@ -202,8 +185,7 @@
 
   // Update the name of the track if unset and the track is not the primary
   // track of a process/thread or a counter track.
-  auto* tracks = context_->storage->mutable_track_table();
-  auto rr = *tracks->FindById(*track_id);
+  auto rr = *context_->storage->mutable_track_table()->FindById(*track_id);
   if (!rr.name().is_null()) {
     return track_id;
   }
@@ -263,8 +245,7 @@
     args.AddArg(has_first_packet_on_sequence_key_id_, Variadic::Boolean(true));
   }
 
-  auto* tracks = context_->storage->mutable_track_table();
-  auto row_ref = *tracks->FindById(track_id);
+  auto row_ref = *context_->storage->mutable_track_table()->FindById(track_id);
   if (parent_id) {
     row_ref.set_parent_id(*parent_id);
   }
@@ -284,7 +265,14 @@
     switch (track.scope()) {
       case ResolvedDescriptorTrack::Scope::kThread: {
         if (track.use_separate_track()) {
-          return InternThreadTrack(track.utid());
+          auto it = thread_tracks_.find(track.utid());
+          if (it != thread_tracks_.end()) {
+            return it->second;
+          }
+          return thread_tracks_[track.utid()] =
+                     context_->track_tracker->CreateThreadTrack(
+                         TrackTracker::TrackClassification::kTrackEvent,
+                         track.utid());
         }
         return context_->track_tracker->InternThreadTrack(track.utid());
       }
@@ -296,47 +284,35 @@
     }
   }
 
+  if (track.is_counter()) {
+    switch (track.scope()) {
+      case ResolvedDescriptorTrack::Scope::kThread:
+        return context_->track_tracker->CreateThreadCounterTrack(
+            TrackTracker::TrackClassification::kTrackEvent, kNullStringId,
+            track.utid());
+      case ResolvedDescriptorTrack::Scope::kProcess:
+        return context_->track_tracker->CreateProcessCounterTrack(
+            TrackTracker::TrackClassification::kTrackEvent, track.upid());
+      case ResolvedDescriptorTrack::Scope::kGlobal:
+        return context_->track_tracker->CreateCounterTrack(
+            TrackTracker::TrackClassification::kTrackEvent, std::nullopt,
+            kNullStringId);
+    }
+  }
+
   switch (track.scope()) {
     case ResolvedDescriptorTrack::Scope::kThread: {
-      if (track.is_counter()) {
-        tables::ThreadCounterTrackTable::Row row;
-        row.utid = track.utid();
-        row.machine_id = context_->machine_id();
-
-        auto* thread_counter_tracks =
-            context_->storage->mutable_thread_counter_track_table();
-        return thread_counter_tracks->Insert(row).id;
-      }
-
-      return InsertThreadTrack(track.utid());
+      return context_->track_tracker->CreateThreadTrack(
+          TrackTracker::TrackClassification::kTrackEvent, track.utid());
     }
     case ResolvedDescriptorTrack::Scope::kProcess: {
-      if (track.is_counter()) {
-        tables::ProcessCounterTrackTable::Row row;
-        row.upid = track.upid();
-        row.machine_id = context_->machine_id();
-
-        auto* process_counter_tracks =
-            context_->storage->mutable_process_counter_track_table();
-        return process_counter_tracks->Insert(row).id;
-      }
-
-      tables::ProcessTrackTable::Row row;
-      row.upid = track.upid();
-      row.machine_id = context_->machine_id();
-
-      auto* process_tracks = context_->storage->mutable_process_track_table();
-      return process_tracks->Insert(row).id;
+      return context_->track_tracker->CreateProcessTrack(
+          TrackTracker::TrackClassification::kTrackEvent, track.upid());
     }
     case ResolvedDescriptorTrack::Scope::kGlobal: {
-      if (track.is_counter()) {
-        tables::CounterTrackTable::Row row;
-        row.machine_id = context_->machine_id();
-        return context_->storage->mutable_counter_track_table()->Insert(row).id;
-      }
-      tables::TrackTable::Row row;
-      row.machine_id = context_->machine_id();
-      return context_->storage->mutable_track_table()->Insert(row).id;
+      return context_->track_tracker->CreateTrack(
+          TrackTracker::TrackClassification::kTrackEvent, std::nullopt,
+          kNullStringId);
     }
   }
   PERFETTO_FATAL("For GCC");
diff --git a/src/trace_processor/importers/proto/track_event_tracker.h b/src/trace_processor/importers/proto/track_event_tracker.h
index e32138d..351adef 100644
--- a/src/trace_processor/importers/proto/track_event_tracker.h
+++ b/src/trace_processor/importers/proto/track_event_tracker.h
@@ -133,13 +133,6 @@
   // GetDescriptorTrack is moved back.
   TrackId GetOrCreateDefaultDescriptorTrack();
 
-  // Track events timestamps in Chrome have microsecond resolution, while
-  // system events use nanoseconds. It results in broken event nesting when
-  // track events and system events share a track.
-  // So TrackEventTracker needs to support its own tracks, separate from the
-  // ones in the TrackTracker.
-  TrackId InternThreadTrack(UniqueTid utid);
-
   // Called by ProtoTraceReader whenever incremental state is cleared on a
   // packet sequence. Resets counter values for any incremental counters of
   // the sequence identified by |packet_sequence_id|.
@@ -238,7 +231,6 @@
       uint64_t uuid,
       const DescriptorTrackReservation&,
       std::vector<uint64_t>* descendent_uuids);
-  TrackId InsertThreadTrack(UniqueTid utid);
 
   static constexpr uint64_t kDefaultDescriptorTrackUuid = 0u;