OSDN Git Service

Merge "Perfprofd: Add IsProfilingEnabled to Config"
[android-x86/system-extras.git] / perfprofd / tests / perfprofd_test.cc
1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <gtest/gtest.h>
18 #include <algorithm>
19 #include <cctype>
20 #include <string>
21 #include <regex>
22 #include <stdio.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26
27 #include <android-base/properties.h>
28 #include <android-base/stringprintf.h>
29
30 #include "config.h"
31 #include "configreader.h"
32 #include "perfprofdcore.h"
33 #include "perfprofdutils.h"
34 #include "perfprofdmockutils.h"
35
36 #include "perf_profile.pb.h"
37 #include "google/protobuf/text_format.h"
38
39 //
40 // Set to argv[0] on startup
41 //
42 static const char *executable_path;
43
44 //
45 // test_dir is the directory containing the test executable and
46 // any files associated with the test (will be created by the harness).
47 //
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.
51 //
52 static std::string test_dir;
53 static std::string dest_dir;
54
55 // Path to perf executable on device
56 #define PERFPATH "/system/bin/perf"
57
58 // Temporary config file that we will emit for the daemon to read
59 #define CONFIGFILE "perfprofd.conf"
60
61 static std::string encoded_file_path(int seq)
62 {
63   return android::base::StringPrintf("%s/perf.data.encoded.%d",
64                                      dest_dir.c_str(), seq);
65 }
66
67 class PerfProfdTest : public testing::Test {
68  protected:
69   virtual void SetUp() {
70     mock_perfprofdutils_init();
71     create_dest_dir();
72     yesclean();
73   }
74
75   virtual void TearDown() {
76     mock_perfprofdutils_finish();
77   }
78
79   void noclean() {
80     clean_ = false;
81   }
82   void yesclean() {
83     clean_ = true;
84   }
85
86  private:
87   bool clean_;
88
89   void create_dest_dir() {
90     setup_dirs();
91     ASSERT_FALSE(dest_dir == "");
92     if (clean_) {
93       std::string cmd("rm -rf ");
94       cmd += dest_dir;
95       system(cmd.c_str());
96     }
97     std::string cmd("mkdir -p ");
98     cmd += dest_dir;
99     system(cmd.c_str());
100   }
101
102   void setup_dirs()
103   {
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);
109       dest_dir = test_dir;
110       dest_dir += "/tmp";
111     }
112   }
113
114 };
115
116 static bool bothWhiteSpace(char lhs, char rhs)
117 {
118   return (std::isspace(lhs) && std::isspace(rhs));
119 }
120
121 //
122 // Squeeze out repeated whitespace from expected/actual logs.
123 //
124 static std::string squeezeWhite(const std::string &str,
125                                 const char *tag,
126                                 bool dump=false)
127 {
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())) {
134     result.pop_back();
135   }
136   if (dump) { fprintf(stderr, "squeezed %s is %s\n", tag, result.c_str()); }
137   return result;
138 }
139
140 //
141 // Replace all occurrences of a string with another string.
142 //
143 static std::string replaceAll(const std::string &str,
144                               const std::string &from,
145                               const std::string &to)
146 {
147   std::string ret = "";
148   size_t pos = 0;
149   while (pos < str.size()) {
150     size_t found = str.find(from, pos);
151     if (found == std::string::npos) {
152       ret += str.substr(pos);
153       break;
154     }
155     ret += str.substr(pos, found - pos) + to;
156     pos = found + from.size();
157   }
158   return ret;
159 }
160
161 //
162 // Replace occurrences of special variables in the string.
163 //
164 static std::string expandVars(const std::string &str) {
165 #ifdef __LP64__
166   return replaceAll(str, "$NATIVE_TESTS", "/data/nativetest64");
167 #else
168   return replaceAll(str, "$NATIVE_TESTS", "/data/nativetest");
169 #endif
170 }
171
172 ///
173 /// Helper class to kick off a run of the perfprofd daemon with a specific
174 /// config file.
175 ///
176 class PerfProfdRunner {
177  public:
178   PerfProfdRunner()
179       : config_path_(test_dir)
180   {
181     config_path_ += "/" CONFIGFILE;
182   }
183
184   ~PerfProfdRunner()
185   {
186     remove_processed_file();
187   }
188
189   void addToConfig(const std::string &line)
190   {
191     config_text_ += line;
192     config_text_ += "\n";
193   }
194
195   void remove_semaphore_file()
196   {
197     std::string semaphore(test_dir);
198     semaphore += "/" SEMAPHORE_FILENAME;
199     unlink(semaphore.c_str());
200   }
201
202   void create_semaphore_file()
203   {
204     std::string semaphore(test_dir);
205     semaphore += "/" SEMAPHORE_FILENAME;
206     close(open(semaphore.c_str(), O_WRONLY|O_CREAT, 0600));
207   }
208
209   void write_processed_file(int start_seq, int end_seq)
210   {
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);
215     }
216     fclose(fp);
217   }
218
219   void remove_processed_file()
220   {
221     std::string processed = test_dir + "/" PROCESSED_FILENAME;
222     unlink(processed.c_str());
223   }
224
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);
229     }
230
231     bool IsProfilingEnabled() const override {
232       //
233       // Check for existence of semaphore file in config directory
234       //
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));
238         return false;
239       }
240
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) {
245         return false;
246       }
247
248       return true;
249     }
250   };
251
252   int invoke()
253   {
254     static const char *argv[3] = { "perfprofd", "-c", "" };
255     argv[2] = config_path_.c_str();
256
257     writeConfigFile(config_path_, config_text_);
258
259     // execute daemon main
260     LoggingConfig config;
261     return perfprofd_main(3, (char **) argv, &config);
262   }
263
264  private:
265   std::string config_path_;
266   std::string config_text_;
267
268   void writeConfigFile(const std::string &config_path,
269                        const std::string &config_text)
270   {
271     FILE *fp = fopen(config_path.c_str(), "w");
272     ASSERT_TRUE(fp != nullptr);
273     fprintf(fp, "%s\n", config_text.c_str());
274     fclose(fp);
275   }
276 };
277
278 //......................................................................
279
280 static void readEncodedProfile(const char *testpoint,
281                                wireless_android_play_playlog::AndroidPerfProfile &encodedProfile)
282 {
283   struct stat statb;
284   int perf_data_stat_result = stat(encoded_file_path(0).c_str(), &statb);
285   ASSERT_NE(-1, perf_data_stat_result);
286
287   // read
288   std::string encoded;
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);
294   fclose(ifp);
295
296   // decode
297   encodedProfile.ParseFromString(encoded);
298 }
299
300 static std::string encodedLoadModuleToString(const wireless_android_play_playlog::LoadModule &lm)
301 {
302   std::stringstream ss;
303   ss << "name: \"" << lm.name() << "\"\n";
304   if (lm.build_id() != "") {
305     ss << "build_id: \"" << lm.build_id() << "\"\n";
306   }
307   return ss.str();
308 }
309
310 static std::string encodedModuleSamplesToString(const wireless_android_play_playlog::LoadModuleSamples &mod)
311 {
312   std::stringstream ss;
313
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();
319          l++) {
320       auto address = mod.address_samples(k).address(l);
321       ss << "    address: " << address << "\n";
322     }
323     ss << "    count: " << sample.count() << "\n";
324     ss << "  }\n";
325   }
326   return ss.str();
327 }
328
329 #define RAW_RESULT(x) #x
330
331 //
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)
336 //
337 static void compareLogMessages(const std::string &actual,
338                                const std::string &expected,
339                                const char *testpoint,
340                                bool exactMatch=false)
341 {
342    std::string sqexp = squeezeWhite(expected, "expected");
343    std::string sqact = squeezeWhite(actual, "actual");
344    if (exactMatch) {
345      EXPECT_STREQ(sqexp.c_str(), sqact.c_str());
346    } else {
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";
353        wasFound = false;
354      }
355      EXPECT_TRUE(wasFound);
356    }
357 }
358
359 TEST_F(PerfProfdTest, TestUtil)
360 {
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"));
366 }
367
368 TEST_F(PerfProfdTest, MissingGMS)
369 {
370   //
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.
376   //
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");
384
385   // Kick off daemon
386   int daemon_main_return_code = runner.invoke();
387
388   // Check return code from daemon
389   EXPECT_EQ(0, daemon_main_return_code);
390
391   // Verify log contents
392   const std::string expected = RAW_RESULT(
393       I: sleep 90 seconds
394       W: unable to open config directory /does/not/exist: (No such file or directory)
395       I: profile collection skipped (missing config directory)
396                                           );
397
398   // check to make sure entire log matches
399   compareLogMessages(mock_perfprofdutils_getlogged(),
400                      expected, "MissingGMS");
401 }
402
403
404 TEST_F(PerfProfdTest, MissingOptInSemaphoreFile)
405 {
406   //
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
411   // up on.
412   //
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");
422
423   runner.remove_semaphore_file();
424
425   // Kick off daemon
426   int daemon_main_return_code = runner.invoke();
427
428   // Check return code from daemon
429   EXPECT_EQ(0, daemon_main_return_code);
430
431   // Verify log contents
432   const std::string expected = RAW_RESULT(
433       I: profile collection skipped (missing semaphore file)
434                                           );
435   // check to make sure log excerpt matches
436   compareLogMessages(mock_perfprofdutils_getlogged(),
437                      expected, "MissingOptInSemaphoreFile");
438 }
439
440 TEST_F(PerfProfdTest, MissingPerfExecutable)
441 {
442   //
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.
447   //
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");
459
460   // Create semaphore file
461   runner.create_semaphore_file();
462
463   // Kick off daemon
464   int daemon_main_return_code = runner.invoke();
465
466   // Check return code from daemon
467   EXPECT_EQ(0, daemon_main_return_code);
468
469   // expected log contents
470   const std::string expected = RAW_RESULT(
471       I: profile collection skipped (missing 'perf' executable)
472                                           );
473   // check to make sure log excerpt matches
474   compareLogMessages(mock_perfprofdutils_getlogged(),
475                      expected, "MissingPerfExecutable");
476 }
477
478 TEST_F(PerfProfdTest, BadPerfRun)
479 {
480   //
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
484   // the error.
485   //
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");
496
497   // Create semaphore file
498   runner.create_semaphore_file();
499
500   // Kick off daemon
501   int daemon_main_return_code = runner.invoke();
502
503   // Check return code from daemon
504   EXPECT_EQ(0, daemon_main_return_code);
505
506   // Verify log contents
507   const std::string expected = RAW_RESULT(
508       I: profile collection failed (perf record returned bad exit status)
509                                           );
510
511   // check to make sure log excerpt matches
512   compareLogMessages(mock_perfprofdutils_getlogged(),
513                      expected, "BadPerfRun");
514 }
515
516 TEST_F(PerfProfdTest, ConfigFileParsing)
517 {
518   //
519   // Gracefully handly malformed items in the config file
520   //
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");
527
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");
534
535   // Kick off daemon
536   int daemon_main_return_code = runner.invoke();
537
538   // Check return code from daemon
539   EXPECT_EQ(0, daemon_main_return_code);
540
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)
548                                           );
549
550   // check to make sure log excerpt matches
551   compareLogMessages(mock_perfprofdutils_getlogged(),
552                      expected, "ConfigFileParsing");
553 }
554
555 TEST_F(PerfProfdTest, ProfileCollectionAnnotations)
556 {
557   unsigned util1 = collect_cpu_utilization();
558   EXPECT_LE(util1, 100);
559   EXPECT_GE(util1, 0);
560
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
563   // active.
564   EXPECT_FALSE(get_booting());
565   EXPECT_TRUE(get_charging());
566   EXPECT_FALSE(get_camera_active());
567 }
568
569 TEST_F(PerfProfdTest, BasicRunWithCannedPerf)
570 {
571   //
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.
576   //
577   std::string input_perf_data(test_dir);
578   input_perf_data += "/canned.perf.data";
579
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);
587
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);
592
593   // Read and decode the resulting perf.data.encoded file
594   wireless_android_play_playlog::AndroidPerfProfile encodedProfile;
595   readEncodedProfile("BasicRunWithCannedPerf",
596                      encodedProfile);
597
598   // Expect 45 programs
599   EXPECT_EQ(45, encodedProfile.programs_size());
600
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"
607                                                 );
608     std::string sqexp0 = squeezeWhite(expected_lm0, "expected_lm0");
609     EXPECT_STREQ(sqexp0.c_str(), sqact0.c_str());
610   }
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"
616                                                 );
617     std::string sqexp9 = squeezeWhite(expected_lm9, "expected_lm9");
618     EXPECT_STREQ(sqexp9.c_str(), sqact9.c_str());
619   }
620
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 }
628                                                 );
629     std::string sqexp1 = squeezeWhite(expected_lm1, "expected_lm1");
630     EXPECT_STREQ(sqexp1.c_str(), sqact1.c_str());
631   }
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(
637         load_module_id: 2
638         address_samples { address: 28030244 count: 1 }
639         address_samples { address: 29657840 count: 1 }
640                                                 );
641     std::string sqexp2 = squeezeWhite(expected_lm2, "expected_lm2");
642     EXPECT_STREQ(sqexp2.c_str(), sqact2.c_str());
643   }
644 }
645
646 TEST_F(PerfProfdTest, CallchainRunWithCannedPerf)
647 {
648   // This test makes sure that the perf.data converter
649   // can handle call chains.
650   //
651   std::string input_perf_data(test_dir);
652   input_perf_data += "/callchain.canned.perf.data";
653
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);
661
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);
666
667   // Read and decode the resulting perf.data.encoded file
668   wireless_android_play_playlog::AndroidPerfProfile encodedProfile;
669   readEncodedProfile("BasicRunWithCannedPerf",
670                      encodedProfile);
671
672
673   // Expect 3 programs 8 load modules
674   EXPECT_EQ(3, encodedProfile.programs_size());
675   EXPECT_EQ(8, encodedProfile.load_modules_size());
676
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"
684                                                 );
685     std::string sqexp0 = squeezeWhite(expected_lm0, "expected_lm0");
686     EXPECT_STREQ(sqexp0.c_str(), sqact0.c_str());
687   }
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"
694                                                 );
695     std::string sqexp1 = squeezeWhite(expected_lm1, "expected_lm1");
696     EXPECT_STREQ(sqexp1.c_str(), sqact1.c_str());
697   }
698
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(
705         load_module_id: 0
706         address_samples { address: 108552 count: 2 }
707                                                 );
708     std::string sqexp1 = squeezeWhite(expected_lm1, "expected_lm1");
709     EXPECT_STREQ(sqexp1.c_str(), sqact1.c_str());
710   }
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 }
717                                                 );
718     std::string sqexp2 = squeezeWhite(expected_lm2, "expected_lm2");
719     EXPECT_STREQ(sqexp2.c_str(), sqact2.c_str());
720   }
721 }
722
723 TEST_F(PerfProfdTest, BasicRunWithLivePerf)
724 {
725   //
726   // Basic test to exercise the main loop of the daemon. It includes
727   // a live 'perf' run
728   //
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");
740
741   // Create semaphore file
742   runner.create_semaphore_file();
743
744   // Kick off daemon
745   int daemon_main_return_code = runner.invoke();
746
747   // Check return code from daemon
748   EXPECT_EQ(0, daemon_main_return_code);
749
750   // Read and decode the resulting perf.data.encoded file
751   wireless_android_play_playlog::AndroidPerfProfile encodedProfile;
752   readEncodedProfile("BasicRunWithLivePerf", encodedProfile);
753
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());
757
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
763       I: sleep 674 seconds
764       I: initiating profile collection
765       I: profile collection complete
766       I: sleep 9325 seconds
767       I: finishing Android Wide Profiling daemon
768                                           );
769   // check to make sure log excerpt matches
770   compareLogMessages(mock_perfprofdutils_getlogged(),
771                      expandVars(expected), "BasicRunWithLivePerf", true);
772 }
773
774 TEST_F(PerfProfdTest, MultipleRunWithLivePerf)
775 {
776   //
777   // Basic test to exercise the main loop of the daemon. It includes
778   // a live 'perf' run
779   //
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);
791
792   // Create semaphore file
793   runner.create_semaphore_file();
794
795   // Kick off daemon
796   int daemon_main_return_code = runner.invoke();
797
798   // Check return code from daemon
799   EXPECT_EQ(0, daemon_main_return_code);
800
801   // Read and decode the resulting perf.data.encoded file
802   wireless_android_play_playlog::AndroidPerfProfile encodedProfile;
803   readEncodedProfile("BasicRunWithLivePerf", encodedProfile);
804
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());
808
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));
813
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
819       I: sleep 674 seconds
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
827       I: sleep 501 seconds
828       I: initiating profile collection
829       I: profile collection complete
830       I: sleep 9498 seconds
831       I: finishing Android Wide Profiling daemon
832                                           );
833   // check to make sure log excerpt matches
834   compareLogMessages(mock_perfprofdutils_getlogged(),
835                      expandVars(expected), "BasicRunWithLivePerf", true);
836 }
837
838 TEST_F(PerfProfdTest, CallChainRunWithLivePerf)
839 {
840   //
841   // Collect a callchain profile, so as to exercise the code in
842   // perf_data post-processing that digests callchains.
843   //
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");
855
856   // Create semaphore file
857   runner.create_semaphore_file();
858
859   // Kick off daemon
860   int daemon_main_return_code = runner.invoke();
861
862   // Check return code from daemon
863   EXPECT_EQ(0, daemon_main_return_code);
864
865   // Read and decode the resulting perf.data.encoded file
866   wireless_android_play_playlog::AndroidPerfProfile encodedProfile;
867   readEncodedProfile("CallChainRunWithLivePerf", encodedProfile);
868
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());
872
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
878       I: sleep 674 seconds
879       I: initiating profile collection
880       I: profile collection complete
881       I: sleep 9325 seconds
882       I: finishing Android Wide Profiling daemon
883                                           );
884   // check to make sure log excerpt matches
885   compareLogMessages(mock_perfprofdutils_getlogged(),
886                      expandVars(expected), "CallChainRunWithLivePerf", true);
887 }
888
889 int main(int argc, char **argv) {
890   executable_path = argv[0];
891   // switch to / before starting testing (perfprofd
892   // should be location-independent)
893   chdir("/");
894   testing::InitGoogleTest(&argc, argv);
895   return RUN_ALL_TESTS();
896 }