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 <base/stringprintf.h>
38 #include <cutils/properties.h>
40 #include "perfprofdcore.h"
41 #include "perfprofdutils.h"
42 #include "perf_data_converter.h"
43 #include "cpuconfig.h"
46 // Perf profiling daemon -- collects system-wide profiles using
48 // simpleperf record -a
50 // and encodes them so that they can be uploaded by a separate service.
53 //......................................................................
56 // Output file from 'perf record'.
58 #define PERF_OUTPUT "perf.data"
61 // This enum holds the results of the "should we profile" configuration check.
65 // All systems go for profile collection.
68 // The selected configuration directory doesn't exist.
69 DONT_PROFILE_MISSING_CONFIG_DIR,
71 // Destination directory does not contain the semaphore file that
72 // the perf profile uploading service creates when it determines
73 // that the user has opted "in" for usage data collection. No
74 // semaphore -> no user approval -> no profiling.
75 DONT_PROFILE_MISSING_SEMAPHORE,
77 // No perf executable present
78 DONT_PROFILE_MISSING_PERF_EXECUTABLE,
80 // We're running in the emulator, perf won't be able to do much
81 DONT_PROFILE_RUNNING_IN_EMULATOR
86 // Are we running in the emulator? If so, stub out profile collection
87 // Starts as uninitialized (-1), then set to 1 or 0 at init time.
89 static int running_in_emulator = -1;
92 // Is this a debug build ('userdebug' or 'eng')?
93 // Starts as uninitialized (-1), then set to 1 or 0 at init time.
95 static int is_debug_build = -1;
98 // Random number generator seed (set at startup time).
100 static unsigned short random_seed[3];
103 // Config file path. May be overridden with -c command line option
105 static const char *config_file_path =
106 "/data/data/com.google.android.gms/files/perfprofd.conf";
109 // This table describes the config file syntax in terms of key/value pairs.
110 // Values come in two flavors: strings, or unsigned integers. In the latter
111 // case the reader sets allowable minimum/maximum for the setting.
119 // Ask for the current setting of a config item
120 unsigned getUnsignedValue(const char *key) const;
121 std::string getStringValue(const char *key) const;
123 // read the specified config file, applying any settings it contains
124 void readFile(bool initial);
127 void addUnsignedEntry(const char *key,
128 unsigned default_value,
131 void addStringEntry(const char *key, const char *default_value);
132 void addDefaultEntries();
133 void parseLine(const char *key, const char *value, unsigned linecount);
135 typedef struct { unsigned minv, maxv; } values;
136 std::map<std::string, values> u_info;
137 std::map<std::string, unsigned> u_entries;
138 std::map<std::string, std::string> s_entries;
139 bool trace_config_read;
142 ConfigReader::ConfigReader()
143 : trace_config_read(false)
148 ConfigReader::~ConfigReader()
153 // Populate the reader with the set of allowable entries
155 void ConfigReader::addDefaultEntries()
157 // Average number of seconds between perf profile collections (if
158 // set to 100, then over time we want to see a perf profile
159 // collected every 100 seconds). The actual time within the interval
160 // for the collection is chosen randomly.
161 addUnsignedEntry("collection_interval", 14400, 100, UINT32_MAX);
163 // Use the specified fixed seed for random number generation (unit
165 addUnsignedEntry("use_fixed_seed", 0, 0, UINT32_MAX);
167 // For testing purposes, number of times to iterate through main
168 // loop. Value of zero indicates that we should loop forever.
169 addUnsignedEntry("main_loop_iterations", 0, 0, UINT32_MAX);
171 // Destination directory (where to write profiles). This location
172 // chosen since it is accessible to the uploader service.
173 addStringEntry("destination_directory", "/data/misc/perfprofd");
175 // Config directory (where to read configs).
176 addStringEntry("config_directory", "/data/data/com.google.android.gms/files");
178 // Full path to 'perf' executable.
179 addStringEntry("perf_path", "/system/xbin/simpleperf");
181 // Desired sampling period (passed to perf -c option). Small
182 // sampling periods can perturb the collected profiles, so enforce
184 addUnsignedEntry("sampling_period", 500000, 5000, UINT32_MAX);
186 // Length of time to collect samples (number of seconds for 'perf
188 addUnsignedEntry("sample_duration", 3, 2, 600);
190 // If this parameter is non-zero it will cause perfprofd to
191 // exit immediately if the build type is not userdebug or eng.
192 // Currently defaults to 1 (true).
193 addUnsignedEntry("only_debug_build", 1, 0, 1);
195 // If the "mpdecision" service is running at the point we are ready
196 // to kick off a profiling run, then temporarily disable the service
197 // and hard-wire all cores on prior to the collection run, provided
198 // that the duration of the recording is less than or equal to the value of
199 // 'hardwire_cpus_max_duration'.
200 addUnsignedEntry("hardwire_cpus", 1, 0, 1);
201 addUnsignedEntry("hardwire_cpus_max_duration", 5, 1, UINT32_MAX);
203 // Maximum number of unprocessed profiles we can accumulate in the
204 // destination directory. Once we reach this limit, we continue
205 // to collect, but we just overwrite the most recent profile.
206 addUnsignedEntry("max_unprocessed_profiles", 10, 1, UINT32_MAX);
208 // If set to 1, pass the -g option when invoking 'perf' (requests
209 // stack traces as opposed to flat profile).
210 addUnsignedEntry("stack_profile", 0, 0, 1);
212 // For unit testing only: if set to 1, emit info messages on config
214 addUnsignedEntry("trace_config_read", 0, 0, 1);
217 void ConfigReader::addUnsignedEntry(const char *key,
218 unsigned default_value,
223 if (u_entries.find(ks) != u_entries.end() ||
224 s_entries.find(ks) != s_entries.end()) {
225 W_ALOGE("internal error -- duplicate entry for key %s", key);
229 vals.minv = min_value;
230 vals.maxv = max_value;
232 u_entries[ks] = default_value;
235 void ConfigReader::addStringEntry(const char *key, const char *default_value)
238 if (u_entries.find(ks) != u_entries.end() ||
239 s_entries.find(ks) != s_entries.end()) {
240 W_ALOGE("internal error -- duplicate entry for key %s", key);
243 if (default_value == nullptr) {
244 W_ALOGE("internal error -- bad default value for key %s", key);
247 s_entries[ks] = std::string(default_value);
250 unsigned ConfigReader::getUnsignedValue(const char *key) const
253 auto it = u_entries.find(ks);
254 assert(it != u_entries.end());
258 std::string ConfigReader::getStringValue(const char *key) const
261 auto it = s_entries.find(ks);
262 assert(it != s_entries.end());
267 // Parse a key=value pair read from the config file. This will issue
268 // warnings or errors to the system logs if the line can't be
269 // interpreted properly.
271 void ConfigReader::parseLine(const char *key,
278 auto uit = u_entries.find(key);
279 if (uit != u_entries.end()) {
281 if (isdigit(value[0]) == 0 || sscanf(value, "%u", &uvalue) != 1) {
282 W_ALOGW("line %d: malformed unsigned value (ignored)", linecount);
285 auto iit = u_info.find(key);
286 assert(iit != u_info.end());
288 if (uvalue < vals.minv || uvalue > vals.maxv) {
289 W_ALOGW("line %d: specified value %u for '%s' "
290 "outside permitted range [%u %u] (ignored)",
291 linecount, uvalue, key, vals.minv, vals.maxv);
293 if (trace_config_read) {
294 W_ALOGI("option %s set to %u", key, uvalue);
296 uit->second = uvalue;
299 trace_config_read = (getUnsignedValue("trace_config_read") != 0);
303 auto sit = s_entries.find(key);
304 if (sit != s_entries.end()) {
305 if (trace_config_read) {
306 W_ALOGI("option %s set to %s", key, value);
308 sit->second = std::string(value);
312 W_ALOGW("line %d: unknown option '%s' ignored", linecount, key);
315 static bool isblank(const std::string &line)
317 for (std::string::const_iterator it = line.begin(); it != line.end(); ++it)
319 if (isspace(*it) == 0) {
326 void ConfigReader::readFile(bool initial)
328 FILE *fp = fopen(config_file_path, "r");
331 W_ALOGE("unable to open configuration file %s", config_file_path);
336 char *linebuf = NULL;
337 size_t line_length = 0;
338 for (unsigned linecount = 1;
339 getline(&linebuf, &line_length, fp) != -1;
345 if (linebuf[0] == '#') {
350 if (isblank(linebuf)) {
354 // look for X=Y assignment
355 eq = strchr(linebuf, '=');
357 W_ALOGW("line %d: line malformed (no '=' found)", linecount);
364 char *ln = strrchr(value, '\n');
365 if (ln) { *ln = '\0'; }
367 parseLine(key, value, linecount);
374 // Parse command line args. Currently you can supply "-c P" to set
375 // the path of the config file to P.
377 static void parse_args(int argc, char** argv)
381 for (ac = 1; ac < argc; ++ac) {
382 if (!strcmp(argv[ac], "-c")) {
384 W_ALOGE("malformed command line: -c option requires argument)");
387 config_file_path = strdup(argv[ac+1]);
388 W_ALOGI("config file path set to %s", config_file_path);
391 W_ALOGE("malformed command line: unknown option or arg %s)", argv[ac]);
398 // Convert a CKPROFILE_RESULT to a string
400 const char *ckprofile_result_to_string(CKPROFILE_RESULT result)
403 case DO_COLLECT_PROFILE:
404 return "DO_COLLECT_PROFILE";
405 case DONT_PROFILE_MISSING_CONFIG_DIR:
406 return "missing config directory";
407 case DONT_PROFILE_MISSING_SEMAPHORE:
408 return "missing semaphore file";
409 case DONT_PROFILE_MISSING_PERF_EXECUTABLE:
410 return "missing 'perf' executable";
411 case DONT_PROFILE_RUNNING_IN_EMULATOR:
412 return "running in emulator";
413 default: return "unknown";
419 // Convert a PROFILE_RESULT to a string
421 const char *profile_result_to_string(PROFILE_RESULT result)
424 case OK_PROFILE_COLLECTION:
425 return "profile collection succeeded";
426 case ERR_FORK_FAILED:
427 return "fork() system call failed";
428 case ERR_PERF_RECORD_FAILED:
429 return "perf record returned bad exit status";
430 case ERR_PERF_ENCODE_FAILED:
431 return "failure encoding perf.data to protobuf";
432 case ERR_OPEN_ENCODED_FILE_FAILED:
433 return "failed to open encoded perf file";
434 case ERR_WRITE_ENCODED_FILE_FAILED:
435 return "write to encoded perf file failed";
436 default: return "unknown";
442 // Check to see whether we should perform a profile collection
444 static CKPROFILE_RESULT check_profiling_enabled(ConfigReader &config)
447 // Profile collection in the emulator doesn't make sense
449 assert(running_in_emulator != -1);
450 if (running_in_emulator) {
451 return DONT_PROFILE_RUNNING_IN_EMULATOR;
455 // Check for existence of semaphore file in config directory
457 if (access(config.getStringValue("config_directory").c_str(), F_OK) == -1) {
458 W_ALOGW("unable to open config directory %s: (%s)",
459 config.getStringValue("config_directory").c_str(), strerror(errno));
460 return DONT_PROFILE_MISSING_CONFIG_DIR;
464 // Check for existence of semaphore file
465 std::string semaphore_filepath = config.getStringValue("config_directory")
466 + "/" + SEMAPHORE_FILENAME;
467 if (access(semaphore_filepath.c_str(), F_OK) == -1) {
468 return DONT_PROFILE_MISSING_SEMAPHORE;
471 // Check for existence of simpleperf/perf executable
472 std::string pp = config.getStringValue("perf_path");
473 if (access(pp.c_str(), R_OK|X_OK) == -1) {
474 W_ALOGW("unable to access/execute %s", pp.c_str());
475 return DONT_PROFILE_MISSING_PERF_EXECUTABLE;
481 return DO_COLLECT_PROFILE;
484 inline char* string_as_array(std::string* str) {
485 return str->empty() ? NULL : &*str->begin();
488 PROFILE_RESULT encode_to_proto(const std::string &data_file_path,
489 const char *encoded_file_path)
492 // Open and read perf.data file
494 const wireless_android_play_playlog::AndroidPerfProfile &encodedProfile =
495 wireless_android_logging_awp::RawPerfDataToAndroidPerfProfile(data_file_path);
498 // Issue error if no samples
500 if (encodedProfile.programs().size() == 0) {
501 return ERR_PERF_ENCODE_FAILED;
505 // Serialize protobuf to array
507 int size = encodedProfile.ByteSize();
510 ::google::protobuf::uint8* dtarget =
511 reinterpret_cast<::google::protobuf::uint8*>(string_as_array(&data));
512 encodedProfile.SerializeWithCachedSizesToArray(dtarget);
515 // Open file and write encoded data to it
517 FILE *fp = fopen(encoded_file_path, "w");
519 return ERR_OPEN_ENCODED_FILE_FAILED;
522 if (fwrite(dtarget, fsiz, 1, fp) != 1) {
524 return ERR_WRITE_ENCODED_FILE_FAILED;
527 chmod(encoded_file_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
529 return OK_PROFILE_COLLECTION;
533 // Invoke "perf record". Return value is OK_PROFILE_COLLECTION for
534 // success, or some other error code if something went wrong.
536 static PROFILE_RESULT invoke_perf(const std::string &perf_path,
537 unsigned sampling_period,
538 const char *stack_profile_opt,
540 const std::string &data_file_path,
541 const std::string &perf_stderr_path)
546 return ERR_FORK_FAILED;
552 // Open file to receive stderr/stdout from perf
553 FILE *efp = fopen(perf_stderr_path.c_str(), "w");
555 dup2(fileno(efp), STDERR_FILENO);
556 dup2(fileno(efp), STDOUT_FILENO);
558 W_ALOGW("unable to open %s for writing", perf_stderr_path.c_str());
561 // marshall arguments
562 constexpr unsigned max_args = 12;
563 const char *argv[max_args];
565 argv[slot++] = perf_path.c_str();
566 argv[slot++] = "record";
570 argv[slot++] = data_file_path.c_str();
574 std::string p_str = android::base::StringPrintf("%u", sampling_period);
575 argv[slot++] = p_str.c_str();
578 if (stack_profile_opt)
579 argv[slot++] = stack_profile_opt;
581 // system wide profiling
585 argv[slot++] = "/system/bin/sleep";
586 std::string d_str = android::base::StringPrintf("%u", duration);
587 argv[slot++] = d_str.c_str();
590 argv[slot++] = nullptr;
591 assert(slot < max_args);
593 // record the final command line in the error output file for
594 // posterity/debugging purposes
595 fprintf(stderr, "perf invocation (pid=%d):\n", getpid());
596 for (unsigned i = 0; argv[i] != nullptr; ++i) {
597 fprintf(stderr, "%s%s", i ? " " : "", argv[i]);
599 fprintf(stderr, "\n");
602 execvp(argv[0], (char * const *)argv);
603 fprintf(stderr, "exec failed: %s\n", strerror(errno));
609 pid_t reaped = TEMP_FAILURE_RETRY(waitpid(pid, &st, 0));
612 W_ALOGW("waitpid failed: %s", strerror(errno));
613 } else if (WIFSIGNALED(st)) {
614 W_ALOGW("perf killed by signal %d", WTERMSIG(st));
615 } else if (WEXITSTATUS(st) != 0) {
616 W_ALOGW("perf bad exit status %d", WEXITSTATUS(st));
618 return OK_PROFILE_COLLECTION;
622 return ERR_PERF_RECORD_FAILED;
626 // Remove all files in the destination directory during initialization
628 static void cleanup_destination_dir(const ConfigReader &config)
630 std::string dest_dir = config.getStringValue("destination_directory");
631 DIR* dir = opendir(dest_dir.c_str());
634 while ((e = readdir(dir)) != 0) {
635 if (e->d_name[0] != '.') {
636 std::string file_path = dest_dir + "/" + e->d_name;
637 remove(file_path.c_str());
642 W_ALOGW("unable to open destination dir %s for cleanup",
648 // Post-processes after profile is collected and converted to protobuf.
649 // * GMS core stores processed file sequence numbers in
650 // /data/data/com.google.android.gms/files/perfprofd_processed.txt
651 // * Update /data/misc/perfprofd/perfprofd_produced.txt to remove the sequence
652 // numbers that have been processed and append the current seq number
653 // Returns true if the current_seq should increment.
655 static bool post_process(const ConfigReader &config, int current_seq)
657 std::string dest_dir = config.getStringValue("destination_directory");
658 std::string processed_file_path =
659 config.getStringValue("config_directory") + "/" + PROCESSED_FILENAME;
660 std::string produced_file_path = dest_dir + "/" + PRODUCED_FILENAME;
663 std::set<int> processed;
664 FILE *fp = fopen(processed_file_path.c_str(), "r");
667 while(fscanf(fp, "%d\n", &seq) > 0) {
668 if (remove(android::base::StringPrintf(
669 "%s/perf.data.encoded.%d", dest_dir.c_str(),seq).c_str()) == 0) {
670 processed.insert(seq);
676 std::set<int> produced;
677 fp = fopen(produced_file_path.c_str(), "r");
680 while(fscanf(fp, "%d\n", &seq) > 0) {
681 if (processed.find(seq) == processed.end()) {
682 produced.insert(seq);
688 unsigned maxLive = config.getUnsignedValue("max_unprocessed_profiles");
689 if (produced.size() >= maxLive) {
693 produced.insert(current_seq);
694 fp = fopen(produced_file_path.c_str(), "w");
696 W_ALOGW("Cannot write %s", produced_file_path.c_str());
699 for (std::set<int>::const_iterator iter = produced.begin();
700 iter != produced.end(); ++iter) {
701 fprintf(fp, "%d\n", *iter);
704 chmod(produced_file_path.c_str(),
705 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
710 // Collect a perf profile. Steps for this operation are:
711 // - kick off 'perf record'
712 // - read perf.data, convert to protocol buf
714 static PROFILE_RESULT collect_profile(const ConfigReader &config, int seq)
717 // Form perf.data file name, perf error output file name
719 std::string destdir = config.getStringValue("destination_directory");
720 std::string data_file_path(destdir);
721 data_file_path += "/";
722 data_file_path += PERF_OUTPUT;
723 std::string perf_stderr_path(destdir);
724 perf_stderr_path += "/perferr.txt";
727 // Remove any existing perf.data file -- if we don't do this, perf
728 // will rename the old file and we'll have extra cruft lying around.
731 if (stat(data_file_path.c_str(), &statb) == 0) { // if file exists...
732 if (unlink(data_file_path.c_str())) { // then try to remove
733 W_ALOGW("unable to unlink previous perf.data file");
738 // The "mpdecision" daemon can cause problems for profile
739 // collection: if it decides to online a CPU partway through the
740 // 'perf record' run, the activity on that CPU will be invisible to
741 // perf, and if it offlines a CPU during the recording this can
742 // sometimes leave the PMU in an unusable state (dmesg errors of the
743 // form "perfevents: unable to request IRQXXX for ..."). To avoid
744 // these issues, if "mpdecision" is running the helper below will
745 // stop the service and then online all available CPUs. The object
746 // destructor (invoked when this routine terminates) will then
747 // restart the service again when needed.
749 unsigned duration = config.getUnsignedValue("sample_duration");
750 unsigned hardwire = config.getUnsignedValue("hardwire_cpus");
751 unsigned max_duration = config.getUnsignedValue("hardwire_cpus_max_duration");
752 bool take_action = (hardwire && duration <= max_duration);
753 HardwireCpuHelper helper(take_action);
758 const char *stack_profile_opt =
759 (config.getUnsignedValue("stack_profile") != 0 ? "-g" : nullptr);
760 std::string perf_path = config.getStringValue("perf_path");
761 unsigned period = config.getUnsignedValue("sampling_period");
763 PROFILE_RESULT ret = invoke_perf(perf_path.c_str(),
769 if (ret != OK_PROFILE_COLLECTION) {
774 // Read the resulting perf.data file, encode into protocol buffer, then write
775 // the result to the file perf.data.encoded
777 std::string path = android::base::StringPrintf(
778 "%s.encoded.%d", data_file_path.c_str(), seq);
779 return encode_to_proto(data_file_path, path.c_str());
783 // SIGHUP handler. Sending SIGHUP to the daemon can be used to break it
784 // out of a sleep() call so as to trigger a new collection (debugging)
786 static void sig_hup(int /* signum */)
791 // Assuming that we want to collect a profile every N seconds,
792 // randomly partition N into two sub-intervals.
794 static void determine_before_after(unsigned &sleep_before_collect,
795 unsigned &sleep_after_collect,
796 unsigned collection_interval)
798 double frac = erand48(random_seed);
799 sleep_before_collect = (unsigned) (((double)collection_interval) * frac);
800 assert(sleep_before_collect <= collection_interval);
801 sleep_after_collect = collection_interval - sleep_before_collect;
805 // Set random number generator seed
807 static void set_seed(ConfigReader &config)
810 unsigned use_fixed_seed = config.getUnsignedValue("use_fixed_seed");
811 if (use_fixed_seed) {
813 // Use fixed user-specified seed
815 seed = use_fixed_seed;
822 W_ALOGI("random seed set to %u", seed);
823 // Distribute the 32-bit seed into the three 16-bit array
824 // elements. The specific values being written do not especially
825 // matter as long as we are setting them to something based on the seed.
826 random_seed[0] = seed & 0xffff;
827 random_seed[1] = (seed >> 16);
828 random_seed[2] = (random_seed[0] ^ random_seed[1]);
834 static void init(ConfigReader &config)
836 config.readFile(true);
838 cleanup_destination_dir(config);
840 char propBuf[PROPERTY_VALUE_MAX];
842 property_get("ro.kernel.qemu", propBuf, "");
843 running_in_emulator = (propBuf[0] == '1');
844 property_get("ro.debuggable", propBuf, "");
845 is_debug_build = (propBuf[0] == '1');
847 signal(SIGHUP, sig_hup);
852 // 1. parse cmd line args
853 // 2. read config file
856 // perform a profile collection
859 int perfprofd_main(int argc, char** argv)
863 W_ALOGI("starting Android Wide Profiling daemon");
865 parse_args(argc, argv);
868 // Early exit if we're not supposed to run on this build flavor
869 if (is_debug_build != 1 &&
870 config.getUnsignedValue("only_debug_build") == 1) {
871 W_ALOGI("early exit due to inappropriate build type");
875 unsigned iterations = 0;
877 while(config.getUnsignedValue("main_loop_iterations") == 0 ||
878 iterations < config.getUnsignedValue("main_loop_iterations")) {
880 // Figure out where in the collection interval we're going to actually
882 unsigned sleep_before_collect = 0;
883 unsigned sleep_after_collect = 0;
884 determine_before_after(sleep_before_collect, sleep_after_collect,
885 config.getUnsignedValue("collection_interval"));
886 perfprofd_sleep(sleep_before_collect);
888 // Reread config file -- the uploader may have rewritten it as a result
889 // of a gservices change
890 config.readFile(false);
892 // Check for profiling enabled...
893 CKPROFILE_RESULT ckresult = check_profiling_enabled(config);
894 if (ckresult != DO_COLLECT_PROFILE) {
895 W_ALOGI("profile collection skipped (%s)",
896 ckprofile_result_to_string(ckresult));
898 // Kick off the profiling run...
899 W_ALOGI("initiating profile collection");
900 PROFILE_RESULT result = collect_profile(config, seq);
901 if (result != OK_PROFILE_COLLECTION) {
902 W_ALOGI("profile collection failed (%s)",
903 profile_result_to_string(result));
905 if (post_process(config, seq)) {
908 W_ALOGI("profile collection complete");
911 perfprofd_sleep(sleep_after_collect);
915 W_ALOGI("finishing Android Wide Profiling daemon");