Debian Codegraph @

src/core/components/sensors/amd/fanspeedrpm.cpp

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 // Copyright 2019 Juan Palacios <[email protected]>
3
4 #include "fanspeedrpm.h"
5
6 #include "../gpusensorprovider.h"
7 #include "../graphitemprofilepart.h"
8 #include "../graphitemxmlparser.h"
9 #include "../sensor.h"
10 #include "common/fileutils.h"
11 #include "common/stringutils.h"
12 #include "core/info/igpuinfo.h"
13 #include "core/info/vendor.h"
14 #include "core/iprofilepart.h"
15 #include "core/iprofilepartxmlparser.h"
16 #include "core/profilepartprovider.h"
17 #include "core/profilepartxmlparserprovider.h"
18 #include "core/sysfsdatasource.h"
19 #include <filesystem>
20 #include <memory>
21 #include <optional>
22 #include <spdlog/spdlog.h>
23 #include <string>
24 #include <units.h>
25 #include <utility>
26 #include <vector>
27
28 namespace AMD::FanSpeedRPM {
29
30 // NOTE FanSpeedRPM is the preferred fan speed sensor.
31 //
32 // It will be used on any system with proper fan1_input and pwm1 support
33 // (most hardware using the amdgpu driver).
34 //
35 // pwm1 sensor is used to report 0 fan speed when the fans are stopped, as
36 // fan1_input could report bad values in this case.
37 // Related bug report: https://gitlab.freedesktop.org/drm/amd/-/issues/335
38 //
39 // See #184.
40
41 class Provider final : public IGPUSensorProvider::IProvider
42 {
43 public:
44 std::vector<std::unique_ptr<ISensor>>
45 provideGPUSensors(IGPUInfo const &gpuInfo, ISWInfo const &) const override
46 {
47 std::vector<std::unique_ptr<ISensor>> sensors;
48
49 if (gpuInfo.vendor() != Vendor::AMD)
50 return {};
51
52 auto path = Utils::File::findHWMonXDirectory(gpuInfo.path().sys / "hwmon");
53 if (!path)
54 return {};
55
56 std::vector<std::string> fileLines;
57 auto fanInput = path.value() / "fan1_input";
58 if (!Utils::File::isSysFSEntryValid(fanInput))
59 return {};
60
61 unsigned int value;
62 fileLines = Utils::File::readFileLines(fanInput);
63 if (!Utils::String::toNumber<unsigned int>(value, fileLines.front())) {
64 SPDLOG_WARN("Unknown data format on {}", fanInput.string());
65 SPDLOG_DEBUG(fileLines.front());
66 return {};
67 }
68
69 auto pwm = path.value() / "pwm1";
70 if (!Utils::File::isSysFSEntryValid(pwm))
71 return {};
72
73 fileLines = Utils::File::readFileLines(pwm);
74 if (!Utils::String::toNumber<unsigned int>(value, fileLines.front())) {
75 SPDLOG_WARN("Unknown data format on {}", pwm.string());
76 SPDLOG_DEBUG(fileLines.front());
77 return {};
78 }
79
80 // fallback sensor range
81 std::optional<std::pair<units::angular_velocity::revolutions_per_minute_t,
82 units::angular_velocity::revolutions_per_minute_t>>
83 range({units::angular_velocity::revolutions_per_minute_t(0),
84 units::angular_velocity::revolutions_per_minute_t(2200)});
85
86 // when supported, read the sensor range
87 auto minPath = path.value() / "fan1_min";
88 auto maxPath = path.value() / "fan1_max";
89 if (Utils::File::isSysFSEntryValid(minPath) &&
90 Utils::File::isSysFSEntryValid(maxPath)) {
91 auto min = Utils::File::readFileLines(minPath);
92 auto max = Utils::File::readFileLines(maxPath);
93 unsigned int minValue;
94 unsigned int maxValue;
95 if (Utils::String::toNumber<unsigned int>(minValue, min.front()) &&
96 Utils::String::toNumber<unsigned int>(maxValue, max.front()))
97 if (min < max) {
98 range = {units::angular_velocity::revolutions_per_minute_t(minValue),
99 units::angular_velocity::revolutions_per_minute_t(maxValue)};
100 }
101 }
102
103 std::vector<std::unique_ptr<IDataSource<unsigned int>>> dataSources;
104 dataSources.emplace_back(std::make_unique<SysFSDataSource<unsigned int>>(
105 fanInput, [](std::string const &data, unsigned int &output) {
106 Utils::String::toNumber<unsigned int>(output, data);
107 }));
108 dataSources.emplace_back(std::make_unique<SysFSDataSource<unsigned int>>(
109 pwm, [](std::string const &data, unsigned int &output) {
110 Utils::String::toNumber<unsigned int>(output, data);
111 }));
112
113 sensors.emplace_back(
114 std::make_unique<Sensor<units::angular_velocity::revolutions_per_minute_t,
115 unsigned int>>(
116 AMD::FanSpeedRPM::ItemID, std::move(dataSources), std::move(range),
117 [](std::vector<unsigned int> const &input) {
118 return input[1] > 0 ? input[0] : 0;
119 }));
120
121 return sensors;
122 }
123 };
124
125 static bool register_()
126 {
127 GPUSensorProvider::registerProvider(
128 std::make_unique<AMD::FanSpeedRPM::Provider>());
129
130 ProfilePartProvider::registerProvider(AMD::FanSpeedRPM::ItemID, []() {
131 return std::make_unique<GraphItemProfilePart>(AMD::FanSpeedRPM::ItemID,
132 "lightskyblue");
133 });
134
135 ProfilePartXMLParserProvider::registerProvider(AMD::FanSpeedRPM::ItemID, []() {
136 return std::make_unique<GraphItemXMLParser>(AMD::FanSpeedRPM::ItemID);
137 });
138
139 return true;
140 }
141
142 static bool const registered_ = register_();
143
144 } // namespace AMD::FanSpeedRPM