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 <cutils/properties.h>
38 #include "perfprofdcore.h"
39 #include "perfprofdutils.h"
40 #include "perf_data_converter.h"
41 #include "cpuconfig.h"
44 // Perf profiling daemon -- collects system-wide profiles using
46 // simpleperf record -a
48 // and encodes them so that they can be uploaded by a separate service.
51 //......................................................................
54 // Output file from 'perf record'.
56 #define PERF_OUTPUT "perf.data"
59 // This enum holds the results of the "should we profile" configuration check.
63 // All systems go for profile collection.
66 // The destination directory selected in the conf file doesn't exist. Most
67 // likely this is due to a missing or out-of-date version of the uploading
68 // service in GMS core.
69 DONT_PROFILE_MISSING_DESTINATION_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 = NULL;
108 // Set by SIGHUP signal handler
110 volatile unsigned please_reread_config_file = 0;
113 // This table describes the config file syntax in terms of key/value pairs.
114 // Values come in two flavors: strings, or unsigned integers. In the latter
115 // case the reader sets allowable minimum/maximum for the setting.
123 // Ask for the current setting of a config item
124 unsigned getUnsignedValue(const char *key) const;
125 std::string getStringValue(const char *key) const;
127 // read the specified config file, applying any settings it contains
128 void readFile(const char *configFilePath);
131 void addUnsignedEntry(const char *key,
132 unsigned default_value,
135 void addStringEntry(const char *key, const char *default_value);
136 void addDefaultEntries();
137 void parseLine(const char *key, const char *value, unsigned linecount);
139 typedef struct { unsigned minv, maxv; } values;
140 std::map<std::string, values> u_info;
141 std::map<std::string, unsigned> u_entries;
142 std::map<std::string, std::string> s_entries;
143 bool trace_config_read;
146 ConfigReader::ConfigReader()
147 : trace_config_read(false)
152 ConfigReader::~ConfigReader()
157 // Populate the reader with the set of allowable entries
159 void ConfigReader::addDefaultEntries()
161 // Average number of seconds between perf profile collections (if
162 // set to 100, then over time we want to see a perf profile
163 // collected every 100 seconds). The actual time within the interval
164 // for the collection is chosen randomly.
165 addUnsignedEntry("collection_interval", 901, 100, UINT32_MAX);
167 // Use the specified fixed seed for random number generation (unit
169 addUnsignedEntry("use_fixed_seed", 0, 0, UINT32_MAX);
171 // For testing purposes, number of times to iterate through main
172 // loop. Value of zero indicates that we should loop forever.
173 addUnsignedEntry("main_loop_iterations", 0, 0, UINT32_MAX);
175 // Destination directory (where to write profiles). This location
176 // chosen since it is accessible to the uploader service.
177 addStringEntry("destination_directory",
178 "/data/data/com.google.android.gms/files");
180 // Full path to 'perf' executable.
181 addStringEntry("perf_path", "/system/xbin/simpleperf");
183 // Desired sampling period (passed to perf -c option). Small
184 // sampling periods can perturb the collected profiles, so enforce
186 addUnsignedEntry("sampling_period", 500000, 5000, UINT32_MAX);
188 // Length of time to collect samples (number of seconds for 'perf
190 addUnsignedEntry("sample_duration", 3, 2, 600);
192 // If this parameter is non-zero it will cause perfprofd to
193 // exit immediately if the build type is not userdebug or eng.
194 // Currently defaults to 1 (true).
195 addUnsignedEntry("only_debug_build", 1, 0, 1);
197 // If the "mpdecision" service is running at the point we are ready
198 // to kick off a profiling run, then temporarily disable the service
199 // and hard-wire all cores on prior to the collection run, provided
200 // that the duration of the recording is less than or equal to the value of
201 // 'hardwire_cpus_max_duration'.
202 addUnsignedEntry("hardwire_cpus", 1, 0, 1);
203 addUnsignedEntry("hardwire_cpus_max_duration", 5, 1, UINT32_MAX);
205 // If set to 1, pass the -g option when invoking 'perf' (requests
206 // stack traces as opposed to flat profile).
207 addUnsignedEntry("stack_profile", 0, 0, 1);
209 // For unit testing only: if set to 1, emit info messages on config
211 addUnsignedEntry("trace_config_read", 0, 0, 1);
214 void ConfigReader::addUnsignedEntry(const char *key,
215 unsigned default_value,
220 if (u_entries.find(ks) != u_entries.end() ||
221 s_entries.find(ks) != s_entries.end()) {
222 W_ALOGE("internal error -- duplicate entry for key %s", key);
226 vals.minv = min_value;
227 vals.maxv = max_value;
229 u_entries[ks] = default_value;
232 void ConfigReader::addStringEntry(const char *key, const char *default_value)
235 if (u_entries.find(ks) != u_entries.end() ||
236 s_entries.find(ks) != s_entries.end()) {
237 W_ALOGE("internal error -- duplicate entry for key %s", key);
240 if (default_value == nullptr) {
241 W_ALOGE("internal error -- bad default value for key %s", key);
244 s_entries[ks] = std::string(default_value);
247 unsigned ConfigReader::getUnsignedValue(const char *key) const
250 auto it = u_entries.find(ks);
251 assert(it != u_entries.end());
255 std::string ConfigReader::getStringValue(const char *key) const
258 auto it = s_entries.find(ks);
259 assert(it != s_entries.end());
264 // Parse a key=value pair read from the config file. This will issue
265 // warnings or errors to the system logs if the line can't be
266 // interpreted properly.
268 void ConfigReader::parseLine(const char *key,
275 auto uit = u_entries.find(key);
276 if (uit != u_entries.end()) {
278 if (isdigit(value[0]) == 0 || sscanf(value, "%u", &uvalue) != 1) {
279 W_ALOGW("line %d: malformed unsigned value (ignored)", linecount);
282 auto iit = u_info.find(key);
283 assert(iit != u_info.end());
285 if (uvalue < vals.minv || uvalue > vals.maxv) {
286 W_ALOGW("line %d: specified value %u for '%s' "
287 "outside permitted range [%u %u] (ignored)",
288 linecount, uvalue, key, vals.minv, vals.maxv);
290 if (trace_config_read) {
291 W_ALOGI("option %s set to %u", key, uvalue);
293 uit->second = uvalue;
296 trace_config_read = (getUnsignedValue("trace_config_read") != 0);
300 auto sit = s_entries.find(key);
301 if (sit != s_entries.end()) {
302 if (trace_config_read) {
303 W_ALOGI("option %s set to %s", key, value);
305 sit->second = std::string(value);
309 W_ALOGW("line %d: unknown option '%s' ignored", linecount, key);
312 static bool isblank(const std::string &line)
314 for (std::string::const_iterator it = line.begin(); it != line.end(); ++it)
316 if (isspace(*it) == 0) {
323 void ConfigReader::readFile(const char *configFilePath)
325 FILE *fp = fopen(configFilePath, "r");
327 W_ALOGE("unable to open configuration file %s", config_file_path);
331 char *linebuf = NULL;
332 size_t line_length = 0;
333 for (unsigned linecount = 1;
334 getline(&linebuf, &line_length, fp) != -1;
340 if (linebuf[0] == '#') {
345 if (isblank(linebuf)) {
349 // look for X=Y assignment
350 eq = strchr(linebuf, '=');
352 W_ALOGW("line %d: line malformed (no '=' found)", linecount);
359 char *ln = strrchr(value, '\n');
360 if (ln) { *ln = '\0'; }
362 parseLine(key, value, linecount);
369 // Parse command line args. Currently you can supply "-c P" to set
370 // the path of the config file to P.
372 static void parse_args(int argc, char** argv)
376 for (ac = 1; ac < argc; ++ac) {
377 if (!strcmp(argv[ac], "-c")) {
379 W_ALOGE("malformed command line: -c option requires argument)");
382 config_file_path = strdup(argv[ac+1]);
383 W_ALOGI("config file path set to %s", config_file_path);
386 W_ALOGE("malformed command line: unknown option or arg %s)", argv[ac]);
393 // Convert a CKPROFILE_RESULT to a string
395 const char *ckprofile_result_to_string(CKPROFILE_RESULT result)
398 case DO_COLLECT_PROFILE:
399 return "DO_COLLECT_PROFILE";
400 case DONT_PROFILE_MISSING_DESTINATION_DIR:
401 return "missing destination directory";
402 case DONT_PROFILE_MISSING_SEMAPHORE:
403 return "missing semaphore file";
404 case DONT_PROFILE_MISSING_PERF_EXECUTABLE:
405 return "missing 'perf' executable";
406 case DONT_PROFILE_RUNNING_IN_EMULATOR:
407 return "running in emulator";
408 default: return "unknown";
414 // Convert a PROFILE_RESULT to a string
416 const char *profile_result_to_string(PROFILE_RESULT result)
419 case OK_PROFILE_COLLECTION:
420 return "profile collection succeeded";
421 case ERR_FORK_FAILED:
422 return "fork() system call failed";
423 case ERR_PERF_RECORD_FAILED:
424 return "perf record returned bad exit status";
425 case ERR_PERF_ENCODE_FAILED:
426 return "failure encoding perf.data to protobuf";
427 case ERR_OPEN_ENCODED_FILE_FAILED:
428 return "failed to open encoded perf file";
429 case ERR_WRITE_ENCODED_FILE_FAILED:
430 return "write to encoded perf file failed";
431 default: return "unknown";
437 // The daemon does a read of the main config file on startup, however
438 // if the destination directory also contains a config file, then we
439 // read parameters from that as well. This provides a mechanism for
440 // changing/controlling the behavior of the daemon via the settings
441 // established in the uploader service (which may be easier to update
444 static void read_aux_config(ConfigReader &config)
446 std::string destConfig(config.getStringValue("destination_directory"));
447 destConfig += "/perfprofd.conf";
448 FILE *fp = fopen(destConfig.c_str(), "r");
451 bool trace_config_read =
452 (config.getUnsignedValue("trace_config_read") != 0);
453 if (trace_config_read) {
454 W_ALOGI("reading auxiliary config file %s", destConfig.c_str());
456 config.readFile(destConfig.c_str());
461 // Check to see whether we should perform a profile collection
463 static CKPROFILE_RESULT check_profiling_enabled(ConfigReader &config)
466 // Profile collection in the emulator doesn't make sense
468 assert(running_in_emulator != -1);
469 if (running_in_emulator) {
470 return DONT_PROFILE_RUNNING_IN_EMULATOR;
474 // Check for the existence of the destination directory
476 std::string destdir = config.getStringValue("destination_directory");
477 DIR* dir = opendir(destdir.c_str());
479 W_ALOGW("unable to open destination directory %s: (%s)",
480 destdir.c_str(), strerror(errno));
481 return DONT_PROFILE_MISSING_DESTINATION_DIR;
484 // Reread aux config file -- it may have changed
485 read_aux_config(config);
487 // Check for existence of simpleperf/perf executable
488 std::string pp = config.getStringValue("perf_path");
489 if (access(pp.c_str(), R_OK|X_OK) == -1) {
490 W_ALOGW("unable to access/execute %s", pp.c_str());
492 return DONT_PROFILE_MISSING_PERF_EXECUTABLE;
495 // Check for existence of semaphore file
498 while ((e = readdir(dir)) != 0) {
499 if (!strcmp(e->d_name, SEMAPHORE_FILENAME)) {
506 return DONT_PROFILE_MISSING_SEMAPHORE;
512 return DO_COLLECT_PROFILE;
515 inline char* string_as_array(std::string* str) {
516 return str->empty() ? NULL : &*str->begin();
519 PROFILE_RESULT encode_to_proto(const std::string &data_file_path,
520 const std::string &encoded_file_path)
523 // Open and read perf.data file
525 const wireless_android_play_playlog::AndroidPerfProfile &encodedProfile =
526 wireless_android_logging_awp::RawPerfDataToAndroidPerfProfile(data_file_path);
529 // Issue error if no samples
531 if (encodedProfile.programs().size() == 0) {
532 return ERR_PERF_ENCODE_FAILED;
536 // Serialize protobuf to array
538 int size = encodedProfile.ByteSize();
541 ::google::protobuf::uint8* dtarget =
542 reinterpret_cast<::google::protobuf::uint8*>(string_as_array(&data));
543 encodedProfile.SerializeWithCachedSizesToArray(dtarget);
546 // Open file and write encoded data to it
548 FILE *fp = fopen(encoded_file_path.c_str(), "w");
550 return ERR_OPEN_ENCODED_FILE_FAILED;
553 if (fwrite(dtarget, fsiz, 1, fp) != 1) {
555 return ERR_WRITE_ENCODED_FILE_FAILED;
559 return OK_PROFILE_COLLECTION;
563 // Invoke "perf record". Return value is OK_PROFILE_COLLECTION for
564 // success, or some other error code if something went wrong.
566 static PROFILE_RESULT invoke_perf(const std::string &perf_path,
567 unsigned sampling_period,
568 const char *stack_profile_opt,
570 const std::string &data_file_path,
571 const std::string &perf_stderr_path)
576 return ERR_FORK_FAILED;
582 // Open file to receive stderr/stdout from perf
583 FILE *efp = fopen(perf_stderr_path.c_str(), "w");
585 dup2(fileno(efp), STDERR_FILENO);
586 dup2(fileno(efp), STDOUT_FILENO);
588 W_ALOGW("unable to open %s for writing", perf_stderr_path.c_str());
591 // marshall arguments
592 constexpr unsigned max_args = 12;
593 const char *argv[max_args];
595 argv[slot++] = perf_path.c_str();
596 argv[slot++] = "record";
600 argv[slot++] = data_file_path.c_str();
604 char pbuf[64]; snprintf(pbuf, 64, "%u", sampling_period);
608 if (stack_profile_opt)
609 argv[slot++] = stack_profile_opt;
611 // system wide profiling
615 argv[slot++] = "/system/bin/sleep";
616 char dbuf[64]; snprintf(dbuf, 64, "%u", duration);
620 argv[slot++] = nullptr;
621 assert(slot < max_args);
623 // record the final command line in the error output file for
624 // posterity/debugging purposes
625 fprintf(stderr, "perf invocation (pid=%d):\n", getpid());
626 for (unsigned i = 0; argv[i] != nullptr; ++i) {
627 fprintf(stderr, "%s%s", i ? " " : "", argv[i]);
629 fprintf(stderr, "\n");
632 execvp(argv[0], (char * const *)argv);
633 fprintf(stderr, "exec failed: %s\n", strerror(errno));
639 pid_t reaped = TEMP_FAILURE_RETRY(waitpid(pid, &st, 0));
642 W_ALOGW("waitpid failed: %s", strerror(errno));
643 } else if (WIFSIGNALED(st)) {
644 W_ALOGW("perf killed by signal %d", WTERMSIG(st));
645 } else if (WEXITSTATUS(st) != 0) {
646 W_ALOGW("perf bad exit status %d", WEXITSTATUS(st));
648 return OK_PROFILE_COLLECTION;
652 return ERR_PERF_RECORD_FAILED;
656 // Collect a perf profile. Steps for this operation are:
657 // - kick off 'perf record'
658 // - read perf.data, convert to protocol buf
660 static PROFILE_RESULT collect_profile(ConfigReader &config)
663 // Form perf.data file name, perf error output file name
665 std::string destdir = config.getStringValue("destination_directory");
666 std::string data_file_path(destdir);
667 data_file_path += "/";
668 data_file_path += PERF_OUTPUT;
669 std::string perf_stderr_path(destdir);
670 perf_stderr_path += "/perferr.txt";
673 // Remove any existing perf.data file -- if we don't do this, perf
674 // will rename the old file and we'll have extra cruft lying around.
677 if (stat(data_file_path.c_str(), &statb) == 0) { // if file exists...
678 if (unlink(data_file_path.c_str())) { // then try to remove
679 W_ALOGW("unable to unlink previous perf.data file");
684 // The "mpdecision" daemon can cause problems for profile
685 // collection: if it decides to online a CPU partway through the
686 // 'perf record' run, the activity on that CPU will be invisible to
687 // perf, and if it offlines a CPU during the recording this can
688 // sometimes leave the PMU in an unusable state (dmesg errors of the
689 // form "perfevents: unable to request IRQXXX for ..."). To avoid
690 // these issues, if "mpdecision" is running the helper below will
691 // stop the service and then online all available CPUs. The object
692 // destructor (invoked when this routine terminates) will then
693 // restart the service again when needed.
695 unsigned duration = config.getUnsignedValue("sample_duration");
696 unsigned hardwire = config.getUnsignedValue("hardwire_cpus");
697 unsigned max_duration = config.getUnsignedValue("hardwire_cpus_max_duration");
698 bool take_action = (hardwire && duration <= max_duration);
699 HardwireCpuHelper helper(take_action);
704 const char *stack_profile_opt =
705 (config.getUnsignedValue("stack_profile") != 0 ? "-g" : nullptr);
706 std::string perf_path = config.getStringValue("perf_path");
707 unsigned period = config.getUnsignedValue("sampling_period");
709 PROFILE_RESULT ret = invoke_perf(perf_path.c_str(),
715 if (ret != OK_PROFILE_COLLECTION) {
720 // Read the resulting perf.data file, encode into protocol buffer, then write
721 // the result to the file perf.data.encoded
723 std::string encoded_file_path(data_file_path);
724 encoded_file_path += ".encoded";
725 return encode_to_proto(data_file_path, encoded_file_path);
729 // SIGHUP handler. Sets a flag to indicate that we should reread the
732 static void sig_hup(int /* signum */)
734 please_reread_config_file = 1;
738 // Assuming that we want to collect a profile every N seconds,
739 // randomly partition N into two sub-intervals.
741 static void determine_before_after(unsigned &sleep_before_collect,
742 unsigned &sleep_after_collect,
743 unsigned collection_interval)
745 double frac = erand48(random_seed);
746 sleep_before_collect = (unsigned) (((double)collection_interval) * frac);
747 assert(sleep_before_collect <= collection_interval);
748 sleep_after_collect = collection_interval - sleep_before_collect;
752 // Set random number generator seed
754 static void set_seed(ConfigReader &config)
757 unsigned use_fixed_seed = config.getUnsignedValue("use_fixed_seed");
758 if (use_fixed_seed) {
760 // Use fixed user-specified seed
762 seed = use_fixed_seed;
769 W_ALOGI("random seed set to %u", seed);
770 // Distribute the 32-bit seed into the three 16-bit array
771 // elements. The specific values being written do not especially
772 // matter as long as we are setting them to something based on the seed.
773 random_seed[0] = seed & 0xffff;
774 random_seed[1] = (seed >> 16);
775 random_seed[2] = (random_seed[0] ^ random_seed[1]);
781 static void init(ConfigReader &config)
783 if (config_file_path != NULL) {
784 config.readFile(config_file_path);
788 char propBuf[PROPERTY_VALUE_MAX];
790 property_get("ro.kernel.qemu", propBuf, "");
791 running_in_emulator = (propBuf[0] == '1');
792 property_get("ro.debuggable", propBuf, "");
793 is_debug_build = (propBuf[0] == '1');
795 signal(SIGHUP, sig_hup);
800 // 1. parse cmd line args
801 // 2. read config file
804 // perform a profile collection
807 int perfprofd_main(int argc, char** argv)
811 W_ALOGI("starting Android Wide Profiling daemon");
813 parse_args(argc, argv);
815 read_aux_config(config);
817 // Early exit if we're not supposed to run on this build flavor
818 if (is_debug_build != 1 &&
819 config.getUnsignedValue("only_debug_build") == 1) {
820 W_ALOGI("early exit due to inappropriate build type");
824 unsigned iterations = 0;
825 while(config.getUnsignedValue("main_loop_iterations") == 0 ||
826 iterations < config.getUnsignedValue("main_loop_iterations")) {
828 // Figure out where in the collection interval we're going to actually
830 unsigned sleep_before_collect = 0;
831 unsigned sleep_after_collect = 0;
832 determine_before_after(sleep_before_collect, sleep_after_collect,
833 config.getUnsignedValue("collection_interval"));
834 perfprofd_sleep(sleep_before_collect);
836 // Reread config file if someone sent a SIGHUP
837 if (please_reread_config_file) {
838 if (config_file_path) {
839 config.readFile(config_file_path);
841 read_aux_config(config);
843 please_reread_config_file = 0;
846 // Check for profiling enabled...
847 CKPROFILE_RESULT ckresult = check_profiling_enabled(config);
848 if (ckresult != DO_COLLECT_PROFILE) {
849 W_ALOGI("profile collection skipped (%s)",
850 ckprofile_result_to_string(ckresult));
852 // Kick off the profiling run...
853 W_ALOGI("initiating profile collection");
854 PROFILE_RESULT result = collect_profile(config);
855 if (result != OK_PROFILE_COLLECTION) {
856 W_ALOGI("profile collection failed (%s)",
857 profile_result_to_string(result));
859 W_ALOGI("profile collection complete");
862 perfprofd_sleep(sleep_after_collect);
866 W_ALOGI("finishing Android Wide Profiling daemon");