OSDN Git Service

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