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

Unified Diff: chromeos/system/cpu_temperature_reader.cc

Issue 2823583002: chromeos: Add CPU temperature reader (Closed)
Patch Set: Make runnable on main UI thread 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 side-by-side diff with in-line comments
Download patch
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..94fd0fda77db7d2bcb8359dc284067e97d3c3950
--- /dev/null
+++ b/chromeos/system/cpu_temperature_reader.cc
@@ -0,0 +1,107 @@
+// 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 {
+
+namespace {
+
+// The location we read our CPU temperature and channel label from.
+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.
+const char kDeviceDir[] = "device";
+const char kHwmonDirectoryPattern[] = "hwmon*";
+const char kCPUTempFilePattern[] = "temp*_input";
+
+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.
+
+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.
+ std::string temperature_string;
+ uint32_t temperature = 0;
+ if (base::ReadFileToString(path, &temperature_string) &&
+ base::StringToUint(temperature_string, &temperature)) {
+ *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.
+ return true;
+ }
+ return false;
+}
+
+std::string GetLabelFromPath(const base::FilePath& label_path) {
+ // Get the label describing this temperature. Use temp*_label
+ // if present, fall back on name file or blank.
+ std::string label;
+ if (base::PathExists(base::FilePath(label_path))) {
+ base::ReadFileToString(base::FilePath(label_path), &label);
+ return label;
+ }
+
+ base::FilePath name_path = label_path.DirName().Append("name");
+ if (base::PathExists(base::FilePath(name_path))) {
+ base::ReadFileToString(name_path, &label);
+ }
+ 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.
+}
+
+std::vector<CPUTemperatureInfo> ReadCPUTemperatures(
+ const std::string& hwmon_dir) {
+ std::vector<CPUTemperatureInfo> result;
+
+ // 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.
+ 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/
Daniel Erat 2017/04/20 21:23:52 nit: add trailing period
Simon Que 2017/04/21 08:52:25 Done.
+ 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();
+ 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
+
+CPUTemperatureReader::CPUTemperatureReader() : hwmon_dir_(kDefaultHwmonDir) {}
+
+void CPUTemperatureReader::GetCPUTemperatures(
+ const GetCPUTemperaturesCallback& callback) {
+ base::PostTaskWithTraitsAndReplyWithResult(
+ FROM_HERE,
+ base::TaskTraits().MayBlock().WithPriority(
+ base::TaskPriority::BACKGROUND),
+ base::Bind(&ReadCPUTemperatures, hwmon_dir_), callback);
+}
+
+} // namespace system
+} // namespace chromeos

Powered by Google App Engine
This is Rietveld 408576698