| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "cpufreq.h" |
| 5 | |
| 6 | #include "core/icommandqueue.h" |
| 7 | #include <algorithm> |
| 8 | #include <cassert> |
| 9 | #include <utility> |
| 10 | |
| 11 | CPUFreq::CPUFreq(std::vector<std::string> &&scalingGovernors, |
| 12 | std::string const &defaultGovernor, |
| 13 | std::vector<std::unique_ptr<IDataSource<std::string>>> |
| 14 | &&scalingGovernorDataSources, |
| 15 | std::unique_ptr<IEPPHandler> &&eppHandler) noexcept |
| 16 | : Control(true) |
| 17 | , id_(CPUFreq::ItemID) |
| 18 | , scalingGovernors_(std::move(scalingGovernors)) |
| 19 | , scalingGovernorDataSources_(std::move(scalingGovernorDataSources)) |
| 20 | , eppHandler_(std::move(eppHandler)) |
| 21 | { |
| 22 | scalingGovernor(defaultGovernor); |
| 23 | if (scalingGovernor_.empty()) |
| 24 | scalingGovernor(scalingGovernors_.front()); |
| 25 | } |
| 26 | |
| 27 | void CPUFreq::preInit(ICommandQueue &ctlCmds) |
| 28 | { |
| 29 | if (eppHandler_) |
| 30 | // We need to set the EPP scaling governor as the current scaling governor |
| 31 | // before the initialization of the EPP handler. |
| 32 | syncScalingGovernor(eppScalingGovernor_, ctlCmds); |
| 33 | } |
| 34 | |
| 35 | void CPUFreq::postInit(ICommandQueue &) |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | void CPUFreq::init() |
| 40 | { |
| 41 | if (eppHandler_) |
| 42 | eppHandler_->init(); |
| 43 | } |
| 44 | |
| 45 | std::string const &CPUFreq::ID() const |
| 46 | { |
| 47 | return id_; |
| 48 | } |
| 49 | |
| 50 | void CPUFreq::importControl(IControl::Importer &i) |
| 51 | { |
| 52 | auto &importer = dynamic_cast<CPUFreq::Importer &>(i); |
| 53 | scalingGovernor(importer.provideCPUFreqScalingGovernor()); |
| 54 | |
| 55 | if (eppHandler_) { |
| 56 | auto hint = importer.provideCPUFreqEPPHint(); |
| 57 | assert(hint.has_value()); |
| 58 | eppHandler_->hint(*hint); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | void CPUFreq::exportControl(IControl::Exporter &e) const |
| 63 | { |
| 64 | auto &exporter = dynamic_cast<CPUFreq::Exporter &>(e); |
| 65 | exporter.takeCPUFreqScalingGovernors(scalingGovernors()); |
| 66 | exporter.takeCPUFreqEPPHints(eppHints()); |
| 67 | exporter.takeCPUFreqScalingGovernor(scalingGovernor()); |
| 68 | exporter.takeCPUFreqEPPHint(eppHint()); |
| 69 | } |
| 70 | |
| 71 | void CPUFreq::cleanControl(ICommandQueue &) |
| 72 | { |
| 73 | } |
| 74 | |
| 75 | void CPUFreq::syncControl(ICommandQueue &ctlCmds) |
| 76 | { |
| 77 | syncScalingGovernor(scalingGovernor(), ctlCmds); |
| 78 | if (eppHandler_ && scalingGovernor() == eppScalingGovernor_) |
| 79 | eppHandler_->sync(ctlCmds); |
| 80 | } |
| 81 | |
| 82 | std::string const &CPUFreq::scalingGovernor() const |
| 83 | { |
| 84 | return scalingGovernor_; |
| 85 | } |
| 86 | |
| 87 | void CPUFreq::scalingGovernor(std::string const &scalingGovernor) |
| 88 | { |
| 89 | // only assign known scalingGovernors |
| 90 | auto iter = std::find_if(scalingGovernors().cbegin(), |
| 91 | scalingGovernors().cend(), |
| 92 | [&](auto const &availableScalingGovernor) { |
| 93 | return scalingGovernor == availableScalingGovernor; |
| 94 | }); |
| 95 | if (iter != scalingGovernors().cend()) |
| 96 | scalingGovernor_ = scalingGovernor; |
| 97 | } |
| 98 | |
| 99 | std::vector<std::string> const &CPUFreq::scalingGovernors() const |
| 100 | { |
| 101 | return scalingGovernors_; |
| 102 | } |
| 103 | |
| 104 | std::optional<std::string> CPUFreq::eppHint() const |
| 105 | { |
| 106 | return eppHandler_ ? std::make_optional(eppHandler_->hint()) : std::nullopt; |
| 107 | } |
| 108 | |
| 109 | std::optional<std::vector<std::string>> CPUFreq::eppHints() const |
| 110 | { |
| 111 | return eppHandler_ ? std::make_optional(eppHandler_->hints()) : std::nullopt; |
| 112 | } |
| 113 | |
| 114 | void CPUFreq::syncScalingGovernor(std::string const &governor, |
| 115 | ICommandQueue &ctlCmds) |
| 116 | { |
| 117 | for (auto &scalingGovernorDataSource : scalingGovernorDataSources_) |
| 118 | if (scalingGovernorDataSource->read(dataSourceEntry_)) { |
| 119 | if (dataSourceEntry_ != governor) |
| 120 | ctlCmds.add({scalingGovernorDataSource->source(), governor}); |
| 121 | } |
| 122 | } |