OSDN Git Service

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