1 /*
2 SPDX-FileCopyrightText: 2020 David Redondo <kde@david-redondo.de>
3
4 SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
5 */
6
7 #include "linuxcpu.h"
8
9 #include <QFile>
10
11 #include <sensors/sensors.h>
12 #include <systemstats/SensorsFeatureSensor.h>
13
readCpuFreq(const QString & cpuId,const QString & attribute,bool & ok)14 static double readCpuFreq(const QString &cpuId, const QString &attribute, bool &ok)
15 {
16 ok = false;
17 QFile file(QStringLiteral("/sys/devices/system/cpu/%1/cpufreq/").arg(cpuId) + attribute);
18 bool open = file.open(QIODevice::ReadOnly);
19 if (open) {
20 // Remove trailing new line
21 return file.readAll().chopped(1).toUInt(&ok) / 1000.0; // CPUFreq reports values in kHZ
22 }
23 return 0;
24 }
25
LinuxCpuObject(const QString & id,const QString & name,KSysGuard::SensorContainer * parent)26 LinuxCpuObject::LinuxCpuObject(const QString &id, const QString &name, KSysGuard::SensorContainer *parent)
27 : CpuObject(id, name, parent)
28
29 {
30 }
31
makeTemperatureSensor(const sensors_chip_name * const chipName,const sensors_feature * const feature)32 void LinuxCpuObject::makeTemperatureSensor(const sensors_chip_name * const chipName, const sensors_feature * const feature)
33 {
34 m_temperature = KSysGuard::makeSensorsFeatureSensor(QStringLiteral("temperature"), chipName, feature, this);
35 }
36
initialize(double initialFrequency)37 void LinuxCpuObject::initialize(double initialFrequency)
38 {
39 CpuObject::initialize();
40 m_frequency->setValue(initialFrequency);
41 bool ok;
42 const double max = readCpuFreq(id(), "cpuinfo_max_freq", ok);
43 if (ok) {
44 m_frequency->setMax(max);
45 }
46 const double min = readCpuFreq(id(), "cpuinfo_min_freq", ok);
47 if (ok) {
48 m_frequency->setMin(min);
49 }
50 }
51
makeSensors()52 void LinuxCpuObject::makeSensors()
53 {
54 BaseCpuObject::makeSensors();
55 m_frequency = new KSysGuard::SensorProperty(QStringLiteral("frequency"), this);
56 if (!m_temperature) {
57 m_temperature = new KSysGuard::SensorProperty(QStringLiteral("temperature"), this);
58 }
59 }
60
update(unsigned long long system,unsigned long long user,unsigned long long wait,unsigned long long idle)61 void LinuxCpuObject::update(unsigned long long system, unsigned long long user, unsigned long long wait, unsigned long long idle)
62 {
63 if (!isSubscribed()) {
64 return;
65 }
66
67 // First update usages
68 m_usageComputer.setTicks(system, user, wait, idle);
69
70 m_system->setValue(m_usageComputer.systemUsage);
71 m_user->setValue(m_usageComputer.userUsage);
72 m_wait->setValue(m_usageComputer.waitUsage);
73 m_usage->setValue(m_usageComputer.totalUsage);
74
75 // Second try to get current frequency
76 bool ok = false;
77 // First try cpuinfo_cur_freq, it is the frequency the hardware runs at (https://www.kernel.org/doc/html/latest/admin-guide/pm/cpufreq.html)
78 int frequency = readCpuFreq(id(), "cpuinfo_cur_freq", ok);
79 if (!ok) {
80 frequency = readCpuFreq(id(), "scaling_cur_freq", ok);
81 }
82 if (ok) {
83 m_frequency->setValue(frequency);
84 }
85 // FIXME Should we fall back to reading /proc/cpuinfo again when the above fails? Could have the
86 // frequency value changed even if the cpu apparently doesn't use CPUFreq?
87
88 // Third update temperature
89 m_temperature->update();
90 }
91
update(unsigned long long system,unsigned long long user,unsigned long long wait,unsigned long long idle)92 void LinuxAllCpusObject::update(unsigned long long system, unsigned long long user, unsigned long long wait, unsigned long long idle) {
93 m_usageComputer.setTicks(system, user, wait, idle);
94
95 m_system->setValue(m_usageComputer.systemUsage);
96 m_user->setValue(m_usageComputer.userUsage);
97 m_wait->setValue(m_usageComputer.waitUsage);
98 m_usage->setValue(m_usageComputer.totalUsage);
99 }
100