| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "cpufreqprofilepart.h" |
| 5 | |
| 6 | #include "core/profilepartprovider.h" |
| 7 | #include <algorithm> |
| 8 | #include <memory> |
| 9 | #include <utility> |
| 10 | |
| 11 | class CPUFreqProfilePart::Initializer final : public CPUFreq::Exporter |
| 12 | { |
| 13 | public: |
| 14 | Initializer(CPUFreqProfilePart &outer) noexcept |
| 15 | : outer_(outer) |
| 16 | { |
| 17 | } |
| 18 | |
| 19 | std::optional<std::reference_wrapper<Exportable::Exporter>> |
| 20 | provideExporter(Item const &) override |
| 21 | { |
| 22 | return {}; |
| 23 | } |
| 24 | |
| 25 | void takeActive(bool active) override; |
| 26 | void takeCPUFreqScalingGovernor(std::string const &governor) override; |
| 27 | void |
| 28 | takeCPUFreqScalingGovernors(std::vector<std::string> const &governors) override; |
| 29 | void takeCPUFreqEPPHint(std::optional<std::string> const &hint) override; |
| 30 | void takeCPUFreqEPPHints( |
| 31 | std::optional<std::vector<std::string>> const &hints) override; |
| 32 | |
| 33 | private: |
| 34 | CPUFreqProfilePart &outer_; |
| 35 | }; |
| 36 | |
| 37 | void CPUFreqProfilePart::Initializer::takeActive(bool active) |
| 38 | { |
| 39 | outer_.activate(active); |
| 40 | } |
| 41 | |
| 42 | void CPUFreqProfilePart::Initializer::takeCPUFreqScalingGovernor( |
| 43 | std::string const &governor) |
| 44 | { |
| 45 | outer_.governor_ = governor; |
| 46 | } |
| 47 | |
| 48 | void CPUFreqProfilePart::Initializer::takeCPUFreqScalingGovernors( |
| 49 | std::vector<std::string> const &governors) |
| 50 | { |
| 51 | outer_.governors_ = governors; |
| 52 | } |
| 53 | |
| 54 | void CPUFreqProfilePart::Initializer::takeCPUFreqEPPHint( |
| 55 | std::optional<std::string> const &hint) |
| 56 | { |
| 57 | outer_.eppHint_ = hint; |
| 58 | } |
| 59 | |
| 60 | void CPUFreqProfilePart::Initializer::takeCPUFreqEPPHints( |
| 61 | std::optional<std::vector<std::string>> const &hints) |
| 62 | { |
| 63 | outer_.eppHints_ = hints; |
| 64 | } |
| 65 | |
| 66 | CPUFreqProfilePart::CPUFreqProfilePart() noexcept |
| 67 | : id_(CPUFreq::ItemID) |
| 68 | { |
| 69 | } |
| 70 | |
| 71 | std::unique_ptr<Exportable::Exporter> |
| 72 | CPUFreqProfilePart::factory(IProfilePartProvider const &) |
| 73 | { |
| 74 | return nullptr; |
| 75 | } |
| 76 | |
| 77 | std::unique_ptr<Exportable::Exporter> CPUFreqProfilePart::initializer() |
| 78 | { |
| 79 | return std::make_unique<CPUFreqProfilePart::Initializer>(*this); |
| 80 | } |
| 81 | |
| 82 | std::string const &CPUFreqProfilePart::ID() const |
| 83 | { |
| 84 | return id_; |
| 85 | } |
| 86 | |
| 87 | std::optional<std::reference_wrapper<Importable::Importer>> |
| 88 | CPUFreqProfilePart::provideImporter(Item const &) |
| 89 | { |
| 90 | return {}; |
| 91 | } |
| 92 | |
| 93 | bool CPUFreqProfilePart::provideActive() const |
| 94 | { |
| 95 | return active(); |
| 96 | } |
| 97 | |
| 98 | std::string const &CPUFreqProfilePart::provideCPUFreqScalingGovernor() const |
| 99 | { |
| 100 | return governor_; |
| 101 | } |
| 102 | |
| 103 | std::optional<std::string> const &CPUFreqProfilePart::provideCPUFreqEPPHint() const |
| 104 | { |
| 105 | return eppHint_; |
| 106 | } |
| 107 | |
| 108 | void CPUFreqProfilePart::importProfilePart(IProfilePart::Importer &i) |
| 109 | { |
| 110 | auto &importer = dynamic_cast<CPUFreqProfilePart::Importer &>(i); |
| 111 | governor(importer.provideCPUFreqScalingGovernor()); |
| 112 | eppHint(importer.provideCPUFreqEPPHint()); |
| 113 | } |
| 114 | |
| 115 | void CPUFreqProfilePart::exportProfilePart(IProfilePart::Exporter &e) const |
| 116 | { |
| 117 | auto &exporter = dynamic_cast<CPUFreqProfilePart::Exporter &>(e); |
| 118 | exporter.takeCPUFreqScalingGovernor(governor_); |
| 119 | exporter.takeCPUFreqEPPHint(eppHint_); |
| 120 | } |
| 121 | |
| 122 | std::unique_ptr<IProfilePart> CPUFreqProfilePart::cloneProfilePart() const |
| 123 | { |
| 124 | auto clone = std::make_unique<CPUFreqProfilePart>(); |
| 125 | clone->governors_ = governors_; |
| 126 | clone->governor_ = governor_; |
| 127 | clone->eppHint_ = eppHint_; |
| 128 | clone->eppHints_ = eppHints_; |
| 129 | |
| 130 | return std::move(clone); |
| 131 | } |
| 132 | |
| 133 | void CPUFreqProfilePart::governor(std::string const &governor) |
| 134 | { |
| 135 | // only import known governors |
| 136 | auto iter = std::find_if(governors_.cbegin(), governors_.cend(), |
| 137 | [&](auto const &availableGovernor) { |
| 138 | return governor == availableGovernor; |
| 139 | }); |
| 140 | if (iter != governors_.cend()) |
| 141 | governor_ = governor; |
| 142 | } |
| 143 | |
| 144 | void CPUFreqProfilePart::eppHint(std::optional<std::string> const &hint) |
| 145 | { |
| 146 | if (!hint || !eppHints_) |
| 147 | return; |
| 148 | |
| 149 | // only import known hints |
| 150 | auto iter = std::find_if( |
| 151 | eppHints_->cbegin(), eppHints_->cend(), |
| 152 | [&](auto const &availableHint) { return *hint == availableHint; }); |
| 153 | if (iter != eppHints_->cend()) |
| 154 | eppHint_ = hint; |
| 155 | } |
| 156 | |
| 157 | bool const CPUFreqProfilePart::registered_ = ProfilePartProvider::registerProvider( |
| 158 | CPUFreq::ItemID, []() { return std::make_unique<CPUFreqProfilePart>(); }); |