Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(84)

Side by Side Diff: chromeos/system/cpu_temp_reader.cc

Issue 2823583002: chromeos: Add CPU temperature reader (Closed)
Patch Set: Factor out temp and label reading; Add class and unit test Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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_temp_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/strings/string_number_conversions.h"
11 #include "base/strings/string_util.h"
12
13 namespace chromeos {
14 namespace system {
15
16 namespace {
17
18 // The location we read our CPU temperature and channel label from.
19 const char kDefaultHwmonDir[] = "/sys/class/hwmon/";
20 const char kDeviceDir[] = "device";
21 const char kHwmonDirectoryPattern[] = "hwmon*";
22 const char kCPUTempFilePattern[] = "temp*_input";
23
24 bool ReadTemperatureFromPath(const base::FilePath& path, double* temp_celsius) {
25 std::string temperature_string;
26 uint32_t temperature = 0;
27 if (base::ReadFileToString(path, &temperature_string) &&
28 base::StringToUint(temperature_string, &temperature)) {
29 *temp_celsius = static_cast<double>(temperature) / 1000;
30 return true;
31 }
32 return false;
33 }
34
35 std::string GetLabelFromPath(const base::FilePath& label_path) {
36 // Get the label describing this temperature. Use temp*_label
37 // if present, fall back on name file or blank.
38 std::string label;
39 if (base::PathExists(base::FilePath(label_path))) {
40 base::ReadFileToString(base::FilePath(label_path), &label);
41 return label;
42 }
43
44 base::FilePath name_path = label_path.DirName().Append("name");
45 if (base::PathExists(base::FilePath(name_path))) {
46 base::ReadFileToString(name_path, &label);
47 }
48 return label;
49 }
50
51 } // namespace
52
53 CPUTemperatureReader::CPUTemperatureReader() : hwmon_dir_(kDefaultHwmonDir) {}
54
55 std::vector<CPUTemperatureInfo> CPUTemperatureReader::GetCPUTemperatures() {
56 std::vector<CPUTemperatureInfo> result;
57
58 // Get directories /sys/class/hwmon/hwmon*
59 base::FileEnumerator hwmon_enumerator(base::FilePath(hwmon_dir_), false,
60 base::FileEnumerator::DIRECTORIES,
61 kHwmonDirectoryPattern);
62 for (base::FilePath hwmon_path = hwmon_enumerator.Next(); !hwmon_path.empty();
63 hwmon_path = hwmon_enumerator.Next()) {
64 // Get temp*_input files in hwmon*/ and hwmon*/device/
65 if (base::PathExists(hwmon_path.Append(kDeviceDir))) {
66 hwmon_path = hwmon_path.Append(kDeviceDir);
67 }
68 base::FileEnumerator enumerator(
69 hwmon_path, false, base::FileEnumerator::FILES, kCPUTempFilePattern);
70 for (base::FilePath temperature_path = enumerator.Next();
71 !temperature_path.empty(); temperature_path = enumerator.Next()) {
72 // Get appropriate temp*_label file.
73 std::string label_path = temperature_path.MaybeAsASCII();
74 if (label_path.empty()) {
75 LOG(WARNING) << "Unable to parse a path to temp*_input file as ASCII";
76 continue;
77 }
78 CPUTemperatureInfo info;
79 if (!ReadTemperatureFromPath(temperature_path, &info.temp_celsius)) {
80 LOG(WARNING) << "Unable to read CPU temperature from " << label_path;
81 continue;
82 }
83 base::ReplaceSubstringsAfterOffset(&label_path, 0, "input", "label");
84 info.label = GetLabelFromPath(base::FilePath(label_path));
85 result.push_back(info);
86 }
87 }
88 return result;
89 }
90
91 } // namespace system
92 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698