Simpleperf: correct the event type name printed in stat command.

Bug: 22884977
Change-Id: I3c4f5c8dc02d5b17f5942e4e98476d1c4f3fbcad
diff --git a/simpleperf/cmd_record.cpp b/simpleperf/cmd_record.cpp
index e4f1d41..060d22c 100644
--- a/simpleperf/cmd_record.cpp
+++ b/simpleperf/cmd_record.cpp
@@ -200,9 +200,8 @@
 
   // 4. Open record file writer, and dump kernel/modules/threads mmap information.
   record_file_writer_ = RecordFileWriter::CreateInstance(
-      record_filename_,
-      event_selection_set_.FindEventAttrByType(measured_event_type_modifier_->event_type),
-      event_selection_set_.FindEventFdsByType(measured_event_type_modifier_->event_type));
+      record_filename_, event_selection_set_.FindEventAttrByType(*measured_event_type_modifier_),
+      event_selection_set_.FindEventFdsByType(*measured_event_type_modifier_));
   if (record_file_writer_ == nullptr) {
     return false;
   }
@@ -413,7 +412,7 @@
     return false;
   }
   const perf_event_attr& attr =
-      event_selection_set_.FindEventAttrByType(measured_event_type_modifier_->event_type);
+      event_selection_set_.FindEventAttrByType(*measured_event_type_modifier_);
   MmapRecord mmap_record = CreateMmapRecord(attr, true, UINT_MAX, 0, kernel_mmap.start_addr,
                                             kernel_mmap.len, kernel_mmap.pgoff, kernel_mmap.name);
   if (!record_file_writer_->WriteData(mmap_record.BinaryFormat())) {
@@ -452,7 +451,7 @@
   }
 
   const perf_event_attr& attr =
-      event_selection_set_.FindEventAttrByType(measured_event_type_modifier_->event_type);
+      event_selection_set_.FindEventAttrByType(*measured_event_type_modifier_);
 
   // Dump processes.
   for (auto& thread : thread_comms) {
diff --git a/simpleperf/cmd_stat.cpp b/simpleperf/cmd_stat.cpp
index d4b1923..6358f50 100644
--- a/simpleperf/cmd_stat.cpp
+++ b/simpleperf/cmd_stat.cpp
@@ -80,14 +80,15 @@
   bool AddMeasuredEventType(const std::string& event_type_name);
   bool AddDefaultMeasuredEventTypes();
   bool SetEventSelection();
-  bool ShowCounters(const std::map<const EventType*, std::vector<PerfCounter>>& counters_map,
-                    std::chrono::steady_clock::duration counting_duration);
+  bool ShowCounters(
+      const std::map<const EventTypeAndModifier*, std::vector<PerfCounter>>& counters_map,
+      std::chrono::steady_clock::duration counting_duration);
 
   bool verbose_mode_;
   bool system_wide_collection_;
   bool child_inherit_;
   std::vector<pid_t> monitored_threads_;
-  std::vector<std::pair<std::string, EventTypeAndModifier>> measured_event_types_;
+  std::vector<EventTypeAndModifier> measured_event_types_;
   EventSelectionSet event_selection_set_;
 
   std::unique_ptr<SignalHandlerRegister> signal_handler_register_;
@@ -153,7 +154,7 @@
   auto end_time = std::chrono::steady_clock::now();
 
   // 5. Read and print counters.
-  std::map<const EventType*, std::vector<PerfCounter>> counters_map;
+  std::map<const EventTypeAndModifier*, std::vector<PerfCounter>> counters_map;
   if (!event_selection_set_.ReadCounters(&counters_map)) {
     return false;
   }
@@ -224,7 +225,7 @@
   if (event_type_modifier == nullptr) {
     return false;
   }
-  measured_event_types_.push_back(std::make_pair(event_type_name, *event_type_modifier));
+  measured_event_types_.push_back(*event_type_modifier);
   return true;
 }
 
