cmd_report_test.cpp revision c855ecc6becfe5d1c2445094aa525856e4538f7c
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 <set>
20#include <unordered_map>
21
22#include <android-base/file.h>
23#include <android-base/strings.h>
24#include <android-base/test_utils.h>
25
26#include "command.h"
27#include "event_selection_set.h"
28#include "get_test_data.h"
29#include "perf_regs.h"
30#include "read_apk.h"
31#include "test_util.h"
32
33static std::unique_ptr<Command> ReportCmd() {
34  return CreateCommandInstance("report");
35}
36
37class ReportCommandTest : public ::testing::Test {
38 protected:
39  void Report(
40      const std::string perf_data,
41      const std::vector<std::string>& add_args = std::vector<std::string>()) {
42    ReportRaw(GetTestData(perf_data), add_args);
43  }
44
45  void ReportRaw(
46      const std::string perf_data,
47      const std::vector<std::string>& add_args = std::vector<std::string>()) {
48    success = false;
49    std::vector<std::string> args = {
50        "-i", perf_data, "--symfs", GetTestDataDir(), "-o", tmp_file.path};
51    args.insert(args.end(), add_args.begin(), add_args.end());
52    ASSERT_TRUE(ReportCmd()->Run(args));
53    ASSERT_TRUE(android::base::ReadFileToString(tmp_file.path, &content));
54    ASSERT_TRUE(!content.empty());
55    std::vector<std::string> raw_lines = android::base::Split(content, "\n");
56    lines.clear();
57    for (const auto& line : raw_lines) {
58      std::string s = android::base::Trim(line);
59      if (!s.empty()) {
60        lines.push_back(s);
61      }
62    }
63    ASSERT_GE(lines.size(), 2u);
64    success = true;
65  }
66
67  TemporaryFile tmp_file;
68  std::string content;
69  std::vector<std::string> lines;
70  bool success;
71};
72
73TEST_F(ReportCommandTest, no_option) {
74  Report(PERF_DATA);
75  ASSERT_TRUE(success);
76  ASSERT_NE(content.find("GlobalFunc"), std::string::npos);
77}
78
79TEST_F(ReportCommandTest, report_symbol_from_elf_file_with_mini_debug_info) {
80  Report(PERF_DATA_WITH_MINI_DEBUG_INFO);
81  ASSERT_TRUE(success);
82  ASSERT_NE(content.find("GlobalFunc"), std::string::npos);
83}
84
85TEST_F(ReportCommandTest, sort_option_pid) {
86  Report(PERF_DATA, {"--sort", "pid"});
87  ASSERT_TRUE(success);
88  size_t line_index = 0;
89  while (line_index < lines.size() &&
90         lines[line_index].find("Pid") == std::string::npos) {
91    line_index++;
92  }
93  ASSERT_LT(line_index + 2, lines.size());
94}
95
96TEST_F(ReportCommandTest, sort_option_more_than_one) {
97  Report(PERF_DATA, {"--sort", "comm,pid,dso,symbol"});
98  ASSERT_TRUE(success);
99  size_t line_index = 0;
100  while (line_index < lines.size() &&
101         lines[line_index].find("Overhead") == std::string::npos) {
102    line_index++;
103  }
104  ASSERT_LT(line_index + 1, lines.size());
105  ASSERT_NE(lines[line_index].find("Command"), std::string::npos);
106  ASSERT_NE(lines[line_index].find("Pid"), std::string::npos);
107  ASSERT_NE(lines[line_index].find("Shared Object"), std::string::npos);
108  ASSERT_NE(lines[line_index].find("Symbol"), std::string::npos);
109  ASSERT_EQ(lines[line_index].find("Tid"), std::string::npos);
110}
111
112TEST_F(ReportCommandTest, children_option) {
113  Report(CALLGRAPH_FP_PERF_DATA, {"--children", "--sort", "symbol"});
114  ASSERT_TRUE(success);
115  std::unordered_map<std::string, std::pair<double, double>> map;
116  for (size_t i = 0; i < lines.size(); ++i) {
117    char name[1024];
118    std::pair<double, double> pair;
119    if (sscanf(lines[i].c_str(), "%lf%%%lf%%%s", &pair.first, &pair.second,
120               name) == 3) {
121      map.insert(std::make_pair(name, pair));
122    }
123  }
124  ASSERT_NE(map.find("GlobalFunc"), map.end());
125  ASSERT_NE(map.find("main"), map.end());
126  auto func_pair = map["GlobalFunc"];
127  auto main_pair = map["main"];
128  ASSERT_GE(main_pair.first, func_pair.first);
129  ASSERT_GE(func_pair.first, func_pair.second);
130  ASSERT_GE(func_pair.second, main_pair.second);
131}
132
133static bool CheckCalleeMode(std::vector<std::string>& lines) {
134  bool found = false;
135  for (size_t i = 0; i + 2 < lines.size(); ++i) {
136    if (lines[i].find("GlobalFunc") != std::string::npos &&
137        lines[i + 1].find("|") != std::string::npos &&
138        lines[i + 2].find("main") != std::string::npos) {
139      found = true;
140      break;
141    }
142  }
143  return found;
144}
145
146static bool CheckCallerMode(std::vector<std::string>& lines) {
147  bool found = false;
148  for (size_t i = 0; i + 2 < lines.size(); ++i) {
149    if (lines[i].find("main") != std::string::npos &&
150        lines[i + 1].find("|") != std::string::npos &&
151        lines[i + 2].find("GlobalFunc") != std::string::npos) {
152      found = true;
153      break;
154    }
155  }
156  return found;
157}
158
159TEST_F(ReportCommandTest, callgraph_option) {
160  Report(CALLGRAPH_FP_PERF_DATA, {"-g"});
161  ASSERT_TRUE(success);
162  ASSERT_TRUE(CheckCalleeMode(lines));
163  Report(CALLGRAPH_FP_PERF_DATA, {"-g", "callee"});
164  ASSERT_TRUE(success);
165  ASSERT_TRUE(CheckCalleeMode(lines));
166  Report(CALLGRAPH_FP_PERF_DATA, {"-g", "caller"});
167  ASSERT_TRUE(success);
168  ASSERT_TRUE(CheckCallerMode(lines));
169}
170
171static bool AllItemsWithString(std::vector<std::string>& lines,
172                               const std::vector<std::string>& strs) {
173  size_t line_index = 0;
174  while (line_index < lines.size() &&
175         lines[line_index].find("Overhead") == std::string::npos) {
176    line_index++;
177  }
178  if (line_index == lines.size() || line_index + 1 == lines.size()) {
179    return false;
180  }
181  line_index++;
182  for (; line_index < lines.size(); ++line_index) {
183    bool exist = false;
184    for (auto& s : strs) {
185      if (lines[line_index].find(s) != std::string::npos) {
186        exist = true;
187        break;
188      }
189    }
190    if (!exist) {
191      return false;
192    }
193  }
194  return true;
195}
196
197TEST_F(ReportCommandTest, pid_filter_option) {
198  Report(PERF_DATA);
199  ASSERT_TRUE("success");
200  ASSERT_FALSE(AllItemsWithString(lines, {"26083"}));
201  ASSERT_FALSE(AllItemsWithString(lines, {"26083", "26090"}));
202  Report(PERF_DATA, {"--pids", "26083"});
203  ASSERT_TRUE(success);
204  ASSERT_TRUE(AllItemsWithString(lines, {"26083"}));
205  Report(PERF_DATA, {"--pids", "26083,26090"});
206  ASSERT_TRUE(success);
207  ASSERT_TRUE(AllItemsWithString(lines, {"26083", "26090"}));
208}
209
210TEST_F(ReportCommandTest, tid_filter_option) {
211  Report(PERF_DATA);
212  ASSERT_TRUE("success");
213  ASSERT_FALSE(AllItemsWithString(lines, {"26083"}));
214  ASSERT_FALSE(AllItemsWithString(lines, {"26083", "26090"}));
215  Report(PERF_DATA, {"--tids", "26083"});
216  ASSERT_TRUE(success);
217  ASSERT_TRUE(AllItemsWithString(lines, {"26083"}));
218  Report(PERF_DATA, {"--tids", "26083,26090"});
219  ASSERT_TRUE(success);
220  ASSERT_TRUE(AllItemsWithString(lines, {"26083", "26090"}));
221}
222
223TEST_F(ReportCommandTest, comm_filter_option) {
224  Report(PERF_DATA, {"--sort", "comm"});
225  ASSERT_TRUE(success);
226  ASSERT_FALSE(AllItemsWithString(lines, {"t1"}));
227  ASSERT_FALSE(AllItemsWithString(lines, {"t1", "t2"}));
228  Report(PERF_DATA, {"--sort", "comm", "--comms", "t1"});
229  ASSERT_TRUE(success);
230  ASSERT_TRUE(AllItemsWithString(lines, {"t1"}));
231  Report(PERF_DATA, {"--sort", "comm", "--comms", "t1,t2"});
232  ASSERT_TRUE(success);
233  ASSERT_TRUE(AllItemsWithString(lines, {"t1", "t2"}));
234}
235
236TEST_F(ReportCommandTest, dso_filter_option) {
237  Report(PERF_DATA, {"--sort", "dso"});
238  ASSERT_TRUE(success);
239  ASSERT_FALSE(AllItemsWithString(lines, {"/t1"}));
240  ASSERT_FALSE(AllItemsWithString(lines, {"/t1", "/t2"}));
241  Report(PERF_DATA, {"--sort", "dso", "--dsos", "/t1"});
242  ASSERT_TRUE(success);
243  ASSERT_TRUE(AllItemsWithString(lines, {"/t1"}));
244  Report(PERF_DATA, {"--sort", "dso", "--dsos", "/t1,/t2"});
245  ASSERT_TRUE(success);
246  ASSERT_TRUE(AllItemsWithString(lines, {"/t1", "/t2"}));
247}
248
249TEST_F(ReportCommandTest, symbol_filter_option) {
250  Report(PERF_DATA_WITH_SYMBOLS, {"--sort", "symbol"});
251  ASSERT_TRUE(success);
252  ASSERT_FALSE(AllItemsWithString(lines, {"func2(int, int)"}));
253  ASSERT_FALSE(AllItemsWithString(lines, {"main", "func2(int, int)"}));
254  Report(PERF_DATA_WITH_SYMBOLS,
255         {"--sort", "symbol", "--symbols", "func2(int, int)"});
256  ASSERT_TRUE(success);
257  ASSERT_TRUE(AllItemsWithString(lines, {"func2(int, int)"}));
258  Report(PERF_DATA_WITH_SYMBOLS,
259         {"--sort", "symbol", "--symbols", "main;func2(int, int)"});
260  ASSERT_TRUE(success);
261  ASSERT_TRUE(AllItemsWithString(lines, {"main", "func2(int, int)"}));
262}
263
264TEST_F(ReportCommandTest, use_branch_address) {
265  Report(BRANCH_PERF_DATA, {"-b", "--sort", "symbol_from,symbol_to"});
266  std::set<std::pair<std::string, std::string>> hit_set;
267  bool after_overhead = false;
268  for (const auto& line : lines) {
269    if (!after_overhead && line.find("Overhead") != std::string::npos) {
270      after_overhead = true;
271    } else if (after_overhead) {
272      char from[80];
273      char to[80];
274      if (sscanf(line.c_str(), "%*f%%%s%s", from, to) == 2) {
275        hit_set.insert(std::make_pair<std::string, std::string>(from, to));
276      }
277    }
278  }
279  ASSERT_NE(hit_set.find(std::make_pair<std::string, std::string>(
280                "GlobalFunc", "CalledFunc")),
281            hit_set.end());
282  ASSERT_NE(hit_set.find(std::make_pair<std::string, std::string>(
283                "CalledFunc", "GlobalFunc")),
284            hit_set.end());
285}
286
287TEST_F(ReportCommandTest, report_symbols_of_nativelib_in_apk) {
288  Report(NATIVELIB_IN_APK_PERF_DATA);
289  ASSERT_TRUE(success);
290  ASSERT_NE(content.find(GetUrlInApk(APK_FILE, NATIVELIB_IN_APK)),
291            std::string::npos);
292  ASSERT_NE(content.find("Func2"), std::string::npos);
293}
294
295TEST_F(ReportCommandTest, report_more_than_one_event_types) {
296  Report(PERF_DATA_WITH_TWO_EVENT_TYPES);
297  ASSERT_TRUE(success);
298  ASSERT_NE(content.find("cpu-cycles"), std::string::npos);
299  ASSERT_NE(content.find("cpu-clock"), std::string::npos);
300}
301
302TEST_F(ReportCommandTest, report_kernel_symbol) {
303  Report(PERF_DATA_WITH_KERNEL_SYMBOL);
304  ASSERT_TRUE(success);
305  ASSERT_NE(content.find("perf_event_aux"), std::string::npos);
306}
307
308TEST_F(ReportCommandTest, report_dumped_symbols) {
309  Report(PERF_DATA_WITH_SYMBOLS);
310  ASSERT_TRUE(success);
311  ASSERT_NE(content.find("main"), std::string::npos);
312  Report(PERF_DATA_WITH_SYMBOLS_FOR_NONZERO_MINVADDR_DSO);
313  ASSERT_TRUE(success);
314  ASSERT_NE(content.find("main"), std::string::npos);
315}
316
317TEST_F(ReportCommandTest, report_sort_vaddr_in_file) {
318  Report(PERF_DATA, {"--sort", "vaddr_in_file"});
319  ASSERT_TRUE(success);
320  ASSERT_NE(content.find("VaddrInFile"), std::string::npos);
321}
322
323TEST_F(ReportCommandTest, check_build_id) {
324  Report(PERF_DATA_FOR_BUILD_ID_CHECK,
325         {"--symfs", GetTestData(CORRECT_SYMFS_FOR_BUILD_ID_CHECK)});
326  ASSERT_TRUE(success);
327  ASSERT_NE(content.find("main"), std::string::npos);
328  ASSERT_EXIT(
329      {
330        Report(PERF_DATA_FOR_BUILD_ID_CHECK,
331               {"--symfs", GetTestData(WRONG_SYMFS_FOR_BUILD_ID_CHECK)});
332        if (!success) {
333          exit(1);
334        }
335        if (content.find("main") != std::string::npos) {
336          exit(2);
337        }
338        exit(0);
339      },
340      testing::ExitedWithCode(0), "build id.*mismatch");
341}
342
343#if defined(__linux__)
344
345static std::unique_ptr<Command> RecordCmd() {
346  return CreateCommandInstance("record");
347}
348
349TEST_F(ReportCommandTest, dwarf_callgraph) {
350  if (IsDwarfCallChainSamplingSupported()) {
351    TemporaryFile tmp_file;
352    ASSERT_TRUE(
353        RecordCmd()->Run({"-g", "-o", tmp_file.path, "sleep", SLEEP_SEC}));
354    ReportRaw(tmp_file.path, {"-g"});
355    ASSERT_TRUE(success);
356  } else {
357    GTEST_LOG_(INFO) << "This test does nothing as dwarf callchain sampling is "
358                        "not supported on this device.";
359  }
360}
361
362TEST_F(ReportCommandTest, report_dwarf_callgraph_of_nativelib_in_apk) {
363  // NATIVELIB_IN_APK_PERF_DATA is recorded on arm64, so can only report
364  // callgraph on arm64.
365  if (GetBuildArch() == ARCH_ARM64) {
366    Report(NATIVELIB_IN_APK_PERF_DATA, {"-g"});
367    ASSERT_NE(content.find(GetUrlInApk(APK_FILE, NATIVELIB_IN_APK)),
368              std::string::npos);
369    ASSERT_NE(content.find("Func2"), std::string::npos);
370    ASSERT_NE(content.find("Func1"), std::string::npos);
371    ASSERT_NE(content.find("GlobalFunc"), std::string::npos);
372  } else {
373    GTEST_LOG_(INFO)
374        << "This test does nothing as it is only run on arm64 devices";
375  }
376}
377
378#endif
379