OSDN Git Service

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