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.
26 #include <sys/types.h>
29 #include <android-base/file.h>
30 #include <android-base/logging.h>
31 #include <android-base/properties.h>
32 #include <android-base/stringprintf.h>
33 #include <android-base/test_utils.h>
34 #include <gtest/gtest.h>
37 #include "configreader.h"
38 #include "perfprofdcore.h"
39 #include "perfprofdutils.h"
40 #include "perfprofdmockutils.h"
42 #include "perf_profile.pb.h"
43 #include "google/protobuf/text_format.h"
46 // Set to argv[0] on startup
48 static std::string gExecutableRealpath;
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 class PerfProfdTest : public testing::Test {
58 virtual void SetUp() {
59 mock_perfprofdutils_init();
63 virtual void TearDown() {
64 mock_perfprofdutils_finish();
66 // TODO: proper management of test files. For now, use old system() code.
67 for (const auto dir : { &dest_dir, &conf_dir }) {
68 std::string cmd("rm -rf ");
70 int ret = system(cmd.c_str());
76 // test_dir is the directory containing the test executable and
77 // any files associated with the test (will be created by the harness).
80 // dest_dir is a temporary directory that we're using as the destination directory.
81 // It is backed by temp_dir1.
84 // conf_dir is a temporary directory that we're using as the configuration directory.
85 // It is backed by temp_dir2.
90 temp_dir1.reset(new TemporaryDir());
91 temp_dir2.reset(new TemporaryDir());
92 dest_dir = temp_dir1->path;
93 conf_dir = temp_dir2->path;
94 test_dir = android::base::Dirname(gExecutableRealpath);
97 std::unique_ptr<TemporaryDir> temp_dir1;
98 std::unique_ptr<TemporaryDir> temp_dir2;
101 static bool bothWhiteSpace(char lhs, char rhs)
103 return (std::isspace(lhs) && std::isspace(rhs));
107 // Squeeze out repeated whitespace from expected/actual logs.
109 static std::string squeezeWhite(const std::string &str,
113 if (dump) { fprintf(stderr, "raw %s is %s\n", tag, str.c_str()); }
114 std::string result(str);
115 std::replace(result.begin(), result.end(), '\n', ' ');
116 auto new_end = std::unique(result.begin(), result.end(), bothWhiteSpace);
117 result.erase(new_end, result.end());
118 while (result.begin() != result.end() && std::isspace(*result.rbegin())) {
121 if (dump) { fprintf(stderr, "squeezed %s is %s\n", tag, result.c_str()); }
126 // Replace all occurrences of a string with another string.
128 static std::string replaceAll(const std::string &str,
129 const std::string &from,
130 const std::string &to)
132 std::string ret = "";
134 while (pos < str.size()) {
135 size_t found = str.find(from, pos);
136 if (found == std::string::npos) {
137 ret += str.substr(pos);
140 ret += str.substr(pos, found - pos) + to;
141 pos = found + from.size();
147 // Replace occurrences of special variables in the string.
149 static std::string expandVars(const std::string &str) {
151 return replaceAll(str, "$NATIVE_TESTS", "/data/nativetest64");
153 return replaceAll(str, "$NATIVE_TESTS", "/data/nativetest");
158 /// Helper class to kick off a run of the perfprofd daemon with a specific
161 class PerfProfdRunner {
163 explicit PerfProfdRunner(const std::string& config_dir)
164 : config_dir_(config_dir)
166 config_path_ = config_dir + "/" CONFIGFILE;
171 remove_processed_file();
174 void addToConfig(const std::string &line)
176 config_text_ += line;
177 config_text_ += "\n";
180 void remove_semaphore_file()
182 std::string semaphore(config_dir_);
183 semaphore += "/" SEMAPHORE_FILENAME;
184 unlink(semaphore.c_str());
187 void create_semaphore_file()
189 std::string semaphore(config_dir_);
190 semaphore += "/" SEMAPHORE_FILENAME;
191 close(open(semaphore.c_str(), O_WRONLY|O_CREAT, 0600));
194 void write_processed_file(int start_seq, int end_seq)
196 std::string processed = config_dir_ + "/" PROCESSED_FILENAME;
197 FILE *fp = fopen(processed.c_str(), "w");
198 for (int i = start_seq; i < end_seq; i++) {
199 fprintf(fp, "%d\n", i);
204 void remove_processed_file()
206 std::string processed = config_dir_ + "/" PROCESSED_FILENAME;
207 unlink(processed.c_str());
210 struct LoggingConfig : public Config {
211 void Sleep(size_t seconds) override {
212 // Log sleep calls but don't sleep.
213 perfprofd_log_info("sleep %d seconds", seconds);
216 bool IsProfilingEnabled() const override {
218 // Check for existence of semaphore file in config directory
220 if (access(config_directory.c_str(), F_OK) == -1) {
221 W_ALOGW("unable to open config directory %s: (%s)",
222 config_directory.c_str(), strerror(errno));
226 // Check for existence of semaphore file
227 std::string semaphore_filepath = config_directory
228 + "/" + SEMAPHORE_FILENAME;
229 if (access(semaphore_filepath.c_str(), F_OK) == -1) {
239 static const char *argv[3] = { "perfprofd", "-c", "" };
240 argv[2] = config_path_.c_str();
242 writeConfigFile(config_path_, config_text_);
244 // execute daemon main
245 LoggingConfig config;
246 return perfprofd_main(3, (char **) argv, &config);
250 std::string config_dir_;
251 std::string config_path_;
252 std::string config_text_;
254 void writeConfigFile(const std::string &config_path,
255 const std::string &config_text)
257 FILE *fp = fopen(config_path.c_str(), "w");
258 ASSERT_TRUE(fp != nullptr);
259 fprintf(fp, "%s\n", config_text.c_str());
264 //......................................................................
266 static std::string encoded_file_path(const std::string& dest_dir,
268 return android::base::StringPrintf("%s/perf.data.encoded.%d",
269 dest_dir.c_str(), seq);
272 static void readEncodedProfile(const std::string& dest_dir,
273 const char *testpoint,
274 wireless_android_play_playlog::AndroidPerfProfile &encodedProfile)
277 int perf_data_stat_result = stat(encoded_file_path(dest_dir, 0).c_str(), &statb);
278 ASSERT_NE(-1, perf_data_stat_result);
282 encoded.resize(statb.st_size);
283 FILE *ifp = fopen(encoded_file_path(dest_dir, 0).c_str(), "r");
284 ASSERT_NE(nullptr, ifp);
285 size_t items_read = fread((void*) encoded.data(), statb.st_size, 1, ifp);
286 ASSERT_EQ(1, items_read);
290 encodedProfile.ParseFromString(encoded);
293 static std::string encodedLoadModuleToString(const wireless_android_play_playlog::LoadModule &lm)
295 std::stringstream ss;
296 ss << "name: \"" << lm.name() << "\"\n";
297 if (lm.build_id() != "") {
298 ss << "build_id: \"" << lm.build_id() << "\"\n";
303 static std::string encodedModuleSamplesToString(const wireless_android_play_playlog::LoadModuleSamples &mod)
305 std::stringstream ss;
307 ss << "load_module_id: " << mod.load_module_id() << "\n";
308 for (size_t k = 0; k < mod.address_samples_size(); k++) {
309 const auto &sample = mod.address_samples(k);
310 ss << " address_samples {\n";
311 for (size_t l = 0; l < mod.address_samples(k).address_size();
313 auto address = mod.address_samples(k).address(l);
314 ss << " address: " << address << "\n";
316 ss << " count: " << sample.count() << "\n";
322 #define RAW_RESULT(x) #x
325 // Check to see if the log messages emitted by the daemon
326 // match the expected result. By default we use a partial
327 // match, e.g. if we see the expected excerpt anywhere in the
328 // result, it's a match (for exact match, set exact to true)
330 static void compareLogMessages(const std::string &actual,
331 const std::string &expected,
332 const char *testpoint,
333 bool exactMatch=false)
335 std::string sqexp = squeezeWhite(expected, "expected");
336 std::string sqact = squeezeWhite(actual, "actual");
338 EXPECT_STREQ(sqexp.c_str(), sqact.c_str());
340 std::size_t foundpos = sqact.find(sqexp);
341 bool wasFound = true;
342 if (foundpos == std::string::npos) {
343 std::cerr << testpoint << ": expected result not found\n";
344 std::cerr << " Actual: \"" << sqact << "\"\n";
345 std::cerr << " Expected: \"" << sqexp << "\"\n";
348 EXPECT_TRUE(wasFound);
352 TEST_F(PerfProfdTest, TestUtil)
354 EXPECT_EQ("", replaceAll("", "", ""));
355 EXPECT_EQ("zzbc", replaceAll("abc", "a", "zz"));
356 EXPECT_EQ("azzc", replaceAll("abc", "b", "zz"));
357 EXPECT_EQ("abzz", replaceAll("abc", "c", "zz"));
358 EXPECT_EQ("xxyyzz", replaceAll("abc", "abc", "xxyyzz"));
361 TEST_F(PerfProfdTest, MissingGMS)
364 // AWP requires cooperation between the daemon and the GMS core
365 // piece. If we're running on a device that has an old or damaged
366 // version of GMS core, then the config directory we're interested in
367 // may not be there. This test insures that the daemon does the
368 // right thing in this case.
370 PerfProfdRunner runner(conf_dir);
371 runner.addToConfig("only_debug_build=0");
372 runner.addToConfig("trace_config_read=0");
373 runner.addToConfig("config_directory=/does/not/exist");
374 runner.addToConfig("main_loop_iterations=1");
375 runner.addToConfig("use_fixed_seed=1");
376 runner.addToConfig("collection_interval=100");
379 int daemon_main_return_code = runner.invoke();
381 // Check return code from daemon
382 EXPECT_EQ(0, daemon_main_return_code);
384 // Verify log contents
385 const std::string expected = RAW_RESULT(
387 W: unable to open config directory /does/not/exist: (No such file or directory)
388 I: profile collection skipped (missing config directory)
391 // check to make sure entire log matches
392 compareLogMessages(mock_perfprofdutils_getlogged(),
393 expected, "MissingGMS");
397 TEST_F(PerfProfdTest, MissingOptInSemaphoreFile)
400 // Android device owners must opt in to "collect and report usage
401 // data" in order for us to be able to collect profiles. The opt-in
402 // check is performed in the GMS core component; if the check
403 // passes, then it creates a semaphore file for the daemon to pick
406 PerfProfdRunner runner(conf_dir);
407 runner.addToConfig("only_debug_build=0");
408 std::string cfparam("config_directory="); cfparam += conf_dir;
409 runner.addToConfig(cfparam);
410 std::string ddparam("destination_directory="); ddparam += dest_dir;
411 runner.addToConfig(ddparam);
412 runner.addToConfig("main_loop_iterations=1");
413 runner.addToConfig("use_fixed_seed=1");
414 runner.addToConfig("collection_interval=100");
416 runner.remove_semaphore_file();
419 int daemon_main_return_code = runner.invoke();
421 // Check return code from daemon
422 EXPECT_EQ(0, daemon_main_return_code);
424 // Verify log contents
425 const std::string expected = RAW_RESULT(
426 I: profile collection skipped (missing config directory)
428 // check to make sure log excerpt matches
429 compareLogMessages(mock_perfprofdutils_getlogged(),
430 expected, "MissingOptInSemaphoreFile");
433 TEST_F(PerfProfdTest, MissingPerfExecutable)
436 // Perfprofd uses the 'simpleperf' tool to collect profiles
437 // (although this may conceivably change in the future). This test
438 // checks to make sure that if 'simpleperf' is not present we bail out
439 // from collecting profiles.
441 PerfProfdRunner runner(conf_dir);
442 runner.addToConfig("only_debug_build=0");
443 runner.addToConfig("trace_config_read=1");
444 std::string cfparam("config_directory="); cfparam += conf_dir;
445 runner.addToConfig(cfparam);
446 std::string ddparam("destination_directory="); ddparam += dest_dir;
447 runner.addToConfig(ddparam);
448 runner.addToConfig("main_loop_iterations=1");
449 runner.addToConfig("use_fixed_seed=1");
450 runner.addToConfig("collection_interval=100");
451 runner.addToConfig("perf_path=/does/not/exist");
453 // Create semaphore file
454 runner.create_semaphore_file();
457 int daemon_main_return_code = runner.invoke();
459 // Check return code from daemon
460 EXPECT_EQ(0, daemon_main_return_code);
462 // expected log contents
463 const std::string expected = RAW_RESULT(
464 I: profile collection skipped (missing 'perf' executable)
466 // check to make sure log excerpt matches
467 compareLogMessages(mock_perfprofdutils_getlogged(),
468 expected, "MissingPerfExecutable");
471 TEST_F(PerfProfdTest, BadPerfRun)
474 // Perf tools tend to be tightly coupled with a specific kernel
475 // version -- if things are out of sync perf could fail or
476 // crash. This test makes sure that we detect such a case and log
479 PerfProfdRunner runner(conf_dir);
480 runner.addToConfig("only_debug_build=0");
481 std::string cfparam("config_directory="); cfparam += conf_dir;
482 runner.addToConfig(cfparam);
483 std::string ddparam("destination_directory="); ddparam += dest_dir;
484 runner.addToConfig(ddparam);
485 runner.addToConfig("main_loop_iterations=1");
486 runner.addToConfig("use_fixed_seed=1");
487 runner.addToConfig("collection_interval=100");
488 runner.addToConfig("perf_path=/system/bin/false");
490 // Create semaphore file
491 runner.create_semaphore_file();
494 int daemon_main_return_code = runner.invoke();
496 // Check return code from daemon
497 EXPECT_EQ(0, daemon_main_return_code);
499 // Verify log contents
500 const std::string expected = RAW_RESULT(
501 I: profile collection failed (perf record returned bad exit status)
504 // check to make sure log excerpt matches
505 compareLogMessages(mock_perfprofdutils_getlogged(),
506 expected, "BadPerfRun");
509 TEST_F(PerfProfdTest, ConfigFileParsing)
512 // Gracefully handly malformed items in the config file
514 PerfProfdRunner runner(conf_dir);
515 runner.addToConfig("only_debug_build=0");
516 runner.addToConfig("main_loop_iterations=1");
517 runner.addToConfig("collection_interval=100");
518 runner.addToConfig("use_fixed_seed=1");
519 runner.addToConfig("destination_directory=/does/not/exist");
521 // assorted bad syntax
522 runner.addToConfig("collection_interval=0");
523 runner.addToConfig("collection_interval=-1");
524 runner.addToConfig("collection_interval=2");
525 runner.addToConfig("nonexistent_key=something");
526 runner.addToConfig("no_equals_stmt");
529 int daemon_main_return_code = runner.invoke();
531 // Check return code from daemon
532 EXPECT_EQ(0, daemon_main_return_code);
534 // Verify log contents
535 const std::string expected = RAW_RESULT(
536 W: line 6: specified value 0 for 'collection_interval' outside permitted range [100 4294967295] (ignored)
537 W: line 7: malformed unsigned value (ignored)
538 W: line 8: specified value 2 for 'collection_interval' outside permitted range [100 4294967295] (ignored)
539 W: line 9: unknown option 'nonexistent_key' ignored
540 W: line 10: line malformed (no '=' found)
543 // check to make sure log excerpt matches
544 compareLogMessages(mock_perfprofdutils_getlogged(),
545 expected, "ConfigFileParsing");
548 TEST_F(PerfProfdTest, ProfileCollectionAnnotations)
550 unsigned util1 = collect_cpu_utilization();
551 EXPECT_LE(util1, 100);
554 // NB: expectation is that when we run this test, the device will be
555 // completed booted, will be on charger, and will not have the camera
557 EXPECT_FALSE(get_booting());
558 EXPECT_TRUE(get_charging());
559 EXPECT_FALSE(get_camera_active());
562 TEST_F(PerfProfdTest, BasicRunWithCannedPerf)
565 // Verify the portion of the daemon that reads and encodes
566 // perf.data files. Here we run the encoder on a canned perf.data
567 // file and verify that the resulting protobuf contains what
568 // we think it should contain.
570 std::string input_perf_data(test_dir);
571 input_perf_data += "/canned.perf.data";
573 // Set up config to avoid these annotations (they are tested elsewhere)
574 ConfigReader config_reader;
575 config_reader.overrideUnsignedEntry("collect_cpu_utilization", 0);
576 config_reader.overrideUnsignedEntry("collect_charging_state", 0);
577 config_reader.overrideUnsignedEntry("collect_camera_active", 0);
578 PerfProfdRunner::LoggingConfig config;
579 config_reader.FillConfig(&config);
581 // Kick off encoder and check return code
582 PROFILE_RESULT result =
583 encode_to_proto(input_perf_data, encoded_file_path(dest_dir, 0).c_str(), config, 0);
584 EXPECT_EQ(OK_PROFILE_COLLECTION, result);
586 // Read and decode the resulting perf.data.encoded file
587 wireless_android_play_playlog::AndroidPerfProfile encodedProfile;
588 readEncodedProfile(dest_dir,
589 "BasicRunWithCannedPerf",
592 // Expect 45 programs
593 EXPECT_EQ(45, encodedProfile.programs_size());
595 // Check a couple of load modules
596 { const auto &lm0 = encodedProfile.load_modules(0);
597 std::string act_lm0 = encodedLoadModuleToString(lm0);
598 std::string sqact0 = squeezeWhite(act_lm0, "actual for lm 0");
599 const std::string expected_lm0 = RAW_RESULT(
600 name: "/data/app/com.google.android.apps.plus-1/lib/arm/libcronet.so"
602 std::string sqexp0 = squeezeWhite(expected_lm0, "expected_lm0");
603 EXPECT_STREQ(sqexp0.c_str(), sqact0.c_str());
605 { const auto &lm9 = encodedProfile.load_modules(9);
606 std::string act_lm9 = encodedLoadModuleToString(lm9);
607 std::string sqact9 = squeezeWhite(act_lm9, "actual for lm 9");
608 const std::string expected_lm9 = RAW_RESULT(
609 name: "/system/lib/libandroid_runtime.so" build_id: "8164ed7b3a8b8f5a220d027788922510"
611 std::string sqexp9 = squeezeWhite(expected_lm9, "expected_lm9");
612 EXPECT_STREQ(sqexp9.c_str(), sqact9.c_str());
615 // Examine some of the samples now
616 { const auto &p1 = encodedProfile.programs(0);
617 const auto &lm1 = p1.modules(0);
618 std::string act_lm1 = encodedModuleSamplesToString(lm1);
619 std::string sqact1 = squeezeWhite(act_lm1, "actual for lm1");
620 const std::string expected_lm1 = RAW_RESULT(
621 load_module_id: 9 address_samples { address: 296100 count: 1 }
623 std::string sqexp1 = squeezeWhite(expected_lm1, "expected_lm1");
624 EXPECT_STREQ(sqexp1.c_str(), sqact1.c_str());
626 { const auto &p1 = encodedProfile.programs(2);
627 const auto &lm2 = p1.modules(0);
628 std::string act_lm2 = encodedModuleSamplesToString(lm2);
629 std::string sqact2 = squeezeWhite(act_lm2, "actual for lm2");
630 const std::string expected_lm2 = RAW_RESULT(
632 address_samples { address: 28030244 count: 1 }
633 address_samples { address: 29657840 count: 1 }
635 std::string sqexp2 = squeezeWhite(expected_lm2, "expected_lm2");
636 EXPECT_STREQ(sqexp2.c_str(), sqact2.c_str());
640 TEST_F(PerfProfdTest, CallchainRunWithCannedPerf)
642 // This test makes sure that the perf.data converter
643 // can handle call chains.
645 std::string input_perf_data(test_dir);
646 input_perf_data += "/callchain.canned.perf.data";
648 // Set up config to avoid these annotations (they are tested elsewhere)
649 ConfigReader config_reader;
650 config_reader.overrideUnsignedEntry("collect_cpu_utilization", 0);
651 config_reader.overrideUnsignedEntry("collect_charging_state", 0);
652 config_reader.overrideUnsignedEntry("collect_camera_active", 0);
653 PerfProfdRunner::LoggingConfig config;
654 config_reader.FillConfig(&config);
656 // Kick off encoder and check return code
657 PROFILE_RESULT result =
658 encode_to_proto(input_perf_data, encoded_file_path(dest_dir, 0).c_str(), config, 0);
659 EXPECT_EQ(OK_PROFILE_COLLECTION, result);
661 // Read and decode the resulting perf.data.encoded file
662 wireless_android_play_playlog::AndroidPerfProfile encodedProfile;
663 readEncodedProfile(dest_dir,
664 "BasicRunWithCannedPerf",
668 // Expect 3 programs 8 load modules
669 EXPECT_EQ(3, encodedProfile.programs_size());
670 EXPECT_EQ(8, encodedProfile.load_modules_size());
672 // Check a couple of load modules
673 { const auto &lm0 = encodedProfile.load_modules(0);
674 std::string act_lm0 = encodedLoadModuleToString(lm0);
675 std::string sqact0 = squeezeWhite(act_lm0, "actual for lm 0");
676 const std::string expected_lm0 = RAW_RESULT(
677 name: "/system/bin/dex2oat"
678 build_id: "ee12bd1a1de39422d848f249add0afc4"
680 std::string sqexp0 = squeezeWhite(expected_lm0, "expected_lm0");
681 EXPECT_STREQ(sqexp0.c_str(), sqact0.c_str());
683 { const auto &lm1 = encodedProfile.load_modules(1);
684 std::string act_lm1 = encodedLoadModuleToString(lm1);
685 std::string sqact1 = squeezeWhite(act_lm1, "actual for lm 1");
686 const std::string expected_lm1 = RAW_RESULT(
687 name: "/system/bin/linker"
688 build_id: "a36715f673a4a0aa76ef290124c516cc"
690 std::string sqexp1 = squeezeWhite(expected_lm1, "expected_lm1");
691 EXPECT_STREQ(sqexp1.c_str(), sqact1.c_str());
694 // Examine some of the samples now
695 { const auto &p0 = encodedProfile.programs(0);
696 const auto &lm1 = p0.modules(0);
697 std::string act_lm1 = encodedModuleSamplesToString(lm1);
698 std::string sqact1 = squeezeWhite(act_lm1, "actual for lm1");
699 const std::string expected_lm1 = RAW_RESULT(
701 address_samples { address: 108552 count: 2 }
703 std::string sqexp1 = squeezeWhite(expected_lm1, "expected_lm1");
704 EXPECT_STREQ(sqexp1.c_str(), sqact1.c_str());
706 { const auto &p4 = encodedProfile.programs(2);
707 const auto &lm2 = p4.modules(1);
708 std::string act_lm2 = encodedModuleSamplesToString(lm2);
709 std::string sqact2 = squeezeWhite(act_lm2, "actual for lm2");
710 const std::string expected_lm2 = RAW_RESULT(
711 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 }
713 std::string sqexp2 = squeezeWhite(expected_lm2, "expected_lm2");
714 EXPECT_STREQ(sqexp2.c_str(), sqact2.c_str());
718 TEST_F(PerfProfdTest, BasicRunWithLivePerf)
721 // Basic test to exercise the main loop of the daemon. It includes
724 PerfProfdRunner runner(conf_dir);
725 runner.addToConfig("only_debug_build=0");
726 std::string ddparam("destination_directory="); ddparam += dest_dir;
727 runner.addToConfig(ddparam);
728 std::string cfparam("config_directory="); cfparam += conf_dir;
729 runner.addToConfig(cfparam);
730 runner.addToConfig("main_loop_iterations=1");
731 runner.addToConfig("use_fixed_seed=12345678");
732 runner.addToConfig("max_unprocessed_profiles=100");
733 runner.addToConfig("collection_interval=9999");
734 runner.addToConfig("sample_duration=2");
736 // Create semaphore file
737 runner.create_semaphore_file();
740 int daemon_main_return_code = runner.invoke();
742 // Check return code from daemon
743 EXPECT_EQ(0, daemon_main_return_code);
745 // Read and decode the resulting perf.data.encoded file
746 wireless_android_play_playlog::AndroidPerfProfile encodedProfile;
747 readEncodedProfile(dest_dir, "BasicRunWithLivePerf", encodedProfile);
749 // Examine what we get back. Since it's a live profile, we can't
750 // really do much in terms of verifying the contents.
751 EXPECT_LT(0, encodedProfile.programs_size());
753 // Verify log contents
754 const std::string expected = std::string(
755 "I: starting Android Wide Profiling daemon ") +
756 "I: config file path set to " + conf_dir + "/perfprofd.conf " +
758 I: random seed set to 12345678
760 I: initiating profile collection
762 I: profile collection complete
763 I: sleep 9325 seconds
764 I: finishing Android Wide Profiling daemon
766 // check to make sure log excerpt matches
767 compareLogMessages(mock_perfprofdutils_getlogged(),
768 expandVars(expected), "BasicRunWithLivePerf", true);
771 TEST_F(PerfProfdTest, MultipleRunWithLivePerf)
774 // Basic test to exercise the main loop of the daemon. It includes
777 PerfProfdRunner runner(conf_dir);
778 runner.addToConfig("only_debug_build=0");
779 std::string ddparam("destination_directory="); ddparam += dest_dir;
780 runner.addToConfig(ddparam);
781 std::string cfparam("config_directory="); cfparam += conf_dir;
782 runner.addToConfig(cfparam);
783 runner.addToConfig("main_loop_iterations=3");
784 runner.addToConfig("use_fixed_seed=12345678");
785 runner.addToConfig("collection_interval=9999");
786 runner.addToConfig("sample_duration=2");
787 runner.write_processed_file(1, 2);
789 // Create semaphore file
790 runner.create_semaphore_file();
793 int daemon_main_return_code = runner.invoke();
795 // Check return code from daemon
796 EXPECT_EQ(0, daemon_main_return_code);
798 // Read and decode the resulting perf.data.encoded file
799 wireless_android_play_playlog::AndroidPerfProfile encodedProfile;
800 readEncodedProfile(dest_dir, "BasicRunWithLivePerf", encodedProfile);
802 // Examine what we get back. Since it's a live profile, we can't
803 // really do much in terms of verifying the contents.
804 EXPECT_LT(0, encodedProfile.programs_size());
806 // Examine that encoded.1 file is removed while encoded.{0|2} exists.
807 EXPECT_EQ(0, access(encoded_file_path(dest_dir, 0).c_str(), F_OK));
808 EXPECT_NE(0, access(encoded_file_path(dest_dir, 1).c_str(), F_OK));
809 EXPECT_EQ(0, access(encoded_file_path(dest_dir, 2).c_str(), F_OK));
811 // Verify log contents
812 const std::string expected = std::string(
813 "I: starting Android Wide Profiling daemon ") +
814 "I: config file path set to " + conf_dir + "/perfprofd.conf " +
816 I: random seed set to 12345678
818 I: initiating profile collection
820 I: profile collection complete
821 I: sleep 9325 seconds
822 I: sleep 4974 seconds
823 I: initiating profile collection
825 I: profile collection complete
826 I: sleep 5025 seconds
828 I: initiating profile collection
830 I: profile collection complete
831 I: sleep 9498 seconds
832 I: finishing Android Wide Profiling daemon
834 // check to make sure log excerpt matches
835 compareLogMessages(mock_perfprofdutils_getlogged(),
836 expandVars(expected), "BasicRunWithLivePerf", true);
839 TEST_F(PerfProfdTest, CallChainRunWithLivePerf)
842 // Collect a callchain profile, so as to exercise the code in
843 // perf_data post-processing that digests callchains.
845 PerfProfdRunner runner(conf_dir);
846 std::string ddparam("destination_directory="); ddparam += dest_dir;
847 runner.addToConfig(ddparam);
848 std::string cfparam("config_directory="); cfparam += conf_dir;
849 runner.addToConfig(cfparam);
850 runner.addToConfig("main_loop_iterations=1");
851 runner.addToConfig("use_fixed_seed=12345678");
852 runner.addToConfig("max_unprocessed_profiles=100");
853 runner.addToConfig("collection_interval=9999");
854 runner.addToConfig("stack_profile=1");
855 runner.addToConfig("sample_duration=2");
857 // Create semaphore file
858 runner.create_semaphore_file();
861 int daemon_main_return_code = runner.invoke();
863 // Check return code from daemon
864 EXPECT_EQ(0, daemon_main_return_code);
866 // Read and decode the resulting perf.data.encoded file
867 wireless_android_play_playlog::AndroidPerfProfile encodedProfile;
868 readEncodedProfile(dest_dir, "CallChainRunWithLivePerf", encodedProfile);
870 // Examine what we get back. Since it's a live profile, we can't
871 // really do much in terms of verifying the contents.
872 EXPECT_LT(0, encodedProfile.programs_size());
874 // Verify log contents
875 const std::string expected = std::string(
876 "I: starting Android Wide Profiling daemon ") +
877 "I: config file path set to " + conf_dir + "/perfprofd.conf " +
879 I: random seed set to 12345678
881 I: initiating profile collection
883 I: profile collection complete
884 I: sleep 9325 seconds
885 I: finishing Android Wide Profiling daemon
887 // check to make sure log excerpt matches
888 compareLogMessages(mock_perfprofdutils_getlogged(),
889 expandVars(expected), "CallChainRunWithLivePerf", true);
892 int main(int argc, char **argv) {
893 // Always log to cerr, so that device failures are visible.
894 android::base::SetLogger(android::base::StderrLogger);
896 CHECK(android::base::Realpath(argv[0], &gExecutableRealpath));
898 // switch to / before starting testing (perfprofd
899 // should be location-independent)
901 testing::InitGoogleTest(&argc, argv);
902 return RUN_ALL_TESTS();