3 ** Copyright 2015, The Android Open Source Project
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
9 ** http://www.apache.org/licenses/LICENSE-2.0
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.
27 #include <sys/types.h>
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>
46 #include <android-base/properties.h>
49 #include "perfprofd_record.pb.h"
51 #include "configreader.h"
52 #include "cpuconfig.h"
53 #include "perf_data_converter.h"
54 #include "perfprofdcore.h"
55 #include "symbolizer.h"
58 // Perf profiling daemon -- collects system-wide profiles using
60 // simpleperf record -a
62 // and encodes them so that they can be uploaded by a separate service.
65 //......................................................................
67 using ProtoUniquePtr = std::unique_ptr<android::perfprofd::PerfprofdRecord>;
70 // Output file from 'perf record'.
72 #define PERF_OUTPUT "perf.data"
75 // This enum holds the results of the "should we profile" configuration check.
79 // All systems go for profile collection.
82 // The selected configuration directory doesn't exist.
83 DONT_PROFILE_MISSING_CONFIG_DIR,
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,
91 // No perf executable present
92 DONT_PROFILE_MISSING_PERF_EXECUTABLE,
94 // We're running in the emulator, perf won't be able to do much
95 DONT_PROFILE_RUNNING_IN_EMULATOR
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.
103 static int running_in_emulator = -1;
106 // Is this a debug build ('userdebug' or 'eng')?
107 // Starts as uninitialized (-1), then set to 1 or 0 at init time.
109 static int is_debug_build = -1;
112 // Path to the perf file to convert and exit? Empty value is the default, daemon mode.
114 static std::string perf_file_to_convert = "";
117 // Random number generator seed (set at startup time).
119 static unsigned short random_seed[3];
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)
125 static void sig_hup(int /* signum */)
127 LOG(WARNING) << "SIGHUP received";
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.
137 static void parse_args(int argc, char** argv)
141 for (ac = 1; ac < argc; ++ac) {
142 if (!strcmp(argv[ac], "-c")) {
144 LOG(ERROR) << "malformed command line: -c option requires argument)";
147 ConfigReader::setConfigFilePath(argv[ac+1]);
149 } else if (!strcmp(argv[ac], "-x")) {
151 LOG(ERROR) << "malformed command line: -x option requires argument)";
154 perf_file_to_convert = argv[ac+1];
157 LOG(ERROR) << "malformed command line: unknown option or arg " << argv[ac] << ")";
164 // Convert a CKPROFILE_RESULT to a string
166 const char *ckprofile_result_to_string(CKPROFILE_RESULT 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";
185 // Convert a PROFILE_RESULT to a string
187 const char *profile_result_to_string(PROFILE_RESULT 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";
208 // Check to see whether we should perform a profile collection
210 static CKPROFILE_RESULT check_profiling_enabled(const Config& config)
213 // Profile collection in the emulator doesn't make sense
215 assert(running_in_emulator != -1);
216 if (running_in_emulator) {
217 return DONT_PROFILE_RUNNING_IN_EMULATOR;
220 if (!config.IsProfilingEnabled()) {
221 return DONT_PROFILE_MISSING_CONFIG_DIR;
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;
234 return DO_COLLECT_PROFILE;
240 return android::base::GetBoolProperty("sys.boot_completed", false) != true;
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:
251 // pid_t child_pid = ...;
252 // { AlarmHelper h(10, child_pid);
253 // ... = read_from_child(child_pid, ...);
256 // NB: this helper is not re-entrant-- avoid nested use or
257 // use by multiple threads
261 AlarmHelper(unsigned num_seconds, pid_t child)
263 struct sigaction sigact;
266 memset(&sigact, 0, sizeof(sigact));
267 sigact.sa_sigaction = handler;
268 sigaction(SIGALRM, &sigact, &oldsigact_);
276 sigaction(SIGALRM, &oldsigact_, NULL);
278 static void handler(int, siginfo_t *, void *);
281 struct sigaction oldsigact_;
285 pid_t AlarmHelper::child_;
287 void AlarmHelper::handler(int, siginfo_t *, void *)
289 LOG(WARNING) << "SIGALRM timeout";
290 kill(child_, SIGKILL);
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".
301 bool get_camera_active()
304 if (pipe2(pipefds, O_CLOEXEC) != 0) {
305 PLOG(ERROR) << "pipe2() failed";
310 PLOG(ERROR) << "fork() failed";
314 } else if (pid == 0) {
317 dup2(pipefds[1], fileno(stderr));
318 dup2(pipefds[1], fileno(stdout));
319 const char *argv[10];
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";
329 AlarmHelper helper(10, pid);
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);
339 std::stringstream ss(dump_output);
341 while (std::getline(ss,line,'\n')) {
342 if (line.find("Camera module API version:") !=
346 if (line.find("No camera module available") !=
348 line.find("No active camera clients yet") !=
350 have_clients = false;
355 // reap child (no zombies please)
357 TEMP_FAILURE_RETRY(waitpid(pid, &st, 0));
358 return have_cam && have_clients;
363 std::string psdir("/sys/class/power_supply");
364 DIR* dir = opendir(psdir.c_str());
366 PLOG(ERROR) << "Failed to open dir " << psdir;
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;
376 if (android::base::ReadFileToString(online_path.c_str(), &contents) &&
377 sscanf(contents.c_str(), "%d", &value) == 1) {
389 bool postprocess_proc_stat_contents(const std::string &pscontents,
390 long unsigned *idleticks,
391 long unsigned *remainingticks)
393 long unsigned usertime, nicetime, systime, idletime, iowaittime;
394 long unsigned irqtime, softirqtime;
396 int rc = sscanf(pscontents.c_str(), "cpu %lu %lu %lu %lu %lu %lu %lu",
397 &usertime, &nicetime, &systime, &idletime,
398 &iowaittime, &irqtime, &softirqtime);
402 *idleticks = idletime;
403 *remainingticks = usertime + nicetime + systime + iowaittime + irqtime + softirqtime;
407 unsigned collect_cpu_utilization()
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)) {
416 if (!postprocess_proc_stat_contents(contents, &idle[iter], &busy[iter])) {
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;
428 static void annotate_encoded_perf_profile(android::perfprofd::PerfprofdRecord* profile,
429 const Config& config,
430 unsigned cpu_utilization)
433 // Incorporate cpu utilization (collected prior to perf run)
435 if (config.collect_cpu_utilization) {
436 profile->set_cpu_utilization(cpu_utilization);
440 // Load average as reported by the kernel
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);
449 PLOG(ERROR) << "Failed to read or scan /proc/loadavg";
453 // Device still booting? Camera in use? Plugged into charger?
455 bool is_booting = get_booting();
456 if (config.collect_booting) {
457 profile->set_booting(is_booting);
459 if (config.collect_camera_active) {
460 profile->set_camera_active(is_booting ? false : get_camera_active());
462 if (config.collect_charging_state) {
463 profile->set_on_charger(get_charging());
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?
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);
476 PLOG(ERROR) << "Failed to read /sys/power/wake_unlock";
480 PROFILE_RESULT SerializeProtobuf(android::perfprofd::PerfprofdRecord* encodedProfile,
481 const char* encoded_file_path) {
483 // Serialize protobuf to array
485 size_t size = encodedProfile->ByteSize();
486 std::unique_ptr<uint8_t[]> data(new uint8_t[size]);
487 encodedProfile->SerializeWithCachedSizesToArray(data.get());
490 // Open file and write encoded data to it
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;
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;
504 return OK_PROFILE_COLLECTION;
507 static ProtoUniquePtr encode_to_proto(const std::string &data_file_path,
508 const Config& config,
509 unsigned cpu_utilization,
510 perfprofd::Symbolizer* symbolizer) {
512 // Open and read perf.data file
514 ProtoUniquePtr encodedProfile(
515 android::perfprofd::RawPerfDataToAndroidPerfProfile(data_file_path, symbolizer));
516 if (encodedProfile == nullptr) {
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);
524 return encodedProfile;
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)
533 ProtoUniquePtr encodedProfile = encode_to_proto(data_file_path,
539 // Issue error if no samples
541 if (encodedProfile == nullptr || encodedProfile->perf_data().events_size() == 0) {
542 return ERR_PERF_ENCODE_FAILED;
545 return SerializeProtobuf(encodedProfile.get(), encoded_file_path);
549 // Invoke "perf record". Return value is OK_PROFILE_COLLECTION for
550 // success, or some other error code if something went wrong.
552 static PROFILE_RESULT invoke_perf(Config& config,
553 const std::string &perf_path,
554 unsigned sampling_period,
555 const char *stack_profile_opt,
557 const std::string &data_file_path,
558 const std::string &perf_stderr_path)
563 return ERR_FORK_FAILED;
569 // Open file to receive stderr/stdout from perf
570 FILE *efp = fopen(perf_stderr_path.c_str(), "w");
572 dup2(fileno(efp), STDERR_FILENO);
573 dup2(fileno(efp), STDOUT_FILENO);
575 PLOG(WARNING) << "unable to open " << perf_stderr_path << " for writing";
578 // marshall arguments
579 constexpr unsigned max_args = 14;
580 const char *argv[max_args];
582 argv[slot++] = perf_path.c_str();
583 argv[slot++] = "record";
587 argv[slot++] = data_file_path.c_str();
591 std::string p_str = android::base::StringPrintf("%u", sampling_period);
592 argv[slot++] = p_str.c_str();
595 if (stack_profile_opt)
596 argv[slot++] = stack_profile_opt;
599 if (config.process < 0) {
600 // system wide profiling
604 pid_str = std::to_string(config.process);
605 argv[slot++] = pid_str.c_str();
608 // no need for kernel symbols
609 argv[slot++] = "--no-dump-kernel-symbols";
612 argv[slot++] = "/system/bin/sleep";
613 std::string d_str = android::base::StringPrintf("%u", duration);
614 argv[slot++] = d_str.c_str();
617 argv[slot++] = nullptr;
618 assert(slot < max_args);
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]);
626 fprintf(stderr, "\n");
629 execvp(argv[0], (char * const *)argv);
630 fprintf(stderr, "exec failed: %s\n", strerror(errno));
637 config.Sleep(duration);
639 // We may have been woken up to stop profiling.
640 if (config.ShouldStopProfiling()) {
641 // Send SIGHUP to simpleperf to make it stop.
645 // Wait for the child, so it's reaped correctly.
647 pid_t reaped = TEMP_FAILURE_RETRY(waitpid(pid, &st, 0));
650 PLOG(WARNING) << "waitpid failed";
651 } else if (WIFSIGNALED(st)) {
652 if (WTERMSIG(st) == SIGHUP && config.ShouldStopProfiling()) {
654 return OK_PROFILE_COLLECTION;
656 LOG(WARNING) << "perf killed by signal " << WTERMSIG(st);
657 } else if (WEXITSTATUS(st) != 0) {
658 LOG(WARNING) << "perf bad exit status " << WEXITSTATUS(st);
660 return OK_PROFILE_COLLECTION;
664 return ERR_PERF_RECORD_FAILED;
668 // Remove all files in the destination directory during initialization
670 static void cleanup_destination_dir(const std::string& dest_dir)
672 DIR* dir = opendir(dest_dir.c_str());
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());
683 PLOG(WARNING) << "unable to open destination dir " << dest_dir << " for cleanup";
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.
695 static bool post_process(const Config& config, int current_seq)
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;
703 std::set<int> processed;
704 FILE *fp = fopen(processed_file_path.c_str(), "r");
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);
716 std::set<int> produced;
717 fp = fopen(produced_file_path.c_str(), "r");
720 while(fscanf(fp, "%d\n", &seq) > 0) {
721 if (processed.find(seq) == processed.end()) {
722 produced.insert(seq);
728 uint32_t maxLive = config.max_unprocessed_profiles;
729 if (produced.size() >= maxLive) {
733 produced.insert(current_seq);
734 fp = fopen(produced_file_path.c_str(), "w");
736 PLOG(WARNING) << "Cannot write " << produced_file_path;
739 for (std::set<int>::const_iterator iter = produced.begin();
740 iter != produced.end(); ++iter) {
741 fprintf(fp, "%d\n", *iter);
744 chmod(produced_file_path.c_str(),
745 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
750 // Collect a perf profile. Steps for this operation are:
751 // - kick off 'perf record'
752 // - read perf.data, convert to protocol buf
754 static ProtoUniquePtr collect_profile(Config& config)
757 // Collect cpu utilization if enabled
759 unsigned cpu_utilization = 0;
760 if (config.collect_cpu_utilization) {
761 cpu_utilization = collect_cpu_utilization();
765 // Form perf.data file name, perf error output file name
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";
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.
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";
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.
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);
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;
811 PROFILE_RESULT ret = invoke_perf(config,
818 if (ret != OK_PROFILE_COLLECTION) {
823 // Read the resulting perf.data file, encode into protocol buffer, then write
824 // the result to the file perf.data.encoded
826 std::unique_ptr<perfprofd::Symbolizer> symbolizer;
827 if (config.use_elf_symbolizer) {
828 symbolizer = perfprofd::CreateELFSymbolizer();
830 return encode_to_proto(data_file_path, config, cpu_utilization, symbolizer.get());
834 // Assuming that we want to collect a profile every N seconds,
835 // randomly partition N into two sub-intervals.
837 static void determine_before_after(unsigned &sleep_before_collect,
838 unsigned &sleep_after_collect,
839 unsigned collection_interval)
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;
848 // Set random number generator seed
850 static void set_seed(uint32_t use_fixed_seed)
853 if (use_fixed_seed) {
855 // Use fixed user-specified seed
857 seed = use_fixed_seed;
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]);
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();
887 set_seed(use_fixed_seed);
888 if (dest_dir != nullptr) {
889 cleanup_destination_dir(dest_dir);
893 running_in_emulator = android::base::GetBoolProperty("ro.kernel.qemu", false);
894 is_debug_build = android::base::GetBoolProperty("ro.debuggable", false);
896 running_in_emulator = false;
897 is_debug_build = true;
904 static void init(const Config& config)
906 // TODO: Consider whether we want to clean things or just overwrite.
907 CommonInit(config.use_fixed_seed, nullptr);
910 static void init(ConfigReader &config)
912 if (!config.readFile()) {
913 LOG(ERROR) << "unable to open configuration file " << config.getConfigFilePath();
916 CommonInit(static_cast<uint32_t>(config.getUnsignedValue("use_fixed_seed")),
917 config.getStringValue("destination_directory").c_str());
919 signal(SIGHUP, sig_hup);
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()) {
931 // Figure out where in the collection interval we're going to actually
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);
939 if (config()->ShouldStopProfiling()) {
943 // Run any necessary updates.
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) << ")";
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";
958 // Always report, even a null result.
959 bool handle_result = handler(proto.get(), config());
961 LOG(INFO) << "profile collection complete";
962 } else if (proto != nullptr) {
963 LOG(WARNING) << "profile handling failed";
967 if (config()->ShouldStopProfiling()) {
971 config()->Sleep(sleep_after_collect);
976 void ProfilingLoop(Config& config, HandlerFn handler) {
979 auto config_fn = [&config]() {
982 auto do_nothing = []() {
984 ProfilingLoopImpl(config_fn, do_nothing, handler);
989 // 1. parse cmd line args
990 // 2. read config file
993 // perform a profile collection
996 int perfprofd_main(int argc, char** argv, Config* config)
998 ConfigReader config_reader;
1000 LOG(INFO) << "starting Android Wide Profiling daemon";
1002 parse_args(argc, argv);
1003 init(config_reader);
1004 config_reader.FillConfig(config);
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);
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";
1018 auto config_fn = [config]() {
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);
1028 auto handler = [&seq](android::perfprofd::PerfprofdRecord* proto, Config* handler_config) {
1029 if (proto == nullptr) {
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) {
1041 if (!post_process(*handler_config, seq)) {
1047 ProfilingLoopImpl(config_fn, reread_config, handler);
1049 LOG(INFO) << "finishing Android Wide Profiling daemon";