| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "cpuprofilepart.h" |
| 5 | |
| 6 | #include "core/info/icpuinfo.h" |
| 7 | #include "core/profilepart.h" |
| 8 | #include "core/profilepartprovider.h" |
| 9 | #include "sensors/isensor.h" |
| 10 | #include <algorithm> |
| 11 | #include <iterator> |
| 12 | #include <unordered_map> |
| 13 | #include <utility> |
| 14 | |
| 15 | class CPUProfilePart::Factory final |
| 16 | : public ProfilePart::Factory |
| 17 | , public ICPU::Exporter |
| 18 | { |
| 19 | public: |
| 20 | Factory(IProfilePartProvider const &profilePartProvider, |
| 21 | CPUProfilePart &outer) noexcept |
| 22 | : ProfilePart::Factory(profilePartProvider) |
| 23 | , outer_(outer) |
| 24 | { |
| 25 | } |
| 26 | |
| 27 | void takeProfilePart(std::unique_ptr<IProfilePart> &&part) override; |
| 28 | |
| 29 | std::optional<std::reference_wrapper<Exportable::Exporter>> |
| 30 | provideExporter(Item const &i) override; |
| 31 | |
| 32 | void takeActive(bool) override |
| 33 | { |
| 34 | } |
| 35 | |
| 36 | void takeInfo(ICPUInfo const &) override; |
| 37 | void takeSensor(ISensor const &) override; |
| 38 | |
| 39 | private: |
| 40 | CPUProfilePart &outer_; |
| 41 | }; |
| 42 | |
| 43 | void CPUProfilePart::Factory::takeProfilePart(std::unique_ptr<IProfilePart> &&part) |
| 44 | { |
| 45 | outer_.parts_.emplace_back(std::move(part)); |
| 46 | } |
| 47 | |
| 48 | std::optional<std::reference_wrapper<Exportable::Exporter>> |
| 49 | CPUProfilePart::Factory::provideExporter(Item const &i) |
| 50 | { |
| 51 | return factory(i.ID()); |
| 52 | } |
| 53 | |
| 54 | void CPUProfilePart::Factory::takeInfo(ICPUInfo const &info) |
| 55 | { |
| 56 | // NOTE info and system component key must be initialized here |
| 57 | outer_.physicalId_ = info.physicalId(); |
| 58 | outer_.updateKey(); |
| 59 | } |
| 60 | |
| 61 | void CPUProfilePart::Factory::takeSensor(ISensor const &sensor) |
| 62 | { |
| 63 | auto sensorProfilePart = createPart(sensor.ID()); |
| 64 | if (sensorProfilePart != nullptr) |
| 65 | outer_.parts_.emplace_back(std::move(sensorProfilePart)); |
| 66 | } |
| 67 | |
| 68 | class CPUProfilePart::Initializer final : public ICPU::Exporter |
| 69 | { |
| 70 | public: |
| 71 | Initializer(CPUProfilePart &outer) noexcept |
| 72 | : outer_(outer) |
| 73 | { |
| 74 | } |
| 75 | |
| 76 | std::optional<std::reference_wrapper<Exportable::Exporter>> |
| 77 | provideExporter(Item const &i) override; |
| 78 | |
| 79 | void takeActive(bool active) override; |
| 80 | |
| 81 | void takeInfo(ICPUInfo const &) override |
| 82 | { |
| 83 | } |
| 84 | |
| 85 | void takeSensor(ISensor const &) override |
| 86 | { |
| 87 | } |
| 88 | |
| 89 | private: |
| 90 | CPUProfilePart &outer_; |
| 91 | std::unordered_map<std::string, std::unique_ptr<Exportable::Exporter>> initializers_; |
| 92 | }; |
| 93 | |
| 94 | std::optional<std::reference_wrapper<Exportable::Exporter>> |
| 95 | CPUProfilePart::Initializer::provideExporter(Item const &i) |
| 96 | { |
| 97 | for (auto &part : outer_.parts_) { |
| 98 | auto &id = part->ID(); |
| 99 | if (id == i.ID()) { |
| 100 | if (initializers_.count(id) > 0) |
| 101 | return *initializers_.at(id); |
| 102 | else { |
| 103 | auto initializer = part->initializer(); |
| 104 | if (initializer != nullptr) { |
| 105 | initializers_.emplace(id, std::move(initializer)); |
| 106 | return *initializers_.at(id); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | break; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | return {}; |
| 115 | } |
| 116 | |
| 117 | void CPUProfilePart::Initializer::takeActive(bool active) |
| 118 | { |
| 119 | outer_.activate(active); |
| 120 | } |
| 121 | |
| 122 | CPUProfilePart::CPUProfilePart() noexcept |
| 123 | : id_(ICPU::ItemID) |
| 124 | { |
| 125 | } |
| 126 | |
| 127 | std::unique_ptr<Exportable::Exporter> |
| 128 | CPUProfilePart::factory(IProfilePartProvider const &profilePartProvider) |
| 129 | { |
| 130 | return std::make_unique<CPUProfilePart::Factory>(profilePartProvider, *this); |
| 131 | } |
| 132 | |
| 133 | std::unique_ptr<Exportable::Exporter> CPUProfilePart::initializer() |
| 134 | { |
| 135 | return std::make_unique<CPUProfilePart::Initializer>(*this); |
| 136 | } |
| 137 | |
| 138 | std::string const &CPUProfilePart::ID() const |
| 139 | { |
| 140 | return id_; |
| 141 | } |
| 142 | |
| 143 | bool CPUProfilePart::belongsTo(Item const &i) const |
| 144 | { |
| 145 | auto cpu = dynamic_cast<ICPU const *>(&i); |
| 146 | if (cpu != nullptr) |
| 147 | return cpu->info().physicalId() == physicalId_; |
| 148 | |
| 149 | return false; |
| 150 | } |
| 151 | |
| 152 | std::string const &CPUProfilePart::key() const |
| 153 | { |
| 154 | return key_; |
| 155 | } |
| 156 | |
| 157 | std::optional<std::reference_wrapper<Importable::Importer>> |
| 158 | CPUProfilePart::provideImporter(Item const &i) |
| 159 | { |
| 160 | auto &id = i.ID(); |
| 161 | auto partIter = std::find_if( |
| 162 | parts_.cbegin(), parts_.cend(), |
| 163 | [&](auto const &part) { return part->ID() == id; }); |
| 164 | |
| 165 | if (partIter != parts_.cend()) { |
| 166 | auto importer = dynamic_cast<Importable::Importer *>(partIter->get()); |
| 167 | if (importer != nullptr) |
| 168 | return *importer; |
| 169 | } |
| 170 | |
| 171 | return {}; |
| 172 | } |
| 173 | |
| 174 | bool CPUProfilePart::provideActive() const |
| 175 | { |
| 176 | return active(); |
| 177 | } |
| 178 | |
| 179 | void CPUProfilePart::importProfilePart(IProfilePart::Importer &i) |
| 180 | { |
| 181 | int oldPhysicalId = physicalId_; |
| 182 | |
| 183 | auto &partImporter = dynamic_cast<ICPUProfilePart::Importer &>(i); |
| 184 | physicalId_ = partImporter.providePhysicalId(); |
| 185 | |
| 186 | if (oldPhysicalId != physicalId_) |
| 187 | updateKey(); |
| 188 | |
| 189 | for (auto &part : parts_) |
| 190 | part->importWith(i); |
| 191 | } |
| 192 | |
| 193 | void CPUProfilePart::exportProfilePart(IProfilePart::Exporter &e) const |
| 194 | { |
| 195 | auto &partExporter = dynamic_cast<ICPUProfilePart::Exporter &>(e); |
| 196 | partExporter.takePhysicalId(physicalId_); |
| 197 | |
| 198 | for (auto const &part : parts_) |
| 199 | part->exportWith(e); |
| 200 | } |
| 201 | |
| 202 | std::unique_ptr<IProfilePart> CPUProfilePart::cloneProfilePart() const |
| 203 | { |
| 204 | auto clone = std::make_unique<CPUProfilePart>(); |
| 205 | clone->physicalId_ = physicalId_; |
| 206 | clone->key_ = key_; |
| 207 | clone->parts_.reserve(parts_.size()); |
| 208 | std::transform(parts_.cbegin(), parts_.cend(), |
| 209 | std::back_inserter(clone->parts_), |
| 210 | [](auto const &part) { return part->clone(); }); |
| 211 | |
| 212 | return std::move(clone); |
| 213 | } |
| 214 | |
| 215 | void CPUProfilePart::updateKey() |
| 216 | { |
| 217 | key_ = "CPU" + std::to_string(physicalId_); |
| 218 | } |
| 219 | |
| 220 | bool const CPUProfilePart::registered_ = ProfilePartProvider::registerProvider( |
| 221 | ICPU::ItemID, []() { return std::make_unique<CPUProfilePart>(); }); |