OSDN Git Service

Merge "simpleperf: restore perf_harden after simpleperf cts test." into nyc-dev
[android-x86/system-extras.git] / simpleperf / sample_tree.cpp
index f8f27b1..a34107b 100644 (file)
 
 #include "sample_tree.h"
 
-#include <base/logging.h>
+#include <android-base/logging.h>
 
 #include "environment.h"
 
+void SampleTree::SetFilters(const std::unordered_set<int>& pid_filter,
+                            const std::unordered_set<int>& tid_filter,
+                            const std::unordered_set<std::string>& comm_filter,
+                            const std::unordered_set<std::string>& dso_filter) {
+  pid_filter_ = pid_filter;
+  tid_filter_ = tid_filter;
+  comm_filter_ = comm_filter;
+  dso_filter_ = dso_filter;
+}
+
 SampleEntry* SampleTree::AddSample(int pid, int tid, uint64_t ip, uint64_t time, uint64_t period,
                                    bool in_kernel) {
   const ThreadEntry* thread = thread_tree_->FindThreadOrNew(pid, tid);
   const MapEntry* map = thread_tree_->FindMap(thread, ip, in_kernel);
-  const SymbolEntry* symbol = thread_tree_->FindSymbol(map, ip);
+  const Symbol* symbol = thread_tree_->FindSymbol(map, ip);
 
   SampleEntry value(ip, time, period, 0, 1, thread, map, symbol);
 
+  if (IsFilteredOut(value)) {
+    return nullptr;
+  }
   return InsertSample(value);
 }
 
@@ -38,12 +51,12 @@ void SampleTree::AddBranchSample(int pid, int tid, uint64_t from_ip, uint64_t to
   if (from_map == thread_tree_->UnknownMap()) {
     from_map = thread_tree_->FindMap(thread, from_ip, true);
   }
-  const SymbolEntry* from_symbol = thread_tree_->FindSymbol(from_map, from_ip);
+  const Symbol* from_symbol = thread_tree_->FindSymbol(from_map, from_ip);
   const MapEntry* to_map = thread_tree_->FindMap(thread, to_ip, false);
   if (to_map == thread_tree_->UnknownMap()) {
     to_map = thread_tree_->FindMap(thread, to_ip, true);
   }
-  const SymbolEntry* to_symbol = thread_tree_->FindSymbol(to_map, to_ip);
+  const Symbol* to_symbol = thread_tree_->FindSymbol(to_map, to_ip);
 
   SampleEntry value(to_ip, time, period, 0, 1, thread, to_map, to_symbol);
   value.branch_from.ip = from_ip;
@@ -51,6 +64,9 @@ void SampleTree::AddBranchSample(int pid, int tid, uint64_t from_ip, uint64_t to
   value.branch_from.symbol = from_symbol;
   value.branch_from.flags = branch_flags;
 
+  if (IsFilteredOut(value)) {
+    return;
+  }
   InsertSample(value);
 }
 
@@ -59,10 +75,21 @@ SampleEntry* SampleTree::AddCallChainSample(int pid, int tid, uint64_t ip, uint6
                                             const std::vector<SampleEntry*>& callchain) {
   const ThreadEntry* thread = thread_tree_->FindThreadOrNew(pid, tid);
   const MapEntry* map = thread_tree_->FindMap(thread, ip, in_kernel);
-  const SymbolEntry* symbol = thread_tree_->FindSymbol(map, ip);
+  const Symbol* symbol = thread_tree_->FindSymbol(map, ip);
 
   SampleEntry value(ip, time, 0, period, 0, thread, map, symbol);
 
+  if (IsFilteredOut(value)) {
+    // Store in callchain_sample_tree_ for use in other SampleEntry's callchain.
+    auto it = callchain_sample_tree_.find(&value);
+    if (it != callchain_sample_tree_.end()) {
+      return *it;
+    }
+    SampleEntry* sample = AllocateSample(value);
+    callchain_sample_tree_.insert(sample);
+    return sample;
+  }
+
   auto it = sample_tree_.find(&value);
   if (it != sample_tree_.end()) {
     SampleEntry* sample = *it;
@@ -74,6 +101,22 @@ SampleEntry* SampleTree::AddCallChainSample(int pid, int tid, uint64_t ip, uint6
   return InsertSample(value);
 }
 
+bool SampleTree::IsFilteredOut(const SampleEntry& value) {
+  if (!pid_filter_.empty() && pid_filter_.find(value.thread->pid) == pid_filter_.end()) {
+    return true;
+  }
+  if (!tid_filter_.empty() && tid_filter_.find(value.thread->tid) == tid_filter_.end()) {
+    return true;
+  }
+  if (!comm_filter_.empty() && comm_filter_.find(value.thread_comm) == comm_filter_.end()) {
+    return true;
+  }
+  if (!dso_filter_.empty() && dso_filter_.find(value.map->dso->Path()) == dso_filter_.end()) {
+    return true;
+  }
+  return false;
+}
+
 SampleEntry* SampleTree::InsertSample(SampleEntry& value) {
   SampleEntry* result;
   auto it = sample_tree_.find(&value);