OSDN Git Service

Perfprofd: Use ConfigReader for shellCommand startProfiling
[android-x86/system-extras.git] / perfprofd / configreader.cc
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
37 ConfigReader::ConfigReader()
38     : trace_config_read(false)
39 {
40   addDefaultEntries();
41 }
42
43 ConfigReader::~ConfigReader()
44 {
45 }
46
47 const char *ConfigReader::getConfigFilePath()
48 {
49   return config_file_path;
50 }
51
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 //
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, 1, 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, 1, UINT32_MAX);
95
96   // Length of time to collect samples (number of seconds for 'perf
97   // record -a' run).
98   addUnsignedEntry("sample_duration", config.sample_duration_in_s, 1, 600);
99
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);
104
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,
113                    1,
114                    UINT32_MAX);
115
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);
120
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);
124
125   // For unit testing only: if set to 1, emit info messages on config
126   // file parsing.
127   addUnsignedEntry("trace_config_read", config.trace_config_read ? 1 : 0, 0, 1);
128
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);
134
135   // If true, use an ELF symbolizer to on-device symbolize.
136   addUnsignedEntry("use_elf_symbolizer", config.use_elf_symbolizer ? 1 : 0, 0, 1);
137
138   // If true, use libz to compress the output proto.
139   addUnsignedEntry("compress", config.compress ? 1 : 0, 0, 1);
140
141   // If true, send the proto to dropbox instead of to a file.
142   addUnsignedEntry("dropbox", config.send_to_dropbox ? 1 : 0, 0, 1);
143 }
144
145 void ConfigReader::addUnsignedEntry(const char *key,
146                                     unsigned default_value,
147                                     unsigned min_value,
148                                     unsigned max_value)
149 {
150   std::string ks(key);
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;
154   values vals;
155   vals.minv = min_value;
156   vals.maxv = max_value;
157   u_info[ks] = vals;
158   u_entries[ks] = default_value;
159 }
160
161 void ConfigReader::addStringEntry(const char *key, const char *default_value)
162 {
163   std::string ks(key);
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);
169 }
170
171 unsigned ConfigReader::getUnsignedValue(const char *key) const
172 {
173   std::string ks(key);
174   auto it = u_entries.find(ks);
175   CHECK(it != u_entries.end());
176   return it->second;
177 }
178
179 bool ConfigReader::getBoolValue(const char *key) const
180 {
181   std::string ks(key);
182   auto it = u_entries.find(ks);
183   CHECK(it != u_entries.end());
184   return it->second != 0;
185 }
186
187 std::string ConfigReader::getStringValue(const char *key) const
188 {
189   std::string ks(key);
190   auto it = s_entries.find(ks);
191   CHECK(it != s_entries.end());
192   return it->second;
193 }
194
195 void ConfigReader::overrideUnsignedEntry(const char *key, unsigned new_value)
196 {
197   std::string ks(key);
198   auto it = u_entries.find(ks);
199   CHECK(it != u_entries.end());
200   values vals;
201   auto iit = u_info.find(key);
202   CHECK(iit != u_info.end());
203   vals = iit->second;
204   CHECK(new_value >= vals.minv && new_value <= vals.maxv);
205   it->second = new_value;
206   LOG(INFO) << "option " << key << " overridden to " << new_value;
207 }
208
209
210 //
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.
214 //
215 bool ConfigReader::parseLine(const char *key,
216                              const char *value,
217                              unsigned linecount)
218 {
219   assert(key);
220   assert(value);
221
222   auto uit = u_entries.find(key);
223   if (uit != u_entries.end()) {
224     unsigned uvalue = 0;
225     if (isdigit(value[0]) == 0 || sscanf(value, "%u", &uvalue) != 1) {
226       LOG(WARNING) << "line " << linecount << ": malformed unsigned value (ignored)";
227       return false;
228     } else {
229       values vals;
230       auto iit = u_info.find(key);
231       assert(iit != u_info.end());
232       vals = iit->second;
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
237                      << "] (ignored)";
238         return false;
239       } else {
240         if (trace_config_read) {
241           LOG(INFO) << "option " << key << " set to " << uvalue;
242         }
243         uit->second = uvalue;
244       }
245     }
246     trace_config_read = (getUnsignedValue("trace_config_read") != 0);
247     return true;
248   }
249
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;
254     }
255     sit->second = std::string(value);
256     return true;
257   }
258
259   LOG(WARNING) << "line " << linecount << ": unknown option '" << key << "' ignored";
260   return false;
261 }
262
263 static bool isblank(const std::string &line)
264 {
265   auto non_space = [](char c) { return isspace(c) == 0; };
266   return std::find_if(line.begin(), line.end(), non_space) == line.end();
267 }
268
269
270
271 bool ConfigReader::readFile()
272 {
273   std::string contents;
274   if (! android::base::ReadFileToString(config_file_path, &contents)) {
275     return false;
276   }
277   return Read(contents, /* fail_on_error */ false);
278 }
279
280 bool ConfigReader::Read(const std::string& content, bool fail_on_error) {
281   std::stringstream ss(content);
282   std::string line;
283   for (unsigned linecount = 1;
284        std::getline(ss,line,'\n');
285        linecount += 1)
286   {
287
288     // comment line?
289     if (line[0] == '#') {
290       continue;
291     }
292
293     // blank line?
294     if (isblank(line.c_str())) {
295       continue;
296     }
297
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)";
302       if (fail_on_error) {
303         return false;
304       }
305       continue;
306     }
307
308     std::string key(line.substr(0, efound));
309     std::string value(line.substr(efound+1, std::string::npos));
310
311     bool parse_success = parseLine(key.c_str(), value.c_str(), linecount);
312     if (fail_on_error && !parse_success) {
313       return false;
314     }
315   }
316
317   return true;
318 }
319
320 void ConfigReader::FillConfig(Config* config) {
321   config->collection_interval_in_s = getUnsignedValue("collection_interval");
322
323   config->use_fixed_seed = getUnsignedValue("use_fixed_seed");
324
325   config->main_loop_iterations = getUnsignedValue("main_loop_iterations");
326
327   config->destination_directory = getStringValue("destination_directory");
328
329   config->config_directory = getStringValue("config_directory");
330
331   config->perf_path = getStringValue("perf_path");
332
333   config->sampling_period = getUnsignedValue("sampling_period");
334
335   config->sample_duration_in_s = getUnsignedValue("sample_duration");
336
337   config->only_debug_build = getBoolValue("only_debug_build");
338
339   config->hardwire_cpus = getBoolValue("hardwire_cpus");
340   config->hardwire_cpus_max_duration_in_s = getUnsignedValue("hardwire_cpus_max_duration");
341
342   config->max_unprocessed_profiles = getUnsignedValue("max_unprocessed_profiles");
343
344   config->stack_profile = getBoolValue("stack_profile");
345
346   config->trace_config_read = getBoolValue("trace_config_read");
347
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");
352
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");
357 }