Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 CPU data is sampled every |kCpuDataSamplePeriodSec| seconds. | |
| 22 const int kCpuDataSamplePeriodSec = 30; | |
| 23 | |
| 24 // The value in the file /sys/devices/system/cpu/cpu<n>/online which indicates | |
| 25 // that CPU-n is online. | |
| 26 const int kCpuOnlineStatus = 1; | |
| 27 | |
| 28 // The base of the path to the files and directories which contain CPU data in | |
| 29 // the sysfs. | |
| 30 const char kCpuDataPathBase[] = "/sys/devices/system/cpu"; | |
| 31 | |
| 32 // Suffix of the path to the file listing the range of possible CPUs on the | |
| 33 // system. | |
| 34 const char kPossibleCpuPathSuffix[] = "/possible"; | |
| 35 | |
| 36 // 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.
| |
| 37 // CPU being online or offline. | |
| 38 const char kCpuOnlinePathSuffixFormat[] = "/cpu%d/online"; | |
| 39 | |
| 40 // Format of the path to the file which contains freq state information of a | |
| 41 // CPU. | |
| 42 const char kCpuFreqTimeInStatePathSuffixFormat[] = | |
| 43 "/cpu%d/cpufreq/stats/time_in_state"; | |
| 44 | |
| 45 // Format of the path to the directory which contains information about an | |
| 46 // idle state of a CPU on the system. | |
| 47 const char kCpuIdleStateDirPathSuffixFormat[] = "/cpu%d/cpuidle/state%d"; | |
| 48 | |
| 49 // Format of the path to the file which contains the name of an idle state | |
| 50 // of a CPU. | |
| 51 const char kCpuIdleStateNamePathSuffixFormat[] = "/cpu%d/cpuidle/state%d/name"; | |
| 52 | |
| 53 // Format of the path which contains information about time spent in an idle | |
| 54 // state on a CPU. | |
| 55 const char kCpuIdleStateTimePathSuffixFormat[] = "/cpu%d/cpuidle/state%d/time"; | |
| 56 | |
| 57 // Returns true if the |i|-th CPU is online; false otherwise. | |
| 58 bool CpuIsOnline(const int i) { | |
| 59 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.
| |
| 60 "%s%s", kCpuDataPathBase, kCpuOnlinePathSuffixFormat); | |
| 61 std::string cpu_online_file = base::StringPrintf( | |
| 62 online_file_format.c_str(), i); | |
| 63 if (!base::PathExists(base::FilePath(cpu_online_file))) { | |
| 64 // If the 'online' status file is missing, then it means that the CPU is | |
| 65 // not hot-pluggable and hence is always online. | |
| 66 return true; | |
| 67 } | |
| 68 | |
| 69 int online; | |
| 70 std::string cpu_online_string; | |
| 71 if (base::ReadFileToString(base::FilePath(cpu_online_file), | |
| 72 &cpu_online_string)) { | |
| 73 base::TrimString(cpu_online_string, " \n", &cpu_online_string); | |
| 74 if (base::StringToInt(cpu_online_string, &online)) | |
| 75 return online == kCpuOnlineStatus; | |
| 76 } | |
| 77 | |
| 78 LOG(ERROR) << "Bad format or error reading " << cpu_online_file << ". " | |
| 79 << "Assuming offline."; | |
| 80 return false; | |
| 81 } | |
| 82 | |
| 83 // Samples the CPU idle state information from sysfs. |cpu_count| is the number | |
| 84 // of possible CPUs on the system. Sample at index i in |idle_samples| | |
| 85 // corresponds to the idle state information of the i-th CPU. | |
| 86 void SampleCpuIdleData( | |
| 87 int cpu_count, | |
| 88 std::vector<CpuDataCollector::StateOccupancySample>* idle_samples) { | |
| 89 base::Time start_time = base::Time::Now(); | |
| 90 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.
| |
| 91 CpuDataCollector::StateOccupancySample idle_sample; | |
| 92 idle_sample.time = base::Time::Now(); | |
| 93 | |
| 94 if (!CpuIsOnline(i)) { | |
| 95 idle_sample.cpu_online = false; | |
| 96 } else { | |
| 97 idle_sample.cpu_online = true; | |
| 98 | |
| 99 int state_count = 0; | |
| 100 std::string idle_state_dir_format = base::StringPrintf( | |
| 101 "%s%s", kCpuDataPathBase, kCpuIdleStateDirPathSuffixFormat); | |
| 102 std::string idle_state_dir = base::StringPrintf( | |
| 103 idle_state_dir_format.c_str(), i, state_count); | |
| 104 while (base::DirectoryExists(base::FilePath(idle_state_dir))) { | |
| 105 std::string name_file_format = base::StringPrintf( | |
| 106 "%s%s", kCpuDataPathBase, kCpuIdleStateNamePathSuffixFormat); | |
| 107 std::string name_file_path = base::StringPrintf( | |
| 108 name_file_format.c_str(), i, state_count); | |
| 109 DCHECK(base::PathExists(base::FilePath(name_file_path))); | |
| 110 | |
| 111 std::string time_file_format = base::StringPrintf( | |
| 112 "%s%s", kCpuDataPathBase, kCpuIdleStateTimePathSuffixFormat); | |
| 113 std::string time_file_path = base::StringPrintf( | |
| 114 time_file_format.c_str(), i, state_count); | |
| 115 DCHECK(base::PathExists(base::FilePath(time_file_path))); | |
| 116 | |
| 117 std::string state_name, occupancy_time_string; | |
| 118 int64 occupancy_time; | |
| 119 if (base::ReadFileToString(base::FilePath(name_file_path), | |
| 120 &state_name) && | |
| 121 base::ReadFileToString(base::FilePath(time_file_path), | |
| 122 &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.
| |
| 123 base::TrimString(state_name, " \n", &state_name); | |
| 124 base::TrimString( | |
| 125 occupancy_time_string, " \n", &occupancy_time_string); | |
| 126 if (base::StringToInt64(occupancy_time_string, &occupancy_time)) { | |
| 127 // idle state occupancy time in sysfs is recorded in microseconds. | |
| 128 idle_sample.state_occupancy[state_name] = occupancy_time/1000; | |
| 129 } else { | |
| 130 LOG(ERROR) << "Bad format in " << time_file_path << ". " | |
| 131 << "Dropping sample."; | |
| 132 idle_samples->clear(); | |
| 133 return; | |
| 134 } | |
| 135 } else { | |
| 136 // If an error occurs reading/parsing single state data, drop all the | |
| 137 // samples as an incomplete sample can mislead consumers of this | |
| 138 // sample. | |
| 139 LOG(ERROR) << "Error reading idle state from " | |
| 140 << idle_state_dir << ". Dropping sample."; | |
| 141 idle_samples->clear(); | |
| 142 return; | |
| 143 } | |
| 144 | |
| 145 ++state_count; | |
| 146 idle_state_dir = base::StringPrintf( | |
| 147 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.
| |
| 148 } | |
| 149 } | |
| 150 | |
| 151 idle_samples->push_back(idle_sample); | |
| 152 } | |
| 153 | |
| 154 // If there was an interruption in sampling (like system suspended), | |
| 155 // discard the samples! | |
| 156 if (base::TimeDelta(base::Time::Now() - start_time).InMilliseconds() > 1000) { | |
| 157 idle_samples->clear(); | |
| 158 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.
| |
| 159 } | |
| 160 } | |
| 161 | |
| 162 // Samples the CPU freq state information from sysfs. |cpu_count| is the number | |
| 163 // of possible CPUs on the system. Sample at index i in |freq_samples| | |
| 164 // corresponds to the freq state information of the i-th CPU. | |
| 165 void SampleCpuFreqData( | |
| 166 int cpu_count, | |
| 167 std::vector<CpuDataCollector::StateOccupancySample>* freq_samples) { | |
| 168 base::Time startTime = base::Time::Now(); | |
| 169 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.
| |
| 170 CpuDataCollector::StateOccupancySample freq_sample; | |
| 171 | |
| 172 if (!CpuIsOnline(i)) { | |
| 173 freq_sample.time = base::Time::Now(); | |
| 174 freq_sample.cpu_online = false; | |
| 175 } else { | |
| 176 freq_sample.cpu_online = true; | |
| 177 | |
| 178 std::string time_in_state_path_format = base::StringPrintf( | |
| 179 "%s%s", kCpuDataPathBase, kCpuFreqTimeInStatePathSuffixFormat); | |
| 180 std::string time_in_state_path = base::StringPrintf( | |
| 181 time_in_state_path_format.c_str(), i); | |
| 182 DCHECK(base::PathExists(base::FilePath(time_in_state_path))); | |
| 183 | |
| 184 std::string time_in_state_string; | |
| 185 // Note time as close to reading the file as possible. This is not | |
| 186 // possible for idle state samples as the information for each state there | |
| 187 // is recorded in different files. | |
| 188 base::Time now = base::Time::Now(); | |
| 189 if (!base::ReadFileToString(base::FilePath(time_in_state_path), | |
| 190 &time_in_state_string)) { | |
| 191 LOG(ERROR) << "Error reading " << time_in_state_path << ". " | |
| 192 << "Dropping sample."; | |
| 193 freq_samples->clear(); | |
| 194 return; | |
| 195 } | |
| 196 | |
| 197 freq_sample.time = now; | |
| 198 | |
| 199 std::vector<std::string> lines; | |
| 200 base::SplitString(time_in_state_string, '\n', &lines); | |
| 201 // The last line could end with '\n'. Ignore the last empty string in | |
| 202 // such cases. | |
| 203 size_t state_count = lines.size(); | |
| 204 if (state_count > 0 && lines.back().empty()) | |
| 205 state_count -= 1; | |
| 206 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.
| |
| 207 std::vector<std::string> pair; | |
| 208 int freq_in_khz; | |
| 209 int64 occupancy_time; | |
| 210 | |
| 211 // Occupancy of each state is in the format "<state> <time>" | |
| 212 base::SplitString(lines[k], ' ', &pair); | |
| 213 for (size_t s = 0; s < pair.size(); ++s) | |
| 214 base::TrimString(pair[s], " \n", &pair[s]); | |
| 215 if (pair.size() == 2 && | |
| 216 base::StringToInt(pair[0], &freq_in_khz) && | |
| 217 base::StringToInt64(pair[1], &occupancy_time)) { | |
| 218 // Freq state occupancy time is recorded in tens of milliseconds. | |
| 219 freq_sample.state_occupancy[base::IntToString(freq_in_khz / 1000)] = | |
| 220 occupancy_time * 10; | |
| 221 } else { | |
| 222 LOG(ERROR) << "Bad format in " << time_in_state_path << ". " | |
| 223 << "Dropping sample."; | |
| 224 freq_samples->clear(); | |
| 225 return; | |
| 226 } | |
| 227 } | |
| 228 } | |
| 229 | |
| 230 freq_samples->push_back(freq_sample); | |
| 231 } | |
| 232 | |
| 233 // If there was an interruption in sampling (like system suspended), | |
| 234 // discard the samples! | |
| 235 if (base::TimeDelta(base::Time::Now() - startTime).InMilliseconds() > 1000) { | |
| 236 freq_samples->clear(); | |
| 237 LOG(WARNING) << "Dropped a freq state sample due to excessive time delay."; | |
| 238 } | |
| 239 } | |
| 240 | |
| 241 } // namespace | |
| 242 | |
| 243 CpuDataCollector::CpuDataCollector() : cpu_count_(1), weak_ptr_factory_(this) { | |
| 244 std::string possible_cpu_path = base::StringPrintf( | |
| 245 "%s%s", kCpuDataPathBase, kPossibleCpuPathSuffix); | |
| 246 if (!base::PathExists(base::FilePath(possible_cpu_path))) { | |
| 247 LOG(ERROR) << "File listing possible CPUs missing. " | |
| 248 << "Defaulting CPU count to 1."; | |
| 249 } else { | |
| 250 std::string possible_string; | |
| 251 if (base::ReadFileToString(base::FilePath(possible_cpu_path), | |
| 252 &possible_string)) { | |
| 253 int max_cpu; | |
| 254 // The possible CPUs are listed in the format "0-N". Hence, N is present | |
| 255 // in the substring starting at offset 2. | |
| 256 base::TrimString(possible_string, " \n", &possible_string); | |
| 257 if (possible_string.find("-") != std::string::npos && | |
| 258 possible_string.length() > 2 && | |
| 259 base::StringToInt(possible_string.substr(2), &max_cpu)) { | |
| 260 cpu_count_ = max_cpu + 1; | |
| 261 } else { | |
| 262 LOG(ERROR) << "Unknown format in the file listing possible CPUs. " | |
| 263 << "Defaulting CPU count to 1."; | |
| 264 } | |
| 265 } else { | |
| 266 LOG(ERROR) << "Error reading the file listing possible CPUs. " | |
| 267 << "Defaulting CPU count to 1."; | |
| 268 } | |
| 269 } | |
| 270 | |
| 271 // Initialize the deques in the data vectors. | |
| 272 cpu_idle_state_data_.resize(cpu_count_); | |
| 273 cpu_freq_state_data_.resize(cpu_count_); | |
| 274 } | |
| 275 | |
| 276 void CpuDataCollector::Start() { | |
| 277 timer_.Start(FROM_HERE, | |
| 278 base::TimeDelta::FromSeconds(kCpuDataSamplePeriodSec), | |
| 279 this, | |
| 280 &CpuDataCollector::PostSampleCpuState); | |
| 281 } | |
| 282 | |
| 283 void CpuDataCollector::PostSampleCpuState() { | |
| 284 std::vector<StateOccupancySample>* idle_samples = | |
| 285 new std::vector<StateOccupancySample>; | |
| 286 std::vector<StateOccupancySample>* freq_samples = | |
| 287 new std::vector<StateOccupancySample>; | |
| 288 content::BrowserThread::PostBlockingPoolTaskAndReply( | |
| 289 FROM_HERE, | |
| 290 base::Bind(&CpuDataCollector::SampleCpuStateOnBlockingPool, | |
| 291 weak_ptr_factory_.GetWeakPtr(), | |
| 292 base::Unretained(idle_samples), | |
| 293 base::Unretained(freq_samples)), | |
| 294 base::Bind(&CpuDataCollector::SaveCpuStateSamplesOnUIThread, | |
| 295 weak_ptr_factory_.GetWeakPtr(), | |
| 296 base::Owned(idle_samples), | |
| 297 base::Owned(freq_samples))); | |
| 298 } | |
| 299 | |
| 300 void CpuDataCollector::SampleCpuStateOnBlockingPool( | |
| 301 std::vector<CpuDataCollector::StateOccupancySample>* idle_samples, | |
| 302 std::vector<CpuDataCollector::StateOccupancySample>* freq_samples) { | |
| 303 DCHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 304 | |
| 305 SampleCpuIdleData(cpu_count_, idle_samples); | |
| 306 SampleCpuFreqData(cpu_count_, freq_samples); | |
| 307 } | |
| 308 | |
| 309 void CpuDataCollector::SaveCpuStateSamplesOnUIThread( | |
| 310 const std::vector<CpuDataCollector::StateOccupancySample>* idle_samples, | |
| 311 const std::vector<CpuDataCollector::StateOccupancySample>* freq_samples) { | |
| 312 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 313 | |
| 314 // |idle_samples| or |freq_samples| could be empty sometimes (for example, if | |
| 315 // sampling was interrupted due to system suspension). Iff they are not empty, | |
| 316 // they will have one sample each for each of the CPUs. | |
| 317 | |
| 318 if (idle_samples->size() > 0) { | |
| 319 DCHECK_EQ(idle_samples->size(), cpu_idle_state_data_.size()); | |
| 320 for (size_t i = 0; i < cpu_idle_state_data_.size(); ++i) | |
| 321 AddSample(&cpu_idle_state_data_[i], (*idle_samples)[i]); | |
| 322 } | |
| 323 | |
| 324 if (freq_samples->size() > 0) { | |
| 325 DCHECK_EQ(freq_samples->size(), cpu_freq_state_data_.size()); | |
| 326 for (size_t i = 0; i < cpu_freq_state_data_.size(); ++i) | |
| 327 AddSample(&cpu_freq_state_data_[i], (*freq_samples)[i]); | |
| 328 } | |
| 329 } | |
| 330 | |
| 331 CpuDataCollector::StateOccupancySample::StateOccupancySample() | |
| 332 : cpu_online(false) { | |
| 333 } | |
| 334 | |
| 335 } // namespace chromeos | |
| OLD | NEW |