OSDN Git Service

Switch perfprofd from using full to lite version of protobuf library.
[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 "perfprofdcore.h"
28 #include "perfprofdutils.h"
29 #include "perfprofdmockutils.h"
30
31 #include "perf_profile.pb.h"
32 #include "google/protobuf/text_format.h"
33
34 //
35 // Set to argv[0] on startup
36 //
37 static const char *executable_path;
38
39 //
40 // test_dir is the directory containing the test executable and
41 // any files associated with the test (will be created by the harness).
42 //
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.
46 //
47 static std::string test_dir;
48 static std::string dest_dir;
49
50 // Path to perf executable on device
51 #define PERFPATH "/system/bin/perf"
52
53 // Temporary config file that we will emit for the daemon to read
54 #define CONFIGFILE "perfprofd.conf"
55
56 static std::string encoded_file_path()
57 {
58   std::string path(dest_dir);
59   path += "/perf.data.encoded";
60   return path;
61 }
62
63 class PerfProfdTest : public testing::Test {
64  protected:
65   virtual void SetUp() {
66     mock_perfprofdutils_init();
67     create_dest_dir();
68     yesclean();
69   }
70
71   virtual void TearDown() {
72     mock_perfprofdutils_finish();
73     remove_dest_dir();
74   }
75
76   void noclean() {
77     clean_ = false;
78   }
79   void yesclean() {
80     clean_ = true;
81   }
82
83  private:
84   bool clean_;
85
86   void create_dest_dir() {
87     setup_dirs();
88     ASSERT_FALSE(dest_dir == "");
89     if (clean_) {
90       std::string cmd("rm -rf ");
91       cmd += dest_dir;
92       system(cmd.c_str());
93     }
94     std::string cmd("mkdir -p ");
95     cmd += dest_dir;
96     system(cmd.c_str());
97   }
98
99   void remove_dest_dir() {
100     setup_dirs();
101     ASSERT_FALSE(dest_dir == "");
102   }
103
104   void setup_dirs()
105   {
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);
111       dest_dir = test_dir;
112       dest_dir += "/tmp";
113     }
114   }
115
116 };
117
118 static bool bothWhiteSpace(char lhs, char rhs)
119 {
120   return (std::isspace(lhs) && std::isspace(rhs));
121 }
122
123 //
124 // Squeeze out repeated whitespace from expected/actual logs.
125 //
126 static std::string squeezeWhite(const std::string &str,
127                                 const char *tag,
128                                 bool dump=false)
129 {
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())) {
136     result.pop_back();
137   }
138   if (dump) { fprintf(stderr, "squeezed %s is %s\n", tag, result.c_str()); }
139   return result;
140 }
141
142 ///
143 /// Helper class to kick off a run of the perfprofd daemon with a specific
144 /// config file.
145 ///
146 class PerfProfdRunner {
147  public:
148   PerfProfdRunner()
149       : config_path_(test_dir)
150       , aux_config_path_(dest_dir)
151   {
152     config_path_ += "/" CONFIGFILE;
153     aux_config_path_ += "/" CONFIGFILE;
154   }
155
156   ~PerfProfdRunner()
157   {
158   }
159
160   void addToConfig(const std::string &line)
161   {
162     config_text_ += line;
163     config_text_ += "\n";
164   }
165
166   void addToAuxConfig(const std::string &line)
167   {
168     aux_config_text_ += line;
169     aux_config_text_ += "\n";
170   }
171
172   void remove_semaphore_file()
173   {
174     std::string semaphore(dest_dir);
175     semaphore += "/" SEMAPHORE_FILENAME;
176     unlink(semaphore.c_str());
177   }
178
179   void create_semaphore_file()
180   {
181     std::string semaphore(dest_dir);
182     semaphore += "/" SEMAPHORE_FILENAME;
183     close(open(semaphore.c_str(), O_WRONLY|O_CREAT));
184   }
185
186   int invoke()
187   {
188     static const char *argv[3] = { "perfprofd", "-c", "" };
189     argv[2] = config_path_.c_str();
190
191     writeConfigFile(config_path_, config_text_);
192     if (aux_config_text_.length()) {
193       writeConfigFile(aux_config_path_, aux_config_text_);
194     }
195
196
197     // execute daemon main
198     return perfprofd_main(3, (char **) argv);
199   }
200
201  private:
202   std::string config_path_;
203   std::string config_text_;
204   std::string aux_config_path_;
205   std::string aux_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().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().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 directory we're interested in may
304   // not be there. This test insures that the daemon does the right
305   // thing in this case.
306   //
307   PerfProfdRunner runner;
308   runner.addToConfig("only_debug_build=0");
309   runner.addToConfig("trace_config_read=1");
310   runner.addToConfig("destination_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: starting Android Wide Profiling daemon
324       I: config file path set to /data/nativetest/perfprofd_test/perfprofd.conf
325       I: option destination_directory set to /does/not/exist
326       I: option main_loop_iterations set to 1
327       I: option use_fixed_seed set to 1
328       I: option collection_interval set to 100
329       I: random seed set to 1
330       I: sleep 90 seconds
331       W: unable to open destination directory /does/not/exist: (No such file or directory)
332       I: profile collection skipped (missing destination directory)
333       I: sleep 10 seconds
334       I: finishing Android Wide Profiling daemon
335                                           );\
336
337   // check to make sure entire log matches
338   bool compareEntireLog = true;
339   compareLogMessages(mock_perfprofdutils_getlogged(),
340                      expected, "MissingGMS", compareEntireLog);
341 }
342
343 TEST_F(PerfProfdTest, MissingOptInSemaphoreFile)
344 {
345   //
346   // Android device owners must opt in to "collect and report usage
347   // data" in order for us to be able to collect profiles. The opt-in
348   // check is performed in the GMS core component; if the check
349   // passes, then it creates a semaphore file for the daemon to pick
350   // up on.
351   //
352   PerfProfdRunner runner;
353   runner.addToConfig("only_debug_build=0");
354   std::string ddparam("destination_directory="); ddparam += dest_dir;
355   runner.addToConfig(ddparam);
356   runner.addToConfig("main_loop_iterations=1");
357   runner.addToConfig("use_fixed_seed=1");
358   runner.addToConfig("collection_interval=100");
359
360   runner.remove_semaphore_file();
361
362   // Kick off daemon
363   int daemon_main_return_code = runner.invoke();
364
365   // Check return code from daemon
366   EXPECT_EQ(0, daemon_main_return_code);
367
368   // Verify log contents
369   const std::string expected = RAW_RESULT(
370       I: profile collection skipped (missing semaphore file)
371                                           );
372   // check to make sure log excerpt matches
373   compareLogMessages(mock_perfprofdutils_getlogged(),
374                      expected, "MissingOptInSemaphoreFile");
375 }
376
377 TEST_F(PerfProfdTest, MissingPerfExecutable)
378 {
379   //
380   // Perfprofd uses the 'simpleperf' tool to collect profiles
381   // (although this may conceivably change in the future). This test
382   // checks to make sure that if 'simpleperf' is not present we bail out
383   // from collecting profiles.
384   //
385   PerfProfdRunner runner;
386   runner.addToConfig("only_debug_build=0");
387   runner.addToConfig("trace_config_read=1");
388   std::string ddparam("destination_directory="); ddparam += dest_dir;
389   runner.addToConfig(ddparam);
390   runner.addToConfig("main_loop_iterations=1");
391   runner.addToConfig("use_fixed_seed=1");
392   runner.addToConfig("collection_interval=100");
393   runner.addToConfig("perf_path=/does/not/exist");
394
395   // Create semaphore file
396   runner.create_semaphore_file();
397
398   // Kick off daemon
399   int daemon_main_return_code = runner.invoke();
400
401   // Check return code from daemon
402   EXPECT_EQ(0, daemon_main_return_code);
403
404   // expected log contents
405   const std::string expected = RAW_RESULT(
406       I: profile collection skipped (missing 'perf' executable)
407                                           );
408   // check to make sure log excerpt matches
409   compareLogMessages(mock_perfprofdutils_getlogged(),
410                      expected, "MissingPerfExecutable");
411 }
412
413 TEST_F(PerfProfdTest, BadPerfRun)
414 {
415   //
416   // Perf tools tend to be tightly coupled with a specific kernel
417   // version -- if things are out of sync perf could fail or
418   // crash. This test makes sure that we detect such a case and log
419   // the error.
420   //
421   PerfProfdRunner runner;
422   runner.addToConfig("only_debug_build=0");
423   std::string ddparam("destination_directory="); ddparam += dest_dir;
424   runner.addToConfig(ddparam);
425   runner.addToConfig("main_loop_iterations=1");
426   runner.addToConfig("use_fixed_seed=1");
427   runner.addToConfig("collection_interval=100");
428   runner.addToConfig("perf_path=/system/bin/false");
429
430   // Create semaphore file
431   runner.create_semaphore_file();
432
433   // Kick off daemon
434   int daemon_main_return_code = runner.invoke();
435
436   // Check return code from daemon
437   EXPECT_EQ(0, daemon_main_return_code);
438
439   // Verify log contents
440   const std::string expected = RAW_RESULT(
441       I: profile collection failed (perf record returned bad exit status)
442                                           );
443
444   // check to make sure log excerpt matches
445   compareLogMessages(mock_perfprofdutils_getlogged(),
446                      expected, "BadPerfRun");
447 }
448
449 TEST_F(PerfProfdTest, ConfigFileParsing)
450 {
451   //
452   // Gracefully handly malformed items in the config file
453   //
454   PerfProfdRunner runner;
455   runner.addToConfig("only_debug_build=0");
456   runner.addToConfig("main_loop_iterations=1");
457   runner.addToConfig("collection_interval=100");
458   runner.addToConfig("use_fixed_seed=1");
459   runner.addToConfig("destination_directory=/does/not/exist");
460
461   // assorted bad syntax
462   runner.addToConfig("collection_interval=0");
463   runner.addToConfig("collection_interval=-1");
464   runner.addToConfig("collection_interval=2");
465   runner.addToConfig("nonexistent_key=something");
466   runner.addToConfig("no_equals_stmt");
467
468   // Kick off daemon
469   int daemon_main_return_code = runner.invoke();
470
471   // Check return code from daemon
472   EXPECT_EQ(0, daemon_main_return_code);
473
474   // Verify log contents
475   const std::string expected = RAW_RESULT(
476       W: line 6: specified value 0 for 'collection_interval' outside permitted range [100 4294967295] (ignored)
477       W: line 7: malformed unsigned value (ignored)
478       W: line 8: specified value 2 for 'collection_interval' outside permitted range [100 4294967295] (ignored)
479       W: line 9: unknown option 'nonexistent_key' ignored
480       W: line 10: line malformed (no '=' found)
481                                           );
482
483   // check to make sure log excerpt matches
484   compareLogMessages(mock_perfprofdutils_getlogged(),
485                      expected, "ConfigFileParsing");
486 }
487
488 TEST_F(PerfProfdTest, AuxiliaryConfigFile)
489 {
490   //
491   // We want to be able to tweak profile collection parameters (sample
492   // duration, etc) using changes to gservices. To carry this out, the
493   // GMS core upload service writes out an perfprofd.conf config file when
494   // it starts up. This test verifies that we can read this file.
495   //
496
497   // Minimal settings in main config file
498   PerfProfdRunner runner;
499   runner.addToConfig("only_debug_build=0");
500   runner.addToConfig("trace_config_read=1");
501   runner.addToConfig("use_fixed_seed=1");
502   std::string ddparam("destination_directory="); ddparam += dest_dir;
503   runner.addToConfig(ddparam);
504
505   // Remaining settings in aux config file
506   runner.addToAuxConfig("main_loop_iterations=1");
507   runner.addToAuxConfig("collection_interval=100");
508   runner.addToAuxConfig("perf_path=/system/bin/true");
509   runner.addToAuxConfig("stack_profile=1");
510   runner.addToAuxConfig("sampling_period=9999");
511   runner.addToAuxConfig("sample_duration=333");
512
513   runner.remove_semaphore_file();
514
515   // Kick off daemon
516   int daemon_main_return_code = runner.invoke();
517
518   // Check return code from daemon
519   EXPECT_EQ(0, daemon_main_return_code);
520
521   // Verify log contents
522   const std::string expected = RAW_RESULT(
523       I: reading auxiliary config file /data/nativetest/perfprofd_test/tmp/perfprofd.conf
524       I: option main_loop_iterations set to 1
525       I: option collection_interval set to 100
526       I: option perf_path set to /system/bin/true
527       I: option stack_profile set to 1
528       I: option sampling_period set to 9999
529       I: option sample_duration set to 333
530       I: sleep 90 seconds
531       I: reading auxiliary config file /data/nativetest/perfprofd_test/tmp/perfprofd.conf
532       I: option main_loop_iterations set to 1
533                                           );
534
535   // check to make sure log excerpt matches
536   compareLogMessages(mock_perfprofdutils_getlogged(),
537                      expected, "AuxiliaryConfigFile");
538 }
539
540 TEST_F(PerfProfdTest, BasicRunWithCannedPerf)
541 {
542   //
543   // Verify the portion of the daemon that reads and encodes
544   // perf.data files. Here we run the encoder on a canned perf.data
545   // file and verify that the resulting protobuf contains what
546   // we think it should contain.
547   //
548   std::string input_perf_data(test_dir);
549   input_perf_data += "/canned.perf.data";
550
551   // Kick off encoder and check return code
552   PROFILE_RESULT result =
553       encode_to_proto(input_perf_data, encoded_file_path());
554   EXPECT_EQ(OK_PROFILE_COLLECTION, result);
555
556   // Read and decode the resulting perf.data.encoded file
557   wireless_android_play_playlog::AndroidPerfProfile encodedProfile;
558   readEncodedProfile("BasicRunWithCannedPerf",
559                      encodedProfile);
560
561   // Expect 29 load modules
562   EXPECT_EQ(29, encodedProfile.programs_size());
563
564   // Check a couple of load modules
565   { const auto &lm0 = encodedProfile.load_modules(0);
566     std::string act_lm0 = encodedLoadModuleToString(lm0);
567     std::string sqact0 = squeezeWhite(act_lm0, "actual for lm 0");
568     const std::string expected_lm0 = RAW_RESULT(
569         name: "/data/app/com.google.android.apps.plus-1/lib/arm/libcronet.so"
570                                                 );
571     std::string sqexp0 = squeezeWhite(expected_lm0, "expected_lm0");
572     EXPECT_STREQ(sqexp0.c_str(), sqact0.c_str());
573   }
574   { const auto &lm9 = encodedProfile.load_modules(9);
575     std::string act_lm9 = encodedLoadModuleToString(lm9);
576     std::string sqact9 = squeezeWhite(act_lm9, "actual for lm 9");
577     const std::string expected_lm9 = RAW_RESULT(
578         name: "/system/lib/libandroid_runtime.so" build_id: "8164ed7b3a8b8f5a220d027788922510"
579                                                 );
580     std::string sqexp9 = squeezeWhite(expected_lm9, "expected_lm9");
581     EXPECT_STREQ(sqexp9.c_str(), sqact9.c_str());
582   }
583
584   // Examine some of the samples now
585   { const auto &p1 = encodedProfile.programs(0);
586     const auto &lm1 = p1.modules(0);
587     std::string act_lm1 = encodedModuleSamplesToString(lm1);
588     std::string sqact1 = squeezeWhite(act_lm1, "actual for lm1");
589     const std::string expected_lm1 = RAW_RESULT(
590         load_module_id: 9 address_samples { address: 296100 count: 1 }
591                                                 );
592     std::string sqexp1 = squeezeWhite(expected_lm1, "expected_lm1");
593     EXPECT_STREQ(sqexp1.c_str(), sqact1.c_str());
594   }
595   { const auto &p1 = encodedProfile.programs(2);
596     const auto &lm2 = p1.modules(0);
597     std::string act_lm2 = encodedModuleSamplesToString(lm2);
598     std::string sqact2 = squeezeWhite(act_lm2, "actual for lm2");
599     const std::string expected_lm2 = RAW_RESULT(
600         load_module_id: 2
601         address_samples { address: 28030244 count: 1 }
602         address_samples { address: 29657840 count: 1 }
603                                                 );
604     std::string sqexp2 = squeezeWhite(expected_lm2, "expected_lm2");
605     EXPECT_STREQ(sqexp2.c_str(), sqact2.c_str());
606   }
607 }
608
609 TEST_F(PerfProfdTest, BasicRunWithLivePerf)
610 {
611   //
612   // Basic test to exercise the main loop of the daemon. It includes
613   // a live 'perf' run
614   //
615   PerfProfdRunner runner;
616   runner.addToConfig("only_debug_build=0");
617   std::string ddparam("destination_directory="); ddparam += dest_dir;
618   runner.addToConfig(ddparam);
619   runner.addToConfig("main_loop_iterations=1");
620   runner.addToConfig("use_fixed_seed=12345678");
621   runner.addToConfig("collection_interval=9999");
622   runner.addToConfig("sample_duration=5");
623
624   // Create semaphore file
625   runner.create_semaphore_file();
626
627   // Kick off daemon
628   int daemon_main_return_code = runner.invoke();
629
630   // Check return code from daemon
631   EXPECT_EQ(0, daemon_main_return_code);
632
633   // Read and decode the resulting perf.data.encoded file
634   wireless_android_play_playlog::AndroidPerfProfile encodedProfile;
635   readEncodedProfile("BasicRunWithLivePerf", encodedProfile);
636
637   // Examine what we get back. Since it's a live profile, we can't
638   // really do much in terms of verifying the contents.
639   EXPECT_LT(0, encodedProfile.programs_size());
640
641   // Verify log contents
642   const std::string expected = RAW_RESULT(
643       I: starting Android Wide Profiling daemon
644       I: config file path set to /data/nativetest/perfprofd_test/perfprofd.conf
645       I: random seed set to 12345678
646       I: sleep 674 seconds
647       I: initiating profile collection
648       I: profile collection complete
649       I: sleep 9325 seconds
650       I: finishing Android Wide Profiling daemon
651                                           );
652   // check to make sure log excerpt matches
653   compareLogMessages(mock_perfprofdutils_getlogged(),
654                      expected, "BasicRunWithLivePerf", true);
655 }
656
657 int main(int argc, char **argv) {
658   executable_path = argv[0];
659   // switch to / before starting testing (perfprofd
660   // should be location-independent)
661   chdir("/");
662   testing::InitGoogleTest(&argc, argv);
663   return RUN_ALL_TESTS();
664 }