1 /*
2 **
3 ** Copyright 2015, The Android Open Source Project
4 **
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
8 **
9 ** http://www.apache.org/licenses/LICENSE-2.0
10 **
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.
16 */
17
18 #include <assert.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21
22 #include <algorithm>
23 #include <cstring>
24 #include <sstream>
25
26 #include <android-base/file.h>
27 #include <android-base/logging.h>
28
29 #include "configreader.h"
30
31 //
32 // Config file path
33 //
34 static const char *config_file_path =
35 "/data/data/com.google.android.gms/files/perfprofd.conf";
36
ConfigReader()37 ConfigReader::ConfigReader()
38 : trace_config_read(false)
39 {
40 addDefaultEntries();
41 }
42
~ConfigReader()43 ConfigReader::~ConfigReader()
44 {
45 }
46
getConfigFilePath()47 const char *ConfigReader::getConfigFilePath()
48 {
49 return config_file_path;
50 }
51
setConfigFilePath(const char * path)52 void ConfigReader::setConfigFilePath(const char *path)
53 {
54 config_file_path = strdup(path);
55 LOG(INFO) << "config file path set to " << config_file_path;
56 }
57
58 //
59 // Populate the reader with the set of allowable entries
60 //
addDefaultEntries()61 void ConfigReader::addDefaultEntries()
62 {
63 struct DummyConfig : public Config {
64 void Sleep(size_t seconds) override {}
65 bool IsProfilingEnabled() const override { return false; }
66 };
67 DummyConfig config;
68
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);
74
75 // Use the specified fixed seed for random number generation (unit
76 // testing)
77 addUnsignedEntry("use_fixed_seed", config.use_fixed_seed, 0, UINT32_MAX);
78
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);
82
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());
86
87 // Config directory (where to read configs).
88 addStringEntry("config_directory", config.config_directory.c_str());
89
90 // Full path to 'perf' executable.
91 addStringEntry("perf_path", config.perf_path.c_str());
92
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);
97
98 // Length of time to collect samples (number of seconds for 'perf
99 // record -a' run).
100 addUnsignedEntry("sample_duration", config.sample_duration_in_s, 1, 600);
101
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);
106
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,
115 1,
116 UINT32_MAX);
117
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);
122
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);
126
127 // For unit testing only: if set to 1, emit info messages on config
128 // file parsing.
129 addUnsignedEntry("trace_config_read", config.trace_config_read ? 1 : 0, 0, 1);
130
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);
136
137 // If true, use an ELF symbolizer to on-device symbolize.
138 addUnsignedEntry("use_elf_symbolizer", config.use_elf_symbolizer ? 1 : 0, 0, 1);
139
140 // If true, use libz to compress the output proto.
141 addUnsignedEntry("compress", config.compress ? 1 : 0, 0, 1);
142
143 // If true, send the proto to dropbox instead of to a file.
144 addUnsignedEntry("dropbox", config.send_to_dropbox ? 1 : 0, 0, 1);
145
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);
149 }
150
addUnsignedEntry(const char * key,unsigned default_value,unsigned min_value,unsigned max_value)151 void ConfigReader::addUnsignedEntry(const char *key,
152 unsigned default_value,
153 unsigned min_value,
154 unsigned max_value)
155 {
156 std::string ks(key);
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;
160 values vals;
161 vals.minv = min_value;
162 vals.maxv = max_value;
163 u_info[ks] = vals;
164 u_entries[ks] = default_value;
165 }
166
addStringEntry(const char * key,const char * default_value)167 void ConfigReader::addStringEntry(const char *key, const char *default_value)
168 {
169 std::string ks(key);
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);
175 }
176
getUnsignedValue(const char * key) const177 unsigned ConfigReader::getUnsignedValue(const char *key) const
178 {
179 std::string ks(key);
180 auto it = u_entries.find(ks);
181 CHECK(it != u_entries.end());
182 return it->second;
183 }
184
getBoolValue(const char * key) const185 bool ConfigReader::getBoolValue(const char *key) const
186 {
187 std::string ks(key);
188 auto it = u_entries.find(ks);
189 CHECK(it != u_entries.end());
190 return it->second != 0;
191 }
192
getStringValue(const char * key) const193 std::string ConfigReader::getStringValue(const char *key) const
194 {
195 std::string ks(key);
196 auto it = s_entries.find(ks);
197 CHECK(it != s_entries.end());
198 return it->second;
199 }
200
overrideUnsignedEntry(const char * key,unsigned new_value)201 void ConfigReader::overrideUnsignedEntry(const char *key, unsigned new_value)
202 {
203 std::string ks(key);
204 auto it = u_entries.find(ks);
205 CHECK(it != u_entries.end());
206 values vals;
207 auto iit = u_info.find(key);
208 CHECK(iit != u_info.end());
209 vals = iit->second;
210 CHECK(new_value >= vals.minv && new_value <= vals.maxv);
211 it->second = new_value;
212 LOG(INFO) << "option " << key << " overridden to " << new_value;
213 }
214
215
216 //
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.
220 //
parseLine(const char * key,const char * value,unsigned linecount)221 bool ConfigReader::parseLine(const char *key,
222 const char *value,
223 unsigned linecount)
224 {
225 assert(key);
226 assert(value);
227
228 auto uit = u_entries.find(key);
229 if (uit != u_entries.end()) {
230 unsigned uvalue = 0;
231 if (isdigit(value[0]) == 0 || sscanf(value, "%u", &uvalue) != 1) {
232 LOG(WARNING) << "line " << linecount << ": malformed unsigned value (ignored)";
233 return false;
234 } else {
235 values vals;
236 auto iit = u_info.find(key);
237 assert(iit != u_info.end());
238 vals = iit->second;
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
243 << "] (ignored)";
244 return false;
245 } else {
246 if (trace_config_read) {
247 LOG(INFO) << "option " << key << " set to " << uvalue;
248 }
249 uit->second = uvalue;
250 }
251 }
252 trace_config_read = (getUnsignedValue("trace_config_read") != 0);
253 return true;
254 }
255
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;
260 }
261 sit->second = std::string(value);
262 return true;
263 }
264
265 LOG(WARNING) << "line " << linecount << ": unknown option '" << key << "' ignored";
266 return false;
267 }
268
isblank(const std::string & line)269 static bool isblank(const std::string &line)
270 {
271 auto non_space = [](char c) { return isspace(c) == 0; };
272 return std::find_if(line.begin(), line.end(), non_space) == line.end();
273 }
274
275
276
readFile()277 bool ConfigReader::readFile()
278 {
279 std::string contents;
280 if (! android::base::ReadFileToString(config_file_path, &contents)) {
281 return false;
282 }
283 return Read(contents, /* fail_on_error */ false);
284 }
285
Read(const std::string & content,bool fail_on_error)286 bool ConfigReader::Read(const std::string& content, bool fail_on_error) {
287 std::stringstream ss(content);
288 std::string line;
289 for (unsigned linecount = 1;
290 std::getline(ss,line,'\n');
291 linecount += 1)
292 {
293
294 // comment line?
295 if (line[0] == '#') {
296 continue;
297 }
298
299 // blank line?
300 if (isblank(line.c_str())) {
301 continue;
302 }
303
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)";
308 if (fail_on_error) {
309 return false;
310 }
311 continue;
312 }
313
314 std::string key(line.substr(0, efound));
315 std::string value(line.substr(efound+1, std::string::npos));
316
317 bool parse_success = parseLine(key.c_str(), value.c_str(), linecount);
318 if (fail_on_error && !parse_success) {
319 return false;
320 }
321 }
322
323 return true;
324 }
325
FillConfig(Config * config)326 void ConfigReader::FillConfig(Config* config) {
327 config->collection_interval_in_s = getUnsignedValue("collection_interval");
328
329 config->use_fixed_seed = getUnsignedValue("use_fixed_seed");
330
331 config->main_loop_iterations = getUnsignedValue("main_loop_iterations");
332
333 config->destination_directory = getStringValue("destination_directory");
334
335 config->config_directory = getStringValue("config_directory");
336
337 config->perf_path = getStringValue("perf_path");
338
339 config->sampling_period = getUnsignedValue("sampling_period");
340 config->sampling_frequency = getUnsignedValue("sampling_frequency");
341
342 config->sample_duration_in_s = getUnsignedValue("sample_duration");
343
344 config->only_debug_build = getBoolValue("only_debug_build");
345
346 config->hardwire_cpus = getBoolValue("hardwire_cpus");
347 config->hardwire_cpus_max_duration_in_s = getUnsignedValue("hardwire_cpus_max_duration");
348
349 config->max_unprocessed_profiles = getUnsignedValue("max_unprocessed_profiles");
350
351 config->stack_profile = getBoolValue("stack_profile");
352
353 config->trace_config_read = getBoolValue("trace_config_read");
354
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");
359
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");
364 }
365