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 using CPUTemperatureInfo = CPUTemperatureReader::CPUTemperatureInfo; | |
20 | |
21 namespace { | |
22 | |
23 // The location we read our CPU temperature and channel label from. | |
24 constexpr char kDefaultHwmonDir[] = "/sys/class/hwmon/"; | |
25 constexpr char kDeviceDir[] = "device"; | |
26 constexpr char kHwmonDirectoryPattern[] = "hwmon*"; | |
27 constexpr char kCPUTempFilePattern[] = "temp*_input"; | |
28 | |
29 // The contents of sysfs files might contain a newline at the end. Use this | |
30 // function to read from a sysfs file and remove the newline. | |
31 bool ReadFileContentsAndTrimWhitespace(const base::FilePath& path, | |
32 std::string* contents_out) { | |
33 if (!base::ReadFileToString(path, contents_out)) | |
34 return false; | |
35 base::TrimWhitespaceASCII(*contents_out, base::TRIM_TRAILING, contents_out); | |
36 return true; | |
37 } | |
38 | |
39 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.
| |
40 double* temp_celsius_out) { | |
41 std::string temperature_string; | |
42 if (!ReadFileContentsAndTrimWhitespace(path, &temperature_string)) | |
43 return false; | |
44 uint32_t temperature = 0; | |
45 if (!base::StringToUint(temperature_string, &temperature)) | |
46 return false; | |
47 *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.
| |
48 return true; | |
49 } | |
50 | |
51 // 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.
| |
52 // is present, or fall back on the file "name" or |label_path|. | |
53 std::string GetLabelFromPath(const base::FilePath& label_path) { | |
54 std::string label; | |
55 if (base::PathExists(label_path) && | |
56 ReadFileContentsAndTrimWhitespace(base::FilePath(label_path), &label) && | |
57 !label.empty()) { | |
58 return label; | |
59 } | |
60 | |
61 base::FilePath name_path = label_path.DirName().Append("name"); | |
62 if (base::PathExists(name_path) && | |
63 ReadFileContentsAndTrimWhitespace(name_path, &label) && !label.empty()) { | |
64 return label; | |
65 } | |
66 return label_path.MaybeAsASCII(); | |
67 } | |
68 | |
69 } // namespace | |
70 | |
71 CPUTemperatureReader::CPUTemperatureReader() : hwmon_dir_(kDefaultHwmonDir) {} | |
72 | |
73 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.
| |
74 | |
75 std::vector<CPUTemperatureInfo> CPUTemperatureReader::GetCPUTemperatures() { | |
76 std::vector<CPUTemperatureInfo> result; | |
77 | |
78 // Get directories /sys/class/hwmon/hwmon*. | |
79 base::FileEnumerator hwmon_enumerator(base::FilePath(hwmon_dir_), false, | |
80 base::FileEnumerator::DIRECTORIES, | |
81 kHwmonDirectoryPattern); | |
82 for (base::FilePath hwmon_path = hwmon_enumerator.Next(); !hwmon_path.empty(); | |
83 hwmon_path = hwmon_enumerator.Next()) { | |
84 // Get temp*_input files in hwmon*/ and hwmon*/device/. | |
85 if (base::PathExists(hwmon_path.Append(kDeviceDir))) { | |
86 hwmon_path = hwmon_path.Append(kDeviceDir); | |
87 } | |
88 base::FileEnumerator enumerator( | |
89 hwmon_path, false, base::FileEnumerator::FILES, kCPUTempFilePattern); | |
90 for (base::FilePath temperature_path = enumerator.Next(); | |
91 !temperature_path.empty(); temperature_path = enumerator.Next()) { | |
92 // Get appropriate temp*_label file. | |
93 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.
| |
94 if (label_path.empty()) { | |
95 LOG(WARNING) << "Unable to parse a path to temp*_input file as ASCII"; | |
96 continue; | |
97 } | |
98 CPUTemperatureInfo info; | |
99 if (!ReadTemperatureFromPath(temperature_path, &info.temp_celsius)) { | |
100 LOG(WARNING) << "Unable to read CPU temperature from " << label_path; | |
101 continue; | |
102 } | |
103 base::ReplaceSubstringsAfterOffset(&label_path, 0, "input", "label"); | |
104 info.label = GetLabelFromPath(base::FilePath(label_path)); | |
105 result.push_back(info); | |
106 } | |
107 } | |
108 return result; | |
109 } | |
110 | |
111 } // namespace system | |
112 } // namespace chromeos | |
OLD | NEW |