OSDN Git Service

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