OSDN Git Service

Simpleperf: dump thread comm/mmaps for selected threads.
authorYabin Cui <yabinc@google.com>
Thu, 16 Jul 2015 17:20:55 +0000 (10:20 -0700)
committerYabin Cui <yabinc@google.com>
Thu, 16 Jul 2015 17:20:55 +0000 (10:20 -0700)
Bug: 22511834
Change-Id: I2775b3a4cd393d75d5c6ebcd8a46e366ceb4ff76

simpleperf/cmd_record.cpp

index 71c4e4c..dd77ec3 100644 (file)
@@ -119,7 +119,7 @@ class RecordCommand : public Command {
   bool SetEventSelection();
   bool WriteData(const char* data, size_t size);
   bool DumpKernelAndModuleMmaps();
-  bool DumpThreadCommAndMmaps();
+  bool DumpThreadCommAndMmaps(bool all_threads, const std::vector<pid_t>& selected_threads);
   bool DumpAdditionalFeatures(const std::vector<std::string>& args);
   bool DumpBuildIdFeature();
 
@@ -207,7 +207,7 @@ bool RecordCommand::Run(const std::vector<std::string>& args) {
   if (!DumpKernelAndModuleMmaps()) {
     return false;
   }
-  if (system_wide_collection_ && !DumpThreadCommAndMmaps()) {
+  if (!DumpThreadCommAndMmaps(system_wide_collection_, monitored_threads_)) {
     return false;
   }
 
@@ -431,11 +431,24 @@ bool RecordCommand::DumpKernelAndModuleMmaps() {
   return true;
 }
 
-bool RecordCommand::DumpThreadCommAndMmaps() {
+bool RecordCommand::DumpThreadCommAndMmaps(bool all_threads,
+                                           const std::vector<pid_t>& selected_threads) {
   std::vector<ThreadComm> thread_comms;
   if (!GetThreadComms(&thread_comms)) {
     return false;
   }
+  // Decide which processes and threads to dump.
+  std::set<pid_t> dump_processes;
+  std::set<pid_t> dump_threads;
+  for (auto& tid : selected_threads) {
+    dump_threads.insert(tid);
+  }
+  for (auto& thread : thread_comms) {
+    if (dump_threads.find(thread.tid) != dump_threads.end()) {
+      dump_processes.insert(thread.pid);
+    }
+  }
+
   const perf_event_attr& attr =
       event_selection_set_.FindEventAttrByType(measured_event_type_modifier_->event_type);
 
@@ -444,6 +457,9 @@ bool RecordCommand::DumpThreadCommAndMmaps() {
     if (thread.pid != thread.tid) {
       continue;
     }
+    if (!all_threads && dump_processes.find(thread.pid) == dump_processes.end()) {
+      continue;
+    }
     CommRecord record = CreateCommRecord(attr, thread.pid, thread.tid, thread.comm);
     if (!record_file_writer_->WriteData(record.BinaryFormat())) {
       return false;
@@ -471,6 +487,9 @@ bool RecordCommand::DumpThreadCommAndMmaps() {
     if (thread.pid == thread.tid) {
       continue;
     }
+    if (!all_threads && dump_threads.find(thread.tid) == dump_threads.end()) {
+      continue;
+    }
     ForkRecord fork_record = CreateForkRecord(attr, thread.pid, thread.tid, thread.pid, thread.pid);
     if (!record_file_writer_->WriteData(fork_record.BinaryFormat())) {
       return false;