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 <android-base/properties.h>
28 #include <android-base/stringprintf.h>
31 #include "configreader.h"
32 #include "perfprofdcore.h"
33 #include "perfprofdutils.h"
34 #include "perfprofdmockutils.h"
36 #include "perf_profile.pb.h"
37 #include "google/protobuf/text_format.h"
40 // Set to argv[0] on startup
42 static const char *executable_path;
45 // test_dir is the directory containing the test executable and
46 // any files associated with the test (will be created by the harness).
48 // dest_dir is a subdirectory of test_dir that we'll create on the fly
49 // at the start of each testpoint (into which new files can be written),
50 // then delete at end of testpoint.
52 static std::string test_dir;
53 static std::string dest_dir;
55 // Path to perf executable on device
56 #define PERFPATH "/system/bin/perf"
58 // Temporary config file that we will emit for the daemon to read
59 #define CONFIGFILE "perfprofd.conf"
61 static std::string encoded_file_path(int seq)
63 return android::base::StringPrintf("%s/perf.data.encoded.%d",
64 dest_dir.c_str(), seq);
67 class PerfProfdTest : public testing::Test {
69 virtual void SetUp() {
70 mock_perfprofdutils_init();
75 virtual void TearDown() {
76 mock_perfprofdutils_finish();
89 void create_dest_dir() {
91 ASSERT_FALSE(dest_dir == "");
93 std::string cmd("rm -rf ");
97 std::string cmd("mkdir -p ");
104 if (test_dir == "") {
105 ASSERT_TRUE(executable_path != nullptr);
106 std::string s(executable_path);
107 auto found = s.find_last_of('/');
108 test_dir = s.substr(0,found);
116 static bool bothWhiteSpace(char lhs, char rhs)
118 return (std::isspace(lhs) && std::isspace(rhs));
122 // Squeeze out repeated whitespace from expected/actual logs.
124 static std::string squeezeWhite(const std::string &str,
128 if (dump) { fprintf(stderr, "raw %s is %s\n", tag, str.c_str()); }
129 std::string result(str);
130 std::replace(result.begin(), result.end(), '\n', ' ');
131 auto new_end = std::unique(result.begin(), result.end(), bothWhiteSpace);
132 result.erase(new_end, result.end());
133 while (result.begin() != result.end() && std::isspace(*result.rbegin())) {
136 if (dump) { fprintf(stderr, "squeezed %s is %s\n", tag, result.c_str()); }
141 // Replace all occurrences of a string with another string.
143 static std::string replaceAll(const std::string &str,
144 const std::string &from,
145 const std::string &to)
147 std::string ret = "";
149 while (pos < str.size()) {
150 size_t found = str.find(from, pos);
151 if (found == std::string::npos) {
152 ret += str.substr(pos);
155 ret += str.substr(pos, found - pos) + to;
156 pos = found + from.size();
162 // Replace occurrences of special variables in the string.
164 static std::string expandVars(const std::string &str) {
166 return replaceAll(str, "$NATIVE_TESTS", "/data/nativetest64");
168 return replaceAll(str, "$NATIVE_TESTS", "/data/nativetest");
173 /// Helper class to kick off a run of the perfprofd daemon with a specific
176 class PerfProfdRunner {
179 : config_path_(test_dir)
181 config_path_ += "/" CONFIGFILE;
186 remove_processed_file();
189 void addToConfig(const std::string &line)
191 config_text_ += line;
192 config_text_ += "\n";
195 void remove_semaphore_file()
197 std::string semaphore(test_dir);
198 semaphore += "/" SEMAPHORE_FILENAME;
199 unlink(semaphore.c_str());
202 void create_semaphore_file()
204 std::string semaphore(test_dir);
205 semaphore += "/" SEMAPHORE_FILENAME;
206 close(open(semaphore.c_str(), O_WRONLY|O_CREAT, 0600));
209 void write_processed_file(int start_seq, int end_seq)
211 std::string processed = test_dir + "/" PROCESSED_FILENAME;
212 FILE *fp = fopen(processed.c_str(), "w");
213 for (int i = start_seq; i < end_seq; i++) {
214 fprintf(fp, "%d\n", i);
219 void remove_processed_file()
221 std::string processed = test_dir + "/" PROCESSED_FILENAME;
222 unlink(processed.c_str());
225 struct LoggingConfig : public Config {
226 void Sleep(size_t seconds) override {
227 // Log sleep calls but don't sleep.
228 perfprofd_log_info("sleep %d seconds", seconds);
231 bool IsProfilingEnabled() const override {
233 // Check for existence of semaphore file in config directory
235 if (access(config_directory.c_str(), F_OK) == -1) {
236 W_ALOGW("unable to open config directory %s: (%s)",
237 config_directory.c_str(), strerror(errno));
241 // Check for existence of semaphore file
242 std::string semaphore_filepath = config_directory
243 + "/" + SEMAPHORE_FILENAME;
244 if (access(semaphore_filepath.c_str(), F_OK) == -1) {
254 static const char *argv[3] = { "perfprofd", "-c", "" };
255 argv[2] = config_path_.c_str();
257 writeConfigFile(config_path_, config_text_);
259 // execute daemon main
260 LoggingConfig config;
261 return perfprofd_main(3, (char **) argv, &config);
265 std::string config_path_;
266 std::string config_text_;
268 void writeConfigFile(const std::string &config_path,
269 const std::string &config_text)
271 FILE *fp = fopen(config_path.c_str(), "w");
272 ASSERT_TRUE(fp != nullptr);
273 fprintf(fp, "%s\n", config_text.c_str());
278 //......................................................................
280 static void readEncodedProfile(const char *testpoint,
281 wireless_android_play_playlog::AndroidPerfProfile &encodedProfile)
284 int perf_data_stat_result = stat(encoded_file_path(0).c_str(), &statb);
285 ASSERT_NE(-1, perf_data_stat_result);
289 encoded.resize(statb.st_size);
290 FILE *ifp = fopen(encoded_file_path(0).c_str(), "r");
291 ASSERT_NE(nullptr, ifp);
292 size_t items_read = fread((void*) encoded.data(), statb.st_size, 1, ifp);
293 ASSERT_EQ(1, items_read);
297 encodedProfile.ParseFromString(encoded);
300 static std::string encodedLoadModuleToString(const wireless_android_play_playlog::LoadModule &lm)
302 std::stringstream ss;
303 ss << "name: \"" << lm.name() << "\"\n";
304 if (lm.build_id() != "") {
305 ss << "build_id: \"" << lm.build_id() << "\"\n";
310 static std::string encodedModuleSamplesToString(const wireless_android_play_playlog::LoadModuleSamples &mod)
312 std::stringstream ss;
314 ss << "load_module_id: " << mod.load_module_id() << "\n";
315 for (size_t k = 0; k < mod.address_samples_size(); k++) {
316 const auto &sample = mod.address_samples(k);
317 ss << " address_samples {\n";
318 for (size_t l = 0; l < mod.address_samples(k).address_size();
320 auto address = mod.address_samples(k).address(l);
321 ss << " address: " << address << "\n";
323 ss << " count: " << sample.count() << "\n";
329 #define RAW_RESULT(x) #x
332 // Check to see if the log messages emitted by the daemon
333 // match the expected result. By default we use a partial
334 // match, e.g. if we see the expected excerpt anywhere in the
335 // result, it's a match (for exact match, set exact to true)
337 static void compareLogMessages(const std::string &actual,
338 const std::string &expected,
339 const char *testpoint,
340 bool exactMatch=false)
342 std::string sqexp = squeezeWhite(expected, "expected");
343 std::string sqact = squeezeWhite(actual, "actual");
345 EXPECT_STREQ(sqexp.c_str(), sqact.c_str());
347 std::size_t foundpos = sqact.find(sqexp);
348 bool wasFound = true;
349 if (foundpos == std::string::npos) {
350 std::cerr << testpoint << ": expected result not found\n";
351 std::cerr << " Actual: \"" << sqact << "\"\n";
352 std::cerr << " Expected: \"" << sqexp << "\"\n";
355 EXPECT_TRUE(wasFound);
359 TEST_F(PerfProfdTest, TestUtil)
361 EXPECT_EQ("", replaceAll("", "", ""));
362 EXPECT_EQ("zzbc", replaceAll("abc", "a", "zz"));
363 EXPECT_EQ("azzc", replaceAll("abc", "b", "zz"));
364 EXPECT_EQ("abzz", replaceAll("abc", "c", "zz"));
365 EXPECT_EQ("xxyyzz", replaceAll("abc", "abc", "xxyyzz"));
368 TEST_F(PerfProfdTest, MissingGMS)
371 // AWP requires cooperation between the daemon and the GMS core
372 // piece. If we're running on a device that has an old or damaged
373 // version of GMS core, then the config directory we're interested in
374 // may not be there. This test insures that the daemon does the
375 // right thing in this case.
377 PerfProfdRunner runner;
378 runner.addToConfig("only_debug_build=0");
379 runner.addToConfig("trace_config_read=0");
380 runner.addToConfig("config_directory=/does/not/exist");
381 runner.addToConfig("main_loop_iterations=1");
382 runner.addToConfig("use_fixed_seed=1");
383 runner.addToConfig("collection_interval=100");
386 int daemon_main_return_code = runner.invoke();
388 // Check return code from daemon
389 EXPECT_EQ(0, daemon_main_return_code);
391 // Verify log contents
392 const std::string expected = RAW_RESULT(
394 W: unable to open config directory /does/not/exist: (No such file or directory)
395 I: profile collection skipped (missing config directory)
398 // check to make sure entire log matches
399 compareLogMessages(mock_perfprofdutils_getlogged(),
400 expected, "MissingGMS");
404 TEST_F(PerfProfdTest, MissingOptInSemaphoreFile)
407 // Android device owners must opt in to "collect and report usage
408 // data" in order for us to be able to collect profiles. The opt-in
409 // check is performed in the GMS core component; if the check
410 // passes, then it creates a semaphore file for the daemon to pick
413 PerfProfdRunner runner;
414 runner.addToConfig("only_debug_build=0");
415 std::string cfparam("config_directory="); cfparam += test_dir;
416 runner.addToConfig(cfparam);
417 std::string ddparam("destination_directory="); ddparam += dest_dir;
418 runner.addToConfig(ddparam);
419 runner.addToConfig("main_loop_iterations=1");
420 runner.addToConfig("use_fixed_seed=1");
421 runner.addToConfig("collection_interval=100");
423 runner.remove_semaphore_file();
426 int daemon_main_return_code = runner.invoke();
428 // Check return code from daemon
429 EXPECT_EQ(0, daemon_main_return_code);
431 // Verify log contents
432 const std::string expected = RAW_RESULT(
433 I: profile collection skipped (missing semaphore file)
435 // check to make sure log excerpt matches
436 compareLogMessages(mock_perfprofdutils_getlogged(),
437 expected, "MissingOptInSemaphoreFile");
440 TEST_F(PerfProfdTest, MissingPerfExecutable)
443 // Perfprofd uses the 'simpleperf' tool to collect profiles
444 // (although this may conceivably change in the future). This test
445 // checks to make sure that if 'simpleperf' is not present we bail out
446 // from collecting profiles.
448 PerfProfdRunner runner;
449 runner.addToConfig("only_debug_build=0");
450 runner.addToConfig("trace_config_read=1");
451 std::string cfparam("config_directory="); cfparam += test_dir;
452 runner.addToConfig(cfparam);
453 std::string ddparam("destination_directory="); ddparam += dest_dir;
454 runner.addToConfig(ddparam);
455 runner.addToConfig("main_loop_iterations=1");
456 runner.addToConfig("use_fixed_seed=1");
457 runner.addToConfig("collection_interval=100");
458 runner.addToConfig("perf_path=/does/not/exist");
460 // Create semaphore file
461 runner.create_semaphore_file();
464 int daemon_main_return_code = runner.invoke();
466 // Check return code from daemon
467 EXPECT_EQ(0, daemon_main_return_code);
469 // expected log contents
470 const std::string expected = RAW_RESULT(
471 I: profile collection skipped (missing 'perf' executable)
473 // check to make sure log excerpt matches
474 compareLogMessages(mock_perfprofdutils_getlogged(),
475 expected, "MissingPerfExecutable");
478 TEST_F(PerfProfdTest, BadPerfRun)
481 // Perf tools tend to be tightly coupled with a specific kernel
482 // version -- if things are out of sync perf could fail or
483 // crash. This test makes sure that we detect such a case and log
486 PerfProfdRunner runner;
487 runner.addToConfig("only_debug_build=0");
488 std::string cfparam("config_directory="); cfparam += test_dir;
489 runner.addToConfig(cfparam);
490 std::string ddparam("destination_directory="); ddparam += dest_dir;
491 runner.addToConfig(ddparam);
492 runner.addToConfig("main_loop_iterations=1");
493 runner.addToConfig("use_fixed_seed=1");
494 runner.addToConfig("collection_interval=100");
495 runner.addToConfig("perf_path=/system/bin/false");
497 // Create semaphore file
498 runner.create_semaphore_file();
501 int daemon_main_return_code = runner.invoke();
503 // Check return code from daemon
504 EXPECT_EQ(0, daemon_main_return_code);
506 // Verify log contents
507 const std::string expected = RAW_RESULT(
508 I: profile collection failed (perf record returned bad exit status)
511 // check to make sure log excerpt matches
512 compareLogMessages(mock_perfprofdutils_getlogged(),
513 expected, "BadPerfRun");
516 TEST_F(PerfProfdTest, ConfigFileParsing)
519 // Gracefully handly malformed items in the config file
521 PerfProfdRunner runner;
522 runner.addToConfig("only_debug_build=0");
523 runner.addToConfig("main_loop_iterations=1");
524 runner.addToConfig("collection_interval=100");
525 runner.addToConfig("use_fixed_seed=1");
526 runner.addToConfig("destination_directory=/does/not/exist");
528 // assorted bad syntax
529 runner.addToConfig("collection_interval=0");
530 runner.addToConfig("collection_interval=-1");
531 runner.addToConfig("collection_interval=2");
532 runner.addToConfig("nonexistent_key=something");
533 runner.addToConfig("no_equals_stmt");
536 int daemon_main_return_code = runner.invoke();
538 // Check return code from daemon
539 EXPECT_EQ(0, daemon_main_return_code);
541 // Verify log contents
542 const std::string expected = RAW_RESULT(
543 W: line 6: specified value 0 for 'collection_interval' outside permitted range [100 4294967295] (ignored)
544 W: line 7: malformed unsigned value (ignored)
545 W: line 8: specified value 2 for 'collection_interval' outside permitted range [100 4294967295] (ignored)
546 W: line 9: unknown option 'nonexistent_key' ignored
547 W: line 10: line malformed (no '=' found)
550 // check to make sure log excerpt matches
551 compareLogMessages(mock_perfprofdutils_getlogged(),
552 expected, "ConfigFileParsing");
555 TEST_F(PerfProfdTest, ProfileCollectionAnnotations)
557 unsigned util1 = collect_cpu_utilization();
558 EXPECT_LE(util1, 100);
561 // NB: expectation is that when we run this test, the device will be
562 // completed booted, will be on charger, and will not have the camera
564 EXPECT_FALSE(get_booting());
565 EXPECT_TRUE(get_charging());
566 EXPECT_FALSE(get_camera_active());
569 TEST_F(PerfProfdTest, BasicRunWithCannedPerf)
572 // Verify the portion of the daemon that reads and encodes
573 // perf.data files. Here we run the encoder on a canned perf.data
574 // file and verify that the resulting protobuf contains what
575 // we think it should contain.
577 std::string input_perf_data(test_dir);
578 input_perf_data += "/canned.perf.data";
580 // Set up config to avoid these annotations (they are tested elsewhere)
581 ConfigReader config_reader;
582 config_reader.overrideUnsignedEntry("collect_cpu_utilization", 0);
583 config_reader.overrideUnsignedEntry("collect_charging_state", 0);
584 config_reader.overrideUnsignedEntry("collect_camera_active", 0);
585 PerfProfdRunner::LoggingConfig config;
586 config_reader.FillConfig(&config);
588 // Kick off encoder and check return code
589 PROFILE_RESULT result =
590 encode_to_proto(input_perf_data, encoded_file_path(0).c_str(), config, 0);
591 EXPECT_EQ(OK_PROFILE_COLLECTION, result);
593 // Read and decode the resulting perf.data.encoded file
594 wireless_android_play_playlog::AndroidPerfProfile encodedProfile;
595 readEncodedProfile("BasicRunWithCannedPerf",
598 // Expect 45 programs
599 EXPECT_EQ(45, encodedProfile.programs_size());
601 // Check a couple of load modules
602 { const auto &lm0 = encodedProfile.load_modules(0);
603 std::string act_lm0 = encodedLoadModuleToString(lm0);
604 std::string sqact0 = squeezeWhite(act_lm0, "actual for lm 0");
605 const std::string expected_lm0 = RAW_RESULT(
606 name: "/data/app/com.google.android.apps.plus-1/lib/arm/libcronet.so"
608 std::string sqexp0 = squeezeWhite(expected_lm0, "expected_lm0");
609 EXPECT_STREQ(sqexp0.c_str(), sqact0.c_str());
611 { const auto &lm9 = encodedProfile.load_modules(9);
612 std::string act_lm9 = encodedLoadModuleToString(lm9);
613 std::string sqact9 = squeezeWhite(act_lm9, "actual for lm 9");
614 const std::string expected_lm9 = RAW_RESULT(
615 name: "/system/lib/libandroid_runtime.so" build_id: "8164ed7b3a8b8f5a220d027788922510"
617 std::string sqexp9 = squeezeWhite(expected_lm9, "expected_lm9");
618 EXPECT_STREQ(sqexp9.c_str(), sqact9.c_str());
621 // Examine some of the samples now
622 { const auto &p1 = encodedProfile.programs(0);
623 const auto &lm1 = p1.modules(0);
624 std::string act_lm1 = encodedModuleSamplesToString(lm1);
625 std::string sqact1 = squeezeWhite(act_lm1, "actual for lm1");
626 const std::string expected_lm1 = RAW_RESULT(
627 load_module_id: 9 address_samples { address: 296100 count: 1 }
629 std::string sqexp1 = squeezeWhite(expected_lm1, "expected_lm1");
630 EXPECT_STREQ(sqexp1.c_str(), sqact1.c_str());
632 { const auto &p1 = encodedProfile.programs(2);
633 const auto &lm2 = p1.modules(0);
634 std::string act_lm2 = encodedModuleSamplesToString(lm2);
635 std::string sqact2 = squeezeWhite(act_lm2, "actual for lm2");
636 const std::string expected_lm2 = RAW_RESULT(
638 address_samples { address: 28030244 count: 1 }
639 address_samples { address: 29657840 count: 1 }
641 std::string sqexp2 = squeezeWhite(expected_lm2, "expected_lm2");
642 EXPECT_STREQ(sqexp2.c_str(), sqact2.c_str());
646 TEST_F(PerfProfdTest, CallchainRunWithCannedPerf)
648 // This test makes sure that the perf.data converter
649 // can handle call chains.
651 std::string input_perf_data(test_dir);
652 input_perf_data += "/callchain.canned.perf.data";
654 // Set up config to avoid these annotations (they are tested elsewhere)
655 ConfigReader config_reader;
656 config_reader.overrideUnsignedEntry("collect_cpu_utilization", 0);
657 config_reader.overrideUnsignedEntry("collect_charging_state", 0);
658 config_reader.overrideUnsignedEntry("collect_camera_active", 0);
659 PerfProfdRunner::LoggingConfig config;
660 config_reader.FillConfig(&config);
662 // Kick off encoder and check return code
663 PROFILE_RESULT result =
664 encode_to_proto(input_perf_data, encoded_file_path(0).c_str(), config, 0);
665 EXPECT_EQ(OK_PROFILE_COLLECTION, result);
667 // Read and decode the resulting perf.data.encoded file
668 wireless_android_play_playlog::AndroidPerfProfile encodedProfile;
669 readEncodedProfile("BasicRunWithCannedPerf",
673 // Expect 3 programs 8 load modules
674 EXPECT_EQ(3, encodedProfile.programs_size());
675 EXPECT_EQ(8, encodedProfile.load_modules_size());
677 // Check a couple of load modules
678 { const auto &lm0 = encodedProfile.load_modules(0);
679 std::string act_lm0 = encodedLoadModuleToString(lm0);
680 std::string sqact0 = squeezeWhite(act_lm0, "actual for lm 0");
681 const std::string expected_lm0 = RAW_RESULT(
682 name: "/system/bin/dex2oat"
683 build_id: "ee12bd1a1de39422d848f249add0afc4"
685 std::string sqexp0 = squeezeWhite(expected_lm0, "expected_lm0");
686 EXPECT_STREQ(sqexp0.c_str(), sqact0.c_str());
688 { const auto &lm1 = encodedProfile.load_modules(1);
689 std::string act_lm1 = encodedLoadModuleToString(lm1);
690 std::string sqact1 = squeezeWhite(act_lm1, "actual for lm 1");
691 const std::string expected_lm1 = RAW_RESULT(
692 name: "/system/bin/linker"
693 build_id: "a36715f673a4a0aa76ef290124c516cc"
695 std::string sqexp1 = squeezeWhite(expected_lm1, "expected_lm1");
696 EXPECT_STREQ(sqexp1.c_str(), sqact1.c_str());
699 // Examine some of the samples now
700 { const auto &p0 = encodedProfile.programs(0);
701 const auto &lm1 = p0.modules(0);
702 std::string act_lm1 = encodedModuleSamplesToString(lm1);
703 std::string sqact1 = squeezeWhite(act_lm1, "actual for lm1");
704 const std::string expected_lm1 = RAW_RESULT(
706 address_samples { address: 108552 count: 2 }
708 std::string sqexp1 = squeezeWhite(expected_lm1, "expected_lm1");
709 EXPECT_STREQ(sqexp1.c_str(), sqact1.c_str());
711 { const auto &p4 = encodedProfile.programs(2);
712 const auto &lm2 = p4.modules(1);
713 std::string act_lm2 = encodedModuleSamplesToString(lm2);
714 std::string sqact2 = squeezeWhite(act_lm2, "actual for lm2");
715 const std::string expected_lm2 = RAW_RESULT(
716 load_module_id: 2 address_samples { address: 403913 count: 1 } address_samples { address: 840761 count: 1 } address_samples { address: 846481 count: 1 } address_samples { address: 999053 count: 1 } address_samples { address: 1012959 count: 1 } address_samples { address: 1524309 count: 1 } address_samples { address: 1580779 count: 1 } address_samples { address: 4287986288 count: 1 }
718 std::string sqexp2 = squeezeWhite(expected_lm2, "expected_lm2");
719 EXPECT_STREQ(sqexp2.c_str(), sqact2.c_str());
723 TEST_F(PerfProfdTest, BasicRunWithLivePerf)
726 // Basic test to exercise the main loop of the daemon. It includes
729 PerfProfdRunner runner;
730 runner.addToConfig("only_debug_build=0");
731 std::string ddparam("destination_directory="); ddparam += dest_dir;
732 runner.addToConfig(ddparam);
733 std::string cfparam("config_directory="); cfparam += test_dir;
734 runner.addToConfig(cfparam);
735 runner.addToConfig("main_loop_iterations=1");
736 runner.addToConfig("use_fixed_seed=12345678");
737 runner.addToConfig("max_unprocessed_profiles=100");
738 runner.addToConfig("collection_interval=9999");
739 runner.addToConfig("sample_duration=2");
741 // Create semaphore file
742 runner.create_semaphore_file();
745 int daemon_main_return_code = runner.invoke();
747 // Check return code from daemon
748 EXPECT_EQ(0, daemon_main_return_code);
750 // Read and decode the resulting perf.data.encoded file
751 wireless_android_play_playlog::AndroidPerfProfile encodedProfile;
752 readEncodedProfile("BasicRunWithLivePerf", encodedProfile);
754 // Examine what we get back. Since it's a live profile, we can't
755 // really do much in terms of verifying the contents.
756 EXPECT_LT(0, encodedProfile.programs_size());
758 // Verify log contents
759 const std::string expected = RAW_RESULT(
760 I: starting Android Wide Profiling daemon
761 I: config file path set to $NATIVE_TESTS/perfprofd_test/perfprofd.conf
762 I: random seed set to 12345678
764 I: initiating profile collection
765 I: profile collection complete
766 I: sleep 9325 seconds
767 I: finishing Android Wide Profiling daemon
769 // check to make sure log excerpt matches
770 compareLogMessages(mock_perfprofdutils_getlogged(),
771 expandVars(expected), "BasicRunWithLivePerf", true);
774 TEST_F(PerfProfdTest, MultipleRunWithLivePerf)
777 // Basic test to exercise the main loop of the daemon. It includes
780 PerfProfdRunner runner;
781 runner.addToConfig("only_debug_build=0");
782 std::string ddparam("destination_directory="); ddparam += dest_dir;
783 runner.addToConfig(ddparam);
784 std::string cfparam("config_directory="); cfparam += test_dir;
785 runner.addToConfig(cfparam);
786 runner.addToConfig("main_loop_iterations=3");
787 runner.addToConfig("use_fixed_seed=12345678");
788 runner.addToConfig("collection_interval=9999");
789 runner.addToConfig("sample_duration=2");
790 runner.write_processed_file(1, 2);
792 // Create semaphore file
793 runner.create_semaphore_file();
796 int daemon_main_return_code = runner.invoke();
798 // Check return code from daemon
799 EXPECT_EQ(0, daemon_main_return_code);
801 // Read and decode the resulting perf.data.encoded file
802 wireless_android_play_playlog::AndroidPerfProfile encodedProfile;
803 readEncodedProfile("BasicRunWithLivePerf", encodedProfile);
805 // Examine what we get back. Since it's a live profile, we can't
806 // really do much in terms of verifying the contents.
807 EXPECT_LT(0, encodedProfile.programs_size());
809 // Examine that encoded.1 file is removed while encoded.{0|2} exists.
810 EXPECT_EQ(0, access(encoded_file_path(0).c_str(), F_OK));
811 EXPECT_NE(0, access(encoded_file_path(1).c_str(), F_OK));
812 EXPECT_EQ(0, access(encoded_file_path(2).c_str(), F_OK));
814 // Verify log contents
815 const std::string expected = RAW_RESULT(
816 I: starting Android Wide Profiling daemon
817 I: config file path set to $NATIVE_TESTS/perfprofd_test/perfprofd.conf
818 I: random seed set to 12345678
820 I: initiating profile collection
821 I: profile collection complete
822 I: sleep 9325 seconds
823 I: sleep 4974 seconds
824 I: initiating profile collection
825 I: profile collection complete
826 I: sleep 5025 seconds
828 I: initiating profile collection
829 I: profile collection complete
830 I: sleep 9498 seconds
831 I: finishing Android Wide Profiling daemon
833 // check to make sure log excerpt matches
834 compareLogMessages(mock_perfprofdutils_getlogged(),
835 expandVars(expected), "BasicRunWithLivePerf", true);
838 TEST_F(PerfProfdTest, CallChainRunWithLivePerf)
841 // Collect a callchain profile, so as to exercise the code in
842 // perf_data post-processing that digests callchains.
844 PerfProfdRunner runner;
845 std::string ddparam("destination_directory="); ddparam += dest_dir;
846 runner.addToConfig(ddparam);
847 std::string cfparam("config_directory="); cfparam += test_dir;
848 runner.addToConfig(cfparam);
849 runner.addToConfig("main_loop_iterations=1");
850 runner.addToConfig("use_fixed_seed=12345678");
851 runner.addToConfig("max_unprocessed_profiles=100");
852 runner.addToConfig("collection_interval=9999");
853 runner.addToConfig("stack_profile=1");
854 runner.addToConfig("sample_duration=2");
856 // Create semaphore file
857 runner.create_semaphore_file();
860 int daemon_main_return_code = runner.invoke();
862 // Check return code from daemon
863 EXPECT_EQ(0, daemon_main_return_code);
865 // Read and decode the resulting perf.data.encoded file
866 wireless_android_play_playlog::AndroidPerfProfile encodedProfile;
867 readEncodedProfile("CallChainRunWithLivePerf", encodedProfile);
869 // Examine what we get back. Since it's a live profile, we can't
870 // really do much in terms of verifying the contents.
871 EXPECT_LT(0, encodedProfile.programs_size());
873 // Verify log contents
874 const std::string expected = RAW_RESULT(
875 I: starting Android Wide Profiling daemon
876 I: config file path set to $NATIVE_TESTS/perfprofd_test/perfprofd.conf
877 I: random seed set to 12345678
879 I: initiating profile collection
880 I: profile collection complete
881 I: sleep 9325 seconds
882 I: finishing Android Wide Profiling daemon
884 // check to make sure log excerpt matches
885 compareLogMessages(mock_perfprofdutils_getlogged(),
886 expandVars(expected), "CallChainRunWithLivePerf", true);
889 int main(int argc, char **argv) {
890 executable_path = argv[0];
891 // switch to / before starting testing (perfprofd
892 // should be location-independent)
894 testing::InitGoogleTest(&argc, argv);
895 return RUN_ALL_TESTS();