| 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/cpu/cpufreq.h" |
| 10 | #include "core/components/controls/cpu/handlers/iepphandler.h" |
| 11 | |
| 12 | extern template struct trompeloeil::reporter<trompeloeil::specialized>; |
| 13 | |
| 14 | namespace Tests::CPUFreq { |
| 15 | |
| 16 | class CPUFreqTestAdapter : public ::CPUFreq |
| 17 | { |
| 18 | public: |
| 19 | using ::CPUFreq::CPUFreq; |
| 20 | |
| 21 | using ::CPUFreq::cleanControl; |
| 22 | using ::CPUFreq::exportControl; |
| 23 | using ::CPUFreq::importControl; |
| 24 | using ::CPUFreq::scalingGovernor; |
| 25 | using ::CPUFreq::scalingGovernors; |
| 26 | using ::CPUFreq::syncControl; |
| 27 | }; |
| 28 | |
| 29 | class CPUFreqImporterStub : public ::CPUFreq::Importer |
| 30 | { |
| 31 | public: |
| 32 | CPUFreqImporterStub(std::string_view scalingGovernor, |
| 33 | std::optional<std::string> eppHint = std::nullopt) |
| 34 | : scalingGovernor_(scalingGovernor) |
| 35 | , eppHint_(eppHint) |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | std::optional<std::reference_wrapper<Importable::Importer>> |
| 40 | provideImporter(Item const &) override |
| 41 | { |
| 42 | return {}; |
| 43 | } |
| 44 | |
| 45 | bool provideActive() const override |
| 46 | { |
| 47 | return false; |
| 48 | } |
| 49 | |
| 50 | std::string const &provideCPUFreqScalingGovernor() const override |
| 51 | { |
| 52 | return scalingGovernor_; |
| 53 | } |
| 54 | |
| 55 | std::optional<std::string> const &provideCPUFreqEPPHint() const override |
| 56 | { |
| 57 | return eppHint_; |
| 58 | } |
| 59 | |
| 60 | private: |
| 61 | std::string scalingGovernor_; |
| 62 | std::optional<std::string> eppHint_; |
| 63 | }; |
| 64 | |
| 65 | class EPPHandlerMock : public IEPPHandler |
| 66 | { |
| 67 | public: |
| 68 | MAKE_CONST_MOCK0(hints, std::vector<std::string> const &(void), override); |
| 69 | MAKE_CONST_MOCK0(hint, std::string const &(), override); |
| 70 | MAKE_MOCK1(hint, void(std::string const &), override); |
| 71 | MAKE_MOCK0(init, void(), override); |
| 72 | MAKE_MOCK0(saveState, void(), override); |
| 73 | MAKE_MOCK1(restoreState, void(ICommandQueue &), override); |
| 74 | MAKE_MOCK1(reset, void(ICommandQueue &), override); |
| 75 | MAKE_MOCK1(sync, void(ICommandQueue &), override); |
| 76 | }; |
| 77 | |
| 78 | class CPUFreqImporterMock : public ::CPUFreq::Importer |
| 79 | { |
| 80 | public: |
| 81 | MAKE_CONST_MOCK0(provideCPUFreqScalingGovernor, std::string const &(void), |
| 82 | override); |
| 83 | MAKE_CONST_MOCK0(provideCPUFreqEPPHint, |
| 84 | std::optional<std::string> const &(void), override); |
| 85 | MAKE_CONST_MOCK0(provideActive, bool(void), override); |
| 86 | MAKE_MOCK1( |
| 87 | provideImporter, |
| 88 | std::optional<std::reference_wrapper<Importable::Importer>>(Item const &), |
| 89 | override); |
| 90 | }; |
| 91 | |
| 92 | class CPUFreqExporterMock : public ::CPUFreq::Exporter |
| 93 | { |
| 94 | public: |
| 95 | MAKE_MOCK1(takeCPUFreqScalingGovernor, void(std::string const &), override); |
| 96 | MAKE_MOCK1(takeCPUFreqScalingGovernors, |
| 97 | void(std::vector<std::string> const &), override); |
| 98 | MAKE_MOCK1(takeCPUFreqEPPHint, void(std::optional<std::string> const &), |
| 99 | override); |
| 100 | MAKE_MOCK1(takeCPUFreqEPPHints, |
| 101 | void(std::optional<std::vector<std::string>> const &), override); |
| 102 | MAKE_MOCK1(takeActive, void(bool), override); |
| 103 | MAKE_MOCK1( |
| 104 | provideExporter, |
| 105 | std::optional<std::reference_wrapper<Exportable::Exporter>>(Item const &), |
| 106 | override); |
| 107 | }; |
| 108 | |
| 109 | TEST_CASE("AMD CPUFreq tests", "[CPU][CPUFreq]") |
| 110 | { |
| 111 | std::vector<std::string> availableGovernors{"performance", "powersave"}; |
| 112 | std::vector<std::unique_ptr<IDataSource<std::string>>> scalingGovernorDataSources; |
| 113 | std::string defaultGovernor{"powersave"}; |
| 114 | std::string const scalingGovernorPath{"scaling_governor"}; |
| 115 | CommandQueueStub ctlCmds; |
| 116 | |
| 117 | SECTION("Has CPUFreq ID") |
| 118 | { |
| 119 | CPUFreqTestAdapter ts(std::move(availableGovernors), defaultGovernor, |
| 120 | std::move(scalingGovernorDataSources)); |
| 121 | |
| 122 | REQUIRE(ts.ID() == ::CPUFreq::ItemID); |
| 123 | } |
| 124 | |
| 125 | SECTION("Is active by default") |
| 126 | { |
| 127 | CPUFreqTestAdapter ts(std::move(availableGovernors), defaultGovernor, |
| 128 | std::move(scalingGovernorDataSources)); |
| 129 | |
| 130 | REQUIRE(ts.active()); |
| 131 | } |
| 132 | |
| 133 | SECTION("Has defaultGovernor selected by default when is available") |
| 134 | { |
| 135 | CPUFreqTestAdapter ts(std::move(availableGovernors), defaultGovernor, |
| 136 | std::move(scalingGovernorDataSources)); |
| 137 | |
| 138 | REQUIRE(ts.scalingGovernor() == defaultGovernor); |
| 139 | } |
| 140 | |
| 141 | SECTION("Has the first available scaling governor selected by default when " |
| 142 | "defaultGovernor is not available") |
| 143 | { |
| 144 | std::vector<std::string> otherGovernors{"_other_0_", "_other_1_"}; |
| 145 | CPUFreqTestAdapter ts(std::move(otherGovernors), defaultGovernor, |
| 146 | std::move(scalingGovernorDataSources)); |
| 147 | |
| 148 | REQUIRE(ts.scalingGovernor() == "_other_0_"); |
| 149 | } |
| 150 | |
| 151 | SECTION("scalingGovernor only sets known scaling governors") |
| 152 | { |
| 153 | CPUFreqTestAdapter ts(std::move(availableGovernors), defaultGovernor, |
| 154 | std::move(scalingGovernorDataSources)); |
| 155 | |
| 156 | ts.scalingGovernor("performance"); |
| 157 | REQUIRE(ts.scalingGovernor() == "performance"); |
| 158 | |
| 159 | ts.scalingGovernor("unknown"); |
| 160 | REQUIRE(ts.scalingGovernor() == "performance"); |
| 161 | } |
| 162 | |
| 163 | SECTION( |
| 164 | "Does not generate pre-init control commands when EPP is not supported") |
| 165 | { |
| 166 | CPUFreqTestAdapter ts(std::move(availableGovernors), defaultGovernor, |
| 167 | std::move(scalingGovernorDataSources)); |
| 168 | ts.preInit(ctlCmds); |
| 169 | |
| 170 | REQUIRE(ctlCmds.commands().empty()); |
| 171 | } |
| 172 | |
| 173 | SECTION( |
| 174 | "Does not generate pre-init control commands when EPP is supported and " |
| 175 | "the current scaling governor is the EPP scaling governor ('powersave')") |
| 176 | { |
| 177 | auto eppHandlerMock = std::make_unique<EPPHandlerMock>(); |
| 178 | |
| 179 | scalingGovernorDataSources.emplace_back( |
| 180 | std::make_unique<StringDataSourceStub>(scalingGovernorPath, |
| 181 | "powersave")); |
| 182 | |
| 183 | CPUFreqTestAdapter ts(std::move(availableGovernors), defaultGovernor, |
| 184 | std::move(scalingGovernorDataSources), |
| 185 | std::move(eppHandlerMock)); |
| 186 | ts.preInit(ctlCmds); |
| 187 | |
| 188 | REQUIRE(ctlCmds.commands().empty()); |
| 189 | } |
| 190 | |
| 191 | SECTION("Does generate pre-init control commands to set the scaling governor " |
| 192 | "when EPP is supported and the current scaling governor is not the " |
| 193 | "EPP scaling governor ('powersave')") |
| 194 | { |
| 195 | auto eppHandlerMock = std::make_unique<EPPHandlerMock>(); |
| 196 | |
| 197 | scalingGovernorDataSources.emplace_back( |
| 198 | std::make_unique<StringDataSourceStub>(scalingGovernorPath, |
| 199 | "performance")); |
| 200 | |
| 201 | CPUFreqTestAdapter ts(std::move(availableGovernors), defaultGovernor, |
| 202 | std::move(scalingGovernorDataSources), |
| 203 | std::move(eppHandlerMock)); |
| 204 | ts.preInit(ctlCmds); |
| 205 | |
| 206 | REQUIRE(ctlCmds.commands().size() == 1); |
| 207 | |
| 208 | auto &[path, value] = ctlCmds.commands().front(); |
| 209 | REQUIRE(path == scalingGovernorPath); |
| 210 | REQUIRE(value == "powersave"); |
| 211 | } |
| 212 | |
| 213 | SECTION("Does not generate post-init control commands") |
| 214 | { |
| 215 | CPUFreqTestAdapter ts(std::move(availableGovernors), defaultGovernor, |
| 216 | std::move(scalingGovernorDataSources)); |
| 217 | ts.postInit(ctlCmds); |
| 218 | |
| 219 | REQUIRE(ctlCmds.commands().empty()); |
| 220 | } |
| 221 | |
| 222 | SECTION("Initialize EPP handler when EPP is supported") |
| 223 | { |
| 224 | auto eppHandlerMock = std::make_unique<EPPHandlerMock>(); |
| 225 | REQUIRE_CALL(*eppHandlerMock, init()); |
| 226 | |
| 227 | CPUFreqTestAdapter ts(std::move(availableGovernors), defaultGovernor, |
| 228 | std::move(scalingGovernorDataSources), |
| 229 | std::move(eppHandlerMock)); |
| 230 | ts.init(); |
| 231 | } |
| 232 | |
| 233 | SECTION("Imports scaling governor state") |
| 234 | { |
| 235 | CPUFreqTestAdapter ts(std::move(availableGovernors), defaultGovernor, |
| 236 | std::move(scalingGovernorDataSources)); |
| 237 | CPUFreqImporterStub i("performance"); |
| 238 | ts.importControl(i); |
| 239 | |
| 240 | REQUIRE(ts.scalingGovernor() == "performance"); |
| 241 | } |
| 242 | |
| 243 | SECTION("Imports EPP hint state when EPP is supported") |
| 244 | { |
| 245 | std::string eppHint{"power"}; |
| 246 | auto eppHandlerMock = std::make_unique<EPPHandlerMock>(); |
| 247 | REQUIRE_CALL(*eppHandlerMock, hint(trompeloeil::_)).LR_WITH(_1 == eppHint); |
| 248 | |
| 249 | CPUFreqTestAdapter ts(std::move(availableGovernors), defaultGovernor, |
| 250 | std::move(scalingGovernorDataSources), |
| 251 | std::move(eppHandlerMock)); |
| 252 | CPUFreqImporterStub i("performance", eppHint); |
| 253 | ts.importControl(i); |
| 254 | } |
| 255 | |
| 256 | SECTION("Export scaling governor state and available scaling governors") |
| 257 | { |
| 258 | auto governors = availableGovernors; |
| 259 | CPUFreqTestAdapter ts(std::move(availableGovernors), defaultGovernor, |
| 260 | std::move(scalingGovernorDataSources)); |
| 261 | trompeloeil::sequence seq; |
| 262 | CPUFreqExporterMock e; |
| 263 | REQUIRE_CALL(e, takeCPUFreqScalingGovernors(trompeloeil::_)) |
| 264 | .LR_WITH(_1 == governors) |
| 265 | .IN_SEQUENCE(seq); |
| 266 | REQUIRE_CALL(e, takeCPUFreqEPPHints(trompeloeil::eq(std::nullopt))) |
| 267 | .IN_SEQUENCE(seq); |
| 268 | REQUIRE_CALL(e, takeCPUFreqScalingGovernor(trompeloeil::eq(defaultGovernor))) |
| 269 | .IN_SEQUENCE(seq); |
| 270 | REQUIRE_CALL(e, takeCPUFreqEPPHint(trompeloeil::eq(std::nullopt))) |
| 271 | .IN_SEQUENCE(seq); |
| 272 | |
| 273 | ts.exportControl(e); |
| 274 | } |
| 275 | |
| 276 | SECTION("Export EPP hint state and available hints when EPP is supported") |
| 277 | { |
| 278 | std::vector<std::string> eppHints{"default", "power"}; |
| 279 | std::string eppHint{"default"}; |
| 280 | auto eppHandlerMock = std::make_unique<EPPHandlerMock>(); |
| 281 | ALLOW_CALL(*eppHandlerMock, hints()).RETURN(eppHints); |
| 282 | ALLOW_CALL(*eppHandlerMock, hint()).RETURN(eppHint); |
| 283 | |
| 284 | auto governors = availableGovernors; |
| 285 | CPUFreqTestAdapter ts(std::move(availableGovernors), defaultGovernor, |
| 286 | std::move(scalingGovernorDataSources), |
| 287 | std::move(eppHandlerMock)); |
| 288 | trompeloeil::sequence seq; |
| 289 | CPUFreqExporterMock e; |
| 290 | ALLOW_CALL(e, takeCPUFreqScalingGovernors(trompeloeil::_)).IN_SEQUENCE(seq); |
| 291 | REQUIRE_CALL(e, takeCPUFreqEPPHints(trompeloeil::_)) |
| 292 | .LR_WITH(_1 == eppHints) |
| 293 | .IN_SEQUENCE(seq); |
| 294 | ALLOW_CALL(e, takeCPUFreqScalingGovernor(trompeloeil::_)).IN_SEQUENCE(seq); |
| 295 | REQUIRE_CALL(e, takeCPUFreqEPPHint(trompeloeil::eq(eppHint))).IN_SEQUENCE(seq); |
| 296 | |
| 297 | ts.exportControl(e); |
| 298 | } |
| 299 | |
| 300 | SECTION("Does not generate clean control commands") |
| 301 | { |
| 302 | CPUFreqTestAdapter ts(std::move(availableGovernors), defaultGovernor, |
| 303 | std::move(scalingGovernorDataSources)); |
| 304 | ts.cleanControl(ctlCmds); |
| 305 | |
| 306 | REQUIRE(ctlCmds.commands().empty()); |
| 307 | } |
| 308 | |
| 309 | SECTION("Does not generate sync control commands when is synced") |
| 310 | { |
| 311 | scalingGovernorDataSources.emplace_back( |
| 312 | std::make_unique<StringDataSourceStub>(scalingGovernorPath, |
| 313 | defaultGovernor)); |
| 314 | CPUFreqTestAdapter ts(std::move(availableGovernors), defaultGovernor, |
| 315 | std::move(scalingGovernorDataSources)); |
| 316 | |
| 317 | ts.syncControl(ctlCmds); |
| 318 | |
| 319 | REQUIRE(ctlCmds.commands().empty()); |
| 320 | } |
| 321 | |
| 322 | SECTION("Does generate sync control commands when is out of sync") |
| 323 | { |
| 324 | scalingGovernorDataSources.emplace_back( |
| 325 | std::make_unique<StringDataSourceStub>(scalingGovernorPath, "_other_")); |
| 326 | CPUFreqTestAdapter ts(std::move(availableGovernors), defaultGovernor, |
| 327 | std::move(scalingGovernorDataSources)); |
| 328 | |
| 329 | ts.syncControl(ctlCmds); |
| 330 | |
| 331 | REQUIRE(ctlCmds.commands().size() == 1); |
| 332 | |
| 333 | auto &[path, value] = ctlCmds.commands().front(); |
| 334 | REQUIRE(path == scalingGovernorPath); |
| 335 | REQUIRE(value == defaultGovernor); |
| 336 | } |
| 337 | |
| 338 | SECTION("Does not generate sync control commands for EPP when this feature " |
| 339 | "is supported and the frequency scaling governor is not set " |
| 340 | "to 'powersave'") |
| 341 | { |
| 342 | auto eppHandlerMock = std::make_unique<EPPHandlerMock>(); |
| 343 | FORBID_CALL(*eppHandlerMock, sync(trompeloeil::_)); |
| 344 | |
| 345 | scalingGovernorDataSources.emplace_back( |
| 346 | std::make_unique<StringDataSourceStub>(scalingGovernorPath, |
| 347 | defaultGovernor)); |
| 348 | CPUFreqTestAdapter ts(std::move(availableGovernors), defaultGovernor, |
| 349 | std::move(scalingGovernorDataSources), |
| 350 | std::move(eppHandlerMock)); |
| 351 | |
| 352 | ts.scalingGovernor("performance"); |
| 353 | ts.syncControl(ctlCmds); |
| 354 | } |
| 355 | |
| 356 | SECTION("Could generate sync control commands for EPP when this feature " |
| 357 | "is supported and the frequency scaling governor is set " |
| 358 | "to 'powersave'") |
| 359 | { |
| 360 | auto eppHandlerMock = std::make_unique<EPPHandlerMock>(); |
| 361 | REQUIRE_CALL(*eppHandlerMock, sync(trompeloeil::_)); |
| 362 | |
| 363 | scalingGovernorDataSources.emplace_back( |
| 364 | std::make_unique<StringDataSourceStub>(scalingGovernorPath, |
| 365 | defaultGovernor)); |
| 366 | CPUFreqTestAdapter ts(std::move(availableGovernors), defaultGovernor, |
| 367 | std::move(scalingGovernorDataSources), |
| 368 | std::move(eppHandlerMock)); |
| 369 | |
| 370 | ts.syncControl(ctlCmds); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | } // namespace Tests::CPUFreq |