Debian Codegraph @

tests/src/test_amdpmpowercap.cpp

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 // Copyright 2019 Juan Palacios <[email protected]>
3
4 #include <catch2/catch_all.hpp>
5 #include <catch2/trompeloeil.hpp>
6
7 #include "common/commandqueuestub.h"
8 #include "core/components/controls/amd/pm/advanced/powercap/pmpowercap.h"
9 #include "core/idatasource.h"
10
11 extern template struct trompeloeil::reporter<trompeloeil::specialized>;
12
13 namespace Tests::AMD::PMPowerCap {
14
15 class ULongDataSourceStub : public IDataSource<unsigned long>
16 {
17 public:
18 ULongDataSourceStub(std::string_view source = "", unsigned long data = 0,
19 bool success = true) noexcept
20 : source_(source)
21 , data_(data)
22 , success_(success)
23 {
24 }
25
26 std::string source() const override
27 {
28 return source_;
29 }
30
31 bool read(unsigned long &data) override
32 {
33 data = data_;
34 return success_;
35 }
36
37 void data(unsigned long data)
38 {
39 data_ = data;
40 }
41
42 std::string const source_;
43 unsigned long data_;
44 bool success_;
45 };
46
47 class PMPowerCapTestAdapter : public ::AMD::PMPowerCap
48 {
49 public:
50 using ::AMD::PMPowerCap::PMPowerCap;
51
52 using ::AMD::PMPowerCap::cleanControl;
53 using ::AMD::PMPowerCap::exportControl;
54 using ::AMD::PMPowerCap::importControl;
55 using ::AMD::PMPowerCap::max;
56 using ::AMD::PMPowerCap::min;
57 using ::AMD::PMPowerCap::syncControl;
58 using ::AMD::PMPowerCap::value;
59 };
60
61 class PMPowerCapImporterStub : public ::AMD::PMPowerCap::Importer
62 {
63 public:
64 PMPowerCapImporterStub(units::power::watt_t value)
65 : value_(value)
66 {
67 }
68
69 std::optional<std::reference_wrapper<Importable::Importer>>
70 provideImporter(Item const &) override
71 {
72 return *this;
73 }
74
75 bool provideActive() const override
76 {
77 return false;
78 }
79
80 units::power::watt_t providePMPowerCapValue() const override
81 {
82 return value_;
83 }
84
85 private:
86 units::power::watt_t value_;
87 };
88
89 class PMPowerCapExporterMock : public ::AMD::PMPowerCap::Exporter
90 {
91 public:
92 MAKE_MOCK1(takePMPowerCapValue, void(units::power::watt_t), override);
93 MAKE_MOCK2(takePMPowerCapRange,
94 void(units::power::watt_t, units::power::watt_t), override);
95 MAKE_MOCK1(takeActive, void(bool), override);
96 MAKE_MOCK1(
97 provideExporter,
98 std::optional<std::reference_wrapper<Exportable::Exporter>>(Item const &),
99 override);
100 };
101
102 TEST_CASE("AMD PMPowerCap tests", "[GPU][AMD][PM][PMPowerCap]")
103 {
104 CommandQueueStub ctlCmds;
105 units::power::watt_t min(50);
106 units::power::watt_t max(100);
107
108 SECTION("Has PMPowerCap ID")
109 {
110 PMPowerCapTestAdapter ts(std::make_unique<ULongDataSourceStub>(), min, max);
111
112 REQUIRE(ts.ID() == ::AMD::PMPowerCap::ItemID);
113 }
114
115 SECTION("Is active by default")
116 {
117 PMPowerCapTestAdapter ts(std::make_unique<ULongDataSourceStub>(), min, max);
118
119 REQUIRE(ts.active());
120 }
121
122 SECTION("Has 1 watt as minimum range value when 0 watt is supplied as the "
123 "minimum value at construction")
124 { // NOTE 0 watt is reserved for reseting the value
125 PMPowerCapTestAdapter ts(std::make_unique<ULongDataSourceStub>(),
126 units::power::watt_t(0), max);
127
128 REQUIRE(ts.min() == units::power::watt_t(1));
129 }
130
131 SECTION("Clamp its value in the [min, max] range")
132 {
133 PMPowerCapTestAdapter ts(
134 std::make_unique<ULongDataSourceStub>("power1_cap", 50000000), min, max);
135
136 ts.value(units::power::watt_t(0));
137 REQUIRE(ts.value() == min);
138
139 ts.value(units::power::watt_t(500));
140 REQUIRE(ts.value() == max);
141 }
142
143 SECTION("Generate pre-init control commands when default power cap is not "
144 "available")
145 {
146 PMPowerCapTestAdapter ts(
147 std::make_unique<ULongDataSourceStub>("power1_cap", 10000000), min, max);
148 ts.preInit(ctlCmds);
149
150 auto &commands = ctlCmds.commands();
151 REQUIRE(commands.size() == 1);
152
153 auto &[cmdPath, cmdValue] = commands.at(0);
154 REQUIRE(cmdPath == "power1_cap");
155 REQUIRE(cmdValue == "0");
156 }
157
158 SECTION("Does not generate pre-init control commands when default power cap "
159 "is available")
160 {
161 PMPowerCapTestAdapter ts(
162 std::make_unique<ULongDataSourceStub>("power1_cap", 10000000), min, max,
163 units::power::watt_t(75));
164 ts.preInit(ctlCmds);
165
166 auto &commands = ctlCmds.commands();
167 REQUIRE(commands.empty());
168 }
169
170 SECTION("Generate post-init control commands when default power cap is not "
171 "available")
172 {
173 PMPowerCapTestAdapter ts(
174 std::make_unique<ULongDataSourceStub>("power1_cap", 10000000), min, max);
175 ts.preInit(ctlCmds);
176 ctlCmds.clear();
177 ts.postInit(ctlCmds);
178
179 auto &commands = ctlCmds.commands();
180 REQUIRE(commands.size() == 1);
181
182 auto &[cmdPath, cmdValue] = commands.at(0);
183 REQUIRE(cmdPath == "power1_cap");
184 REQUIRE(cmdValue == "10000000"); // restore pre-init value
185 }
186
187 SECTION("Does not generate post-init control commands when default power cap "
188 "is available")
189 {
190 PMPowerCapTestAdapter ts(
191 std::make_unique<ULongDataSourceStub>("power1_cap", 10000000), min, max,
192 units::power::watt_t(75));
193 ts.postInit(ctlCmds);
194
195 auto &commands = ctlCmds.commands();
196 REQUIRE(commands.empty());
197 }
198
199 SECTION("Power cap has the value of the default power cap when the later is "
200 "available")
201 {
202 PMPowerCapTestAdapter ts(
203 std::make_unique<ULongDataSourceStub>("power1_cap", 50000000), min, max,
204 units::power::watt_t(75));
205
206 REQUIRE(ts.value() == units::power::watt_t(75));
207 }
208
209 SECTION("Initializes power cap value from power1_cap data source when "
210 "default power cap is not available")
211 {
212 PMPowerCapTestAdapter ts(
213 std::make_unique<ULongDataSourceStub>("power1_cap", 50000000), min, max);
214 ts.init();
215
216 REQUIRE(ts.value() == units::power::watt_t(50));
217 }
218
219 SECTION("Imports its state")
220 {
221 PMPowerCapTestAdapter ts(
222 std::make_unique<ULongDataSourceStub>("power1_cap"), min, max);
223
224 PMPowerCapImporterStub i(units::power::watt_t(80));
225 ts.importWith(i);
226
227 REQUIRE(ts.value() == units::power::watt_t(80));
228 }
229
230 SECTION("Export its state")
231 {
232 PMPowerCapTestAdapter ts(
233 std::make_unique<ULongDataSourceStub>("power1_cap"), min, max);
234
235 units::power::watt_t value(80);
236 ts.value(value);
237
238 trompeloeil::sequence seq;
239 PMPowerCapExporterMock e;
240 REQUIRE_CALL(e, takePMPowerCapRange(trompeloeil::_, trompeloeil::_))
241 .LR_WITH(_1 == min)
242 .LR_WITH(_2 == max)
243 .IN_SEQUENCE(seq);
244 REQUIRE_CALL(e, takePMPowerCapValue(trompeloeil::_))
245 .LR_WITH(_1 == value)
246 .IN_SEQUENCE(seq);
247
248 ts.exportControl(e);
249 }
250
251 SECTION("Generate clean control commands...")
252 {
253 SECTION(
254 "To restore the default value when default power cap is not available")
255 {
256 PMPowerCapTestAdapter ts(
257 std::make_unique<ULongDataSourceStub>("power1_cap"), min, max);
258
259 ts.cleanControl(ctlCmds);
260
261 auto &commands = ctlCmds.commands();
262 REQUIRE(commands.size() == 1);
263
264 auto &[cmdPath, cmdValue] = commands.at(0);
265 REQUIRE(cmdPath == "power1_cap");
266 REQUIRE(cmdValue == "0");
267 }
268
269 SECTION(
270 "Assigning the default power value when default power cap is available")
271 {
272 units::power::watt_t defaultValue(75);
273 PMPowerCapTestAdapter ts(
274 std::make_unique<ULongDataSourceStub>("power1_cap"), min, max,
275 defaultValue);
276
277 ts.cleanControl(ctlCmds);
278
279 auto &commands = ctlCmds.commands();
280 REQUIRE(commands.size() == 1);
281
282 auto &[cmdPath, cmdValue] = commands.at(0);
283 REQUIRE(cmdPath == "power1_cap");
284 REQUIRE(cmdValue == "75000000");
285 }
286 }
287
288 SECTION("Does not generate sync control commands when is synced")
289 {
290 PMPowerCapTestAdapter ts(
291 std::make_unique<ULongDataSourceStub>("power1_cap", 50000000), min, max);
292 ts.init();
293
294 ts.syncControl(ctlCmds);
295 REQUIRE(ctlCmds.commands().empty());
296 }
297
298 SECTION("Does generate sync control commands when is out of sync")
299 {
300 PMPowerCapTestAdapter ts(
301 std::make_unique<ULongDataSourceStub>("power1_cap", 50000000), min, max);
302 ts.init();
303 ts.value(units::power::watt_t(80));
304
305 ts.sync(ctlCmds);
306
307 auto &commands = ctlCmds.commands();
308 REQUIRE(commands.size() == 1);
309
310 auto &[cmdPath, cmdValue] = commands.at(0);
311 REQUIRE(cmdPath == "power1_cap");
312 REQUIRE(cmdValue == "80000000");
313 }
314 }
315
316 } // namespace Tests::AMD::PMPowerCap