trace_to_text: Store thread names

Store the thread names from prev sched switch events
to be used for any other ftrace events with the
same tid in the future.

Bug:119035890
Change-Id: I634cbc40d11215434434e369fdb77797d6fc4531
diff --git a/tools/trace_to_text/ftrace_event_formatter.cc b/tools/trace_to_text/ftrace_event_formatter.cc
index 913188a..2357eef 100644
--- a/tools/trace_to_text/ftrace_event_formatter.cc
+++ b/tools/trace_to_text/ftrace_event_formatter.cc
@@ -3534,13 +3534,21 @@
     uint64_t timestamp,
     uint32_t cpu,
     const protos::FtraceEvent& event,
-    const std::unordered_map<uint32_t /*tid*/, uint32_t /*tgid*/>& thread_map) {
+    const std::unordered_map<uint32_t /*tid*/, uint32_t /*tgid*/>& thread_map,
+    std::unordered_map<uint32_t /*tid*/, std::string>& thread_names) {
   // Sched_switch events contain the thread name so use that in the prefix.
   std::string name;
   if (event.has_sched_switch()) {
     name = event.sched_switch().prev_comm();
+    thread_names[event.pid()] = event.sched_switch().prev_comm();
   } else {
-    name = "<...>";
+    // For non sched switch events use name stored from a sched switch event.
+    auto it = thread_names.find(event.pid());
+    if (it != thread_names.end()) {
+      name = it->second;
+    } else {
+      name = "<...>";
+    }
   }
 
   std::string line = FormatEventText(event);
diff --git a/tools/trace_to_text/ftrace_event_formatter.h b/tools/trace_to_text/ftrace_event_formatter.h
index 593cee2..0309a0e 100644
--- a/tools/trace_to_text/ftrace_event_formatter.h
+++ b/tools/trace_to_text/ftrace_event_formatter.h
@@ -30,7 +30,8 @@
     uint64_t timestamp,
     uint32_t cpu,
     const protos::FtraceEvent&,
-    const std::unordered_map<uint32_t /*tid*/, uint32_t /*tgid*/>& thread_map);
+    const std::unordered_map<uint32_t /*tid*/, uint32_t /*tgid*/>& thread_map,
+    std::unordered_map<uint32_t /*tid*/, std::string>& thread_names);
 
 }  // namespace perfetto
 
diff --git a/tools/trace_to_text/trace_to_systrace.cc b/tools/trace_to_text/trace_to_systrace.cc
index 19818a9..0b2cc12 100644
--- a/tools/trace_to_text/trace_to_systrace.cc
+++ b/tools/trace_to_text/trace_to_systrace.cc
@@ -100,6 +100,7 @@
   std::vector<std::string> proc_dump;
   std::vector<std::string> thread_dump;
   std::unordered_map<uint32_t /*tid*/, uint32_t /*tgid*/> thread_map;
+  std::unordered_map<uint32_t /*tid*/, std::string> thread_names;
 
   std::vector<const char*> meminfo_strs = BuildMeminfoCounterNames();
   std::vector<const char*> vmstat_strs = BuildVmstatCounterNames();
@@ -107,7 +108,7 @@
   std::vector<protos::TracePacket> packets_to_process;
 
   ForEachPacketInTrace(
-      input, [&thread_map, &packets_to_process, &proc_dump,
+      input, [&thread_map, &packets_to_process, &proc_dump, &thread_names,
               &thread_dump](const protos::TracePacket& packet) {
         if (!packet.has_process_tree()) {
           packets_to_process.emplace_back(std::move(packet));
@@ -125,6 +126,9 @@
           // Populate thread map for matching tids to tgids.
           thread_map[static_cast<uint32_t>(thread.tid())] =
               static_cast<uint32_t>(thread.tgid());
+          if (thread.has_name()) {
+            thread_names[static_cast<uint32_t>(thread.tid())] = thread.name();
+          }
           std::string t = FormatThread(thread);
           thread_dump.emplace_back(t);
         }
@@ -135,7 +139,7 @@
       const FtraceEventBundle& bundle = packet.ftrace_events();
       for (const FtraceEvent& event : bundle.event()) {
         std::string line = FormatFtraceEvent(event.timestamp(), bundle.cpu(),
-                                             event, thread_map);
+                                             event, thread_map, thread_names);
         if (line == "")
           continue;
         ftrace_sorted.emplace(event.timestamp(), line);
@@ -153,7 +157,8 @@
         sprintf(str, "C|1|%s|%" PRIu64, meminfo_strs[meminfo.key()],
                 static_cast<uint64_t>(meminfo.value()));
         event.mutable_print()->set_buf(str);
-        ftrace_sorted.emplace(ts, FormatFtraceEvent(ts, 0, event, thread_map));
+        ftrace_sorted.emplace(
+            ts, FormatFtraceEvent(ts, 0, event, thread_map, thread_names));
       }
       for (const auto& vmstat : sys_stats.vmstat()) {
         FtraceEvent event;
@@ -164,7 +169,8 @@
         sprintf(str, "C|1|%s|%" PRIu64, vmstat_strs[vmstat.key()],
                 static_cast<uint64_t>(vmstat.value()));
         event.mutable_print()->set_buf(str);
-        ftrace_sorted.emplace(ts, FormatFtraceEvent(ts, 0, event, thread_map));
+        ftrace_sorted.emplace(
+            ts, FormatFtraceEvent(ts, 0, event, thread_map, thread_names));
       }
     }
   }