| 1 | /* |
| 2 | SPDX-FileCopyrightText: 2010 Alex Merry <alex.merry@kdemail.net> |
| 3 | |
| 4 | SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL |
| 5 | */ |
| 6 | |
| 7 | #include "cpuinfo.h" |
| 8 | #include "cpuinfo_arm.h" |
| 9 | |
| 10 | #include <QFile> |
| 11 | #include <QRegularExpression> |
| 12 | #include <QStringList> |
| 13 | |
| 14 | namespace Solid |
| 15 | { |
| 16 | namespace Backends |
| 17 | { |
| 18 | namespace UDev |
| 19 | { |
| 20 | class CpuInfo |
| 21 | { |
| 22 | public: |
| 23 | CpuInfo(); |
| 24 | QString extractCpuInfoLine(int processorNumber, const QString ®ExpStr); |
| 25 | QString extractInfoLine(const QString ®ExpStr); |
| 26 | |
| 27 | private: |
| 28 | QStringList cpuInfo; |
| 29 | }; |
| 30 | |
| 31 | QString (int processorNumber) |
| 32 | { |
| 33 | CpuInfo info; |
| 34 | QString vendor; |
| 35 | |
| 36 | #ifndef BUILDING_FOR_ARM_TARGET |
| 37 | vendor = info.extractCpuInfoLine(processorNumber, QStringLiteral("vendor_id\\s+:\\s+(\\S.+)" )); |
| 38 | if (vendor.isEmpty()) { |
| 39 | vendor = info.extractInfoLine(QStringLiteral("Hardware\\s+:\\s+(\\S.+)" )); |
| 40 | } |
| 41 | #else |
| 42 | // ARM ? "CPU implementer : 0x41" |
| 43 | vendor = info.extractCpuInfoLine(processorNumber, QStringLiteral("CPU implementer\\s+:\\s+(\\S.+)" )); |
| 44 | bool ok = false; |
| 45 | const int vendorId = vendor.toInt(&ok, 16); |
| 46 | if (ok) { |
| 47 | const ArmCpuImplementer *impl = findArmCpuImplementer(vendorId); |
| 48 | if (impl) { |
| 49 | vendor = QString::fromUtf8(impl->name); |
| 50 | } |
| 51 | } |
| 52 | #endif |
| 53 | |
| 54 | return vendor; |
| 55 | } |
| 56 | |
| 57 | QString (int processorNumber) |
| 58 | { |
| 59 | CpuInfo info; |
| 60 | QString model; |
| 61 | |
| 62 | #ifndef BUILDING_FOR_ARM_TARGET |
| 63 | model = info.extractCpuInfoLine(processorNumber, QStringLiteral("model name\\s+:\\s+(\\S.+)" )); |
| 64 | if (model.isEmpty()) { |
| 65 | model = info.extractInfoLine(QStringLiteral("Processor\\s+:\\s+(\\S.+)" )); |
| 66 | } |
| 67 | |
| 68 | // for ppc64, extract from "cpu:" line |
| 69 | if (model.isEmpty()) { |
| 70 | model = info.extractInfoLine(QStringLiteral("cpu\\s+:\\s+(\\S.+)" )); |
| 71 | } |
| 72 | #else |
| 73 | // ARM? "CPU part : 0xd03" |
| 74 | const QString vendor = info.extractCpuInfoLine(processorNumber, QStringLiteral("CPU implementer\\s+:\\s+(\\S.+)" )); |
| 75 | model = info.extractCpuInfoLine(processorNumber, QStringLiteral("CPU part\\s+:\\s+(\\S.+)" )); |
| 76 | if (!model.isEmpty() && !vendor.isEmpty()) { |
| 77 | bool vendorOk = false, modelOk = false; |
| 78 | const int vendorId = vendor.toInt(&vendorOk, 16); |
| 79 | const int modelId = model.toInt(&modelOk, 16); |
| 80 | if (vendorOk && modelOk) { |
| 81 | model = findArmCpuModel(vendorId, modelId); |
| 82 | } |
| 83 | } |
| 84 | #endif |
| 85 | |
| 86 | return model; |
| 87 | } |
| 88 | |
| 89 | int (int processorNumber) |
| 90 | { |
| 91 | CpuInfo info; |
| 92 | int speed = info.extractCpuInfoLine(processorNumber, QStringLiteral("cpu MHz\\s+:\\s+(\\d+).*" )).toInt(); |
| 93 | return speed; |
| 94 | } |
| 95 | |
| 96 | CpuInfo::CpuInfo() |
| 97 | { |
| 98 | QFile cpuInfoFile(QStringLiteral("/proc/cpuinfo" )); |
| 99 | if (!cpuInfoFile.open(flags: QIODevice::ReadOnly)) { |
| 100 | return; |
| 101 | } |
| 102 | cpuInfo = QString::fromLatin1(ba: cpuInfoFile.readAll()).split(sep: QLatin1Char('\n'), behavior: Qt::SkipEmptyParts); |
| 103 | } |
| 104 | |
| 105 | QString CpuInfo::(int processorNumber, const QString ®ExpStr) |
| 106 | { |
| 107 | const QRegularExpression processorRegExp(QRegularExpression::anchoredPattern(QStringLiteral("processor\\s+:\\s+(\\d+)" ))); |
| 108 | const QRegularExpression regExp(QRegularExpression::anchoredPattern(expression: regExpStr)); |
| 109 | |
| 110 | int line = 0; |
| 111 | while (line < cpuInfo.size()) { |
| 112 | QRegularExpressionMatch match; |
| 113 | if ((match = processorRegExp.match(subject: cpuInfo.at(i: line))).hasMatch()) { |
| 114 | int recordProcNum = match.captured(nth: 1).toInt(); |
| 115 | if (recordProcNum == processorNumber) { |
| 116 | ++line; |
| 117 | while (line < cpuInfo.size()) { |
| 118 | if ((match = regExp.match(subject: cpuInfo.at(i: line))).hasMatch()) { |
| 119 | return match.captured(nth: 1); |
| 120 | } |
| 121 | ++line; |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | ++line; |
| 126 | } |
| 127 | |
| 128 | return QString(); |
| 129 | } |
| 130 | |
| 131 | QString CpuInfo::(const QString ®ExpStr) |
| 132 | { |
| 133 | const QRegularExpression regExp(QRegularExpression::anchoredPattern(expression: regExpStr)); |
| 134 | |
| 135 | for (const QString &line : std::as_const(t&: cpuInfo)) { |
| 136 | QRegularExpressionMatch match = regExp.match(subject: line); |
| 137 | if (match.hasMatch()) { |
| 138 | return match.captured(nth: 1); |
| 139 | } |
| 140 | } |
| 141 | return QString(); |
| 142 | } |
| 143 | |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |