| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "cpu.h" |
| 5 | |
| 6 | #include "controls/icontrol.h" |
| 7 | #include "core/info/icpuinfo.h" |
| 8 | #include "sensors/isensor.h" |
| 9 | #include <utility> |
| 10 | |
| 11 | CPU::CPU(std::unique_ptr<ICPUInfo> &&info, |
| 12 | std::vector<std::unique_ptr<IControl>> &&controls, |
| 13 | std::vector<std::unique_ptr<ISensor>> &&sensors) noexcept |
| 14 | : id_(ICPU::ItemID) |
| 15 | , info_(std::move(info)) |
| 16 | , controls_(std::move(controls)) |
| 17 | , sensors_(std::move(sensors)) |
| 18 | { |
| 19 | key_ = "CPU" + std::to_string(info_->physicalId()); |
| 20 | } |
| 21 | |
| 22 | bool CPU::active() const |
| 23 | { |
| 24 | return active_; |
| 25 | } |
| 26 | |
| 27 | void CPU::activate(bool active) |
| 28 | { |
| 29 | active_ = active; |
| 30 | } |
| 31 | |
| 32 | std::string const &CPU::key() const |
| 33 | { |
| 34 | return key_; |
| 35 | } |
| 36 | |
| 37 | std::pair<std::string, std::vector<std::pair<std::string, std::string>>> |
| 38 | CPU::componentInfo() const |
| 39 | { |
| 40 | std::pair<std::string, std::vector<std::pair<std::string, std::string>>> info; |
| 41 | |
| 42 | auto name(info_->info(ICPUInfo::Keys::modelName)); |
| 43 | if (!name.empty()) |
| 44 | name.append("\n"); |
| 45 | name.append("[CPU ").append(std::to_string(info_->physicalId())).append("]"); |
| 46 | info.first = name; |
| 47 | |
| 48 | auto infoKeys = info_->keys(); |
| 49 | for (auto &infoKey : infoKeys) |
| 50 | info.second.emplace_back(infoKey, info_->info(infoKey)); |
| 51 | |
| 52 | return info; |
| 53 | } |
| 54 | |
| 55 | void CPU::init() |
| 56 | { |
| 57 | for (auto &control : controls_) |
| 58 | control->init(); |
| 59 | } |
| 60 | |
| 61 | void CPU::preInit(ICommandQueue &ctlCmds) |
| 62 | { |
| 63 | for (auto &control : controls_) |
| 64 | control->preInit(ctlCmds); |
| 65 | } |
| 66 | |
| 67 | void CPU::postInit(ICommandQueue &ctlCmds) |
| 68 | { |
| 69 | for (auto &control : controls_) |
| 70 | control->postInit(ctlCmds); |
| 71 | } |
| 72 | |
| 73 | void CPU::sync(ICommandQueue &ctlCmds) |
| 74 | { |
| 75 | if (active_) { |
| 76 | // NOTE clean and sync commands generation cannot be interleaved. |
| 77 | // Interleaving them could cause conflicts between clean and sync |
| 78 | // commands of different controls, leading to an incorrect hardware |
| 79 | // state. |
| 80 | for (auto &control : controls_) |
| 81 | control->clean(ctlCmds); |
| 82 | for (auto &control : controls_) |
| 83 | control->sync(ctlCmds); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | void CPU::updateSensors( |
| 88 | std::unordered_map<std::string, std::unordered_set<std::string>> const &ignored) |
| 89 | { |
| 90 | for (auto &sensor : sensors_) { |
| 91 | if (ignored.count(key_) > 0 && ignored.at(key_).count(sensor->ID()) > 0) |
| 92 | continue; // skip ignored sensors |
| 93 | |
| 94 | sensor->update(); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | ICPUInfo const &CPU::info() const |
| 99 | { |
| 100 | return *info_; |
| 101 | } |
| 102 | |
| 103 | std::string const &CPU::ID() const |
| 104 | { |
| 105 | return id_; |
| 106 | } |
| 107 | |
| 108 | void CPU::importWith(Importable::Importer &i) |
| 109 | { |
| 110 | auto importer = i.provideImporter(*this); |
| 111 | if (importer.has_value()) { |
| 112 | auto &cpuImporter = dynamic_cast<ICPU::Importer &>(importer->get()); |
| 113 | |
| 114 | activate(cpuImporter.provideActive()); |
| 115 | |
| 116 | for (auto &control : controls_) |
| 117 | control->importWith(*importer); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | void CPU::exportWith(Exportable::Exporter &e) const |
| 122 | { |
| 123 | auto exporter = e.provideExporter(*this); |
| 124 | if (exporter.has_value()) { |
| 125 | auto &cpuExporter = dynamic_cast<ICPU::Exporter &>(exporter->get()); |
| 126 | |
| 127 | cpuExporter.takeActive(active()); |
| 128 | cpuExporter.takeInfo(info()); |
| 129 | |
| 130 | for (auto const &sensor : sensors_) |
| 131 | cpuExporter.takeSensor(*sensor); |
| 132 | |
| 133 | for (auto const &control : controls_) |
| 134 | control->exportWith(*exporter); |
| 135 | } |
| 136 | } |