cpu_temperature_reader.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. #ifndef CHROMEOS_SYSTEM_CPU_TEMPERATURE_READER_H_
  5. #define CHROMEOS_SYSTEM_CPU_TEMPERATURE_READER_H_
  6. #include <string>
  7. #include <utility>
  8. #include <vector>
  9. #include "base/component_export.h"
  10. #include "base/files/file_path.h"
  11. namespace chromeos {
  12. namespace system {
  13. // Used to read CPU temperature info from sysfs hwmon.
  14. class COMPONENT_EXPORT(CHROMEOS_SYSTEM) CPUTemperatureReader {
  15. public:
  16. // Contains info from a CPU temperature sensor.
  17. struct CPUTemperatureInfo {
  18. CPUTemperatureInfo();
  19. ~CPUTemperatureInfo();
  20. bool operator<(const CPUTemperatureInfo& other) const {
  21. return std::make_pair(label, temp_celsius) <
  22. std::make_pair(other.label, other.temp_celsius);
  23. }
  24. // The temperature read by a CPU temperature sensor in degrees Celsius.
  25. double temp_celsius;
  26. // The name of the CPU temperature zone monitored by this sensor. Used to
  27. // identify the source of each temperature reading. Taken from sysfs "name"
  28. // or "label" field, if it exists.
  29. std::string label;
  30. };
  31. CPUTemperatureReader();
  32. CPUTemperatureReader(const CPUTemperatureReader&) = delete;
  33. CPUTemperatureReader& operator=(const CPUTemperatureReader&) = delete;
  34. ~CPUTemperatureReader();
  35. void set_hwmon_dir_for_test(const base::FilePath& dir) { hwmon_dir_ = dir; }
  36. // Reads temperature from each thermal sensor of the CPU. Returns a vector
  37. // containing a reading from each sensor. This is a blocking function that
  38. // should be run on a thread that allows blocking operations.
  39. std::vector<CPUTemperatureInfo> GetCPUTemperatures();
  40. private:
  41. // Sysfs hwmon directory path.
  42. base::FilePath hwmon_dir_;
  43. };
  44. } // namespace system
  45. } // namespace chromeos
  46. #endif // CHROMEOS_SYSTEM_CPU_TEMPERATURE_READER_H_