trace_to_text: add event counts for each type of ftrace event

Bug: 73228552
Change-Id: I7fb8e6472e0519be8e6ab7d7e9b11e90385eaa36
diff --git a/tools/trace_to_text/main.cc b/tools/trace_to_text/main.cc
index 9e4f93b..dcbc20d 100644
--- a/tools/trace_to_text/main.cc
+++ b/tools/trace_to_text/main.cc
@@ -261,6 +261,7 @@
 
 void PrintFtraceStats(std::ostream* output,
                       uint64_t overwrite_count,
+                      std::map<FtraceEvent::EventCase, uint64_t> event_counts,
                       bool compact_output) {
   if (!compact_output)
     *output << "--------------------Ftrace Stats-------------------\n";
@@ -273,6 +274,27 @@
   }
   *output << std::string(line);
 
+  DiskSourceTree dst;
+  dst.MapPath("perfetto", "protos/perfetto");
+  MFE mfe;
+  Importer importer(&dst, &mfe);
+  const FileDescriptor* parsed_file =
+      importer.Import("perfetto/trace/ftrace/ftrace_event.proto");
+
+  DynamicMessageFactory dmf;
+  const Descriptor* ftrace_descriptor = parsed_file->message_type(0);
+  for (const auto& event_to_count : event_counts) {
+    const std::string& event_name =
+        ftrace_descriptor->FindFieldByNumber(event_to_count.first)->name();
+    uint64_t count = event_to_count.second;
+    if (compact_output) {
+      sprintf(line, "%s,%" PRIu64 "\n", event_name.c_str(), count);
+    } else {
+      sprintf(line, "%s count: %" PRIu64 "\n", event_name.c_str(), count);
+    }
+    *output << std::string(line);
+  }
+
   if (!compact_output)
     *output << "\n";
 }
@@ -394,7 +416,7 @@
             "tids_with_pinfo,%zu\ntids,%zu\ntids_with_pinfo_percentage,%zu\n",
             intersect.size(), tids_in_events.size(), thread_id_process_info);
   } else {
-    sprintf(matching, "Thread ids with process info: %zu/%zu -> %zu %%\n\n",
+    sprintf(matching, "Thread ids with process info: %zu/%zu -> %zu %%\n",
             intersect.size(), tids_in_events.size(), thread_id_process_info);
   }
   *output << std::string(matching);
@@ -409,6 +431,7 @@
   uint64_t start = std::numeric_limits<uint64_t>::max();
   uint64_t end = 0;
   uint64_t ftrace_overwrites = 0;
+  std::map<FtraceEvent::EventCase, uint64_t> ftrace_event_counts;
   std::multiset<uint64_t> ftrace_timestamps;
   std::set<pid_t> tids_in_tree;
   std::set<pid_t> tids_in_events;
@@ -418,11 +441,10 @@
   std::set<uint64_t> resolved_scan_inodes;
 
   ForEachPacketInTrace(
-      input, [&start, &end, &ftrace_overwrites, &ftrace_timestamps,
-              &tids_in_tree, &tids_in_events, &ftrace_inodes,
-              &ftrace_inode_count, &resolved_map_inodes,
+      input, [&start, &end, &ftrace_overwrites, &ftrace_event_counts,
+              &ftrace_timestamps, &tids_in_tree, &tids_in_events,
+              &ftrace_inodes, &ftrace_inode_count, &resolved_map_inodes,
               &resolved_scan_inodes](const protos::TracePacket& packet) {
-
         if (packet.has_process_tree()) {
           const ProcessTree& tree = packet.process_tree();
           for (Process process : tree.processes()) {
@@ -453,6 +475,8 @@
 
         uint64_t inode_number = 0;
         for (const FtraceEvent& event : bundle.event()) {
+          ftrace_event_counts[event.event_case()] += 1;
+
           if (ParseInode(event, &inode_number)) {
             ftrace_inodes.insert(inode_number);
             ftrace_inode_count++;
@@ -481,7 +505,8 @@
 
   if (!compact_output)
     PrintFtraceTrack(output, start, end, ftrace_timestamps);
-  PrintFtraceStats(output, ftrace_overwrites, compact_output);
+  PrintFtraceStats(output, ftrace_overwrites, ftrace_event_counts,
+                   compact_output);
   PrintProcessStats(output, tids_in_tree, tids_in_events, compact_output);
   PrintInodeStats(output, ftrace_inodes, ftrace_inode_count,
                   resolved_map_inodes, resolved_scan_inodes, compact_output);