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>
37 #include <android-base/file.h>
38 #include <android-base/macros.h>
39 #include <android-base/properties.h>
40 #include <android-base/stringprintf.h>
42 #include "perfprofdcore.h"
43 #include "perfprofdutils.h"
44 #include "perf_data_converter.h"
45 #include "cpuconfig.h"
46 #include "configreader.h"
49 // Perf profiling daemon -- collects system-wide profiles using
51 // simpleperf record -a
53 // and encodes them so that they can be uploaded by a separate service.
56 //......................................................................
59 // Output file from 'perf record'.
61 #define PERF_OUTPUT "perf.data"
64 // This enum holds the results of the "should we profile" configuration check.
68 // All systems go for profile collection.
71 // The selected configuration directory doesn't exist.
72 DONT_PROFILE_MISSING_CONFIG_DIR,
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,
80 // No perf executable present
81 DONT_PROFILE_MISSING_PERF_EXECUTABLE,
83 // We're running in the emulator, perf won't be able to do much
84 DONT_PROFILE_RUNNING_IN_EMULATOR
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.
92 static int running_in_emulator = -1;
95 // Is this a debug build ('userdebug' or 'eng')?
96 // Starts as uninitialized (-1), then set to 1 or 0 at init time.
98 static int is_debug_build = -1;
101 // Path to the perf file to convert and exit? Empty value is the default, daemon mode.
103 static std::string perf_file_to_convert = "";
106 // Random number generator seed (set at startup time).
108 static unsigned short random_seed[3];
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)
114 static void sig_hup(int /* signum */)
116 W_ALOGW("SIGHUP received");
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.
126 static void parse_args(int argc, char** argv)
130 for (ac = 1; ac < argc; ++ac) {
131 if (!strcmp(argv[ac], "-c")) {
133 W_ALOGE("malformed command line: -c option requires argument)");
136 ConfigReader::setConfigFilePath(argv[ac+1]);
138 } else if (!strcmp(argv[ac], "-x")) {
140 W_ALOGE("malformed command line: -x option requires argument)");
143 perf_file_to_convert = argv[ac+1];
146 W_ALOGE("malformed command line: unknown option or arg %s)", argv[ac]);
153 // Convert a CKPROFILE_RESULT to a string
155 const char *ckprofile_result_to_string(CKPROFILE_RESULT 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";
174 // Convert a PROFILE_RESULT to a string
176 const char *profile_result_to_string(PROFILE_RESULT 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";
197 // Check to see whether we should perform a profile collection
199 static CKPROFILE_RESULT check_profiling_enabled(const Config& config)
202 // Profile collection in the emulator doesn't make sense
204 assert(running_in_emulator != -1);
205 if (running_in_emulator) {
206 return DONT_PROFILE_RUNNING_IN_EMULATOR;
209 if (!config.IsProfilingEnabled()) {
210 return DONT_PROFILE_MISSING_CONFIG_DIR;
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;
223 return DO_COLLECT_PROFILE;
228 return android::base::GetBoolProperty("sys.boot_completed", false) != true;
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:
236 // pid_t child_pid = ...;
237 // { AlarmHelper h(10, child_pid);
238 // ... = read_from_child(child_pid, ...);
241 // NB: this helper is not re-entrant-- avoid nested use or
242 // use by multiple threads
246 AlarmHelper(unsigned num_seconds, pid_t child)
248 struct sigaction sigact;
251 memset(&sigact, 0, sizeof(sigact));
252 sigact.sa_sigaction = handler;
253 sigaction(SIGALRM, &sigact, &oldsigact_);
261 sigaction(SIGALRM, &oldsigact_, NULL);
263 static void handler(int, siginfo_t *, void *);
266 struct sigaction oldsigact_;
270 pid_t AlarmHelper::child_;
272 void AlarmHelper::handler(int, siginfo_t *, void *)
274 W_ALOGW("SIGALRM timeout");
275 kill(child_, SIGKILL);
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".
286 bool get_camera_active()
289 if (pipe2(pipefds, O_CLOEXEC) != 0) {
290 W_ALOGE("pipe2() failed (%s)", strerror(errno));
295 W_ALOGE("fork() failed (%s)", strerror(errno));
299 } else if (pid == 0) {
302 dup2(pipefds[1], fileno(stderr));
303 dup2(pipefds[1], fileno(stdout));
304 const char *argv[10];
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));
314 AlarmHelper helper(10, pid);
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);
324 std::stringstream ss(dump_output);
326 while (std::getline(ss,line,'\n')) {
327 if (line.find("Camera module API version:") !=
331 if (line.find("No camera module available") !=
333 line.find("No active camera clients yet") !=
335 have_clients = false;
340 // reap child (no zombies please)
342 TEMP_FAILURE_RETRY(waitpid(pid, &st, 0));
343 return have_cam && have_clients;
348 std::string psdir("/sys/class/power_supply");
349 DIR* dir = opendir(psdir.c_str());
351 W_ALOGE("Failed to open dir %s (%s)", psdir.c_str(), strerror(errno));
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;
361 if (android::base::ReadFileToString(online_path.c_str(), &contents) &&
362 sscanf(contents.c_str(), "%d", &value) == 1) {
374 bool postprocess_proc_stat_contents(const std::string &pscontents,
375 long unsigned *idleticks,
376 long unsigned *remainingticks)
378 long unsigned usertime, nicetime, systime, idletime, iowaittime;
379 long unsigned irqtime, softirqtime;
381 int rc = sscanf(pscontents.c_str(), "cpu %lu %lu %lu %lu %lu %lu %lu",
382 &usertime, &nicetime, &systime, &idletime,
383 &iowaittime, &irqtime, &softirqtime);
387 *idleticks = idletime;
388 *remainingticks = usertime + nicetime + systime + iowaittime + irqtime + softirqtime;
392 unsigned collect_cpu_utilization()
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)) {
401 if (!postprocess_proc_stat_contents(contents, &idle[iter], &busy[iter])) {
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;
413 static void annotate_encoded_perf_profile(wireless_android_play_playlog::AndroidPerfProfile *profile,
414 const Config& config,
415 unsigned cpu_utilization)
418 // Incorporate cpu utilization (collected prior to perf run)
420 if (config.collect_cpu_utilization) {
421 profile->set_cpu_utilization(cpu_utilization);
425 // Load average as reported by the kernel
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);
434 W_ALOGE("Failed to read or scan /proc/loadavg (%s)", strerror(errno));
438 // Device still booting? Camera in use? Plugged into charger?
440 bool is_booting = get_booting();
441 if (config.collect_booting) {
442 profile->set_booting(is_booting);
444 if (config.collect_camera_active) {
445 profile->set_camera_active(is_booting ? false : get_camera_active());
447 if (config.collect_charging_state) {
448 profile->set_on_charger(get_charging());
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?
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);
461 W_ALOGE("Failed to read /sys/power/wake_unlock (%s)", strerror(errno));
465 inline char* string_as_array(std::string* str) {
466 return str->empty() ? NULL : &*str->begin();
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)
475 // Open and read perf.data file
477 const wireless_android_play_playlog::AndroidPerfProfile &encodedProfile =
478 wireless_android_logging_awp::RawPerfDataToAndroidPerfProfile(data_file_path);
481 // Issue error if no samples
483 if (encodedProfile.programs().size() == 0) {
484 return ERR_PERF_ENCODE_FAILED;
487 // All of the info in 'encodedProfile' is derived from the perf.data file;
488 // here we tack display status, cpu utilization, system load, etc.
489 wireless_android_play_playlog::AndroidPerfProfile &prof =
490 const_cast<wireless_android_play_playlog::AndroidPerfProfile&>
492 annotate_encoded_perf_profile(&prof, config, cpu_utilization);
495 // Serialize protobuf to array
497 int size = encodedProfile.ByteSize();
500 ::google::protobuf::uint8* dtarget =
501 reinterpret_cast<::google::protobuf::uint8*>(string_as_array(&data));
502 encodedProfile.SerializeWithCachedSizesToArray(dtarget);
505 // Open file and write encoded data to it
507 FILE *fp = fopen(encoded_file_path, "w");
509 return ERR_OPEN_ENCODED_FILE_FAILED;
512 if (fwrite(dtarget, fsiz, 1, fp) != 1) {
514 return ERR_WRITE_ENCODED_FILE_FAILED;
517 chmod(encoded_file_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
519 return OK_PROFILE_COLLECTION;
523 // Invoke "perf record". Return value is OK_PROFILE_COLLECTION for
524 // success, or some other error code if something went wrong.
526 static PROFILE_RESULT invoke_perf(Config& config,
527 const std::string &perf_path,
528 unsigned sampling_period,
529 const char *stack_profile_opt,
531 const std::string &data_file_path,
532 const std::string &perf_stderr_path)
537 return ERR_FORK_FAILED;
543 // Open file to receive stderr/stdout from perf
544 FILE *efp = fopen(perf_stderr_path.c_str(), "w");
546 dup2(fileno(efp), STDERR_FILENO);
547 dup2(fileno(efp), STDOUT_FILENO);
549 W_ALOGW("unable to open %s for writing", perf_stderr_path.c_str());
552 // marshall arguments
553 constexpr unsigned max_args = 14;
554 const char *argv[max_args];
556 argv[slot++] = perf_path.c_str();
557 argv[slot++] = "record";
561 argv[slot++] = data_file_path.c_str();
565 std::string p_str = android::base::StringPrintf("%u", sampling_period);
566 argv[slot++] = p_str.c_str();
569 if (stack_profile_opt)
570 argv[slot++] = stack_profile_opt;
573 if (config.process < 0) {
574 // system wide profiling
578 pid_str = std::to_string(config.process);
579 argv[slot++] = pid_str.c_str();
582 // no need for kernel symbols
583 argv[slot++] = "--no-dump-kernel-symbols";
586 argv[slot++] = "/system/bin/sleep";
587 std::string d_str = android::base::StringPrintf("%u", duration);
588 argv[slot++] = d_str.c_str();
591 argv[slot++] = nullptr;
592 assert(slot < max_args);
594 // record the final command line in the error output file for
595 // posterity/debugging purposes
596 fprintf(stderr, "perf invocation (pid=%d):\n", getpid());
597 for (unsigned i = 0; argv[i] != nullptr; ++i) {
598 fprintf(stderr, "%s%s", i ? " " : "", argv[i]);
600 fprintf(stderr, "\n");
603 execvp(argv[0], (char * const *)argv);
604 fprintf(stderr, "exec failed: %s\n", strerror(errno));
611 config.Sleep(duration);
613 // We may have been woken up to stop profiling.
614 if (config.ShouldStopProfiling()) {
615 // Send SIGHUP to simpleperf to make it stop.
619 // Wait for the child, so it's reaped correctly.
621 pid_t reaped = TEMP_FAILURE_RETRY(waitpid(pid, &st, 0));
624 W_ALOGW("waitpid failed: %s", strerror(errno));
625 } else if (WIFSIGNALED(st)) {
626 if (WTERMSIG(st) == SIGHUP && config.ShouldStopProfiling()) {
628 return OK_PROFILE_COLLECTION;
630 W_ALOGW("perf killed by signal %d", WTERMSIG(st));
631 } else if (WEXITSTATUS(st) != 0) {
632 W_ALOGW("perf bad exit status %d", WEXITSTATUS(st));
634 return OK_PROFILE_COLLECTION;
638 return ERR_PERF_RECORD_FAILED;
642 // Remove all files in the destination directory during initialization
644 static void cleanup_destination_dir(const std::string& dest_dir)
646 DIR* dir = opendir(dest_dir.c_str());
649 while ((e = readdir(dir)) != 0) {
650 if (e->d_name[0] != '.') {
651 std::string file_path = dest_dir + "/" + e->d_name;
652 remove(file_path.c_str());
657 W_ALOGW("unable to open destination dir %s for cleanup",
663 // Post-processes after profile is collected and converted to protobuf.
664 // * GMS core stores processed file sequence numbers in
665 // /data/data/com.google.android.gms/files/perfprofd_processed.txt
666 // * Update /data/misc/perfprofd/perfprofd_produced.txt to remove the sequence
667 // numbers that have been processed and append the current seq number
668 // Returns true if the current_seq should increment.
670 static bool post_process(const Config& config, int current_seq)
672 const std::string& dest_dir = config.destination_directory;
673 std::string processed_file_path =
674 config.config_directory + "/" + PROCESSED_FILENAME;
675 std::string produced_file_path = dest_dir + "/" + PRODUCED_FILENAME;
678 std::set<int> processed;
679 FILE *fp = fopen(processed_file_path.c_str(), "r");
682 while(fscanf(fp, "%d\n", &seq) > 0) {
683 if (remove(android::base::StringPrintf(
684 "%s/perf.data.encoded.%d", dest_dir.c_str(),seq).c_str()) == 0) {
685 processed.insert(seq);
691 std::set<int> produced;
692 fp = fopen(produced_file_path.c_str(), "r");
695 while(fscanf(fp, "%d\n", &seq) > 0) {
696 if (processed.find(seq) == processed.end()) {
697 produced.insert(seq);
703 uint32_t maxLive = config.max_unprocessed_profiles;
704 if (produced.size() >= maxLive) {
708 produced.insert(current_seq);
709 fp = fopen(produced_file_path.c_str(), "w");
711 W_ALOGW("Cannot write %s", produced_file_path.c_str());
714 for (std::set<int>::const_iterator iter = produced.begin();
715 iter != produced.end(); ++iter) {
716 fprintf(fp, "%d\n", *iter);
719 chmod(produced_file_path.c_str(),
720 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
725 // Collect a perf profile. Steps for this operation are:
726 // - kick off 'perf record'
727 // - read perf.data, convert to protocol buf
729 static PROFILE_RESULT collect_profile(Config& config, int seq)
732 // Collect cpu utilization if enabled
734 unsigned cpu_utilization = 0;
735 if (config.collect_cpu_utilization) {
736 cpu_utilization = collect_cpu_utilization();
740 // Form perf.data file name, perf error output file name
742 const std::string& destdir = config.destination_directory;
743 std::string data_file_path(destdir);
744 data_file_path += "/";
745 data_file_path += PERF_OUTPUT;
746 std::string perf_stderr_path(destdir);
747 perf_stderr_path += "/perferr.txt";
750 // Remove any existing perf.data file -- if we don't do this, perf
751 // will rename the old file and we'll have extra cruft lying around.
754 if (stat(data_file_path.c_str(), &statb) == 0) { // if file exists...
755 if (unlink(data_file_path.c_str())) { // then try to remove
756 W_ALOGW("unable to unlink previous perf.data file");
761 // The "mpdecision" daemon can cause problems for profile
762 // collection: if it decides to online a CPU partway through the
763 // 'perf record' run, the activity on that CPU will be invisible to
764 // perf, and if it offlines a CPU during the recording this can
765 // sometimes leave the PMU in an unusable state (dmesg errors of the
766 // form "perfevents: unable to request IRQXXX for ..."). To avoid
767 // these issues, if "mpdecision" is running the helper below will
768 // stop the service and then online all available CPUs. The object
769 // destructor (invoked when this routine terminates) will then
770 // restart the service again when needed.
772 uint32_t duration = config.sample_duration_in_s;
773 bool hardwire = config.hardwire_cpus;
774 uint32_t max_duration = config.hardwire_cpus_max_duration_in_s;
775 bool take_action = (hardwire && duration <= max_duration);
776 HardwireCpuHelper helper(take_action);
781 const char *stack_profile_opt =
782 (config.stack_profile ? "-g" : nullptr);
783 const std::string& perf_path = config.perf_path;
784 uint32_t period = config.sampling_period;
786 PROFILE_RESULT ret = invoke_perf(config,
793 if (ret != OK_PROFILE_COLLECTION) {
798 // Read the resulting perf.data file, encode into protocol buffer, then write
799 // the result to the file perf.data.encoded
801 std::string path = android::base::StringPrintf(
802 "%s.encoded.%d", data_file_path.c_str(), seq);
803 return encode_to_proto(data_file_path, path.c_str(), config, cpu_utilization);
807 // Assuming that we want to collect a profile every N seconds,
808 // randomly partition N into two sub-intervals.
810 static void determine_before_after(unsigned &sleep_before_collect,
811 unsigned &sleep_after_collect,
812 unsigned collection_interval)
814 double frac = erand48(random_seed);
815 sleep_before_collect = (unsigned) (((double)collection_interval) * frac);
816 assert(sleep_before_collect <= collection_interval);
817 sleep_after_collect = collection_interval - sleep_before_collect;
821 // Set random number generator seed
823 static void set_seed(uint32_t use_fixed_seed)
826 if (use_fixed_seed) {
828 // Use fixed user-specified seed
830 seed = use_fixed_seed;
837 W_ALOGI("random seed set to %u", seed);
838 // Distribute the 32-bit seed into the three 16-bit array
839 // elements. The specific values being written do not especially
840 // matter as long as we are setting them to something based on the seed.
841 random_seed[0] = seed & 0xffff;
842 random_seed[1] = (seed >> 16);
843 random_seed[2] = (random_seed[0] ^ random_seed[1]);
846 static void CommonInit(uint32_t use_fixed_seed, const char* dest_dir) {
847 // Children of init inherit an artificially low OOM score -- this is not
848 // desirable for perfprofd (its OOM score should be on par with
849 // other user processes).
850 std::stringstream oomscore_path;
851 oomscore_path << "/proc/" << getpid() << "/oom_score_adj";
852 if (!android::base::WriteStringToFile("0", oomscore_path.str())) {
853 W_ALOGE("unable to write to %s", oomscore_path.str().c_str());
856 set_seed(use_fixed_seed);
857 if (dest_dir != nullptr) {
858 cleanup_destination_dir(dest_dir);
861 running_in_emulator = android::base::GetBoolProperty("ro.kernel.qemu", false);
862 is_debug_build = android::base::GetBoolProperty("ro.debuggable", false);
868 static void init(const Config& config)
870 // TODO: Consider whether we want to clean things or just overwrite.
871 CommonInit(config.use_fixed_seed, nullptr);
874 static void init(ConfigReader &config)
876 if (!config.readFile()) {
877 W_ALOGE("unable to open configuration file %s",
878 config.getConfigFilePath());
881 CommonInit(static_cast<uint32_t>(config.getUnsignedValue("use_fixed_seed")),
882 config.getStringValue("destination_directory").c_str());
884 signal(SIGHUP, sig_hup);
887 template <typename ConfigFn, typename UpdateFn>
888 static void ProfilingLoopImpl(ConfigFn config, UpdateFn update) {
889 unsigned iterations = 0;
891 while(config()->main_loop_iterations == 0 ||
892 iterations < config()->main_loop_iterations) {
893 if (config()->ShouldStopProfiling()) {
897 // Figure out where in the collection interval we're going to actually
899 unsigned sleep_before_collect = 0;
900 unsigned sleep_after_collect = 0;
901 determine_before_after(sleep_before_collect, sleep_after_collect,
902 config()->collection_interval_in_s);
903 config()->Sleep(sleep_before_collect);
905 if (config()->ShouldStopProfiling()) {
909 // Run any necessary updates.
912 // Check for profiling enabled...
913 CKPROFILE_RESULT ckresult = check_profiling_enabled(*config());
914 if (ckresult != DO_COLLECT_PROFILE) {
915 W_ALOGI("profile collection skipped (%s)",
916 ckprofile_result_to_string(ckresult));
918 // Kick off the profiling run...
919 W_ALOGI("initiating profile collection");
920 PROFILE_RESULT result = collect_profile(*config(), seq);
921 if (result != OK_PROFILE_COLLECTION) {
922 W_ALOGI("profile collection failed (%s)",
923 profile_result_to_string(result));
925 if (post_process(*config(), seq)) {
928 W_ALOGI("profile collection complete");
932 if (config()->ShouldStopProfiling()) {
936 config()->Sleep(sleep_after_collect);
941 void ProfilingLoop(Config& config) {
944 auto config_fn = [&config]() {
947 auto do_nothing = []() {
949 ProfilingLoopImpl(config_fn, do_nothing);
954 // 1. parse cmd line args
955 // 2. read config file
958 // perform a profile collection
961 int perfprofd_main(int argc, char** argv, Config* config)
963 ConfigReader config_reader;
965 W_ALOGI("starting Android Wide Profiling daemon");
967 parse_args(argc, argv);
969 config_reader.FillConfig(config);
971 if (!perf_file_to_convert.empty()) {
972 std::string encoded_path = perf_file_to_convert + ".encoded";
973 encode_to_proto(perf_file_to_convert, encoded_path.c_str(), *config, 0);
977 // Early exit if we're not supposed to run on this build flavor
978 if (is_debug_build != 1 && config->only_debug_build) {
979 W_ALOGI("early exit due to inappropriate build type");
983 auto config_fn = [config]() {
986 auto reread_config = [&config_reader, config]() {
987 // Reread config file -- the uploader may have rewritten it as a result
988 // of a gservices change
989 config_reader.readFile();
990 config_reader.FillConfig(config);
992 ProfilingLoopImpl(config_fn, reread_config);
994 W_ALOGI("finishing Android Wide Profiling daemon");