OSDN Git Service

Merge "simpleperf: allow more opened files."
[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 <android-base/file.h>
38 #include <android-base/logging.h>
39 #include <android-base/properties.h>
40 #include <android-base/stringprintf.h>
41
42 #include "perfprofdcore.h"
43 #include "perfprofdutils.h"
44 #include "perf_data_converter.h"
45 #include "cpuconfig.h"
46 #include "configreader.h"
47
48 //
49 // Perf profiling daemon -- collects system-wide profiles using
50 //
51 //       simpleperf record -a
52 //
53 // and encodes them so that they can be uploaded by a separate service.
54 //
55
56 //......................................................................
57
58 //
59 // Output file from 'perf record'.
60 //
61 #define PERF_OUTPUT "perf.data"
62
63 //
64 // This enum holds the results of the "should we profile" configuration check.
65 //
66 typedef enum {
67
68   // All systems go for profile collection.
69   DO_COLLECT_PROFILE,
70
71   // The selected configuration directory doesn't exist.
72   DONT_PROFILE_MISSING_CONFIG_DIR,
73
74   // Destination directory does not contain the semaphore file that
75   // the perf profile uploading service creates when it determines
76   // that the user has opted "in" for usage data collection. No
77   // semaphore -> no user approval -> no profiling.
78   DONT_PROFILE_MISSING_SEMAPHORE,
79
80   // No perf executable present
81   DONT_PROFILE_MISSING_PERF_EXECUTABLE,
82
83   // We're running in the emulator, perf won't be able to do much
84   DONT_PROFILE_RUNNING_IN_EMULATOR
85
86 } CKPROFILE_RESULT;
87
88 //
89 // Are we running in the emulator? If so, stub out profile collection
90 // Starts as uninitialized (-1), then set to 1 or 0 at init time.
91 //
92 static int running_in_emulator = -1;
93
94 //
95 // Is this a debug build ('userdebug' or 'eng')?
96 // Starts as uninitialized (-1), then set to 1 or 0 at init time.
97 //
98 static int is_debug_build = -1;
99
100 //
101 // Path to the perf file to convert and exit? Empty value is the default, daemon mode.
102 //
103 static std::string perf_file_to_convert = "";
104
105 //
106 // Random number generator seed (set at startup time).
107 //
108 static unsigned short random_seed[3];
109
110 //
111 // SIGHUP handler. Sending SIGHUP to the daemon can be used to break it
112 // out of a sleep() call so as to trigger a new collection (debugging)
113 //
114 static void sig_hup(int /* signum */)
115 {
116   W_ALOGW("SIGHUP received");
117 }
118
119 //
120 // Parse command line args. Currently supported flags:
121 // *  "-c PATH" sets the path of the config file to PATH.
122 // *  "-x PATH" reads PATH as a perf data file and saves it as a file in
123 //    perf_profile.proto format. ".encoded" suffix is appended to PATH to form
124 //    the output file path.
125 //
126 static void parse_args(int argc, char** argv)
127 {
128   int ac;
129
130   for (ac = 1; ac < argc; ++ac) {
131     if (!strcmp(argv[ac], "-c")) {
132       if (ac >= argc-1) {
133         W_ALOGE("malformed command line: -c option requires argument)");
134         continue;
135       }
136       ConfigReader::setConfigFilePath(argv[ac+1]);
137       ++ac;
138     } else if (!strcmp(argv[ac], "-x")) {
139       if (ac >= argc-1) {
140         W_ALOGE("malformed command line: -x option requires argument)");
141         continue;
142       }
143       perf_file_to_convert = argv[ac+1];
144       ++ac;
145     } else {
146       W_ALOGE("malformed command line: unknown option or arg %s)", argv[ac]);
147       continue;
148     }
149   }
150 }
151
152 //
153 // Convert a CKPROFILE_RESULT to a string
154 //
155 const char *ckprofile_result_to_string(CKPROFILE_RESULT result)
156 {
157   switch (result) {
158     case DO_COLLECT_PROFILE:
159       return "DO_COLLECT_PROFILE";
160     case DONT_PROFILE_MISSING_CONFIG_DIR:
161       return "missing config directory";
162     case DONT_PROFILE_MISSING_SEMAPHORE:
163       return "missing semaphore file";
164     case DONT_PROFILE_MISSING_PERF_EXECUTABLE:
165       return "missing 'perf' executable";
166     case DONT_PROFILE_RUNNING_IN_EMULATOR:
167       return "running in emulator";
168     default: return "unknown";
169   }
170   return "notreached";
171 }
172
173 //
174 // Convert a PROFILE_RESULT to a string
175 //
176 const char *profile_result_to_string(PROFILE_RESULT result)
177 {
178   switch(result) {
179     case OK_PROFILE_COLLECTION:
180       return "profile collection succeeded";
181     case ERR_FORK_FAILED:
182       return "fork() system call failed";
183     case ERR_PERF_RECORD_FAILED:
184       return "perf record returned bad exit status";
185     case ERR_PERF_ENCODE_FAILED:
186       return "failure encoding perf.data to protobuf";
187     case ERR_OPEN_ENCODED_FILE_FAILED:
188       return "failed to open encoded perf file";
189     case ERR_WRITE_ENCODED_FILE_FAILED:
190       return "write to encoded perf file failed";
191     default: return "unknown";
192   }
193   return "notreached";
194 }
195
196 //
197 // Check to see whether we should perform a profile collection
198 //
199 static CKPROFILE_RESULT check_profiling_enabled(ConfigReader &config)
200 {
201   //
202   // Profile collection in the emulator doesn't make sense
203   //
204   assert(running_in_emulator != -1);
205   if (running_in_emulator) {
206     return DONT_PROFILE_RUNNING_IN_EMULATOR;
207   }
208
209   //
210   // Check for existence of semaphore file in config directory
211   //
212   if (access(config.getStringValue("config_directory").c_str(), F_OK) == -1) {
213     W_ALOGW("unable to open config directory %s: (%s)",
214             config.getStringValue("config_directory").c_str(), strerror(errno));
215     return DONT_PROFILE_MISSING_CONFIG_DIR;
216   }
217
218
219   // Check for existence of semaphore file
220   std::string semaphore_filepath = config.getStringValue("config_directory")
221                                    + "/" + SEMAPHORE_FILENAME;
222   if (access(semaphore_filepath.c_str(), F_OK) == -1) {
223     return DONT_PROFILE_MISSING_SEMAPHORE;
224   }
225
226   // Check for existence of simpleperf/perf executable
227   std::string pp = config.getStringValue("perf_path");
228   if (access(pp.c_str(), R_OK|X_OK) == -1) {
229     W_ALOGW("unable to access/execute %s", pp.c_str());
230     return DONT_PROFILE_MISSING_PERF_EXECUTABLE;
231   }
232
233   //
234   // We are good to go
235   //
236   return DO_COLLECT_PROFILE;
237 }
238
239 bool get_booting()
240 {
241   return android::base::GetBoolProperty("sys.boot_completed", false) != true;
242 }
243
244 //
245 // Constructor takes a timeout (in seconds) and a child pid; If an
246 // alarm set for the specified number of seconds triggers, then a
247 // SIGKILL is sent to the child. Destructor resets alarm. Example:
248 //
249 //       pid_t child_pid = ...;
250 //       { AlarmHelper h(10, child_pid);
251 //         ... = read_from_child(child_pid, ...);
252 //       }
253 //
254 // NB: this helper is not re-entrant-- avoid nested use or
255 // use by multiple threads
256 //
257 class AlarmHelper {
258  public:
259   AlarmHelper(unsigned num_seconds, pid_t child)
260   {
261     struct sigaction sigact;
262     assert(child);
263     assert(child_ == 0);
264     memset(&sigact, 0, sizeof(sigact));
265     sigact.sa_sigaction = handler;
266     sigaction(SIGALRM, &sigact, &oldsigact_);
267     child_ = child;
268     alarm(num_seconds);
269   }
270   ~AlarmHelper()
271   {
272     alarm(0);
273     child_ = 0;
274     sigaction(SIGALRM, &oldsigact_, NULL);
275   }
276   static void handler(int, siginfo_t *, void *);
277
278  private:
279   struct sigaction oldsigact_;
280   static pid_t child_;
281 };
282
283 pid_t AlarmHelper::child_;
284
285 void AlarmHelper::handler(int, siginfo_t *, void *)
286 {
287   W_ALOGW("SIGALRM timeout");
288   kill(child_, SIGKILL);
289 }
290
291 //
292 // This implementation invokes "dumpsys media.camera" and inspects the
293 // output to determine if any camera clients are active. NB: this is
294 // currently disable (via config option) until the selinux issues can
295 // be sorted out. Another possible implementation (not yet attempted)
296 // would be to use the binder to call into the native camera service
297 // via "ICameraService".
298 //
299 bool get_camera_active()
300 {
301   int pipefds[2];
302   if (pipe2(pipefds, O_CLOEXEC) != 0) {
303     W_ALOGE("pipe2() failed (%s)", strerror(errno));
304     return false;
305   }
306   pid_t pid = fork();
307   if (pid == -1) {
308     W_ALOGE("fork() failed (%s)", strerror(errno));
309     close(pipefds[0]);
310     close(pipefds[1]);
311     return false;
312   } else if (pid == 0) {
313     // child
314     close(pipefds[0]);
315     dup2(pipefds[1], fileno(stderr));
316     dup2(pipefds[1], fileno(stdout));
317     const char *argv[10];
318     unsigned slot = 0;
319     argv[slot++] = "/system/bin/dumpsys";
320     argv[slot++] = "media.camera";
321     argv[slot++] = nullptr;
322     execvp(argv[0], (char * const *)argv);
323     W_ALOGE("execvp() failed (%s)", strerror(errno));
324     return false;
325   }
326   // parent
327   AlarmHelper helper(10, pid);
328   close(pipefds[1]);
329
330   // read output
331   bool have_cam = false;
332   bool have_clients = true;
333   std::string dump_output;
334   bool result = android::base::ReadFdToString(pipefds[0], &dump_output);
335   close(pipefds[0]);
336   if (result) {
337     std::stringstream ss(dump_output);
338     std::string line;
339     while (std::getline(ss,line,'\n')) {
340       if (line.find("Camera module API version:") !=
341           std::string::npos) {
342         have_cam = true;
343       }
344       if (line.find("No camera module available") !=
345           std::string::npos ||
346           line.find("No active camera clients yet") !=
347           std::string::npos) {
348         have_clients = false;
349       }
350     }
351   }
352
353   // reap child (no zombies please)
354   int st = 0;
355   TEMP_FAILURE_RETRY(waitpid(pid, &st, 0));
356   return have_cam && have_clients;
357 }
358
359 bool get_charging()
360 {
361   std::string psdir("/sys/class/power_supply");
362   DIR* dir = opendir(psdir.c_str());
363   if (dir == NULL) {
364     W_ALOGE("Failed to open dir %s (%s)", psdir.c_str(), strerror(errno));
365     return false;
366   }
367   struct dirent* e;
368   bool result = false;
369   while ((e = readdir(dir)) != 0) {
370     if (e->d_name[0] != '.') {
371       std::string online_path = psdir + "/" + e->d_name + "/online";
372       std::string contents;
373       int value = 0;
374       if (android::base::ReadFileToString(online_path.c_str(), &contents) &&
375           sscanf(contents.c_str(), "%d", &value) == 1) {
376         if (value) {
377           result = true;
378           break;
379         }
380       }
381     }
382   }
383   closedir(dir);
384   return result;
385 }
386
387 bool postprocess_proc_stat_contents(const std::string &pscontents,
388                                     long unsigned *idleticks,
389                                     long unsigned *remainingticks)
390 {
391   long unsigned usertime, nicetime, systime, idletime, iowaittime;
392   long unsigned irqtime, softirqtime;
393
394   int rc = sscanf(pscontents.c_str(), "cpu  %lu %lu %lu %lu %lu %lu %lu",
395                   &usertime, &nicetime, &systime, &idletime,
396                   &iowaittime, &irqtime, &softirqtime);
397   if (rc != 7) {
398     return false;
399   }
400   *idleticks = idletime;
401   *remainingticks = usertime + nicetime + systime + iowaittime + irqtime + softirqtime;
402   return true;
403 }
404
405 unsigned collect_cpu_utilization()
406 {
407   std::string contents;
408   long unsigned idle[2];
409   long unsigned busy[2];
410   for (unsigned iter = 0; iter < 2; ++iter) {
411     if (!android::base::ReadFileToString("/proc/stat", &contents)) {
412       return 0;
413     }
414     if (!postprocess_proc_stat_contents(contents, &idle[iter], &busy[iter])) {
415       return 0;
416     }
417     if (iter == 0) {
418       sleep(1);
419     }
420   }
421   long unsigned total_delta = (idle[1] + busy[1]) - (idle[0] + busy[0]);
422   long unsigned busy_delta = busy[1] - busy[0];
423   return busy_delta * 100 / total_delta;
424 }
425
426 static void annotate_encoded_perf_profile(wireless_android_play_playlog::AndroidPerfProfile *profile,
427                                           const ConfigReader &config,
428                                           unsigned cpu_utilization)
429 {
430   //
431   // Incorporate cpu utilization (collected prior to perf run)
432   //
433   if (config.getUnsignedValue("collect_cpu_utilization")) {
434     profile->set_cpu_utilization(cpu_utilization);
435   }
436
437   //
438   // Load average as reported by the kernel
439   //
440   std::string load;
441   double fload = 0.0;
442   if (android::base::ReadFileToString("/proc/loadavg", &load) &&
443       sscanf(load.c_str(), "%lf", &fload) == 1) {
444     int iload = static_cast<int>(fload * 100.0);
445     profile->set_sys_load_average(iload);
446   } else {
447     W_ALOGE("Failed to read or scan /proc/loadavg (%s)", strerror(errno));
448   }
449
450   //
451   // Device still booting? Camera in use? Plugged into charger?
452   //
453   bool is_booting = get_booting();
454   if (config.getUnsignedValue("collect_booting")) {
455     profile->set_booting(is_booting);
456   }
457   if (config.getUnsignedValue("collect_camera_active")) {
458     profile->set_camera_active(is_booting ? false : get_camera_active());
459   }
460   if (config.getUnsignedValue("collect_charging_state")) {
461     profile->set_on_charger(get_charging());
462   }
463
464   //
465   // Examine the contents of wake_unlock to determine whether the
466   // device display is on or off. NB: is this really the only way to
467   // determine this info?
468   //
469   std::string disp;
470   if (android::base::ReadFileToString("/sys/power/wake_unlock", &disp)) {
471     bool ison = (strstr(disp.c_str(), "PowerManagerService.Display") == 0);
472     profile->set_display_on(ison);
473   } else {
474     W_ALOGE("Failed to read /sys/power/wake_unlock (%s)", strerror(errno));
475   }
476 }
477
478 inline char* string_as_array(std::string* str) {
479   return str->empty() ? NULL : &*str->begin();
480 }
481
482 PROFILE_RESULT encode_to_proto(const std::string &data_file_path,
483                                const char *encoded_file_path,
484                                const ConfigReader &config,
485                                unsigned cpu_utilization)
486 {
487   //
488   // Open and read perf.data file
489   //
490   const wireless_android_play_playlog::AndroidPerfProfile &encodedProfile =
491       wireless_android_logging_awp::RawPerfDataToAndroidPerfProfile(data_file_path);
492
493   //
494   // Issue error if no samples
495   //
496   if (encodedProfile.programs().size() == 0) {
497     return ERR_PERF_ENCODE_FAILED;
498   }
499
500   // All of the info in 'encodedProfile' is derived from the perf.data file;
501   // here we tack display status, cpu utilization, system load, etc.
502   wireless_android_play_playlog::AndroidPerfProfile &prof =
503       const_cast<wireless_android_play_playlog::AndroidPerfProfile&>
504       (encodedProfile);
505   annotate_encoded_perf_profile(&prof, config, cpu_utilization);
506
507   //
508   // Serialize protobuf to array
509   //
510   int size = encodedProfile.ByteSize();
511   std::string data;
512   data.resize(size);
513   ::google::protobuf::uint8* dtarget =
514         reinterpret_cast<::google::protobuf::uint8*>(string_as_array(&data));
515   encodedProfile.SerializeWithCachedSizesToArray(dtarget);
516
517   //
518   // Open file and write encoded data to it
519   //
520   FILE *fp = fopen(encoded_file_path, "w");
521   if (!fp) {
522     return ERR_OPEN_ENCODED_FILE_FAILED;
523   }
524   size_t fsiz = size;
525   if (fwrite(dtarget, fsiz, 1, fp) != 1) {
526     fclose(fp);
527     return ERR_WRITE_ENCODED_FILE_FAILED;
528   }
529   fclose(fp);
530   chmod(encoded_file_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
531
532   return OK_PROFILE_COLLECTION;
533 }
534
535 //
536 // Invoke "perf record". Return value is OK_PROFILE_COLLECTION for
537 // success, or some other error code if something went wrong.
538 //
539 static PROFILE_RESULT invoke_perf(const std::string &perf_path,
540                                   unsigned sampling_period,
541                                   const char *stack_profile_opt,
542                                   unsigned duration,
543                                   const std::string &data_dir_path,
544                                   const std::string &data_file_path,
545                                   const std::string &perf_stderr_path)
546 {
547   pid_t pid = fork();
548
549   if (pid == -1) {
550     return ERR_FORK_FAILED;
551   }
552
553   if (pid == 0) {
554     // child
555
556     // Workaround for temporary files.
557     if (chdir(data_dir_path.c_str()) != 0) {
558       PLOG(WARNING) << "Could not change working directory to " << data_dir_path;
559     }
560
561     // Open file to receive stderr/stdout from perf
562     FILE *efp = fopen(perf_stderr_path.c_str(), "w");
563     if (efp) {
564       dup2(fileno(efp), STDERR_FILENO);
565       dup2(fileno(efp), STDOUT_FILENO);
566     } else {
567       W_ALOGW("unable to open %s for writing", perf_stderr_path.c_str());
568     }
569
570     // marshall arguments
571     constexpr unsigned max_args = 13;
572     const char *argv[max_args];
573     unsigned slot = 0;
574     argv[slot++] = perf_path.c_str();
575     argv[slot++] = "record";
576
577     // -o perf.data
578     argv[slot++] = "-o";
579     argv[slot++] = data_file_path.c_str();
580
581     // -c N
582     argv[slot++] = "-c";
583     std::string p_str = android::base::StringPrintf("%u", sampling_period);
584     argv[slot++] = p_str.c_str();
585
586     // -g if desired
587     if (stack_profile_opt)
588       argv[slot++] = stack_profile_opt;
589
590     // system wide profiling
591     argv[slot++] = "-a";
592
593     // no need for kernel symbols
594     argv[slot++] = "--no-dump-kernel-symbols";
595
596     // sleep <duration>
597     argv[slot++] = "/system/bin/sleep";
598     std::string d_str = android::base::StringPrintf("%u", duration);
599     argv[slot++] = d_str.c_str();
600
601     // terminator
602     argv[slot++] = nullptr;
603     assert(slot < max_args);
604
605     // record the final command line in the error output file for
606     // posterity/debugging purposes
607     fprintf(stderr, "perf invocation (pid=%d):\n", getpid());
608     for (unsigned i = 0; argv[i] != nullptr; ++i) {
609       fprintf(stderr, "%s%s", i ? " " : "", argv[i]);
610     }
611     fprintf(stderr, "\n");
612
613     // exec
614     execvp(argv[0], (char * const *)argv);
615     fprintf(stderr, "exec failed: %s\n", strerror(errno));
616     exit(1);
617
618   } else {
619     // parent
620     int st = 0;
621     pid_t reaped = TEMP_FAILURE_RETRY(waitpid(pid, &st, 0));
622
623     if (reaped == -1) {
624       W_ALOGW("waitpid failed: %s", strerror(errno));
625     } else if (WIFSIGNALED(st)) {
626       W_ALOGW("perf killed by signal %d", WTERMSIG(st));
627     } else if (WEXITSTATUS(st) != 0) {
628       W_ALOGW("perf bad exit status %d", WEXITSTATUS(st));
629     } else {
630       return OK_PROFILE_COLLECTION;
631     }
632   }
633
634   return ERR_PERF_RECORD_FAILED;
635 }
636
637 //
638 // Remove all files in the destination directory during initialization
639 //
640 static void cleanup_destination_dir(const ConfigReader &config)
641 {
642   std::string dest_dir = config.getStringValue("destination_directory");
643   DIR* dir = opendir(dest_dir.c_str());
644   if (dir != NULL) {
645     struct dirent* e;
646     while ((e = readdir(dir)) != 0) {
647       if (e->d_name[0] != '.') {
648         std::string file_path = dest_dir + "/" + e->d_name;
649         remove(file_path.c_str());
650       }
651     }
652     closedir(dir);
653   } else {
654     W_ALOGW("unable to open destination dir %s for cleanup",
655             dest_dir.c_str());
656   }
657 }
658
659 //
660 // Post-processes after profile is collected and converted to protobuf.
661 // * GMS core stores processed file sequence numbers in
662 //   /data/data/com.google.android.gms/files/perfprofd_processed.txt
663 // * Update /data/misc/perfprofd/perfprofd_produced.txt to remove the sequence
664 //   numbers that have been processed and append the current seq number
665 // Returns true if the current_seq should increment.
666 //
667 static bool post_process(const ConfigReader &config, int current_seq)
668 {
669   std::string dest_dir = config.getStringValue("destination_directory");
670   std::string processed_file_path =
671       config.getStringValue("config_directory") + "/" + PROCESSED_FILENAME;
672   std::string produced_file_path = dest_dir + "/" + PRODUCED_FILENAME;
673
674
675   std::set<int> processed;
676   FILE *fp = fopen(processed_file_path.c_str(), "r");
677   if (fp != NULL) {
678     int seq;
679     while(fscanf(fp, "%d\n", &seq) > 0) {
680       if (remove(android::base::StringPrintf(
681           "%s/perf.data.encoded.%d", dest_dir.c_str(),seq).c_str()) == 0) {
682         processed.insert(seq);
683       }
684     }
685     fclose(fp);
686   }
687
688   std::set<int> produced;
689   fp = fopen(produced_file_path.c_str(), "r");
690   if (fp != NULL) {
691     int seq;
692     while(fscanf(fp, "%d\n", &seq) > 0) {
693       if (processed.find(seq) == processed.end()) {
694         produced.insert(seq);
695       }
696     }
697     fclose(fp);
698   }
699
700   unsigned maxLive = config.getUnsignedValue("max_unprocessed_profiles");
701   if (produced.size() >= maxLive) {
702     return false;
703   }
704
705   produced.insert(current_seq);
706   fp = fopen(produced_file_path.c_str(), "w");
707   if (fp == NULL) {
708     W_ALOGW("Cannot write %s", produced_file_path.c_str());
709     return false;
710   }
711   for (std::set<int>::const_iterator iter = produced.begin();
712        iter != produced.end(); ++iter) {
713     fprintf(fp, "%d\n", *iter);
714   }
715   fclose(fp);
716   chmod(produced_file_path.c_str(),
717         S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
718   return true;
719 }
720
721 //
722 // Collect a perf profile. Steps for this operation are:
723 // - kick off 'perf record'
724 // - read perf.data, convert to protocol buf
725 //
726 static PROFILE_RESULT collect_profile(const ConfigReader &config, int seq)
727 {
728   //
729   // Collect cpu utilization if enabled
730   //
731   unsigned cpu_utilization = 0;
732   if (config.getUnsignedValue("collect_cpu_utilization")) {
733     cpu_utilization = collect_cpu_utilization();
734   }
735
736   //
737   // Form perf.data file name, perf error output file name
738   //
739   std::string destdir = config.getStringValue("destination_directory");
740   std::string data_dir_path(destdir);
741   std::string data_file_path = data_dir_path + "/" PERF_OUTPUT;
742   std::string perf_stderr_path(destdir);
743   perf_stderr_path += "/perferr.txt";
744
745   //
746   // Remove any existing perf.data file -- if we don't do this, perf
747   // will rename the old file and we'll have extra cruft lying around.
748   //
749   struct stat statb;
750   if (stat(data_file_path.c_str(), &statb) == 0) { // if file exists...
751     if (unlink(data_file_path.c_str())) {          // then try to remove
752       W_ALOGW("unable to unlink previous perf.data file");
753     }
754   }
755
756   //
757   // The "mpdecision" daemon can cause problems for profile
758   // collection: if it decides to online a CPU partway through the
759   // 'perf record' run, the activity on that CPU will be invisible to
760   // perf, and if it offlines a CPU during the recording this can
761   // sometimes leave the PMU in an unusable state (dmesg errors of the
762   // form "perfevents: unable to request IRQXXX for ...").  To avoid
763   // these issues, if "mpdecision" is running the helper below will
764   // stop the service and then online all available CPUs. The object
765   // destructor (invoked when this routine terminates) will then
766   // restart the service again when needed.
767   //
768   unsigned duration = config.getUnsignedValue("sample_duration");
769   unsigned hardwire = config.getUnsignedValue("hardwire_cpus");
770   unsigned max_duration = config.getUnsignedValue("hardwire_cpus_max_duration");
771   bool take_action = (hardwire && duration <= max_duration);
772   HardwireCpuHelper helper(take_action);
773
774   //
775   // Invoke perf
776   //
777   const char *stack_profile_opt =
778       (config.getUnsignedValue("stack_profile") != 0 ? "-g" : nullptr);
779   std::string perf_path = config.getStringValue("perf_path");
780   unsigned period = config.getUnsignedValue("sampling_period");
781
782   PROFILE_RESULT ret = invoke_perf(perf_path.c_str(),
783                                   period,
784                                   stack_profile_opt,
785                                   duration,
786                                   data_dir_path,
787                                   data_file_path,
788                                   perf_stderr_path);
789   if (ret != OK_PROFILE_COLLECTION) {
790     return ret;
791   }
792
793   //
794   // Read the resulting perf.data file, encode into protocol buffer, then write
795   // the result to the file perf.data.encoded
796   //
797   std::string path = android::base::StringPrintf(
798       "%s.encoded.%d", data_file_path.c_str(), seq);
799   return encode_to_proto(data_file_path, path.c_str(), config, cpu_utilization);
800 }
801
802 //
803 // Assuming that we want to collect a profile every N seconds,
804 // randomly partition N into two sub-intervals.
805 //
806 static void determine_before_after(unsigned &sleep_before_collect,
807                                    unsigned &sleep_after_collect,
808                                    unsigned collection_interval)
809 {
810   double frac = erand48(random_seed);
811   sleep_before_collect = (unsigned) (((double)collection_interval) * frac);
812   assert(sleep_before_collect <= collection_interval);
813   sleep_after_collect = collection_interval - sleep_before_collect;
814 }
815
816 //
817 // Set random number generator seed
818 //
819 static void set_seed(ConfigReader &config)
820 {
821   unsigned seed = 0;
822   unsigned use_fixed_seed = config.getUnsignedValue("use_fixed_seed");
823   if (use_fixed_seed) {
824     //
825     // Use fixed user-specified seed
826     //
827     seed = use_fixed_seed;
828   } else {
829     //
830     // Randomized seed
831     //
832     seed = arc4random();
833   }
834   W_ALOGI("random seed set to %u", seed);
835   // Distribute the 32-bit seed into the three 16-bit array
836   // elements. The specific values being written do not especially
837   // matter as long as we are setting them to something based on the seed.
838   random_seed[0] = seed & 0xffff;
839   random_seed[1] = (seed >> 16);
840   random_seed[2] = (random_seed[0] ^ random_seed[1]);
841 }
842
843 //
844 // Initialization
845 //
846 static void init(ConfigReader &config)
847 {
848   if (!config.readFile()) {
849     W_ALOGE("unable to open configuration file %s",
850             config.getConfigFilePath());
851   }
852
853   // Children of init inherit an artificially low OOM score -- this is not
854   // desirable for perfprofd (its OOM score should be on par with
855   // other user processes).
856   std::stringstream oomscore_path;
857   oomscore_path << "/proc/" << getpid() << "/oom_score_adj";
858   if (!android::base::WriteStringToFile("0", oomscore_path.str())) {
859     W_ALOGE("unable to write to %s", oomscore_path.str().c_str());
860   }
861
862   set_seed(config);
863   cleanup_destination_dir(config);
864
865   running_in_emulator = android::base::GetBoolProperty("ro.kernel.qemu", false);
866   is_debug_build = android::base::GetBoolProperty("ro.debuggable", false);
867
868   signal(SIGHUP, sig_hup);
869 }
870
871 //
872 // Main routine:
873 // 1. parse cmd line args
874 // 2. read config file
875 // 3. loop: {
876 //       sleep for a while
877 //       perform a profile collection
878 //    }
879 //
880 int perfprofd_main(int argc, char** argv)
881 {
882   ConfigReader config;
883
884   W_ALOGI("starting Android Wide Profiling daemon");
885
886   parse_args(argc, argv);
887   init(config);
888
889   if (!perf_file_to_convert.empty()) {
890     std::string encoded_path = perf_file_to_convert + ".encoded";
891     encode_to_proto(perf_file_to_convert, encoded_path.c_str(), config, 0);
892     return 0;
893   }
894
895   // Early exit if we're not supposed to run on this build flavor
896   if (is_debug_build != 1 &&
897       config.getUnsignedValue("only_debug_build") == 1) {
898     W_ALOGI("early exit due to inappropriate build type");
899     return 0;
900   }
901
902   unsigned iterations = 0;
903   int seq = 0;
904   while(config.getUnsignedValue("main_loop_iterations") == 0 ||
905         iterations < config.getUnsignedValue("main_loop_iterations")) {
906
907     // Figure out where in the collection interval we're going to actually
908     // run perf
909     unsigned sleep_before_collect = 0;
910     unsigned sleep_after_collect = 0;
911     determine_before_after(sleep_before_collect, sleep_after_collect,
912                            config.getUnsignedValue("collection_interval"));
913     perfprofd_sleep(sleep_before_collect);
914
915     // Reread config file -- the uploader may have rewritten it as a result
916     // of a gservices change
917     config.readFile();
918
919     // Check for profiling enabled...
920     CKPROFILE_RESULT ckresult = check_profiling_enabled(config);
921     if (ckresult != DO_COLLECT_PROFILE) {
922       W_ALOGI("profile collection skipped (%s)",
923               ckprofile_result_to_string(ckresult));
924     } else {
925       // Kick off the profiling run...
926       W_ALOGI("initiating profile collection");
927       PROFILE_RESULT result = collect_profile(config, seq);
928       if (result != OK_PROFILE_COLLECTION) {
929         W_ALOGI("profile collection failed (%s)",
930                 profile_result_to_string(result));
931       } else {
932         if (post_process(config, seq)) {
933           seq++;
934         }
935         W_ALOGI("profile collection complete");
936       }
937     }
938     perfprofd_sleep(sleep_after_collect);
939     iterations += 1;
940   }
941
942   W_ALOGI("finishing Android Wide Profiling daemon");
943   return 0;
944 }