xref: /aosp_15_r20/system/extras/simpleperf/sample_tree_test.cpp (revision 288bf5226967eb3dac5cce6c939ccc2a7f2b4fe5)
1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <gtest/gtest.h>
18 
19 #include "sample_tree.h"
20 #include "thread_tree.h"
21 
22 using namespace simpleperf;
23 
24 namespace {
25 
26 struct SampleEntry {
27   int pid;
28   int tid;
29   const char* thread_comm;
30   std::string dso_name;
31   uint64_t map_start_addr;
32   size_t sample_count;
33 
SampleEntry__anonb7e208560111::SampleEntry34   SampleEntry(int pid, int tid, const char* thread_comm, const std::string& dso_name,
35               uint64_t map_start_addr, size_t sample_count = 1u)
36       : pid(pid),
37         tid(tid),
38         thread_comm(thread_comm),
39         dso_name(dso_name),
40         map_start_addr(map_start_addr),
41         sample_count(sample_count) {}
42 };
43 
44 BUILD_COMPARE_VALUE_FUNCTION(TestComparePid, pid);
45 BUILD_COMPARE_VALUE_FUNCTION(TestCompareTid, tid);
46 BUILD_COMPARE_STRING_FUNCTION(TestCompareDsoName, dso_name.c_str());
47 BUILD_COMPARE_VALUE_FUNCTION(TestCompareMapStartAddr, map_start_addr);
48 
49 class TestSampleComparator : public SampleComparator<SampleEntry> {
50  public:
TestSampleComparator()51   TestSampleComparator() {
52     AddCompareFunction(TestComparePid);
53     AddCompareFunction(TestCompareTid);
54     AddCompareFunction(CompareComm);
55     AddCompareFunction(TestCompareDsoName);
56     AddCompareFunction(TestCompareMapStartAddr);
57   }
58 };
59 
60 class TestSampleTreeBuilder : public SampleTreeBuilder<SampleEntry, int> {
61  public:
TestSampleTreeBuilder(ThreadTree * thread_tree)62   explicit TestSampleTreeBuilder(ThreadTree* thread_tree)
63       : SampleTreeBuilder(TestSampleComparator()), thread_tree_(thread_tree) {}
64 
AddSample(int pid,int tid,uint64_t ip,bool in_kernel)65   void AddSample(int pid, int tid, uint64_t ip, bool in_kernel) {
66     const ThreadEntry* thread = thread_tree_->FindThreadOrNew(pid, tid);
67     const MapEntry* map = thread_tree_->FindMap(thread, ip, in_kernel);
68     InsertSample(std::unique_ptr<SampleEntry>(
69         new SampleEntry(pid, tid, thread->comm, map->dso->Path(), map->start_addr)));
70   }
71 
72  protected:
CreateSample(const SampleRecord &,bool,int *)73   SampleEntry* CreateSample(const SampleRecord&, bool, int*) override { return nullptr; }
CreateBranchSample(const SampleRecord &,const BranchStackItemType &)74   SampleEntry* CreateBranchSample(const SampleRecord&, const BranchStackItemType&) override {
75     return nullptr;
76   };
CreateCallChainSample(const ThreadEntry *,const SampleEntry *,uint64_t,bool,const std::vector<SampleEntry * > &,const int &)77   SampleEntry* CreateCallChainSample(const ThreadEntry*, const SampleEntry*, uint64_t, bool,
78                                      const std::vector<SampleEntry*>&, const int&) override {
79     return nullptr;
80   }
GetThreadOfSample(SampleEntry *)81   const ThreadEntry* GetThreadOfSample(SampleEntry*) override { return nullptr; }
GetPeriodForCallChain(const int &)82   uint64_t GetPeriodForCallChain(const int&) override { return 0; }
MergeSample(SampleEntry * sample1,SampleEntry * sample2)83   void MergeSample(SampleEntry* sample1, SampleEntry* sample2) override {
84     sample1->sample_count += sample2->sample_count;
85   }
86 
87  private:
88   ThreadTree* thread_tree_;
89 };
90 
SampleMatchExpectation(const SampleEntry & sample,const SampleEntry & expected,bool * has_error)91 static void SampleMatchExpectation(const SampleEntry& sample, const SampleEntry& expected,
92                                    bool* has_error) {
93   *has_error = true;
94   ASSERT_EQ(expected.pid, sample.pid);
95   ASSERT_EQ(expected.tid, sample.tid);
96   ASSERT_STREQ(expected.thread_comm, sample.thread_comm);
97   ASSERT_EQ(expected.dso_name, sample.dso_name);
98   ASSERT_EQ(expected.map_start_addr, sample.map_start_addr);
99   ASSERT_EQ(expected.sample_count, sample.sample_count);
100   *has_error = false;
101 }
102 
CheckSamples(const std::vector<SampleEntry * > & samples,const std::vector<SampleEntry> & expected_samples)103 static void CheckSamples(const std::vector<SampleEntry*>& samples,
104                          const std::vector<SampleEntry>& expected_samples) {
105   ASSERT_EQ(samples.size(), expected_samples.size());
106   for (size_t i = 0; i < samples.size(); ++i) {
107     bool has_error;
108     SampleMatchExpectation(*samples[i], expected_samples[i], &has_error);
109     ASSERT_FALSE(has_error) << "Error matching sample at pos " << i;
110   }
111 }
112 }  // namespace
113 
114 class SampleTreeTest : public testing::Test {
115  protected:
SetUp()116   virtual void SetUp() {
117     thread_tree.SetThreadName(1, 1, "p1t1");
118     thread_tree.SetThreadName(1, 11, "p1t11");
119     thread_tree.SetThreadName(2, 2, "p2t2");
120     thread_tree.AddThreadMap(1, 1, 1, 5, 0, "process1_thread1");
121     thread_tree.AddThreadMap(1, 11, 6, 5, 0, "process1_thread1_map2");
122     thread_tree.AddThreadMap(2, 2, 1, 20, 0, "process2_thread2");
123     thread_tree.AddKernelMap(10, 20, 0, "kernel");
124     sample_tree_builder.reset(new TestSampleTreeBuilder(&thread_tree));
125   }
126 
CheckSamples(const std::vector<SampleEntry> & expected_samples)127   void CheckSamples(const std::vector<SampleEntry>& expected_samples) {
128     ::CheckSamples(sample_tree_builder->GetSamples(), expected_samples);
129   }
130 
131   ThreadTree thread_tree;
132   std::unique_ptr<TestSampleTreeBuilder> sample_tree_builder;
133 };
134 
135 // @CddTest = 6.1/C-0-2
TEST_F(SampleTreeTest,ip_in_map)136 TEST_F(SampleTreeTest, ip_in_map) {
137   sample_tree_builder->AddSample(1, 1, 1, false);
138   sample_tree_builder->AddSample(1, 1, 2, false);
139   sample_tree_builder->AddSample(1, 1, 5, false);
140   std::vector<SampleEntry> expected_samples = {
141       SampleEntry(1, 1, "p1t1", "process1_thread1", 1, 3),
142   };
143   CheckSamples(expected_samples);
144 }
145 
146 // @CddTest = 6.1/C-0-2
TEST_F(SampleTreeTest,different_pid)147 TEST_F(SampleTreeTest, different_pid) {
148   sample_tree_builder->AddSample(1, 1, 1, false);
149   sample_tree_builder->AddSample(2, 2, 1, false);
150   std::vector<SampleEntry> expected_samples = {
151       SampleEntry(1, 1, "p1t1", "process1_thread1", 1, 1),
152       SampleEntry(2, 2, "p2t2", "process2_thread2", 1, 1),
153   };
154   CheckSamples(expected_samples);
155 }
156 
157 // @CddTest = 6.1/C-0-2
TEST_F(SampleTreeTest,different_tid)158 TEST_F(SampleTreeTest, different_tid) {
159   sample_tree_builder->AddSample(1, 1, 1, false);
160   sample_tree_builder->AddSample(1, 11, 1, false);
161   std::vector<SampleEntry> expected_samples = {
162       SampleEntry(1, 1, "p1t1", "process1_thread1", 1, 1),
163       SampleEntry(1, 11, "p1t11", "process1_thread1", 1, 1),
164   };
165   CheckSamples(expected_samples);
166 }
167 
168 // @CddTest = 6.1/C-0-2
TEST_F(SampleTreeTest,different_comm)169 TEST_F(SampleTreeTest, different_comm) {
170   sample_tree_builder->AddSample(1, 1, 1, false);
171   thread_tree.SetThreadName(1, 1, "p1t1_comm2");
172   sample_tree_builder->AddSample(1, 1, 1, false);
173   std::vector<SampleEntry> expected_samples = {
174       SampleEntry(1, 1, "p1t1", "process1_thread1", 1, 1),
175       SampleEntry(1, 1, "p1t1_comm2", "process1_thread1", 1, 1),
176   };
177   CheckSamples(expected_samples);
178 }
179 
180 // @CddTest = 6.1/C-0-2
TEST_F(SampleTreeTest,different_map)181 TEST_F(SampleTreeTest, different_map) {
182   sample_tree_builder->AddSample(1, 1, 1, false);
183   sample_tree_builder->AddSample(1, 1, 6, false);
184   std::vector<SampleEntry> expected_samples = {
185       SampleEntry(1, 1, "p1t1", "process1_thread1", 1, 1),
186       SampleEntry(1, 1, "p1t1", "process1_thread1_map2", 6, 1),
187   };
188   CheckSamples(expected_samples);
189 }
190 
191 // @CddTest = 6.1/C-0-2
TEST_F(SampleTreeTest,unmapped_sample)192 TEST_F(SampleTreeTest, unmapped_sample) {
193   sample_tree_builder->AddSample(1, 1, 0, false);
194   sample_tree_builder->AddSample(1, 1, 31, false);
195   sample_tree_builder->AddSample(1, 1, 70, false);
196   // Match the unknown map.
197   std::vector<SampleEntry> expected_samples = {
198       SampleEntry(1, 1, "p1t1", "unknown", 0, 3),
199   };
200   CheckSamples(expected_samples);
201 }
202 
203 // @CddTest = 6.1/C-0-2
TEST_F(SampleTreeTest,map_kernel)204 TEST_F(SampleTreeTest, map_kernel) {
205   sample_tree_builder->AddSample(1, 1, 10, true);
206   sample_tree_builder->AddSample(1, 1, 10, false);
207   std::vector<SampleEntry> expected_samples = {
208       SampleEntry(1, 1, "p1t1", "kernel", 10, 1),
209       SampleEntry(1, 1, "p1t1", "process1_thread1_map2", 6, 1),
210   };
211   CheckSamples(expected_samples);
212 }
213 
214 // @CddTest = 6.1/C-0-2
TEST(sample_tree,overlapped_map)215 TEST(sample_tree, overlapped_map) {
216   ThreadTree thread_tree;
217   TestSampleTreeBuilder sample_tree_builder(&thread_tree);
218   thread_tree.SetThreadName(1, 1, "thread1");
219   thread_tree.AddThreadMap(1, 1, 1, 10, 0, "map1");  // Add map 1.
220   sample_tree_builder.AddSample(1, 1, 5, false);     // Hit map 1.
221   thread_tree.AddThreadMap(1, 1, 5, 20, 0, "map2");  // Add map 2.
222   sample_tree_builder.AddSample(1, 1, 6, false);     // Hit map 2.
223   sample_tree_builder.AddSample(1, 1, 4, false);     // Hit map 1.
224   thread_tree.AddThreadMap(1, 1, 2, 7, 0, "map3");   // Add map 3.
225   sample_tree_builder.AddSample(1, 1, 7, false);     // Hit map 3.
226   sample_tree_builder.AddSample(1, 1, 10, false);    // Hit map 2.
227 
228   std::vector<SampleEntry> expected_samples = {
229       SampleEntry(1, 1, "thread1", "map1", 1, 2),
230       SampleEntry(1, 1, "thread1", "map2", 5, 1),
231       SampleEntry(1, 1, "thread1", "map2", 9, 1),
232       SampleEntry(1, 1, "thread1", "map3", 2, 1),
233   };
234   CheckSamples(sample_tree_builder.GetSamples(), expected_samples);
235 }
236 
237 // @CddTest = 6.1/C-0-2
TEST(thread_tree,symbol_ULLONG_MAX)238 TEST(thread_tree, symbol_ULLONG_MAX) {
239   ThreadTree thread_tree;
240   thread_tree.ShowIpForUnknownSymbol();
241   ASSERT_TRUE(thread_tree.FindKernelSymbol(ULLONG_MAX) != nullptr);
242 }
243