| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "cpuinfolscpu.h" |
| 5 | |
| 6 | #include "../infoproviderregistry.h" |
| 7 | #include "common/stringutils.h" |
| 8 | #include <QProcess> |
| 9 | #include <QStringList> |
| 10 | #include <algorithm> |
| 11 | #include <spdlog/spdlog.h> |
| 12 | |
| 13 | class CPUInfoLsCpuDataSource : public IDataSource<std::vector<std::string>> |
| 14 | { |
| 15 | public: |
| 16 | std::string source() const override |
| 17 | { |
| 18 | return "lscpu"; |
| 19 | } |
| 20 | |
| 21 | bool read(std::vector<std::string> &data) override |
| 22 | { |
| 23 | auto env = QProcessEnvironment::systemEnvironment(); |
| 24 | env.insert("LC_ALL", "C"); |
| 25 | |
| 26 | QProcess cmd; |
| 27 | cmd.setProcessChannelMode(QProcess::MergedChannels); |
| 28 | cmd.setProcessEnvironment(env); |
| 29 | cmd.start(source().c_str(), QStringList()); |
| 30 | |
| 31 | if (cmd.waitForFinished()) { |
| 32 | auto rawData = cmd.readAllStandardOutput().toStdString(); |
| 33 | data = Utils::String::split(rawData, '\n'); |
| 34 | return true; |
| 35 | } |
| 36 | |
| 37 | SPDLOG_WARN("lscpu command failed"); |
| 38 | return false; |
| 39 | } |
| 40 | }; |
| 41 | |
| 42 | CPUInfoLsCpu::CPUInfoLsCpu( |
| 43 | std::unique_ptr<IDataSource<std::vector<std::string>>> &&dataSource) noexcept |
| 44 | : dataSource_(std::move(dataSource)) |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | std::vector<std::pair<std::string, std::string>> |
| 49 | CPUInfoLsCpu::provideInfo(int, std::vector<ICPUInfo::ExecutionUnit> const &) |
| 50 | { |
| 51 | std::vector<std::pair<std::string, std::string>> info; |
| 52 | |
| 53 | std::vector<std::string> data; |
| 54 | if (dataSource_->read(data)) { |
| 55 | addInfo("Architecture", Keys::arch, info, data); |
| 56 | addInfo("CPU op-mode(s)", Keys::opMode, info, data); |
| 57 | addInfo("Byte Order", Keys::byteOrder, info, data); |
| 58 | addInfo("Virtualization", Keys::virt, info, data); |
| 59 | addInfo("L1d cache", Keys::l1dCache, info, data); |
| 60 | addInfo("L1i cache", Keys::l1iCache, info, data); |
| 61 | addInfo("L2 cache", Keys::l2Cache, info, data); |
| 62 | } |
| 63 | |
| 64 | return info; |
| 65 | } |
| 66 | |
| 67 | std::vector<std::string> |
| 68 | CPUInfoLsCpu::provideCapabilities(int, |
| 69 | std::vector<ICPUInfo::ExecutionUnit> const &) |
| 70 | { |
| 71 | return {}; |
| 72 | } |
| 73 | |
| 74 | std::string CPUInfoLsCpu::extractLineData(std::string const &line) const |
| 75 | { |
| 76 | auto colonPos = line.find(':'); |
| 77 | if (colonPos != std::string::npos) { |
| 78 | auto dataPos = line.find_first_not_of("\t: ", colonPos); |
| 79 | if (dataPos != std::string::npos) |
| 80 | return line.substr(dataPos); |
| 81 | } |
| 82 | |
| 83 | return {}; |
| 84 | } |
| 85 | |
| 86 | void CPUInfoLsCpu::addInfo(std::string_view target, std::string_view key, |
| 87 | std::vector<std::pair<std::string, std::string>> &info, |
| 88 | std::vector<std::string> const &data) const |
| 89 | { |
| 90 | auto keyIt = std::find_if(data.cbegin(), data.cend(), |
| 91 | [=](std::string const &line) { |
| 92 | return line.find(target) != std::string::npos; |
| 93 | }); |
| 94 | if (keyIt != data.cend()) |
| 95 | info.emplace_back(key, extractLineData(*keyIt)); |
| 96 | } |
| 97 | |
| 98 | bool CPUInfoLsCpu::registered_ = InfoProviderRegistry::add( |
| 99 | std::make_unique<CPUInfoLsCpu>(std::make_unique<CPUInfoLsCpuDataSource>())); |