trace_processor: Refactor in order to implement global ordering.

This CL contains the changes to the end of the pipeline.
Specifically, splitting the handling of sched and process events from
the storage into their own class. Trace storage now only stores things.

The main pieces of new code are the Trace processor context and some
handling to clear the TraceStorage. All the rest has mainly just been
moved.

Bug:111389336
Change-Id: I5810375f24bc48837fff73b7ba60ee12ff2f2e1d
diff --git a/src/trace_processor/trace_storage.cc b/src/trace_processor/trace_storage.cc
index 1e6bcac..9bd9c79 100644
--- a/src/trace_processor/trace_storage.cc
+++ b/src/trace_processor/trace_storage.cc
@@ -29,84 +29,12 @@
 
 TraceStorage::~TraceStorage() {}
 
-void TraceStorage::PushSchedSwitch(uint32_t cpu,
-                                   uint64_t timestamp,
-                                   uint32_t prev_pid,
-                                   uint32_t prev_state,
-                                   const char* prev_comm,
-                                   size_t prev_comm_len,
-                                   uint32_t next_pid) {
-  SchedSwitchEvent* prev = &last_sched_per_cpu_[cpu];
-
-  // If we had a valid previous event, then inform the storage about the
-  // slice.
-  if (prev->valid() && prev->next_pid != 0 /* Idle process (swapper/N) */) {
-    uint64_t duration = timestamp - prev->timestamp;
-    cpu_events_[cpu].AddSlice(prev->timestamp, duration, prev->prev_pid,
-                              prev->prev_thread_name_id);
-  } else {
-    cpu_events_[cpu].InitalizeSlices(this);
-  }
-
-  // If the this events previous pid does not match the previous event's next
-  // pid, make a note of this.
-  if (prev_pid != prev->next_pid) {
-    stats_.mismatched_sched_switch_tids_++;
-  }
-
-  // Update the map with the current event.
-  prev->cpu = cpu;
-  prev->timestamp = timestamp;
-  prev->prev_pid = prev_pid;
-  prev->prev_state = prev_state;
-  prev->prev_thread_name_id = InternString(prev_comm, prev_comm_len);
-  prev->next_pid = next_pid;
-}
-
-void TraceStorage::PushProcess(uint32_t pid,
-                               const char* process_name,
-                               size_t process_name_len) {
-  auto pids_pair = UpidsForPid(pid);
-  auto proc_name_id = InternString(process_name, process_name_len);
-
-  // We only create a new upid if there isn't one for that pid.
-  if (pids_pair.first == pids_pair.second) {
-    pids_.emplace(pid, unique_processes_.size());
-    Process new_process;
-    new_process.name_id = proc_name_id;
-    unique_processes_.emplace_back(std::move(new_process));
-  }
-}
-
-void TraceStorage::MatchThreadToProcess(uint32_t tid, uint32_t tgid) {
-  auto tids_pair = UtidsForTid(tid);
-  // We only care about tids for which we have a matching utid.
-  PERFETTO_DCHECK(std::distance(tids_pair.first, tids_pair.second) <= 1);
-  if (tids_pair.first != tids_pair.second) {
-    PERFETTO_DCHECK(tids_pair.first->second < unique_threads_.size());
-    Thread* thread = &unique_threads_[tids_pair.first->second];
-    // If no upid is set - look it up.
-    if (thread->upid == 0) {
-      auto pids_pair = UpidsForPid(tgid);
-      PERFETTO_DCHECK(std::distance(pids_pair.first, pids_pair.second) <= 1);
-      if (pids_pair.first != pids_pair.second) {
-        thread->upid = pids_pair.first->second;
-        // If this is the first time we've used this process, set start_ns.
-        Process* process = &unique_processes_[pids_pair.first->second];
-        if (process->start_ns == 0)
-          process->start_ns = thread->start_ns;
-      }
-    }
-  }
-}
-
-TraceStorage::UniqueProcessRange TraceStorage::UpidsForPid(uint32_t pid) {
-  return pids_.equal_range(pid);
-}
-
-TraceStorage::UniqueThreadRange TraceStorage::UtidsForTid(uint32_t tid) {
-  return tids_.equal_range(tid);
-}
+void TraceStorage::AddSliceToCpu(uint32_t cpu,
+                                 uint64_t start_ns,
+                                 uint64_t duration_ns,
+                                 UniqueTid utid) {
+  cpu_events_[cpu].AddSlice(start_ns, duration_ns, utid);
+};
 
 TraceStorage::StringId TraceStorage::InternString(const char* data,
                                                   size_t length) {
@@ -128,5 +56,9 @@
   return string_id;
 }
 
+void TraceStorage::ResetStorage() {
+  *this = TraceStorage();
+}
+
 }  // namespace trace_processor
 }  // namespace perfetto