OSDN Git Service

Perfprofd: various changes related to config parameters.
[android-x86/system-extras.git] / perfprofd / perfprofdcore.cc
1 /*
2 **
3 ** Copyright 2015, The Android Open Source Project
4 **
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
8 **
9 **     http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 */
17
18 #include <assert.h>
19 #include <dirent.h>
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <signal.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28 #include <sys/wait.h>
29 #include <time.h>
30 #include <unistd.h>
31 #include <string>
32 #include <sstream>
33 #include <map>
34 #include <set>
35 #include <cctype>
36
37 #include <base/stringprintf.h>
38 #include <cutils/properties.h>
39
40 #include "perfprofdcore.h"
41 #include "perfprofdutils.h"
42 #include "perf_data_converter.h"
43 #include "cpuconfig.h"
44
45 //
46 // Perf profiling daemon -- collects system-wide profiles using
47 //
48 //       simpleperf record -a
49 //
50 // and encodes them so that they can be uploaded by a separate service.
51 //
52
53 //......................................................................
54
55 //
56 // Output file from 'perf record'.
57 //
58 #define PERF_OUTPUT "perf.data"
59
60 //
61 // This enum holds the results of the "should we profile" configuration check.
62 //
63 typedef enum {
64
65   // All systems go for profile collection.
66   DO_COLLECT_PROFILE,
67
68   // The selected configuration directory doesn't exist.
69   DONT_PROFILE_MISSING_CONFIG_DIR,
70
71   // Destination directory does not contain the semaphore file that
72   // the perf profile uploading service creates when it determines
73   // that the user has opted "in" for usage data collection. No
74   // semaphore -> no user approval -> no profiling.
75   DONT_PROFILE_MISSING_SEMAPHORE,
76
77   // No perf executable present
78   DONT_PROFILE_MISSING_PERF_EXECUTABLE,
79
80   // We're running in the emulator, perf won't be able to do much
81   DONT_PROFILE_RUNNING_IN_EMULATOR
82
83 } CKPROFILE_RESULT;
84
85 //
86 // Are we running in the emulator? If so, stub out profile collection
87 // Starts as uninitialized (-1), then set to 1 or 0 at init time.
88 //
89 static int running_in_emulator = -1;
90
91 //
92 // Is this a debug build ('userdebug' or 'eng')?
93 // Starts as uninitialized (-1), then set to 1 or 0 at init time.
94 //
95 static int is_debug_build = -1;
96
97 //
98 // Random number generator seed (set at startup time).
99 //
100 static unsigned short random_seed[3];
101
102 //
103 // Config file path. May be overridden with -c command line option
104 //
105 static const char *config_file_path =
106     "/data/data/com.google.android.gms/files/perfprofd.conf";
107
108 //
109 // This table describes the config file syntax in terms of key/value pairs.
110 // Values come in two flavors: strings, or unsigned integers. In the latter
111 // case the reader sets allowable minimum/maximum for the setting.
112 //
113 class ConfigReader {
114
115  public:
116   ConfigReader();
117   ~ConfigReader();
118
119   // Ask for the current setting of a config item
120   unsigned getUnsignedValue(const char *key) const;
121   std::string getStringValue(const char *key) const;
122
123   // read the specified config file, applying any settings it contains
124   void readFile(bool initial);
125
126  private:
127   void addUnsignedEntry(const char *key,
128                         unsigned default_value,
129                         unsigned min_value,
130                         unsigned max_value);
131   void addStringEntry(const char *key, const char *default_value);
132   void addDefaultEntries();
133   void parseLine(const char *key, const char *value, unsigned linecount);
134
135   typedef struct { unsigned minv, maxv; } values;
136   std::map<std::string, values> u_info;
137   std::map<std::string, unsigned> u_entries;
138   std::map<std::string, std::string> s_entries;
139   bool trace_config_read;
140 };
141
142 ConfigReader::ConfigReader()
143     : trace_config_read(false)
144 {
145   addDefaultEntries();
146 }
147
148 ConfigReader::~ConfigReader()
149 {
150 }
151
152 //
153 // Populate the reader with the set of allowable entries
154 //
155 void ConfigReader::addDefaultEntries()
156 {
157   // Average number of seconds between perf profile collections (if
158   // set to 100, then over time we want to see a perf profile
159   // collected every 100 seconds). The actual time within the interval
160   // for the collection is chosen randomly.
161   addUnsignedEntry("collection_interval", 14400, 100, UINT32_MAX);
162
163   // Use the specified fixed seed for random number generation (unit
164   // testing)
165   addUnsignedEntry("use_fixed_seed", 0, 0, UINT32_MAX);
166
167   // For testing purposes, number of times to iterate through main
168   // loop.  Value of zero indicates that we should loop forever.
169   addUnsignedEntry("main_loop_iterations", 0, 0, UINT32_MAX);
170
171   // Destination directory (where to write profiles). This location
172   // chosen since it is accessible to the uploader service.
173   addStringEntry("destination_directory", "/data/misc/perfprofd");
174
175   // Config directory (where to read configs).
176   addStringEntry("config_directory", "/data/data/com.google.android.gms/files");
177
178   // Full path to 'perf' executable.
179   addStringEntry("perf_path", "/system/xbin/simpleperf");
180
181   // Desired sampling period (passed to perf -c option). Small
182   // sampling periods can perturb the collected profiles, so enforce
183   // min/max.
184   addUnsignedEntry("sampling_period", 500000, 5000, UINT32_MAX);
185
186   // Length of time to collect samples (number of seconds for 'perf
187   // record -a' run).
188   addUnsignedEntry("sample_duration", 3, 2, 600);
189
190   // If this parameter is non-zero it will cause perfprofd to
191   // exit immediately if the build type is not userdebug or eng.
192   // Currently defaults to 1 (true).
193   addUnsignedEntry("only_debug_build", 1, 0, 1);
194
195   // If the "mpdecision" service is running at the point we are ready
196   // to kick off a profiling run, then temporarily disable the service
197   // and hard-wire all cores on prior to the collection run, provided
198   // that the duration of the recording is less than or equal to the value of
199   // 'hardwire_cpus_max_duration'.
200   addUnsignedEntry("hardwire_cpus", 1, 0, 1);
201   addUnsignedEntry("hardwire_cpus_max_duration", 5, 1, UINT32_MAX);
202
203   // Maximum number of unprocessed profiles we can accumulate in the
204   // destination directory. Once we reach this limit, we continue
205   // to collect, but we just overwrite the most recent profile.
206   addUnsignedEntry("max_unprocessed_profiles", 10, 1, UINT32_MAX);
207
208   // If set to 1, pass the -g option when invoking 'perf' (requests
209   // stack traces as opposed to flat profile).
210   addUnsignedEntry("stack_profile", 0, 0, 1);
211
212   // For unit testing only: if set to 1, emit info messages on config
213   // file parsing.
214   addUnsignedEntry("trace_config_read", 0, 0, 1);
215 }
216
217 void ConfigReader::addUnsignedEntry(const char *key,
218                                     unsigned default_value,
219                                     unsigned min_value,
220                                     unsigned max_value)
221 {
222   std::string ks(key);
223   if (u_entries.find(ks) != u_entries.end() ||
224       s_entries.find(ks) != s_entries.end()) {
225     W_ALOGE("internal error -- duplicate entry for key %s", key);
226     exit(9);
227   }
228   values vals;
229   vals.minv = min_value;
230   vals.maxv = max_value;
231   u_info[ks] = vals;
232   u_entries[ks] = default_value;
233 }
234
235 void ConfigReader::addStringEntry(const char *key, const char *default_value)
236 {
237   std::string ks(key);
238   if (u_entries.find(ks) != u_entries.end() ||
239       s_entries.find(ks) != s_entries.end()) {
240     W_ALOGE("internal error -- duplicate entry for key %s", key);
241     exit(9);
242   }
243   if (default_value == nullptr) {
244     W_ALOGE("internal error -- bad default value for key %s", key);
245     exit(9);
246   }
247   s_entries[ks] = std::string(default_value);
248 }
249
250 unsigned ConfigReader::getUnsignedValue(const char *key) const
251 {
252   std::string ks(key);
253   auto it = u_entries.find(ks);
254   assert(it != u_entries.end());
255   return it->second;
256 }
257
258 std::string ConfigReader::getStringValue(const char *key) const
259 {
260   std::string ks(key);
261   auto it = s_entries.find(ks);
262   assert(it != s_entries.end());
263   return it->second;
264 }
265
266 //
267 // Parse a key=value pair read from the config file. This will issue
268 // warnings or errors to the system logs if the line can't be
269 // interpreted properly.
270 //
271 void ConfigReader::parseLine(const char *key,
272                              const char *value,
273                              unsigned linecount)
274 {
275   assert(key);
276   assert(value);
277
278   auto uit = u_entries.find(key);
279   if (uit != u_entries.end()) {
280     unsigned uvalue = 0;
281     if (isdigit(value[0]) == 0 || sscanf(value, "%u", &uvalue) != 1) {
282       W_ALOGW("line %d: malformed unsigned value (ignored)", linecount);
283     } else {
284       values vals;
285       auto iit = u_info.find(key);
286       assert(iit != u_info.end());
287       vals = iit->second;
288       if (uvalue < vals.minv || uvalue > vals.maxv) {
289         W_ALOGW("line %d: specified value %u for '%s' "
290                 "outside permitted range [%u %u] (ignored)",
291                 linecount, uvalue, key, vals.minv, vals.maxv);
292       } else {
293         if (trace_config_read) {
294           W_ALOGI("option %s set to %u", key, uvalue);
295         }
296         uit->second = uvalue;
297       }
298     }
299     trace_config_read = (getUnsignedValue("trace_config_read") != 0);
300     return;
301   }
302
303   auto sit = s_entries.find(key);
304   if (sit != s_entries.end()) {
305     if (trace_config_read) {
306       W_ALOGI("option %s set to %s", key, value);
307     }
308     sit->second = std::string(value);
309     return;
310   }
311
312   W_ALOGW("line %d: unknown option '%s' ignored", linecount, key);
313 }
314
315 static bool isblank(const std::string &line)
316 {
317   for (std::string::const_iterator it = line.begin(); it != line.end(); ++it)
318   {
319     if (isspace(*it) == 0) {
320       return false;
321     }
322   }
323   return true;
324 }
325
326 void ConfigReader::readFile(bool initial)
327 {
328   FILE *fp = fopen(config_file_path, "r");
329   if (!fp) {
330     if (initial) {
331       W_ALOGE("unable to open configuration file %s", config_file_path);
332     }
333     return;
334   }
335
336   char *linebuf = NULL;
337   size_t line_length = 0;
338   for (unsigned linecount = 1;
339        getline(&linebuf, &line_length, fp) != -1;
340        ++linecount) {
341     char *eq = 0;
342     char *key, *value;
343
344     // comment line?
345     if (linebuf[0] == '#') {
346       continue;
347     }
348
349     // blank line?
350     if (isblank(linebuf)) {
351       continue;
352     }
353
354     // look for X=Y assignment
355     eq = strchr(linebuf, '=');
356     if (!eq) {
357       W_ALOGW("line %d: line malformed (no '=' found)", linecount);
358       continue;
359     }
360
361     *eq = '\0';
362     key = linebuf;
363     value = eq+1;
364     char *ln = strrchr(value, '\n');
365     if (ln) { *ln = '\0'; }
366
367     parseLine(key, value, linecount);
368   }
369   free(linebuf);
370   fclose(fp);
371 }
372
373 //
374 // Parse command line args. Currently you can supply "-c P" to set
375 // the path of the config file to P.
376 //
377 static void parse_args(int argc, char** argv)
378 {
379   int ac;
380
381   for (ac = 1; ac < argc; ++ac) {
382     if (!strcmp(argv[ac], "-c")) {
383       if (ac >= argc-1) {
384         W_ALOGE("malformed command line: -c option requires argument)");
385         continue;
386       }
387       config_file_path = strdup(argv[ac+1]);
388       W_ALOGI("config file path set to %s", config_file_path);
389       ++ac;
390     } else {
391       W_ALOGE("malformed command line: unknown option or arg %s)", argv[ac]);
392       continue;
393     }
394   }
395 }
396
397 //
398 // Convert a CKPROFILE_RESULT to a string
399 //
400 const char *ckprofile_result_to_string(CKPROFILE_RESULT result)
401 {
402   switch (result) {
403     case DO_COLLECT_PROFILE:
404       return "DO_COLLECT_PROFILE";
405     case DONT_PROFILE_MISSING_CONFIG_DIR:
406       return "missing config directory";
407     case DONT_PROFILE_MISSING_SEMAPHORE:
408       return "missing semaphore file";
409     case DONT_PROFILE_MISSING_PERF_EXECUTABLE:
410       return "missing 'perf' executable";
411     case DONT_PROFILE_RUNNING_IN_EMULATOR:
412       return "running in emulator";
413     default: return "unknown";
414   }
415   return "notreached";
416 }
417
418 //
419 // Convert a PROFILE_RESULT to a string
420 //
421 const char *profile_result_to_string(PROFILE_RESULT result)
422 {
423   switch(result) {
424     case OK_PROFILE_COLLECTION:
425       return "profile collection succeeded";
426     case ERR_FORK_FAILED:
427       return "fork() system call failed";
428     case ERR_PERF_RECORD_FAILED:
429       return "perf record returned bad exit status";
430     case ERR_PERF_ENCODE_FAILED:
431       return "failure encoding perf.data to protobuf";
432     case ERR_OPEN_ENCODED_FILE_FAILED:
433       return "failed to open encoded perf file";
434     case ERR_WRITE_ENCODED_FILE_FAILED:
435       return "write to encoded perf file failed";
436     default: return "unknown";
437   }
438   return "notreached";
439 }
440
441 //
442 // Check to see whether we should perform a profile collection
443 //
444 static CKPROFILE_RESULT check_profiling_enabled(ConfigReader &config)
445 {
446   //
447   // Profile collection in the emulator doesn't make sense
448   //
449   assert(running_in_emulator != -1);
450   if (running_in_emulator) {
451     return DONT_PROFILE_RUNNING_IN_EMULATOR;
452   }
453
454   //
455   // Check for existence of semaphore file in config directory
456   //
457   if (access(config.getStringValue("config_directory").c_str(), F_OK) == -1) {
458     W_ALOGW("unable to open config directory %s: (%s)",
459             config.getStringValue("config_directory").c_str(), strerror(errno));
460     return DONT_PROFILE_MISSING_CONFIG_DIR;
461   }
462
463
464   // Check for existence of semaphore file
465   std::string semaphore_filepath = config.getStringValue("config_directory")
466                                    + "/" + SEMAPHORE_FILENAME;
467   if (access(semaphore_filepath.c_str(), F_OK) == -1) {
468     return DONT_PROFILE_MISSING_SEMAPHORE;
469   }
470
471   // Check for existence of simpleperf/perf executable
472   std::string pp = config.getStringValue("perf_path");
473   if (access(pp.c_str(), R_OK|X_OK) == -1) {
474     W_ALOGW("unable to access/execute %s", pp.c_str());
475     return DONT_PROFILE_MISSING_PERF_EXECUTABLE;
476   }
477
478   //
479   // We are good to go
480   //
481   return DO_COLLECT_PROFILE;
482 }
483
484 inline char* string_as_array(std::string* str) {
485   return str->empty() ? NULL : &*str->begin();
486 }
487
488 PROFILE_RESULT encode_to_proto(const std::string &data_file_path,
489                                const char *encoded_file_path)
490 {
491   //
492   // Open and read perf.data file
493   //
494   const wireless_android_play_playlog::AndroidPerfProfile &encodedProfile =
495       wireless_android_logging_awp::RawPerfDataToAndroidPerfProfile(data_file_path);
496
497   //
498   // Issue error if no samples
499   //
500   if (encodedProfile.programs().size() == 0) {
501     return ERR_PERF_ENCODE_FAILED;
502   }
503
504   //
505   // Serialize protobuf to array
506   //
507   int size = encodedProfile.ByteSize();
508   std::string data;
509   data.resize(size);
510   ::google::protobuf::uint8* dtarget =
511         reinterpret_cast<::google::protobuf::uint8*>(string_as_array(&data));
512   encodedProfile.SerializeWithCachedSizesToArray(dtarget);
513
514   //
515   // Open file and write encoded data to it
516   //
517   FILE *fp = fopen(encoded_file_path, "w");
518   if (!fp) {
519     return ERR_OPEN_ENCODED_FILE_FAILED;
520   }
521   size_t fsiz = size;
522   if (fwrite(dtarget, fsiz, 1, fp) != 1) {
523     fclose(fp);
524     return ERR_WRITE_ENCODED_FILE_FAILED;
525   }
526   fclose(fp);
527   chmod(encoded_file_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
528
529   return OK_PROFILE_COLLECTION;
530 }
531
532 //
533 // Invoke "perf record". Return value is OK_PROFILE_COLLECTION for
534 // success, or some other error code if something went wrong.
535 //
536 static PROFILE_RESULT invoke_perf(const std::string &perf_path,
537                                   unsigned sampling_period,
538                                   const char *stack_profile_opt,
539                                   unsigned duration,
540                                   const std::string &data_file_path,
541                                   const std::string &perf_stderr_path)
542 {
543   pid_t pid = fork();
544
545   if (pid == -1) {
546     return ERR_FORK_FAILED;
547   }
548
549   if (pid == 0) {
550     // child
551
552     // Open file to receive stderr/stdout from perf
553     FILE *efp = fopen(perf_stderr_path.c_str(), "w");
554     if (efp) {
555       dup2(fileno(efp), STDERR_FILENO);
556       dup2(fileno(efp), STDOUT_FILENO);
557     } else {
558       W_ALOGW("unable to open %s for writing", perf_stderr_path.c_str());
559     }
560
561     // marshall arguments
562     constexpr unsigned max_args = 12;
563     const char *argv[max_args];
564     unsigned slot = 0;
565     argv[slot++] = perf_path.c_str();
566     argv[slot++] = "record";
567
568     // -o perf.data
569     argv[slot++] = "-o";
570     argv[slot++] = data_file_path.c_str();
571
572     // -c N
573     argv[slot++] = "-c";
574     std::string p_str = android::base::StringPrintf("%u", sampling_period);
575     argv[slot++] = p_str.c_str();
576
577     // -g if desired
578     if (stack_profile_opt)
579       argv[slot++] = stack_profile_opt;
580
581     // system wide profiling
582     argv[slot++] = "-a";
583
584     // sleep <duration>
585     argv[slot++] = "/system/bin/sleep";
586     std::string d_str = android::base::StringPrintf("%u", duration);
587     argv[slot++] = d_str.c_str();
588
589     // terminator
590     argv[slot++] = nullptr;
591     assert(slot < max_args);
592
593     // record the final command line in the error output file for
594     // posterity/debugging purposes
595     fprintf(stderr, "perf invocation (pid=%d):\n", getpid());
596     for (unsigned i = 0; argv[i] != nullptr; ++i) {
597       fprintf(stderr, "%s%s", i ? " " : "", argv[i]);
598     }
599     fprintf(stderr, "\n");
600
601     // exec
602     execvp(argv[0], (char * const *)argv);
603     fprintf(stderr, "exec failed: %s\n", strerror(errno));
604     exit(1);
605
606   } else {
607     // parent
608     int st = 0;
609     pid_t reaped = TEMP_FAILURE_RETRY(waitpid(pid, &st, 0));
610
611     if (reaped == -1) {
612       W_ALOGW("waitpid failed: %s", strerror(errno));
613     } else if (WIFSIGNALED(st)) {
614       W_ALOGW("perf killed by signal %d", WTERMSIG(st));
615     } else if (WEXITSTATUS(st) != 0) {
616       W_ALOGW("perf bad exit status %d", WEXITSTATUS(st));
617     } else {
618       return OK_PROFILE_COLLECTION;
619     }
620   }
621
622   return ERR_PERF_RECORD_FAILED;
623 }
624
625 //
626 // Remove all files in the destination directory during initialization
627 //
628 static void cleanup_destination_dir(const ConfigReader &config)
629 {
630   std::string dest_dir = config.getStringValue("destination_directory");
631   DIR* dir = opendir(dest_dir.c_str());
632   if (dir != NULL) {
633     struct dirent* e;
634     while ((e = readdir(dir)) != 0) {
635       if (e->d_name[0] != '.') {
636         std::string file_path = dest_dir + "/" + e->d_name;
637         remove(file_path.c_str());
638       }
639     }
640     closedir(dir);
641   } else {
642     W_ALOGW("unable to open destination dir %s for cleanup",
643             dest_dir.c_str());
644   }
645 }
646
647 //
648 // Post-processes after profile is collected and converted to protobuf.
649 // * GMS core stores processed file sequence numbers in
650 //   /data/data/com.google.android.gms/files/perfprofd_processed.txt
651 // * Update /data/misc/perfprofd/perfprofd_produced.txt to remove the sequence
652 //   numbers that have been processed and append the current seq number
653 // Returns true if the current_seq should increment.
654 //
655 static bool post_process(const ConfigReader &config, int current_seq)
656 {
657   std::string dest_dir = config.getStringValue("destination_directory");
658   std::string processed_file_path =
659       config.getStringValue("config_directory") + "/" + PROCESSED_FILENAME;
660   std::string produced_file_path = dest_dir + "/" + PRODUCED_FILENAME;
661
662
663   std::set<int> processed;
664   FILE *fp = fopen(processed_file_path.c_str(), "r");
665   if (fp != NULL) {
666     int seq;
667     while(fscanf(fp, "%d\n", &seq) > 0) {
668       if (remove(android::base::StringPrintf(
669           "%s/perf.data.encoded.%d", dest_dir.c_str(),seq).c_str()) == 0) {
670         processed.insert(seq);
671       }
672     }
673     fclose(fp);
674   }
675
676   std::set<int> produced;
677   fp = fopen(produced_file_path.c_str(), "r");
678   if (fp != NULL) {
679     int seq;
680     while(fscanf(fp, "%d\n", &seq) > 0) {
681       if (processed.find(seq) == processed.end()) {
682         produced.insert(seq);
683       }
684     }
685     fclose(fp);
686   }
687
688   unsigned maxLive = config.getUnsignedValue("max_unprocessed_profiles");
689   if (produced.size() >= maxLive) {
690     return false;
691   }
692
693   produced.insert(current_seq);
694   fp = fopen(produced_file_path.c_str(), "w");
695   if (fp == NULL) {
696     W_ALOGW("Cannot write %s", produced_file_path.c_str());
697     return false;
698   }
699   for (std::set<int>::const_iterator iter = produced.begin();
700        iter != produced.end(); ++iter) {
701     fprintf(fp, "%d\n", *iter);
702   }
703   fclose(fp);
704   chmod(produced_file_path.c_str(),
705         S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
706   return true;
707 }
708
709 //
710 // Collect a perf profile. Steps for this operation are:
711 // - kick off 'perf record'
712 // - read perf.data, convert to protocol buf
713 //
714 static PROFILE_RESULT collect_profile(const ConfigReader &config, int seq)
715 {
716   //
717   // Form perf.data file name, perf error output file name
718   //
719   std::string destdir = config.getStringValue("destination_directory");
720   std::string data_file_path(destdir);
721   data_file_path += "/";
722   data_file_path += PERF_OUTPUT;
723   std::string perf_stderr_path(destdir);
724   perf_stderr_path += "/perferr.txt";
725
726   //
727   // Remove any existing perf.data file -- if we don't do this, perf
728   // will rename the old file and we'll have extra cruft lying around.
729   //
730   struct stat statb;
731   if (stat(data_file_path.c_str(), &statb) == 0) { // if file exists...
732     if (unlink(data_file_path.c_str())) {          // then try to remove
733       W_ALOGW("unable to unlink previous perf.data file");
734     }
735   }
736
737   //
738   // The "mpdecision" daemon can cause problems for profile
739   // collection: if it decides to online a CPU partway through the
740   // 'perf record' run, the activity on that CPU will be invisible to
741   // perf, and if it offlines a CPU during the recording this can
742   // sometimes leave the PMU in an unusable state (dmesg errors of the
743   // form "perfevents: unable to request IRQXXX for ...").  To avoid
744   // these issues, if "mpdecision" is running the helper below will
745   // stop the service and then online all available CPUs. The object
746   // destructor (invoked when this routine terminates) will then
747   // restart the service again when needed.
748   //
749   unsigned duration = config.getUnsignedValue("sample_duration");
750   unsigned hardwire = config.getUnsignedValue("hardwire_cpus");
751   unsigned max_duration = config.getUnsignedValue("hardwire_cpus_max_duration");
752   bool take_action = (hardwire && duration <= max_duration);
753   HardwireCpuHelper helper(take_action);
754
755   //
756   // Invoke perf
757   //
758   const char *stack_profile_opt =
759       (config.getUnsignedValue("stack_profile") != 0 ? "-g" : nullptr);
760   std::string perf_path = config.getStringValue("perf_path");
761   unsigned period = config.getUnsignedValue("sampling_period");
762
763   PROFILE_RESULT ret = invoke_perf(perf_path.c_str(),
764                                   period,
765                                   stack_profile_opt,
766                                   duration,
767                                   data_file_path,
768                                   perf_stderr_path);
769   if (ret != OK_PROFILE_COLLECTION) {
770     return ret;
771   }
772
773   //
774   // Read the resulting perf.data file, encode into protocol buffer, then write
775   // the result to the file perf.data.encoded
776   //
777   std::string path = android::base::StringPrintf(
778       "%s.encoded.%d", data_file_path.c_str(), seq);
779   return encode_to_proto(data_file_path, path.c_str());
780 }
781
782 //
783 // SIGHUP handler. Sending SIGHUP to the daemon can be used to break it
784 // out of a sleep() call so as to trigger a new collection (debugging)
785 //
786 static void sig_hup(int /* signum */)
787 {
788 }
789
790 //
791 // Assuming that we want to collect a profile every N seconds,
792 // randomly partition N into two sub-intervals.
793 //
794 static void determine_before_after(unsigned &sleep_before_collect,
795                                    unsigned &sleep_after_collect,
796                                    unsigned collection_interval)
797 {
798   double frac = erand48(random_seed);
799   sleep_before_collect = (unsigned) (((double)collection_interval) * frac);
800   assert(sleep_before_collect <= collection_interval);
801   sleep_after_collect = collection_interval - sleep_before_collect;
802 }
803
804 //
805 // Set random number generator seed
806 //
807 static void set_seed(ConfigReader &config)
808 {
809   unsigned seed = 0;
810   unsigned use_fixed_seed = config.getUnsignedValue("use_fixed_seed");
811   if (use_fixed_seed) {
812     //
813     // Use fixed user-specified seed
814     //
815     seed = use_fixed_seed;
816   } else {
817     //
818     // Randomized seed
819     //
820     seed = arc4random();
821   }
822   W_ALOGI("random seed set to %u", seed);
823   // Distribute the 32-bit seed into the three 16-bit array
824   // elements. The specific values being written do not especially
825   // matter as long as we are setting them to something based on the seed.
826   random_seed[0] = seed & 0xffff;
827   random_seed[1] = (seed >> 16);
828   random_seed[2] = (random_seed[0] ^ random_seed[1]);
829 }
830
831 //
832 // Initialization
833 //
834 static void init(ConfigReader &config)
835 {
836   config.readFile(true);
837   set_seed(config);
838   cleanup_destination_dir(config);
839
840   char propBuf[PROPERTY_VALUE_MAX];
841   propBuf[0] = '\0';
842   property_get("ro.kernel.qemu", propBuf, "");
843   running_in_emulator = (propBuf[0] == '1');
844   property_get("ro.debuggable", propBuf, "");
845   is_debug_build = (propBuf[0] == '1');
846
847   signal(SIGHUP, sig_hup);
848 }
849
850 //
851 // Main routine:
852 // 1. parse cmd line args
853 // 2. read config file
854 // 3. loop: {
855 //       sleep for a while
856 //       perform a profile collection
857 //    }
858 //
859 int perfprofd_main(int argc, char** argv)
860 {
861   ConfigReader config;
862
863   W_ALOGI("starting Android Wide Profiling daemon");
864
865   parse_args(argc, argv);
866   init(config);
867
868   // Early exit if we're not supposed to run on this build flavor
869   if (is_debug_build != 1 &&
870       config.getUnsignedValue("only_debug_build") == 1) {
871     W_ALOGI("early exit due to inappropriate build type");
872     return 0;
873   }
874
875   unsigned iterations = 0;
876   int seq = 0;
877   while(config.getUnsignedValue("main_loop_iterations") == 0 ||
878         iterations < config.getUnsignedValue("main_loop_iterations")) {
879
880     // Figure out where in the collection interval we're going to actually
881     // run perf
882     unsigned sleep_before_collect = 0;
883     unsigned sleep_after_collect = 0;
884     determine_before_after(sleep_before_collect, sleep_after_collect,
885                            config.getUnsignedValue("collection_interval"));
886     perfprofd_sleep(sleep_before_collect);
887
888     // Reread config file -- the uploader may have rewritten it as a result
889     // of a gservices change
890     config.readFile(false);
891
892     // Check for profiling enabled...
893     CKPROFILE_RESULT ckresult = check_profiling_enabled(config);
894     if (ckresult != DO_COLLECT_PROFILE) {
895       W_ALOGI("profile collection skipped (%s)",
896               ckprofile_result_to_string(ckresult));
897     } else {
898       // Kick off the profiling run...
899       W_ALOGI("initiating profile collection");
900       PROFILE_RESULT result = collect_profile(config, seq);
901       if (result != OK_PROFILE_COLLECTION) {
902         W_ALOGI("profile collection failed (%s)",
903                 profile_result_to_string(result));
904       } else {
905         if (post_process(config, seq)) {
906           seq++;
907         }
908         W_ALOGI("profile collection complete");
909       }
910     }
911     perfprofd_sleep(sleep_after_collect);
912     iterations += 1;
913   }
914
915   W_ALOGI("finishing Android Wide Profiling daemon");
916   return 0;
917 }