| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "cpuinfoproccpuinfo.h" |
| 5 | |
| 6 | #include "../infoproviderregistry.h" |
| 7 | #include "common/fileutils.h" |
| 8 | #include "core/components/cpuutils.h" |
| 9 | #include <spdlog/spdlog.h> |
| 10 | |
| 11 | class CPUInfoProcCpuInfoDataSource : public IDataSource<std::vector<std::string>> |
| 12 | { |
| 13 | public: |
| 14 | std::string source() const override |
| 15 | { |
| 16 | return "/proc/cpuinfo"; |
| 17 | } |
| 18 | |
| 19 | bool read(std::vector<std::string> &data) override |
| 20 | { |
| 21 | auto lines = Utils::File::readFileLines(source()); |
| 22 | if (!lines.empty()) { |
| 23 | std::swap(data, lines); |
| 24 | return true; |
| 25 | } |
| 26 | |
| 27 | SPDLOG_WARN("Cannot retrieve device information from {}", source()); |
| 28 | return false; |
| 29 | } |
| 30 | }; |
| 31 | |
| 32 | CPUInfoProcCpuInfo::CPUInfoProcCpuInfo( |
| 33 | std::unique_ptr<IDataSource<std::vector<std::string>>> &&dataSource) noexcept |
| 34 | : dataSource_(std::move(dataSource)) |
| 35 | { |
| 36 | } |
| 37 | |
| 38 | std::vector<std::pair<std::string, std::string>> CPUInfoProcCpuInfo::provideInfo( |
| 39 | int, std::vector<ICPUInfo::ExecutionUnit> const &executionUnits) |
| 40 | { |
| 41 | std::vector<std::pair<std::string, std::string>> info; |
| 42 | |
| 43 | std::vector<std::string> data; |
| 44 | if (!executionUnits.empty() && dataSource_->read(data)) { |
| 45 | |
| 46 | info.emplace_back(ICPUInfo::Keys::executionUnits, |
| 47 | std::to_string(executionUnits.size())); |
| 48 | auto cpuId = executionUnits.front().cpuId; |
| 49 | |
| 50 | addInfo("vendor_id", ICPUInfo::Keys::vendorId, cpuId, info, data); |
| 51 | addInfo("cpu family", ICPUInfo::Keys::cpuFamily, cpuId, info, data); |
| 52 | addInfo("model", ICPUInfo::Keys::model, cpuId, info, data); |
| 53 | addInfo("model name", ICPUInfo::Keys::modelName, cpuId, info, data); |
| 54 | addInfo("stepping", ICPUInfo::Keys::stepping, cpuId, info, data); |
| 55 | addInfo("microcode", ICPUInfo::Keys::ucode, cpuId, info, data); |
| 56 | addInfo("cache size", ICPUInfo::Keys::l3Cache, cpuId, info, data); |
| 57 | addInfo("cpu cores", ICPUInfo::Keys::cores, cpuId, info, data); |
| 58 | addInfo("flags", ICPUInfo::Keys::flags, cpuId, info, data); |
| 59 | addInfo("bugs", ICPUInfo::Keys::bugs, cpuId, info, data); |
| 60 | addInfo("bogomips", ICPUInfo::Keys::bogomips, cpuId, info, data); |
| 61 | } |
| 62 | |
| 63 | return info; |
| 64 | } |
| 65 | |
| 66 | std::vector<std::string> CPUInfoProcCpuInfo::provideCapabilities( |
| 67 | int, std::vector<ICPUInfo::ExecutionUnit> const &) |
| 68 | { |
| 69 | return {}; |
| 70 | } |
| 71 | |
| 72 | void CPUInfoProcCpuInfo::addInfo( |
| 73 | std::string_view target, std::string_view key, int cpuId, |
| 74 | std::vector<std::pair<std::string, std::string>> &info, |
| 75 | std::vector<std::string> const &lines) const |
| 76 | { |
| 77 | auto data = Utils::CPU::parseProcCpuInfo(lines, cpuId, target); |
| 78 | if (data.has_value()) |
| 79 | info.emplace_back(key, *data); |
| 80 | } |
| 81 | |
| 82 | bool CPUInfoProcCpuInfo::registered_ = |
| 83 | InfoProviderRegistry::add(std::make_unique<CPUInfoProcCpuInfo>( |
| 84 | std::make_unique<CPUInfoProcCpuInfoDataSource>())); |