Index: chrome/browser/chromeos/power/cpu_data_collector.cc |
diff --git a/chrome/browser/chromeos/power/cpu_data_collector.cc b/chrome/browser/chromeos/power/cpu_data_collector.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..196f555301d74cd5bf13203788c0d0ea0a2e3e4e |
--- /dev/null |
+++ b/chrome/browser/chromeos/power/cpu_data_collector.cc |
@@ -0,0 +1,335 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include <vector> |
+ |
+#include "base/bind.h" |
+#include "base/file_util.h" |
+#include "base/logging.h" |
+#include "base/strings/string_number_conversions.h" |
+#include "base/strings/string_split.h" |
+#include "base/strings/string_util.h" |
+#include "base/strings/stringprintf.h" |
+#include "chrome/browser/chromeos/power/cpu_data_collector.h" |
+#include "chrome/browser/chromeos/power/power_data_collector.h" |
+#include "content/public/browser/browser_thread.h" |
+ |
+namespace chromeos { |
+ |
+namespace { |
+// The CPU data is sampled every |kCpuDataSamplePeriodSec| seconds. |
+const int kCpuDataSamplePeriodSec = 30; |
+ |
+// The value in the file /sys/devices/system/cpu/cpu<n>/online which indicates |
+// that CPU-n is online. |
+const int kCpuOnlineStatus = 1; |
+ |
+// The base of the path to the files and directories which contain CPU data in |
+// the sysfs. |
+const char kCpuDataPathBase[] = "/sys/devices/system/cpu"; |
+ |
+// Suffix of the path to the file listing the range of possible CPUs on the |
+// system. |
+const char kPossibleCpuPathSuffix[] = "/possible"; |
+ |
+// Format of the path to the file which contains information about a particular |
Daniel Erat
2014/02/21 02:29:37
nit: update all of these comments to clarify that
Siva Chandra
2014/03/05 21:20:27
Done.
|
+// CPU being online or offline. |
+const char kCpuOnlinePathSuffixFormat[] = "/cpu%d/online"; |
+ |
+// Format of the path to the file which contains freq state information of a |
+// CPU. |
+const char kCpuFreqTimeInStatePathSuffixFormat[] = |
+ "/cpu%d/cpufreq/stats/time_in_state"; |
+ |
+// Format of the path to the directory which contains information about an |
+// idle state of a CPU on the system. |
+const char kCpuIdleStateDirPathSuffixFormat[] = "/cpu%d/cpuidle/state%d"; |
+ |
+// Format of the path to the file which contains the name of an idle state |
+// of a CPU. |
+const char kCpuIdleStateNamePathSuffixFormat[] = "/cpu%d/cpuidle/state%d/name"; |
+ |
+// Format of the path which contains information about time spent in an idle |
+// state on a CPU. |
+const char kCpuIdleStateTimePathSuffixFormat[] = "/cpu%d/cpuidle/state%d/time"; |
+ |
+// Returns true if the |i|-th CPU is online; false otherwise. |
+bool CpuIsOnline(const int i) { |
+ std::string online_file_format = base::StringPrintf( |
Daniel Erat
2014/02/21 02:29:37
nit: probably best to make these const
Siva Chandra
2014/03/05 21:20:27
Done.
|
+ "%s%s", kCpuDataPathBase, kCpuOnlinePathSuffixFormat); |
+ std::string cpu_online_file = base::StringPrintf( |
+ online_file_format.c_str(), i); |
+ if (!base::PathExists(base::FilePath(cpu_online_file))) { |
+ // If the 'online' status file is missing, then it means that the CPU is |
+ // not hot-pluggable and hence is always online. |
+ return true; |
+ } |
+ |
+ int online; |
+ std::string cpu_online_string; |
+ if (base::ReadFileToString(base::FilePath(cpu_online_file), |
+ &cpu_online_string)) { |
+ base::TrimString(cpu_online_string, " \n", &cpu_online_string); |
+ if (base::StringToInt(cpu_online_string, &online)) |
+ return online == kCpuOnlineStatus; |
+ } |
+ |
+ LOG(ERROR) << "Bad format or error reading " << cpu_online_file << ". " |
+ << "Assuming offline."; |
+ return false; |
+} |
+ |
+// Samples the CPU idle state information from sysfs. |cpu_count| is the number |
+// of possible CPUs on the system. Sample at index i in |idle_samples| |
+// corresponds to the idle state information of the i-th CPU. |
+void SampleCpuIdleData( |
+ int cpu_count, |
+ std::vector<CpuDataCollector::StateOccupancySample>* idle_samples) { |
+ base::Time start_time = base::Time::Now(); |
+ for (int i = 0; i < cpu_count; ++i) { |
Daniel Erat
2014/02/21 02:29:37
nit: s/i/cpu_index/ ?
Siva Chandra
2014/03/05 21:20:27
Done.
|
+ CpuDataCollector::StateOccupancySample idle_sample; |
+ idle_sample.time = base::Time::Now(); |
+ |
+ if (!CpuIsOnline(i)) { |
+ idle_sample.cpu_online = false; |
+ } else { |
+ idle_sample.cpu_online = true; |
+ |
+ int state_count = 0; |
+ std::string idle_state_dir_format = base::StringPrintf( |
+ "%s%s", kCpuDataPathBase, kCpuIdleStateDirPathSuffixFormat); |
+ std::string idle_state_dir = base::StringPrintf( |
+ idle_state_dir_format.c_str(), i, state_count); |
+ while (base::DirectoryExists(base::FilePath(idle_state_dir))) { |
+ std::string name_file_format = base::StringPrintf( |
+ "%s%s", kCpuDataPathBase, kCpuIdleStateNamePathSuffixFormat); |
+ std::string name_file_path = base::StringPrintf( |
+ name_file_format.c_str(), i, state_count); |
+ DCHECK(base::PathExists(base::FilePath(name_file_path))); |
+ |
+ std::string time_file_format = base::StringPrintf( |
+ "%s%s", kCpuDataPathBase, kCpuIdleStateTimePathSuffixFormat); |
+ std::string time_file_path = base::StringPrintf( |
+ time_file_format.c_str(), i, state_count); |
+ DCHECK(base::PathExists(base::FilePath(time_file_path))); |
+ |
+ std::string state_name, occupancy_time_string; |
+ int64 occupancy_time; |
+ if (base::ReadFileToString(base::FilePath(name_file_path), |
+ &state_name) && |
+ base::ReadFileToString(base::FilePath(time_file_path), |
+ &occupancy_time_string)) { |
Daniel Erat
2014/02/21 02:29:37
nit: invert this to !base::ReadFileToString() || !
Siva Chandra
2014/03/05 21:20:27
Done.
|
+ base::TrimString(state_name, " \n", &state_name); |
+ base::TrimString( |
+ occupancy_time_string, " \n", &occupancy_time_string); |
+ if (base::StringToInt64(occupancy_time_string, &occupancy_time)) { |
+ // idle state occupancy time in sysfs is recorded in microseconds. |
+ idle_sample.state_occupancy[state_name] = occupancy_time/1000; |
+ } else { |
+ LOG(ERROR) << "Bad format in " << time_file_path << ". " |
+ << "Dropping sample."; |
+ idle_samples->clear(); |
+ return; |
+ } |
+ } else { |
+ // If an error occurs reading/parsing single state data, drop all the |
+ // samples as an incomplete sample can mislead consumers of this |
+ // sample. |
+ LOG(ERROR) << "Error reading idle state from " |
+ << idle_state_dir << ". Dropping sample."; |
+ idle_samples->clear(); |
+ return; |
+ } |
+ |
+ ++state_count; |
+ idle_state_dir = base::StringPrintf( |
+ idle_state_dir_format.c_str(), i, state_count); |
Daniel Erat
2014/02/21 02:29:37
i think you can avoid duplicating the code to gene
Siva Chandra
2014/03/05 21:20:27
Done.
|
+ } |
+ } |
+ |
+ idle_samples->push_back(idle_sample); |
+ } |
+ |
+ // If there was an interruption in sampling (like system suspended), |
+ // discard the samples! |
+ if (base::TimeDelta(base::Time::Now() - start_time).InMilliseconds() > 1000) { |
+ idle_samples->clear(); |
+ LOG(WARNING) << "Dropped an idle state sample due to excessive time delay."; |
Daniel Erat
2014/02/21 02:29:37
nit: include the delay in the warning
Siva Chandra
2014/03/05 21:20:27
Done.
|
+ } |
+} |
+ |
+// Samples the CPU freq state information from sysfs. |cpu_count| is the number |
+// of possible CPUs on the system. Sample at index i in |freq_samples| |
+// corresponds to the freq state information of the i-th CPU. |
+void SampleCpuFreqData( |
+ int cpu_count, |
+ std::vector<CpuDataCollector::StateOccupancySample>* freq_samples) { |
+ base::Time startTime = base::Time::Now(); |
+ for (int i = 0; i < cpu_count; ++i) { |
Daniel Erat
2014/02/21 02:29:37
nit: s/i/cpu_index/
Siva Chandra
2014/03/05 21:20:27
Done.
|
+ CpuDataCollector::StateOccupancySample freq_sample; |
+ |
+ if (!CpuIsOnline(i)) { |
+ freq_sample.time = base::Time::Now(); |
+ freq_sample.cpu_online = false; |
+ } else { |
+ freq_sample.cpu_online = true; |
+ |
+ std::string time_in_state_path_format = base::StringPrintf( |
+ "%s%s", kCpuDataPathBase, kCpuFreqTimeInStatePathSuffixFormat); |
+ std::string time_in_state_path = base::StringPrintf( |
+ time_in_state_path_format.c_str(), i); |
+ DCHECK(base::PathExists(base::FilePath(time_in_state_path))); |
+ |
+ std::string time_in_state_string; |
+ // Note time as close to reading the file as possible. This is not |
+ // possible for idle state samples as the information for each state there |
+ // is recorded in different files. |
+ base::Time now = base::Time::Now(); |
+ if (!base::ReadFileToString(base::FilePath(time_in_state_path), |
+ &time_in_state_string)) { |
+ LOG(ERROR) << "Error reading " << time_in_state_path << ". " |
+ << "Dropping sample."; |
+ freq_samples->clear(); |
+ return; |
+ } |
+ |
+ freq_sample.time = now; |
+ |
+ std::vector<std::string> lines; |
+ base::SplitString(time_in_state_string, '\n', &lines); |
+ // The last line could end with '\n'. Ignore the last empty string in |
+ // such cases. |
+ size_t state_count = lines.size(); |
+ if (state_count > 0 && lines.back().empty()) |
+ state_count -= 1; |
+ for (size_t k = 0; k < state_count; ++k) { |
Daniel Erat
2014/02/21 02:29:37
nit: s/k/state_index/
Siva Chandra
2014/03/05 21:20:27
Done.
|
+ std::vector<std::string> pair; |
+ int freq_in_khz; |
+ int64 occupancy_time; |
+ |
+ // Occupancy of each state is in the format "<state> <time>" |
+ base::SplitString(lines[k], ' ', &pair); |
+ for (size_t s = 0; s < pair.size(); ++s) |
+ base::TrimString(pair[s], " \n", &pair[s]); |
+ if (pair.size() == 2 && |
+ base::StringToInt(pair[0], &freq_in_khz) && |
+ base::StringToInt64(pair[1], &occupancy_time)) { |
+ // Freq state occupancy time is recorded in tens of milliseconds. |
+ freq_sample.state_occupancy[base::IntToString(freq_in_khz / 1000)] = |
+ occupancy_time * 10; |
+ } else { |
+ LOG(ERROR) << "Bad format in " << time_in_state_path << ". " |
+ << "Dropping sample."; |
+ freq_samples->clear(); |
+ return; |
+ } |
+ } |
+ } |
+ |
+ freq_samples->push_back(freq_sample); |
+ } |
+ |
+ // If there was an interruption in sampling (like system suspended), |
+ // discard the samples! |
+ if (base::TimeDelta(base::Time::Now() - startTime).InMilliseconds() > 1000) { |
+ freq_samples->clear(); |
+ LOG(WARNING) << "Dropped a freq state sample due to excessive time delay."; |
+ } |
+} |
+ |
+} // namespace |
+ |
+CpuDataCollector::CpuDataCollector() : cpu_count_(1), weak_ptr_factory_(this) { |
+ std::string possible_cpu_path = base::StringPrintf( |
+ "%s%s", kCpuDataPathBase, kPossibleCpuPathSuffix); |
+ if (!base::PathExists(base::FilePath(possible_cpu_path))) { |
+ LOG(ERROR) << "File listing possible CPUs missing. " |
+ << "Defaulting CPU count to 1."; |
+ } else { |
+ std::string possible_string; |
+ if (base::ReadFileToString(base::FilePath(possible_cpu_path), |
+ &possible_string)) { |
+ int max_cpu; |
+ // The possible CPUs are listed in the format "0-N". Hence, N is present |
+ // in the substring starting at offset 2. |
+ base::TrimString(possible_string, " \n", &possible_string); |
+ if (possible_string.find("-") != std::string::npos && |
+ possible_string.length() > 2 && |
+ base::StringToInt(possible_string.substr(2), &max_cpu)) { |
+ cpu_count_ = max_cpu + 1; |
+ } else { |
+ LOG(ERROR) << "Unknown format in the file listing possible CPUs. " |
+ << "Defaulting CPU count to 1."; |
+ } |
+ } else { |
+ LOG(ERROR) << "Error reading the file listing possible CPUs. " |
+ << "Defaulting CPU count to 1."; |
+ } |
+ } |
+ |
+ // Initialize the deques in the data vectors. |
+ cpu_idle_state_data_.resize(cpu_count_); |
+ cpu_freq_state_data_.resize(cpu_count_); |
+} |
+ |
+void CpuDataCollector::Start() { |
+ timer_.Start(FROM_HERE, |
+ base::TimeDelta::FromSeconds(kCpuDataSamplePeriodSec), |
+ this, |
+ &CpuDataCollector::PostSampleCpuState); |
+} |
+ |
+void CpuDataCollector::PostSampleCpuState() { |
+ std::vector<StateOccupancySample>* idle_samples = |
+ new std::vector<StateOccupancySample>; |
+ std::vector<StateOccupancySample>* freq_samples = |
+ new std::vector<StateOccupancySample>; |
+ content::BrowserThread::PostBlockingPoolTaskAndReply( |
+ FROM_HERE, |
+ base::Bind(&CpuDataCollector::SampleCpuStateOnBlockingPool, |
+ weak_ptr_factory_.GetWeakPtr(), |
+ base::Unretained(idle_samples), |
+ base::Unretained(freq_samples)), |
+ base::Bind(&CpuDataCollector::SaveCpuStateSamplesOnUIThread, |
+ weak_ptr_factory_.GetWeakPtr(), |
+ base::Owned(idle_samples), |
+ base::Owned(freq_samples))); |
+} |
+ |
+void CpuDataCollector::SampleCpuStateOnBlockingPool( |
+ std::vector<CpuDataCollector::StateOccupancySample>* idle_samples, |
+ std::vector<CpuDataCollector::StateOccupancySample>* freq_samples) { |
+ DCHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
+ |
+ SampleCpuIdleData(cpu_count_, idle_samples); |
+ SampleCpuFreqData(cpu_count_, freq_samples); |
+} |
+ |
+void CpuDataCollector::SaveCpuStateSamplesOnUIThread( |
+ const std::vector<CpuDataCollector::StateOccupancySample>* idle_samples, |
+ const std::vector<CpuDataCollector::StateOccupancySample>* freq_samples) { |
+ DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
+ |
+ // |idle_samples| or |freq_samples| could be empty sometimes (for example, if |
+ // sampling was interrupted due to system suspension). Iff they are not empty, |
+ // they will have one sample each for each of the CPUs. |
+ |
+ if (idle_samples->size() > 0) { |
+ DCHECK_EQ(idle_samples->size(), cpu_idle_state_data_.size()); |
+ for (size_t i = 0; i < cpu_idle_state_data_.size(); ++i) |
+ AddSample(&cpu_idle_state_data_[i], (*idle_samples)[i]); |
+ } |
+ |
+ if (freq_samples->size() > 0) { |
+ DCHECK_EQ(freq_samples->size(), cpu_freq_state_data_.size()); |
+ for (size_t i = 0; i < cpu_freq_state_data_.size(); ++i) |
+ AddSample(&cpu_freq_state_data_[i], (*freq_samples)[i]); |
+ } |
+} |
+ |
+CpuDataCollector::StateOccupancySample::StateOccupancySample() |
+ : cpu_online(false) { |
+} |
+ |
+} // namespace chromeos |