trace_processor: Match threads to processes using tgid.

Bug:80416541
Change-Id: Ic066ef0b7e5e24fe89585c1d1d7e158398d1c64b
diff --git a/src/trace_processor/trace_storage.h b/src/trace_processor/trace_storage.h
index c95c41d..8f5494c 100644
--- a/src/trace_processor/trace_storage.h
+++ b/src/trace_processor/trace_storage.h
@@ -45,15 +45,19 @@
   // Unix pids are reused and thus not guaranteed to be unique over a long
   // period of time.
   using UniquePid = uint32_t;
-
-  // UniqueTid is an offset into |unique_threads_|. Necessary because tids can
-  // be reused.
-  using UniqueTid = uint32_t;
   using UniqueProcessIterator =
       std::multimap<uint32_t, UniquePid>::const_iterator;
   using UniqueProcessRange =
       std::pair<UniqueProcessIterator, UniqueProcessIterator>;
 
+  // UniqueTid is an offset into |unique_threads_|. Necessary because tids can
+  // be reused.
+  using UniqueTid = uint32_t;
+  using UniqueThreadIterator =
+      std::multimap<uint32_t, UniqueTid>::const_iterator;
+  using UniqueThreadRange =
+      std::pair<UniqueThreadIterator, UniqueThreadIterator>;
+
   class SlicesPerCpu {
    public:
     inline void AddSlice(uint64_t start_ns,
@@ -64,16 +68,16 @@
       durations_.emplace_back(duration_ns);
 
       auto pair_it = storage_->tids_.equal_range(tid);
-
       // If there is a previous utid for that tid, use that.
       if (pair_it.first != pair_it.second) {
         UniqueTid prev_utid = std::prev(pair_it.second)->second;
         utids_.emplace_back(prev_utid);
       } else {
         // If none exist, assign a new utid and store it.
-        TaskInfo new_thread;
+        Thread new_thread;
         new_thread.name_id = thread_name_id;
         new_thread.start_ns = start_ns;
+        new_thread.upid = 0;
         storage_->tids_.emplace(tid, storage_->unique_threads_.size());
         utids_.emplace_back(storage_->unique_threads_.size());
         storage_->unique_threads_.emplace_back(std::move(new_thread));
@@ -106,13 +110,21 @@
 
   virtual ~TraceStorage();
 
-  // Information about a unique process or thread seen in a trace.
-  struct TaskInfo {
+  // Information about a unique process seen in a trace.
+  struct Process {
     uint64_t start_ns = 0;
     uint64_t end_ns = 0;
     StringId name_id;
   };
 
+  // Information about a unique thread seen in a trace.
+  struct Thread {
+    uint64_t start_ns = 0;
+    uint64_t end_ns = 0;
+    StringId name_id;
+    UniquePid upid;
+  };
+
   // Adds a sched slice for a given cpu.
   // Virtual for testing.
   virtual void PushSchedSwitch(uint32_t cpu,
@@ -128,22 +140,29 @@
                            const char* process_name,
                            size_t process_name_len);
 
+  // Adds a thread entry for the tid.
+  virtual void MatchThreadToProcess(uint32_t tid, uint32_t tgid);
+
   // Returns the bounds of a range that includes all UniquePids that have the
   // requested pid.
   UniqueProcessRange UpidsForPid(uint32_t pid);
 
+  // Returns the bounds of a range that includes all UniqueTids that have the
+  // requested tid.
+  UniqueThreadRange UtidsForTid(uint32_t tid);
+
   // Reading methods.
   const SlicesPerCpu& SlicesForCpu(uint32_t cpu) const {
     PERFETTO_CHECK(cpu < cpu_events_.size());
     return cpu_events_[cpu];
   }
 
-  const TaskInfo& GetProcess(UniquePid upid) {
+  const Process& GetProcess(UniquePid upid) {
     PERFETTO_CHECK(upid < unique_processes_.size());
     return unique_processes_[upid];
   }
 
-  const TaskInfo& GetThread(UniqueTid utid) {
+  const Thread& GetThread(UniqueTid utid) {
     PERFETTO_CHECK(utid < unique_threads_.size());
     return unique_threads_[utid];
   }
@@ -191,14 +210,14 @@
   std::multimap<uint32_t, UniquePid> pids_;
 
   // One entry for each UniquePid, with UniquePid as the index.
-  std::deque<TaskInfo> unique_processes_;
+  std::deque<Process> unique_processes_;
 
   // Each tid can have multiple UniqueTid entries, a new UniqueTid is assigned
   // each time a thread is seen in the trace.
   std::multimap<uint32_t, UniqueTid> tids_;
 
   // One entry for each UniqueTid, with UniqueTid as the index.
-  std::deque<TaskInfo> unique_threads_;
+  std::deque<Thread> unique_threads_;
 };
 
 }  // namespace trace_processor