| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "cpuinfo.h" |
| 5 | |
| 6 | #include <algorithm> |
| 7 | #include <iterator> |
| 8 | #include <utility> |
| 9 | |
| 10 | CPUInfo::CPUInfo(int physicalId, |
| 11 | std::vector<ICPUInfo::ExecutionUnit> &&executionUnits) noexcept |
| 12 | : physicalId_(physicalId) |
| 13 | , executionUnits_(std::move(executionUnits)) |
| 14 | { |
| 15 | } |
| 16 | |
| 17 | int CPUInfo::physicalId() const |
| 18 | { |
| 19 | return physicalId_; |
| 20 | } |
| 21 | |
| 22 | std::vector<ICPUInfo::ExecutionUnit> const &CPUInfo::executionUnits() const |
| 23 | { |
| 24 | return executionUnits_; |
| 25 | } |
| 26 | |
| 27 | void CPUInfo::addExecutionUnit(ICPUInfo::ExecutionUnit &&unit) |
| 28 | { |
| 29 | executionUnits_.emplace_back(std::move(unit)); |
| 30 | } |
| 31 | |
| 32 | std::vector<std::string> CPUInfo::keys() const |
| 33 | { |
| 34 | std::vector<std::string> keys; |
| 35 | keys.reserve(info_.size()); |
| 36 | |
| 37 | auto const keySelector = [](auto const &pair) { return pair.first; }; |
| 38 | std::transform(info_.cbegin(), info_.cend(), std::back_inserter(keys), |
| 39 | keySelector); |
| 40 | |
| 41 | return keys; |
| 42 | } |
| 43 | |
| 44 | std::string CPUInfo::info(std::string_view key) const |
| 45 | { |
| 46 | auto const dataIt = info_.find(std::string(key)); |
| 47 | if (dataIt != info_.cend()) |
| 48 | return dataIt->second; |
| 49 | |
| 50 | return {}; |
| 51 | } |
| 52 | |
| 53 | bool CPUInfo::hasCapability(std::string_view name) const |
| 54 | { |
| 55 | return capabilities_.count(std::string(name)) > 0; |
| 56 | } |
| 57 | |
| 58 | void CPUInfo::initialize( |
| 59 | std::vector<std::unique_ptr<ICPUInfo::IProvider>> const &providers) |
| 60 | { |
| 61 | for (auto &provider : providers) { |
| 62 | auto infos = provider->provideInfo(physicalId_, executionUnits_); |
| 63 | for (auto &info : infos) |
| 64 | info_.emplace(std::move(info)); |
| 65 | |
| 66 | auto capabilities = provider->provideCapabilities(physicalId_, |
| 67 | executionUnits_); |
| 68 | for (auto &capability : capabilities) |
| 69 | capabilities_.emplace(std::move(capability)); |
| 70 | } |
| 71 | } |