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,
221 bool debugDump=false)
224 int perf_data_stat_result = stat(encoded_file_path().c_str(), &statb);
225 ASSERT_NE(-1, perf_data_stat_result);
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);
237 encodedProfile.ParseFromString(encoded);
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");
245 fwrite(textdump.c_str(), textdump.size(), 1, ofp);
251 #define RAW_RESULT(x) #x
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)
259 static void compareLogMessages(const std::string &actual,
260 const std::string &expected,
261 const char *testpoint,
262 bool exactMatch=false)
264 std::string sqexp = squeezeWhite(expected, "expected");
265 std::string sqact = squeezeWhite(actual, "actual");
267 EXPECT_STREQ(sqexp.c_str(), sqact.c_str());
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";
277 EXPECT_TRUE(wasFound);
281 TEST_F(PerfProfdTest, MissingGMS)
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.
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");
299 int daemon_main_return_code = runner.invoke();
301 // Check return code from daemon
302 EXPECT_EQ(0, daemon_main_return_code);
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
314 W: unable to open destination directory /does/not/exist: (No such file or directory)
315 I: profile collection skipped (missing destination directory)
317 I: finishing Android Wide Profiling daemon
320 // check to make sure entire log matches
321 bool compareEntireLog = true;
322 compareLogMessages(mock_perfprofdutils_getlogged(),
323 expected, "MissingGMS", compareEntireLog);
326 TEST_F(PerfProfdTest, MissingOptInSemaphoreFile)
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
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");
343 runner.remove_semaphore_file();
346 int daemon_main_return_code = runner.invoke();
348 // Check return code from daemon
349 EXPECT_EQ(0, daemon_main_return_code);
351 // Verify log contents
352 const std::string expected = RAW_RESULT(
353 I: profile collection skipped (missing semaphore file)
355 // check to make sure log excerpt matches
356 compareLogMessages(mock_perfprofdutils_getlogged(),
357 expected, "MissingOptInSemaphoreFile");
360 TEST_F(PerfProfdTest, MissingPerfExecutable)
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.
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");
378 // Create semaphore file
379 runner.create_semaphore_file();
382 int daemon_main_return_code = runner.invoke();
384 // Check return code from daemon
385 EXPECT_EQ(0, daemon_main_return_code);
387 // expected log contents
388 const std::string expected = RAW_RESULT(
389 I: profile collection skipped (missing 'perf' executable)
391 // check to make sure log excerpt matches
392 compareLogMessages(mock_perfprofdutils_getlogged(),
393 expected, "MissingPerfExecutable");
396 TEST_F(PerfProfdTest, BadPerfRun)
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
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");
413 // Create semaphore file
414 runner.create_semaphore_file();
417 int daemon_main_return_code = runner.invoke();
419 // Check return code from daemon
420 EXPECT_EQ(0, daemon_main_return_code);
422 // Verify log contents
423 const std::string expected = RAW_RESULT(
424 I: profile collection failed (perf record returned bad exit status)
427 // check to make sure log excerpt matches
428 compareLogMessages(mock_perfprofdutils_getlogged(),
429 expected, "BadPerfRun");
432 TEST_F(PerfProfdTest, ConfigFileParsing)
435 // Gracefully handly malformed items in the config file
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");
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");
452 int daemon_main_return_code = runner.invoke();
454 // Check return code from daemon
455 EXPECT_EQ(0, daemon_main_return_code);
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)
466 // check to make sure log excerpt matches
467 compareLogMessages(mock_perfprofdutils_getlogged(),
468 expected, "ConfigFileParsing");
471 TEST_F(PerfProfdTest, AuxiliaryConfigFile)
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.
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);
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");
496 runner.remove_semaphore_file();
499 int daemon_main_return_code = runner.invoke();
501 // Check return code from daemon
502 EXPECT_EQ(0, daemon_main_return_code);
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
514 I: reading auxiliary config file /data/nativetest/perfprofd_test/tmp/perfprofd.conf
515 I: option main_loop_iterations set to 1
518 // check to make sure log excerpt matches
519 compareLogMessages(mock_perfprofdutils_getlogged(),
520 expected, "AuxiliaryConfigFile");
523 TEST_F(PerfProfdTest, BasicRunWithCannedPerf)
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.
531 std::string input_perf_data(test_dir);
532 input_perf_data += "/canned.perf.data";
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);
539 // Read and decode the resulting perf.data.encoded file
540 wireless_android_play_playlog::AndroidPerfProfile encodedProfile;
541 readEncodedProfile("BasicRunWithCannedPerf",
544 // Expect 29 load modules
545 EXPECT_EQ(29, encodedProfile.programs_size());
547 // Check a couple of load modules
548 { const auto &lm0 = encodedProfile.load_modules(0);
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"
555 std::string sqexp0 = squeezeWhite(expected_lm0, "expected_lm0");
556 EXPECT_STREQ(sqexp0.c_str(), sqact0.c_str());
558 { const auto &lm9 = encodedProfile.load_modules(9);
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"
565 std::string sqexp9 = squeezeWhite(expected_lm9, "expected_lm9");
566 EXPECT_STREQ(sqexp9.c_str(), sqact9.c_str());
569 // Examine some of the samples now
570 { const auto &p1 = encodedProfile.programs(0);
571 const auto &lm1 = p1.modules(0);
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 }
578 std::string sqexp1 = squeezeWhite(expected_lm1, "expected_lm1");
579 EXPECT_STREQ(sqexp1.c_str(), sqact1.c_str());
581 { const auto &p1 = encodedProfile.programs(2);
582 const auto &lm2 = p1.modules(0);
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(
588 address_samples { address: 28030244 count: 1 }
589 address_samples { address: 29657840 count: 1 }
591 std::string sqexp2 = squeezeWhite(expected_lm2, "expected_lm2");
592 EXPECT_STREQ(sqexp2.c_str(), sqact2.c_str());
596 TEST_F(PerfProfdTest, BasicRunWithLivePerf)
599 // Basic test to exercise the main loop of the daemon. It includes
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");
609 runner.addToConfig("sample_duration=5");
611 // Create semaphore file
612 runner.create_semaphore_file();
615 int daemon_main_return_code = runner.invoke();
617 // Check return code from daemon
618 EXPECT_EQ(0, daemon_main_return_code);
620 // Read and decode the resulting perf.data.encoded file
621 wireless_android_play_playlog::AndroidPerfProfile encodedProfile;
622 readEncodedProfile("BasicRunWithLivePerf", encodedProfile);
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());
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
634 I: initiating profile collection
635 I: profile collection complete
636 I: sleep 9325 seconds
637 I: finishing Android Wide Profiling daemon
639 // check to make sure log excerpt matches
640 compareLogMessages(mock_perfprofdutils_getlogged(),
641 expected, "BasicRunWithLivePerf", true);
644 int main(int argc, char **argv) {
645 executable_path = argv[0];
646 // switch to / before starting testing (perfprofd
647 // should be location-independent)
649 testing::InitGoogleTest(&argc, argv);
650 return RUN_ALL_TESTS();