Solid

udevprocessor.cpp
1/*
2 SPDX-FileCopyrightText: 2010 Rafael Fernández López <ereslibre@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5*/
6
7#include "udevprocessor.h"
8
9#include "../shared/cpufeatures.h"
10#include "cpuinfo.h"
11#include "udevdevice.h"
12
13#include <QFile>
14
15using namespace Solid::Backends::UDev;
16
17Processor::Processor(UDevDevice *device)
18 : DeviceInterface(device)
19 , m_canChangeFrequency(NotChecked)
20 , m_maxSpeed(-1)
21{
22}
23
24Processor::~Processor()
25{
26}
27
28int Processor::number() const
29{
30 // There's a subtle assumption here: suppose the system's ACPI
31 // supports more processors/cores than are installed, and so udev reports
32 // 4 cores when there are 2, say. Will the processor numbers (in
33 // /proc/cpuinfo, in particular) always match the sysfs device numbers?
34 return m_device->deviceNumber();
35}
36
37int Processor::maxSpeed() const
38{
39 if (m_maxSpeed == -1) {
40 QFile cpuMaxFreqFile(m_device->deviceName() + prefix() + QStringLiteral("/cpufreq/cpuinfo_max_freq"));
41 if (cpuMaxFreqFile.open(QIODevice::ReadOnly)) {
42 QByteArray value = cpuMaxFreqFile.readAll().trimmed();
43 // cpuinfo_max_freq is in kHz
44 m_maxSpeed = static_cast<int>(value.toLongLong() / 1000);
45 }
46 if (m_maxSpeed <= 0) {
47 // couldn't get the info from /sys, try /proc instead
48 m_maxSpeed = extractCurrentCpuSpeed(number());
49 }
50 }
51 return m_maxSpeed;
52}
53
54bool Processor::canChangeFrequency() const
55{
56 if (m_canChangeFrequency == NotChecked) {
57 /* Note that cpufreq is the right information source here, rather than
58 * anything to do with throttling (ACPI T-states). */
59
60 m_canChangeFrequency = CannotChangeFreq;
61
62 QFile cpuMinFreqFile(m_device->deviceName() + prefix() + QStringLiteral("/cpufreq/cpuinfo_min_freq"));
63 QFile cpuMaxFreqFile(m_device->deviceName() + prefix() + QStringLiteral("/cpufreq/cpuinfo_max_freq"));
64 if (cpuMinFreqFile.open(QIODevice::ReadOnly) && cpuMaxFreqFile.open(QIODevice::ReadOnly)) {
65 qlonglong minFreq = cpuMinFreqFile.readAll().trimmed().toLongLong();
66 qlonglong maxFreq = cpuMaxFreqFile.readAll().trimmed().toLongLong();
67 if (minFreq > 0 && maxFreq > minFreq) {
68 m_canChangeFrequency = CanChangeFreq;
69 }
70 }
71 }
72
73 return m_canChangeFrequency == CanChangeFreq;
74}
75
76Solid::Processor::InstructionSets Processor::instructionSets() const
77{
78 static Solid::Processor::InstructionSets cpuextensions = Solid::Backends::Shared::cpuFeatures();
79
80 return cpuextensions;
81}
82
83QString Processor::prefix() const
84{
85 QLatin1String sysPrefix("/sysdev");
86 if (QFile::exists(m_device->deviceName() + sysPrefix)) {
87 return sysPrefix;
88 }
89
90 return QString();
91}
92
93#include "moc_udevprocessor.cpp"
qlonglong toLongLong(bool *ok, int base) const const
QByteArray trimmed() const const
bool exists() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:01:49 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.