Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(318)

Side by Side Diff: chrome/browser/chromeos/power/cpu_data_collector.cc

Issue 149973002: [chromeos/about:power] Collect cpuidle and cpufreq stats (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix build after rebase Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <vector>
6
7 #include "base/bind.h"
8 #include "base/file_util.h"
9 #include "base/logging.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/string_split.h"
12 #include "base/strings/string_util.h"
13 #include "base/strings/stringprintf.h"
14 #include "chrome/browser/chromeos/power/cpu_data_collector.h"
15 #include "chrome/browser/chromeos/power/power_data_collector.h"
16 #include "content/public/browser/browser_thread.h"
17
18 namespace chromeos {
19
20 namespace {
21 // The sampling of CPU idle or CPU freq data should not take more than this
22 // limit.
23 const int kSamplingDurationLimitMillisec = 500;
Daniel Erat 2014/03/07 00:49:18 nit: s/Millisec/Ms/ (abbreviating "milliseconds" j
Siva Chandra 2014/03/07 22:20:01 Done.
24
25 // The CPU data is sampled every |kCpuDataSamplePeriodSec| seconds.
26 const int kCpuDataSamplePeriodSec = 30;
27
28 // The value in the file /sys/devices/system/cpu/cpu<n>/online which indicates
29 // that CPU-n is online.
30 const int kCpuOnlineStatus = 1;
31
32 // The base of the path to the files and directories which contain CPU data in
33 // the sysfs.
34 const char kCpuDataPathBase[] = "/sys/devices/system/cpu";
35
36 // Suffix of the path to the file listing the range of possible CPUs on the
37 // system.
38 const char kPossibleCpuPathSuffix[] = "/possible";
39
40 // Format of the suffix of the path to the file which contains information
41 // about a particular CPU being online or offline.
42 const char kCpuOnlinePathSuffixFormat[] = "/cpu%d/online";
43
44 // Format of the suffix of the path to the file which contains freq state
45 // information of a CPU.
46 const char kCpuFreqTimeInStatePathSuffixFormat[] =
47 "/cpu%d/cpufreq/stats/time_in_state";
48
49 // Format of the suffix of the path to the directory which contains information
50 // about an idle state of a CPU on the system.
51 const char kCpuIdleStateDirPathSuffixFormat[] = "/cpu%d/cpuidle/state%d";
52
53 // Format of the suffix of the path to the file which contains the name of an
54 // idle state of a CPU.
55 const char kCpuIdleStateNamePathSuffixFormat[] = "/cpu%d/cpuidle/state%d/name";
56
57 // Format of the suffix of the path which contains information about time spent
58 // in an idle state on a CPU.
59 const char kCpuIdleStateTimePathSuffixFormat[] = "/cpu%d/cpuidle/state%d/time";
60
61 // Returns the index at which |str| is in |vector|. If |str| is not present in
62 // |vector|, then it is added to it before its index is returned.
63 size_t IndexInVector(const std::string& str,
64 std::vector<std::string>* vector) {
65 for (size_t i = 0; i < vector->size(); ++i) {
66 if (str == (*vector)[i])
67 return i;
68 }
69
70 // If this is reached, then it means |str| is not present in vector. Add it.
71 vector->push_back(str);
72 return vector->size() - 1;
73 }
74
75 // Returns true if the |i|-th CPU is online; false otherwise.
76 bool CpuIsOnline(const int i) {
77 const std::string online_file_format = base::StringPrintf(
78 "%s%s", kCpuDataPathBase, kCpuOnlinePathSuffixFormat);
79 const std::string cpu_online_file = base::StringPrintf(
80 online_file_format.c_str(), i);
81 if (!base::PathExists(base::FilePath(cpu_online_file))) {
82 // If the 'online' status file is missing, then it means that the CPU is
83 // not hot-pluggable and hence is always online.
84 return true;
85 }
86
87 int online;
88 std::string cpu_online_string;
89 if (base::ReadFileToString(base::FilePath(cpu_online_file),
90 &cpu_online_string)) {
91 base::TrimWhitespace(cpu_online_string, base::TRIM_ALL, &cpu_online_string);
92 if (base::StringToInt(cpu_online_string, &online))
93 return online == kCpuOnlineStatus;
94 }
95
96 LOG(ERROR) << "Bad format or error reading " << cpu_online_file << ". "
97 << "Assuming offline.";
98 return false;
99 }
100
101 // Samples the CPU idle state information from sysfs. |cpu_count| is the number
102 // of possible CPUs on the system. Sample at index i in |idle_samples|
103 // corresponds to the idle state information of the i-th CPU.
104 void SampleCpuIdleData(
105 int cpu_count,
106 std::vector<std::string>* cpu_idle_state_names,
107 std::vector<CpuDataCollector::StateOccupancySample>* idle_samples) {
108 base::Time start_time = base::Time::Now();
109 for (int cpu = 0; cpu < cpu_count; ++cpu) {
110 CpuDataCollector::StateOccupancySample idle_sample;
111 idle_sample.time = base::Time::Now();
112
113 if (!CpuIsOnline(cpu)) {
114 idle_sample.cpu_online = false;
115 } else {
116 idle_sample.cpu_online = true;
117
118 const std::string idle_state_dir_format = base::StringPrintf(
119 "%s%s", kCpuDataPathBase, kCpuIdleStateDirPathSuffixFormat);
120 for (int state_count = 0; ; ++state_count) {
121 std::string idle_state_dir = base::StringPrintf(
122 idle_state_dir_format.c_str(), cpu, state_count);
123 if (!base::DirectoryExists(base::FilePath(idle_state_dir)))
124 break;
125
126 const std::string name_file_format = base::StringPrintf(
127 "%s%s", kCpuDataPathBase, kCpuIdleStateNamePathSuffixFormat);
128 const std::string name_file_path = base::StringPrintf(
129 name_file_format.c_str(), cpu, state_count);
130 DCHECK(base::PathExists(base::FilePath(name_file_path)));
131
132 const std::string time_file_format = base::StringPrintf(
133 "%s%s", kCpuDataPathBase, kCpuIdleStateTimePathSuffixFormat);
134 const std::string time_file_path = base::StringPrintf(
135 time_file_format.c_str(), cpu, state_count);
136 DCHECK(base::PathExists(base::FilePath(time_file_path)));
137
138 std::string state_name, occupancy_time_string;
139 int64 occupancy_time;
Daniel Erat 2014/03/07 00:49:18 nit: rename to occupancy_time_usec
Siva Chandra 2014/03/07 22:20:01 Done.
140 if (!base::ReadFileToString(base::FilePath(name_file_path),
141 &state_name) ||
Daniel Erat 2014/03/07 00:49:18 nit: fix indenting (should be indented one more sp
Siva Chandra 2014/03/07 22:20:01 Done.
142 !base::ReadFileToString(base::FilePath(time_file_path),
143 &occupancy_time_string)) {
Daniel Erat 2014/03/07 00:49:18 nit: here too
Siva Chandra 2014/03/07 22:20:01 Done.
144 // If an error occurs reading/parsing single state data, drop all the
145 // samples as an incomplete sample can mislead consumers of this
146 // sample.
147 LOG(ERROR) << "Error reading idle state from "
148 << idle_state_dir << ". Dropping sample.";
149 idle_samples->clear();
150 return;
151 }
152
153 base::TrimWhitespace(state_name, base::TRIM_ALL, &state_name);
154 base::TrimWhitespace(
155 occupancy_time_string, base::TRIM_ALL, &occupancy_time_string);
156 if (base::StringToInt64(occupancy_time_string, &occupancy_time)) {
157 // idle state occupancy time in sysfs is recorded in microseconds.
158 int64 time_in_state = occupancy_time / 1000;
Daniel Erat 2014/03/07 00:49:18 nit: rename to time_in_state_ms
Siva Chandra 2014/03/07 22:20:01 Done.
159 size_t index = IndexInVector(state_name, cpu_idle_state_names);
160 if (index >= idle_sample.time_in_state.size())
161 idle_sample.time_in_state.resize(index + 1);
162 idle_sample.time_in_state[index] = time_in_state;
163 } else {
164 LOG(ERROR) << "Bad format in " << time_file_path << ". "
165 << "Dropping sample.";
166 idle_samples->clear();
167 return;
168 }
169 }
170 }
171
172 idle_samples->push_back(idle_sample);
173 }
174
175 // If there was an interruption in sampling (like system suspended),
176 // discard the samples!
177 int64 delay =
178 base::TimeDelta(base::Time::Now() - start_time).InMilliseconds();
179 if (delay > kSamplingDurationLimitMillisec) {
180 idle_samples->clear();
181 LOG(WARNING) << "Dropped an idle state sample due to excessive time delay: "
182 << delay << "milliseconds.";
183 }
184 }
185
186 // Samples the CPU freq state information from sysfs. |cpu_count| is the number
187 // of possible CPUs on the system. Sample at index i in |freq_samples|
188 // corresponds to the freq state information of the i-th CPU.
189 void SampleCpuFreqData(
190 int cpu_count,
191 std::vector<std::string>* cpu_freq_state_names,
192 std::vector<CpuDataCollector::StateOccupancySample>* freq_samples) {
193 base::Time start_time = base::Time::Now();
194 for (int cpu = 0; cpu < cpu_count; ++cpu) {
195 CpuDataCollector::StateOccupancySample freq_sample;
196
197 if (!CpuIsOnline(cpu)) {
198 freq_sample.time = base::Time::Now();
199 freq_sample.cpu_online = false;
200 } else {
201 freq_sample.cpu_online = true;
202
203 const std::string time_in_state_path_format = base::StringPrintf(
204 "%s%s", kCpuDataPathBase, kCpuFreqTimeInStatePathSuffixFormat);
205 const std::string time_in_state_path = base::StringPrintf(
206 time_in_state_path_format.c_str(), cpu);
207 DCHECK(base::PathExists(base::FilePath(time_in_state_path)));
208
209 std::string time_in_state_string;
210 // Note time as close to reading the file as possible. This is not
211 // possible for idle state samples as the information for each state there
212 // is recorded in different files.
213 base::Time now = base::Time::Now();
214 if (!base::ReadFileToString(base::FilePath(time_in_state_path),
215 &time_in_state_string)) {
216 LOG(ERROR) << "Error reading " << time_in_state_path << ". "
217 << "Dropping sample.";
218 freq_samples->clear();
219 return;
220 }
221
222 freq_sample.time = now;
223
224 std::vector<std::string> lines;
225 base::SplitString(time_in_state_string, '\n', &lines);
226 // The last line could end with '\n'. Ignore the last empty string in
227 // such cases.
228 size_t state_count = lines.size();
229 if (state_count > 0 && lines.back().empty())
230 state_count -= 1;
231 for (size_t state = 0; state < state_count; ++state) {
232 std::vector<std::string> pair;
233 int freq_in_khz;
234 int64 occupancy_time;
Daniel Erat 2014/03/07 00:49:18 nit: rename this to include the units
Siva Chandra 2014/03/07 22:20:01 Done.
235
236 // Occupancy of each state is in the format "<state> <time>"
237 base::SplitString(lines[state], ' ', &pair);
238 for (size_t s = 0; s < pair.size(); ++s)
239 base::TrimWhitespace(pair[s], base::TRIM_ALL, &pair[s]);
240 if (pair.size() == 2 &&
241 base::StringToInt(pair[0], &freq_in_khz) &&
242 base::StringToInt64(pair[1], &occupancy_time)) {
243 const std::string state_name = base::IntToString(freq_in_khz / 1000);
244 size_t index = IndexInVector(state_name, cpu_freq_state_names);
245 if (index >= freq_sample.time_in_state.size())
246 freq_sample.time_in_state.resize(index + 1);
247 freq_sample.time_in_state[index] = occupancy_time * 10;
Daniel Erat 2014/03/07 00:49:18 multiplying by 10 seems a bit strange -- is this c
Siva Chandra 2014/03/07 22:20:01 From Documentation/cpu-freq/cpufreq-stats.txt: "T
Daniel Erat 2014/03/07 22:28:34 thanks!
248 } else {
249 LOG(ERROR) << "Bad format in " << time_in_state_path << ". "
250 << "Dropping sample.";
251 freq_samples->clear();
252 return;
253 }
254 }
255 }
256
257 freq_samples->push_back(freq_sample);
258 }
259
260 // If there was an interruption in sampling (like system suspended),
261 // discard the samples!
262 int64 delay =
263 base::TimeDelta(base::Time::Now() - start_time).InMilliseconds();
264 if (delay > kSamplingDurationLimitMillisec) {
265 freq_samples->clear();
266 LOG(WARNING) << "Dropped a freq state sample due to excessive time delay: "
267 << delay << "milliseconds.";
268 }
269 }
270
271 } // namespace
272
273 // Set |cpu_count_| to -1 and let SampleCpuStateOnBlockingPool discover the
274 // correct number of CPUs.
275 CpuDataCollector::CpuDataCollector() : cpu_count_(-1), weak_ptr_factory_(this) {
276 }
277
278 void CpuDataCollector::Start() {
279 timer_.Start(FROM_HERE,
280 base::TimeDelta::FromSeconds(kCpuDataSamplePeriodSec),
281 this,
282 &CpuDataCollector::PostSampleCpuState);
283 }
284
285 void CpuDataCollector::PostSampleCpuState() {
286 std::vector<std::string>* cpu_idle_state_names = new std::vector<std::string>;
Daniel Erat 2014/03/07 00:49:18 these should probably initialized with the existin
Siva Chandra 2014/03/07 22:20:01 Done.
287 std::vector<StateOccupancySample>* idle_samples =
288 new std::vector<StateOccupancySample>;
289 std::vector<std::string>* cpu_freq_state_names = new std::vector<std::string>;
Daniel Erat 2014/03/07 00:49:18 same here
Siva Chandra 2014/03/07 22:20:01 Done.
290 std::vector<StateOccupancySample>* freq_samples =
291 new std::vector<StateOccupancySample>;
292 content::BrowserThread::PostBlockingPoolTaskAndReply(
293 FROM_HERE,
294 base::Bind(&CpuDataCollector::SampleCpuStateOnBlockingPool,
295 weak_ptr_factory_.GetWeakPtr(),
296 base::Unretained(cpu_idle_state_names),
297 base::Unretained(idle_samples),
298 base::Unretained(cpu_freq_state_names),
299 base::Unretained(freq_samples)),
300 base::Bind(&CpuDataCollector::SaveCpuStateSamplesOnUIThread,
301 weak_ptr_factory_.GetWeakPtr(),
302 base::Owned(cpu_idle_state_names),
303 base::Owned(idle_samples),
304 base::Owned(cpu_freq_state_names),
305 base::Owned(freq_samples)));
306 }
307
308 void CpuDataCollector::SampleCpuStateOnBlockingPool(
309 std::vector<std::string>* cpu_idle_state_names,
310 std::vector<CpuDataCollector::StateOccupancySample>* idle_samples,
311 std::vector<std::string>* cpu_freq_state_names,
312 std::vector<CpuDataCollector::StateOccupancySample>* freq_samples) {
313 DCHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
314
315 if (cpu_count_ < 0) {
316 // Set |cpu_count_| to 1. If it is something else, it will get corrected
317 // later. A system will atleast have one CPU. Hence, a value of 1 here
Daniel Erat 2014/03/07 00:49:18 nit: s/atleast/at least/
Siva Chandra 2014/03/07 22:20:01 Done.
318 // will serve as a default value in case of errors.
319 cpu_count_ = 1;
320 const std::string possible_cpu_path = base::StringPrintf(
321 "%s%s", kCpuDataPathBase, kPossibleCpuPathSuffix);
322 if (!base::PathExists(base::FilePath(possible_cpu_path))) {
323 LOG(ERROR) << "File listing possible CPUs missing. "
324 << "Defaulting CPU count to 1.";
325 } else {
326 std::string possible_string;
327 if (base::ReadFileToString(base::FilePath(possible_cpu_path),
328 &possible_string)) {
329 int max_cpu;
330 // The possible CPUs are listed in the format "0-N". Hence, N is present
331 // in the substring starting at offset 2.
332 base::TrimWhitespace(possible_string, base::TRIM_ALL, &possible_string);
333 if (possible_string.find("-") != std::string::npos &&
334 possible_string.length() > 2 &&
335 base::StringToInt(possible_string.substr(2), &max_cpu)) {
336 cpu_count_ = max_cpu + 1;
337 } else {
338 LOG(ERROR) << "Unknown format in the file listing possible CPUs. "
339 << "Defaulting CPU count to 1.";
340 }
341 } else {
342 LOG(ERROR) << "Error reading the file listing possible CPUs. "
343 << "Defaulting CPU count to 1.";
344 }
345 }
346 }
347
348 // Initialize the deques in the data vectors.
349 cpu_idle_state_data_.resize(cpu_count_);
Daniel Erat 2014/03/07 00:49:18 this seems wrong -- what happens if the ui thread
Siva Chandra 2014/03/07 22:20:01 I moved this to SaveCpuStateSamplesOnUIThread.
350 cpu_freq_state_data_.resize(cpu_count_);
351 SampleCpuIdleData(cpu_count_, cpu_idle_state_names, idle_samples);
352 SampleCpuFreqData(cpu_count_, cpu_freq_state_names, freq_samples);
353 }
354
355 void CpuDataCollector::SaveCpuStateSamplesOnUIThread(
356 const std::vector<std::string>* cpu_idle_state_names,
357 const std::vector<CpuDataCollector::StateOccupancySample>* idle_samples,
358 const std::vector<std::string>* cpu_freq_state_names,
359 const std::vector<CpuDataCollector::StateOccupancySample>* freq_samples) {
360 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
361
362 // |idle_samples| or |freq_samples| could be empty sometimes (for example, if
363 // sampling was interrupted due to system suspension). Iff they are not empty,
364 // they will have one sample each for each of the CPUs.
365
366 if (idle_samples->size() > 0) {
Daniel Erat 2014/03/07 00:49:18 nit: !idle_samples->empty()
Siva Chandra 2014/03/07 22:20:01 Done.
367 DCHECK_EQ(idle_samples->size(), cpu_idle_state_data_.size());
368 for (size_t i = 0; i < cpu_idle_state_data_.size(); ++i)
369 AddSample(&cpu_idle_state_data_[i], (*idle_samples)[i]);
370
371 cpu_idle_state_names_ = *cpu_idle_state_names;
372 }
373
374 if (freq_samples->size() > 0) {
Daniel Erat 2014/03/07 00:49:18 nit: !freq_samples->empty()
Siva Chandra 2014/03/07 22:20:01 Done.
375 DCHECK_EQ(freq_samples->size(), cpu_freq_state_data_.size());
376 for (size_t i = 0; i < cpu_freq_state_data_.size(); ++i)
377 AddSample(&cpu_freq_state_data_[i], (*freq_samples)[i]);
378
379 cpu_freq_state_names_ = *cpu_freq_state_names;
380 }
381 }
382
383 CpuDataCollector::StateOccupancySample::StateOccupancySample()
384 : cpu_online(false) {
385 }
386
387 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698