OSDN Git Service

Remove unused function declarations
[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/stringprintf.h>
28 #include <cutils/properties.h>
29
30 #include "perfprofdcore.h"
31 #include "configreader.h"
32 #include "perfprofdutils.h"
33 #include "perfprofdmockutils.h"
34
35 #include "perf_profile.pb.h"
36 #include "google/protobuf/text_format.h"
37
38 //
39 // Set to argv[0] on startup
40 //
41 static const char *executable_path;
42
43 //
44 // test_dir is the directory containing the test executable and
45 // any files associated with the test (will be created by the harness).
46 //
47 // dest_dir is a subdirectory of test_dir that we'll create on the fly
48 // at the start of each testpoint (into which new files can be written),
49 // then delete at end of testpoint.
50 //
51 static std::string test_dir;
52 static std::string dest_dir;
53
54 // Path to perf executable on device
55 #define PERFPATH "/system/bin/perf"
56
57 // Temporary config file that we will emit for the daemon to read
58 #define CONFIGFILE "perfprofd.conf"
59
60 static std::string encoded_file_path(int seq)
61 {
62   return android::base::StringPrintf("%s/perf.data.encoded.%d",
63                                      dest_dir.c_str(), seq);
64 }
65
66 class PerfProfdTest : public testing::Test {
67  protected:
68   virtual void SetUp() {
69     mock_perfprofdutils_init();
70     create_dest_dir();
71     yesclean();
72   }
73
74   virtual void TearDown() {
75     mock_perfprofdutils_finish();
76   }
77
78   void noclean() {
79     clean_ = false;
80   }
81   void yesclean() {
82     clean_ = true;
83   }
84
85  private:
86   bool clean_;
87
88   void create_dest_dir() {
89     setup_dirs();
90     ASSERT_FALSE(dest_dir == "");
91     if (clean_) {
92       std::string cmd("rm -rf ");
93       cmd += dest_dir;
94       system(cmd.c_str());
95     }
96     std::string cmd("mkdir -p ");
97     cmd += dest_dir;
98     system(cmd.c_str());
99   }
100
101   void setup_dirs()
102   {
103     if (test_dir == "") {
104       ASSERT_TRUE(executable_path != nullptr);
105       std::string s(executable_path);
106       auto found = s.find_last_of('/');
107       test_dir = s.substr(0,found);
108       dest_dir = test_dir;
109       dest_dir += "/tmp";
110     }
111   }
112
113 };
114
115 static bool bothWhiteSpace(char lhs, char rhs)
116 {
117   return (std::isspace(lhs) && std::isspace(rhs));
118 }
119
120 //
121 // Squeeze out repeated whitespace from expected/actual logs.
122 //
123 static std::string squeezeWhite(const std::string &str,
124                                 const char *tag,
125                                 bool dump=false)
126 {
127   if (dump) { fprintf(stderr, "raw %s is %s\n", tag, str.c_str()); }
128   std::string result(str);
129   std::replace( result.begin(), result.end(), '\n', ' ');
130   auto new_end = std::unique(result.begin(), result.end(), bothWhiteSpace);
131   result.erase(new_end, result.end());
132   while (result.begin() != result.end() && std::isspace(*result.rbegin())) {
133     result.pop_back();
134   }
135   if (dump) { fprintf(stderr, "squeezed %s is %s\n", tag, result.c_str()); }
136   return result;
137 }
138
139 ///
140 /// Helper class to kick off a run of the perfprofd daemon with a specific
141 /// config file.
142 ///
143 class PerfProfdRunner {
144  public:
145   PerfProfdRunner()
146       : config_path_(test_dir)
147   {
148     config_path_ += "/" CONFIGFILE;
149   }
150
151   ~PerfProfdRunner()
152   {
153     remove_processed_file();
154   }
155
156   void addToConfig(const std::string &line)
157   {
158     config_text_ += line;
159     config_text_ += "\n";
160   }
161
162   void remove_semaphore_file()
163   {
164     std::string semaphore(test_dir);
165     semaphore += "/" SEMAPHORE_FILENAME;
166     unlink(semaphore.c_str());
167   }
168
169   void create_semaphore_file()
170   {
171     std::string semaphore(test_dir);
172     semaphore += "/" SEMAPHORE_FILENAME;
173     close(open(semaphore.c_str(), O_WRONLY|O_CREAT));
174   }
175
176   void write_processed_file(int start_seq, int end_seq)
177   {
178     std::string processed = test_dir + "/" PROCESSED_FILENAME;
179     FILE *fp = fopen(processed.c_str(), "w");
180     for (int i = start_seq; i < end_seq; i++) {
181       fprintf(fp, "%d\n", i);
182     }
183     fclose(fp);
184   }
185
186   void remove_processed_file()
187   {
188     std::string processed = test_dir + "/" PROCESSED_FILENAME;
189     unlink(processed.c_str());
190   }
191
192   int invoke()
193   {
194     static const char *argv[3] = { "perfprofd", "-c", "" };
195     argv[2] = config_path_.c_str();
196
197     writeConfigFile(config_path_, config_text_);
198
199     // execute daemon main
200     return perfprofd_main(3, (char **) argv);
201   }
202
203  private:
204   std::string config_path_;
205   std::string config_text_;
206
207   void writeConfigFile(const std::string &config_path,
208                        const std::string &config_text)
209   {
210     FILE *fp = fopen(config_path.c_str(), "w");
211     ASSERT_TRUE(fp != nullptr);
212     fprintf(fp, "%s\n", config_text.c_str());
213     fclose(fp);
214   }
215 };
216
217 //......................................................................
218
219 static void readEncodedProfile(const char *testpoint,
220                                wireless_android_play_playlog::AndroidPerfProfile &encodedProfile)
221 {
222   struct stat statb;
223   int perf_data_stat_result = stat(encoded_file_path(0).c_str(), &statb);
224   ASSERT_NE(-1, perf_data_stat_result);
225
226   // read
227   std::string encoded;
228   encoded.resize(statb.st_size);
229   FILE *ifp = fopen(encoded_file_path(0).c_str(), "r");
230   ASSERT_NE(nullptr, ifp);
231   size_t items_read = fread((void*) encoded.data(), statb.st_size, 1, ifp);
232   ASSERT_EQ(1, items_read);
233   fclose(ifp);
234
235   // decode
236   encodedProfile.ParseFromString(encoded);
237 }
238
239 static std::string encodedLoadModuleToString(const wireless_android_play_playlog::LoadModule &lm)
240 {
241   std::stringstream ss;
242   ss << "name: \"" << lm.name() << "\"\n";
243   if (lm.build_id() != "") {
244     ss << "build_id: \"" << lm.build_id() << "\"\n";
245   }
246   return ss.str();
247 }
248
249 static std::string encodedModuleSamplesToString(const wireless_android_play_playlog::LoadModuleSamples &mod)
250 {
251   std::stringstream ss;
252
253   ss << "load_module_id: " << mod.load_module_id() << "\n";
254   for (size_t k = 0; k < mod.address_samples_size(); k++) {
255     const auto &sample = mod.address_samples(k);
256     ss << "  address_samples {\n";
257     for (size_t l = 0; l < mod.address_samples(k).address_size();
258          l++) {
259       auto address = mod.address_samples(k).address(l);
260       ss << "    address: " << address << "\n";
261     }
262     ss << "    count: " << sample.count() << "\n";
263     ss << "  }\n";
264   }
265   return ss.str();
266 }
267
268 #define RAW_RESULT(x) #x
269
270 //
271 // Check to see if the log messages emitted by the daemon
272 // match the expected result. By default we use a partial
273 // match, e.g. if we see the expected excerpt anywhere in the
274 // result, it's a match (for exact match, set exact to true)
275 //
276 static void compareLogMessages(const std::string &actual,
277                                const std::string &expected,
278                                const char *testpoint,
279                                bool exactMatch=false)
280 {
281    std::string sqexp = squeezeWhite(expected, "expected");
282    std::string sqact = squeezeWhite(actual, "actual");
283    if (exactMatch) {
284      EXPECT_STREQ(sqexp.c_str(), sqact.c_str());
285    } else {
286      std::size_t foundpos = sqact.find(sqexp);
287      bool wasFound = true;
288      if (foundpos == std::string::npos) {
289        std::cerr << testpoint << ": expected result not found\n";
290        std::cerr << " Actual: \"" << sqact << "\"\n";
291        std::cerr << " Expected: \"" << sqexp << "\"\n";
292        wasFound = false;
293      }
294      EXPECT_TRUE(wasFound);
295    }
296 }
297
298 TEST_F(PerfProfdTest, MissingGMS)
299 {
300   //
301   // AWP requires cooperation between the daemon and the GMS core
302   // piece. If we're running on a device that has an old or damaged
303   // version of GMS core, then the config directory we're interested in
304   // may not be there. This test insures that the daemon does the
305   // right thing in this case.
306   //
307   PerfProfdRunner runner;
308   runner.addToConfig("only_debug_build=0");
309   runner.addToConfig("trace_config_read=0");
310   runner.addToConfig("config_directory=/does/not/exist");
311   runner.addToConfig("main_loop_iterations=1");
312   runner.addToConfig("use_fixed_seed=1");
313   runner.addToConfig("collection_interval=100");
314
315   // Kick off daemon
316   int daemon_main_return_code = runner.invoke();
317
318   // Check return code from daemon
319   EXPECT_EQ(0, daemon_main_return_code);
320
321   // Verify log contents
322   const std::string expected = RAW_RESULT(
323       I: sleep 90 seconds
324       W: unable to open config directory /does/not/exist: (No such file or directory)
325       I: profile collection skipped (missing config directory)
326                                           );
327
328   // check to make sure entire log matches
329   compareLogMessages(mock_perfprofdutils_getlogged(),
330                      expected, "MissingGMS");
331 }
332
333
334 TEST_F(PerfProfdTest, MissingOptInSemaphoreFile)
335 {
336   //
337   // Android device owners must opt in to "collect and report usage
338   // data" in order for us to be able to collect profiles. The opt-in
339   // check is performed in the GMS core component; if the check
340   // passes, then it creates a semaphore file for the daemon to pick
341   // up on.
342   //
343   PerfProfdRunner runner;
344   runner.addToConfig("only_debug_build=0");
345   std::string cfparam("config_directory="); cfparam += test_dir;
346   runner.addToConfig(cfparam);
347   std::string ddparam("destination_directory="); ddparam += dest_dir;
348   runner.addToConfig(ddparam);
349   runner.addToConfig("main_loop_iterations=1");
350   runner.addToConfig("use_fixed_seed=1");
351   runner.addToConfig("collection_interval=100");
352
353   runner.remove_semaphore_file();
354
355   // Kick off daemon
356   int daemon_main_return_code = runner.invoke();
357
358   // Check return code from daemon
359   EXPECT_EQ(0, daemon_main_return_code);
360
361   // Verify log contents
362   const std::string expected = RAW_RESULT(
363       I: profile collection skipped (missing semaphore file)
364                                           );
365   // check to make sure log excerpt matches
366   compareLogMessages(mock_perfprofdutils_getlogged(),
367                      expected, "MissingOptInSemaphoreFile");
368 }
369
370 TEST_F(PerfProfdTest, MissingPerfExecutable)
371 {
372   //
373   // Perfprofd uses the 'simpleperf' tool to collect profiles
374   // (although this may conceivably change in the future). This test
375   // checks to make sure that if 'simpleperf' is not present we bail out
376   // from collecting profiles.
377   //
378   PerfProfdRunner runner;
379   runner.addToConfig("only_debug_build=0");
380   runner.addToConfig("trace_config_read=1");
381   std::string cfparam("config_directory="); cfparam += test_dir;
382   runner.addToConfig(cfparam);
383   std::string ddparam("destination_directory="); ddparam += dest_dir;
384   runner.addToConfig(ddparam);
385   runner.addToConfig("main_loop_iterations=1");
386   runner.addToConfig("use_fixed_seed=1");
387   runner.addToConfig("collection_interval=100");
388   runner.addToConfig("perf_path=/does/not/exist");
389
390   // Create semaphore file
391   runner.create_semaphore_file();
392
393   // Kick off daemon
394   int daemon_main_return_code = runner.invoke();
395
396   // Check return code from daemon
397   EXPECT_EQ(0, daemon_main_return_code);
398
399   // expected log contents
400   const std::string expected = RAW_RESULT(
401       I: profile collection skipped (missing 'perf' executable)
402                                           );
403   // check to make sure log excerpt matches
404   compareLogMessages(mock_perfprofdutils_getlogged(),
405                      expected, "MissingPerfExecutable");
406 }
407
408 TEST_F(PerfProfdTest, BadPerfRun)
409 {
410   //
411   // Perf tools tend to be tightly coupled with a specific kernel
412   // version -- if things are out of sync perf could fail or
413   // crash. This test makes sure that we detect such a case and log
414   // the error.
415   //
416   PerfProfdRunner runner;
417   runner.addToConfig("only_debug_build=0");
418   std::string cfparam("config_directory="); cfparam += test_dir;
419   runner.addToConfig(cfparam);
420   std::string ddparam("destination_directory="); ddparam += dest_dir;
421   runner.addToConfig(ddparam);
422   runner.addToConfig("main_loop_iterations=1");
423   runner.addToConfig("use_fixed_seed=1");
424   runner.addToConfig("collection_interval=100");
425   runner.addToConfig("perf_path=/system/bin/false");
426
427   // Create semaphore file
428   runner.create_semaphore_file();
429
430   // Kick off daemon
431   int daemon_main_return_code = runner.invoke();
432
433   // Check return code from daemon
434   EXPECT_EQ(0, daemon_main_return_code);
435
436   // Verify log contents
437   const std::string expected = RAW_RESULT(
438       I: profile collection failed (perf record returned bad exit status)
439                                           );
440
441   // check to make sure log excerpt matches
442   compareLogMessages(mock_perfprofdutils_getlogged(),
443                      expected, "BadPerfRun");
444 }
445
446 TEST_F(PerfProfdTest, ConfigFileParsing)
447 {
448   //
449   // Gracefully handly malformed items in the config file
450   //
451   PerfProfdRunner runner;
452   runner.addToConfig("only_debug_build=0");
453   runner.addToConfig("main_loop_iterations=1");
454   runner.addToConfig("collection_interval=100");
455   runner.addToConfig("use_fixed_seed=1");
456   runner.addToConfig("destination_directory=/does/not/exist");
457
458   // assorted bad syntax
459   runner.addToConfig("collection_interval=0");
460   runner.addToConfig("collection_interval=-1");
461   runner.addToConfig("collection_interval=2");
462   runner.addToConfig("nonexistent_key=something");
463   runner.addToConfig("no_equals_stmt");
464
465   // Kick off daemon
466   int daemon_main_return_code = runner.invoke();
467
468   // Check return code from daemon
469   EXPECT_EQ(0, daemon_main_return_code);
470
471   // Verify log contents
472   const std::string expected = RAW_RESULT(
473       W: line 6: specified value 0 for 'collection_interval' outside permitted range [100 4294967295] (ignored)
474       W: line 7: malformed unsigned value (ignored)
475       W: line 8: specified value 2 for 'collection_interval' outside permitted range [100 4294967295] (ignored)
476       W: line 9: unknown option 'nonexistent_key' ignored
477       W: line 10: line malformed (no '=' found)
478                                           );
479
480   // check to make sure log excerpt matches
481   compareLogMessages(mock_perfprofdutils_getlogged(),
482                      expected, "ConfigFileParsing");
483 }
484
485 TEST_F(PerfProfdTest, ProfileCollectionAnnotations)
486 {
487   unsigned util1 = collect_cpu_utilization();
488   EXPECT_LE(util1, 100);
489   EXPECT_GE(util1, 0);
490
491   // NB: expectation is that when we run this test, the device will be
492   // completed booted, will be on charger, and will not have the camera
493   // active.
494   EXPECT_FALSE(get_booting());
495   EXPECT_TRUE(get_charging());
496   EXPECT_FALSE(get_camera_active());
497 }
498
499 TEST_F(PerfProfdTest, BasicRunWithCannedPerf)
500 {
501   //
502   // Verify the portion of the daemon that reads and encodes
503   // perf.data files. Here we run the encoder on a canned perf.data
504   // file and verify that the resulting protobuf contains what
505   // we think it should contain.
506   //
507   std::string input_perf_data(test_dir);
508   input_perf_data += "/canned.perf.data";
509
510   // Set up config to avoid these annotations (they are tested elsewhere)
511   ConfigReader config;
512   config.overrideUnsignedEntry("collect_cpu_utilization", 0);
513   config.overrideUnsignedEntry("collect_charging_state", 0);
514   config.overrideUnsignedEntry("collect_camera_active", 0);
515
516   // Kick off encoder and check return code
517   PROFILE_RESULT result =
518       encode_to_proto(input_perf_data, encoded_file_path(0).c_str(), config, 0);
519   EXPECT_EQ(OK_PROFILE_COLLECTION, result);
520
521   // Read and decode the resulting perf.data.encoded file
522   wireless_android_play_playlog::AndroidPerfProfile encodedProfile;
523   readEncodedProfile("BasicRunWithCannedPerf",
524                      encodedProfile);
525
526   // Expect 29 load modules
527   EXPECT_EQ(29, encodedProfile.programs_size());
528
529   // Check a couple of load modules
530   { const auto &lm0 = encodedProfile.load_modules(0);
531     std::string act_lm0 = encodedLoadModuleToString(lm0);
532     std::string sqact0 = squeezeWhite(act_lm0, "actual for lm 0");
533     const std::string expected_lm0 = RAW_RESULT(
534         name: "/data/app/com.google.android.apps.plus-1/lib/arm/libcronet.so"
535                                                 );
536     std::string sqexp0 = squeezeWhite(expected_lm0, "expected_lm0");
537     EXPECT_STREQ(sqexp0.c_str(), sqact0.c_str());
538   }
539   { const auto &lm9 = encodedProfile.load_modules(9);
540     std::string act_lm9 = encodedLoadModuleToString(lm9);
541     std::string sqact9 = squeezeWhite(act_lm9, "actual for lm 9");
542     const std::string expected_lm9 = RAW_RESULT(
543         name: "/system/lib/libandroid_runtime.so" build_id: "8164ed7b3a8b8f5a220d027788922510"
544                                                 );
545     std::string sqexp9 = squeezeWhite(expected_lm9, "expected_lm9");
546     EXPECT_STREQ(sqexp9.c_str(), sqact9.c_str());
547   }
548
549   // Examine some of the samples now
550   { const auto &p1 = encodedProfile.programs(0);
551     const auto &lm1 = p1.modules(0);
552     std::string act_lm1 = encodedModuleSamplesToString(lm1);
553     std::string sqact1 = squeezeWhite(act_lm1, "actual for lm1");
554     const std::string expected_lm1 = RAW_RESULT(
555         load_module_id: 9 address_samples { address: 296100 count: 1 }
556                                                 );
557     std::string sqexp1 = squeezeWhite(expected_lm1, "expected_lm1");
558     EXPECT_STREQ(sqexp1.c_str(), sqact1.c_str());
559   }
560   { const auto &p1 = encodedProfile.programs(2);
561     const auto &lm2 = p1.modules(0);
562     std::string act_lm2 = encodedModuleSamplesToString(lm2);
563     std::string sqact2 = squeezeWhite(act_lm2, "actual for lm2");
564     const std::string expected_lm2 = RAW_RESULT(
565         load_module_id: 2
566         address_samples { address: 28030244 count: 1 }
567         address_samples { address: 29657840 count: 1 }
568                                                 );
569     std::string sqexp2 = squeezeWhite(expected_lm2, "expected_lm2");
570     EXPECT_STREQ(sqexp2.c_str(), sqact2.c_str());
571   }
572 }
573
574 TEST_F(PerfProfdTest, CallchainRunWithCannedPerf)
575 {
576   // This test makes sure that the perf.data converter
577   // can handle call chains.
578   //
579   std::string input_perf_data(test_dir);
580   input_perf_data += "/callchain.canned.perf.data";
581
582   // Set up config to avoid these annotations (they are tested elsewhere)
583   ConfigReader config;
584   config.overrideUnsignedEntry("collect_cpu_utilization", 0);
585   config.overrideUnsignedEntry("collect_charging_state", 0);
586   config.overrideUnsignedEntry("collect_camera_active", 0);
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
599   // Expect 4 programs 8 load modules
600   EXPECT_EQ(4, encodedProfile.programs_size());
601   EXPECT_EQ(8, encodedProfile.load_modules_size());
602
603   // Check a couple of load modules
604   { const auto &lm0 = encodedProfile.load_modules(0);
605     std::string act_lm0 = encodedLoadModuleToString(lm0);
606     std::string sqact0 = squeezeWhite(act_lm0, "actual for lm 0");
607     const std::string expected_lm0 = RAW_RESULT(
608         name: "/system/bin/dex2oat"
609         build_id: "ee12bd1a1de39422d848f249add0afc4"
610                                                 );
611     std::string sqexp0 = squeezeWhite(expected_lm0, "expected_lm0");
612     EXPECT_STREQ(sqexp0.c_str(), sqact0.c_str());
613   }
614   { const auto &lm1 = encodedProfile.load_modules(1);
615     std::string act_lm1 = encodedLoadModuleToString(lm1);
616     std::string sqact1 = squeezeWhite(act_lm1, "actual for lm 1");
617     const std::string expected_lm1 = RAW_RESULT(
618         name: "/system/bin/linker"
619         build_id: "a36715f673a4a0aa76ef290124c516cc"
620                                                 );
621     std::string sqexp1 = squeezeWhite(expected_lm1, "expected_lm1");
622     EXPECT_STREQ(sqexp1.c_str(), sqact1.c_str());
623   }
624
625   // Examine some of the samples now
626   { const auto &p0 = encodedProfile.programs(0);
627     const auto &lm1 = p0.modules(0);
628     std::string act_lm1 = encodedModuleSamplesToString(lm1);
629     std::string sqact1 = squeezeWhite(act_lm1, "actual for lm1");
630     const std::string expected_lm1 = RAW_RESULT(
631         load_module_id: 0
632         address_samples { address: 108552 count: 2 }
633                                                 );
634     std::string sqexp1 = squeezeWhite(expected_lm1, "expected_lm1");
635     EXPECT_STREQ(sqexp1.c_str(), sqact1.c_str());
636   }
637   { const auto &p4 = encodedProfile.programs(3);
638     const auto &lm2 = p4.modules(1);
639     std::string act_lm2 = encodedModuleSamplesToString(lm2);
640     std::string sqact2 = squeezeWhite(act_lm2, "actual for lm2");
641     const std::string expected_lm2 = RAW_RESULT(
642         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 }
643                                                 );
644     std::string sqexp2 = squeezeWhite(expected_lm2, "expected_lm2");
645     EXPECT_STREQ(sqexp2.c_str(), sqact2.c_str());
646   }
647 }
648
649 TEST_F(PerfProfdTest, BasicRunWithLivePerf)
650 {
651   //
652   // Basic test to exercise the main loop of the daemon. It includes
653   // a live 'perf' run
654   //
655   PerfProfdRunner runner;
656   runner.addToConfig("only_debug_build=0");
657   std::string ddparam("destination_directory="); ddparam += dest_dir;
658   runner.addToConfig(ddparam);
659   std::string cfparam("config_directory="); cfparam += test_dir;
660   runner.addToConfig(cfparam);
661   runner.addToConfig("main_loop_iterations=1");
662   runner.addToConfig("use_fixed_seed=12345678");
663   runner.addToConfig("max_unprocessed_profiles=100");
664   runner.addToConfig("collection_interval=9999");
665   runner.addToConfig("sample_duration=2");
666
667   // Create semaphore file
668   runner.create_semaphore_file();
669
670   // Kick off daemon
671   int daemon_main_return_code = runner.invoke();
672
673   // Check return code from daemon
674   EXPECT_EQ(0, daemon_main_return_code);
675
676   // Read and decode the resulting perf.data.encoded file
677   wireless_android_play_playlog::AndroidPerfProfile encodedProfile;
678   readEncodedProfile("BasicRunWithLivePerf", encodedProfile);
679
680   // Examine what we get back. Since it's a live profile, we can't
681   // really do much in terms of verifying the contents.
682   EXPECT_LT(0, encodedProfile.programs_size());
683
684   // Verify log contents
685   const std::string expected = RAW_RESULT(
686       I: starting Android Wide Profiling daemon
687       I: config file path set to /data/nativetest/perfprofd_test/perfprofd.conf
688       I: random seed set to 12345678
689       I: sleep 674 seconds
690       I: initiating profile collection
691       I: profile collection complete
692       I: sleep 9325 seconds
693       I: finishing Android Wide Profiling daemon
694                                           );
695   // check to make sure log excerpt matches
696   compareLogMessages(mock_perfprofdutils_getlogged(),
697                      expected, "BasicRunWithLivePerf", true);
698 }
699
700 TEST_F(PerfProfdTest, MultipleRunWithLivePerf)
701 {
702   //
703   // Basic test to exercise the main loop of the daemon. It includes
704   // a live 'perf' run
705   //
706   PerfProfdRunner runner;
707   runner.addToConfig("only_debug_build=0");
708   std::string ddparam("destination_directory="); ddparam += dest_dir;
709   runner.addToConfig(ddparam);
710   std::string cfparam("config_directory="); cfparam += test_dir;
711   runner.addToConfig(cfparam);
712   runner.addToConfig("main_loop_iterations=3");
713   runner.addToConfig("use_fixed_seed=12345678");
714   runner.addToConfig("collection_interval=9999");
715   runner.addToConfig("sample_duration=2");
716   runner.write_processed_file(1, 2);
717
718   // Create semaphore file
719   runner.create_semaphore_file();
720
721   // Kick off daemon
722   int daemon_main_return_code = runner.invoke();
723
724   // Check return code from daemon
725   EXPECT_EQ(0, daemon_main_return_code);
726
727   // Read and decode the resulting perf.data.encoded file
728   wireless_android_play_playlog::AndroidPerfProfile encodedProfile;
729   readEncodedProfile("BasicRunWithLivePerf", encodedProfile);
730
731   // Examine what we get back. Since it's a live profile, we can't
732   // really do much in terms of verifying the contents.
733   EXPECT_LT(0, encodedProfile.programs_size());
734
735   // Examine that encoded.1 file is removed while encoded.{0|2} exists.
736   EXPECT_EQ(0, access(encoded_file_path(0).c_str(), F_OK));
737   EXPECT_NE(0, access(encoded_file_path(1).c_str(), F_OK));
738   EXPECT_EQ(0, access(encoded_file_path(2).c_str(), F_OK));
739
740   // Verify log contents
741   const std::string expected = RAW_RESULT(
742       I: starting Android Wide Profiling daemon
743       I: config file path set to /data/nativetest/perfprofd_test/perfprofd.conf
744       I: random seed set to 12345678
745       I: sleep 674 seconds
746       I: initiating profile collection
747       I: profile collection complete
748       I: sleep 9325 seconds
749       I: sleep 4974 seconds
750       I: initiating profile collection
751       I: profile collection complete
752       I: sleep 5025 seconds
753       I: sleep 501 seconds
754       I: initiating profile collection
755       I: profile collection complete
756       I: sleep 9498 seconds
757       I: finishing Android Wide Profiling daemon
758                                           );
759   // check to make sure log excerpt matches
760   compareLogMessages(mock_perfprofdutils_getlogged(),
761                      expected, "BasicRunWithLivePerf", true);
762 }
763
764 TEST_F(PerfProfdTest, CallChainRunWithLivePerf)
765 {
766   //
767   // Callchain profiles are only supported on certain devices.
768   // For now this test is stubbed out except when run on "angler".
769   //
770   char propBuf[PROPERTY_VALUE_MAX];
771   propBuf[0] = '\0';
772   property_get("ro.hardware", propBuf, "");
773   if (strcmp(propBuf, "angler")) {
774     return;
775   }
776
777   //
778   // Collect a callchain profile, so as to exercise the code in
779   // perf_data post-processing that digests callchains.
780   //
781   PerfProfdRunner runner;
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=1");
787   runner.addToConfig("use_fixed_seed=12345678");
788   runner.addToConfig("max_unprocessed_profiles=100");
789   runner.addToConfig("collection_interval=9999");
790   runner.addToConfig("stack_profile=1");
791   runner.addToConfig("sample_duration=2");
792
793   // Create semaphore file
794   runner.create_semaphore_file();
795
796   // Kick off daemon
797   int daemon_main_return_code = runner.invoke();
798
799   // Check return code from daemon
800   EXPECT_EQ(0, daemon_main_return_code);
801
802   // Read and decode the resulting perf.data.encoded file
803   wireless_android_play_playlog::AndroidPerfProfile encodedProfile;
804   readEncodedProfile("CallChainRunWithLivePerf", encodedProfile);
805
806   // Examine what we get back. Since it's a live profile, we can't
807   // really do much in terms of verifying the contents.
808   EXPECT_LT(0, encodedProfile.programs_size());
809
810   // Verify log contents
811   const std::string expected = RAW_RESULT(
812       I: starting Android Wide Profiling daemon
813       I: config file path set to /data/nativetest/perfprofd_test/perfprofd.conf
814       I: random seed set to 12345678
815       I: sleep 674 seconds
816       I: initiating profile collection
817       I: profile collection complete
818       I: sleep 9325 seconds
819       I: finishing Android Wide Profiling daemon
820                                           );
821   // check to make sure log excerpt matches
822   compareLogMessages(mock_perfprofdutils_getlogged(),
823                      expected, "CallChainRunWithLivePerf", true);
824 }
825
826 int main(int argc, char **argv) {
827   executable_path = argv[0];
828   // switch to / before starting testing (perfprofd
829   // should be location-independent)
830   chdir("/");
831   testing::InitGoogleTest(&argc, argv);
832   return RUN_ALL_TESTS();
833 }