Chromium Code Reviews| Index: chromeos/system/cpu_temperature_reader.cc |
| diff --git a/chromeos/system/cpu_temperature_reader.cc b/chromeos/system/cpu_temperature_reader.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..2db816668ee7c61ef68aa69704c17f5b6586420c |
| --- /dev/null |
| +++ b/chromeos/system/cpu_temperature_reader.cc |
| @@ -0,0 +1,112 @@ |
| +// Copyright 2017 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 "chromeos/system/cpu_temperature_reader.h" |
| + |
| +#include "base/files/file_enumerator.h" |
| +#include "base/files/file_path.h" |
| +#include "base/files/file_util.h" |
| +#include "base/location.h" |
| +#include "base/strings/string_number_conversions.h" |
| +#include "base/strings/string_util.h" |
| +#include "base/task_scheduler/post_task.h" |
| +#include "base/task_scheduler/task_traits.h" |
| + |
| +namespace chromeos { |
| +namespace system { |
| + |
| +using CPUTemperatureInfo = CPUTemperatureReader::CPUTemperatureInfo; |
| + |
| +namespace { |
| + |
| +// The location we read our CPU temperature and channel label from. |
| +constexpr char kDefaultHwmonDir[] = "/sys/class/hwmon/"; |
| +constexpr char kDeviceDir[] = "device"; |
| +constexpr char kHwmonDirectoryPattern[] = "hwmon*"; |
| +constexpr char kCPUTempFilePattern[] = "temp*_input"; |
| + |
| +// The contents of sysfs files might contain a newline at the end. Use this |
| +// function to read from a sysfs file and remove the newline. |
| +bool ReadFileContentsAndTrimWhitespace(const base::FilePath& path, |
| + std::string* contents_out) { |
| + if (!base::ReadFileToString(path, contents_out)) |
| + return false; |
| + base::TrimWhitespaceASCII(*contents_out, base::TRIM_TRAILING, contents_out); |
| + return true; |
| +} |
| + |
| +bool ReadTemperatureFromPath(const base::FilePath& path, |
|
Daniel Erat
2017/04/21 21:01:20
nit: add a comment describing this method
Simon Que
2017/04/24 13:59:17
Done.
|
| + double* temp_celsius_out) { |
| + std::string temperature_string; |
| + if (!ReadFileContentsAndTrimWhitespace(path, &temperature_string)) |
| + return false; |
| + uint32_t temperature = 0; |
| + if (!base::StringToUint(temperature_string, &temperature)) |
| + return false; |
| + *temp_celsius_out = temperature / 1000.; |
|
Daniel Erat
2017/04/21 21:01:20
nit: add '0' after '.'
Simon Que
2017/04/24 13:59:17
Done.
|
| + return true; |
| +} |
| + |
| +// Get the label describing this temperature. Use the file "temp*_label" if it |
|
Daniel Erat
2017/04/21 21:01:20
nit: s/Get/Gets/
Simon Que
2017/04/24 13:59:16
Done.
|
| +// is present, or fall back on the file "name" or |label_path|. |
| +std::string GetLabelFromPath(const base::FilePath& label_path) { |
| + std::string label; |
| + if (base::PathExists(label_path) && |
| + ReadFileContentsAndTrimWhitespace(base::FilePath(label_path), &label) && |
| + !label.empty()) { |
| + return label; |
| + } |
| + |
| + base::FilePath name_path = label_path.DirName().Append("name"); |
| + if (base::PathExists(name_path) && |
| + ReadFileContentsAndTrimWhitespace(name_path, &label) && !label.empty()) { |
| + return label; |
| + } |
| + return label_path.MaybeAsASCII(); |
| +} |
| + |
| +} // namespace |
| + |
| +CPUTemperatureReader::CPUTemperatureReader() : hwmon_dir_(kDefaultHwmonDir) {} |
| + |
| +CPUTemperatureReader::~CPUTemperatureReader() {} |
|
Daniel Erat
2017/04/21 21:01:20
nit: i think we prefer "= default;" instead of "{}
Simon Que
2017/04/24 13:59:17
Done.
|
| + |
| +std::vector<CPUTemperatureInfo> CPUTemperatureReader::GetCPUTemperatures() { |
| + std::vector<CPUTemperatureInfo> result; |
| + |
| + // Get directories /sys/class/hwmon/hwmon*. |
| + base::FileEnumerator hwmon_enumerator(base::FilePath(hwmon_dir_), false, |
| + base::FileEnumerator::DIRECTORIES, |
| + kHwmonDirectoryPattern); |
| + for (base::FilePath hwmon_path = hwmon_enumerator.Next(); !hwmon_path.empty(); |
| + hwmon_path = hwmon_enumerator.Next()) { |
| + // Get temp*_input files in hwmon*/ and hwmon*/device/. |
| + if (base::PathExists(hwmon_path.Append(kDeviceDir))) { |
| + hwmon_path = hwmon_path.Append(kDeviceDir); |
| + } |
| + base::FileEnumerator enumerator( |
| + hwmon_path, false, base::FileEnumerator::FILES, kCPUTempFilePattern); |
| + for (base::FilePath temperature_path = enumerator.Next(); |
| + !temperature_path.empty(); temperature_path = enumerator.Next()) { |
| + // Get appropriate temp*_label file. |
| + std::string label_path = temperature_path.MaybeAsASCII(); |
|
Daniel Erat
2017/04/21 21:01:20
it feels confusing how you initialize this local h
Simon Que
2017/04/24 13:59:17
Done.
|
| + if (label_path.empty()) { |
| + LOG(WARNING) << "Unable to parse a path to temp*_input file as ASCII"; |
| + continue; |
| + } |
| + CPUTemperatureInfo info; |
| + if (!ReadTemperatureFromPath(temperature_path, &info.temp_celsius)) { |
| + LOG(WARNING) << "Unable to read CPU temperature from " << label_path; |
| + continue; |
| + } |
| + base::ReplaceSubstringsAfterOffset(&label_path, 0, "input", "label"); |
| + info.label = GetLabelFromPath(base::FilePath(label_path)); |
| + result.push_back(info); |
| + } |
| + } |
| + return result; |
| +} |
| + |
| +} // namespace system |
| +} // namespace chromeos |