Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 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 "chromeos/system/cpu_temperature_reader.h" | |
| 6 | |
| 7 #include "base/files/file_enumerator.h" | |
| 8 #include "base/files/file_path.h" | |
| 9 #include "base/files/file_util.h" | |
| 10 #include "base/location.h" | |
| 11 #include "base/strings/string_number_conversions.h" | |
| 12 #include "base/strings/string_util.h" | |
| 13 #include "base/task_scheduler/post_task.h" | |
| 14 #include "base/task_scheduler/task_traits.h" | |
| 15 | |
| 16 namespace chromeos { | |
| 17 namespace system { | |
| 18 | |
| 19 namespace { | |
| 20 | |
| 21 // The location we read our CPU temperature and channel label from. | |
| 22 const char kDefaultHwmonDir[] = "/sys/class/hwmon/"; | |
|
Daniel Erat
2017/04/20 21:23:52
nit: use constexpr char for all of these
Simon Que
2017/04/21 08:52:25
Done.
| |
| 23 const char kDeviceDir[] = "device"; | |
| 24 const char kHwmonDirectoryPattern[] = "hwmon*"; | |
| 25 const char kCPUTempFilePattern[] = "temp*_input"; | |
| 26 | |
| 27 using CPUTemperatureInfo = CPUTemperatureReader::CPUTemperatureInfo; | |
|
Daniel Erat
2017/04/20 21:23:52
move this up outside of the anon namespace (it fee
Simon Que
2017/04/21 08:52:26
Done.
| |
| 28 | |
| 29 bool ReadTemperatureFromPath(const base::FilePath& path, double* temp_celsius) { | |
|
Daniel Erat
2017/04/20 21:23:53
temp_celsius_out
Simon Que
2017/04/21 08:52:25
Done.
| |
| 30 std::string temperature_string; | |
| 31 uint32_t temperature = 0; | |
| 32 if (base::ReadFileToString(path, &temperature_string) && | |
| 33 base::StringToUint(temperature_string, &temperature)) { | |
| 34 *temp_celsius = static_cast<double>(temperature) / 1000; | |
|
Daniel Erat
2017/04/20 21:23:53
instead of static_cast, you can probably just divi
Simon Que
2017/04/21 08:52:26
Done.
| |
| 35 return true; | |
| 36 } | |
| 37 return false; | |
| 38 } | |
| 39 | |
| 40 std::string GetLabelFromPath(const base::FilePath& label_path) { | |
| 41 // Get the label describing this temperature. Use temp*_label | |
| 42 // if present, fall back on name file or blank. | |
| 43 std::string label; | |
| 44 if (base::PathExists(base::FilePath(label_path))) { | |
| 45 base::ReadFileToString(base::FilePath(label_path), &label); | |
| 46 return label; | |
| 47 } | |
| 48 | |
| 49 base::FilePath name_path = label_path.DirName().Append("name"); | |
| 50 if (base::PathExists(base::FilePath(name_path))) { | |
| 51 base::ReadFileToString(name_path, &label); | |
| 52 } | |
| 53 return label; | |
|
Daniel Erat
2017/04/20 21:23:53
returning an empty string feels a bit strange. wou
Simon Que
2017/04/21 08:52:25
Done.
| |
| 54 } | |
| 55 | |
| 56 std::vector<CPUTemperatureInfo> ReadCPUTemperatures( | |
| 57 const std::string& hwmon_dir) { | |
| 58 std::vector<CPUTemperatureInfo> result; | |
| 59 | |
| 60 // Get directories /sys/class/hwmon/hwmon* | |
|
Daniel Erat
2017/04/20 21:23:53
nit: add trailing period
Simon Que
2017/04/21 08:52:26
Done.
| |
| 61 base::FileEnumerator hwmon_enumerator(base::FilePath(hwmon_dir), false, | |
| 62 base::FileEnumerator::DIRECTORIES, | |
| 63 kHwmonDirectoryPattern); | |
| 64 for (base::FilePath hwmon_path = hwmon_enumerator.Next(); !hwmon_path.empty(); | |
| 65 hwmon_path = hwmon_enumerator.Next()) { | |
| 66 // Get temp*_input files in hwmon*/ and hwmon*/device/ | |
|
Daniel Erat
2017/04/20 21:23:52
nit: add trailing period
Simon Que
2017/04/21 08:52:25
Done.
| |
| 67 if (base::PathExists(hwmon_path.Append(kDeviceDir))) { | |
| 68 hwmon_path = hwmon_path.Append(kDeviceDir); | |
| 69 } | |
| 70 base::FileEnumerator enumerator( | |
| 71 hwmon_path, false, base::FileEnumerator::FILES, kCPUTempFilePattern); | |
| 72 for (base::FilePath temperature_path = enumerator.Next(); | |
| 73 !temperature_path.empty(); temperature_path = enumerator.Next()) { | |
| 74 // Get appropriate temp*_label file. | |
| 75 std::string label_path = temperature_path.MaybeAsASCII(); | |
| 76 if (label_path.empty()) { | |
| 77 LOG(WARNING) << "Unable to parse a path to temp*_input file as ASCII"; | |
| 78 continue; | |
| 79 } | |
| 80 CPUTemperatureInfo info; | |
| 81 if (!ReadTemperatureFromPath(temperature_path, &info.temp_celsius)) { | |
| 82 LOG(WARNING) << "Unable to read CPU temperature from " << label_path; | |
| 83 continue; | |
| 84 } | |
| 85 base::ReplaceSubstringsAfterOffset(&label_path, 0, "input", "label"); | |
| 86 info.label = GetLabelFromPath(base::FilePath(label_path)); | |
| 87 result.push_back(info); | |
| 88 } | |
| 89 } | |
| 90 return result; | |
| 91 } | |
| 92 | |
| 93 } // namespace | |
| 94 | |
| 95 CPUTemperatureReader::CPUTemperatureReader() : hwmon_dir_(kDefaultHwmonDir) {} | |
| 96 | |
| 97 void CPUTemperatureReader::GetCPUTemperatures( | |
| 98 const GetCPUTemperaturesCallback& callback) { | |
| 99 base::PostTaskWithTraitsAndReplyWithResult( | |
| 100 FROM_HERE, | |
| 101 base::TaskTraits().MayBlock().WithPriority( | |
| 102 base::TaskPriority::BACKGROUND), | |
| 103 base::Bind(&ReadCPUTemperatures, hwmon_dir_), callback); | |
| 104 } | |
| 105 | |
| 106 } // namespace system | |
| 107 } // namespace chromeos | |
| OLD | NEW |