| 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 | f560a6f | 2016-12-14 17:43:26 -0800 | [diff] [blame] | 48 | TemporaryFile tmp_file; |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 49 | std::vector<std::string> args = { |
| 50 | "-i", perf_data, "--symfs", GetTestDataDir(), "-o", tmp_file.path}; |
| Yabin Cui | 6e51bef | 2016-02-23 21:41:03 -0800 | [diff] [blame] | 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 | |
| Yabin Cui | 6e51bef | 2016-02-23 21:41:03 -0800 | [diff] [blame] | 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; |
| Yabin Cui | dcb2a3e | 2017-05-18 17:11:32 -0700 | [diff] [blame] | 134 | for (size_t i = 0; i + 1 < lines.size(); ++i) { |
| Yabin Cui | 6e51bef | 2016-02-23 21:41:03 -0800 | [diff] [blame] | 135 | if (lines[i].find("GlobalFunc") != std::string::npos && |
| Yabin Cui | dcb2a3e | 2017-05-18 17:11:32 -0700 | [diff] [blame] | 136 | lines[i + 1].find("main") != std::string::npos) { |
| Yabin Cui | 6e51bef | 2016-02-23 21:41:03 -0800 | [diff] [blame] | 137 | found = true; |
| 138 | break; |
| 139 | } |
| 140 | } |
| 141 | return found; |
| 142 | } |
| 143 | |
| 144 | static bool CheckCallerMode(std::vector<std::string>& lines) { |
| 145 | bool found = false; |
| Yabin Cui | dcb2a3e | 2017-05-18 17:11:32 -0700 | [diff] [blame] | 146 | for (size_t i = 0; i + 1 < lines.size(); ++i) { |
| Yabin Cui | 6e51bef | 2016-02-23 21:41:03 -0800 | [diff] [blame] | 147 | if (lines[i].find("main") != std::string::npos && |
| Yabin Cui | dcb2a3e | 2017-05-18 17:11:32 -0700 | [diff] [blame] | 148 | lines[i + 1].find("GlobalFunc") != std::string::npos) { |
| Yabin Cui | 6e51bef | 2016-02-23 21:41:03 -0800 | [diff] [blame] | 149 | found = true; |
| 150 | break; |
| 151 | } |
| 152 | } |
| 153 | return found; |
| Yabin Cui | ecb9a30 | 2015-07-01 10:00:52 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | TEST_F(ReportCommandTest, callgraph_option) { |
| Yabin Cui | 6e51bef | 2016-02-23 21:41:03 -0800 | [diff] [blame] | 157 | Report(CALLGRAPH_FP_PERF_DATA, {"-g"}); |
| 158 | ASSERT_TRUE(success); |
| Yabin Cui | 8a599d7 | 2016-07-21 18:32:53 -0700 | [diff] [blame] | 159 | ASSERT_TRUE(CheckCallerMode(lines)); |
| Yabin Cui | 6e51bef | 2016-02-23 21:41:03 -0800 | [diff] [blame] | 160 | Report(CALLGRAPH_FP_PERF_DATA, {"-g", "callee"}); |
| 161 | ASSERT_TRUE(success); |
| 162 | ASSERT_TRUE(CheckCalleeMode(lines)); |
| 163 | Report(CALLGRAPH_FP_PERF_DATA, {"-g", "caller"}); |
| 164 | ASSERT_TRUE(success); |
| 165 | ASSERT_TRUE(CheckCallerMode(lines)); |
| 166 | } |
| 167 | |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 168 | static bool AllItemsWithString(std::vector<std::string>& lines, |
| 169 | const std::vector<std::string>& strs) { |
| Yabin Cui | 6e51bef | 2016-02-23 21:41:03 -0800 | [diff] [blame] | 170 | size_t line_index = 0; |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 171 | while (line_index < lines.size() && |
| 172 | lines[line_index].find("Overhead") == std::string::npos) { |
| Yabin Cui | 6e51bef | 2016-02-23 21:41:03 -0800 | [diff] [blame] | 173 | line_index++; |
| 174 | } |
| 175 | if (line_index == lines.size() || line_index + 1 == lines.size()) { |
| 176 | return false; |
| 177 | } |
| 178 | line_index++; |
| 179 | for (; line_index < lines.size(); ++line_index) { |
| 180 | bool exist = false; |
| 181 | for (auto& s : strs) { |
| 182 | if (lines[line_index].find(s) != std::string::npos) { |
| 183 | exist = true; |
| 184 | break; |
| 185 | } |
| 186 | } |
| 187 | if (!exist) { |
| 188 | return false; |
| 189 | } |
| 190 | } |
| 191 | return true; |
| Yabin Cui | ecb9a30 | 2015-07-01 10:00:52 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| Yabin Cui | 38e573e | 2015-08-06 11:25:09 -0700 | [diff] [blame] | 194 | TEST_F(ReportCommandTest, pid_filter_option) { |
| Yabin Cui | 861f655 | 2016-08-08 14:42:25 -0700 | [diff] [blame] | 195 | Report(PERF_DATA_WITH_MULTIPLE_PIDS_AND_TIDS, {"--sort", "pid"}); |
| Yabin Cui | 6e51bef | 2016-02-23 21:41:03 -0800 | [diff] [blame] | 196 | ASSERT_TRUE(success); |
| Yabin Cui | 861f655 | 2016-08-08 14:42:25 -0700 | [diff] [blame] | 197 | ASSERT_FALSE(AllItemsWithString(lines, {"17441"})); |
| 198 | ASSERT_FALSE(AllItemsWithString(lines, {"17441", "17443"})); |
| 199 | Report(PERF_DATA_WITH_MULTIPLE_PIDS_AND_TIDS, |
| 200 | {"--sort", "pid", "--pids", "17441"}); |
| Yabin Cui | 6e51bef | 2016-02-23 21:41:03 -0800 | [diff] [blame] | 201 | ASSERT_TRUE(success); |
| Yabin Cui | 861f655 | 2016-08-08 14:42:25 -0700 | [diff] [blame] | 202 | ASSERT_TRUE(AllItemsWithString(lines, {"17441"})); |
| 203 | Report(PERF_DATA_WITH_MULTIPLE_PIDS_AND_TIDS, |
| 204 | {"--sort", "pid", "--pids", "17441,17443"}); |
| 205 | ASSERT_TRUE(success); |
| 206 | ASSERT_TRUE(AllItemsWithString(lines, {"17441", "17443"})); |
| 207 | |
| 208 | // Test that --pids option is not the same as --tids option. |
| 209 | // Thread 17445 and 17441 are in process 17441. |
| 210 | Report(PERF_DATA_WITH_MULTIPLE_PIDS_AND_TIDS, |
| 211 | {"--sort", "tid", "--pids", "17441"}); |
| 212 | ASSERT_TRUE(success); |
| 213 | ASSERT_NE(content.find("17441"), std::string::npos); |
| 214 | ASSERT_NE(content.find("17445"), std::string::npos); |
| 215 | } |
| 216 | |
| 217 | TEST_F(ReportCommandTest, wrong_pid_filter_option) { |
| 218 | ASSERT_EXIT( |
| 219 | { |
| 220 | Report(PERF_DATA_WITH_MULTIPLE_PIDS_AND_TIDS, {"--pids", "2,bogus"}); |
| 221 | exit(success ? 0 : 1); |
| 222 | }, |
| 223 | testing::ExitedWithCode(1), "invalid id in --pids option: bogus"); |
| Yabin Cui | 38e573e | 2015-08-06 11:25:09 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | TEST_F(ReportCommandTest, tid_filter_option) { |
| Yabin Cui | 861f655 | 2016-08-08 14:42:25 -0700 | [diff] [blame] | 227 | Report(PERF_DATA_WITH_MULTIPLE_PIDS_AND_TIDS, {"--sort", "tid"}); |
| Yabin Cui | 6e51bef | 2016-02-23 21:41:03 -0800 | [diff] [blame] | 228 | ASSERT_TRUE(success); |
| Yabin Cui | 861f655 | 2016-08-08 14:42:25 -0700 | [diff] [blame] | 229 | ASSERT_FALSE(AllItemsWithString(lines, {"17441"})); |
| 230 | ASSERT_FALSE(AllItemsWithString(lines, {"17441", "17445"})); |
| 231 | Report(PERF_DATA_WITH_MULTIPLE_PIDS_AND_TIDS, |
| 232 | {"--sort", "tid", "--tids", "17441"}); |
| Yabin Cui | 6e51bef | 2016-02-23 21:41:03 -0800 | [diff] [blame] | 233 | ASSERT_TRUE(success); |
| Yabin Cui | 861f655 | 2016-08-08 14:42:25 -0700 | [diff] [blame] | 234 | ASSERT_TRUE(AllItemsWithString(lines, {"17441"})); |
| 235 | Report(PERF_DATA_WITH_MULTIPLE_PIDS_AND_TIDS, |
| 236 | {"--sort", "tid", "--tids", "17441,17445"}); |
| 237 | ASSERT_TRUE(success); |
| 238 | ASSERT_TRUE(AllItemsWithString(lines, {"17441", "17445"})); |
| 239 | } |
| 240 | |
| 241 | TEST_F(ReportCommandTest, wrong_tid_filter_option) { |
| 242 | ASSERT_EXIT( |
| 243 | { |
| 244 | Report(PERF_DATA_WITH_MULTIPLE_PIDS_AND_TIDS, {"--tids", "2,bogus"}); |
| 245 | exit(success ? 0 : 1); |
| 246 | }, |
| 247 | testing::ExitedWithCode(1), "invalid id in --tids option: bogus"); |
| Yabin Cui | 38e573e | 2015-08-06 11:25:09 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | TEST_F(ReportCommandTest, comm_filter_option) { |
| Yabin Cui | 6e51bef | 2016-02-23 21:41:03 -0800 | [diff] [blame] | 251 | Report(PERF_DATA, {"--sort", "comm"}); |
| 252 | ASSERT_TRUE(success); |
| 253 | ASSERT_FALSE(AllItemsWithString(lines, {"t1"})); |
| 254 | ASSERT_FALSE(AllItemsWithString(lines, {"t1", "t2"})); |
| 255 | Report(PERF_DATA, {"--sort", "comm", "--comms", "t1"}); |
| 256 | ASSERT_TRUE(success); |
| 257 | ASSERT_TRUE(AllItemsWithString(lines, {"t1"})); |
| 258 | Report(PERF_DATA, {"--sort", "comm", "--comms", "t1,t2"}); |
| 259 | ASSERT_TRUE(success); |
| 260 | ASSERT_TRUE(AllItemsWithString(lines, {"t1", "t2"})); |
| Yabin Cui | 38e573e | 2015-08-06 11:25:09 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | TEST_F(ReportCommandTest, dso_filter_option) { |
| Yabin Cui | 6e51bef | 2016-02-23 21:41:03 -0800 | [diff] [blame] | 264 | Report(PERF_DATA, {"--sort", "dso"}); |
| 265 | ASSERT_TRUE(success); |
| 266 | ASSERT_FALSE(AllItemsWithString(lines, {"/t1"})); |
| 267 | ASSERT_FALSE(AllItemsWithString(lines, {"/t1", "/t2"})); |
| 268 | Report(PERF_DATA, {"--sort", "dso", "--dsos", "/t1"}); |
| 269 | ASSERT_TRUE(success); |
| 270 | ASSERT_TRUE(AllItemsWithString(lines, {"/t1"})); |
| 271 | Report(PERF_DATA, {"--sort", "dso", "--dsos", "/t1,/t2"}); |
| 272 | ASSERT_TRUE(success); |
| 273 | ASSERT_TRUE(AllItemsWithString(lines, {"/t1", "/t2"})); |
| Yabin Cui | 38e573e | 2015-08-06 11:25:09 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| Yabin Cui | f79fbd1 | 2016-07-06 12:26:13 -0700 | [diff] [blame] | 276 | TEST_F(ReportCommandTest, symbol_filter_option) { |
| 277 | Report(PERF_DATA_WITH_SYMBOLS, {"--sort", "symbol"}); |
| 278 | ASSERT_TRUE(success); |
| Yabin Cui | 05ef2ea | 2016-07-11 13:50:01 -0700 | [diff] [blame] | 279 | ASSERT_FALSE(AllItemsWithString(lines, {"func2(int, int)"})); |
| 280 | ASSERT_FALSE(AllItemsWithString(lines, {"main", "func2(int, int)"})); |
| Yabin Cui | f79fbd1 | 2016-07-06 12:26:13 -0700 | [diff] [blame] | 281 | Report(PERF_DATA_WITH_SYMBOLS, |
| Yabin Cui | 05ef2ea | 2016-07-11 13:50:01 -0700 | [diff] [blame] | 282 | {"--sort", "symbol", "--symbols", "func2(int, int)"}); |
| Yabin Cui | f79fbd1 | 2016-07-06 12:26:13 -0700 | [diff] [blame] | 283 | ASSERT_TRUE(success); |
| Yabin Cui | 05ef2ea | 2016-07-11 13:50:01 -0700 | [diff] [blame] | 284 | ASSERT_TRUE(AllItemsWithString(lines, {"func2(int, int)"})); |
| Yabin Cui | f79fbd1 | 2016-07-06 12:26:13 -0700 | [diff] [blame] | 285 | Report(PERF_DATA_WITH_SYMBOLS, |
| Yabin Cui | 05ef2ea | 2016-07-11 13:50:01 -0700 | [diff] [blame] | 286 | {"--sort", "symbol", "--symbols", "main;func2(int, int)"}); |
| Yabin Cui | f79fbd1 | 2016-07-06 12:26:13 -0700 | [diff] [blame] | 287 | ASSERT_TRUE(success); |
| Yabin Cui | 05ef2ea | 2016-07-11 13:50:01 -0700 | [diff] [blame] | 288 | ASSERT_TRUE(AllItemsWithString(lines, {"main", "func2(int, int)"})); |
| Yabin Cui | f79fbd1 | 2016-07-06 12:26:13 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| Yabin Cui | 6e51bef | 2016-02-23 21:41:03 -0800 | [diff] [blame] | 291 | TEST_F(ReportCommandTest, use_branch_address) { |
| 292 | Report(BRANCH_PERF_DATA, {"-b", "--sort", "symbol_from,symbol_to"}); |
| 293 | std::set<std::pair<std::string, std::string>> hit_set; |
| 294 | bool after_overhead = false; |
| 295 | for (const auto& line : lines) { |
| 296 | if (!after_overhead && line.find("Overhead") != std::string::npos) { |
| 297 | after_overhead = true; |
| 298 | } else if (after_overhead) { |
| 299 | char from[80]; |
| 300 | char to[80]; |
| 301 | if (sscanf(line.c_str(), "%*f%%%s%s", from, to) == 2) { |
| 302 | hit_set.insert(std::make_pair<std::string, std::string>(from, to)); |
| 303 | } |
| 304 | } |
| Yabin Cui | 8a530e3 | 2015-06-23 20:42:01 -0700 | [diff] [blame] | 305 | } |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 306 | ASSERT_NE(hit_set.find(std::make_pair<std::string, std::string>( |
| 307 | "GlobalFunc", "CalledFunc")), |
| Yabin Cui | 6e51bef | 2016-02-23 21:41:03 -0800 | [diff] [blame] | 308 | hit_set.end()); |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 309 | ASSERT_NE(hit_set.find(std::make_pair<std::string, std::string>( |
| 310 | "CalledFunc", "GlobalFunc")), |
| Yabin Cui | 6e51bef | 2016-02-23 21:41:03 -0800 | [diff] [blame] | 311 | hit_set.end()); |
| Yabin Cui | 8a530e3 | 2015-06-23 20:42:01 -0700 | [diff] [blame] | 312 | } |
| Yabin Cui | 3c8c213 | 2015-08-13 20:30:20 -0700 | [diff] [blame] | 313 | |
| Yabin Cui | 8f680f6 | 2016-03-18 18:47:43 -0700 | [diff] [blame] | 314 | TEST_F(ReportCommandTest, report_symbols_of_nativelib_in_apk) { |
| 315 | Report(NATIVELIB_IN_APK_PERF_DATA); |
| 316 | ASSERT_TRUE(success); |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 317 | ASSERT_NE(content.find(GetUrlInApk(APK_FILE, NATIVELIB_IN_APK)), |
| 318 | std::string::npos); |
| Yabin Cui | 8f680f6 | 2016-03-18 18:47:43 -0700 | [diff] [blame] | 319 | ASSERT_NE(content.find("Func2"), std::string::npos); |
| 320 | } |
| 321 | |
| Yabin Cui | 2d6efe4 | 2016-04-01 20:22:35 -0700 | [diff] [blame] | 322 | TEST_F(ReportCommandTest, report_more_than_one_event_types) { |
| 323 | Report(PERF_DATA_WITH_TWO_EVENT_TYPES); |
| 324 | ASSERT_TRUE(success); |
| Yabin Cui | ada97db | 2017-02-23 15:54:11 -0800 | [diff] [blame] | 325 | size_t pos = 0; |
| 326 | ASSERT_NE(pos = content.find("cpu-cycles", pos), std::string::npos); |
| 327 | ASSERT_NE(pos = content.find("Samples:", pos), std::string::npos); |
| 328 | ASSERT_NE(pos = content.find("cpu-clock", pos), std::string::npos); |
| 329 | ASSERT_NE(pos = content.find("Samples:", pos), std::string::npos); |
| Yabin Cui | 2d6efe4 | 2016-04-01 20:22:35 -0700 | [diff] [blame] | 330 | } |
| 331 | |
| Yabin Cui | b421297 | 2016-05-25 14:08:05 -0700 | [diff] [blame] | 332 | TEST_F(ReportCommandTest, report_kernel_symbol) { |
| 333 | Report(PERF_DATA_WITH_KERNEL_SYMBOL); |
| 334 | ASSERT_TRUE(success); |
| Yabin Cui | 1761a27 | 2016-06-23 17:11:14 -0700 | [diff] [blame] | 335 | ASSERT_NE(content.find("perf_event_aux"), std::string::npos); |
| Yabin Cui | b421297 | 2016-05-25 14:08:05 -0700 | [diff] [blame] | 336 | } |
| 337 | |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 338 | TEST_F(ReportCommandTest, report_dumped_symbols) { |
| 339 | Report(PERF_DATA_WITH_SYMBOLS); |
| 340 | ASSERT_TRUE(success); |
| Yabin Cui | 05ef2ea | 2016-07-11 13:50:01 -0700 | [diff] [blame] | 341 | ASSERT_NE(content.find("main"), std::string::npos); |
| Yabin Cui | c855ecc | 2016-07-11 17:04:54 -0700 | [diff] [blame] | 342 | Report(PERF_DATA_WITH_SYMBOLS_FOR_NONZERO_MINVADDR_DSO); |
| 343 | ASSERT_TRUE(success); |
| Yabin Cui | c5b4a31 | 2016-10-24 13:38:38 -0700 | [diff] [blame] | 344 | ASSERT_NE(content.find("memcpy"), std::string::npos); |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 345 | } |
| 346 | |
| Yabin Cui | e2f1078 | 2016-12-15 12:00:44 -0800 | [diff] [blame] | 347 | TEST_F(ReportCommandTest, report_dumped_symbols_with_symfs_dir) { |
| 348 | // Check if we can report symbols when they appear both in perf.data and symfs dir. |
| 349 | Report(PERF_DATA_WITH_SYMBOLS, {"--symfs", GetTestDataDir()}); |
| 350 | ASSERT_TRUE(success); |
| 351 | ASSERT_NE(content.find("main"), std::string::npos); |
| 352 | } |
| 353 | |
| Yabin Cui | 16f9310 | 2018-11-13 11:09:20 -0800 | [diff] [blame^] | 354 | TEST_F(ReportCommandTest, report_without_symfs_dir) { |
| 355 | TemporaryFile tmpfile; |
| 356 | ASSERT_TRUE(ReportCmd()->Run({"-i", GetTestData(PERF_DATA), "-o", tmpfile.path})); |
| 357 | } |
| 358 | |
| Yabin Cui | 9970a23 | 2016-06-29 12:18:11 -0700 | [diff] [blame] | 359 | TEST_F(ReportCommandTest, report_sort_vaddr_in_file) { |
| 360 | Report(PERF_DATA, {"--sort", "vaddr_in_file"}); |
| 361 | ASSERT_TRUE(success); |
| 362 | ASSERT_NE(content.find("VaddrInFile"), std::string::npos); |
| 363 | } |
| 364 | |
| Yabin Cui | eec606c | 2016-07-07 13:53:33 -0700 | [diff] [blame] | 365 | TEST_F(ReportCommandTest, check_build_id) { |
| 366 | Report(PERF_DATA_FOR_BUILD_ID_CHECK, |
| 367 | {"--symfs", GetTestData(CORRECT_SYMFS_FOR_BUILD_ID_CHECK)}); |
| 368 | ASSERT_TRUE(success); |
| 369 | ASSERT_NE(content.find("main"), std::string::npos); |
| 370 | ASSERT_EXIT( |
| 371 | { |
| 372 | Report(PERF_DATA_FOR_BUILD_ID_CHECK, |
| 373 | {"--symfs", GetTestData(WRONG_SYMFS_FOR_BUILD_ID_CHECK)}); |
| 374 | if (!success) { |
| 375 | exit(1); |
| 376 | } |
| 377 | if (content.find("main") != std::string::npos) { |
| 378 | exit(2); |
| 379 | } |
| 380 | exit(0); |
| 381 | }, |
| Yabin Cui | 40b70ff | 2018-04-09 14:06:08 -0700 | [diff] [blame] | 382 | testing::ExitedWithCode(0), "failed to read symbols from /elf_for_build_id_check"); |
| Yabin Cui | eec606c | 2016-07-07 13:53:33 -0700 | [diff] [blame] | 383 | } |
| 384 | |
| Yabin Cui | 15475e6 | 2016-07-14 13:26:19 -0700 | [diff] [blame] | 385 | TEST_F(ReportCommandTest, no_show_ip_option) { |
| 386 | Report(PERF_DATA); |
| 387 | ASSERT_TRUE(success); |
| 388 | ASSERT_EQ(content.find("unknown"), std::string::npos); |
| 389 | Report(PERF_DATA, {"--no-show-ip"}); |
| 390 | ASSERT_TRUE(success); |
| 391 | ASSERT_NE(content.find("unknown"), std::string::npos); |
| 392 | } |
| 393 | |
| Yabin Cui | dec43c1 | 2016-07-29 16:40:40 -0700 | [diff] [blame] | 394 | TEST_F(ReportCommandTest, no_symbol_table_warning) { |
| 395 | ASSERT_EXIT( |
| 396 | { |
| 397 | Report(PERF_DATA, |
| 398 | {"--symfs", GetTestData(SYMFS_FOR_NO_SYMBOL_TABLE_WARNING)}); |
| 399 | if (!success) { |
| 400 | exit(1); |
| 401 | } |
| 402 | if (content.find("GlobalFunc") != std::string::npos) { |
| 403 | exit(2); |
| 404 | } |
| 405 | exit(0); |
| 406 | }, |
| 407 | testing::ExitedWithCode(0), "elf doesn't contain symbol table"); |
| 408 | } |
| 409 | |
| 410 | TEST_F(ReportCommandTest, read_elf_file_warning) { |
| 411 | ASSERT_EXIT( |
| 412 | { |
| 413 | Report(PERF_DATA, |
| 414 | {"--symfs", GetTestData(SYMFS_FOR_READ_ELF_FILE_WARNING)}); |
| 415 | if (!success) { |
| 416 | exit(1); |
| 417 | } |
| 418 | if (content.find("GlobalFunc") != std::string::npos) { |
| 419 | exit(2); |
| 420 | } |
| 421 | exit(0); |
| 422 | }, |
| Yabin Cui | 40b70ff | 2018-04-09 14:06:08 -0700 | [diff] [blame] | 423 | testing::ExitedWithCode(0), "failed to read symbols from /elf: File not found"); |
| Yabin Cui | dec43c1 | 2016-07-29 16:40:40 -0700 | [diff] [blame] | 424 | } |
| 425 | |
| Yabin Cui | eafa718 | 2016-08-30 13:13:17 -0700 | [diff] [blame] | 426 | TEST_F(ReportCommandTest, report_data_generated_by_linux_perf) { |
| 427 | Report(PERF_DATA_GENERATED_BY_LINUX_PERF); |
| 428 | ASSERT_TRUE(success); |
| 429 | } |
| 430 | |
| Yabin Cui | c0565bb | 2016-09-29 15:59:33 -0700 | [diff] [blame] | 431 | TEST_F(ReportCommandTest, max_stack_and_percent_limit_option) { |
| 432 | Report(PERF_DATA_MAX_STACK_AND_PERCENT_LIMIT, {"-g"}); |
| 433 | ASSERT_TRUE(success); |
| 434 | ASSERT_NE(content.find("89.03"), std::string::npos); |
| 435 | |
| 436 | Report(PERF_DATA_MAX_STACK_AND_PERCENT_LIMIT, {"-g", "--max-stack", "0"}); |
| 437 | ASSERT_TRUE(success); |
| 438 | ASSERT_EQ(content.find("89.03"), std::string::npos); |
| Yabin Cui | dcb2a3e | 2017-05-18 17:11:32 -0700 | [diff] [blame] | 439 | Report(PERF_DATA_MAX_STACK_AND_PERCENT_LIMIT, {"-g", "--max-stack", "2"}); |
| Yabin Cui | c0565bb | 2016-09-29 15:59:33 -0700 | [diff] [blame] | 440 | ASSERT_TRUE(success); |
| 441 | ASSERT_NE(content.find("89.03"), std::string::npos); |
| 442 | |
| 443 | Report(PERF_DATA_MAX_STACK_AND_PERCENT_LIMIT, |
| 444 | {"-g", "--percent-limit", "90"}); |
| 445 | ASSERT_TRUE(success); |
| 446 | ASSERT_EQ(content.find("89.03"), std::string::npos); |
| 447 | Report(PERF_DATA_MAX_STACK_AND_PERCENT_LIMIT, |
| 448 | {"-g", "--percent-limit", "70"}); |
| 449 | ASSERT_TRUE(success); |
| 450 | ASSERT_NE(content.find("89.03"), std::string::npos); |
| 451 | } |
| 452 | |
| Yabin Cui | 18385c4 | 2016-12-09 14:51:04 -0800 | [diff] [blame] | 453 | TEST_F(ReportCommandTest, kallsyms_option) { |
| 454 | Report(PERF_DATA, {"--kallsyms", GetTestData("kallsyms")}); |
| 455 | ASSERT_TRUE(success); |
| 456 | ASSERT_NE(content.find("FakeKernelSymbol"), std::string::npos); |
| 457 | } |
| 458 | |
| Yabin Cui | 044861b | 2016-12-13 17:49:19 -0800 | [diff] [blame] | 459 | TEST_F(ReportCommandTest, invalid_perf_data) { |
| 460 | ASSERT_FALSE(ReportCmd()->Run({"-i", GetTestData(INVALID_PERF_DATA)})); |
| 461 | } |
| 462 | |
| Yabin Cui | afe99a5 | 2017-02-23 16:27:09 -0800 | [diff] [blame] | 463 | TEST_F(ReportCommandTest, raw_period_option) { |
| 464 | Report(PERF_DATA, {"--raw-period"}); |
| 465 | ASSERT_TRUE(success); |
| 466 | ASSERT_NE(content.find("GlobalFunc"), std::string::npos); |
| Chih-Hung Hsieh | f6d5b0d | 2017-08-03 14:04:06 -0700 | [diff] [blame] | 467 | ASSERT_EQ(content.find('%'), std::string::npos); |
| Yabin Cui | afe99a5 | 2017-02-23 16:27:09 -0800 | [diff] [blame] | 468 | } |
| 469 | |
| Yabin Cui | dcb2a3e | 2017-05-18 17:11:32 -0700 | [diff] [blame] | 470 | TEST_F(ReportCommandTest, full_callgraph_option) { |
| 471 | Report(CALLGRAPH_FP_PERF_DATA, {"-g"}); |
| Yabin Cui | 0c093f3 | 2017-04-19 11:48:44 -0700 | [diff] [blame] | 472 | ASSERT_TRUE(success); |
| 473 | ASSERT_NE(content.find("skipped in brief callgraph mode"), std::string::npos); |
| Yabin Cui | dcb2a3e | 2017-05-18 17:11:32 -0700 | [diff] [blame] | 474 | Report(CALLGRAPH_FP_PERF_DATA, {"-g", "--full-callgraph"}); |
| 475 | ASSERT_TRUE(success); |
| 476 | ASSERT_EQ(content.find("skipped in brief callgraph mode"), std::string::npos); |
| Yabin Cui | 0c093f3 | 2017-04-19 11:48:44 -0700 | [diff] [blame] | 477 | } |
| 478 | |
| Yabin Cui | 68b8383 | 2017-07-19 17:54:57 -0700 | [diff] [blame] | 479 | TEST_F(ReportCommandTest, report_offcpu_time) { |
| 480 | Report(PERF_DATA_WITH_TRACE_OFFCPU, {"--children"}); |
| 481 | ASSERT_TRUE(success); |
| 482 | ASSERT_NE(content.find("Time in ns"), std::string::npos); |
| 483 | bool found = false; |
| 484 | for (auto& line : lines) { |
| 485 | if (line.find("SleepFunction") != std::string::npos) { |
| Yabin Cui | 8b5a352 | 2018-06-18 17:44:27 -0700 | [diff] [blame] | 486 | ASSERT_NE(line.find("38.76%"), std::string::npos); |
| Yabin Cui | 68b8383 | 2017-07-19 17:54:57 -0700 | [diff] [blame] | 487 | found = true; |
| 488 | break; |
| 489 | } |
| 490 | } |
| 491 | ASSERT_TRUE(found); |
| 492 | } |
| 493 | |
| Yabin Cui | 8cd9233 | 2018-03-21 14:34:29 -0700 | [diff] [blame] | 494 | TEST_F(ReportCommandTest, report_big_trace_data) { |
| 495 | Report(PERF_DATA_WITH_BIG_TRACE_DATA); |
| 496 | ASSERT_TRUE(success); |
| 497 | } |
| 498 | |
| Yabin Cui | 19e6b6d | 2016-03-10 11:49:57 -0800 | [diff] [blame] | 499 | #if defined(__linux__) |
| Yabin Cui | 003b245 | 2016-09-29 15:32:45 -0700 | [diff] [blame] | 500 | #include "event_selection_set.h" |
| Yabin Cui | 6e51bef | 2016-02-23 21:41:03 -0800 | [diff] [blame] | 501 | |
| 502 | static std::unique_ptr<Command> RecordCmd() { |
| 503 | return CreateCommandInstance("record"); |
| Yabin Cui | 3c8c213 | 2015-08-13 20:30:20 -0700 | [diff] [blame] | 504 | } |
| Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 505 | |
| Yabin Cui | 6e51bef | 2016-02-23 21:41:03 -0800 | [diff] [blame] | 506 | TEST_F(ReportCommandTest, dwarf_callgraph) { |
| Yabin Cui | 8faa615 | 2018-06-07 15:52:57 -0700 | [diff] [blame] | 507 | TEST_REQUIRE_HW_COUNTER(); |
| Yabin Cui | 64a9ecd | 2017-09-13 13:21:32 -0700 | [diff] [blame] | 508 | OMIT_TEST_ON_NON_NATIVE_ABIS(); |
| Yabin Cui | d18c42e | 2017-07-12 14:50:07 -0700 | [diff] [blame] | 509 | ASSERT_TRUE(IsDwarfCallChainSamplingSupported()); |
| 510 | std::vector<std::unique_ptr<Workload>> workloads; |
| 511 | CreateProcesses(1, &workloads); |
| 512 | std::string pid = std::to_string(workloads[0]->GetPid()); |
| 513 | TemporaryFile tmp_file; |
| 514 | ASSERT_TRUE( |
| 515 | RecordCmd()->Run({"-p", pid, "-g", "-o", tmp_file.path, "sleep", SLEEP_SEC})); |
| 516 | ReportRaw(tmp_file.path, {"-g"}); |
| 517 | ASSERT_TRUE(success); |
| Yabin Cui | 6e51bef | 2016-02-23 21:41:03 -0800 | [diff] [blame] | 518 | } |
| 519 | |
| Yabin Cui | 8f680f6 | 2016-03-18 18:47:43 -0700 | [diff] [blame] | 520 | TEST_F(ReportCommandTest, report_dwarf_callgraph_of_nativelib_in_apk) { |
| Yabin Cui | 6e75e1b | 2018-02-06 13:42:16 -0800 | [diff] [blame] | 521 | Report(NATIVELIB_IN_APK_PERF_DATA, {"-g"}); |
| 522 | ASSERT_NE(content.find(GetUrlInApk(APK_FILE, NATIVELIB_IN_APK)), |
| 523 | std::string::npos); |
| 524 | ASSERT_NE(content.find("Func2"), std::string::npos); |
| 525 | ASSERT_NE(content.find("Func1"), std::string::npos); |
| 526 | ASSERT_NE(content.find("GlobalFunc"), std::string::npos); |
| Yabin Cui | 8f680f6 | 2016-03-18 18:47:43 -0700 | [diff] [blame] | 527 | } |
| 528 | |
| Yabin Cui | d3cb3b0 | 2017-07-24 14:59:46 -0700 | [diff] [blame] | 529 | TEST_F(ReportCommandTest, exclude_kernel_callchain) { |
| Yabin Cui | 8faa615 | 2018-06-07 15:52:57 -0700 | [diff] [blame] | 530 | TEST_REQUIRE_HW_COUNTER(); |
| Yabin Cui | d3cb3b0 | 2017-07-24 14:59:46 -0700 | [diff] [blame] | 531 | TEST_REQUIRE_HOST_ROOT(); |
| Yabin Cui | 64a9ecd | 2017-09-13 13:21:32 -0700 | [diff] [blame] | 532 | OMIT_TEST_ON_NON_NATIVE_ABIS(); |
| Yabin Cui | d3cb3b0 | 2017-07-24 14:59:46 -0700 | [diff] [blame] | 533 | std::vector<std::unique_ptr<Workload>> workloads; |
| 534 | CreateProcesses(1, &workloads); |
| 535 | std::string pid = std::to_string(workloads[0]->GetPid()); |
| 536 | TemporaryFile tmpfile; |
| 537 | ASSERT_TRUE(RecordCmd()->Run({"--trace-offcpu", "-e", "cpu-cycles:u", "-p", pid, |
| 538 | "--duration", "2", "-o", tmpfile.path, "-g"})); |
| 539 | ReportRaw(tmpfile.path, {"-g"}); |
| 540 | ASSERT_TRUE(success); |
| 541 | ASSERT_EQ(content.find("[kernel.kallsyms]"), std::string::npos); |
| 542 | } |
| 543 | |
| Yabin Cui | 6e51bef | 2016-02-23 21:41:03 -0800 | [diff] [blame] | 544 | #endif |