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__anonb1d6cd600111::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 
TEST_F(SampleTreeTest,ip_in_map)135 TEST_F(SampleTreeTest, ip_in_map) {
136   sample_tree_builder->AddSample(1, 1, 1, false);
137   sample_tree_builder->AddSample(1, 1, 2, false);
138   sample_tree_builder->AddSample(1, 1, 5, false);
139   std::vector<SampleEntry> expected_samples = {
140       SampleEntry(1, 1, "p1t1", "process1_thread1", 1, 3),
141   };
142   CheckSamples(expected_samples);
143 }
144 
TEST_F(SampleTreeTest,different_pid)145 TEST_F(SampleTreeTest, different_pid) {
146   sample_tree_builder->AddSample(1, 1, 1, false);
147   sample_tree_builder->AddSample(2, 2, 1, false);
148   std::vector<SampleEntry> expected_samples = {
149       SampleEntry(1, 1, "p1t1", "process1_thread1", 1, 1),
150       SampleEntry(2, 2, "p2t2", "process2_thread2", 1, 1),
151   };
152   CheckSamples(expected_samples);
153 }
154 
TEST_F(SampleTreeTest,different_tid)155 TEST_F(SampleTreeTest, different_tid) {
156   sample_tree_builder->AddSample(1, 1, 1, false);
157   sample_tree_builder->AddSample(1, 11, 1, false);
158   std::vector<SampleEntry> expected_samples = {
159       SampleEntry(1, 1, "p1t1", "process1_thread1", 1, 1),
160       SampleEntry(1, 11, "p1t11", "process1_thread1", 1, 1),
161   };
162   CheckSamples(expected_samples);
163 }
164 
TEST_F(SampleTreeTest,different_comm)165 TEST_F(SampleTreeTest, different_comm) {
166   sample_tree_builder->AddSample(1, 1, 1, false);
167   thread_tree.SetThreadName(1, 1, "p1t1_comm2");
168   sample_tree_builder->AddSample(1, 1, 1, false);
169   std::vector<SampleEntry> expected_samples = {
170       SampleEntry(1, 1, "p1t1", "process1_thread1", 1, 1),
171       SampleEntry(1, 1, "p1t1_comm2", "process1_thread1", 1, 1),
172   };
173   CheckSamples(expected_samples);
174 }
175 
TEST_F(SampleTreeTest,different_map)176 TEST_F(SampleTreeTest, different_map) {
177   sample_tree_builder->AddSample(1, 1, 1, false);
178   sample_tree_builder->AddSample(1, 1, 6, false);
179   std::vector<SampleEntry> expected_samples = {
180       SampleEntry(1, 1, "p1t1", "process1_thread1", 1, 1),
181       SampleEntry(1, 1, "p1t1", "process1_thread1_map2", 6, 1),
182   };
183   CheckSamples(expected_samples);
184 }
185 
TEST_F(SampleTreeTest,unmapped_sample)186 TEST_F(SampleTreeTest, unmapped_sample) {
187   sample_tree_builder->AddSample(1, 1, 0, false);
188   sample_tree_builder->AddSample(1, 1, 31, false);
189   sample_tree_builder->AddSample(1, 1, 70, false);
190   // Match the unknown map.
191   std::vector<SampleEntry> expected_samples = {
192       SampleEntry(1, 1, "p1t1", "unknown", 0, 3),
193   };
194   CheckSamples(expected_samples);
195 }
196 
TEST_F(SampleTreeTest,map_kernel)197 TEST_F(SampleTreeTest, map_kernel) {
198   sample_tree_builder->AddSample(1, 1, 10, true);
199   sample_tree_builder->AddSample(1, 1, 10, false);
200   std::vector<SampleEntry> expected_samples = {
201       SampleEntry(1, 1, "p1t1", "kernel", 10, 1),
202       SampleEntry(1, 1, "p1t1", "process1_thread1_map2", 6, 1),
203   };
204   CheckSamples(expected_samples);
205 }
206 
TEST(sample_tree,overlapped_map)207 TEST(sample_tree, overlapped_map) {
208   ThreadTree thread_tree;
209   TestSampleTreeBuilder sample_tree_builder(&thread_tree);
210   thread_tree.SetThreadName(1, 1, "thread1");
211   thread_tree.AddThreadMap(1, 1, 1, 10, 0, "map1");  // Add map 1.
212   sample_tree_builder.AddSample(1, 1, 5, false);     // Hit map 1.
213   thread_tree.AddThreadMap(1, 1, 5, 20, 0, "map2");  // Add map 2.
214   sample_tree_builder.AddSample(1, 1, 6, false);     // Hit map 2.
215   sample_tree_builder.AddSample(1, 1, 4, false);     // Hit map 1.
216   thread_tree.AddThreadMap(1, 1, 2, 7, 0, "map3");   // Add map 3.
217   sample_tree_builder.AddSample(1, 1, 7, false);     // Hit map 3.
218   sample_tree_builder.AddSample(1, 1, 10, false);    // Hit map 2.
219 
220   std::vector<SampleEntry> expected_samples = {
221       SampleEntry(1, 1, "thread1", "map1", 1, 2),
222       SampleEntry(1, 1, "thread1", "map2", 5, 1),
223       SampleEntry(1, 1, "thread1", "map2", 9, 1),
224       SampleEntry(1, 1, "thread1", "map3", 2, 1),
225   };
226   CheckSamples(sample_tree_builder.GetSamples(), expected_samples);
227 }
228 
TEST(thread_tree,symbol_ULLONG_MAX)229 TEST(thread_tree, symbol_ULLONG_MAX) {
230   ThreadTree thread_tree;
231   thread_tree.ShowIpForUnknownSymbol();
232   ASSERT_TRUE(thread_tree.FindKernelSymbol(ULLONG_MAX) != nullptr);
233 }
234