| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/power/cpu_data_collector.h" | 5 #include "chrome/browser/chromeos/power/cpu_data_collector.h" |
| 6 | 6 |
| 7 #include <stddef.h> |
| 8 |
| 7 #include <vector> | 9 #include <vector> |
| 8 | 10 |
| 9 #include "base/bind.h" | 11 #include "base/bind.h" |
| 10 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
| 11 #include "base/logging.h" | 13 #include "base/logging.h" |
| 12 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/strings/string_split.h" | 15 #include "base/strings/string_split.h" |
| 14 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
| 15 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
| 16 #include "base/sys_info.h" | 18 #include "base/sys_info.h" |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 name_file_format.c_str(), cpu, state_count); | 141 name_file_format.c_str(), cpu, state_count); |
| 140 DCHECK(base::PathExists(base::FilePath(name_file_path))); | 142 DCHECK(base::PathExists(base::FilePath(name_file_path))); |
| 141 | 143 |
| 142 const std::string time_file_format = base::StringPrintf( | 144 const std::string time_file_format = base::StringPrintf( |
| 143 "%s%s", kCpuDataPathBase, kCpuIdleStateTimePathSuffixFormat); | 145 "%s%s", kCpuDataPathBase, kCpuIdleStateTimePathSuffixFormat); |
| 144 const std::string time_file_path = base::StringPrintf( | 146 const std::string time_file_path = base::StringPrintf( |
| 145 time_file_format.c_str(), cpu, state_count); | 147 time_file_format.c_str(), cpu, state_count); |
| 146 DCHECK(base::PathExists(base::FilePath(time_file_path))); | 148 DCHECK(base::PathExists(base::FilePath(time_file_path))); |
| 147 | 149 |
| 148 std::string state_name, occupancy_time_string; | 150 std::string state_name, occupancy_time_string; |
| 149 int64 occupancy_time_usec; | 151 int64_t occupancy_time_usec; |
| 150 if (!base::ReadFileToString(base::FilePath(name_file_path), | 152 if (!base::ReadFileToString(base::FilePath(name_file_path), |
| 151 &state_name) || | 153 &state_name) || |
| 152 !base::ReadFileToString(base::FilePath(time_file_path), | 154 !base::ReadFileToString(base::FilePath(time_file_path), |
| 153 &occupancy_time_string)) { | 155 &occupancy_time_string)) { |
| 154 // If an error occurs reading/parsing single state data, drop all the | 156 // If an error occurs reading/parsing single state data, drop all the |
| 155 // samples as an incomplete sample can mislead consumers of this | 157 // samples as an incomplete sample can mislead consumers of this |
| 156 // sample. | 158 // sample. |
| 157 LOG(ERROR) << "Error reading idle state from " | 159 LOG(ERROR) << "Error reading idle state from " |
| 158 << idle_state_dir << ". Dropping sample."; | 160 << idle_state_dir << ". Dropping sample."; |
| 159 idle_samples->clear(); | 161 idle_samples->clear(); |
| 160 return; | 162 return; |
| 161 } | 163 } |
| 162 | 164 |
| 163 base::TrimWhitespaceASCII(state_name, base::TRIM_ALL, &state_name); | 165 base::TrimWhitespaceASCII(state_name, base::TRIM_ALL, &state_name); |
| 164 base::TrimWhitespaceASCII(occupancy_time_string, base::TRIM_ALL, | 166 base::TrimWhitespaceASCII(occupancy_time_string, base::TRIM_ALL, |
| 165 &occupancy_time_string); | 167 &occupancy_time_string); |
| 166 if (base::StringToInt64(occupancy_time_string, &occupancy_time_usec)) { | 168 if (base::StringToInt64(occupancy_time_string, &occupancy_time_usec)) { |
| 167 // idle state occupancy time in sysfs is recorded in microseconds. | 169 // idle state occupancy time in sysfs is recorded in microseconds. |
| 168 int64 time_in_state_ms = occupancy_time_usec / 1000; | 170 int64_t time_in_state_ms = occupancy_time_usec / 1000; |
| 169 size_t index = IndexInVector(state_name, cpu_idle_state_names); | 171 size_t index = IndexInVector(state_name, cpu_idle_state_names); |
| 170 if (index >= idle_sample.time_in_state.size()) | 172 if (index >= idle_sample.time_in_state.size()) |
| 171 idle_sample.time_in_state.resize(index + 1); | 173 idle_sample.time_in_state.resize(index + 1); |
| 172 idle_sample.time_in_state[index] = time_in_state_ms; | 174 idle_sample.time_in_state[index] = time_in_state_ms; |
| 173 } else { | 175 } else { |
| 174 LOG(ERROR) << "Bad format in " << time_file_path << ". " | 176 LOG(ERROR) << "Bad format in " << time_file_path << ". " |
| 175 << "Dropping sample."; | 177 << "Dropping sample."; |
| 176 idle_samples->clear(); | 178 idle_samples->clear(); |
| 177 return; | 179 return; |
| 178 } | 180 } |
| 179 } | 181 } |
| 180 } | 182 } |
| 181 | 183 |
| 182 idle_samples->push_back(idle_sample); | 184 idle_samples->push_back(idle_sample); |
| 183 } | 185 } |
| 184 | 186 |
| 185 // If there was an interruption in sampling (like system suspended), | 187 // If there was an interruption in sampling (like system suspended), |
| 186 // discard the samples! | 188 // discard the samples! |
| 187 int64 delay = | 189 int64_t delay = |
| 188 base::TimeDelta(base::Time::Now() - start_time).InMilliseconds(); | 190 base::TimeDelta(base::Time::Now() - start_time).InMilliseconds(); |
| 189 if (delay > kSamplingDurationLimitMs) { | 191 if (delay > kSamplingDurationLimitMs) { |
| 190 idle_samples->clear(); | 192 idle_samples->clear(); |
| 191 LOG(WARNING) << "Dropped an idle state sample due to excessive time delay: " | 193 LOG(WARNING) << "Dropped an idle state sample due to excessive time delay: " |
| 192 << delay << "milliseconds."; | 194 << delay << "milliseconds."; |
| 193 } | 195 } |
| 194 } | 196 } |
| 195 | 197 |
| 196 bool ReadCpuFreqFromOldFile( | 198 bool ReadCpuFreqFromOldFile( |
| 197 const std::string& path, | 199 const std::string& path, |
| (...skipping 11 matching lines...) Expand all Loading... |
| 209 | 211 |
| 210 std::vector<base::StringPiece> lines = base::SplitStringPiece( | 212 std::vector<base::StringPiece> lines = base::SplitStringPiece( |
| 211 time_in_state_string, "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | 213 time_in_state_string, "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| 212 // The last line could end with '\n'. Ignore the last empty string in | 214 // The last line could end with '\n'. Ignore the last empty string in |
| 213 // such cases. | 215 // such cases. |
| 214 size_t state_count = lines.size(); | 216 size_t state_count = lines.size(); |
| 215 if (state_count > 0 && lines.back().empty()) | 217 if (state_count > 0 && lines.back().empty()) |
| 216 state_count -= 1; | 218 state_count -= 1; |
| 217 for (size_t state = 0; state < state_count; ++state) { | 219 for (size_t state = 0; state < state_count; ++state) { |
| 218 int freq_in_khz; | 220 int freq_in_khz; |
| 219 int64 occupancy_time_centisecond; | 221 int64_t occupancy_time_centisecond; |
| 220 | 222 |
| 221 // Occupancy of each state is in the format "<state> <time>" | 223 // Occupancy of each state is in the format "<state> <time>" |
| 222 std::vector<base::StringPiece> pair = base::SplitStringPiece( | 224 std::vector<base::StringPiece> pair = base::SplitStringPiece( |
| 223 lines[state], " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | 225 lines[state], " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| 224 if (pair.size() == 2 && base::StringToInt(pair[0], &freq_in_khz) && | 226 if (pair.size() == 2 && base::StringToInt(pair[0], &freq_in_khz) && |
| 225 base::StringToInt64(pair[1], &occupancy_time_centisecond)) { | 227 base::StringToInt64(pair[1], &occupancy_time_centisecond)) { |
| 226 const std::string state_name = base::IntToString(freq_in_khz / 1000); | 228 const std::string state_name = base::IntToString(freq_in_khz / 1000); |
| 227 size_t index = IndexInVector(state_name, cpu_freq_state_names); | 229 size_t index = IndexInVector(state_name, cpu_freq_state_names); |
| 228 if (index >= freq_sample->time_in_state.size()) | 230 if (index >= freq_sample->time_in_state.size()) |
| 229 freq_sample->time_in_state.resize(index + 1); | 231 freq_sample->time_in_state.resize(index + 1); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 279 freq_samples->clear(); | 281 freq_samples->clear(); |
| 280 return; | 282 return; |
| 281 } | 283 } |
| 282 } | 284 } |
| 283 | 285 |
| 284 freq_samples->push_back(freq_sample); | 286 freq_samples->push_back(freq_sample); |
| 285 } | 287 } |
| 286 | 288 |
| 287 // If there was an interruption in sampling (like system suspended), | 289 // If there was an interruption in sampling (like system suspended), |
| 288 // discard the samples! | 290 // discard the samples! |
| 289 int64 delay = | 291 int64_t delay = |
| 290 base::TimeDelta(base::Time::Now() - start_time).InMilliseconds(); | 292 base::TimeDelta(base::Time::Now() - start_time).InMilliseconds(); |
| 291 if (delay > kSamplingDurationLimitMs) { | 293 if (delay > kSamplingDurationLimitMs) { |
| 292 freq_samples->clear(); | 294 freq_samples->clear(); |
| 293 LOG(WARNING) << "Dropped a freq state sample due to excessive time delay: " | 295 LOG(WARNING) << "Dropped a freq state sample due to excessive time delay: " |
| 294 << delay << "milliseconds."; | 296 << delay << "milliseconds."; |
| 295 } | 297 } |
| 296 } | 298 } |
| 297 | 299 |
| 298 // Samples CPU idle and CPU freq data from sysfs. This function should run on | 300 // Samples CPU idle and CPU freq data from sysfs. This function should run on |
| 299 // the blocking pool as reading from sysfs is a blocking task. Elements at | 301 // the blocking pool as reading from sysfs is a blocking task. Elements at |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 437 } | 439 } |
| 438 | 440 |
| 439 CpuDataCollector::StateOccupancySample::StateOccupancySample() | 441 CpuDataCollector::StateOccupancySample::StateOccupancySample() |
| 440 : cpu_online(false) { | 442 : cpu_online(false) { |
| 441 } | 443 } |
| 442 | 444 |
| 443 CpuDataCollector::StateOccupancySample::~StateOccupancySample() { | 445 CpuDataCollector::StateOccupancySample::~StateOccupancySample() { |
| 444 } | 446 } |
| 445 | 447 |
| 446 } // namespace chromeos | 448 } // namespace chromeos |
| OLD | NEW |