2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <gtest/gtest.h>
23 #include <sys/types.h>
27 #include "perfprofdcore.h"
28 #include "perfprofdutils.h"
29 #include "perfprofdmockutils.h"
31 #include "perf_profile.pb.h"
32 #include "google/protobuf/text_format.h"
35 // Set to argv[0] on startup
37 static const char *executable_path;
40 // test_dir is the directory containing the test executable and
41 // any files associated with the test (will be created by the harness).
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.
47 static std::string test_dir;
48 static std::string dest_dir;
50 // Path to perf executable on device
51 #define PERFPATH "/system/bin/perf"
53 // Temporary config file that we will emit for the daemon to read
54 #define CONFIGFILE "perfprofd.conf"
56 static std::string encoded_file_path()
58 std::string path(dest_dir);
59 path += "/perf.data.encoded";
63 class PerfProfdTest : public testing::Test {
65 virtual void SetUp() {
66 mock_perfprofdutils_init();
71 virtual void TearDown() {
72 mock_perfprofdutils_finish();
86 void create_dest_dir() {
88 ASSERT_FALSE(dest_dir == "");
90 std::string cmd("rm -rf ");
94 std::string cmd("mkdir -p ");
99 void remove_dest_dir() {
101 ASSERT_FALSE(dest_dir == "");
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);
118 static bool bothWhiteSpace(char lhs, char rhs)
120 return (std::isspace(lhs) && std::isspace(rhs));
124 // Squeeze out repeated whitespace from expected/actual logs.
126 static std::string squeezeWhite(const std::string &str,
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())) {
138 if (dump) { fprintf(stderr, "squeezed %s is %s\n", tag, result.c_str()); }
143 /// Helper class to kick off a run of the perfprofd daemon with a specific
146 class PerfProfdRunner {
149 : config_path_(test_dir)
150 , aux_config_path_(dest_dir)
152 config_path_ += "/" CONFIGFILE;
153 aux_config_path_ += "/" CONFIGFILE;
160 void addToConfig(const std::string &line)
162 config_text_ += line;
163 config_text_ += "\n";
166 void addToAuxConfig(const std::string &line)
168 aux_config_text_ += line;
169 aux_config_text_ += "\n";
172 void remove_semaphore_file()
174 std::string semaphore(dest_dir);
175 semaphore += "/" SEMAPHORE_FILENAME;
176 unlink(semaphore.c_str());
179 void create_semaphore_file()
181 std::string semaphore(dest_dir);
182 semaphore += "/" SEMAPHORE_FILENAME;
183 close(open(semaphore.c_str(), O_WRONLY|O_CREAT));
188 static const char *argv[3] = { "perfprofd", "-c", "" };
189 argv[2] = config_path_.c_str();
191 writeConfigFile(config_path_, config_text_);
192 if (aux_config_text_.length()) {
193 writeConfigFile(aux_config_path_, aux_config_text_);
197 // execute daemon main
198 return perfprofd_main(3, (char **) argv);
202 std::string config_path_;
203 std::string config_text_;
204 std::string aux_config_path_;
205 std::string aux_config_text_;
207 void writeConfigFile(const std::string &config_path,
208 const std::string &config_text)
210 FILE *fp = fopen(config_path.c_str(), "w");
211 ASSERT_TRUE(fp != nullptr);
212 fprintf(fp, "%s\n", config_text.c_str());
217 //......................................................................
219 static void readEncodedProfile(const char *testpoint,
220 wireless_android_play_playlog::AndroidPerfProfile &encodedProfile)
223 int perf_data_stat_result = stat(encoded_file_path().c_str(), &statb);
224 ASSERT_NE(-1, perf_data_stat_result);
228 encoded.resize(statb.st_size);
229 FILE *ifp = fopen(encoded_file_path().c_str(), "r");
230 ASSERT_NE(nullptr, ifp);
231 size_t items_read = fread((void*) encoded.data(), statb.st_size, 1, ifp);
232 ASSERT_EQ(1, items_read);
236 encodedProfile.ParseFromString(encoded);
239 static std::string encodedLoadModuleToString(const wireless_android_play_playlog::LoadModule &lm)
241 std::stringstream ss;
242 ss << "name: \"" << lm.name() << "\"\n";
243 if (lm.build_id() != "") {
244 ss << "build_id: \"" << lm.build_id() << "\"\n";
249 static std::string encodedModuleSamplesToString(const wireless_android_play_playlog::LoadModuleSamples &mod)
251 std::stringstream ss;
253 ss << "load_module_id: " << mod.load_module_id() << "\n";
254 for (size_t k = 0; k < mod.address_samples_size(); k++) {
255 const auto &sample = mod.address_samples(k);
256 ss << " address_samples {\n";
257 for (size_t l = 0; l < mod.address_samples(k).address_size();
259 auto address = mod.address_samples(k).address(l);
260 ss << " address: " << address << "\n";
262 ss << " count: " << sample.count() << "\n";
268 #define RAW_RESULT(x) #x
271 // Check to see if the log messages emitted by the daemon
272 // match the expected result. By default we use a partial
273 // match, e.g. if we see the expected excerpt anywhere in the
274 // result, it's a match (for exact match, set exact to true)
276 static void compareLogMessages(const std::string &actual,
277 const std::string &expected,
278 const char *testpoint,
279 bool exactMatch=false)
281 std::string sqexp = squeezeWhite(expected, "expected");
282 std::string sqact = squeezeWhite(actual, "actual");
284 EXPECT_STREQ(sqexp.c_str(), sqact.c_str());
286 std::size_t foundpos = sqact.find(sqexp);
287 bool wasFound = true;
288 if (foundpos == std::string::npos) {
289 std::cerr << testpoint << ": expected result not found\n";
290 std::cerr << " Actual: \"" << sqact << "\"\n";
291 std::cerr << " Expected: \"" << sqexp << "\"\n";
294 EXPECT_TRUE(wasFound);
298 TEST_F(PerfProfdTest, MissingGMS)
301 // AWP requires cooperation between the daemon and the GMS core
302 // piece. If we're running on a device that has an old or damaged
303 // version of GMS core, then the directory we're interested in may
304 // not be there. This test insures that the daemon does the right
305 // thing in this case.
307 PerfProfdRunner runner;
308 runner.addToConfig("only_debug_build=0");
309 runner.addToConfig("trace_config_read=1");
310 runner.addToConfig("destination_directory=/does/not/exist");
311 runner.addToConfig("main_loop_iterations=1");
312 runner.addToConfig("use_fixed_seed=1");
313 runner.addToConfig("collection_interval=100");
316 int daemon_main_return_code = runner.invoke();
318 // Check return code from daemon
319 EXPECT_EQ(0, daemon_main_return_code);
321 // Verify log contents
322 const std::string expected = RAW_RESULT(
323 I: starting Android Wide Profiling daemon
324 I: config file path set to /data/nativetest/perfprofd_test/perfprofd.conf
325 I: option destination_directory set to /does/not/exist
326 I: option main_loop_iterations set to 1
327 I: option use_fixed_seed set to 1
328 I: option collection_interval set to 100
329 I: random seed set to 1
331 W: unable to open destination directory /does/not/exist: (No such file or directory)
332 I: profile collection skipped (missing destination directory)
334 I: finishing Android Wide Profiling daemon
337 // check to make sure entire log matches
338 bool compareEntireLog = true;
339 compareLogMessages(mock_perfprofdutils_getlogged(),
340 expected, "MissingGMS", compareEntireLog);
343 TEST_F(PerfProfdTest, MissingOptInSemaphoreFile)
346 // Android device owners must opt in to "collect and report usage
347 // data" in order for us to be able to collect profiles. The opt-in
348 // check is performed in the GMS core component; if the check
349 // passes, then it creates a semaphore file for the daemon to pick
352 PerfProfdRunner runner;
353 runner.addToConfig("only_debug_build=0");
354 std::string ddparam("destination_directory="); ddparam += dest_dir;
355 runner.addToConfig(ddparam);
356 runner.addToConfig("main_loop_iterations=1");
357 runner.addToConfig("use_fixed_seed=1");
358 runner.addToConfig("collection_interval=100");
360 runner.remove_semaphore_file();
363 int daemon_main_return_code = runner.invoke();
365 // Check return code from daemon
366 EXPECT_EQ(0, daemon_main_return_code);
368 // Verify log contents
369 const std::string expected = RAW_RESULT(
370 I: profile collection skipped (missing semaphore file)
372 // check to make sure log excerpt matches
373 compareLogMessages(mock_perfprofdutils_getlogged(),
374 expected, "MissingOptInSemaphoreFile");
377 TEST_F(PerfProfdTest, MissingPerfExecutable)
380 // Perfprofd uses the 'simpleperf' tool to collect profiles
381 // (although this may conceivably change in the future). This test
382 // checks to make sure that if 'simpleperf' is not present we bail out
383 // from collecting profiles.
385 PerfProfdRunner runner;
386 runner.addToConfig("only_debug_build=0");
387 runner.addToConfig("trace_config_read=1");
388 std::string ddparam("destination_directory="); ddparam += dest_dir;
389 runner.addToConfig(ddparam);
390 runner.addToConfig("main_loop_iterations=1");
391 runner.addToConfig("use_fixed_seed=1");
392 runner.addToConfig("collection_interval=100");
393 runner.addToConfig("perf_path=/does/not/exist");
395 // Create semaphore file
396 runner.create_semaphore_file();
399 int daemon_main_return_code = runner.invoke();
401 // Check return code from daemon
402 EXPECT_EQ(0, daemon_main_return_code);
404 // expected log contents
405 const std::string expected = RAW_RESULT(
406 I: profile collection skipped (missing 'perf' executable)
408 // check to make sure log excerpt matches
409 compareLogMessages(mock_perfprofdutils_getlogged(),
410 expected, "MissingPerfExecutable");
413 TEST_F(PerfProfdTest, BadPerfRun)
416 // Perf tools tend to be tightly coupled with a specific kernel
417 // version -- if things are out of sync perf could fail or
418 // crash. This test makes sure that we detect such a case and log
421 PerfProfdRunner runner;
422 runner.addToConfig("only_debug_build=0");
423 std::string ddparam("destination_directory="); ddparam += dest_dir;
424 runner.addToConfig(ddparam);
425 runner.addToConfig("main_loop_iterations=1");
426 runner.addToConfig("use_fixed_seed=1");
427 runner.addToConfig("collection_interval=100");
428 runner.addToConfig("perf_path=/system/bin/false");
430 // Create semaphore file
431 runner.create_semaphore_file();
434 int daemon_main_return_code = runner.invoke();
436 // Check return code from daemon
437 EXPECT_EQ(0, daemon_main_return_code);
439 // Verify log contents
440 const std::string expected = RAW_RESULT(
441 I: profile collection failed (perf record returned bad exit status)
444 // check to make sure log excerpt matches
445 compareLogMessages(mock_perfprofdutils_getlogged(),
446 expected, "BadPerfRun");
449 TEST_F(PerfProfdTest, ConfigFileParsing)
452 // Gracefully handly malformed items in the config file
454 PerfProfdRunner runner;
455 runner.addToConfig("only_debug_build=0");
456 runner.addToConfig("main_loop_iterations=1");
457 runner.addToConfig("collection_interval=100");
458 runner.addToConfig("use_fixed_seed=1");
459 runner.addToConfig("destination_directory=/does/not/exist");
461 // assorted bad syntax
462 runner.addToConfig("collection_interval=0");
463 runner.addToConfig("collection_interval=-1");
464 runner.addToConfig("collection_interval=2");
465 runner.addToConfig("nonexistent_key=something");
466 runner.addToConfig("no_equals_stmt");
469 int daemon_main_return_code = runner.invoke();
471 // Check return code from daemon
472 EXPECT_EQ(0, daemon_main_return_code);
474 // Verify log contents
475 const std::string expected = RAW_RESULT(
476 W: line 6: specified value 0 for 'collection_interval' outside permitted range [100 4294967295] (ignored)
477 W: line 7: malformed unsigned value (ignored)
478 W: line 8: specified value 2 for 'collection_interval' outside permitted range [100 4294967295] (ignored)
479 W: line 9: unknown option 'nonexistent_key' ignored
480 W: line 10: line malformed (no '=' found)
483 // check to make sure log excerpt matches
484 compareLogMessages(mock_perfprofdutils_getlogged(),
485 expected, "ConfigFileParsing");
488 TEST_F(PerfProfdTest, AuxiliaryConfigFile)
491 // We want to be able to tweak profile collection parameters (sample
492 // duration, etc) using changes to gservices. To carry this out, the
493 // GMS core upload service writes out an perfprofd.conf config file when
494 // it starts up. This test verifies that we can read this file.
497 // Minimal settings in main config file
498 PerfProfdRunner runner;
499 runner.addToConfig("only_debug_build=0");
500 runner.addToConfig("trace_config_read=1");
501 runner.addToConfig("use_fixed_seed=1");
502 std::string ddparam("destination_directory="); ddparam += dest_dir;
503 runner.addToConfig(ddparam);
505 // Remaining settings in aux config file
506 runner.addToAuxConfig("main_loop_iterations=1");
507 runner.addToAuxConfig("collection_interval=100");
508 runner.addToAuxConfig("perf_path=/system/bin/true");
509 runner.addToAuxConfig("stack_profile=1");
510 runner.addToAuxConfig("sampling_period=9999");
511 runner.addToAuxConfig("sample_duration=333");
513 runner.remove_semaphore_file();
516 int daemon_main_return_code = runner.invoke();
518 // Check return code from daemon
519 EXPECT_EQ(0, daemon_main_return_code);
521 // Verify log contents
522 const std::string expected = RAW_RESULT(
523 I: reading auxiliary config file /data/nativetest/perfprofd_test/tmp/perfprofd.conf
524 I: option main_loop_iterations set to 1
525 I: option collection_interval set to 100
526 I: option perf_path set to /system/bin/true
527 I: option stack_profile set to 1
528 I: option sampling_period set to 9999
529 I: option sample_duration set to 333
531 I: reading auxiliary config file /data/nativetest/perfprofd_test/tmp/perfprofd.conf
532 I: option main_loop_iterations set to 1
535 // check to make sure log excerpt matches
536 compareLogMessages(mock_perfprofdutils_getlogged(),
537 expected, "AuxiliaryConfigFile");
540 TEST_F(PerfProfdTest, BasicRunWithCannedPerf)
543 // Verify the portion of the daemon that reads and encodes
544 // perf.data files. Here we run the encoder on a canned perf.data
545 // file and verify that the resulting protobuf contains what
546 // we think it should contain.
548 std::string input_perf_data(test_dir);
549 input_perf_data += "/canned.perf.data";
551 // Kick off encoder and check return code
552 PROFILE_RESULT result =
553 encode_to_proto(input_perf_data, encoded_file_path());
554 EXPECT_EQ(OK_PROFILE_COLLECTION, result);
556 // Read and decode the resulting perf.data.encoded file
557 wireless_android_play_playlog::AndroidPerfProfile encodedProfile;
558 readEncodedProfile("BasicRunWithCannedPerf",
561 // Expect 29 load modules
562 EXPECT_EQ(29, encodedProfile.programs_size());
564 // Check a couple of load modules
565 { const auto &lm0 = encodedProfile.load_modules(0);
566 std::string act_lm0 = encodedLoadModuleToString(lm0);
567 std::string sqact0 = squeezeWhite(act_lm0, "actual for lm 0");
568 const std::string expected_lm0 = RAW_RESULT(
569 name: "/data/app/com.google.android.apps.plus-1/lib/arm/libcronet.so"
571 std::string sqexp0 = squeezeWhite(expected_lm0, "expected_lm0");
572 EXPECT_STREQ(sqexp0.c_str(), sqact0.c_str());
574 { const auto &lm9 = encodedProfile.load_modules(9);
575 std::string act_lm9 = encodedLoadModuleToString(lm9);
576 std::string sqact9 = squeezeWhite(act_lm9, "actual for lm 9");
577 const std::string expected_lm9 = RAW_RESULT(
578 name: "/system/lib/libandroid_runtime.so" build_id: "8164ed7b3a8b8f5a220d027788922510"
580 std::string sqexp9 = squeezeWhite(expected_lm9, "expected_lm9");
581 EXPECT_STREQ(sqexp9.c_str(), sqact9.c_str());
584 // Examine some of the samples now
585 { const auto &p1 = encodedProfile.programs(0);
586 const auto &lm1 = p1.modules(0);
587 std::string act_lm1 = encodedModuleSamplesToString(lm1);
588 std::string sqact1 = squeezeWhite(act_lm1, "actual for lm1");
589 const std::string expected_lm1 = RAW_RESULT(
590 load_module_id: 9 address_samples { address: 296100 count: 1 }
592 std::string sqexp1 = squeezeWhite(expected_lm1, "expected_lm1");
593 EXPECT_STREQ(sqexp1.c_str(), sqact1.c_str());
595 { const auto &p1 = encodedProfile.programs(2);
596 const auto &lm2 = p1.modules(0);
597 std::string act_lm2 = encodedModuleSamplesToString(lm2);
598 std::string sqact2 = squeezeWhite(act_lm2, "actual for lm2");
599 const std::string expected_lm2 = RAW_RESULT(
601 address_samples { address: 28030244 count: 1 }
602 address_samples { address: 29657840 count: 1 }
604 std::string sqexp2 = squeezeWhite(expected_lm2, "expected_lm2");
605 EXPECT_STREQ(sqexp2.c_str(), sqact2.c_str());
609 TEST_F(PerfProfdTest, BasicRunWithLivePerf)
612 // Basic test to exercise the main loop of the daemon. It includes
615 PerfProfdRunner runner;
616 runner.addToConfig("only_debug_build=0");
617 std::string ddparam("destination_directory="); ddparam += dest_dir;
618 runner.addToConfig(ddparam);
619 runner.addToConfig("main_loop_iterations=1");
620 runner.addToConfig("use_fixed_seed=12345678");
621 runner.addToConfig("collection_interval=9999");
622 runner.addToConfig("sample_duration=5");
624 // Create semaphore file
625 runner.create_semaphore_file();
628 int daemon_main_return_code = runner.invoke();
630 // Check return code from daemon
631 EXPECT_EQ(0, daemon_main_return_code);
633 // Read and decode the resulting perf.data.encoded file
634 wireless_android_play_playlog::AndroidPerfProfile encodedProfile;
635 readEncodedProfile("BasicRunWithLivePerf", encodedProfile);
637 // Examine what we get back. Since it's a live profile, we can't
638 // really do much in terms of verifying the contents.
639 EXPECT_LT(0, encodedProfile.programs_size());
641 // Verify log contents
642 const std::string expected = RAW_RESULT(
643 I: starting Android Wide Profiling daemon
644 I: config file path set to /data/nativetest/perfprofd_test/perfprofd.conf
645 I: random seed set to 12345678
647 I: initiating profile collection
648 I: profile collection complete
649 I: sleep 9325 seconds
650 I: finishing Android Wide Profiling daemon
652 // check to make sure log excerpt matches
653 compareLogMessages(mock_perfprofdutils_getlogged(),
654 expected, "BasicRunWithLivePerf", true);
657 int main(int argc, char **argv) {
658 executable_path = argv[0];
659 // switch to / before starting testing (perfprofd
660 // should be location-independent)
662 testing::InitGoogleTest(&argc, argv);
663 return RUN_ALL_TESTS();