Than McIntosh | 7e2f4e9 | 2015-03-05 11:05:02 -0500 | [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 | #include <algorithm> |
| 19 | #include <cctype> |
| 20 | #include <string> |
| 21 | #include <regex> |
| 22 | #include <stdio.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <fcntl.h> |
| 26 | |
| 27 | #include "perfprofdcore.h" |
| 28 | #include "perfprofdutils.h" |
| 29 | #include "perfprofdmockutils.h" |
| 30 | |
| 31 | #include "perf_profile.pb.h" |
| 32 | #include "google/protobuf/text_format.h" |
| 33 | |
| 34 | // |
| 35 | // Set to argv[0] on startup |
| 36 | // |
| 37 | static const char *executable_path; |
| 38 | |
| 39 | // |
| 40 | // test_dir is the directory containing the test executable and |
| 41 | // any files associated with the test (will be created by the harness). |
| 42 | // |
| 43 | // dest_dir is a subdirectory of test_dir that we'll create on the fly |
| 44 | // at the start of each testpoint (into which new files can be written), |
| 45 | // then delete at end of testpoint. |
| 46 | // |
| 47 | static std::string test_dir; |
| 48 | static std::string dest_dir; |
| 49 | |
| 50 | // Path to perf executable on device |
| 51 | #define PERFPATH "/system/bin/perf" |
| 52 | |
| 53 | // Temporary config file that we will emit for the daemon to read |
| 54 | #define CONFIGFILE "perfprofd.conf" |
| 55 | |
| 56 | static std::string encoded_file_path() |
| 57 | { |
| 58 | std::string path(dest_dir); |
| 59 | path += "/perf.data.encoded"; |
| 60 | return path; |
| 61 | } |
| 62 | |
| 63 | class PerfProfdTest : public testing::Test { |
| 64 | protected: |
| 65 | virtual void SetUp() { |
| 66 | mock_perfprofdutils_init(); |
| 67 | create_dest_dir(); |
Than McIntosh | 07f00fd | 2015-04-17 15:10:43 -0400 | [diff] [blame] | 68 | yesclean(); |
Than McIntosh | 7e2f4e9 | 2015-03-05 11:05:02 -0500 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | virtual void TearDown() { |
| 72 | mock_perfprofdutils_finish(); |
| 73 | remove_dest_dir(); |
| 74 | } |
| 75 | |
Than McIntosh | 07f00fd | 2015-04-17 15:10:43 -0400 | [diff] [blame] | 76 | void noclean() { |
| 77 | clean_ = false; |
| 78 | } |
| 79 | void yesclean() { |
| 80 | clean_ = true; |
| 81 | } |
| 82 | |
Than McIntosh | 7e2f4e9 | 2015-03-05 11:05:02 -0500 | [diff] [blame] | 83 | private: |
Than McIntosh | 07f00fd | 2015-04-17 15:10:43 -0400 | [diff] [blame] | 84 | bool clean_; |
Than McIntosh | 7e2f4e9 | 2015-03-05 11:05:02 -0500 | [diff] [blame] | 85 | |
| 86 | void create_dest_dir() { |
| 87 | setup_dirs(); |
| 88 | ASSERT_FALSE(dest_dir == ""); |
Than McIntosh | 07f00fd | 2015-04-17 15:10:43 -0400 | [diff] [blame] | 89 | if (clean_) { |
| 90 | std::string cmd("rm -rf "); |
| 91 | cmd += dest_dir; |
| 92 | system(cmd.c_str()); |
| 93 | } |
Than McIntosh | 7e2f4e9 | 2015-03-05 11:05:02 -0500 | [diff] [blame] | 94 | std::string cmd("mkdir -p "); |
| 95 | cmd += dest_dir; |
| 96 | system(cmd.c_str()); |
| 97 | } |
| 98 | |
| 99 | void remove_dest_dir() { |
| 100 | setup_dirs(); |
| 101 | ASSERT_FALSE(dest_dir == ""); |
Than McIntosh | 7e2f4e9 | 2015-03-05 11:05:02 -0500 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | void setup_dirs() |
| 105 | { |
| 106 | if (test_dir == "") { |
| 107 | ASSERT_TRUE(executable_path != nullptr); |
| 108 | std::string s(executable_path); |
| 109 | auto found = s.find_last_of("/"); |
| 110 | test_dir = s.substr(0,found); |
| 111 | dest_dir = test_dir; |
| 112 | dest_dir += "/tmp"; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | }; |
| 117 | |
| 118 | static bool bothWhiteSpace(char lhs, char rhs) |
| 119 | { |
| 120 | return (std::isspace(lhs) && std::isspace(rhs)); |
| 121 | } |
| 122 | |
| 123 | // |
| 124 | // Squeeze out repeated whitespace from expected/actual logs. |
| 125 | // |
| 126 | static std::string squeezeWhite(const std::string &str, |
| 127 | const char *tag, |
| 128 | bool dump=false) |
| 129 | { |
| 130 | if (dump) { fprintf(stderr, "raw %s is %s\n", tag, str.c_str()); } |
| 131 | std::string result(str); |
| 132 | std::replace( result.begin(), result.end(), '\n', ' '); |
| 133 | auto new_end = std::unique(result.begin(), result.end(), bothWhiteSpace); |
| 134 | result.erase(new_end, result.end()); |
| 135 | while (result.begin() != result.end() && std::isspace(*result.rbegin())) { |
| 136 | result.pop_back(); |
| 137 | } |
| 138 | if (dump) { fprintf(stderr, "squeezed %s is %s\n", tag, result.c_str()); } |
| 139 | return result; |
| 140 | } |
| 141 | |
| 142 | /// |
| 143 | /// Helper class to kick off a run of the perfprofd daemon with a specific |
| 144 | /// config file. |
| 145 | /// |
| 146 | class PerfProfdRunner { |
| 147 | public: |
| 148 | PerfProfdRunner() |
| 149 | : config_path_(test_dir) |
| 150 | , aux_config_path_(dest_dir) |
| 151 | { |
| 152 | config_path_ += "/" CONFIGFILE; |
| 153 | aux_config_path_ += "/" CONFIGFILE; |
| 154 | } |
| 155 | |
| 156 | ~PerfProfdRunner() |
| 157 | { |
| 158 | } |
| 159 | |
| 160 | void addToConfig(const std::string &line) |
| 161 | { |
| 162 | config_text_ += line; |
| 163 | config_text_ += "\n"; |
| 164 | } |
| 165 | |
| 166 | void addToAuxConfig(const std::string &line) |
| 167 | { |
| 168 | aux_config_text_ += line; |
| 169 | aux_config_text_ += "\n"; |
| 170 | } |
| 171 | |
| 172 | void remove_semaphore_file() |
| 173 | { |
| 174 | std::string semaphore(dest_dir); |
| 175 | semaphore += "/" SEMAPHORE_FILENAME; |
| 176 | unlink(semaphore.c_str()); |
| 177 | } |
| 178 | |
| 179 | void create_semaphore_file() |
| 180 | { |
| 181 | std::string semaphore(dest_dir); |
| 182 | semaphore += "/" SEMAPHORE_FILENAME; |
| 183 | close(open(semaphore.c_str(), O_WRONLY|O_CREAT)); |
| 184 | } |
| 185 | |
| 186 | int invoke() |
| 187 | { |
| 188 | static const char *argv[3] = { "perfprofd", "-c", "" }; |
| 189 | argv[2] = config_path_.c_str(); |
| 190 | |
| 191 | writeConfigFile(config_path_, config_text_); |
| 192 | if (aux_config_text_.length()) { |
| 193 | writeConfigFile(aux_config_path_, aux_config_text_); |
| 194 | } |
| 195 | |
Than McIntosh | 07f00fd | 2015-04-17 15:10:43 -0400 | [diff] [blame] | 196 | |
Than McIntosh | 7e2f4e9 | 2015-03-05 11:05:02 -0500 | [diff] [blame] | 197 | // execute daemon main |
| 198 | return perfprofd_main(3, (char **) argv); |
| 199 | } |
| 200 | |
| 201 | private: |
| 202 | std::string config_path_; |
| 203 | std::string config_text_; |
| 204 | std::string aux_config_path_; |
| 205 | std::string aux_config_text_; |
| 206 | |
| 207 | void writeConfigFile(const std::string &config_path, |
| 208 | const std::string &config_text) |
| 209 | { |
| 210 | FILE *fp = fopen(config_path.c_str(), "w"); |
| 211 | ASSERT_TRUE(fp != nullptr); |
| 212 | fprintf(fp, "%s\n", config_text.c_str()); |
| 213 | fclose(fp); |
| 214 | } |
| 215 | }; |
| 216 | |
| 217 | //...................................................................... |
| 218 | |
| 219 | static void readEncodedProfile(const char *testpoint, |
| 220 | wireless_android_play_playlog::AndroidPerfProfile &encodedProfile, |
| 221 | bool debugDump=false) |
| 222 | { |
| 223 | struct stat statb; |
| 224 | int perf_data_stat_result = stat(encoded_file_path().c_str(), &statb); |
| 225 | ASSERT_NE(-1, perf_data_stat_result); |
| 226 | |
| 227 | // read |
| 228 | std::string encoded; |
| 229 | encoded.resize(statb.st_size); |
| 230 | FILE *ifp = fopen(encoded_file_path().c_str(), "r"); |
| 231 | ASSERT_NE(nullptr, ifp); |
| 232 | size_t items_read = fread((void*) encoded.data(), statb.st_size, 1, ifp); |
| 233 | ASSERT_EQ(1, items_read); |
| 234 | fclose(ifp); |
| 235 | |
| 236 | // decode |
| 237 | encodedProfile.ParseFromString(encoded); |
| 238 | |
| 239 | if (debugDump) { |
| 240 | std::string textdump; |
| 241 | ::google::protobuf::TextFormat::PrintToString(encodedProfile, &textdump); |
| 242 | std::string dfp(dest_dir); dfp += "/"; dfp += testpoint; dfp += ".dump_encoded.txt"; |
| 243 | FILE *ofp = fopen(dfp.c_str(), "w"); |
| 244 | if (ofp) { |
| 245 | fwrite(textdump.c_str(), textdump.size(), 1, ofp); |
| 246 | fclose(ofp); |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | #define RAW_RESULT(x) #x |
| 252 | |
| 253 | // |
| 254 | // Check to see if the log messages emitted by the daemon |
| 255 | // match the expected result. By default we use a partial |
| 256 | // match, e.g. if we see the expected excerpt anywhere in the |
| 257 | // result, it's a match (for exact match, set exact to true) |
| 258 | // |
| 259 | static void compareLogMessages(const std::string &actual, |
| 260 | const std::string &expected, |
| 261 | const char *testpoint, |
| 262 | bool exactMatch=false) |
| 263 | { |
| 264 | std::string sqexp = squeezeWhite(expected, "expected"); |
| 265 | std::string sqact = squeezeWhite(actual, "actual"); |
| 266 | if (exactMatch) { |
| 267 | EXPECT_STREQ(sqexp.c_str(), sqact.c_str()); |
| 268 | } else { |
| 269 | std::size_t foundpos = sqact.find(sqexp); |
| 270 | bool wasFound = true; |
| 271 | if (foundpos == std::string::npos) { |
| 272 | std::cerr << testpoint << ": expected result not found\n"; |
| 273 | std::cerr << " Actual: \"" << sqact << "\"\n"; |
| 274 | std::cerr << " Expected: \"" << sqexp << "\"\n"; |
| 275 | wasFound = false; |
| 276 | } |
| 277 | EXPECT_TRUE(wasFound); |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | TEST_F(PerfProfdTest, MissingGMS) |
| 282 | { |
| 283 | // |
| 284 | // AWP requires cooperation between the daemon and the GMS core |
| 285 | // piece. If we're running on a device that has an old or damaged |
| 286 | // version of GMS core, then the directory we're interested in may |
| 287 | // not be there. This test insures that the daemon does the right |
| 288 | // thing in this case. |
| 289 | // |
| 290 | PerfProfdRunner runner; |
| 291 | runner.addToConfig("only_debug_build=0"); |
| 292 | runner.addToConfig("trace_config_read=1"); |
| 293 | runner.addToConfig("destination_directory=/does/not/exist"); |
| 294 | runner.addToConfig("main_loop_iterations=1"); |
| 295 | runner.addToConfig("use_fixed_seed=1"); |
| 296 | runner.addToConfig("collection_interval=100"); |
| 297 | |
| 298 | // Kick off daemon |
| 299 | int daemon_main_return_code = runner.invoke(); |
| 300 | |
| 301 | // Check return code from daemon |
| 302 | EXPECT_EQ(0, daemon_main_return_code); |
| 303 | |
| 304 | // Verify log contents |
| 305 | const std::string expected = RAW_RESULT( |
| 306 | I: starting Android Wide Profiling daemon |
| 307 | I: config file path set to /data/nativetest/perfprofd_test/perfprofd.conf |
| 308 | I: option destination_directory set to /does/not/exist |
| 309 | I: option main_loop_iterations set to 1 |
| 310 | I: option use_fixed_seed set to 1 |
| 311 | I: option collection_interval set to 100 |
| 312 | I: random seed set to 1 |
| 313 | I: sleep 90 seconds |
| 314 | W: unable to open destination directory /does/not/exist: (No such file or directory) |
| 315 | I: profile collection skipped (missing destination directory) |
| 316 | I: sleep 10 seconds |
| 317 | I: finishing Android Wide Profiling daemon |
| 318 | );\ |
| 319 | |
| 320 | // check to make sure entire log matches |
| 321 | bool compareEntireLog = true; |
| 322 | compareLogMessages(mock_perfprofdutils_getlogged(), |
| 323 | expected, "MissingGMS", compareEntireLog); |
| 324 | } |
| 325 | |
| 326 | TEST_F(PerfProfdTest, MissingOptInSemaphoreFile) |
| 327 | { |
| 328 | // |
| 329 | // Android device owners must opt in to "collect and report usage |
| 330 | // data" in order for us to be able to collect profiles. The opt-in |
| 331 | // check is performed in the GMS core component; if the check |
| 332 | // passes, then it creates a semaphore file for the daemon to pick |
| 333 | // up on. |
| 334 | // |
| 335 | PerfProfdRunner runner; |
| 336 | runner.addToConfig("only_debug_build=0"); |
| 337 | std::string ddparam("destination_directory="); ddparam += dest_dir; |
| 338 | runner.addToConfig(ddparam); |
| 339 | runner.addToConfig("main_loop_iterations=1"); |
| 340 | runner.addToConfig("use_fixed_seed=1"); |
| 341 | runner.addToConfig("collection_interval=100"); |
| 342 | |
| 343 | runner.remove_semaphore_file(); |
| 344 | |
| 345 | // Kick off daemon |
| 346 | int daemon_main_return_code = runner.invoke(); |
| 347 | |
| 348 | // Check return code from daemon |
| 349 | EXPECT_EQ(0, daemon_main_return_code); |
| 350 | |
| 351 | // Verify log contents |
| 352 | const std::string expected = RAW_RESULT( |
| 353 | I: profile collection skipped (missing semaphore file) |
| 354 | ); |
| 355 | // check to make sure log excerpt matches |
| 356 | compareLogMessages(mock_perfprofdutils_getlogged(), |
| 357 | expected, "MissingOptInSemaphoreFile"); |
| 358 | } |
| 359 | |
| 360 | TEST_F(PerfProfdTest, MissingPerfExecutable) |
| 361 | { |
| 362 | // |
Than McIntosh | 07f00fd | 2015-04-17 15:10:43 -0400 | [diff] [blame] | 363 | // Perfprofd uses the 'simpleperf' tool to collect profiles |
| 364 | // (although this may conceivably change in the future). This test |
| 365 | // checks to make sure that if 'simpleperf' is not present we bail out |
| 366 | // from collecting profiles. |
Than McIntosh | 7e2f4e9 | 2015-03-05 11:05:02 -0500 | [diff] [blame] | 367 | // |
| 368 | PerfProfdRunner runner; |
| 369 | runner.addToConfig("only_debug_build=0"); |
| 370 | runner.addToConfig("trace_config_read=1"); |
| 371 | std::string ddparam("destination_directory="); ddparam += dest_dir; |
| 372 | runner.addToConfig(ddparam); |
| 373 | runner.addToConfig("main_loop_iterations=1"); |
| 374 | runner.addToConfig("use_fixed_seed=1"); |
| 375 | runner.addToConfig("collection_interval=100"); |
| 376 | runner.addToConfig("perf_path=/does/not/exist"); |
| 377 | |
| 378 | // Create semaphore file |
| 379 | runner.create_semaphore_file(); |
| 380 | |
| 381 | // Kick off daemon |
| 382 | int daemon_main_return_code = runner.invoke(); |
| 383 | |
| 384 | // Check return code from daemon |
| 385 | EXPECT_EQ(0, daemon_main_return_code); |
| 386 | |
| 387 | // expected log contents |
| 388 | const std::string expected = RAW_RESULT( |
| 389 | I: profile collection skipped (missing 'perf' executable) |
| 390 | ); |
| 391 | // check to make sure log excerpt matches |
| 392 | compareLogMessages(mock_perfprofdutils_getlogged(), |
| 393 | expected, "MissingPerfExecutable"); |
| 394 | } |
| 395 | |
| 396 | TEST_F(PerfProfdTest, BadPerfRun) |
| 397 | { |
| 398 | // |
Than McIntosh | 07f00fd | 2015-04-17 15:10:43 -0400 | [diff] [blame] | 399 | // Perf tools tend to be tightly coupled with a specific kernel |
| 400 | // version -- if things are out of sync perf could fail or |
| 401 | // crash. This test makes sure that we detect such a case and log |
| 402 | // the error. |
Than McIntosh | 7e2f4e9 | 2015-03-05 11:05:02 -0500 | [diff] [blame] | 403 | // |
| 404 | PerfProfdRunner runner; |
| 405 | runner.addToConfig("only_debug_build=0"); |
| 406 | std::string ddparam("destination_directory="); ddparam += dest_dir; |
| 407 | runner.addToConfig(ddparam); |
| 408 | runner.addToConfig("main_loop_iterations=1"); |
| 409 | runner.addToConfig("use_fixed_seed=1"); |
| 410 | runner.addToConfig("collection_interval=100"); |
| 411 | runner.addToConfig("perf_path=/system/bin/false"); |
| 412 | |
| 413 | // Create semaphore file |
| 414 | runner.create_semaphore_file(); |
| 415 | |
| 416 | // Kick off daemon |
| 417 | int daemon_main_return_code = runner.invoke(); |
| 418 | |
| 419 | // Check return code from daemon |
| 420 | EXPECT_EQ(0, daemon_main_return_code); |
| 421 | |
| 422 | // Verify log contents |
| 423 | const std::string expected = RAW_RESULT( |
| 424 | I: profile collection failed (perf record returned bad exit status) |
| 425 | ); |
| 426 | |
| 427 | // check to make sure log excerpt matches |
| 428 | compareLogMessages(mock_perfprofdutils_getlogged(), |
| 429 | expected, "BadPerfRun"); |
| 430 | } |
| 431 | |
| 432 | TEST_F(PerfProfdTest, ConfigFileParsing) |
| 433 | { |
| 434 | // |
| 435 | // Gracefully handly malformed items in the config file |
| 436 | // |
| 437 | PerfProfdRunner runner; |
| 438 | runner.addToConfig("only_debug_build=0"); |
| 439 | runner.addToConfig("main_loop_iterations=1"); |
| 440 | runner.addToConfig("collection_interval=100"); |
| 441 | runner.addToConfig("use_fixed_seed=1"); |
| 442 | runner.addToConfig("destination_directory=/does/not/exist"); |
| 443 | |
| 444 | // assorted bad syntax |
| 445 | runner.addToConfig("collection_interval=0"); |
| 446 | runner.addToConfig("collection_interval=-1"); |
| 447 | runner.addToConfig("collection_interval=2"); |
| 448 | runner.addToConfig("nonexistent_key=something"); |
| 449 | runner.addToConfig("no_equals_stmt"); |
| 450 | |
| 451 | // Kick off daemon |
| 452 | int daemon_main_return_code = runner.invoke(); |
| 453 | |
| 454 | // Check return code from daemon |
| 455 | EXPECT_EQ(0, daemon_main_return_code); |
| 456 | |
| 457 | // Verify log contents |
| 458 | const std::string expected = RAW_RESULT( |
| 459 | W: line 6: specified value 0 for 'collection_interval' outside permitted range [100 4294967295] (ignored) |
| 460 | W: line 7: malformed unsigned value (ignored) |
| 461 | W: line 8: specified value 2 for 'collection_interval' outside permitted range [100 4294967295] (ignored) |
| 462 | W: line 9: unknown option 'nonexistent_key' ignored |
| 463 | W: line 10: line malformed (no '=' found) |
| 464 | ); |
| 465 | |
| 466 | // check to make sure log excerpt matches |
| 467 | compareLogMessages(mock_perfprofdutils_getlogged(), |
| 468 | expected, "ConfigFileParsing"); |
| 469 | } |
| 470 | |
| 471 | TEST_F(PerfProfdTest, AuxiliaryConfigFile) |
| 472 | { |
| 473 | // |
| 474 | // We want to be able to tweak profile collection parameters (sample |
| 475 | // duration, etc) using changes to gservices. To carry this out, the |
| 476 | // GMS core upload service writes out an perfprofd.conf config file when |
| 477 | // it starts up. This test verifies that we can read this file. |
| 478 | // |
| 479 | |
| 480 | // Minimal settings in main config file |
| 481 | PerfProfdRunner runner; |
| 482 | runner.addToConfig("only_debug_build=0"); |
| 483 | runner.addToConfig("trace_config_read=1"); |
| 484 | runner.addToConfig("use_fixed_seed=1"); |
| 485 | std::string ddparam("destination_directory="); ddparam += dest_dir; |
| 486 | runner.addToConfig(ddparam); |
| 487 | |
| 488 | // Remaining settings in aux config file |
| 489 | runner.addToAuxConfig("main_loop_iterations=1"); |
| 490 | runner.addToAuxConfig("collection_interval=100"); |
| 491 | runner.addToAuxConfig("perf_path=/system/bin/true"); |
| 492 | runner.addToAuxConfig("stack_profile=1"); |
| 493 | runner.addToAuxConfig("sampling_period=9999"); |
| 494 | runner.addToAuxConfig("sample_duration=333"); |
| 495 | |
| 496 | runner.remove_semaphore_file(); |
| 497 | |
| 498 | // Kick off daemon |
| 499 | int daemon_main_return_code = runner.invoke(); |
| 500 | |
| 501 | // Check return code from daemon |
| 502 | EXPECT_EQ(0, daemon_main_return_code); |
| 503 | |
| 504 | // Verify log contents |
| 505 | const std::string expected = RAW_RESULT( |
| 506 | I: reading auxiliary config file /data/nativetest/perfprofd_test/tmp/perfprofd.conf |
| 507 | I: option main_loop_iterations set to 1 |
| 508 | I: option collection_interval set to 100 |
| 509 | I: option perf_path set to /system/bin/true |
| 510 | I: option stack_profile set to 1 |
| 511 | I: option sampling_period set to 9999 |
| 512 | I: option sample_duration set to 333 |
| 513 | I: sleep 90 seconds |
| 514 | I: reading auxiliary config file /data/nativetest/perfprofd_test/tmp/perfprofd.conf |
| 515 | I: option main_loop_iterations set to 1 |
| 516 | ); |
| 517 | |
| 518 | // check to make sure log excerpt matches |
| 519 | compareLogMessages(mock_perfprofdutils_getlogged(), |
| 520 | expected, "AuxiliaryConfigFile"); |
| 521 | } |
| 522 | |
| 523 | TEST_F(PerfProfdTest, BasicRunWithCannedPerf) |
| 524 | { |
| 525 | // |
| 526 | // Verify the portion of the daemon that reads and encodes |
| 527 | // perf.data files. Here we run the encoder on a canned perf.data |
| 528 | // file and verify that the resulting protobuf contains what |
| 529 | // we think it should contain. |
| 530 | // |
| 531 | std::string input_perf_data(test_dir); |
| 532 | input_perf_data += "/canned.perf.data"; |
| 533 | |
| 534 | // Kick off encoder and check return code |
| 535 | PROFILE_RESULT result = |
| 536 | encode_to_proto(input_perf_data, encoded_file_path()); |
| 537 | EXPECT_EQ(OK_PROFILE_COLLECTION, result); |
| 538 | |
| 539 | // Read and decode the resulting perf.data.encoded file |
| 540 | wireless_android_play_playlog::AndroidPerfProfile encodedProfile; |
| 541 | readEncodedProfile("BasicRunWithCannedPerf", |
| 542 | encodedProfile); |
| 543 | |
| 544 | // Expect 29 load modules |
| 545 | EXPECT_EQ(29, encodedProfile.programs_size()); |
| 546 | |
| 547 | // Check a couple of load modules |
| 548 | { const auto &lm0 = encodedProfile.load_modules(0); |
| 549 | std::string act_lm0; |
| 550 | ::google::protobuf::TextFormat::PrintToString(lm0, &act_lm0); |
| 551 | std::string sqact0 = squeezeWhite(act_lm0, "actual for lm 0"); |
| 552 | const std::string expected_lm0 = RAW_RESULT( |
| 553 | name: "/data/app/com.google.android.apps.plus-1/lib/arm/libcronet.so" |
| 554 | ); |
| 555 | std::string sqexp0 = squeezeWhite(expected_lm0, "expected_lm0"); |
| 556 | EXPECT_STREQ(sqexp0.c_str(), sqact0.c_str()); |
| 557 | } |
| 558 | { const auto &lm9 = encodedProfile.load_modules(9); |
| 559 | std::string act_lm9; |
| 560 | ::google::protobuf::TextFormat::PrintToString(lm9, &act_lm9); |
| 561 | std::string sqact9 = squeezeWhite(act_lm9, "actual for lm 9"); |
| 562 | const std::string expected_lm9 = RAW_RESULT( |
| 563 | name: "/system/lib/libandroid_runtime.so" build_id: "8164ed7b3a8b8f5a220d027788922510" |
| 564 | ); |
| 565 | std::string sqexp9 = squeezeWhite(expected_lm9, "expected_lm9"); |
| 566 | EXPECT_STREQ(sqexp9.c_str(), sqact9.c_str()); |
| 567 | } |
| 568 | |
| 569 | // Examine some of the samples now |
| 570 | { const auto &p1 = encodedProfile.programs(0); |
| 571 | const auto &lm1 = p1.modules(0); |
| 572 | std::string act_lm1; |
| 573 | ::google::protobuf::TextFormat::PrintToString(lm1, &act_lm1); |
| 574 | std::string sqact1 = squeezeWhite(act_lm1, "actual for lm1"); |
| 575 | const std::string expected_lm1 = RAW_RESULT( |
| 576 | load_module_id: 9 address_samples { address: 296100 count: 1 } |
| 577 | ); |
| 578 | std::string sqexp1 = squeezeWhite(expected_lm1, "expected_lm1"); |
| 579 | EXPECT_STREQ(sqexp1.c_str(), sqact1.c_str()); |
| 580 | } |
| 581 | { const auto &p1 = encodedProfile.programs(2); |
| 582 | const auto &lm2 = p1.modules(0); |
| 583 | std::string act_lm2; |
| 584 | ::google::protobuf::TextFormat::PrintToString(lm2, &act_lm2); |
| 585 | std::string sqact2 = squeezeWhite(act_lm2, "actual for lm2"); |
| 586 | const std::string expected_lm2 = RAW_RESULT( |
| 587 | load_module_id: 2 |
| 588 | address_samples { address: 28030244 count: 1 } |
| 589 | address_samples { address: 29657840 count: 1 } |
| 590 | ); |
| 591 | std::string sqexp2 = squeezeWhite(expected_lm2, "expected_lm2"); |
| 592 | EXPECT_STREQ(sqexp2.c_str(), sqact2.c_str()); |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | TEST_F(PerfProfdTest, BasicRunWithLivePerf) |
| 597 | { |
| 598 | // |
| 599 | // Basic test to exercise the main loop of the daemon. It includes |
| 600 | // a live 'perf' run |
| 601 | // |
| 602 | PerfProfdRunner runner; |
| 603 | runner.addToConfig("only_debug_build=0"); |
| 604 | std::string ddparam("destination_directory="); ddparam += dest_dir; |
| 605 | runner.addToConfig(ddparam); |
| 606 | runner.addToConfig("main_loop_iterations=1"); |
| 607 | runner.addToConfig("use_fixed_seed=12345678"); |
| 608 | runner.addToConfig("collection_interval=9999"); |
Than McIntosh | 7e2f4e9 | 2015-03-05 11:05:02 -0500 | [diff] [blame] | 609 | runner.addToConfig("sample_duration=5"); |
| 610 | |
| 611 | // Create semaphore file |
| 612 | runner.create_semaphore_file(); |
| 613 | |
| 614 | // Kick off daemon |
| 615 | int daemon_main_return_code = runner.invoke(); |
| 616 | |
| 617 | // Check return code from daemon |
| 618 | EXPECT_EQ(0, daemon_main_return_code); |
| 619 | |
| 620 | // Read and decode the resulting perf.data.encoded file |
| 621 | wireless_android_play_playlog::AndroidPerfProfile encodedProfile; |
| 622 | readEncodedProfile("BasicRunWithLivePerf", encodedProfile); |
| 623 | |
| 624 | // Examine what we get back. Since it's a live profile, we can't |
| 625 | // really do much in terms of verifying the contents. |
| 626 | EXPECT_LT(0, encodedProfile.programs_size()); |
| 627 | |
| 628 | // Verify log contents |
| 629 | const std::string expected = RAW_RESULT( |
| 630 | I: starting Android Wide Profiling daemon |
| 631 | I: config file path set to /data/nativetest/perfprofd_test/perfprofd.conf |
| 632 | I: random seed set to 12345678 |
| 633 | I: sleep 674 seconds |
| 634 | I: initiating profile collection |
| 635 | I: profile collection complete |
| 636 | I: sleep 9325 seconds |
| 637 | I: finishing Android Wide Profiling daemon |
| 638 | ); |
| 639 | // check to make sure log excerpt matches |
| 640 | compareLogMessages(mock_perfprofdutils_getlogged(), |
| 641 | expected, "BasicRunWithLivePerf", true); |
| 642 | } |
| 643 | |
| 644 | int main(int argc, char **argv) { |
| 645 | executable_path = argv[0]; |
| 646 | // switch to / before starting testing (perfprofd |
| 647 | // should be location-independent) |
| 648 | chdir("/"); |
| 649 | testing::InitGoogleTest(&argc, argv); |
| 650 | return RUN_ALL_TESTS(); |
| 651 | } |