Solid

cpuinfo.cpp
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
14namespace Solid
15{
16namespace Backends
17{
18namespace UDev
19{
20class CpuInfo
21{
22public:
23 CpuInfo();
24 QString extractCpuInfoLine(int processorNumber, const QString &regExpStr);
25 QString extractInfoLine(const QString &regExpStr);
26
27private:
28 QStringList cpuInfo;
29};
30
31QString extractCpuVendor(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
57QString extractCpuModel(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
89int extractCurrentCpuSpeed(int processorNumber)
90{
91 CpuInfo info;
92 int speed = info.extractCpuInfoLine(processorNumber, QStringLiteral("cpu MHz\\s+:\\s+(\\d+).*")).toInt();
93 return speed;
94}
95
96CpuInfo::CpuInfo()
97{
98 QFile cpuInfoFile(QStringLiteral("/proc/cpuinfo"));
99 if (!cpuInfoFile.open(QIODevice::ReadOnly)) {
100 return;
101 }
102 cpuInfo = QString::fromLatin1(cpuInfoFile.readAll()).split(QLatin1Char('\n'), Qt::SkipEmptyParts);
103}
104
105QString CpuInfo::extractCpuInfoLine(int processorNumber, const QString &regExpStr)
106{
107 const QRegularExpression processorRegExp(QRegularExpression::anchoredPattern(QStringLiteral("processor\\s+:\\s+(\\d+)")));
109
110 int line = 0;
111 while (line < cpuInfo.size()) {
113 if ((match = processorRegExp.match(cpuInfo.at(line))).hasMatch()) {
114 int recordProcNum = match.captured(1).toInt();
115 if (recordProcNum == processorNumber) {
116 ++line;
117 while (line < cpuInfo.size()) {
118 if ((match = regExp.match(cpuInfo.at(line))).hasMatch()) {
119 return match.captured(1);
120 }
121 ++line;
122 }
123 }
124 }
125 ++line;
126 }
127
128 return QString();
129}
130
131QString CpuInfo::extractInfoLine(const QString &regExpStr)
132{
134
135 for (const QString &line : std::as_const(cpuInfo)) {
136 QRegularExpressionMatch match = regExp.match(line);
137 if (match.hasMatch()) {
138 return match.captured(1);
139 }
140 }
141 return QString();
142}
143
144}
145}
146}
KCOREADDONS_EXPORT Result match(QStringView pattern, QStringView str)
const_reference at(qsizetype i) const const
qsizetype size() const const
QString anchoredPattern(QStringView expression)
QString fromLatin1(QByteArrayView str)
QString fromUtf8(QByteArrayView str)
bool isEmpty() const const
QStringList split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
int toInt(bool *ok, int base) const const
SkipEmptyParts
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 6 2024 12:07:11 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.