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

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

Issue 2823583002: chromeos: Add CPU temperature reader (Closed)
Patch Set: 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 kHwmonDir[] = "/sys/class/hwmon/";
20 const char kDeviceDir[] = "device";
21 const char kHwmonDirectoryPattern[] = "hwmon*";
22 const char kCPUTempFilePattern[] = "temp*_input";
23
24 } // namespace
25
26 std::vector<CPUTemperatureInfo> GetCPUTemperatures() {
27 std::vector<CPUTemperatureInfo> result;
28
29 // Get directories /sys/class/hwmon/hwmon*
30 base::FileEnumerator hwmon_enumerator(base::FilePath(kHwmonDir), false,
31 base::FileEnumerator::DIRECTORIES,
32 kHwmonDirectoryPattern);
33 for (base::FilePath hwmon_path = hwmon_enumerator.Next(); !hwmon_path.empty();
34 hwmon_path = hwmon_enumerator.Next()) {
35 // Get temp*_input files in hwmon*/ and hwmon*/device/
36 if (base::PathExists(hwmon_path.Append(kDeviceDir))) {
37 hwmon_path = hwmon_path.Append(kDeviceDir);
38 }
39 base::FileEnumerator enumerator(
40 hwmon_path, false, base::FileEnumerator::FILES, kCPUTempFilePattern);
41 for (base::FilePath temperature_path = enumerator.Next();
42 !temperature_path.empty(); temperature_path = enumerator.Next()) {
43 // Get appropriate temp*_label file.
44 std::string label_path = temperature_path.MaybeAsASCII();
45 if (label_path.empty()) {
46 LOG(WARNING) << "Unable to parse a path to temp*_input file as ASCII";
47 continue;
48 }
stevenjb 2017/04/14 15:44:04 This would be a bit more readable if some or all o
Simon Que 2017/04/17 16:41:07 Done.
49 base::ReplaceSubstringsAfterOffset(&label_path, 0, "input", "label");
50 base::FilePath name_path = hwmon_path.Append("name");
51
52 // Get the label describing this temperature. Use temp*_label
53 // if present, fall back on name file or blank.
54 std::string label;
55 if (base::PathExists(base::FilePath(label_path))) {
56 base::ReadFileToString(base::FilePath(label_path), &label);
57 } else if (base::PathExists(base::FilePath(name_path))) {
58 base::ReadFileToString(name_path, &label);
59 } else {
60 label = std::string();
61 }
62
63 // Read temperature in millidegree Celsius.
64 std::string temperature_string;
65 uint32_t temperature = 0;
66 if (base::ReadFileToString(temperature_path, &temperature_string) &&
67 base::StringToUint(temperature_string, &temperature)) {
68 CPUTemperatureInfo info;
69 info.temp_celsius = static_cast<double>(temperature) / 1000;
70 info.label = label;
71 result.push_back(info);
Daniel Erat 2017/04/14 18:53:32 i think, but am not certain, that you might just b
Simon Que 2017/04/17 16:41:07 No longer applicable due to refactoring.
72 } else {
73 LOG(WARNING) << "Unable to read CPU temp from "
74 << temperature_path.MaybeAsASCII();
75 }
76 }
77 }
78 return result;
79 }
80
81 } // namespace system
82 } // namespace chromeos
OLDNEW
« chromeos/system/cpu_temp_reader.h ('K') | « chromeos/system/cpu_temp_reader.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698