@@ -244,8 +245,8 @@
 }
 
 bool StatCommand::SetEventSelection() {
-  for (auto& pair : measured_event_types_) {
-    if (!event_selection_set_.AddEventType(pair.second)) {
+  for (auto& event_type : measured_event_types_) {
+    if (!event_selection_set_.AddEventType(event_type)) {
       return false;
     }
   }
@@ -254,12 +255,12 @@
 }
 
 bool StatCommand::ShowCounters(
-    const std::map<const EventType*, std::vector<PerfCounter>>& counters_map,
+    const std::map<const EventTypeAndModifier*, std::vector<PerfCounter>>& counters_map,
     std::chrono::steady_clock::duration counting_duration) {
   printf("Performance counter statistics:\n\n");
 
   for (auto& pair : counters_map) {
-    auto& event_type = pair.first;
+    auto& event_type_modifier = pair.first;
     auto& counters = pair.second;
     if (verbose_mode_) {
       for (auto& counter : counters) {
@@ -288,14 +289,8 @@
                                             sum_counter.time_enabled / sum_counter.time_running);
       }
     }
-    std::string event_type_name;
-    for (auto& pair : measured_event_types_) {
-      if (pair.second.event_type.name == event_type->name) {
-        event_type_name = pair.first;
-      }
-    }
     printf("%'30" PRId64 "%s  %s\n", scaled_count, scaled ? "(scaled)" : "       ",
-           event_type_name.c_str());
+           event_type_modifier->name.c_str());
   }
   printf("\nTotal test time: %lf seconds.\n",
          std::chrono::duration_cast<std::chrono::duration<double>>(counting_duration).count());
diff --git a/simpleperf/event_selection_set.cpp b/simpleperf/event_selection_set.cpp
index 9d09b06..555246e 100644
--- a/simpleperf/event_selection_set.cpp
+++ b/simpleperf/event_selection_set.cpp
@@ -50,7 +50,7 @@
 
 bool EventSelectionSet::AddEventType(const EventTypeAndModifier& event_type_modifier) {
   EventSelection selection;
-  selection.event_type = event_type_modifier.event_type;
+  selection.event_type_modifier = event_type_modifier;
   selection.event_attr = CreateDefaultPerfEventAttr(event_type_modifier.event_type);
   selection.event_attr.exclude_user = event_type_modifier.exclude_user;
   selection.event_attr.exclude_kernel = event_type_modifier.exclude_kernel;
@@ -59,7 +59,7 @@
   selection.event_attr.exclude_guest = event_type_modifier.exclude_guest;
   selection.event_attr.precise_ip = event_type_modifier.precise_ip;
   if (!IsEventAttrSupportedByKernel(selection.event_attr)) {
-    LOG(ERROR) << "Event type '" << selection.event_type.name << "' is not supported by the kernel";
+    LOG(ERROR) << "Event type '" << event_type_modifier.name << "' is not supported by the kernel";
     return false;
   }
   selections_.push_back(std::move(selection));
@@ -184,8 +184,8 @@
       // As the online cpus can be enabled or disabled at runtime, we may not open event file for
       // all cpus successfully. But we should open at least one cpu successfully.
       if (open_per_thread == 0) {
-        PLOG(ERROR) << "failed to open perf event file for event_type " << selection.event_type.name
-                    << " for "
+        PLOG(ERROR) << "failed to open perf event file for event_type "
+                    << selection.event_type_modifier.name << " for "
                     << (tid == -1 ? "all threads" : android::base::StringPrintf(" thread %d", tid));
         return false;
       }
@@ -206,7 +206,7 @@
 }
 
 bool EventSelectionSet::ReadCounters(
-    std::map<const EventType*, std::vector<PerfCounter>>* counters_map) {
+    std::map<const EventTypeAndModifier*, std::vector<PerfCounter>>* counters_map) {
   for (auto& selection : selections_) {
     std::vector<PerfCounter> counters;
     for (auto& event_fd : selection.event_fds) {
@@ -216,7 +216,7 @@
       }
       counters.push_back(counter);
     }
-    counters_map->insert(std::make_pair(&selection.event_type, counters));
+    counters_map->insert(std::make_pair(&selection.event_type_modifier, counters));
   }
   return true;
 }
