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.
26 #include <android-base/file.h>
27 #include <android-base/logging.h>
29 #include "configreader.h"
34 static const char *config_file_path =
35 "/data/data/com.google.android.gms/files/perfprofd.conf";
37 ConfigReader::ConfigReader()
38 : trace_config_read(false)
43 ConfigReader::~ConfigReader()
47 const char *ConfigReader::getConfigFilePath()
49 return config_file_path;
52 void ConfigReader::setConfigFilePath(const char *path)
54 config_file_path = strdup(path);
55 LOG(INFO) << "config file path set to " << config_file_path;
59 // Populate the reader with the set of allowable entries
61 void ConfigReader::addDefaultEntries()
63 struct DummyConfig : public Config {
64 void Sleep(size_t seconds) override {}
65 bool IsProfilingEnabled() const override { return false; }
69 // Average number of seconds between perf profile collections (if
70 // set to 100, then over time we want to see a perf profile
71 // collected every 100 seconds). The actual time within the interval
72 // for the collection is chosen randomly.
73 addUnsignedEntry("collection_interval", config.collection_interval_in_s, 0, UINT32_MAX);
75 // Use the specified fixed seed for random number generation (unit
77 addUnsignedEntry("use_fixed_seed", config.use_fixed_seed, 0, UINT32_MAX);
79 // For testing purposes, number of times to iterate through main
80 // loop. Value of zero indicates that we should loop forever.
81 addUnsignedEntry("main_loop_iterations", config.main_loop_iterations, 0, UINT32_MAX);
83 // Destination directory (where to write profiles). This location
84 // chosen since it is accessible to the uploader service.
85 addStringEntry("destination_directory", config.destination_directory.c_str());
87 // Config directory (where to read configs).
88 addStringEntry("config_directory", config.config_directory.c_str());
90 // Full path to 'perf' executable.
91 addStringEntry("perf_path", config.perf_path.c_str());
93 // Desired sampling period (passed to perf -c option).
94 addUnsignedEntry("sampling_period", config.sampling_period, 0, UINT32_MAX);
95 // Desired sampling frequency (passed to perf -f option).
96 addUnsignedEntry("sampling_frequency", config.sampling_frequency, 0, UINT32_MAX);
98 // Length of time to collect samples (number of seconds for 'perf
100 addUnsignedEntry("sample_duration", config.sample_duration_in_s, 1, 600);
102 // If this parameter is non-zero it will cause perfprofd to
103 // exit immediately if the build type is not userdebug or eng.
104 // Currently defaults to 1 (true).
105 addUnsignedEntry("only_debug_build", config.only_debug_build ? 1 : 0, 0, 1);
107 // If the "mpdecision" service is running at the point we are ready
108 // to kick off a profiling run, then temporarily disable the service
109 // and hard-wire all cores on prior to the collection run, provided
110 // that the duration of the recording is less than or equal to the value of
111 // 'hardwire_cpus_max_duration'.
112 addUnsignedEntry("hardwire_cpus", config.hardwire_cpus, 0, 1);
113 addUnsignedEntry("hardwire_cpus_max_duration",
114 config.hardwire_cpus_max_duration_in_s,
118 // Maximum number of unprocessed profiles we can accumulate in the
119 // destination directory. Once we reach this limit, we continue
120 // to collect, but we just overwrite the most recent profile.
121 addUnsignedEntry("max_unprocessed_profiles", config.max_unprocessed_profiles, 1, UINT32_MAX);
123 // If set to 1, pass the -g option when invoking 'perf' (requests
124 // stack traces as opposed to flat profile).
125 addUnsignedEntry("stack_profile", config.stack_profile ? 1 : 0, 0, 1);
127 // For unit testing only: if set to 1, emit info messages on config
129 addUnsignedEntry("trace_config_read", config.trace_config_read ? 1 : 0, 0, 1);
131 // Control collection of various additional profile tags
132 addUnsignedEntry("collect_cpu_utilization", config.collect_cpu_utilization ? 1 : 0, 0, 1);
133 addUnsignedEntry("collect_charging_state", config.collect_charging_state ? 1 : 0, 0, 1);
134 addUnsignedEntry("collect_booting", config.collect_booting ? 1 : 0, 0, 1);
135 addUnsignedEntry("collect_camera_active", config.collect_camera_active ? 1 : 0, 0, 1);
137 // If true, use an ELF symbolizer to on-device symbolize.
138 addUnsignedEntry("use_elf_symbolizer", config.use_elf_symbolizer ? 1 : 0, 0, 1);
140 // If true, use libz to compress the output proto.
141 addUnsignedEntry("compress", config.compress ? 1 : 0, 0, 1);
143 // If true, send the proto to dropbox instead of to a file.
144 addUnsignedEntry("dropbox", config.send_to_dropbox ? 1 : 0, 0, 1);
146 // The pid of the process to profile. May be negative, in which case
147 // the whole system will be profiled.
148 addUnsignedEntry("process", static_cast<uint32_t>(-1), 0, UINT32_MAX);
151 void ConfigReader::addUnsignedEntry(const char *key,
152 unsigned default_value,
157 CHECK(u_entries.find(ks) == u_entries.end() &&
158 s_entries.find(ks) == s_entries.end())
159 << "internal error -- duplicate entry for key " << key;
161 vals.minv = min_value;
162 vals.maxv = max_value;
164 u_entries[ks] = default_value;
167 void ConfigReader::addStringEntry(const char *key, const char *default_value)
170 CHECK(u_entries.find(ks) == u_entries.end() &&
171 s_entries.find(ks) == s_entries.end())
172 << "internal error -- duplicate entry for key " << key;
173 CHECK(default_value != nullptr) << "internal error -- bad default value for key " << key;
174 s_entries[ks] = std::string(default_value);
177 unsigned ConfigReader::getUnsignedValue(const char *key) const
180 auto it = u_entries.find(ks);
181 CHECK(it != u_entries.end());
185 bool ConfigReader::getBoolValue(const char *key) const
188 auto it = u_entries.find(ks);
189 CHECK(it != u_entries.end());
190 return it->second != 0;
193 std::string ConfigReader::getStringValue(const char *key) const
196 auto it = s_entries.find(ks);
197 CHECK(it != s_entries.end());
201 void ConfigReader::overrideUnsignedEntry(const char *key, unsigned new_value)
204 auto it = u_entries.find(ks);
205 CHECK(it != u_entries.end());
207 auto iit = u_info.find(key);
208 CHECK(iit != u_info.end());
210 CHECK(new_value >= vals.minv && new_value <= vals.maxv);
211 it->second = new_value;
212 LOG(INFO) << "option " << key << " overridden to " << new_value;
217 // Parse a key=value pair read from the config file. This will issue
218 // warnings or errors to the system logs if the line can't be
219 // interpreted properly.
221 bool ConfigReader::parseLine(const char *key,
228 auto uit = u_entries.find(key);
229 if (uit != u_entries.end()) {
231 if (isdigit(value[0]) == 0 || sscanf(value, "%u", &uvalue) != 1) {
232 LOG(WARNING) << "line " << linecount << ": malformed unsigned value (ignored)";
236 auto iit = u_info.find(key);
237 assert(iit != u_info.end());
239 if (uvalue < vals.minv || uvalue > vals.maxv) {
240 LOG(WARNING) << "line " << linecount << ": "
241 << "specified value " << uvalue << " for '" << key << "' "
242 << "outside permitted range [" << vals.minv << " " << vals.maxv
246 if (trace_config_read) {
247 LOG(INFO) << "option " << key << " set to " << uvalue;
249 uit->second = uvalue;
252 trace_config_read = (getUnsignedValue("trace_config_read") != 0);
256 auto sit = s_entries.find(key);
257 if (sit != s_entries.end()) {
258 if (trace_config_read) {
259 LOG(INFO) << "option " << key << " set to " << value;
261 sit->second = std::string(value);
265 LOG(WARNING) << "line " << linecount << ": unknown option '" << key << "' ignored";
269 static bool isblank(const std::string &line)
271 auto non_space = [](char c) { return isspace(c) == 0; };
272 return std::find_if(line.begin(), line.end(), non_space) == line.end();
277 bool ConfigReader::readFile()
279 std::string contents;
280 if (! android::base::ReadFileToString(config_file_path, &contents)) {
283 return Read(contents, /* fail_on_error */ false);
286 bool ConfigReader::Read(const std::string& content, bool fail_on_error) {
287 std::stringstream ss(content);
289 for (unsigned linecount = 1;
290 std::getline(ss,line,'\n');
295 if (line[0] == '#') {
300 if (isblank(line.c_str())) {
304 // look for X=Y assignment
305 auto efound = line.find('=');
306 if (efound == std::string::npos) {
307 LOG(WARNING) << "line " << linecount << ": line malformed (no '=' found)";
314 std::string key(line.substr(0, efound));
315 std::string value(line.substr(efound+1, std::string::npos));
317 bool parse_success = parseLine(key.c_str(), value.c_str(), linecount);
318 if (fail_on_error && !parse_success) {
326 void ConfigReader::FillConfig(Config* config) {
327 config->collection_interval_in_s = getUnsignedValue("collection_interval");
329 config->use_fixed_seed = getUnsignedValue("use_fixed_seed");
331 config->main_loop_iterations = getUnsignedValue("main_loop_iterations");
333 config->destination_directory = getStringValue("destination_directory");
335 config->config_directory = getStringValue("config_directory");
337 config->perf_path = getStringValue("perf_path");
339 config->sampling_period = getUnsignedValue("sampling_period");
340 config->sampling_frequency = getUnsignedValue("sampling_frequency");
342 config->sample_duration_in_s = getUnsignedValue("sample_duration");
344 config->only_debug_build = getBoolValue("only_debug_build");
346 config->hardwire_cpus = getBoolValue("hardwire_cpus");
347 config->hardwire_cpus_max_duration_in_s = getUnsignedValue("hardwire_cpus_max_duration");
349 config->max_unprocessed_profiles = getUnsignedValue("max_unprocessed_profiles");
351 config->stack_profile = getBoolValue("stack_profile");
353 config->trace_config_read = getBoolValue("trace_config_read");
355 config->collect_cpu_utilization = getBoolValue("collect_cpu_utilization");
356 config->collect_charging_state = getBoolValue("collect_charging_state");
357 config->collect_booting = getBoolValue("collect_booting");
358 config->collect_camera_active = getBoolValue("collect_camera_active");
360 config->process = static_cast<int32_t>(getUnsignedValue("process"));
361 config->use_elf_symbolizer = getBoolValue("use_elf_symbolizer");
362 config->compress = getBoolValue("compress");
363 config->send_to_dropbox = getBoolValue("dropbox");