xref: /aosp_15_r20/system/extras/simpleperf/SampleComparator.h (revision 288bf5226967eb3dac5cce6c939ccc2a7f2b4fe5)
1*288bf522SAndroid Build Coastguard Worker /*
2*288bf522SAndroid Build Coastguard Worker  * Copyright (C) 2016 The Android Open Source Project
3*288bf522SAndroid Build Coastguard Worker  *
4*288bf522SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*288bf522SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*288bf522SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*288bf522SAndroid Build Coastguard Worker  *
8*288bf522SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*288bf522SAndroid Build Coastguard Worker  *
10*288bf522SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*288bf522SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*288bf522SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*288bf522SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*288bf522SAndroid Build Coastguard Worker  * limitations under the License.
15*288bf522SAndroid Build Coastguard Worker  */
16*288bf522SAndroid Build Coastguard Worker 
17*288bf522SAndroid Build Coastguard Worker #ifndef SIMPLE_PERF_SAMPLE_COMPARATOR_H_
18*288bf522SAndroid Build Coastguard Worker #define SIMPLE_PERF_SAMPLE_COMPARATOR_H_
19*288bf522SAndroid Build Coastguard Worker 
20*288bf522SAndroid Build Coastguard Worker #include <string.h>
21*288bf522SAndroid Build Coastguard Worker 
22*288bf522SAndroid Build Coastguard Worker #include <vector>
23*288bf522SAndroid Build Coastguard Worker 
24*288bf522SAndroid Build Coastguard Worker namespace simpleperf {
25*288bf522SAndroid Build Coastguard Worker 
26*288bf522SAndroid Build Coastguard Worker // The compare functions below are used to compare two samples by their item
27*288bf522SAndroid Build Coastguard Worker // content.
28*288bf522SAndroid Build Coastguard Worker 
29*288bf522SAndroid Build Coastguard Worker template <typename T>
Compare(const T & a,const T & b)30*288bf522SAndroid Build Coastguard Worker int Compare(const T& a, const T& b) {
31*288bf522SAndroid Build Coastguard Worker   if (a != b) {
32*288bf522SAndroid Build Coastguard Worker     return a < b ? -1 : 1;
33*288bf522SAndroid Build Coastguard Worker   }
34*288bf522SAndroid Build Coastguard Worker   return 0;
35*288bf522SAndroid Build Coastguard Worker }
36*288bf522SAndroid Build Coastguard Worker 
37*288bf522SAndroid Build Coastguard Worker #define BUILD_COMPARE_VALUE_FUNCTION(function_name, compare_part)   \
38*288bf522SAndroid Build Coastguard Worker   template <typename EntryT>                                        \
39*288bf522SAndroid Build Coastguard Worker   int function_name(const EntryT* sample1, const EntryT* sample2) { \
40*288bf522SAndroid Build Coastguard Worker     return Compare(sample1->compare_part, sample2->compare_part);   \
41*288bf522SAndroid Build Coastguard Worker   }
42*288bf522SAndroid Build Coastguard Worker 
43*288bf522SAndroid Build Coastguard Worker #define BUILD_COMPARE_VALUE_FUNCTION_REVERSE(function_name, compare_part) \
44*288bf522SAndroid Build Coastguard Worker   template <typename EntryT>                                              \
45*288bf522SAndroid Build Coastguard Worker   int function_name(const EntryT* sample1, const EntryT* sample2) {       \
46*288bf522SAndroid Build Coastguard Worker     return Compare(sample2->compare_part, sample1->compare_part);         \
47*288bf522SAndroid Build Coastguard Worker   }
48*288bf522SAndroid Build Coastguard Worker 
49*288bf522SAndroid Build Coastguard Worker #define BUILD_COMPARE_STRING_FUNCTION(function_name, compare_part)  \
50*288bf522SAndroid Build Coastguard Worker   template <typename EntryT>                                        \
51*288bf522SAndroid Build Coastguard Worker   int function_name(const EntryT* sample1, const EntryT* sample2) { \
52*288bf522SAndroid Build Coastguard Worker     return strcmp(sample1->compare_part, sample2->compare_part);    \
53*288bf522SAndroid Build Coastguard Worker   }
54*288bf522SAndroid Build Coastguard Worker 
55*288bf522SAndroid Build Coastguard Worker BUILD_COMPARE_VALUE_FUNCTION(ComparePid, pid);
56*288bf522SAndroid Build Coastguard Worker BUILD_COMPARE_VALUE_FUNCTION(CompareTid, tid);
57*288bf522SAndroid Build Coastguard Worker BUILD_COMPARE_VALUE_FUNCTION_REVERSE(CompareSampleCount, sample_count);
58*288bf522SAndroid Build Coastguard Worker BUILD_COMPARE_STRING_FUNCTION(CompareComm, thread_comm);
59*288bf522SAndroid Build Coastguard Worker BUILD_COMPARE_STRING_FUNCTION(CompareDso, map->dso->GetReportPath().data());
60*288bf522SAndroid Build Coastguard Worker BUILD_COMPARE_STRING_FUNCTION(CompareSymbol, symbol->DemangledName());
61*288bf522SAndroid Build Coastguard Worker BUILD_COMPARE_STRING_FUNCTION(CompareDsoFrom, branch_from.map->dso->GetReportPath().data());
62*288bf522SAndroid Build Coastguard Worker BUILD_COMPARE_STRING_FUNCTION(CompareSymbolFrom, branch_from.symbol->DemangledName());
63*288bf522SAndroid Build Coastguard Worker BUILD_COMPARE_VALUE_FUNCTION(CompareCallGraphDuplicated, callchain.duplicated);
64*288bf522SAndroid Build Coastguard Worker 
65*288bf522SAndroid Build Coastguard Worker template <typename EntryT>
CompareTotalPeriod(const EntryT * sample1,const EntryT * sample2)66*288bf522SAndroid Build Coastguard Worker int CompareTotalPeriod(const EntryT* sample1, const EntryT* sample2) {
67*288bf522SAndroid Build Coastguard Worker   uint64_t period1 = sample1->period + sample1->accumulated_period;
68*288bf522SAndroid Build Coastguard Worker   uint64_t period2 = sample2->period + sample2->accumulated_period;
69*288bf522SAndroid Build Coastguard Worker   return Compare(period2, period1);
70*288bf522SAndroid Build Coastguard Worker }
71*288bf522SAndroid Build Coastguard Worker 
72*288bf522SAndroid Build Coastguard Worker template <typename EntryT>
ComparePeriod(const EntryT * sample1,const EntryT * sample2)73*288bf522SAndroid Build Coastguard Worker int ComparePeriod(const EntryT* sample1, const EntryT* sample2) {
74*288bf522SAndroid Build Coastguard Worker   return Compare(sample2->period, sample1->period);
75*288bf522SAndroid Build Coastguard Worker }
76*288bf522SAndroid Build Coastguard Worker 
77*288bf522SAndroid Build Coastguard Worker // SampleComparator is a class using a collection of compare functions to
78*288bf522SAndroid Build Coastguard Worker // compare two samples.
79*288bf522SAndroid Build Coastguard Worker 
80*288bf522SAndroid Build Coastguard Worker template <typename EntryT>
81*288bf522SAndroid Build Coastguard Worker class SampleComparator {
82*288bf522SAndroid Build Coastguard Worker  public:
83*288bf522SAndroid Build Coastguard Worker   typedef int (*compare_sample_func_t)(const EntryT*, const EntryT*);
84*288bf522SAndroid Build Coastguard Worker 
AddCompareFunction(compare_sample_func_t func)85*288bf522SAndroid Build Coastguard Worker   void AddCompareFunction(compare_sample_func_t func) { compare_v_.push_back(func); }
86*288bf522SAndroid Build Coastguard Worker 
AddComparator(const SampleComparator<EntryT> & other)87*288bf522SAndroid Build Coastguard Worker   void AddComparator(const SampleComparator<EntryT>& other) {
88*288bf522SAndroid Build Coastguard Worker     compare_v_.insert(compare_v_.end(), other.compare_v_.begin(), other.compare_v_.end());
89*288bf522SAndroid Build Coastguard Worker   }
90*288bf522SAndroid Build Coastguard Worker 
operator()91*288bf522SAndroid Build Coastguard Worker   bool operator()(const EntryT* sample1, const EntryT* sample2) const {
92*288bf522SAndroid Build Coastguard Worker     for (const auto& func : compare_v_) {
93*288bf522SAndroid Build Coastguard Worker       int ret = func(sample1, sample2);
94*288bf522SAndroid Build Coastguard Worker       if (ret != 0) {
95*288bf522SAndroid Build Coastguard Worker         return ret < 0;
96*288bf522SAndroid Build Coastguard Worker       }
97*288bf522SAndroid Build Coastguard Worker     }
98*288bf522SAndroid Build Coastguard Worker     return false;
99*288bf522SAndroid Build Coastguard Worker   }
100*288bf522SAndroid Build Coastguard Worker 
operator()101*288bf522SAndroid Build Coastguard Worker   bool operator()(const EntryT& sample1, const EntryT& sample2) const {
102*288bf522SAndroid Build Coastguard Worker     for (const auto& func : compare_v_) {
103*288bf522SAndroid Build Coastguard Worker       int ret = func(&sample1, &sample2);
104*288bf522SAndroid Build Coastguard Worker       if (ret != 0) {
105*288bf522SAndroid Build Coastguard Worker         return ret < 0;
106*288bf522SAndroid Build Coastguard Worker       }
107*288bf522SAndroid Build Coastguard Worker     }
108*288bf522SAndroid Build Coastguard Worker     return false;
109*288bf522SAndroid Build Coastguard Worker   }
110*288bf522SAndroid Build Coastguard Worker 
IsSameSample(const EntryT * sample1,const EntryT * sample2)111*288bf522SAndroid Build Coastguard Worker   bool IsSameSample(const EntryT* sample1, const EntryT* sample2) const {
112*288bf522SAndroid Build Coastguard Worker     for (const auto& func : compare_v_) {
113*288bf522SAndroid Build Coastguard Worker       if (func(sample1, sample2) != 0) {
114*288bf522SAndroid Build Coastguard Worker         return false;
115*288bf522SAndroid Build Coastguard Worker       }
116*288bf522SAndroid Build Coastguard Worker     }
117*288bf522SAndroid Build Coastguard Worker     return true;
118*288bf522SAndroid Build Coastguard Worker   }
119*288bf522SAndroid Build Coastguard Worker 
empty()120*288bf522SAndroid Build Coastguard Worker   bool empty() const { return compare_v_.empty(); }
121*288bf522SAndroid Build Coastguard Worker 
122*288bf522SAndroid Build Coastguard Worker  private:
123*288bf522SAndroid Build Coastguard Worker   std::vector<compare_sample_func_t> compare_v_;
124*288bf522SAndroid Build Coastguard Worker };
125*288bf522SAndroid Build Coastguard Worker 
126*288bf522SAndroid Build Coastguard Worker }  // namespace simpleperf
127*288bf522SAndroid Build Coastguard Worker 
128*288bf522SAndroid Build Coastguard Worker #endif  // SIMPLE_PERF_SAMPLE_COMPARATOR_H_
129