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>
36 #include <android-base/file.h>
37 #include <android-base/logging.h>
38 #include <android-base/macros.h>
39 #include <android-base/stringprintf.h>
42 #include <android-base/properties.h>
45 #include "perfprofd_record.pb.h"
48 #include "cpuconfig.h"
49 #include "perf_data_converter.h"
50 #include "perfprofdcore.h"
51 #include "perfprofd_io.h"
52 #include "symbolizer.h"
55 // Perf profiling daemon -- collects system-wide profiles using
57 // simpleperf record -a
59 // and encodes them so that they can be uploaded by a separate service.
62 //......................................................................
64 using ProtoUniquePtr = std::unique_ptr<android::perfprofd::PerfprofdRecord>;
67 // Output file from 'perf record'.
69 #define PERF_OUTPUT "perf.data"
72 // This enum holds the results of the "should we profile" configuration check.
76 // All systems go for profile collection.
79 // The selected configuration directory doesn't exist.
80 DONT_PROFILE_MISSING_CONFIG_DIR,
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,
88 // No perf executable present
89 DONT_PROFILE_MISSING_PERF_EXECUTABLE,
91 // We're running in the emulator, perf won't be able to do much
92 DONT_PROFILE_RUNNING_IN_EMULATOR
96 static bool common_initialized = false;
99 // Are we running in the emulator? If so, stub out profile collection
100 // Starts as uninitialized (-1), then set to 1 or 0 at init time.
102 static int running_in_emulator = -1;
105 // Is this a debug build ('userdebug' or 'eng')?
107 static bool is_debug_build = false;
110 // Random number generator seed (set at startup time).
112 static unsigned short random_seed[3];
115 // Convert a CKPROFILE_RESULT to a string
117 static const char *ckprofile_result_to_string(CKPROFILE_RESULT result)
120 case DO_COLLECT_PROFILE:
121 return "DO_COLLECT_PROFILE";
122 case DONT_PROFILE_MISSING_CONFIG_DIR:
123 return "missing config directory";
124 case DONT_PROFILE_MISSING_SEMAPHORE:
125 return "missing semaphore file";
126 case DONT_PROFILE_MISSING_PERF_EXECUTABLE:
127 return "missing 'perf' executable";
128 case DONT_PROFILE_RUNNING_IN_EMULATOR:
129 return "running in emulator";
136 // Check to see whether we should perform a profile collection
138 static CKPROFILE_RESULT check_profiling_enabled(const Config& config)
141 // Profile collection in the emulator doesn't make sense
143 assert(running_in_emulator != -1);
144 if (running_in_emulator) {
145 return DONT_PROFILE_RUNNING_IN_EMULATOR;
148 if (!config.IsProfilingEnabled()) {
149 return DONT_PROFILE_MISSING_CONFIG_DIR;
152 // Check for existence of simpleperf/perf executable
153 std::string pp = config.perf_path;
154 if (access(pp.c_str(), R_OK|X_OK) == -1) {
155 LOG(WARNING) << "unable to access/execute " << pp;
156 return DONT_PROFILE_MISSING_PERF_EXECUTABLE;
162 return DO_COLLECT_PROFILE;
168 return android::base::GetBoolProperty("sys.boot_completed", false) != true;
175 // Constructor takes a timeout (in seconds) and a child pid; If an
176 // alarm set for the specified number of seconds triggers, then a
177 // SIGKILL is sent to the child. Destructor resets alarm. Example:
179 // pid_t child_pid = ...;
180 // { AlarmHelper h(10, child_pid);
181 // ... = read_from_child(child_pid, ...);
184 // NB: this helper is not re-entrant-- avoid nested use or
185 // use by multiple threads
189 AlarmHelper(unsigned num_seconds, pid_t child)
191 struct sigaction sigact;
194 memset(&sigact, 0, sizeof(sigact));
195 sigact.sa_sigaction = handler;
196 sigaction(SIGALRM, &sigact, &oldsigact_);
204 sigaction(SIGALRM, &oldsigact_, NULL);
206 static void handler(int, siginfo_t *, void *);
209 struct sigaction oldsigact_;
213 pid_t AlarmHelper::child_;
215 void AlarmHelper::handler(int, siginfo_t *, void *)
217 LOG(WARNING) << "SIGALRM timeout";
218 kill(child_, SIGKILL);
222 // This implementation invokes "dumpsys media.camera" and inspects the
223 // output to determine if any camera clients are active. NB: this is
224 // currently disable (via config option) until the selinux issues can
225 // be sorted out. Another possible implementation (not yet attempted)
226 // would be to use the binder to call into the native camera service
227 // via "ICameraService".
229 bool get_camera_active()
232 if (pipe2(pipefds, O_CLOEXEC) != 0) {
233 PLOG(ERROR) << "pipe2() failed";
238 PLOG(ERROR) << "fork() failed";
242 } else if (pid == 0) {
245 dup2(pipefds[1], fileno(stderr));
246 dup2(pipefds[1], fileno(stdout));
247 const char *argv[10];
249 argv[slot++] = "/system/bin/dumpsys";
250 argv[slot++] = "media.camera";
251 argv[slot++] = nullptr;
252 execvp(argv[0], (char * const *)argv);
253 PLOG(ERROR) << "execvp() failed";
257 AlarmHelper helper(10, pid);
261 bool have_cam = false;
262 bool have_clients = true;
263 std::string dump_output;
264 bool result = android::base::ReadFdToString(pipefds[0], &dump_output);
267 std::stringstream ss(dump_output);
269 while (std::getline(ss,line,'\n')) {
270 if (line.find("Camera module API version:") !=
274 if (line.find("No camera module available") !=
276 line.find("No active camera clients yet") !=
278 have_clients = false;
283 // reap child (no zombies please)
285 TEMP_FAILURE_RETRY(waitpid(pid, &st, 0));
286 return have_cam && have_clients;
291 std::string psdir("/sys/class/power_supply");
292 DIR* dir = opendir(psdir.c_str());
294 PLOG(ERROR) << "Failed to open dir " << psdir;
299 while ((e = readdir(dir)) != 0) {
300 if (e->d_name[0] != '.') {
301 std::string online_path = psdir + "/" + e->d_name + "/online";
302 std::string contents;
304 if (android::base::ReadFileToString(online_path.c_str(), &contents) &&
305 sscanf(contents.c_str(), "%d", &value) == 1) {
317 static bool postprocess_proc_stat_contents(const std::string &pscontents,
318 long unsigned *idleticks,
319 long unsigned *remainingticks)
321 long unsigned usertime, nicetime, systime, idletime, iowaittime;
322 long unsigned irqtime, softirqtime;
324 int rc = sscanf(pscontents.c_str(), "cpu %lu %lu %lu %lu %lu %lu %lu",
325 &usertime, &nicetime, &systime, &idletime,
326 &iowaittime, &irqtime, &softirqtime);
330 *idleticks = idletime;
331 *remainingticks = usertime + nicetime + systime + iowaittime + irqtime + softirqtime;
335 unsigned collect_cpu_utilization()
337 std::string contents;
338 long unsigned idle[2];
339 long unsigned busy[2];
340 for (unsigned iter = 0; iter < 2; ++iter) {
341 if (!android::base::ReadFileToString("/proc/stat", &contents)) {
344 if (!postprocess_proc_stat_contents(contents, &idle[iter], &busy[iter])) {
351 long unsigned total_delta = (idle[1] + busy[1]) - (idle[0] + busy[0]);
352 long unsigned busy_delta = busy[1] - busy[0];
353 return busy_delta * 100 / total_delta;
356 static void annotate_encoded_perf_profile(android::perfprofd::PerfprofdRecord* profile,
357 const Config& config,
358 unsigned cpu_utilization)
361 // Incorporate cpu utilization (collected prior to perf run)
363 if (config.collect_cpu_utilization) {
364 profile->set_cpu_utilization(cpu_utilization);
368 // Load average as reported by the kernel
372 if (android::base::ReadFileToString("/proc/loadavg", &load) &&
373 sscanf(load.c_str(), "%lf", &fload) == 1) {
374 int iload = static_cast<int>(fload * 100.0);
375 profile->set_sys_load_average(iload);
377 PLOG(ERROR) << "Failed to read or scan /proc/loadavg";
381 // Device still booting? Camera in use? Plugged into charger?
383 bool is_booting = get_booting();
384 if (config.collect_booting) {
385 profile->set_booting(is_booting);
387 if (config.collect_camera_active) {
388 profile->set_camera_active(is_booting ? false : get_camera_active());
390 if (config.collect_charging_state) {
391 profile->set_on_charger(get_charging());
395 // Examine the contents of wake_unlock to determine whether the
396 // device display is on or off. NB: is this really the only way to
397 // determine this info?
400 if (android::base::ReadFileToString("/sys/power/wake_unlock", &disp)) {
401 bool ison = (strstr(disp.c_str(), "PowerManagerService.Display") == 0);
402 profile->set_display_on(ison);
404 PLOG(ERROR) << "Failed to read /sys/power/wake_unlock";
408 static ProtoUniquePtr encode_to_proto(const std::string &data_file_path,
409 const Config& config,
410 unsigned cpu_utilization,
411 perfprofd::Symbolizer* symbolizer) {
413 // Open and read perf.data file
415 ProtoUniquePtr encodedProfile(
416 android::perfprofd::RawPerfDataToAndroidPerfProfile(data_file_path, symbolizer));
417 if (encodedProfile == nullptr) {
421 // All of the info in 'encodedProfile' is derived from the perf.data file;
422 // here we tack display status, cpu utilization, system load, etc.
423 annotate_encoded_perf_profile(encodedProfile.get(), config, cpu_utilization);
425 return encodedProfile;
428 PROFILE_RESULT encode_to_proto(const std::string &data_file_path,
429 const char *encoded_file_path,
430 const Config& config,
431 unsigned cpu_utilization,
432 perfprofd::Symbolizer* symbolizer)
434 ProtoUniquePtr encodedProfile = encode_to_proto(data_file_path,
440 // Issue error if no samples
442 if (encodedProfile == nullptr || encodedProfile->perf_data().events_size() == 0) {
443 return ERR_PERF_ENCODE_FAILED;
446 return android::perfprofd::SerializeProtobuf(encodedProfile.get(),
449 ? OK_PROFILE_COLLECTION
450 : ERR_WRITE_ENCODED_FILE_FAILED;
454 // Invoke "perf record". Return value is OK_PROFILE_COLLECTION for
455 // success, or some other error code if something went wrong.
457 static PROFILE_RESULT invoke_perf(Config& config,
458 const std::string &perf_path,
459 unsigned sampling_period,
460 const char *stack_profile_opt,
462 const std::string &data_file_path,
463 const std::string &perf_stderr_path)
468 return ERR_FORK_FAILED;
474 // Open file to receive stderr/stdout from perf
475 FILE *efp = fopen(perf_stderr_path.c_str(), "w");
477 dup2(fileno(efp), STDERR_FILENO);
478 dup2(fileno(efp), STDOUT_FILENO);
480 PLOG(WARNING) << "unable to open " << perf_stderr_path << " for writing";
483 // marshall arguments
484 constexpr unsigned max_args = 14;
485 const char *argv[max_args];
487 argv[slot++] = perf_path.c_str();
488 argv[slot++] = "record";
492 argv[slot++] = data_file_path.c_str();
496 std::string p_str = android::base::StringPrintf("%u", sampling_period);
497 argv[slot++] = p_str.c_str();
500 if (stack_profile_opt)
501 argv[slot++] = stack_profile_opt;
504 if (config.process < 0) {
505 // system wide profiling
509 pid_str = std::to_string(config.process);
510 argv[slot++] = pid_str.c_str();
513 // no need for kernel symbols
514 argv[slot++] = "--no-dump-kernel-symbols";
517 argv[slot++] = "/system/bin/sleep";
518 std::string d_str = android::base::StringPrintf("%u", duration);
519 argv[slot++] = d_str.c_str();
522 argv[slot++] = nullptr;
523 assert(slot < max_args);
525 // record the final command line in the error output file for
526 // posterity/debugging purposes
527 fprintf(stderr, "perf invocation (pid=%d):\n", getpid());
528 for (unsigned i = 0; argv[i] != nullptr; ++i) {
529 fprintf(stderr, "%s%s", i ? " " : "", argv[i]);
531 fprintf(stderr, "\n");
534 execvp(argv[0], (char * const *)argv);
535 fprintf(stderr, "exec failed: %s\n", strerror(errno));
542 config.Sleep(duration);
544 // We may have been woken up to stop profiling.
545 if (config.ShouldStopProfiling()) {
546 // Send SIGHUP to simpleperf to make it stop.
550 // Wait for the child, so it's reaped correctly.
552 pid_t reaped = TEMP_FAILURE_RETRY(waitpid(pid, &st, 0));
555 PLOG(WARNING) << "waitpid failed";
556 } else if (WIFSIGNALED(st)) {
557 if (WTERMSIG(st) == SIGHUP && config.ShouldStopProfiling()) {
559 return OK_PROFILE_COLLECTION;
561 LOG(WARNING) << "perf killed by signal " << WTERMSIG(st);
562 } else if (WEXITSTATUS(st) != 0) {
563 LOG(WARNING) << "perf bad exit status " << WEXITSTATUS(st);
565 return OK_PROFILE_COLLECTION;
569 return ERR_PERF_RECORD_FAILED;
573 // Remove all files in the destination directory during initialization
575 static void cleanup_destination_dir(const std::string& dest_dir)
577 DIR* dir = opendir(dest_dir.c_str());
580 while ((e = readdir(dir)) != 0) {
581 if (e->d_name[0] != '.') {
582 std::string file_path = dest_dir + "/" + e->d_name;
583 remove(file_path.c_str());
588 PLOG(WARNING) << "unable to open destination dir " << dest_dir << " for cleanup";
593 // Collect a perf profile. Steps for this operation are:
594 // - kick off 'perf record'
595 // - read perf.data, convert to protocol buf
597 static ProtoUniquePtr collect_profile(Config& config)
600 // Collect cpu utilization if enabled
602 unsigned cpu_utilization = 0;
603 if (config.collect_cpu_utilization) {
604 cpu_utilization = collect_cpu_utilization();
608 // Form perf.data file name, perf error output file name
610 const std::string& destdir = config.destination_directory;
611 std::string data_file_path(destdir);
612 data_file_path += "/";
613 data_file_path += PERF_OUTPUT;
614 std::string perf_stderr_path(destdir);
615 perf_stderr_path += "/perferr.txt";
618 // Remove any existing perf.data file -- if we don't do this, perf
619 // will rename the old file and we'll have extra cruft lying around.
622 if (stat(data_file_path.c_str(), &statb) == 0) { // if file exists...
623 if (unlink(data_file_path.c_str())) { // then try to remove
624 PLOG(WARNING) << "unable to unlink previous perf.data file";
629 // The "mpdecision" daemon can cause problems for profile
630 // collection: if it decides to online a CPU partway through the
631 // 'perf record' run, the activity on that CPU will be invisible to
632 // perf, and if it offlines a CPU during the recording this can
633 // sometimes leave the PMU in an unusable state (dmesg errors of the
634 // form "perfevents: unable to request IRQXXX for ..."). To avoid
635 // these issues, if "mpdecision" is running the helper below will
636 // stop the service and then online all available CPUs. The object
637 // destructor (invoked when this routine terminates) will then
638 // restart the service again when needed.
640 uint32_t duration = config.sample_duration_in_s;
641 bool hardwire = config.hardwire_cpus;
642 uint32_t max_duration = config.hardwire_cpus_max_duration_in_s;
643 bool take_action = (hardwire && duration <= max_duration);
644 HardwireCpuHelper helper(take_action);
649 const char *stack_profile_opt =
650 (config.stack_profile ? "-g" : nullptr);
651 const std::string& perf_path = config.perf_path;
652 uint32_t period = config.sampling_period;
654 PROFILE_RESULT ret = invoke_perf(config,
661 if (ret != OK_PROFILE_COLLECTION) {
666 // Read the resulting perf.data file, encode into protocol buffer, then write
667 // the result to the file perf.data.encoded
669 std::unique_ptr<perfprofd::Symbolizer> symbolizer;
670 if (config.use_elf_symbolizer) {
671 symbolizer = perfprofd::CreateELFSymbolizer();
673 return encode_to_proto(data_file_path, config, cpu_utilization, symbolizer.get());
677 // Assuming that we want to collect a profile every N seconds,
678 // randomly partition N into two sub-intervals.
680 static void determine_before_after(unsigned &sleep_before_collect,
681 unsigned &sleep_after_collect,
682 unsigned collection_interval)
684 double frac = erand48(random_seed);
685 sleep_before_collect = (unsigned) (((double)collection_interval) * frac);
686 assert(sleep_before_collect <= collection_interval);
687 sleep_after_collect = collection_interval - sleep_before_collect;
691 // Set random number generator seed
693 static void set_seed(uint32_t use_fixed_seed)
696 if (use_fixed_seed) {
698 // Use fixed user-specified seed
700 seed = use_fixed_seed;
711 LOG(INFO) << "random seed set to " << seed;
712 // Distribute the 32-bit seed into the three 16-bit array
713 // elements. The specific values being written do not especially
714 // matter as long as we are setting them to something based on the seed.
715 random_seed[0] = seed & 0xffff;
716 random_seed[1] = (seed >> 16);
717 random_seed[2] = (random_seed[0] ^ random_seed[1]);
720 void CommonInit(uint32_t use_fixed_seed, const char* dest_dir) {
721 // Children of init inherit an artificially low OOM score -- this is not
722 // desirable for perfprofd (its OOM score should be on par with
723 // other user processes).
724 std::stringstream oomscore_path;
725 oomscore_path << "/proc/" << getpid() << "/oom_score_adj";
726 if (!android::base::WriteStringToFile("0", oomscore_path.str())) {
727 LOG(ERROR) << "unable to write to " << oomscore_path.str();
730 set_seed(use_fixed_seed);
731 if (dest_dir != nullptr) {
732 cleanup_destination_dir(dest_dir);
736 running_in_emulator = android::base::GetBoolProperty("ro.kernel.qemu", false);
737 is_debug_build = android::base::GetBoolProperty("ro.debuggable", false);
739 running_in_emulator = false;
740 is_debug_build = true;
743 common_initialized = true;
746 bool IsDebugBuild() {
747 CHECK(common_initialized);
748 return is_debug_build;
751 template <typename ConfigFn, typename UpdateFn>
752 static void ProfilingLoopImpl(ConfigFn config, UpdateFn update, HandlerFn handler) {
753 unsigned iterations = 0;
754 while(config()->main_loop_iterations == 0 ||
755 iterations < config()->main_loop_iterations) {
756 if (config()->ShouldStopProfiling()) {
760 // Figure out where in the collection interval we're going to actually
762 unsigned sleep_before_collect = 0;
763 unsigned sleep_after_collect = 0;
764 determine_before_after(sleep_before_collect, sleep_after_collect,
765 config()->collection_interval_in_s);
766 config()->Sleep(sleep_before_collect);
768 if (config()->ShouldStopProfiling()) {
772 // Run any necessary updates.
775 // Check for profiling enabled...
776 CKPROFILE_RESULT ckresult = check_profiling_enabled(*config());
777 if (ckresult != DO_COLLECT_PROFILE) {
778 LOG(INFO) << "profile collection skipped (" << ckprofile_result_to_string(ckresult) << ")";
780 // Kick off the profiling run...
781 LOG(INFO) << "initiating profile collection";
782 ProtoUniquePtr proto = collect_profile(*config());
783 if (proto == nullptr) {
784 LOG(WARNING) << "profile collection failed";
787 // Always report, even a null result.
788 bool handle_result = handler(proto.get(), config());
790 LOG(INFO) << "profile collection complete";
791 } else if (proto != nullptr) {
792 LOG(WARNING) << "profile handling failed";
796 if (config()->ShouldStopProfiling()) {
800 config()->Sleep(sleep_after_collect);
805 void ProfilingLoop(Config& config, HandlerFn handler) {
806 CommonInit(config.use_fixed_seed, nullptr);
808 auto config_fn = [&config]() {
811 auto do_nothing = []() {
813 ProfilingLoopImpl(config_fn, do_nothing, handler);
816 void ProfilingLoop(std::function<Config*()> config_fn,
817 std::function<void()> update_fn,
819 ProfilingLoopImpl(config_fn, update_fn, handler);