cmd_report_test.cpp revision afe99a53d3030f54fa843af3e1558852a4cb3815
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 "get_test_data.h"
28#include "perf_regs.h"
29#include "read_apk.h"
30#include "test_util.h"
31
32static std::unique_ptr<Command> ReportCmd() {
33  return CreateCommandInstance("report");
34}
35
36class ReportCommandTest : public ::testing::Test {
37 protected:
38  void Report(
39      const std::string& perf_data,
40      const std::vector<std::string>& add_args = std::vector<std::string>()) {
41    ReportRaw(GetTestData(perf_data), add_args);
42  }
43
44  void ReportRaw(
45      const std::string& perf_data,
46      const std::vector<std::string>& add_args = std::vector<std::string>()) {
47    success = false;
48    TemporaryFile tmp_file;
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  std::string content;
68  std::vector<std::string> lines;
69  bool success;
70};
71
72TEST_F(ReportCommandTest, no_option) {
73  Report(PERF_DATA);
74  ASSERT_TRUE(success);
75  ASSERT_NE(content.find("GlobalFunc"), std::string::npos);
76}
77
78TEST_F(ReportCommandTest, report_symbol_from_elf_file_with_mini_debug_info) {
79  Report(PERF_DATA_WITH_MINI_DEBUG_INFO);
80  ASSERT_TRUE(success);
81  ASSERT_NE(content.find("GlobalFunc"), std::string::npos);
82}
83
84TEST_F(ReportCommandTest, sort_option_pid) {
85  Report(PERF_DATA, {"--sort", "pid"});
86  ASSERT_TRUE(success);
87  size_t line_index = 0;
88  while (line_index < lines.size() &&
89         lines[line_index].find("Pid") == std::string::npos) {
90    line_index++;
91  }
92  ASSERT_LT(line_index + 2, lines.size());
93}
94
95TEST_F(ReportCommandTest, sort_option_more_than_one) {
96  Report(PERF_DATA, {"--sort", "comm,pid,dso,symbol"});
97  ASSERT_TRUE(success);
98  size_t line_index = 0;
99  while (line_index < lines.size() &&
100         lines[line_index].find("Overhead") == std::string::npos) {
101    line_index++;
102  }
103  ASSERT_LT(line_index + 1, lines.size());
104  ASSERT_NE(lines[line_index].find("Command"), std::string::npos);
105  ASSERT_NE(lines[line_index].find("Pid"), std::string::npos);
106  ASSERT_NE(lines[line_index].find("Shared Object"), std::string::npos);
107  ASSERT_NE(lines[line_index].find("Symbol"), std::string::npos);
108  ASSERT_EQ(lines[line_index].find("Tid"), std::string::npos);
109}
110
111TEST_F(ReportCommandTest, children_option) {
112  Report(CALLGRAPH_FP_PERF_DATA, {"--children", "--sort", "symbol"});
113  ASSERT_TRUE(success);
114  std::unordered_map<std::string, std::pair<double, double>> map;
115  for (size_t i = 0; i < lines.size(); ++i) {
116    char name[1024];
117    std::pair<double, double> pair;
118    if (sscanf(lines[i].c_str(), "%lf%%%lf%%%s", &pair.first, &pair.second,
119               name) == 3) {
120      map.insert(std::make_pair(name, pair));
121    }
122  }
123  ASSERT_NE(map.find("GlobalFunc"), map.end());
124  ASSERT_NE(map.find("main"), map.end());
125  auto func_pair = map["GlobalFunc"];
126  auto main_pair = map["main"];
127  ASSERT_GE(main_pair.first, func_pair.first);
128  ASSERT_GE(func_pair.first, func_pair.second);
129  ASSERT_GE(func_pair.second, main_pair.second);
130}
131
132static bool CheckCalleeMode(std::vector<std::string>& lines) {
133  bool found = false;
134  for (size_t i = 0; i + 2 < lines.size(); ++i) {
135    if (lines[i].find("GlobalFunc") != std::string::npos &&
136        lines[i + 1].find('|') != std::string::npos &&
137        lines[i + 2].find("main") != std::string::npos) {
138      found = true;
139      break;
140    }
141  }
142  return found;
143}
144
145static bool CheckCallerMode(std::vector<std::string>& lines) {
146  bool found = false;
147  for (size_t i = 0; i + 2 < lines.size(); ++i) {
148    if (lines[i].find("main") != std::string::npos &&
149        lines[i + 1].find('|') != std::string::npos &&
150        lines[i + 2].find("GlobalFunc") != std::string::npos) {
151      found = true;
152      break;
153    }
154  }
155  return found;
156}
157
158TEST_F(ReportCommandTest, callgraph_option) {
159  Report(CALLGRAPH_FP_PERF_DATA, {"-g"});
160  ASSERT_TRUE(success);
161  ASSERT_TRUE(CheckCallerMode(lines));
162  Report(CALLGRAPH_FP_PERF_DATA, {"-g", "callee"});
163  ASSERT_TRUE(success);
164  ASSERT_TRUE(CheckCalleeMode(lines));
165  Report(CALLGRAPH_FP_PERF_DATA, {"-g", "caller"});
166  ASSERT_TRUE(success);
167  ASSERT_TRUE(CheckCallerMode(lines));
168}
169
170static bool AllItemsWithString(std::vector<std::string>& lines,
171                               const std::vector<std::string>& strs) {
172  size_t line_index = 0;
173  while (line_index < lines.size() &&
174         lines[line_index].find("Overhead") == std::string::npos) {
175    line_index++;
176  }
177  if (line_index == lines.size() || line_index + 1 == lines.size()) {
178    return false;
179  }
180  line_index++;
181  for (; line_index < lines.size(); ++line_index) {
182    bool exist = false;
183    for (auto& s : strs) {
184      if (lines[line_index].find(s) != std::string::npos) {
185        exist = true;
186        break;
187      }
188    }
189    if (!exist) {
190      return false;
191    }
192  }
193  return true;
194}
195
196TEST_F(ReportCommandTest, pid_filter_option) {
197  Report(PERF_DATA_WITH_MULTIPLE_PIDS_AND_TIDS, {"--sort", "pid"});
198  ASSERT_TRUE(success);
199  ASSERT_FALSE(AllItemsWithString(lines, {"17441"}));
200  ASSERT_FALSE(AllItemsWithString(lines, {"17441", "17443"}));
201  Report(PERF_DATA_WITH_MULTIPLE_PIDS_AND_TIDS,
202         {"--sort", "pid", "--pids", "17441"});
203  ASSERT_TRUE(success);
204  ASSERT_TRUE(AllItemsWithString(lines, {"17441"}));
205  Report(PERF_DATA_WITH_MULTIPLE_PIDS_AND_TIDS,
206         {"--sort", "pid", "--pids", "17441,17443"});
207  ASSERT_TRUE(success);
208  ASSERT_TRUE(AllItemsWithString(lines, {"17441", "17443"}));
209
210  // Test that --pids option is not the same as --tids option.
211  // Thread 17445 and 17441 are in process 17441.
212  Report(PERF_DATA_WITH_MULTIPLE_PIDS_AND_TIDS,
213         {"--sort", "tid", "--pids", "17441"});
214  ASSERT_TRUE(success);
215  ASSERT_NE(content.find("17441"), std::string::npos);
216  ASSERT_NE(content.find("17445"), std::string::npos);
217}
218
219TEST_F(ReportCommandTest, wrong_pid_filter_option) {
220  ASSERT_EXIT(
221      {
222        Report(PERF_DATA_WITH_MULTIPLE_PIDS_AND_TIDS, {"--pids", "2,bogus"});
223        exit(success ? 0 : 1);
224      },
225      testing::ExitedWithCode(1), "invalid id in --pids option: bogus");
226}
227
228TEST_F(ReportCommandTest, tid_filter_option) {
229  Report(PERF_DATA_WITH_MULTIPLE_PIDS_AND_TIDS, {"--sort", "tid"});
230  ASSERT_TRUE(success);
231  ASSERT_FALSE(AllItemsWithString(lines, {"17441"}));
232  ASSERT_FALSE(AllItemsWithString(lines, {"17441", "17445"}));
233  Report(PERF_DATA_WITH_MULTIPLE_PIDS_AND_TIDS,
234         {"--sort", "tid", "--tids", "17441"});
235  ASSERT_TRUE(success);
236  ASSERT_TRUE(AllItemsWithString(lines, {"17441"}));
237  Report(PERF_DATA_WITH_MULTIPLE_PIDS_AND_TIDS,
238         {"--sort", "tid", "--tids", "17441,17445"});
239  ASSERT_TRUE(success);
240  ASSERT_TRUE(AllItemsWithString(lines, {"17441", "17445"}));
241}
242
243TEST_F(ReportCommandTest, wrong_tid_filter_option) {
244  ASSERT_EXIT(
245      {
246        Report(PERF_DATA_WITH_MULTIPLE_PIDS_AND_TIDS, {"--tids", "2,bogus"});
247        exit(success ? 0 : 1);
248      },
249      testing::ExitedWithCode(1), "invalid id in --tids option: bogus");
250}
251
252TEST_F(ReportCommandTest, comm_filter_option) {
253  Report(PERF_DATA, {"--sort", "comm"});
254  ASSERT_TRUE(success);
255  ASSERT_FALSE(AllItemsWithString(lines, {"t1"}));
256  ASSERT_FALSE(AllItemsWithString(lines, {"t1", "t2"}));
257  Report(PERF_DATA, {"--sort", "comm", "--comms", "t1"});
258  ASSERT_TRUE(success);
259  ASSERT_TRUE(AllItemsWithString(lines, {"t1"}));
260  Report(PERF_DATA, {"--sort", "comm", "--comms", "t1,t2"});
261  ASSERT_TRUE(success);
262  ASSERT_TRUE(AllItemsWithString(lines, {"t1", "t2"}));
263}
264
265TEST_F(ReportCommandTest, dso_filter_option) {
266  Report(PERF_DATA, {"--sort", "dso"});
267  ASSERT_TRUE(success);
268  ASSERT_FALSE(AllItemsWithString(lines, {"/t1"}));
269  ASSERT_FALSE(AllItemsWithString(lines, {"/t1", "/t2"}));
270  Report(PERF_DATA, {"--sort", "dso", "--dsos", "/t1"});
271  ASSERT_TRUE(success);
272  ASSERT_TRUE(AllItemsWithString(lines, {"/t1"}));
273  Report(PERF_DATA, {"--sort", "dso", "--dsos", "/t1,/t2"});
274  ASSERT_TRUE(success);
275  ASSERT_TRUE(AllItemsWithString(lines, {"/t1", "/t2"}));
276}
277
278TEST_F(ReportCommandTest, symbol_filter_option) {
279  Report(PERF_DATA_WITH_SYMBOLS, {"--sort", "symbol"});
280  ASSERT_TRUE(success);
281  ASSERT_FALSE(AllItemsWithString(lines, {"func2(int, int)"}));
282  ASSERT_FALSE(AllItemsWithString(lines, {"main", "func2(int, int)"}));
283  Report(PERF_DATA_WITH_SYMBOLS,
284         {"--sort", "symbol", "--symbols", "func2(int, int)"});
285  ASSERT_TRUE(success);
286  ASSERT_TRUE(AllItemsWithString(lines, {"func2(int, int)"}));
287  Report(PERF_DATA_WITH_SYMBOLS,
288         {"--sort", "symbol", "--symbols", "main;func2(int, int)"});
289  ASSERT_TRUE(success);
290  ASSERT_TRUE(AllItemsWithString(lines, {"main", "func2(int, int)"}));
291}
292
293TEST_F(ReportCommandTest, use_branch_address) {
294  Report(BRANCH_PERF_DATA, {"-b", "--sort", "symbol_from,symbol_to"});
295  std::set<std::pair<std::string, std::string>> hit_set;
296  bool after_overhead = false;
297  for (const auto& line : lines) {
298    if (!after_overhead && line.find("Overhead") != std::string::npos) {
299      after_overhead = true;
300    } else if (after_overhead) {
301      char from[80];
302      char to[80];
303      if (sscanf(line.c_str(), "%*f%%%s%s", from, to) == 2) {
304        hit_set.insert(std::make_pair<std::string, std::string>(from, to));
305      }
306    }
307  }
308  ASSERT_NE(hit_set.find(std::make_pair<std::string, std::string>(
309                "GlobalFunc", "CalledFunc")),
310            hit_set.end());
311  ASSERT_NE(hit_set.find(std::make_pair<std::string, std::string>(
312                "CalledFunc", "GlobalFunc")),
313            hit_set.end());
314}
315
316TEST_F(ReportCommandTest, report_symbols_of_nativelib_in_apk) {
317  Report(NATIVELIB_IN_APK_PERF_DATA);
318  ASSERT_TRUE(success);
319  ASSERT_NE(content.find(GetUrlInApk(APK_FILE, NATIVELIB_IN_APK)),
320            std::string::npos);
321  ASSERT_NE(content.find("Func2"), std::string::npos);
322}
323
324TEST_F(ReportCommandTest, report_more_than_one_event_types) {
325  Report(PERF_DATA_WITH_TWO_EVENT_TYPES);
326  ASSERT_TRUE(success);
327  ASSERT_NE(content.find("cpu-cycles"), std::string::npos);
328  ASSERT_NE(content.find("cpu-clock"), std::string::npos);
329}
330
331TEST_F(ReportCommandTest, report_kernel_symbol) {
332  Report(PERF_DATA_WITH_KERNEL_SYMBOL);
333  ASSERT_TRUE(success);
334  ASSERT_NE(content.find("perf_event_aux"), std::string::npos);
335}
336
337TEST_F(ReportCommandTest, report_dumped_symbols) {
338  Report(PERF_DATA_WITH_SYMBOLS);
339  ASSERT_TRUE(success);
340  ASSERT_NE(content.find("main"), std::string::npos);
341  Report(PERF_DATA_WITH_SYMBOLS_FOR_NONZERO_MINVADDR_DSO);
342  ASSERT_TRUE(success);
343  ASSERT_NE(content.find("memcpy"), std::string::npos);
344}
345
346TEST_F(ReportCommandTest, report_dumped_symbols_with_symfs_dir) {
347  // Check if we can report symbols when they appear both in perf.data and symfs dir.
348  Report(PERF_DATA_WITH_SYMBOLS, {"--symfs", GetTestDataDir()});
349  ASSERT_TRUE(success);
350  ASSERT_NE(content.find("main"), std::string::npos);
351}
352
353TEST_F(ReportCommandTest, report_sort_vaddr_in_file) {
354  Report(PERF_DATA, {"--sort", "vaddr_in_file"});
355  ASSERT_TRUE(success);
356  ASSERT_NE(content.find("VaddrInFile"), std::string::npos);
357}
358
359TEST_F(ReportCommandTest, check_build_id) {
360  Report(PERF_DATA_FOR_BUILD_ID_CHECK,
361         {"--symfs", GetTestData(CORRECT_SYMFS_FOR_BUILD_ID_CHECK)});
362  ASSERT_TRUE(success);
363  ASSERT_NE(content.find("main"), std::string::npos);
364  ASSERT_EXIT(
365      {
366        Report(PERF_DATA_FOR_BUILD_ID_CHECK,
367               {"--symfs", GetTestData(WRONG_SYMFS_FOR_BUILD_ID_CHECK)});
368        if (!success) {
369          exit(1);
370        }
371        if (content.find("main") != std::string::npos) {
372          exit(2);
373        }
374        exit(0);
375      },
376      testing::ExitedWithCode(0), "Build id mismatch");
377}
378
379TEST_F(ReportCommandTest, no_show_ip_option) {
380  Report(PERF_DATA);
381  ASSERT_TRUE(success);
382  ASSERT_EQ(content.find("unknown"), std::string::npos);
383  Report(PERF_DATA, {"--no-show-ip"});
384  ASSERT_TRUE(success);
385  ASSERT_NE(content.find("unknown"), std::string::npos);
386}
387
388TEST_F(ReportCommandTest, no_symbol_table_warning) {
389  ASSERT_EXIT(
390      {
391        Report(PERF_DATA,
392               {"--symfs", GetTestData(SYMFS_FOR_NO_SYMBOL_TABLE_WARNING)});
393        if (!success) {
394          exit(1);
395        }
396        if (content.find("GlobalFunc") != std::string::npos) {
397          exit(2);
398        }
399        exit(0);
400      },
401      testing::ExitedWithCode(0), "elf doesn't contain symbol table");
402}
403
404TEST_F(ReportCommandTest, read_elf_file_warning) {
405  ASSERT_EXIT(
406      {
407        Report(PERF_DATA,
408               {"--symfs", GetTestData(SYMFS_FOR_READ_ELF_FILE_WARNING)});
409        if (!success) {
410          exit(1);
411        }
412        if (content.find("GlobalFunc") != std::string::npos) {
413          exit(2);
414        }
415        exit(0);
416      },
417      testing::ExitedWithCode(0), "elf: Read failed");
418}
419
420TEST_F(ReportCommandTest, report_data_generated_by_linux_perf) {
421  Report(PERF_DATA_GENERATED_BY_LINUX_PERF);
422  ASSERT_TRUE(success);
423}
424
425TEST_F(ReportCommandTest, max_stack_and_percent_limit_option) {
426  Report(PERF_DATA_MAX_STACK_AND_PERCENT_LIMIT, {"-g"});
427  ASSERT_TRUE(success);
428  ASSERT_NE(content.find("89.03"), std::string::npos);
429
430  Report(PERF_DATA_MAX_STACK_AND_PERCENT_LIMIT, {"-g", "--max-stack", "0"});
431  ASSERT_TRUE(success);
432  ASSERT_EQ(content.find("89.03"), std::string::npos);
433  Report(PERF_DATA_MAX_STACK_AND_PERCENT_LIMIT, {"-g", "--max-stack", "1"});
434  ASSERT_TRUE(success);
435  ASSERT_NE(content.find("89.03"), std::string::npos);
436
437  Report(PERF_DATA_MAX_STACK_AND_PERCENT_LIMIT,
438         {"-g", "--percent-limit", "90"});
439  ASSERT_TRUE(success);
440  ASSERT_EQ(content.find("89.03"), std::string::npos);
441  Report(PERF_DATA_MAX_STACK_AND_PERCENT_LIMIT,
442         {"-g", "--percent-limit", "70"});
443  ASSERT_TRUE(success);
444  ASSERT_NE(content.find("89.03"), std::string::npos);
445}
446
447TEST_F(ReportCommandTest, kallsyms_option) {
448  Report(PERF_DATA, {"--kallsyms", GetTestData("kallsyms")});
449  ASSERT_TRUE(success);
450  ASSERT_NE(content.find("FakeKernelSymbol"), std::string::npos);
451}
452
453TEST_F(ReportCommandTest, invalid_perf_data) {
454  ASSERT_FALSE(ReportCmd()->Run({"-i", GetTestData(INVALID_PERF_DATA)}));
455}
456
457TEST_F(ReportCommandTest, raw_period_option) {
458  Report(PERF_DATA, {"--raw-period"});
459  ASSERT_TRUE(success);
460  ASSERT_NE(content.find("GlobalFunc"), std::string::npos);
461  ASSERT_EQ(content.find("%"), std::string::npos);
462}
463
464#if defined(__linux__)
465#include "event_selection_set.h"
466
467static std::unique_ptr<Command> RecordCmd() {
468  return CreateCommandInstance("record");
469}
470
471TEST_F(ReportCommandTest, dwarf_callgraph) {
472  if (IsDwarfCallChainSamplingSupported()) {
473    std::vector<std::unique_ptr<Workload>> workloads;
474    CreateProcesses(1, &workloads);
475    std::string pid = std::to_string(workloads[0]->GetPid());
476    TemporaryFile tmp_file;
477    ASSERT_TRUE(
478        RecordCmd()->Run({"-p", pid, "-g", "-o", tmp_file.path, "sleep", SLEEP_SEC}));
479    ReportRaw(tmp_file.path, {"-g"});
480    ASSERT_TRUE(success);
481  } else {
482    GTEST_LOG_(INFO) << "This test does nothing as dwarf callchain sampling is "
483                        "not supported on this device.";
484  }
485}
486
487TEST_F(ReportCommandTest, report_dwarf_callgraph_of_nativelib_in_apk) {
488  // NATIVELIB_IN_APK_PERF_DATA is recorded on arm64, so can only report
489  // callgraph on arm64.
490  if (GetBuildArch() == ARCH_ARM64) {
491    Report(NATIVELIB_IN_APK_PERF_DATA, {"-g"});
492    ASSERT_NE(content.find(GetUrlInApk(APK_FILE, NATIVELIB_IN_APK)),
493              std::string::npos);
494    ASSERT_NE(content.find("Func2"), std::string::npos);
495    ASSERT_NE(content.find("Func1"), std::string::npos);
496    ASSERT_NE(content.find("GlobalFunc"), std::string::npos);
497  } else {
498    GTEST_LOG_(INFO)
499        << "This test does nothing as it is only run on arm64 devices";
500  }
501}
502
503#endif
504