@@ -290,20 +290,21 @@
 }
 
 EventSelectionSet::EventSelection* EventSelectionSet::FindSelectionByType(
-    const EventType& event_type) {
+    const EventTypeAndModifier& event_type_modifier) {
   for (auto& selection : selections_) {
-    if (selection.event_type.name == event_type.name) {
+    if (selection.event_type_modifier.name == event_type_modifier.name) {
       return &selection;
     }
   }
   return nullptr;
 }
 
-const perf_event_attr& EventSelectionSet::FindEventAttrByType(const EventType& event_type) {
-  return FindSelectionByType(event_type)->event_attr;
+const perf_event_attr& EventSelectionSet::FindEventAttrByType(
+    const EventTypeAndModifier& event_type_modifier) {
+  return FindSelectionByType(event_type_modifier)->event_attr;
 }
 
 const std::vector<std::unique_ptr<EventFd>>& EventSelectionSet::FindEventFdsByType(
-    const EventType& event_type) {
-  return FindSelectionByType(event_type)->event_fds;
+    const EventTypeAndModifier& event_type_modifier) {
+  return FindSelectionByType(event_type_modifier)->event_fds;
 }
diff --git a/simpleperf/event_selection_set.h b/simpleperf/event_selection_set.h
index 229b424..afde0af 100644
--- a/simpleperf/event_selection_set.h
+++ b/simpleperf/event_selection_set.h
@@ -62,24 +62,25 @@
   bool OpenEventFilesForThreads(const std::vector<pid_t>& threads);
   bool OpenEventFilesForThreadsOnAllCpus(const std::vector<pid_t>& threads);
   bool EnableEvents();
-  bool ReadCounters(std::map<const EventType*, std::vector<PerfCounter>>* counters_map);
+  bool ReadCounters(std::map<const EventTypeAndModifier*, std::vector<PerfCounter>>* counters_map);
   void PreparePollForEventFiles(std::vector<pollfd>* pollfds);
   bool MmapEventFiles(size_t mmap_pages);
   bool ReadMmapEventData(std::function<bool(const char*, size_t)> callback);
 
   std::string FindEventFileNameById(uint64_t id);
-  const perf_event_attr& FindEventAttrByType(const EventType& event_type);
-  const std::vector<std::unique_ptr<EventFd>>& FindEventFdsByType(const EventType& event_type);
+  const perf_event_attr& FindEventAttrByType(const EventTypeAndModifier& event_type_modifier);
+  const std::vector<std::unique_ptr<EventFd>>& FindEventFdsByType(
+      const EventTypeAndModifier& event_type_modifier);
 
  private:
   bool OpenEventFiles(const std::vector<pid_t>& threads, const std::vector<int>& cpus);
 
   struct EventSelection {
-    EventType event_type;
+    EventTypeAndModifier event_type_modifier;
     perf_event_attr event_attr;
     std::vector<std::unique_ptr<EventFd>> event_fds;
   };
-  EventSelection* FindSelectionByType(const EventType& event_type);
+  EventSelection* FindSelectionByType(const EventTypeAndModifier& event_type_modifier);
 
   std::vector<EventSelection> selections_;
 
diff --git a/simpleperf/event_type.cpp b/simpleperf/event_type.cpp
index 526cfa5..508fb9b 100644
--- a/simpleperf/event_type.cpp
+++ b/simpleperf/event_type.cpp
@@ -105,7 +105,8 @@
 std::unique_ptr<EventTypeAndModifier> ParseEventType(const std::string& event_type_str) {
   static std::string modifier_characters = "ukhGHp";
   std::unique_ptr<EventTypeAndModifier> event_type_modifier(new EventTypeAndModifier);
-  std::string name = event_type_str;
+  event_type_modifier->name = event_type_str;
+  std::string event_type_name = event_type_str;
   std::string modifier;
   size_t comm_pos = event_type_str.rfind(':');
   if (comm_pos != std::string::npos) {
@@ -118,17 +119,17 @@
       }
     }
     if (match_modifier) {
-      name = event_type_str.substr(0, comm_pos);
+      event_type_name = event_type_str.substr(0, comm_pos);
       modifier = event_type_str.substr(comm_pos + 1);
     }
   }
-  const EventType* event_type = FindEventTypeByName(name);
+  const EventType* event_type = FindEventTypeByName(event_type_name);
   if (event_type == nullptr) {
     // Try if the modifier belongs to the event type name, like some tracepoint events.
     if (!modifier.empty()) {
-      name = event_type_str;
+      event_type_name = event_type_str;
       modifier.clear();
-      event_type = FindEventTypeByName(name);
+      event_type = FindEventTypeByName(event_type_name);
     }
     if (event_type == nullptr) {
       return nullptr;
diff --git a/simpleperf/event_type.h b/simpleperf/event_type.h
index df2f782..7273e52 100644
--- a/simpleperf/event_type.h
+++ b/simpleperf/event_type.h
@@ -45,6 +45,7 @@
 const EventType* FindEventTypeByName(const std::string& name);
 
 struct EventTypeAndModifier {
+  std::string name;
   EventType event_type;
   bool exclude_user;
   bool exclude_kernel;