| 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 <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 if (!base::PathExists(base::FilePath(cpu_online_file))) { | 87 if (!base::PathExists(base::FilePath(cpu_online_file))) { |
| 88 // If the 'online' status file is missing, then it means that the CPU is | 88 // If the 'online' status file is missing, then it means that the CPU is |
| 89 // not hot-pluggable and hence is always online. | 89 // not hot-pluggable and hence is always online. |
| 90 return true; | 90 return true; |
| 91 } | 91 } |
| 92 | 92 |
| 93 int online; | 93 int online; |
| 94 std::string cpu_online_string; | 94 std::string cpu_online_string; |
| 95 if (base::ReadFileToString(base::FilePath(cpu_online_file), | 95 if (base::ReadFileToString(base::FilePath(cpu_online_file), |
| 96 &cpu_online_string)) { | 96 &cpu_online_string)) { |
| 97 base::TrimWhitespace(cpu_online_string, base::TRIM_ALL, &cpu_online_string); | 97 base::TrimWhitespaceASCII(cpu_online_string, base::TRIM_ALL, |
| 98 &cpu_online_string); |
| 98 if (base::StringToInt(cpu_online_string, &online)) | 99 if (base::StringToInt(cpu_online_string, &online)) |
| 99 return online == kCpuOnlineStatus; | 100 return online == kCpuOnlineStatus; |
| 100 } | 101 } |
| 101 | 102 |
| 102 LOG(ERROR) << "Bad format or error reading " << cpu_online_file << ". " | 103 LOG(ERROR) << "Bad format or error reading " << cpu_online_file << ". " |
| 103 << "Assuming offline."; | 104 << "Assuming offline."; |
| 104 return false; | 105 return false; |
| 105 } | 106 } |
| 106 | 107 |
| 107 // Samples the CPU idle state information from sysfs. |cpu_count| is the number | 108 // Samples the CPU idle state information from sysfs. |cpu_count| is the number |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 &occupancy_time_string)) { | 153 &occupancy_time_string)) { |
| 153 // If an error occurs reading/parsing single state data, drop all the | 154 // If an error occurs reading/parsing single state data, drop all the |
| 154 // samples as an incomplete sample can mislead consumers of this | 155 // samples as an incomplete sample can mislead consumers of this |
| 155 // sample. | 156 // sample. |
| 156 LOG(ERROR) << "Error reading idle state from " | 157 LOG(ERROR) << "Error reading idle state from " |
| 157 << idle_state_dir << ". Dropping sample."; | 158 << idle_state_dir << ". Dropping sample."; |
| 158 idle_samples->clear(); | 159 idle_samples->clear(); |
| 159 return; | 160 return; |
| 160 } | 161 } |
| 161 | 162 |
| 162 base::TrimWhitespace(state_name, base::TRIM_ALL, &state_name); | 163 base::TrimWhitespaceASCII(state_name, base::TRIM_ALL, &state_name); |
| 163 base::TrimWhitespace( | 164 base::TrimWhitespaceASCII(occupancy_time_string, base::TRIM_ALL, |
| 164 occupancy_time_string, base::TRIM_ALL, &occupancy_time_string); | 165 &occupancy_time_string); |
| 165 if (base::StringToInt64(occupancy_time_string, &occupancy_time_usec)) { | 166 if (base::StringToInt64(occupancy_time_string, &occupancy_time_usec)) { |
| 166 // idle state occupancy time in sysfs is recorded in microseconds. | 167 // idle state occupancy time in sysfs is recorded in microseconds. |
| 167 int64 time_in_state_ms = occupancy_time_usec / 1000; | 168 int64 time_in_state_ms = occupancy_time_usec / 1000; |
| 168 size_t index = IndexInVector(state_name, cpu_idle_state_names); | 169 size_t index = IndexInVector(state_name, cpu_idle_state_names); |
| 169 if (index >= idle_sample.time_in_state.size()) | 170 if (index >= idle_sample.time_in_state.size()) |
| 170 idle_sample.time_in_state.resize(index + 1); | 171 idle_sample.time_in_state.resize(index + 1); |
| 171 idle_sample.time_in_state[index] = time_in_state_ms; | 172 idle_sample.time_in_state[index] = time_in_state_ms; |
| 172 } else { | 173 } else { |
| 173 LOG(ERROR) << "Bad format in " << time_file_path << ". " | 174 LOG(ERROR) << "Bad format in " << time_file_path << ". " |
| 174 << "Dropping sample."; | 175 << "Dropping sample."; |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 317 if (!base::PathExists(base::FilePath(possible_cpu_path))) { | 318 if (!base::PathExists(base::FilePath(possible_cpu_path))) { |
| 318 LOG(ERROR) << "File listing possible CPUs missing. " | 319 LOG(ERROR) << "File listing possible CPUs missing. " |
| 319 << "Defaulting CPU count to 1."; | 320 << "Defaulting CPU count to 1."; |
| 320 } else { | 321 } else { |
| 321 std::string possible_string; | 322 std::string possible_string; |
| 322 if (base::ReadFileToString(base::FilePath(possible_cpu_path), | 323 if (base::ReadFileToString(base::FilePath(possible_cpu_path), |
| 323 &possible_string)) { | 324 &possible_string)) { |
| 324 int max_cpu; | 325 int max_cpu; |
| 325 // The possible CPUs are listed in the format "0-N". Hence, N is present | 326 // The possible CPUs are listed in the format "0-N". Hence, N is present |
| 326 // in the substring starting at offset 2. | 327 // in the substring starting at offset 2. |
| 327 base::TrimWhitespace(possible_string, base::TRIM_ALL, &possible_string); | 328 base::TrimWhitespaceASCII(possible_string, base::TRIM_ALL, |
| 329 &possible_string); |
| 328 if (possible_string.find("-") != std::string::npos && | 330 if (possible_string.find("-") != std::string::npos && |
| 329 possible_string.length() > 2 && | 331 possible_string.length() > 2 && |
| 330 base::StringToInt(possible_string.substr(2), &max_cpu)) { | 332 base::StringToInt(possible_string.substr(2), &max_cpu)) { |
| 331 *cpu_count = max_cpu + 1; | 333 *cpu_count = max_cpu + 1; |
| 332 } else { | 334 } else { |
| 333 LOG(ERROR) << "Unknown format in the file listing possible CPUs. " | 335 LOG(ERROR) << "Unknown format in the file listing possible CPUs. " |
| 334 << "Defaulting CPU count to 1."; | 336 << "Defaulting CPU count to 1."; |
| 335 } | 337 } |
| 336 } else { | 338 } else { |
| 337 LOG(ERROR) << "Error reading the file listing possible CPUs. " | 339 LOG(ERROR) << "Error reading the file listing possible CPUs. " |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 435 } | 437 } |
| 436 | 438 |
| 437 CpuDataCollector::StateOccupancySample::StateOccupancySample() | 439 CpuDataCollector::StateOccupancySample::StateOccupancySample() |
| 438 : cpu_online(false) { | 440 : cpu_online(false) { |
| 439 } | 441 } |
| 440 | 442 |
| 441 CpuDataCollector::StateOccupancySample::~StateOccupancySample() { | 443 CpuDataCollector::StateOccupancySample::~StateOccupancySample() { |
| 442 } | 444 } |
| 443 | 445 |
| 444 } // namespace chromeos | 446 } // namespace chromeos |
| OLD | NEW |