OSDN Git Service

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