This source file includes following definitions.
- IndexInVector
- CpuIsOnline
- SampleCpuIdleData
- SampleCpuFreqData
- SampleCpuStateOnBlockingPool
- weak_ptr_factory_
- Start
- PostSampleCpuState
- SaveCpuStateSamplesOnUIThread
#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 {
const int kSamplingDurationLimitMs = 500;
const int kCpuDataSamplePeriodSec = 30;
const int kCpuOnlineStatus = 1;
const char kCpuDataPathBase[] = "/sys/devices/system/cpu";
const char kPossibleCpuPathSuffix[] = "/possible";
const char kCpuOnlinePathSuffixFormat[] = "/cpu%d/online";
const char kCpuFreqTimeInStatePathSuffixFormat[] =
"/cpu%d/cpufreq/stats/time_in_state";
const char kCpuIdleStateDirPathSuffixFormat[] = "/cpu%d/cpuidle/state%d";
const char kCpuIdleStateNamePathSuffixFormat[] = "/cpu%d/cpuidle/state%d/name";
const char kCpuIdleStateTimePathSuffixFormat[] = "/cpu%d/cpuidle/state%d/time";
size_t IndexInVector(const std::string& str,
std::vector<std::string>* vector) {
for (size_t i = 0; i < vector->size(); ++i) {
if (str == (*vector)[i])
return i;
}
vector->push_back(str);
return vector->size() - 1;
}
bool CpuIsOnline(const int i) {
const std::string online_file_format = base::StringPrintf(
"%s%s", kCpuDataPathBase, kCpuOnlinePathSuffixFormat);
const std::string cpu_online_file = base::StringPrintf(
online_file_format.c_str(), i);
if (!base::PathExists(base::FilePath(cpu_online_file))) {
return true;
}
int online;
std::string cpu_online_string;
if (base::ReadFileToString(base::FilePath(cpu_online_file),
&cpu_online_string)) {
base::TrimWhitespace(cpu_online_string, base::TRIM_ALL, &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;
}
void SampleCpuIdleData(
int cpu_count,
std::vector<std::string>* cpu_idle_state_names,
std::vector<CpuDataCollector::StateOccupancySample>* idle_samples) {
base::Time start_time = base::Time::Now();
for (int cpu = 0; cpu < cpu_count; ++cpu) {
CpuDataCollector::StateOccupancySample idle_sample;
idle_sample.time = base::Time::Now();
idle_sample.time_in_state.reserve(cpu_idle_state_names->size());
if (!CpuIsOnline(cpu)) {
idle_sample.cpu_online = false;
} else {
idle_sample.cpu_online = true;
const std::string idle_state_dir_format = base::StringPrintf(
"%s%s", kCpuDataPathBase, kCpuIdleStateDirPathSuffixFormat);
for (int state_count = 0; ; ++state_count) {
std::string idle_state_dir = base::StringPrintf(
idle_state_dir_format.c_str(), cpu, state_count);
if (!base::DirectoryExists(base::FilePath(idle_state_dir)))
break;
const std::string name_file_format = base::StringPrintf(
"%s%s", kCpuDataPathBase, kCpuIdleStateNamePathSuffixFormat);
const std::string name_file_path = base::StringPrintf(
name_file_format.c_str(), cpu, state_count);
DCHECK(base::PathExists(base::FilePath(name_file_path)));
const std::string time_file_format = base::StringPrintf(
"%s%s", kCpuDataPathBase, kCpuIdleStateTimePathSuffixFormat);
const std::string time_file_path = base::StringPrintf(
time_file_format.c_str(), cpu, state_count);
DCHECK(base::PathExists(base::FilePath(time_file_path)));
std::string state_name, occupancy_time_string;
int64 occupancy_time_usec;
if (!base::ReadFileToString(base::FilePath(name_file_path),
&state_name) ||
!base::ReadFileToString(base::FilePath(time_file_path),
&occupancy_time_string)) {
LOG(ERROR) << "Error reading idle state from "
<< idle_state_dir << ". Dropping sample.";
idle_samples->clear();
return;
}
base::TrimWhitespace(state_name, base::TRIM_ALL, &state_name);
base::TrimWhitespace(
occupancy_time_string, base::TRIM_ALL, &occupancy_time_string);
if (base::StringToInt64(occupancy_time_string, &occupancy_time_usec)) {
int64 time_in_state_ms = occupancy_time_usec / 1000;
size_t index = IndexInVector(state_name, cpu_idle_state_names);
if (index >= idle_sample.time_in_state.size())
idle_sample.time_in_state.resize(index + 1);
idle_sample.time_in_state[index] = time_in_state_ms;
} else {
LOG(ERROR) << "Bad format in " << time_file_path << ". "
<< "Dropping sample.";
idle_samples->clear();
return;
}
}
}
idle_samples->push_back(idle_sample);
}
int64 delay =
base::TimeDelta(base::Time::Now() - start_time).InMilliseconds();
if (delay > kSamplingDurationLimitMs) {
idle_samples->clear();
LOG(WARNING) << "Dropped an idle state sample due to excessive time delay: "
<< delay << "milliseconds.";
}
}
void SampleCpuFreqData(
int cpu_count,
std::vector<std::string>* cpu_freq_state_names,
std::vector<CpuDataCollector::StateOccupancySample>* freq_samples) {
base::Time start_time = base::Time::Now();
for (int cpu = 0; cpu < cpu_count; ++cpu) {
CpuDataCollector::StateOccupancySample freq_sample;
freq_sample.time_in_state.reserve(cpu_freq_state_names->size());
if (!CpuIsOnline(cpu)) {
freq_sample.time = base::Time::Now();
freq_sample.cpu_online = false;
} else {
freq_sample.cpu_online = true;
const std::string time_in_state_path_format = base::StringPrintf(
"%s%s", kCpuDataPathBase, kCpuFreqTimeInStatePathSuffixFormat);
const std::string time_in_state_path = base::StringPrintf(
time_in_state_path_format.c_str(), cpu);
if (!base::PathExists(base::FilePath(time_in_state_path))) {
LOG(ERROR) << "CPU freq stats not available in sysfs.";
freq_samples->clear();
return;
}
std::string time_in_state_string;
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);
size_t state_count = lines.size();
if (state_count > 0 && lines.back().empty())
state_count -= 1;
for (size_t state = 0; state < state_count; ++state) {
std::vector<std::string> pair;
int freq_in_khz;
int64 occupancy_time_centisecond;
base::SplitString(lines[state], ' ', &pair);
for (size_t s = 0; s < pair.size(); ++s)
base::TrimWhitespace(pair[s], base::TRIM_ALL, &pair[s]);
if (pair.size() == 2 &&
base::StringToInt(pair[0], &freq_in_khz) &&
base::StringToInt64(pair[1], &occupancy_time_centisecond)) {
const std::string state_name = base::IntToString(freq_in_khz / 1000);
size_t index = IndexInVector(state_name, cpu_freq_state_names);
if (index >= freq_sample.time_in_state.size())
freq_sample.time_in_state.resize(index + 1);
freq_sample.time_in_state[index] = occupancy_time_centisecond * 10;
} else {
LOG(ERROR) << "Bad format in " << time_in_state_path << ". "
<< "Dropping sample.";
freq_samples->clear();
return;
}
}
}
freq_samples->push_back(freq_sample);
}
int64 delay =
base::TimeDelta(base::Time::Now() - start_time).InMilliseconds();
if (delay > kSamplingDurationLimitMs) {
freq_samples->clear();
LOG(WARNING) << "Dropped a freq state sample due to excessive time delay: "
<< delay << "milliseconds.";
}
}
void 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) {
DCHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
if (*cpu_count < 0) {
*cpu_count = 1;
const 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;
base::TrimWhitespace(possible_string, base::TRIM_ALL, &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.";
}
}
}
SampleCpuIdleData(*cpu_count, cpu_idle_state_names, idle_samples);
SampleCpuFreqData(*cpu_count, cpu_freq_state_names, freq_samples);
}
}
CpuDataCollector::CpuDataCollector() : cpu_count_(-1), weak_ptr_factory_(this) {
}
CpuDataCollector::~CpuDataCollector() {
}
void CpuDataCollector::Start() {
timer_.Start(FROM_HERE,
base::TimeDelta::FromSeconds(kCpuDataSamplePeriodSec),
this,
&CpuDataCollector::PostSampleCpuState);
}
void CpuDataCollector::PostSampleCpuState() {
int* cpu_count = new int(cpu_count_);
std::vector<std::string>* cpu_idle_state_names =
new std::vector<std::string>(cpu_idle_state_names_);
std::vector<StateOccupancySample>* idle_samples =
new std::vector<StateOccupancySample>;
std::vector<std::string>* cpu_freq_state_names =
new std::vector<std::string>(cpu_freq_state_names_);
std::vector<StateOccupancySample>* freq_samples =
new std::vector<StateOccupancySample>;
content::BrowserThread::PostBlockingPoolTaskAndReply(
FROM_HERE,
base::Bind(&SampleCpuStateOnBlockingPool,
base::Unretained(cpu_count),
base::Unretained(cpu_idle_state_names),
base::Unretained(idle_samples),
base::Unretained(cpu_freq_state_names),
base::Unretained(freq_samples)),
base::Bind(&CpuDataCollector::SaveCpuStateSamplesOnUIThread,
weak_ptr_factory_.GetWeakPtr(),
base::Owned(cpu_count),
base::Owned(cpu_idle_state_names),
base::Owned(idle_samples),
base::Owned(cpu_freq_state_names),
base::Owned(freq_samples)));
}
void CpuDataCollector::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) {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
cpu_count_ = *cpu_count;
if (!idle_samples->empty()) {
if (cpu_idle_state_data_.empty()) {
cpu_idle_state_data_.resize(idle_samples->size());
} else {
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]);
cpu_idle_state_names_ = *cpu_idle_state_names;
}
if (!freq_samples->empty()) {
if (cpu_freq_state_data_.empty()) {
cpu_freq_state_data_.resize(freq_samples->size());
} else {
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]);
cpu_freq_state_names_ = *cpu_freq_state_names;
}
}
CpuDataCollector::StateOccupancySample::StateOccupancySample()
: cpu_online(false) {
}
CpuDataCollector::StateOccupancySample::~StateOccupancySample() {
}
}