Debian Codegraph @

tests/src/test_amdpmpowerprofile.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 "common/stringdatasourcestub.h"
9 #include "core/components/controls/amd/pm/advanced/powerprofile/pmpowerprofile.h"
10 #include "core/idatasource.h"
11
12 extern template struct trompeloeil::reporter<trompeloeil::specialized>;
13
14 namespace Tests::AMD::PMPowerProfile {
15
16 class PMPowerProfileTestAdapter : public ::AMD::PMPowerProfile
17 {
18 public:
19 using ::AMD::PMPowerProfile::PMPowerProfile;
20
21 using PMPowerProfile::cleanControl;
22 using PMPowerProfile::exportControl;
23 using PMPowerProfile::importControl;
24 using PMPowerProfile::mode;
25 using PMPowerProfile::modes;
26 using PMPowerProfile::syncControl;
27 };
28
29 class PMPowerProfileImporterStub : public ::AMD::PMPowerProfile::Importer
30 {
31 public:
32 PMPowerProfileImporterStub(std::string mode)
33 : mode_(std::move(mode))
34 {
35 }
36
37 std::optional<std::reference_wrapper<Importable::Importer>>
38 provideImporter(Item const &) override
39 {
40 return {};
41 }
42
43 bool provideActive() const override
44 {
45 return false;
46 }
47
48 std::string const &providePMPowerProfileMode() const override
49 {
50 return mode_;
51 }
52
53 private:
54 std::string mode_;
55 };
56
57 class PMPowerProfileExporterMock : public ::AMD::PMPowerProfile::Exporter
58 {
59 public:
60 MAKE_MOCK1(takePMPowerProfileModes, void(std::vector<std::string> const &),
61 override);
62 MAKE_MOCK1(takePMPowerProfileMode, void(std::string const &), override);
63 MAKE_MOCK1(takeActive, void(bool), override);
64 MAKE_MOCK1(
65 provideExporter,
66 std::optional<std::reference_wrapper<Exportable::Exporter>>(Item const &),
67 override);
68 };
69
70 class OptionalIntDataSourceStub : public IDataSource<std::optional<int>>
71 {
72 public:
73 OptionalIntDataSourceStub(std::string_view source = "",
74 std::optional<int> data = std::nullopt,
75 bool success = true) noexcept
76 : source_(source)
77 , data_(data)
78 , success_(success)
79 {
80 }
81
82 std::string source() const override
83 {
84 return source_;
85 }
86
87 bool read(std::optional<int> &data) override
88 {
89 data = data_;
90 return success_;
91 }
92
93 std::string const source_;
94 std::optional<int> const data_;
95 bool success_;
96 };
97
98 TEST_CASE("AMD PMPowerProfile tests",
99 "[GPU][AMD][PM][PMAdvanced][PMPowerProfile]")
100 {
101 CommandQueueStub ctlCmds;
102 std::vector<std::pair<std::string, int>> modes{
103 std::make_pair("3D_FULL_SCREEN", 0), std::make_pair("POWER_SAVING", 1)};
104
105 SECTION("Has PMPowerProfile ID")
106 {
107 PMPowerProfileTestAdapter ts(std::make_unique<StringDataSourceStub>(),
108 std::make_unique<OptionalIntDataSourceStub>(),
109 modes);
110 REQUIRE(ts.ID() == ::AMD::PMPowerProfile::ItemID);
111 }
112
113 SECTION("Is active by default")
114 {
115 PMPowerProfileTestAdapter ts(std::make_unique<StringDataSourceStub>(),
116 std::make_unique<OptionalIntDataSourceStub>(),
117 modes);
118 REQUIRE(ts.active());
119 }
120
121 SECTION("Has first mode selected by default")
122 {
123 PMPowerProfileTestAdapter ts(
124 std::make_unique<StringDataSourceStub>(),
125 std::make_unique<OptionalIntDataSourceStub>("pp_power_profile_mode",
126 std::make_optional(0)),
127 modes);
128 REQUIRE(ts.mode() == "3D_FULL_SCREEN");
129 }
130
131 SECTION("mode ignores unknown modes")
132 {
133 PMPowerProfileTestAdapter ts(
134 std::make_unique<StringDataSourceStub>(),
135 std::make_unique<OptionalIntDataSourceStub>("pp_power_profile_mode",
136 std::make_optional(0)),
137 modes);
138
139 ts.mode("UNKNOWN");
140 REQUIRE(ts.mode() == "3D_FULL_SCREEN");
141 }
142
143 SECTION("Does not generate pre-init control commands")
144 {
145 PMPowerProfileTestAdapter ts(std::make_unique<StringDataSourceStub>(),
146 std::make_unique<OptionalIntDataSourceStub>(),
147 modes);
148 ts.preInit(ctlCmds);
149 REQUIRE(ctlCmds.commands().empty());
150 }
151
152 SECTION("Does not generate post-init control commands")
153 {
154 PMPowerProfileTestAdapter ts(std::make_unique<StringDataSourceStub>(),
155 std::make_unique<OptionalIntDataSourceStub>(),
156 modes);
157 ts.postInit(ctlCmds);
158 REQUIRE(ctlCmds.commands().empty());
159 }
160
161 SECTION("Import its mode")
162 {
163 PMPowerProfileTestAdapter ts(
164 std::make_unique<StringDataSourceStub>(),
165 std::make_unique<OptionalIntDataSourceStub>("pp_power_profile_mode",
166 std::make_optional(0)),
167 modes);
168 ts.init();
169 PMPowerProfileImporterStub i("POWER_SAVING");
170 ts.importControl(i);
171
172 REQUIRE(ts.mode() == "POWER_SAVING");
173 }
174
175 SECTION("Export its mode and available modes")
176 {
177 PMPowerProfileTestAdapter ts(
178 std::make_unique<StringDataSourceStub>(),
179 std::make_unique<OptionalIntDataSourceStub>("pp_power_profile_mode",
180 std::make_optional(0)),
181 modes);
182 ts.init();
183
184 trompeloeil::sequence seq;
185 PMPowerProfileExporterMock e;
186 REQUIRE_CALL(e, takePMPowerProfileModes(trompeloeil::_)).IN_SEQUENCE(seq);
187 REQUIRE_CALL(e, takePMPowerProfileMode(trompeloeil::eq("3D_FULL_SCREEN")))
188 .IN_SEQUENCE(seq);
189
190 ts.exportControl(e);
191 }
192
193 SECTION("Generate clean control commands...")
194 {
195 SECTION("Including performance level command when its value is not manual")
196 {
197 PMPowerProfileTestAdapter ts(
198 std::make_unique<StringDataSourceStub>(
199 "power_dpm_force_performance_level", "auto"),
200 std::make_unique<OptionalIntDataSourceStub>("pp_power_profile_mode",
201 std::make_optional(0)),
202 modes);
203 ts.init();
204 ts.cleanControl(ctlCmds);
205
206 auto &commands = ctlCmds.commands();
207 REQUIRE(commands.size() == 2);
208
209 auto &[cmd0Path, cmd0Value] = commands[0];
210 REQUIRE(cmd0Path == "power_dpm_force_performance_level");
211 REQUIRE(cmd0Value == "manual");
212
213 auto &[cmd1Path, cmd1Value] = commands[1];
214 REQUIRE(cmd1Path == "pp_power_profile_mode");
215 REQUIRE(cmd1Value == "0");
216 }
217
218 SECTION("Excluding performance level command when its value is manual")
219 {
220 PMPowerProfileTestAdapter ts(
221 std::make_unique<StringDataSourceStub>(
222 "power_dpm_force_performance_level", "manual"),
223 std::make_unique<OptionalIntDataSourceStub>("pp_power_profile_mode",
224 std::make_optional(0)),
225 modes);
226 ts.init();
227 ts.cleanControl(ctlCmds);
228
229 auto &commands = ctlCmds.commands();
230 REQUIRE(commands.size() == 1);
231
232 auto &[cmd0Path, cmd0Value] = commands[0];
233 REQUIRE(cmd0Path == "pp_power_profile_mode");
234 REQUIRE(cmd0Value == "0");
235 }
236 }
237
238 SECTION("Does not generate sync control commands when is synced")
239 {
240 PMPowerProfileTestAdapter ts(
241 std::make_unique<StringDataSourceStub>(
242 "power_dpm_force_performance_level", "manual"),
243 std::make_unique<OptionalIntDataSourceStub>("pp_power_profile_mode",
244 std::make_optional(1)),
245 modes);
246 ts.init();
247 ts.mode("POWER_SAVING");
248 ts.syncControl(ctlCmds);
249
250 REQUIRE(ctlCmds.commands().empty());
251 }
252
253 SECTION("Does generate sync control commands when...")
254 {
255 SECTION("power_dpm_force_performance_level is out of sync")
256 {
257 PMPowerProfileTestAdapter ts(
258 std::make_unique<StringDataSourceStub>(
259 "power_dpm_force_performance_level", "auto"),
260 std::make_unique<OptionalIntDataSourceStub>("pp_power_profile_mode",
261 std::make_optional(0)),
262 modes);
263 ts.init();
264 ts.mode("POWER_SAVING");
265 ts.syncControl(ctlCmds);
266
267 auto &commands = ctlCmds.commands();
268 REQUIRE(commands.size() == 2);
269
270 auto &[cmd0Path, cmd0Value] = commands[0];
271 REQUIRE(cmd0Path == "power_dpm_force_performance_level");
272 REQUIRE(cmd0Value == "manual");
273
274 auto &[cmd1Path, cmd1Value] = commands[1];
275 REQUIRE(cmd1Path == "pp_power_profile_mode");
276 REQUIRE(cmd1Value == "1");
277 }
278
279 SECTION("pp_power_profile_mode is out of sync")
280 {
281 PMPowerProfileTestAdapter ts(
282 std::make_unique<StringDataSourceStub>(
283 "power_dpm_force_performance_level", "manual"),
284 std::make_unique<OptionalIntDataSourceStub>("pp_power_profile_mode",
285 std::make_optional(1)),
286 modes);
287 ts.init();
288 ts.syncControl(ctlCmds);
289
290 REQUIRE(ctlCmds.commands().size() == 1);
291
292 auto &[cmdPath, cmdValue] = ctlCmds.commands().front();
293 REQUIRE(cmdPath == "pp_power_profile_mode");
294 REQUIRE(cmdValue == "0");
295 }
296 }
297 }
298
299 } // namespace Tests::AMD::PMPowerProfile