| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "gpuprofilepart.h" |
| 5 | |
| 6 | #include "core/info/igpuinfo.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 GPUProfilePart::Factory final |
| 16 | : public ProfilePart::Factory |
| 17 | , public IGPU::Exporter |
| 18 | { |
| 19 | public: |
| 20 | Factory(IProfilePartProvider const &profilePartProvider, |
| 21 | GPUProfilePart &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(IGPUInfo const &) override; |
| 37 | void takeSensor(ISensor const &) override; |
| 38 | |
| 39 | private: |
| 40 | GPUProfilePart &outer_; |
| 41 | }; |
| 42 | |
| 43 | void GPUProfilePart::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 | GPUProfilePart::Factory::provideExporter(Item const &i) |
| 50 | { |
| 51 | return factory(i.ID()); |
| 52 | } |
| 53 | |
| 54 | void GPUProfilePart::Factory::takeInfo(IGPUInfo const &info) |
| 55 | { |
| 56 | // NOTE info and system component key must be initialized here |
| 57 | outer_.deviceID_ = info.info(IGPUInfo::Keys::deviceID); |
| 58 | outer_.revision_ = info.info(IGPUInfo::Keys::revision); |
| 59 | |
| 60 | auto uniqueID = info.info(IGPUInfo::Keys::uniqueID); |
| 61 | if (!uniqueID.empty()) |
| 62 | outer_.uniqueID_ = uniqueID; |
| 63 | |
| 64 | outer_.index_ = info.index(); |
| 65 | outer_.updateKey(); |
| 66 | } |
| 67 | |
| 68 | void GPUProfilePart::Factory::takeSensor(ISensor const &sensor) |
| 69 | { |
| 70 | auto sensorProfilePart = createPart(sensor.ID()); |
| 71 | if (sensorProfilePart != nullptr) |
| 72 | outer_.parts_.emplace_back(std::move(sensorProfilePart)); |
| 73 | } |
| 74 | |
| 75 | class GPUProfilePart::Initializer final : public IGPU::Exporter |
| 76 | { |
| 77 | public: |
| 78 | Initializer(GPUProfilePart &outer) noexcept |
| 79 | : outer_(outer) |
| 80 | { |
| 81 | } |
| 82 | |
| 83 | std::optional<std::reference_wrapper<Exportable::Exporter>> |
| 84 | provideExporter(Item const &i) override; |
| 85 | |
| 86 | void takeActive(bool active) override; |
| 87 | |
| 88 | void takeInfo(IGPUInfo const &) override |
| 89 | { |
| 90 | } |
| 91 | |
| 92 | void takeSensor(ISensor const &) override |
| 93 | { |
| 94 | } |
| 95 | |
| 96 | private: |
| 97 | GPUProfilePart &outer_; |
| 98 | std::unordered_map<std::string, std::unique_ptr<Exportable::Exporter>> initializers_; |
| 99 | }; |
| 100 | |
| 101 | std::optional<std::reference_wrapper<Exportable::Exporter>> |
| 102 | GPUProfilePart::Initializer::provideExporter(Item const &i) |
| 103 | { |
| 104 | for (auto &part : outer_.parts_) { |
| 105 | auto &id = part->ID(); |
| 106 | if (id == i.ID()) { |
| 107 | if (initializers_.count(id) > 0) |
| 108 | return *initializers_.at(id); |
| 109 | else { |
| 110 | auto initializer = part->initializer(); |
| 111 | if (initializer != nullptr) { |
| 112 | initializers_.emplace(id, std::move(initializer)); |
| 113 | return *initializers_.at(id); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | break; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | return {}; |
| 122 | } |
| 123 | |
| 124 | void GPUProfilePart::Initializer::takeActive(bool active) |
| 125 | { |
| 126 | outer_.activate(active); |
| 127 | } |
| 128 | |
| 129 | GPUProfilePart::GPUProfilePart() noexcept |
| 130 | : id_(IGPU::ItemID) |
| 131 | { |
| 132 | } |
| 133 | |
| 134 | std::unique_ptr<Exportable::Exporter> |
| 135 | GPUProfilePart::factory(IProfilePartProvider const &profilePartProvider) |
| 136 | { |
| 137 | return std::make_unique<GPUProfilePart::Factory>(profilePartProvider, *this); |
| 138 | } |
| 139 | |
| 140 | std::unique_ptr<Exportable::Exporter> GPUProfilePart::initializer() |
| 141 | { |
| 142 | return std::make_unique<GPUProfilePart::Initializer>(*this); |
| 143 | } |
| 144 | |
| 145 | std::string const &GPUProfilePart::ID() const |
| 146 | { |
| 147 | return id_; |
| 148 | } |
| 149 | |
| 150 | bool GPUProfilePart::belongsTo(Item const &i) const |
| 151 | { |
| 152 | auto gpu = dynamic_cast<IGPU const *>(&i); |
| 153 | if (gpu != nullptr) { |
| 154 | auto &info = gpu->info(); |
| 155 | |
| 156 | // when available, use the GPU unique ID |
| 157 | auto uniqueID = info.info(IGPUInfo::Keys::uniqueID); |
| 158 | if (!uniqueID.empty()) |
| 159 | return uniqueID == uniqueID_; |
| 160 | |
| 161 | return info.index() == index_ && |
| 162 | info.info(IGPUInfo::Keys::deviceID) == deviceID_ && |
| 163 | info.info(IGPUInfo::Keys::revision) == revision_; |
| 164 | } |
| 165 | |
| 166 | return false; |
| 167 | } |
| 168 | |
| 169 | std::string const &GPUProfilePart::key() const |
| 170 | { |
| 171 | return key_; |
| 172 | } |
| 173 | |
| 174 | std::optional<std::reference_wrapper<Importable::Importer>> |
| 175 | GPUProfilePart::provideImporter(Item const &i) |
| 176 | { |
| 177 | auto &id = i.ID(); |
| 178 | auto partIter = std::find_if( |
| 179 | parts_.cbegin(), parts_.cend(), |
| 180 | [&](auto const &part) { return part->ID() == id; }); |
| 181 | |
| 182 | if (partIter != parts_.cend()) { |
| 183 | auto importer = dynamic_cast<Importable::Importer *>(partIter->get()); |
| 184 | if (importer != nullptr) |
| 185 | return *importer; |
| 186 | } |
| 187 | |
| 188 | return {}; |
| 189 | } |
| 190 | |
| 191 | bool GPUProfilePart::provideActive() const |
| 192 | { |
| 193 | return active(); |
| 194 | } |
| 195 | |
| 196 | void GPUProfilePart::importProfilePart(IProfilePart::Importer &i) |
| 197 | { |
| 198 | int oldIndex = index_; |
| 199 | |
| 200 | auto &gImporter = dynamic_cast<IGPUProfilePart::Importer &>(i); |
| 201 | index_ = gImporter.provideIndex(); |
| 202 | deviceID_ = gImporter.provideDeviceID(); |
| 203 | revision_ = gImporter.provideRevision(); |
| 204 | uniqueID_ = gImporter.provideUniqueID(); |
| 205 | |
| 206 | if (oldIndex != index_) |
| 207 | updateKey(); |
| 208 | |
| 209 | for (auto &part : parts_) |
| 210 | part->importWith(i); |
| 211 | } |
| 212 | |
| 213 | void GPUProfilePart::exportProfilePart(IProfilePart::Exporter &e) const |
| 214 | { |
| 215 | auto &gExporter = dynamic_cast<IGPUProfilePart::Exporter &>(e); |
| 216 | gExporter.takeIndex(index_); |
| 217 | gExporter.takeDeviceID(deviceID_); |
| 218 | gExporter.takeRevision(revision_); |
| 219 | gExporter.takeUniqueID(uniqueID_); |
| 220 | |
| 221 | for (auto const &part : parts_) |
| 222 | part->exportWith(e); |
| 223 | } |
| 224 | |
| 225 | std::unique_ptr<IProfilePart> GPUProfilePart::cloneProfilePart() const |
| 226 | { |
| 227 | auto clone = std::make_unique<GPUProfilePart>(); |
| 228 | clone->deviceID_ = deviceID_; |
| 229 | clone->revision_ = revision_; |
| 230 | clone->uniqueID_ = uniqueID_; |
| 231 | clone->index_ = index_; |
| 232 | clone->key_ = key_; |
| 233 | clone->parts_.reserve(parts_.size()); |
| 234 | std::transform(parts_.cbegin(), parts_.cend(), |
| 235 | std::back_inserter(clone->parts_), |
| 236 | [](auto const &part) { return part->clone(); }); |
| 237 | |
| 238 | return std::move(clone); |
| 239 | } |
| 240 | |
| 241 | void GPUProfilePart::updateKey() |
| 242 | { |
| 243 | key_ = "GPU" + std::to_string(index_); |
| 244 | } |
| 245 | |
| 246 | bool const GPUProfilePart::registered_ = ProfilePartProvider::registerProvider( |
| 247 | IGPU::ItemID, []() { return std::make_unique<GPUProfilePart>(); }); |