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
22namespace {
23
24struct SampleEntry {
25  int pid;
26  int tid;
27  const char* thread_comm;
28  std::string dso_name;
29  uint64_t map_start_addr;
30  size_t sample_count;
31
32  SampleEntry(int pid, int tid, const char* thread_comm,
33              const std::string& dso_name, uint64_t map_start_addr,
34              size_t sample_count = 1u)
35      : pid(pid),
36        tid(tid),
37        thread_comm(thread_comm),
38        dso_name(dso_name),
39        map_start_addr(map_start_addr),
40        sample_count(sample_count) {}
41};
42
43BUILD_COMPARE_VALUE_FUNCTION(TestComparePid, pid);
44BUILD_COMPARE_VALUE_FUNCTION(TestCompareTid, tid);
45BUILD_COMPARE_STRING_FUNCTION(TestCompareDsoName, dso_name.c_str());
46BUILD_COMPARE_VALUE_FUNCTION(TestCompareMapStartAddr, map_start_addr);
47
48class TestSampleComparator : public SampleComparator<SampleEntry> {
49 public:
50  TestSampleComparator() {
51    AddCompareFunction(TestComparePid);
52    AddCompareFunction(TestCompareTid);
53    AddCompareFunction(CompareComm);
54    AddCompareFunction(TestCompareDsoName);
55    AddCompareFunction(TestCompareMapStartAddr);
56  }
57};
58
59class TestSampleTreeBuilder : public SampleTreeBuilder<SampleEntry, int> {
60 public:
61  explicit TestSampleTreeBuilder(ThreadTree* thread_tree)
62      : SampleTreeBuilder(TestSampleComparator()), thread_tree_(thread_tree) {}
63
64  void AddSample(int pid, int tid, uint64_t ip, bool in_kernel) {
65    const ThreadEntry* thread = thread_tree_->FindThreadOrNew(pid, tid);
66    const MapEntry* map = thread_tree_->FindMap(thread, ip, in_kernel);
67    InsertSample(std::unique_ptr<SampleEntry>(new SampleEntry(
68        pid, tid, thread->comm, map->dso->Path(), map->start_addr)));
69  }
70
71 protected:
72  SampleEntry* CreateSample(const SampleRecord&, bool, int*) override {
73    return nullptr;
74  }
75  SampleEntry* CreateBranchSample(const SampleRecord&,
76                                  const BranchStackItemType&) override {
77    return nullptr;
78  };
79  SampleEntry* CreateCallChainSample(const SampleEntry*, uint64_t, bool,
80                                     const std::vector<SampleEntry*>&,
81                                     const int&) override {
82    return nullptr;
83  }
84  const ThreadEntry* GetThreadOfSample(SampleEntry*) override {
85    return nullptr;
86  }
87  uint64_t GetPeriodForCallChain(const int&) override { return 0; }
88  void MergeSample(SampleEntry* sample1, SampleEntry* sample2) override {
89    sample1->sample_count += sample2->sample_count;
90  }
91
92 private:
93  ThreadTree* thread_tree_;
94};
95
96static void SampleMatchExpectation(const SampleEntry& sample,
97                                   const SampleEntry& expected,
98                                   bool* has_error) {
99  *has_error = true;
100  ASSERT_EQ(expected.pid, sample.pid);
101  ASSERT_EQ(expected.tid, sample.tid);
102  ASSERT_STREQ(expected.thread_comm, sample.thread_comm);
103  ASSERT_EQ(expected.dso_name, sample.dso_name);
104  ASSERT_EQ(expected.map_start_addr, sample.map_start_addr);
105  ASSERT_EQ(expected.sample_count, sample.sample_count);
106  *has_error = false;
107}
108
109static void CheckSamples(const std::vector<SampleEntry*>& samples,
110                         const std::vector<SampleEntry>& expected_samples) {
111  ASSERT_EQ(samples.size(), expected_samples.size());
112  for (size_t i = 0; i < samples.size(); ++i) {
113    bool has_error;
114    SampleMatchExpectation(*samples[i], expected_samples[i], &has_error);
115    ASSERT_FALSE(has_error) << "Error matching sample at pos " << i;
116  }
117}
118}
119
120class SampleTreeTest : public testing::Test {
121 protected:
122  virtual void SetUp() {
123    thread_tree.SetThreadName(1, 1, "p1t1");
124    thread_tree.SetThreadName(1, 11, "p1t11");
125    thread_tree.SetThreadName(2, 2, "p2t2");
126    thread_tree.AddThreadMap(1, 1, 1, 5, 0, 0, "process1_thread1");
127    thread_tree.AddThreadMap(1, 11, 6, 5, 0, 0, "process1_thread1_map2");
128    thread_tree.AddThreadMap(2, 2, 1, 20, 0, 0, "process2_thread2");
129    thread_tree.AddKernelMap(10, 20, 0, 0, "kernel");
130    sample_tree_builder.reset(new TestSampleTreeBuilder(&thread_tree));
131  }
132
133  void CheckSamples(const std::vector<SampleEntry>& expected_samples) {
134    ::CheckSamples(sample_tree_builder->GetSamples(), expected_samples);
135  }
136
137  ThreadTree thread_tree;
138  std::unique_ptr<TestSampleTreeBuilder> sample_tree_builder;
139};
140
141TEST_F(SampleTreeTest, ip_in_map) {
142  sample_tree_builder->AddSample(1, 1, 1, false);
143  sample_tree_builder->AddSample(1, 1, 2, false);
144  sample_tree_builder->AddSample(1, 1, 5, false);
145  std::vector<SampleEntry> expected_samples = {
146      SampleEntry(1, 1, "p1t1", "process1_thread1", 1, 3),
147  };
148  CheckSamples(expected_samples);
149}
150
151TEST_F(SampleTreeTest, different_pid) {
152  sample_tree_builder->AddSample(1, 1, 1, false);
153  sample_tree_builder->AddSample(2, 2, 1, false);
154  std::vector<SampleEntry> expected_samples = {
155      SampleEntry(1, 1, "p1t1", "process1_thread1", 1, 1),
156      SampleEntry(2, 2, "p2t2", "process2_thread2", 1, 1),
157  };
158  CheckSamples(expected_samples);
159}
160
161TEST_F(SampleTreeTest, different_tid) {
162  sample_tree_builder->AddSample(1, 1, 1, false);
163  sample_tree_builder->AddSample(1, 11, 1, false);
164  std::vector<SampleEntry> expected_samples = {
165      SampleEntry(1, 1, "p1t1", "process1_thread1", 1, 1),
166      SampleEntry(1, 11, "p1t11", "process1_thread1", 1, 1),
167  };
168  CheckSamples(expected_samples);
169}
170
171TEST_F(SampleTreeTest, different_comm) {
172  sample_tree_builder->AddSample(1, 1, 1, false);
173  thread_tree.SetThreadName(1, 1, "p1t1_comm2");
174  sample_tree_builder->AddSample(1, 1, 1, false);
175  std::vector<SampleEntry> expected_samples = {
176      SampleEntry(1, 1, "p1t1", "process1_thread1", 1, 1),
177      SampleEntry(1, 1, "p1t1_comm2", "process1_thread1", 1, 1),
178  };
179  CheckSamples(expected_samples);
180}
181
182TEST_F(SampleTreeTest, different_map) {
183  sample_tree_builder->AddSample(1, 1, 1, false);
184  sample_tree_builder->AddSample(1, 1, 6, false);
185  std::vector<SampleEntry> expected_samples = {
186      SampleEntry(1, 1, "p1t1", "process1_thread1", 1, 1),
187      SampleEntry(1, 1, "p1t1", "process1_thread1_map2", 6, 1),
188  };
189  CheckSamples(expected_samples);
190}
191
192TEST_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
203TEST_F(SampleTreeTest, map_kernel) {
204  sample_tree_builder->AddSample(1, 1, 10, true);
205  sample_tree_builder->AddSample(1, 1, 10, false);
206  std::vector<SampleEntry> expected_samples = {
207      SampleEntry(1, 1, "p1t1", "kernel", 10, 1),
208      SampleEntry(1, 1, "p1t1", "process1_thread1_map2", 6, 1),
209  };
210  CheckSamples(expected_samples);
211}
212
213TEST(sample_tree, overlapped_map) {
214  ThreadTree thread_tree;
215  TestSampleTreeBuilder sample_tree_builder(&thread_tree);
216  thread_tree.SetThreadName(1, 1, "thread1");
217  thread_tree.AddThreadMap(1, 1, 1, 10, 0, 0, "map1");  // Add map 1.
218  sample_tree_builder.AddSample(1, 1, 5, false);        // Hit map 1.
219  thread_tree.AddThreadMap(1, 1, 5, 20, 0, 0, "map2");  // Add map 2.
220  sample_tree_builder.AddSample(1, 1, 6, false);        // Hit map 2.
221  sample_tree_builder.AddSample(1, 1, 4, false);        // Hit map 1.
222  thread_tree.AddThreadMap(1, 1, 2, 7, 0, 0, "map3");   // Add map 3.
223  sample_tree_builder.AddSample(1, 1, 7, false);        // Hit map 3.
224  sample_tree_builder.AddSample(1, 1, 10, false);       // Hit map 2.
225
226  std::vector<SampleEntry> expected_samples = {
227      SampleEntry(1, 1, "thread1", "map1", 1, 2),
228      SampleEntry(1, 1, "thread1", "map2", 5, 1),
229      SampleEntry(1, 1, "thread1", "map2", 9, 1),
230      SampleEntry(1, 1, "thread1", "map3", 2, 1),
231  };
232  CheckSamples(sample_tree_builder.GetSamples(), expected_samples);
233}
234
235TEST(thread_tree, symbol_ULLONG_MAX) {
236  ThreadTree thread_tree;
237  thread_tree.ShowIpForUnknownSymbol();
238  ASSERT_TRUE(thread_tree.FindKernelSymbol(ULLONG_MAX) != nullptr);
239}
240