Commit 0221cfc6 authored by David Redondo's avatar David Redondo 🏎
Browse files

Add makeSensors() and initalize() like gpu plugin to enable custom sensorproperty subclasses

Use it for Temperature and for eventaul sysctl sensor.
parent 3359c6fb
...@@ -24,64 +24,106 @@ ...@@ -24,64 +24,106 @@
BaseCpuObject::BaseCpuObject(const QString &id, const QString &name, SensorContainer *parent) BaseCpuObject::BaseCpuObject(const QString &id, const QString &name, SensorContainer *parent)
: SensorObject(id, name, parent) : SensorObject(id, name, parent)
{ {
m_usage = new SensorProperty(QStringLiteral("usage"), i18nc("@title", "Total Usage"), this); makeSensors();
m_usage->setPrefix(name); initialize();
}
void BaseCpuObject::makeSensors()
{
m_usage = new SensorProperty(QStringLiteral("usage"), this);
m_system = new SensorProperty(QStringLiteral("system"), this);
m_user = new SensorProperty(QStringLiteral("user"), this);
m_wait = new SensorProperty(QStringLiteral("wait"), this);
auto n = new SensorProperty(QStringLiteral("name"), i18nc("@title", "Name"), name(), this);
n->setVariantType(QVariant::String);
}
void BaseCpuObject::initialize()
{
m_usage->setPrefix(name());
m_usage->setName(i18nc("@title", "Total Usage"));
m_usage->setShortName(i18nc("@title, Short for 'Total Usage'", "Usage")); m_usage->setShortName(i18nc("@title, Short for 'Total Usage'", "Usage"));
m_usage->setUnit(KSysGuard::UnitPercent); m_usage->setUnit(KSysGuard::UnitPercent);
m_usage->setVariantType(QVariant::Double); m_usage->setVariantType(QVariant::Double);
m_usage->setMax(100); m_usage->setMax(100);
m_system = new SensorProperty(QStringLiteral("system"), i18nc("@title", "System Usage"), this); m_system->setPrefix(name());
m_system->setPrefix(name); m_system->setName(i18nc("@title", "System Usage"));
m_system->setShortName(i18nc("@title, Short for 'System Usage'", "System")); m_system->setShortName(i18nc("@title, Short for 'System Usage'", "System"));
m_system->setUnit(KSysGuard::UnitPercent); m_system->setUnit(KSysGuard::UnitPercent);
m_system->setVariantType(QVariant::Double); m_system->setVariantType(QVariant::Double);
m_system->setMax(100); m_system->setMax(100);
m_user = new SensorProperty(QStringLiteral("user"), i18nc("@title", "User Usage"), this); m_user->setPrefix(name());
m_user->setPrefix(name); m_user->setName(i18nc("@title", "User Usage"));
m_user->setShortName(i18nc("@title, Short for 'User Usage'", "User")); m_user->setShortName(i18nc("@title, Short for 'User Usage'", "User"));
m_user->setUnit(KSysGuard::UnitPercent); m_user->setUnit(KSysGuard::UnitPercent);
m_user->setVariantType(QVariant::Double); m_user->setVariantType(QVariant::Double);
m_user->setMax(100); m_user->setMax(100);
m_wait = new SensorProperty(QStringLiteral("wait"), i18nc("@title", "Wait Usage"), this); m_wait->setPrefix(name());
m_wait->setPrefix(name); m_wait->setName(i18nc("@title", "Wait Usage"));
m_wait->setShortName(i18nc("@title, Short for 'Wait Load'", "Wait")); m_wait->setShortName(i18nc("@title, Short for 'Wait Load'", "Wait"));
m_wait->setUnit(KSysGuard::UnitPercent); m_wait->setUnit(KSysGuard::UnitPercent);
m_wait->setVariantType(QVariant::Double); m_wait->setVariantType(QVariant::Double);
m_wait->setMax(100); m_wait->setMax(100);
auto n = new SensorProperty(QStringLiteral("name"), i18nc("@title", "Name"), name, this);
n->setVariantType(QVariant::String);
} }
CpuObject::CpuObject(const QString &id, const QString &name, SensorContainer *parent) CpuObject::CpuObject(const QString &id, const QString &name, SensorContainer *parent)
: BaseCpuObject(id, name, parent) : BaseCpuObject(id, name, parent)
{ {
}
void CpuObject::makeSensors()
{
BaseCpuObject::makeSensors();
m_frequency = new SensorProperty(QStringLiteral("frequency"), this);
m_temperature = new SensorProperty(QStringLiteral("temperature"), this);
}
m_frequency = new SensorProperty(QStringLiteral("frequency"), i18nc("@title", "Current Frequency"), this); void CpuObject::initialize()
m_frequency->setPrefix(name); {
BaseCpuObject::initialize();
m_frequency->setPrefix(name());
m_frequency->setName(i18nc("@title", "Current Frequency"));
m_frequency->setShortName(i18nc("@title, Short for 'Current Frequency'", "Frequency")); m_frequency->setShortName(i18nc("@title, Short for 'Current Frequency'", "Frequency"));
m_frequency->setDescription(i18nc("@info", "Current frequency of the CPU")); m_frequency->setDescription(i18nc("@info", "Current frequency of the CPU"));
m_frequency->setVariantType(QVariant::Double); m_frequency->setVariantType(QVariant::Double);
m_frequency->setUnit(KSysGuard::Unit::UnitMegaHertz); m_frequency->setUnit(KSysGuard::Unit::UnitMegaHertz);
m_temperature = new SensorProperty(QStringLiteral("temperature"), i18nc("@title", "Current Temperature"), this); m_temperature->setPrefix(name());
m_temperature->setPrefix(name); m_temperature->setPrefix(i18nc("@title", "Current Temperature"));
m_temperature->setShortName(i18nc("@title, Short for Current Temperatur", "Temperature")); m_temperature->setShortName(i18nc("@title, Short for Current Temperatur", "Temperature"));
m_temperature->setVariantType(QVariant::Double); m_temperature->setVariantType(QVariant::Double);
m_temperature->setUnit(KSysGuard::Unit::UnitCelsius); m_temperature->setUnit(KSysGuard::Unit::UnitCelsius);
} }
AllCpusObject::AllCpusObject(unsigned int cpuCount, unsigned int coreCount, SensorContainer *parent) AllCpusObject::AllCpusObject(unsigned int cpuCount, unsigned int coreCount, SensorContainer *parent)
: BaseCpuObject(QStringLiteral("all"), i18nc("@title", "All"), parent) : BaseCpuObject(QStringLiteral("all"), i18nc("@title", "All"), parent)
{ {
auto cpus = new SensorProperty(QStringLiteral("cpuCount"), i18nc("@title", "Number of CPUs"), cpuCount, this); m_cpuCount->setValue(cpuCount);
cpus->setShortName(i18nc("@title, Short fort 'Number of CPUs'", "CPUs")); m_coreCount->setValue(cpuCount);
cpus->setDescription(i18nc("@info", "Number of physical CPUs installed in the system")); }
void AllCpusObject::makeSensors()
{
m_cpuCount = new SensorProperty(QStringLiteral("cpuCount"), this);
m_coreCount = new SensorProperty(QStringLiteral("coreCount"), this);
}
void AllCpusObject::initialize()
{
m_cpuCount->setName(i18nc("@title", "Number of CPUs"));
m_cpuCount->setShortName(i18nc("@title, Short fort 'Number of CPUs'", "CPUs"));
m_cpuCount->setDescription(i18nc("@info", "Number of physical CPUs installed in the system"));
auto cores = new SensorProperty(QStringLiteral("coreCount"), i18nc("@title", "Number of Cores"), coreCount, this); m_coreCount->setName(i18nc("@title", "Number of Cores"));
cores->setShortName(i18nc("@title, Short fort 'Number of Cores'", "Cores")); m_coreCount->setShortName(i18nc("@title, Short fort 'Number of Cores'", "Cores"));
cores->setDescription(i18nc("@info", "Number of CPU cores across all physical CPUS")); m_coreCount->setDescription(i18nc("@info", "Number of CPU cores across all physical CPUS"));
} }
...@@ -23,8 +23,13 @@ ...@@ -23,8 +23,13 @@
#include <SensorObject.h> #include <SensorObject.h>
class BaseCpuObject : public SensorObject { class BaseCpuObject : public SensorObject {
public:
protected: protected:
BaseCpuObject(const QString &id, const QString &name, SensorContainer *parent); BaseCpuObject(const QString &id, const QString &name, SensorContainer *parent);
virtual void initialize();
virtual void makeSensors();
SensorProperty *m_usage; SensorProperty *m_usage;
SensorProperty *m_system; SensorProperty *m_system;
SensorProperty *m_user; SensorProperty *m_user;
...@@ -36,6 +41,9 @@ public: ...@@ -36,6 +41,9 @@ public:
CpuObject(const QString &id, const QString &name, SensorContainer *parent); CpuObject(const QString &id, const QString &name, SensorContainer *parent);
protected: protected:
void initialize() override;
void makeSensors() override;
SensorProperty *m_frequency; SensorProperty *m_frequency;
SensorProperty *m_temperature; SensorProperty *m_temperature;
}; };
...@@ -45,6 +53,9 @@ public: ...@@ -45,6 +53,9 @@ public:
AllCpusObject(unsigned int cpuCount, unsigned int coreCount, SensorContainer *parent); AllCpusObject(unsigned int cpuCount, unsigned int coreCount, SensorContainer *parent);
protected: protected:
void initialize() override;
void makeSensors() override;
SensorProperty *m_cpuCount; SensorProperty *m_cpuCount;
SensorProperty *m_coreCount; SensorProperty *m_coreCount;
}; };
......
...@@ -17,24 +17,14 @@ static double readCpuFreq(const QString &cpuId, const QString &attribute, bool & ...@@ -17,24 +17,14 @@ static double readCpuFreq(const QString &cpuId, const QString &attribute, bool &
return 0; return 0;
} }
LinuxCpuObject::LinuxCpuObject(const QString &id, const QString &name, SensorContainer *parent, double frequency) TemperatureSensor::TemperatureSensor(const QString& id, SensorObject* parent)
: CpuObject(id, name, parent) : SensorProperty(id, parent)
, m_sensorChipName{nullptr} , m_sensorChipName{nullptr}
, m_temperatureSubfeature{-1} , m_temperatureSubfeature{-1}
{ {
m_frequency->setValue(frequency);
bool ok;
const double max = readCpuFreq(id, "cpuinfo_max_freq", ok);
if (ok) {
m_frequency->setMax(max);
}
const double min = readCpuFreq(id, "cpuinfo_min_freq", ok);
if (ok) {
m_frequency->setMin(min);
}
} }
void LinuxCpuObject::setTemperatureSensor(const sensors_chip_name * const chipName, const sensors_feature * const feature) void TemperatureSensor::setFeature(const sensors_chip_name *const chipName, const sensors_feature *const feature)
{ {
#ifdef HAVE_SENSORS #ifdef HAVE_SENSORS
m_sensorChipName = chipName; m_sensorChipName = chipName;
...@@ -42,19 +32,55 @@ void LinuxCpuObject::setTemperatureSensor(const sensors_chip_name * const chipNa ...@@ -42,19 +32,55 @@ void LinuxCpuObject::setTemperatureSensor(const sensors_chip_name * const chipNa
if (temperature) { if (temperature) {
m_temperatureSubfeature = temperature->number; m_temperatureSubfeature = temperature->number;
} }
// Typically temp_emergency > temp_crit > temp_max, but not every processor has them // Typically temp_emergency > temp_crit > temp_max, but not every processor has them
// see https://www.kernel.org/doc/html/latest/hwmon/sysfs-interface.html // see https://www.kernel.org/doc/html/latest/hwmon/sysfs-interface.html
double value; double value;
for (auto subfeatureType : {SENSORS_SUBFEATURE_TEMP_EMERGENCY, SENSORS_SUBFEATURE_TEMP_CRIT, SENSORS_SUBFEATURE_TEMP_MAX}) { for (auto subfeatureType : {SENSORS_SUBFEATURE_TEMP_EMERGENCY, SENSORS_SUBFEATURE_TEMP_CRIT, SENSORS_SUBFEATURE_TEMP_MAX}) {
const sensors_subfeature * const subfeature = sensors_get_subfeature(chipName, feature, subfeatureType); const sensors_subfeature * const subfeature = sensors_get_subfeature(chipName, feature, subfeatureType);
if (subfeature && sensors_get_value(chipName, subfeature->number, &value) == 0) { if (subfeature && sensors_get_value(chipName, subfeature->number, &value) == 0) {
m_temperature->setMax(value); setMax(value);
break; break;
} }
} }
#endif #endif
} }
void TemperatureSensor::update()
{
#ifdef HAVE_SENSORS
if (m_sensorChipName && m_temperatureSubfeature != -1) {
double value;
if (sensors_get_value(m_sensorChipName, m_temperatureSubfeature, &value) == 0) {
setValue(value);
}
}
#endif
}
LinuxCpuObject::LinuxCpuObject(const QString &id, const QString &name, SensorContainer *parent, double frequency)
: CpuObject(id, name, parent)
{
m_frequency->setValue(frequency);
bool ok;
const double max = readCpuFreq(id, "cpuinfo_max_freq", ok);
if (ok) {
m_frequency->setMax(max);
}
const double min = readCpuFreq(id, "cpuinfo_min_freq", ok);
if (ok) {
m_frequency->setMin(min);
}
}
void LinuxCpuObject::makeSensors()
{
BaseCpuObject::makeSensors();
m_frequency = new SensorProperty(QStringLiteral("frequency"), this);
m_temperatureSensor = new TemperatureSensor(QStringLiteral("temperature"), this);
m_temperature = m_temperatureSensor;
}
void LinuxCpuObject::update(unsigned long long system, unsigned long long user, unsigned long long wait, unsigned long long idle) void LinuxCpuObject::update(unsigned long long system, unsigned long long user, unsigned long long wait, unsigned long long idle)
{ {
if (!isSubscribed()) { if (!isSubscribed()) {
...@@ -83,14 +109,7 @@ void LinuxCpuObject::update(unsigned long long system, unsigned long long user, ...@@ -83,14 +109,7 @@ void LinuxCpuObject::update(unsigned long long system, unsigned long long user,
// frequency value changed even if the cpu apparently doesn't use CPUFreq? // frequency value changed even if the cpu apparently doesn't use CPUFreq?
// Third update temperature // Third update temperature
#ifdef HAVE_SENSORS m_temperatureSensor->update();
if (m_sensorChipName && m_temperatureSubfeature != -1) {
double value;
if (sensors_get_value(m_sensorChipName, m_temperatureSubfeature, &value) == 0) {
m_temperature->setValue(value);
}
}
#endif
} }
void LinuxAllCpusObject::update(unsigned long long system, unsigned long long user, unsigned long long wait, unsigned long long idle) { void LinuxAllCpusObject::update(unsigned long long system, unsigned long long user, unsigned long long wait, unsigned long long idle) {
...@@ -102,4 +121,9 @@ void LinuxAllCpusObject::update(unsigned long long system, unsigned long long us ...@@ -102,4 +121,9 @@ void LinuxAllCpusObject::update(unsigned long long system, unsigned long long us
m_usage->setValue(m_usageComputer.totalUsage); m_usage->setValue(m_usageComputer.totalUsage);
} }
TemperatureSensor *LinuxCpuObject::temperatureSensor()
{
return m_temperatureSensor;
}
...@@ -7,17 +7,28 @@ struct sensors_feature; ...@@ -7,17 +7,28 @@ struct sensors_feature;
#include "cpu.h" #include "cpu.h"
#include "usagecomputer.h" #include "usagecomputer.h"
class TemperatureSensor : public SensorProperty {
public:
TemperatureSensor(const QString &id, SensorObject *parent);
void setFeature(const sensors_chip_name * const chipName, const sensors_feature * const feature);
void update();
private:
const sensors_chip_name * m_sensorChipName;
int m_temperatureSubfeature;
};
class LinuxCpuObject : public CpuObject class LinuxCpuObject : public CpuObject
{ {
public: public:
LinuxCpuObject(const QString &id, const QString &name, SensorContainer *parent, double frequency = 0); LinuxCpuObject(const QString &id, const QString &name, SensorContainer *parent, double frequency = 0);
void update(unsigned long long system, unsigned long long user, unsigned long long wait, unsigned long long idle); void update(unsigned long long system, unsigned long long user, unsigned long long wait, unsigned long long idle);
void setTemperatureSensor(const sensors_chip_name * const chipName, const sensors_feature * const feature); TemperatureSensor* temperatureSensor();
private: private:
void makeSensors() override;
UsageComputer m_usageComputer; UsageComputer m_usageComputer;
const sensors_chip_name * m_sensorChipName; TemperatureSensor *m_temperatureSensor;
int m_temperatureSubfeature;
}; };
class LinuxAllCpusObject : public AllCpusObject { class LinuxAllCpusObject : public AllCpusObject {
......
...@@ -156,7 +156,7 @@ void LinuxCpuPluginPrivate::addSensorsIntel(const sensors_chip_name * const chip ...@@ -156,7 +156,7 @@ void LinuxCpuPluginPrivate::addSensorsIntel(const sensors_chip_name * const chip
// Naturally they share the same temperature sensor and have the same coreId. // Naturally they share the same temperature sensor and have the same coreId.
auto cpu_range = m_cpusBySystemIds.equal_range({physicalId, feature.key()}); auto cpu_range = m_cpusBySystemIds.equal_range({physicalId, feature.key()});
for (auto cpu_it = cpu_range.first; cpu_it != cpu_range.second; ++cpu_it) { for (auto cpu_it = cpu_range.first; cpu_it != cpu_range.second; ++cpu_it) {
(*cpu_it)->setTemperatureSensor(chipName, feature.value()); (*cpu_it)->temperatureSensor()->setFeature(chipName, feature.value());
} }
} }
} }
...@@ -196,7 +196,7 @@ void LinuxCpuPluginPrivate::addSensorsAmd(const sensors_chip_name * const chipNa ...@@ -196,7 +196,7 @@ void LinuxCpuPluginPrivate::addSensorsAmd(const sensors_chip_name * const chipNa
auto setSingleSensor = [this, chipName] (const sensors_feature * const feature) { auto setSingleSensor = [this, chipName] (const sensors_feature * const feature) {
for (auto &cpu : m_cpusBySystemIds) { for (auto &cpu : m_cpusBySystemIds) {
cpu->setTemperatureSensor(chipName, feature); cpu->temperatureSensor()->setFeature(chipName, feature);
} }
}; };
if (tdie) { if (tdie) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment