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, 1, 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, 1, UINT32_MAX);
96 // Length of time to collect samples (number of seconds for 'perf
98 addUnsignedEntry("sample_duration", config.sample_duration_in_s, 1, 600);
100 // If this parameter is non-zero it will cause perfprofd to
101 // exit immediately if the build type is not userdebug or eng.
102 // Currently defaults to 1 (true).
103 addUnsignedEntry("only_debug_build", config.only_debug_build ? 1 : 0, 0, 1);
105 // If the "mpdecision" service is running at the point we are ready
106 // to kick off a profiling run, then temporarily disable the service
107 // and hard-wire all cores on prior to the collection run, provided
108 // that the duration of the recording is less than or equal to the value of
109 // 'hardwire_cpus_max_duration'.
110 addUnsignedEntry("hardwire_cpus", config.hardwire_cpus, 0, 1);
111 addUnsignedEntry("hardwire_cpus_max_duration",
112 config.hardwire_cpus_max_duration_in_s,
116 // Maximum number of unprocessed profiles we can accumulate in the
117 // destination directory. Once we reach this limit, we continue
118 // to collect, but we just overwrite the most recent profile.
119 addUnsignedEntry("max_unprocessed_profiles", config.max_unprocessed_profiles, 1, UINT32_MAX);
121 // If set to 1, pass the -g option when invoking 'perf' (requests
122 // stack traces as opposed to flat profile).
123 addUnsignedEntry("stack_profile", config.stack_profile ? 1 : 0, 0, 1);
125 // For unit testing only: if set to 1, emit info messages on config
127 addUnsignedEntry("trace_config_read", config.trace_config_read ? 1 : 0, 0, 1);
129 // Control collection of various additional profile tags
130 addUnsignedEntry("collect_cpu_utilization", config.collect_cpu_utilization ? 1 : 0, 0, 1);
131 addUnsignedEntry("collect_charging_state", config.collect_charging_state ? 1 : 0, 0, 1);
132 addUnsignedEntry("collect_booting", config.collect_booting ? 1 : 0, 0, 1);
133 addUnsignedEntry("collect_camera_active", config.collect_camera_active ? 1 : 0, 0, 1);
135 // If true, use an ELF symbolizer to on-device symbolize.
136 addUnsignedEntry("use_elf_symbolizer", config.use_elf_symbolizer ? 1 : 0, 0, 1);
138 // If true, use libz to compress the output proto.
139 addUnsignedEntry("compress", config.compress ? 1 : 0, 0, 1);
141 // If true, send the proto to dropbox instead of to a file.
142 addUnsignedEntry("dropbox", config.send_to_dropbox ? 1 : 0, 0, 1);
145 void ConfigReader::addUnsignedEntry(const char *key,
146 unsigned default_value,
151 CHECK(u_entries.find(ks) == u_entries.end() &&
152 s_entries.find(ks) == s_entries.end())
153 << "internal error -- duplicate entry for key " << key;
155 vals.minv = min_value;
156 vals.maxv = max_value;
158 u_entries[ks] = default_value;
161 void ConfigReader::addStringEntry(const char *key, const char *default_value)
164 CHECK(u_entries.find(ks) == u_entries.end() &&
165 s_entries.find(ks) == s_entries.end())
166 << "internal error -- duplicate entry for key " << key;
167 CHECK(default_value != nullptr) << "internal error -- bad default value for key " << key;
168 s_entries[ks] = std::string(default_value);
171 unsigned ConfigReader::getUnsignedValue(const char *key) const
174 auto it = u_entries.find(ks);
175 CHECK(it != u_entries.end());
179 bool ConfigReader::getBoolValue(const char *key) const
182 auto it = u_entries.find(ks);
183 CHECK(it != u_entries.end());
184 return it->second != 0;
187 std::string ConfigReader::getStringValue(const char *key) const
190 auto it = s_entries.find(ks);
191 CHECK(it != s_entries.end());
195 void ConfigReader::overrideUnsignedEntry(const char *key, unsigned new_value)
198 auto it = u_entries.find(ks);
199 CHECK(it != u_entries.end());
201 auto iit = u_info.find(key);
202 CHECK(iit != u_info.end());
204 CHECK(new_value >= vals.minv && new_value <= vals.maxv);
205 it->second = new_value;
206 LOG(INFO) << "option " << key << " overridden to " << new_value;
211 // Parse a key=value pair read from the config file. This will issue
212 // warnings or errors to the system logs if the line can't be
213 // interpreted properly.
215 bool ConfigReader::parseLine(const char *key,
222 auto uit = u_entries.find(key);
223 if (uit != u_entries.end()) {
225 if (isdigit(value[0]) == 0 || sscanf(value, "%u", &uvalue) != 1) {
226 LOG(WARNING) << "line " << linecount << ": malformed unsigned value (ignored)";
230 auto iit = u_info.find(key);
231 assert(iit != u_info.end());
233 if (uvalue < vals.minv || uvalue > vals.maxv) {
234 LOG(WARNING) << "line " << linecount << ": "
235 << "specified value " << uvalue << " for '" << key << "' "
236 << "outside permitted range [" << vals.minv << " " << vals.maxv
240 if (trace_config_read) {
241 LOG(INFO) << "option " << key << " set to " << uvalue;
243 uit->second = uvalue;
246 trace_config_read = (getUnsignedValue("trace_config_read") != 0);
250 auto sit = s_entries.find(key);
251 if (sit != s_entries.end()) {
252 if (trace_config_read) {
253 LOG(INFO) << "option " << key << " set to " << value;
255 sit->second = std::string(value);
259 LOG(WARNING) << "line " << linecount << ": unknown option '" << key << "' ignored";
263 static bool isblank(const std::string &line)
265 auto non_space = [](char c) { return isspace(c) == 0; };
266 return std::find_if(line.begin(), line.end(), non_space) == line.end();
271 bool ConfigReader::readFile()
273 std::string contents;
274 if (! android::base::ReadFileToString(config_file_path, &contents)) {
277 return Read(contents, /* fail_on_error */ false);
280 bool ConfigReader::Read(const std::string& content, bool fail_on_error) {
281 std::stringstream ss(content);
283 for (unsigned linecount = 1;
284 std::getline(ss,line,'\n');
289 if (line[0] == '#') {
294 if (isblank(line.c_str())) {
298 // look for X=Y assignment
299 auto efound = line.find('=');
300 if (efound == std::string::npos) {
301 LOG(WARNING) << "line " << linecount << ": line malformed (no '=' found)";
308 std::string key(line.substr(0, efound));
309 std::string value(line.substr(efound+1, std::string::npos));
311 bool parse_success = parseLine(key.c_str(), value.c_str(), linecount);
312 if (fail_on_error && !parse_success) {
320 void ConfigReader::FillConfig(Config* config) {
321 config->collection_interval_in_s = getUnsignedValue("collection_interval");
323 config->use_fixed_seed = getUnsignedValue("use_fixed_seed");
325 config->main_loop_iterations = getUnsignedValue("main_loop_iterations");
327 config->destination_directory = getStringValue("destination_directory");
329 config->config_directory = getStringValue("config_directory");
331 config->perf_path = getStringValue("perf_path");
333 config->sampling_period = getUnsignedValue("sampling_period");
335 config->sample_duration_in_s = getUnsignedValue("sample_duration");
337 config->only_debug_build = getBoolValue("only_debug_build");
339 config->hardwire_cpus = getBoolValue("hardwire_cpus");
340 config->hardwire_cpus_max_duration_in_s = getUnsignedValue("hardwire_cpus_max_duration");
342 config->max_unprocessed_profiles = getUnsignedValue("max_unprocessed_profiles");
344 config->stack_profile = getBoolValue("stack_profile");
346 config->trace_config_read = getBoolValue("trace_config_read");
348 config->collect_cpu_utilization = getBoolValue("collect_cpu_utilization");
349 config->collect_charging_state = getBoolValue("collect_charging_state");
350 config->collect_booting = getBoolValue("collect_booting");
351 config->collect_camera_active = getBoolValue("collect_camera_active");
353 config->process = -1;
354 config->use_elf_symbolizer = getBoolValue("use_elf_symbolizer");
355 config->compress = getBoolValue("compress");
356 config->send_to_dropbox = getBoolValue("dropbox");