Commit 14ac9b56 authored by David Redondo's avatar David Redondo 🏎

Add sysctl sensor and use it for temperatur and frequencY

parent 0221cfc6
#include "freebsdcpuplugin.h"
#include "sysctlsensor.h"
#include <algorithm>
#include <vector>
......@@ -19,10 +21,28 @@ bool readSysctl(const char *name, T *buffer, size_t size = sizeof(T)) {
FreeBsdCpuObject::FreeBsdCpuObject(const QString &id, const QString &name, SensorContainer *parent)
: CpuObject(id, name, parent)
{
}
void FreeBsdCpuObject::makeSensors()
{
BaseCpuObject::makeSensors();
const QByteArray prefix = QByteArrayLiteral("dev.cpu.") + id().right(1).toLocal8Bit();
auto freq = new SysctlSensor<int>(QStringLiteral("frequency"), prefix + QByteArrayLiteral(".freq"), this);
auto temp = new SysctlSensor<int>(QStringLiteral("temperature"), prefix + QByteArrayLiteral(".freq"), this);
m_sysctlSensors.append({freq, temp});
m_frequency = freq;
m_temperature = temp;
}
void FreeBsdCpuObject::initialize()
{
const QByteArray prefix = QByteArrayLiteral("dev.cpu.") + id().right(1).toLocal8Bit();
// For min and max frequency we have to parse the values return by freq_levels because only
// minimum is exposed as a single value
size_t size;
const QByteArray levelsName = QByteArrayLiteral("dev.cpu.") + id.right(1).toLocal8Bit() + QByteArrayLiteral(".freq_levels");
const QByteArray levelsName = prefix + QByteArrayLiteral(".freq_levels");
// calling sysctl with nullptr writes the needed size to size
if (sysctlbyname(levelsName, nullptr, &size, nullptr, 0) != -1) {
QByteArray freqLevels(size, Qt::Uninitialized);
if (sysctlbyname(levelsName, freqLevels.data(), &size, nullptr, 0) != -1) {
......@@ -40,7 +60,7 @@ FreeBsdCpuObject::FreeBsdCpuObject(const QString &id, const QString &name, Senso
m_frequency->setMax(max);
}
}
const QByteArray tjmax = QByteArrayLiteral("dev.cpu.") + id.right(1).toLocal8Bit() + QByteArrayLiteral(".coretemp.tjmax");
const QByteArray tjmax = prefix + QByteArrayLiteral(".coretemp.tjmax");
int maxTemperature;
// This is only availabel on Intel (using the coretemp driver)
if (readSysctl(tjmax.constData(), &maxTemperature)) {
......@@ -60,18 +80,9 @@ void FreeBsdCpuObject::update(long system, long user, long idle)
m_system->setValue(m_usageComputer.systemUsage);
m_user->setValue(m_usageComputer.userUsage);
m_usage->setValue(m_usageComputer.totalUsage);
int frequency;
const QByteArray prefix = QByteArrayLiteral("dev.cpu.") + id().right(1).toLocal8Bit();
const QByteArray freq = prefix + QByteArrayLiteral(".freq");
if (readSysctl(freq.constData(), &frequency)){
// value is already in MHz
m_frequency->setValue(frequency);
}
int temperature;
const QByteArray temp = prefix + QByteArrayLiteral(".temperature");
if (readSysctl(temp.constData(), &temperature)) {
m_temperature->setValue(temperature);
for (auto sensor : m_sysctlSensors) {
sensor->update();
}
}
......
......@@ -5,12 +5,18 @@
#include "cpuplugin_p.h"
#include "usagecomputer.h"
template <typename T>
class SysctlSensor;
class FreeBsdCpuObject : public CpuObject {
public:
FreeBsdCpuObject(const QString &id, const QString &name, SensorContainer *parent);
void update(long system, long user, long idle);
private:
void makeSensors() override;
void initialize() override;
UsageComputer m_usageComputer;
QVector<SysctlSensor<int>*> m_sysctlSensors;
};
class FreeBsdAllCpusObject : public AllCpusObject {
......
/*
Copyright (c) 2020 David Redondo <kde@david-redondo.de>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#ifndef SYSCTLSENSOR_H
#define SYSCTLSENSOR_H
#include <SensorProperty.h>
template <typename T>
class SysctlSensor : public SensorProperty {
public:
SysctlSensor(const QString &id, const QByteArray &sysctlName, SensorObject *parent);
void update();
private:
const QByteArray m_sysctlName;
};
#ifdef Q_OS_FREEBSD
#include <sys/sysctl.h>
template <typename T>
SysctlSensor<T>::SysctlSensor(const QString& id, const QByteArray &sysctlName, SensorObject* parent)
: SensorProperty(id, parent)
, m_sysctlName(sysctlName)
{
}
template <typename T>
void SysctlSensor<T>::update()
{
T value;
size_t size = sizeof(T);
if (sysctlbyname(m_sysctlName.constData(), &value, &size, nullptr, 0) != -1) {
setValue(value);
}
}
#endif
#endif
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