cmd_report_test.cpp revision 767dd17947f1ae9dd3d02f738d442a23ed76f2f6
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, use_branch_address) { 250 Report(BRANCH_PERF_DATA, {"-b", "--sort", "symbol_from,symbol_to"}); 251 std::set<std::pair<std::string, std::string>> hit_set; 252 bool after_overhead = false; 253 for (const auto& line : lines) { 254 if (!after_overhead && line.find("Overhead") != std::string::npos) { 255 after_overhead = true; 256 } else if (after_overhead) { 257 char from[80]; 258 char to[80]; 259 if (sscanf(line.c_str(), "%*f%%%s%s", from, to) == 2) { 260 hit_set.insert(std::make_pair<std::string, std::string>(from, to)); 261 } 262 } 263 } 264 ASSERT_NE(hit_set.find(std::make_pair<std::string, std::string>( 265 "GlobalFunc", "CalledFunc")), 266 hit_set.end()); 267 ASSERT_NE(hit_set.find(std::make_pair<std::string, std::string>( 268 "CalledFunc", "GlobalFunc")), 269 hit_set.end()); 270} 271 272TEST_F(ReportCommandTest, report_symbols_of_nativelib_in_apk) { 273 Report(NATIVELIB_IN_APK_PERF_DATA); 274 ASSERT_TRUE(success); 275 ASSERT_NE(content.find(GetUrlInApk(APK_FILE, NATIVELIB_IN_APK)), 276 std::string::npos); 277 ASSERT_NE(content.find("Func2"), std::string::npos); 278} 279 280TEST_F(ReportCommandTest, report_more_than_one_event_types) { 281 Report(PERF_DATA_WITH_TWO_EVENT_TYPES); 282 ASSERT_TRUE(success); 283 ASSERT_NE(content.find("cpu-cycles"), std::string::npos); 284 ASSERT_NE(content.find("cpu-clock"), std::string::npos); 285} 286 287TEST_F(ReportCommandTest, report_kernel_symbol) { 288 Report(PERF_DATA_WITH_KERNEL_SYMBOL); 289 ASSERT_TRUE(success); 290 ASSERT_NE(content.find("perf_event_comm_output"), std::string::npos); 291} 292 293TEST_F(ReportCommandTest, report_dumped_symbols) { 294 Report(PERF_DATA_WITH_SYMBOLS); 295 ASSERT_TRUE(success); 296 ASSERT_NE(content.find("page_fault"), std::string::npos); 297} 298 299#if defined(__linux__) 300 301static std::unique_ptr<Command> RecordCmd() { 302 return CreateCommandInstance("record"); 303} 304 305TEST_F(ReportCommandTest, dwarf_callgraph) { 306 if (IsDwarfCallChainSamplingSupported()) { 307 TemporaryFile tmp_file; 308 ASSERT_TRUE( 309 RecordCmd()->Run({"-g", "-o", tmp_file.path, "sleep", SLEEP_SEC})); 310 ReportRaw(tmp_file.path, {"-g"}); 311 ASSERT_TRUE(success); 312 } else { 313 GTEST_LOG_(INFO) << "This test does nothing as dwarf callchain sampling is " 314 "not supported on this device."; 315 } 316} 317 318TEST_F(ReportCommandTest, report_dwarf_callgraph_of_nativelib_in_apk) { 319 // NATIVELIB_IN_APK_PERF_DATA is recorded on arm64, so can only report 320 // callgraph on arm64. 321 if (GetBuildArch() == ARCH_ARM64) { 322 Report(NATIVELIB_IN_APK_PERF_DATA, {"-g"}); 323 ASSERT_NE(content.find(GetUrlInApk(APK_FILE, NATIVELIB_IN_APK)), 324 std::string::npos); 325 ASSERT_NE(content.find("Func2"), std::string::npos); 326 ASSERT_NE(content.find("Func1"), std::string::npos); 327 ASSERT_NE(content.find("GlobalFunc"), std::string::npos); 328 } else { 329 GTEST_LOG_(INFO) 330 << "This test does nothing as it is only run on arm64 devices"; 331 } 332} 333 334#endif 335