Debian Codegraph @

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

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 // Copyright 2019 Juan Palacios <[email protected]>
3
4 #include "power.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/components/amdutils.h"
13 #include "core/devfsdatasource.h"
14 #include "core/info/igpuinfo.h"
15 #include "core/info/vendor.h"
16 #include "core/iprofilepart.h"
17 #include "core/iprofilepartxmlparser.h"
18 #include "core/profilepartprovider.h"
19 #include "core/profilepartxmlparserprovider.h"
20 #include "core/sysfsdatasource.h"
21 #include <filesystem>
22 #include <memory>
23 #include <optional>
24 #include <string>
25 #include <units.h>
26 #include <utility>
27 #include <vector>
28
29 namespace AMD::Power {
30
31 class Provider final : public IGPUSensorProvider::IProvider
32 {
33 public:
34 std::vector<std::unique_ptr<ISensor>>
35 provideGPUSensors(IGPUInfo const &gpuInfo, ISWInfo const &) const override
36 {
37 if (gpuInfo.vendor() != Vendor::AMD)
38 return {};
39
40 std::optional<std::vector<std::unique_ptr<IDataSource<unsigned int>>>> dataSource;
41 std::optional<std::pair<units::power::microwatt_t, units::power::microwatt_t>>
42 range;
43
44 auto hwmonPath =
45 Utils::File::findHWMonXDirectory(gpuInfo.path().sys / "hwmon");
46 if (hwmonPath) {
47 range = getRange(*hwmonPath);
48 dataSource = createHWMonDataSource(*hwmonPath);
49 }
50
51 // The hwmon data source is not available. Try the ioctl data source.
52 if (!dataSource)
53 dataSource = createIOCtlDataSource(gpuInfo);
54
55 if (!dataSource)
56 return {};
57
58 std::vector<std::unique_ptr<ISensor>> sensors;
59 sensors.emplace_back(
60 std::make_unique<Sensor<units::power::watt_t, unsigned int>>(
61 AMD::Power::ItemID, std::move(*dataSource), std::move(range)));
62
63 return sensors;
64 }
65
66 private:
67 std::optional<std::vector<std::unique_ptr<IDataSource<unsigned int>>>>
68 createIOCtlDataSource(IGPUInfo const &gpuInfo) const
69 {
70 #if defined(AMDGPU_INFO_SENSOR_GPU_AVG_POWER)
71 std::vector<std::unique_ptr<IDataSource<unsigned int>>> dataSource;
72 dataSource.emplace_back(std::make_unique<DevFSDataSource<unsigned int>>(
73 gpuInfo.path().dev, [](int fd) {
74 unsigned int value;
75 bool success = Utils::AMD::readAMDGPUInfoSensor(
76 fd, &value, AMDGPU_INFO_SENSOR_GPU_AVG_POWER);
77 return success ? value : 0;
78 }));
79 return dataSource;
80 #else
81 return {};
82 #endif
83 }
84
85 std::optional<std::vector<std::unique_ptr<IDataSource<unsigned int>>>>
86 createHWMonDataSource(std::filesystem::path const &hwmonPath) const
87 {
88 // Try average power.
89 auto powerPath = hwmonPath / "power1_average";
90 if (!Utils::File::isSysFSEntryValid(powerPath)) {
91 // Some older models don't support average power.
92 // Use instantaneous power instead.
93 powerPath = hwmonPath / "power1_input";
94 if (!Utils::File::isSysFSEntryValid(powerPath))
95 return {};
96 }
97
98 unsigned long data;
99 auto powerLines = Utils::File::readFileLines(powerPath);
100 if (!Utils::String::toNumber<unsigned long>(data, powerLines.front()))
101 return {};
102
103 std::vector<std::unique_ptr<IDataSource<unsigned int>>> dataSource;
104 dataSource.emplace_back(std::make_unique<SysFSDataSource<unsigned int>>(
105 powerPath, [](std::string const &data, unsigned int &output) {
106 unsigned int power;
107 Utils::String::toNumber<unsigned int>(power, data);
108 // Data source power unit is microwatt. Convert it to watt.
109 output = power / 1000000;
110 }));
111 return dataSource;
112 }
113
114 std::optional<std::pair<units::power::microwatt_t, units::power::microwatt_t>>
115 getRange(std::filesystem::path const &hwmonPath) const
116 {
117 auto power1CapMinPath = hwmonPath / "power1_cap_min";
118 auto power1CapMaxPath = hwmonPath / "power1_cap_max";
119 if (!(Utils::File::isSysFSEntryValid(power1CapMinPath) &&
120 Utils::File::isSysFSEntryValid(power1CapMaxPath)))
121 return {};
122
123 unsigned long power1CapMinValue = 0;
124 unsigned long power1CapMaxValue = 0;
125 auto power1CapMinLines = Utils::File::readFileLines(power1CapMinPath);
126 auto power1CapMaxLines = Utils::File::readFileLines(power1CapMaxPath);
127 if (!(Utils::String::toNumber<unsigned long>(power1CapMinValue,
128 power1CapMinLines.front()) &&
129 Utils::String::toNumber<unsigned long>(power1CapMaxValue,
130 power1CapMaxLines.front())))
131 return {};
132
133 if (power1CapMaxValue <= power1CapMinValue)
134 return {};
135
136 return std::make_optional(
137 std::make_pair(units::power::microwatt_t(power1CapMinValue),
138 units::power::microwatt_t(power1CapMaxValue)));
139 }
140 };
141
142 static bool register_()
143 {
144 GPUSensorProvider::registerProvider(std::make_unique<AMD::Power::Provider>());
145
146 ProfilePartProvider::registerProvider(AMD::Power::ItemID, []() {
147 return std::make_unique<GraphItemProfilePart>(AMD::Power::ItemID, "gold");
148 });
149
150 ProfilePartXMLParserProvider::registerProvider(AMD::Power::ItemID, []() {
151 return std::make_unique<GraphItemXMLParser>(AMD::Power::ItemID);
152 });
153
154 return true;
155 }
156
157 static bool const registered_ = register_();
158
159 } // namespace AMD::Power