File: cpu_temperature_reader.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (126 lines) | stat: -rw-r--r-- 4,988 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright 2017 The Chromium Authors
// 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/logging.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/task/task_traits.h"

namespace chromeos {
namespace system {

using CPUTemperatureInfo = CPUTemperatureReader::CPUTemperatureInfo;

namespace {

// The location we read our CPU temperature and channel label from.
constexpr char kDefaultHwmonDir[] = "/sys/class/hwmon/";
constexpr char kDeviceDir[] = "device";
constexpr char kHwmonDirectoryPattern[] = "hwmon*";
constexpr char kCPUTempFilePattern[] = "temp*_input";

// The contents of sysfs files might contain a newline at the end. Use this
// function to read from a sysfs file and remove the newline.
bool ReadFileContentsAndTrimWhitespace(const base::FilePath& path,
                                       std::string* contents_out) {
  if (!base::ReadFileToString(path, contents_out))
    return false;
  base::TrimWhitespaceASCII(*contents_out, base::TRIM_TRAILING, contents_out);
  return true;
}

// Reads the temperature as degrees Celsius from the file at |path|, which is
// expected to contain the temperature in millidegrees Celsius. Converts the
// value into degrees and then stores it in |*temp_celsius_out|. returns true if
// the value was successfully read from file, parsed as an int, and converted.
bool ReadTemperatureFromPath(const base::FilePath& path,
                             double* temp_celsius_out) {
  std::string temperature_string;
  if (!ReadFileContentsAndTrimWhitespace(path, &temperature_string))
    return false;
  uint32_t temperature = 0;
  if (!base::StringToUint(temperature_string, &temperature))
    return false;
  *temp_celsius_out = temperature / 1000.0;
  return true;
}

// Gets the label describing this temperature. Use the file "temp*_label" if it
// is present, or fall back on the file "name" or |label_path|.
std::string GetLabelFromPath(const base::FilePath& label_path) {
  std::string label;
  if (base::PathExists(label_path) &&
      ReadFileContentsAndTrimWhitespace(base::FilePath(label_path), &label) &&
      !label.empty()) {
    return label;
  }

  base::FilePath name_path = label_path.DirName().Append("name");
  if (base::PathExists(name_path) &&
      ReadFileContentsAndTrimWhitespace(name_path, &label) && !label.empty()) {
    return label;
  }
  return label_path.value();
}

void ReadTemperaturesFromDirectory(const base::FilePath& hwmon_path,
                                   std::vector<CPUTemperatureInfo>* result) {
  base::FileEnumerator enumerator(
      hwmon_path, false, base::FileEnumerator::FILES, kCPUTempFilePattern);
  for (base::FilePath temperature_path = enumerator.Next();
       !temperature_path.empty(); temperature_path = enumerator.Next()) {
    CPUTemperatureInfo info;
    if (!ReadTemperatureFromPath(temperature_path, &info.temp_celsius)) {
      DLOG(WARNING) << "Unable to read CPU temperature from "
                    << temperature_path.value();
      continue;
    }
    // Get appropriate temp*_label file.
    std::string label_path = temperature_path.value();
    base::ReplaceSubstringsAfterOffset(&label_path, 0, "input", "label");
    info.label = GetLabelFromPath(base::FilePath(label_path));
    result->push_back(info);
  }
}

}  // namespace

CPUTemperatureReader::CPUTemperatureInfo::CPUTemperatureInfo() = default;

CPUTemperatureReader::CPUTemperatureInfo::~CPUTemperatureInfo() = default;

CPUTemperatureReader::CPUTemperatureReader() : hwmon_dir_(kDefaultHwmonDir) {}

CPUTemperatureReader::~CPUTemperatureReader() = default;

std::vector<CPUTemperatureInfo> CPUTemperatureReader::GetCPUTemperatures() {
  std::vector<CPUTemperatureInfo> result;

  // Get directories /sys/class/hwmon/hwmon*.
  base::FileEnumerator hwmon_enumerator(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/. A survey of DUTs
    // suggests that temp values are contained in hwmon*/device on kernel 3.14
    // and earlier and directly in hwmon*/ in kernel 3.18 and later. When no
    // device subdirectory is present, the temp values are always contained
    // directly within the hwmon directory.
    ReadTemperaturesFromDirectory(hwmon_path, &result);
    if (base::PathExists(hwmon_path.Append(kDeviceDir))) {
      ReadTemperaturesFromDirectory(hwmon_path.Append(kDeviceDir), &result);
    }
  }
  return result;
}

}  // namespace system
}  // namespace chromeos