| Yabin Cui | 2672dea | 2015-05-21 12:17:23 -0700 | [diff] [blame] | 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 | |
| Yabin Cui | 6e51bef | 2016-02-23 21:41:03 -0800 | [diff] [blame] | 19 | #include <set> |
| 20 | #include <unordered_map> |
| 21 | |
| Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 22 | #include <android-base/file.h> |
| Yabin Cui | 6e51bef | 2016-02-23 21:41:03 -0800 | [diff] [blame] | 23 | #include <android-base/strings.h> |
| Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 24 | #include <android-base/test_utils.h> |
| 25 | |
| Yabin Cui | 2672dea | 2015-05-21 12:17:23 -0700 | [diff] [blame] | 26 | #include "command.h" |
| Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 27 | #include "get_test_data.h" |
| Yabin Cui | 8f680f6 | 2016-03-18 18:47:43 -0700 | [diff] [blame] | 28 | #include "perf_regs.h" |
| Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 29 | #include "read_apk.h" |
| Yabin Cui | 6e51bef | 2016-02-23 21:41:03 -0800 | [diff] [blame] | 30 | #include "test_util.h" |
| Yabin Cui | 2672dea | 2015-05-21 12:17:23 -0700 | [diff] [blame] | 31 | |
| 32 | static std::unique_ptr<Command> ReportCmd() { |
| 33 | return CreateCommandInstance("report"); |
| 34 | } |
| 35 | |
| 36 | class ReportCommandTest : public ::testing::Test { |
| 37 | protected: |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 38 | void Report( |
| Chih-Hung Hsieh | a2b4909 | 2016-07-27 15:22:01 -0700 | [diff] [blame] | 39 | const std::string& perf_data, |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 40 | const std::vector<std::string>& add_args = std::vector<std::string>()) { |
| Yabin Cui | 6e51bef | 2016-02-23 21:41:03 -0800 | [diff] [blame] | 41 | ReportRaw(GetTestData(perf_data), add_args); |
| Yabin Cui | 2672dea | 2015-05-21 12:17:23 -0700 | [diff] [blame] | 42 | } |
| Yabin Cui | 6e51bef | 2016-02-23 21:41:03 -0800 | [diff] [blame] | 43 | |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 44 | void ReportRaw( |
| Chih-Hung Hsieh | a2b4909 | 2016-07-27 15:22:01 -0700 | [diff] [blame] | 45 | const std::string& perf_data, |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 46 | const std::vector<std::string>& add_args = std::vector<std::string>()) { |
| Yabin Cui | 6e51bef | 2016-02-23 21:41:03 -0800 | [diff] [blame] | 47 | success = false; |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 48 | std::vector<std::string> args = { |
| 49 | "-i", perf_data, "--symfs", GetTestDataDir(), "-o", tmp_file.path}; |
| Yabin Cui | 6e51bef | 2016-02-23 21:41:03 -0800 | [diff] [blame] | 50 | args.insert(args.end(), add_args.begin(), add_args.end()); |
| 51 | ASSERT_TRUE(ReportCmd()->Run(args)); |
| 52 | ASSERT_TRUE(android::base::ReadFileToString(tmp_file.path, &content)); |
| 53 | ASSERT_TRUE(!content.empty()); |
| 54 | std::vector<std::string> raw_lines = android::base::Split(content, "\n"); |
| 55 | lines.clear(); |
| 56 | for (const auto& line : raw_lines) { |
| 57 | std::string s = android::base::Trim(line); |
| 58 | if (!s.empty()) { |
| 59 | lines.push_back(s); |
| 60 | } |
| 61 | } |
| 62 | ASSERT_GE(lines.size(), 2u); |
| 63 | success = true; |
| 64 | } |
| 65 | |
| 66 | TemporaryFile tmp_file; |
| 67 | std::string content; |
| 68 | std::vector<std::string> lines; |
| 69 | bool success; |
| Yabin Cui | 2672dea | 2015-05-21 12:17:23 -0700 | [diff] [blame] | 70 | }; |
| 71 | |
| Yabin Cui | 6e51bef | 2016-02-23 21:41:03 -0800 | [diff] [blame] | 72 | TEST_F(ReportCommandTest, no_option) { |
| 73 | Report(PERF_DATA); |
| 74 | ASSERT_TRUE(success); |
| 75 | ASSERT_NE(content.find("GlobalFunc"), std::string::npos); |
| Yabin Cui | 2672dea | 2015-05-21 12:17:23 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| Yabin Cui | 0540053 | 2016-03-17 21:18:53 -0700 | [diff] [blame] | 78 | TEST_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 | |
| Yabin Cui | 2672dea | 2015-05-21 12:17:23 -0700 | [diff] [blame] | 84 | TEST_F(ReportCommandTest, sort_option_pid) { |
| Yabin Cui | 6e51bef | 2016-02-23 21:41:03 -0800 | [diff] [blame] | 85 | Report(PERF_DATA, {"--sort", "pid"}); |
| 86 | ASSERT_TRUE(success); |
| 87 | size_t line_index = 0; |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 88 | while (line_index < lines.size() && |
| 89 | lines[line_index].find("Pid") == std::string::npos) { |
| Yabin Cui | 6e51bef | 2016-02-23 21:41:03 -0800 | [diff] [blame] | 90 | line_index++; |
| 91 | } |
| 92 | ASSERT_LT(line_index + 2, lines.size()); |
| Yabin Cui | 2672dea | 2015-05-21 12:17:23 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| Yabin Cui | 6e51bef | 2016-02-23 21:41:03 -0800 | [diff] [blame] | 95 | TEST_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; |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 99 | while (line_index < lines.size() && |
| 100 | lines[line_index].find("Overhead") == std::string::npos) { |
| Yabin Cui | 6e51bef | 2016-02-23 21:41:03 -0800 | [diff] [blame] | 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); |
| Yabin Cui | 2672dea | 2015-05-21 12:17:23 -0700 | [diff] [blame] | 109 | } |
| Yabin Cui | 8a530e3 | 2015-06-23 20:42:01 -0700 | [diff] [blame] | 110 | |
| Yabin Cui | ecb9a30 | 2015-07-01 10:00:52 -0700 | [diff] [blame] | 111 | TEST_F(ReportCommandTest, children_option) { |
| Yabin Cui | 6e51bef | 2016-02-23 21:41:03 -0800 | [diff] [blame] | 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; |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 118 | if (sscanf(lines[i].c_str(), "%lf%%%lf%%%s", &pair.first, &pair.second, |
| 119 | name) == 3) { |
| Yabin Cui | 6e51bef | 2016-02-23 21:41:03 -0800 | [diff] [blame] | 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 | |
| 132 | static 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 && |
| Chih-Hung Hsieh | a2b4909 | 2016-07-27 15:22:01 -0700 | [diff] [blame] | 136 | lines[i + 1].find('|') != std::string::npos && |
| Yabin Cui | 6e51bef | 2016-02-23 21:41:03 -0800 | [diff] [blame] | 137 | lines[i + 2].find("main") != std::string::npos) { |
| 138 | found = true; |
| 139 | break; |
| 140 | } |
| 141 | } |
| 142 | return found; |
| 143 | } |
| 144 | |
| 145 | static 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 && |
| Chih-Hung Hsieh | a2b4909 | 2016-07-27 15:22:01 -0700 | [diff] [blame] | 149 | lines[i + 1].find('|') != std::string::npos && |
| Yabin Cui | 6e51bef | 2016-02-23 21:41:03 -0800 | [diff] [blame] | 150 | lines[i + 2].find("GlobalFunc") != std::string::npos) { |
| 151 | found = true; |
| 152 | break; |
| 153 | } |
| 154 | } |
| 155 | return found; |
| Yabin Cui | ecb9a30 | 2015-07-01 10:00:52 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | TEST_F(ReportCommandTest, callgraph_option) { |
| Yabin Cui | 6e51bef | 2016-02-23 21:41:03 -0800 | [diff] [blame] | 159 | Report(CALLGRAPH_FP_PERF_DATA, {"-g"}); |
| 160 | ASSERT_TRUE(success); |
| Yabin Cui | 8a599d7 | 2016-07-21 18:32:53 -0700 | [diff] [blame] | 161 | ASSERT_TRUE(CheckCallerMode(lines)); |
| Yabin Cui | 6e51bef | 2016-02-23 21:41:03 -0800 | [diff] [blame] | 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 | |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 170 | static bool AllItemsWithString(std::vector<std::string>& lines, |
| 171 | const std::vector<std::string>& strs) { |
| Yabin Cui | 6e51bef | 2016-02-23 21:41:03 -0800 | [diff] [blame] | 172 | size_t line_index = 0; |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 173 | while (line_index < lines.size() && |
| 174 | lines[line_index].find("Overhead") == std::string::npos) { |
| Yabin Cui | 6e51bef | 2016-02-23 21:41:03 -0800 | [diff] [blame] | 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; |
| Yabin Cui | ecb9a30 | 2015-07-01 10:00:52 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| Yabin Cui | 38e573e | 2015-08-06 11:25:09 -0700 | [diff] [blame] | 196 | TEST_F(ReportCommandTest, pid_filter_option) { |
| Yabin Cui | 861f655 | 2016-08-08 14:42:25 -0700 | [diff] [blame] | 197 | Report(PERF_DATA_WITH_MULTIPLE_PIDS_AND_TIDS, {"--sort", "pid"}); |
| Yabin Cui | 6e51bef | 2016-02-23 21:41:03 -0800 | [diff] [blame] | 198 | ASSERT_TRUE(success); |
| Yabin Cui | 861f655 | 2016-08-08 14:42:25 -0700 | [diff] [blame] | 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"}); |
| Yabin Cui | 6e51bef | 2016-02-23 21:41:03 -0800 | [diff] [blame] | 203 | ASSERT_TRUE(success); |
| Yabin Cui | 861f655 | 2016-08-08 14:42:25 -0700 | [diff] [blame] | 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 | |
| 219 | TEST_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"); |
| Yabin Cui | 38e573e | 2015-08-06 11:25:09 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | TEST_F(ReportCommandTest, tid_filter_option) { |
| Yabin Cui | 861f655 | 2016-08-08 14:42:25 -0700 | [diff] [blame] | 229 | Report(PERF_DATA_WITH_MULTIPLE_PIDS_AND_TIDS, {"--sort", "tid"}); |
| Yabin Cui | 6e51bef | 2016-02-23 21:41:03 -0800 | [diff] [blame] | 230 | ASSERT_TRUE(success); |
| Yabin Cui | 861f655 | 2016-08-08 14:42:25 -0700 | [diff] [blame] | 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"}); |
| Yabin Cui | 6e51bef | 2016-02-23 21:41:03 -0800 | [diff] [blame] | 235 | ASSERT_TRUE(success); |
| Yabin Cui | 861f655 | 2016-08-08 14:42:25 -0700 | [diff] [blame] | 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 | |
| 243 | TEST_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"); |
| Yabin Cui | 38e573e | 2015-08-06 11:25:09 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | TEST_F(ReportCommandTest, comm_filter_option) { |
| Yabin Cui | 6e51bef | 2016-02-23 21:41:03 -0800 | [diff] [blame] | 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"})); |
| Yabin Cui | 38e573e | 2015-08-06 11:25:09 -0700 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | TEST_F(ReportCommandTest, dso_filter_option) { |
| Yabin Cui | 6e51bef | 2016-02-23 21:41:03 -0800 | [diff] [blame] | 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"})); |
| Yabin Cui | 38e573e | 2015-08-06 11:25:09 -0700 | [diff] [blame] | 276 | } |
| 277 | |
| Yabin Cui | f79fbd1 | 2016-07-06 12:26:13 -0700 | [diff] [blame] | 278 | TEST_F(ReportCommandTest, symbol_filter_option) { |
| 279 | Report(PERF_DATA_WITH_SYMBOLS, {"--sort", "symbol"}); |
| 280 | ASSERT_TRUE(success); |
| Yabin Cui | 05ef2ea | 2016-07-11 13:50:01 -0700 | [diff] [blame] | 281 | ASSERT_FALSE(AllItemsWithString(lines, {"func2(int, int)"})); |
| 282 | ASSERT_FALSE(AllItemsWithString(lines, {"main", "func2(int, int)"})); |
| Yabin Cui | f79fbd1 | 2016-07-06 12:26:13 -0700 | [diff] [blame] | 283 | Report(PERF_DATA_WITH_SYMBOLS, |
| Yabin Cui | 05ef2ea | 2016-07-11 13:50:01 -0700 | [diff] [blame] | 284 | {"--sort", "symbol", "--symbols", "func2(int, int)"}); |
| Yabin Cui | f79fbd1 | 2016-07-06 12:26:13 -0700 | [diff] [blame] | 285 | ASSERT_TRUE(success); |
| Yabin Cui | 05ef2ea | 2016-07-11 13:50:01 -0700 | [diff] [blame] | 286 | ASSERT_TRUE(AllItemsWithString(lines, {"func2(int, int)"})); |
| Yabin Cui | f79fbd1 | 2016-07-06 12:26:13 -0700 | [diff] [blame] | 287 | Report(PERF_DATA_WITH_SYMBOLS, |
| Yabin Cui | 05ef2ea | 2016-07-11 13:50:01 -0700 | [diff] [blame] | 288 | {"--sort", "symbol", "--symbols", "main;func2(int, int)"}); |
| Yabin Cui | f79fbd1 | 2016-07-06 12:26:13 -0700 | [diff] [blame] | 289 | ASSERT_TRUE(success); |
| Yabin Cui | 05ef2ea | 2016-07-11 13:50:01 -0700 | [diff] [blame] | 290 | ASSERT_TRUE(AllItemsWithString(lines, {"main", "func2(int, int)"})); |
| Yabin Cui | f79fbd1 | 2016-07-06 12:26:13 -0700 | [diff] [blame] | 291 | } |
| 292 | |
| Yabin Cui | 6e51bef | 2016-02-23 21:41:03 -0800 | [diff] [blame] | 293 | TEST_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 | } |
| Yabin Cui | 8a530e3 | 2015-06-23 20:42:01 -0700 | [diff] [blame] | 307 | } |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 308 | ASSERT_NE(hit_set.find(std::make_pair<std::string, std::string>( |
| 309 | "GlobalFunc", "CalledFunc")), |
| Yabin Cui | 6e51bef | 2016-02-23 21:41:03 -0800 | [diff] [blame] | 310 | hit_set.end()); |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 311 | ASSERT_NE(hit_set.find(std::make_pair<std::string, std::string>( |
| 312 | "CalledFunc", "GlobalFunc")), |
| Yabin Cui | 6e51bef | 2016-02-23 21:41:03 -0800 | [diff] [blame] | 313 | hit_set.end()); |
| Yabin Cui | 8a530e3 | 2015-06-23 20:42:01 -0700 | [diff] [blame] | 314 | } |
| Yabin Cui | 3c8c213 | 2015-08-13 20:30:20 -0700 | [diff] [blame] | 315 | |
| Yabin Cui | 8f680f6 | 2016-03-18 18:47:43 -0700 | [diff] [blame] | 316 | TEST_F(ReportCommandTest, report_symbols_of_nativelib_in_apk) { |
| 317 | Report(NATIVELIB_IN_APK_PERF_DATA); |
| 318 | ASSERT_TRUE(success); |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 319 | ASSERT_NE(content.find(GetUrlInApk(APK_FILE, NATIVELIB_IN_APK)), |
| 320 | std::string::npos); |
| Yabin Cui | 8f680f6 | 2016-03-18 18:47:43 -0700 | [diff] [blame] | 321 | ASSERT_NE(content.find("Func2"), std::string::npos); |
| 322 | } |
| 323 | |
| Yabin Cui | 2d6efe4 | 2016-04-01 20:22:35 -0700 | [diff] [blame] | 324 | TEST_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 | |
| Yabin Cui | b421297 | 2016-05-25 14:08:05 -0700 | [diff] [blame] | 331 | TEST_F(ReportCommandTest, report_kernel_symbol) { |
| 332 | Report(PERF_DATA_WITH_KERNEL_SYMBOL); |
| 333 | ASSERT_TRUE(success); |
| Yabin Cui | 1761a27 | 2016-06-23 17:11:14 -0700 | [diff] [blame] | 334 | ASSERT_NE(content.find("perf_event_aux"), std::string::npos); |
| Yabin Cui | b421297 | 2016-05-25 14:08:05 -0700 | [diff] [blame] | 335 | } |
| 336 | |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 337 | TEST_F(ReportCommandTest, report_dumped_symbols) { |
| 338 | Report(PERF_DATA_WITH_SYMBOLS); |
| 339 | ASSERT_TRUE(success); |
| Yabin Cui | 05ef2ea | 2016-07-11 13:50:01 -0700 | [diff] [blame] | 340 | ASSERT_NE(content.find("main"), std::string::npos); |
| Yabin Cui | c855ecc | 2016-07-11 17:04:54 -0700 | [diff] [blame] | 341 | Report(PERF_DATA_WITH_SYMBOLS_FOR_NONZERO_MINVADDR_DSO); |
| 342 | ASSERT_TRUE(success); |
| Yabin Cui | c5b4a31 | 2016-10-24 13:38:38 -0700 | [diff] [blame] | 343 | ASSERT_NE(content.find("memcpy"), std::string::npos); |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 344 | } |
| 345 | |
| Yabin Cui | e2f1078 | 2016-12-15 12:00:44 -0800 | [diff] [blame^] | 346 | TEST_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 | |
| Yabin Cui | 9970a23 | 2016-06-29 12:18:11 -0700 | [diff] [blame] | 353 | TEST_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 | |
| Yabin Cui | eec606c | 2016-07-07 13:53:33 -0700 | [diff] [blame] | 359 | TEST_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 | }, |
| Yabin Cui | dec43c1 | 2016-07-29 16:40:40 -0700 | [diff] [blame] | 376 | testing::ExitedWithCode(0), "Build id mismatch"); |
| Yabin Cui | eec606c | 2016-07-07 13:53:33 -0700 | [diff] [blame] | 377 | } |
| 378 | |
| Yabin Cui | 15475e6 | 2016-07-14 13:26:19 -0700 | [diff] [blame] | 379 | TEST_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 | |
| Yabin Cui | dec43c1 | 2016-07-29 16:40:40 -0700 | [diff] [blame] | 388 | TEST_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 | |
| 404 | TEST_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 | |
| Yabin Cui | eafa718 | 2016-08-30 13:13:17 -0700 | [diff] [blame] | 420 | TEST_F(ReportCommandTest, report_data_generated_by_linux_perf) { |
| 421 | Report(PERF_DATA_GENERATED_BY_LINUX_PERF); |
| 422 | ASSERT_TRUE(success); |
| 423 | } |
| 424 | |
| Yabin Cui | c0565bb | 2016-09-29 15:59:33 -0700 | [diff] [blame] | 425 | TEST_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 | |
| Yabin Cui | 18385c4 | 2016-12-09 14:51:04 -0800 | [diff] [blame] | 447 | TEST_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 | |
| Yabin Cui | 044861b | 2016-12-13 17:49:19 -0800 | [diff] [blame] | 453 | TEST_F(ReportCommandTest, invalid_perf_data) { |
| 454 | ASSERT_FALSE(ReportCmd()->Run({"-i", GetTestData(INVALID_PERF_DATA)})); |
| 455 | } |
| 456 | |
| Yabin Cui | 19e6b6d | 2016-03-10 11:49:57 -0800 | [diff] [blame] | 457 | #if defined(__linux__) |
| Yabin Cui | 003b245 | 2016-09-29 15:32:45 -0700 | [diff] [blame] | 458 | #include "event_selection_set.h" |
| Yabin Cui | 6e51bef | 2016-02-23 21:41:03 -0800 | [diff] [blame] | 459 | |
| 460 | static std::unique_ptr<Command> RecordCmd() { |
| 461 | return CreateCommandInstance("record"); |
| Yabin Cui | 3c8c213 | 2015-08-13 20:30:20 -0700 | [diff] [blame] | 462 | } |
| Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 463 | |
| Yabin Cui | 6e51bef | 2016-02-23 21:41:03 -0800 | [diff] [blame] | 464 | TEST_F(ReportCommandTest, dwarf_callgraph) { |
| Yabin Cui | 19e6b6d | 2016-03-10 11:49:57 -0800 | [diff] [blame] | 465 | if (IsDwarfCallChainSamplingSupported()) { |
| Yabin Cui | 5be914d | 2016-11-29 15:21:13 -0800 | [diff] [blame] | 466 | std::vector<std::unique_ptr<Workload>> workloads; |
| 467 | CreateProcesses(1, &workloads); |
| 468 | std::string pid = std::to_string(workloads[0]->GetPid()); |
| Yabin Cui | 19e6b6d | 2016-03-10 11:49:57 -0800 | [diff] [blame] | 469 | TemporaryFile tmp_file; |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 470 | ASSERT_TRUE( |
| Yabin Cui | 5be914d | 2016-11-29 15:21:13 -0800 | [diff] [blame] | 471 | RecordCmd()->Run({"-p", pid, "-g", "-o", tmp_file.path, "sleep", SLEEP_SEC})); |
| Yabin Cui | 19e6b6d | 2016-03-10 11:49:57 -0800 | [diff] [blame] | 472 | ReportRaw(tmp_file.path, {"-g"}); |
| 473 | ASSERT_TRUE(success); |
| 474 | } else { |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 475 | GTEST_LOG_(INFO) << "This test does nothing as dwarf callchain sampling is " |
| 476 | "not supported on this device."; |
| Yabin Cui | 19e6b6d | 2016-03-10 11:49:57 -0800 | [diff] [blame] | 477 | } |
| Yabin Cui | 6e51bef | 2016-02-23 21:41:03 -0800 | [diff] [blame] | 478 | } |
| 479 | |
| Yabin Cui | 8f680f6 | 2016-03-18 18:47:43 -0700 | [diff] [blame] | 480 | TEST_F(ReportCommandTest, report_dwarf_callgraph_of_nativelib_in_apk) { |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 481 | // NATIVELIB_IN_APK_PERF_DATA is recorded on arm64, so can only report |
| 482 | // callgraph on arm64. |
| Yabin Cui | 8f680f6 | 2016-03-18 18:47:43 -0700 | [diff] [blame] | 483 | if (GetBuildArch() == ARCH_ARM64) { |
| 484 | Report(NATIVELIB_IN_APK_PERF_DATA, {"-g"}); |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 485 | ASSERT_NE(content.find(GetUrlInApk(APK_FILE, NATIVELIB_IN_APK)), |
| 486 | std::string::npos); |
| Yabin Cui | 8f680f6 | 2016-03-18 18:47:43 -0700 | [diff] [blame] | 487 | ASSERT_NE(content.find("Func2"), std::string::npos); |
| 488 | ASSERT_NE(content.find("Func1"), std::string::npos); |
| 489 | ASSERT_NE(content.find("GlobalFunc"), std::string::npos); |
| 490 | } else { |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 491 | GTEST_LOG_(INFO) |
| 492 | << "This test does nothing as it is only run on arm64 devices"; |
| Yabin Cui | 8f680f6 | 2016-03-18 18:47:43 -0700 | [diff] [blame] | 493 | } |
| 494 | } |
| 495 | |
| Yabin Cui | 6e51bef | 2016-02-23 21:41:03 -0800 | [diff] [blame] | 496 | #endif |