• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 kSamplingDurationLimitMs = 500;
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.
IndexInVector(const std::string & str,std::vector<std::string> * vector)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.
CpuIsOnline(const int i)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.
SampleCpuIdleData(int cpu_count,std::vector<std::string> * cpu_idle_state_names,std::vector<CpuDataCollector::StateOccupancySample> * idle_samples)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     idle_sample.time_in_state.reserve(cpu_idle_state_names->size());
113 
114     if (!CpuIsOnline(cpu)) {
115       idle_sample.cpu_online = false;
116     } else {
117       idle_sample.cpu_online = true;
118 
119       const std::string idle_state_dir_format = base::StringPrintf(
120           "%s%s", kCpuDataPathBase, kCpuIdleStateDirPathSuffixFormat);
121       for (int state_count = 0; ; ++state_count) {
122         std::string idle_state_dir = base::StringPrintf(
123             idle_state_dir_format.c_str(), cpu, state_count);
124         // This insures us from the unlikely case wherein the 'cpuidle_stats'
125         // kernel module is not loaded. This could happen on a VM.
126         if (!base::DirectoryExists(base::FilePath(idle_state_dir)))
127           break;
128 
129         const std::string name_file_format = base::StringPrintf(
130             "%s%s", kCpuDataPathBase, kCpuIdleStateNamePathSuffixFormat);
131         const std::string name_file_path = base::StringPrintf(
132             name_file_format.c_str(), cpu, state_count);
133         DCHECK(base::PathExists(base::FilePath(name_file_path)));
134 
135         const std::string time_file_format = base::StringPrintf(
136             "%s%s", kCpuDataPathBase, kCpuIdleStateTimePathSuffixFormat);
137         const std::string time_file_path = base::StringPrintf(
138             time_file_format.c_str(), cpu, state_count);
139         DCHECK(base::PathExists(base::FilePath(time_file_path)));
140 
141         std::string state_name, occupancy_time_string;
142         int64 occupancy_time_usec;
143         if (!base::ReadFileToString(base::FilePath(name_file_path),
144                                     &state_name) ||
145             !base::ReadFileToString(base::FilePath(time_file_path),
146                                     &occupancy_time_string)) {
147           // If an error occurs reading/parsing single state data, drop all the
148           // samples as an incomplete sample can mislead consumers of this
149           // sample.
150           LOG(ERROR) << "Error reading idle state from "
151                      << idle_state_dir << ". Dropping sample.";
152           idle_samples->clear();
153           return;
154         }
155 
156         base::TrimWhitespace(state_name, base::TRIM_ALL, &state_name);
157         base::TrimWhitespace(
158             occupancy_time_string, base::TRIM_ALL, &occupancy_time_string);
159         if (base::StringToInt64(occupancy_time_string, &occupancy_time_usec)) {
160           // idle state occupancy time in sysfs is recorded in microseconds.
161           int64 time_in_state_ms = occupancy_time_usec / 1000;
162           size_t index = IndexInVector(state_name, cpu_idle_state_names);
163           if (index >= idle_sample.time_in_state.size())
164             idle_sample.time_in_state.resize(index + 1);
165           idle_sample.time_in_state[index] = time_in_state_ms;
166         } else {
167           LOG(ERROR) << "Bad format in " << time_file_path << ". "
168                      << "Dropping sample.";
169           idle_samples->clear();
170           return;
171         }
172       }
173     }
174 
175     idle_samples->push_back(idle_sample);
176   }
177 
178   // If there was an interruption in sampling (like system suspended),
179   // discard the samples!
180   int64 delay =
181       base::TimeDelta(base::Time::Now() - start_time).InMilliseconds();
182   if (delay > kSamplingDurationLimitMs) {
183     idle_samples->clear();
184     LOG(WARNING) << "Dropped an idle state sample due to excessive time delay: "
185                  << delay << "milliseconds.";
186   }
187 }
188 
189 // Samples the CPU freq state information from sysfs. |cpu_count| is the number
190 // of possible CPUs on the system. Sample at index i in |freq_samples|
191 // corresponds to the freq state information of the i-th CPU.
SampleCpuFreqData(int cpu_count,std::vector<std::string> * cpu_freq_state_names,std::vector<CpuDataCollector::StateOccupancySample> * freq_samples)192 void SampleCpuFreqData(
193     int cpu_count,
194     std::vector<std::string>* cpu_freq_state_names,
195     std::vector<CpuDataCollector::StateOccupancySample>* freq_samples) {
196   base::Time start_time = base::Time::Now();
197   for (int cpu = 0; cpu < cpu_count; ++cpu) {
198     CpuDataCollector::StateOccupancySample freq_sample;
199     freq_sample.time_in_state.reserve(cpu_freq_state_names->size());
200 
201     if (!CpuIsOnline(cpu)) {
202       freq_sample.time = base::Time::Now();
203       freq_sample.cpu_online = false;
204     } else {
205       freq_sample.cpu_online = true;
206 
207       const std::string time_in_state_path_format = base::StringPrintf(
208           "%s%s", kCpuDataPathBase, kCpuFreqTimeInStatePathSuffixFormat);
209       const std::string time_in_state_path = base::StringPrintf(
210           time_in_state_path_format.c_str(), cpu);
211       if (!base::PathExists(base::FilePath(time_in_state_path))) {
212         // If the path to the 'time_in_state' for a single CPU is missing,
213         // then 'time_in_state' for all CPUs is missing. This could happen
214         // on a VM where the 'cpufreq_stats' kernel module is not loaded.
215         LOG(ERROR) << "CPU freq stats not available in sysfs.";
216         freq_samples->clear();
217         return;
218       }
219 
220       std::string time_in_state_string;
221       // Note time as close to reading the file as possible. This is not
222       // possible for idle state samples as the information for each state there
223       // is recorded in different files.
224       base::Time now = base::Time::Now();
225       if (!base::ReadFileToString(base::FilePath(time_in_state_path),
226                                   &time_in_state_string)) {
227         LOG(ERROR) << "Error reading " << time_in_state_path << ". "
228                    << "Dropping sample.";
229         freq_samples->clear();
230         return;
231       }
232 
233       freq_sample.time = now;
234 
235       std::vector<std::string> lines;
236       base::SplitString(time_in_state_string, '\n', &lines);
237       // The last line could end with '\n'. Ignore the last empty string in
238       // such cases.
239       size_t state_count = lines.size();
240       if (state_count > 0 && lines.back().empty())
241         state_count -= 1;
242       for (size_t state = 0; state < state_count; ++state) {
243         std::vector<std::string> pair;
244         int freq_in_khz;
245         int64 occupancy_time_centisecond;
246 
247         // Occupancy of each state is in the format "<state> <time>"
248         base::SplitString(lines[state], ' ', &pair);
249         for (size_t s = 0; s < pair.size(); ++s)
250           base::TrimWhitespace(pair[s], base::TRIM_ALL, &pair[s]);
251         if (pair.size() == 2 &&
252             base::StringToInt(pair[0], &freq_in_khz) &&
253             base::StringToInt64(pair[1], &occupancy_time_centisecond)) {
254           const std::string state_name = base::IntToString(freq_in_khz / 1000);
255           size_t index = IndexInVector(state_name, cpu_freq_state_names);
256           if (index >= freq_sample.time_in_state.size())
257             freq_sample.time_in_state.resize(index + 1);
258           // The occupancy time is in units of centiseconds.
259           freq_sample.time_in_state[index] = occupancy_time_centisecond * 10;
260         } else {
261           LOG(ERROR) << "Bad format in " << time_in_state_path << ". "
262                      << "Dropping sample.";
263           freq_samples->clear();
264           return;
265         }
266       }
267     }
268 
269     freq_samples->push_back(freq_sample);
270   }
271 
272   // If there was an interruption in sampling (like system suspended),
273   // discard the samples!
274   int64 delay =
275       base::TimeDelta(base::Time::Now() - start_time).InMilliseconds();
276   if (delay > kSamplingDurationLimitMs) {
277     freq_samples->clear();
278     LOG(WARNING) << "Dropped a freq state sample due to excessive time delay: "
279                  << delay << "milliseconds.";
280   }
281 }
282 
283 // Samples CPU idle and CPU freq data from sysfs. This function should run on
284 // the blocking pool as reading from sysfs is a blocking task. Elements at
285 // index i in |idle_samples| and |freq_samples| correspond to the idle and
286 // freq samples of CPU i. This also function reads the number of CPUs from
287 // sysfs if *|cpu_count| < 0.
SampleCpuStateOnBlockingPool(int * cpu_count,std::vector<std::string> * cpu_idle_state_names,std::vector<CpuDataCollector::StateOccupancySample> * idle_samples,std::vector<std::string> * cpu_freq_state_names,std::vector<CpuDataCollector::StateOccupancySample> * freq_samples)288 void SampleCpuStateOnBlockingPool(
289     int* cpu_count,
290     std::vector<std::string>* cpu_idle_state_names,
291     std::vector<CpuDataCollector::StateOccupancySample>* idle_samples,
292     std::vector<std::string>* cpu_freq_state_names,
293     std::vector<CpuDataCollector::StateOccupancySample>* freq_samples) {
294   DCHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
295 
296   if (*cpu_count < 0) {
297     // Set |cpu_count_| to 1. If it is something else, it will get corrected
298     // later. A system will at least have one CPU. Hence, a value of 1 here
299     // will serve as a default value in case of errors.
300     *cpu_count = 1;
301     const std::string possible_cpu_path = base::StringPrintf(
302         "%s%s", kCpuDataPathBase, kPossibleCpuPathSuffix);
303     if (!base::PathExists(base::FilePath(possible_cpu_path))) {
304       LOG(ERROR) << "File listing possible CPUs missing. "
305                  << "Defaulting CPU count to 1.";
306     } else {
307       std::string possible_string;
308       if (base::ReadFileToString(base::FilePath(possible_cpu_path),
309                                  &possible_string)) {
310         int max_cpu;
311         // The possible CPUs are listed in the format "0-N". Hence, N is present
312         // in the substring starting at offset 2.
313         base::TrimWhitespace(possible_string, base::TRIM_ALL, &possible_string);
314         if (possible_string.find("-") != std::string::npos &&
315             possible_string.length() > 2 &&
316             base::StringToInt(possible_string.substr(2), &max_cpu)) {
317           *cpu_count = max_cpu + 1;
318         } else {
319           LOG(ERROR) << "Unknown format in the file listing possible CPUs. "
320                      << "Defaulting CPU count to 1.";
321         }
322       } else {
323         LOG(ERROR) << "Error reading the file listing possible CPUs. "
324                    << "Defaulting CPU count to 1.";
325       }
326     }
327   }
328 
329   // Initialize the deques in the data vectors.
330   SampleCpuIdleData(*cpu_count, cpu_idle_state_names, idle_samples);
331   SampleCpuFreqData(*cpu_count, cpu_freq_state_names, freq_samples);
332 }
333 
334 }  // namespace
335 
336 // Set |cpu_count_| to -1 and let SampleCpuStateOnBlockingPool discover the
337 // correct number of CPUs.
CpuDataCollector()338 CpuDataCollector::CpuDataCollector() : cpu_count_(-1), weak_ptr_factory_(this) {
339 }
340 
~CpuDataCollector()341 CpuDataCollector::~CpuDataCollector() {
342 }
343 
Start()344 void CpuDataCollector::Start() {
345   timer_.Start(FROM_HERE,
346                base::TimeDelta::FromSeconds(kCpuDataSamplePeriodSec),
347                this,
348                &CpuDataCollector::PostSampleCpuState);
349 }
350 
PostSampleCpuState()351 void CpuDataCollector::PostSampleCpuState() {
352   int* cpu_count = new int(cpu_count_);
353   std::vector<std::string>* cpu_idle_state_names =
354       new std::vector<std::string>(cpu_idle_state_names_);
355   std::vector<StateOccupancySample>* idle_samples =
356       new std::vector<StateOccupancySample>;
357   std::vector<std::string>* cpu_freq_state_names =
358       new std::vector<std::string>(cpu_freq_state_names_);
359   std::vector<StateOccupancySample>* freq_samples =
360       new std::vector<StateOccupancySample>;
361 
362   content::BrowserThread::PostBlockingPoolTaskAndReply(
363       FROM_HERE,
364       base::Bind(&SampleCpuStateOnBlockingPool,
365                  base::Unretained(cpu_count),
366                  base::Unretained(cpu_idle_state_names),
367                  base::Unretained(idle_samples),
368                  base::Unretained(cpu_freq_state_names),
369                  base::Unretained(freq_samples)),
370       base::Bind(&CpuDataCollector::SaveCpuStateSamplesOnUIThread,
371                  weak_ptr_factory_.GetWeakPtr(),
372                  base::Owned(cpu_count),
373                  base::Owned(cpu_idle_state_names),
374                  base::Owned(idle_samples),
375                  base::Owned(cpu_freq_state_names),
376                  base::Owned(freq_samples)));
377 }
378 
SaveCpuStateSamplesOnUIThread(const int * cpu_count,const std::vector<std::string> * cpu_idle_state_names,const std::vector<CpuDataCollector::StateOccupancySample> * idle_samples,const std::vector<std::string> * cpu_freq_state_names,const std::vector<CpuDataCollector::StateOccupancySample> * freq_samples)379 void CpuDataCollector::SaveCpuStateSamplesOnUIThread(
380     const int* cpu_count,
381     const std::vector<std::string>* cpu_idle_state_names,
382     const std::vector<CpuDataCollector::StateOccupancySample>* idle_samples,
383     const std::vector<std::string>* cpu_freq_state_names,
384     const std::vector<CpuDataCollector::StateOccupancySample>* freq_samples) {
385   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
386 
387   cpu_count_ = *cpu_count;
388 
389   // |idle_samples| or |freq_samples| could be empty sometimes (for example, if
390   // sampling was interrupted due to system suspension). Iff they are not empty,
391   // they will have one sample each for each of the CPUs.
392 
393   if (!idle_samples->empty()) {
394     // When committing the first sample, resize the data vector to the number of
395     // CPUs on the system. This number should be the same as the number of
396     // samples in |idle_samples|.
397     if (cpu_idle_state_data_.empty()) {
398       cpu_idle_state_data_.resize(idle_samples->size());
399     } else {
400       DCHECK_EQ(idle_samples->size(), cpu_idle_state_data_.size());
401     }
402     for (size_t i = 0; i < cpu_idle_state_data_.size(); ++i)
403       AddSample(&cpu_idle_state_data_[i], (*idle_samples)[i]);
404 
405     cpu_idle_state_names_ = *cpu_idle_state_names;
406   }
407 
408   if (!freq_samples->empty()) {
409     // As with idle samples, resize the data vector before committing the first
410     // sample.
411     if (cpu_freq_state_data_.empty()) {
412       cpu_freq_state_data_.resize(freq_samples->size());
413     } else {
414       DCHECK_EQ(freq_samples->size(), cpu_freq_state_data_.size());
415     }
416     for (size_t i = 0; i < cpu_freq_state_data_.size(); ++i)
417       AddSample(&cpu_freq_state_data_[i], (*freq_samples)[i]);
418 
419     cpu_freq_state_names_ = *cpu_freq_state_names;
420   }
421 }
422 
StateOccupancySample()423 CpuDataCollector::StateOccupancySample::StateOccupancySample()
424     : cpu_online(false) {
425 }
426 
~StateOccupancySample()427 CpuDataCollector::StateOccupancySample::~StateOccupancySample() {
428 }
429 
430 }  // namespace chromeos
431