diff options
Diffstat (limited to 'superkaramba/src')
-rw-r--r-- | superkaramba/src/CMakeLists.txt | 4 | ||||
-rw-r--r-- | superkaramba/src/gpusensor.cpp | 129 | ||||
-rw-r--r-- | superkaramba/src/gpusensor.h | 52 | ||||
-rw-r--r-- | superkaramba/src/karamba.cpp | 20 | ||||
-rw-r--r-- | superkaramba/src/karamba.h | 7 | ||||
-rw-r--r-- | superkaramba/src/memsensor.cpp | 78 |
6 files changed, 263 insertions, 27 deletions
diff --git a/superkaramba/src/CMakeLists.txt b/superkaramba/src/CMakeLists.txt index 211ee0c..c38d41b 100644 --- a/superkaramba/src/CMakeLists.txt +++ b/superkaramba/src/CMakeLists.txt @@ -53,8 +53,8 @@ tde_add_executable( superkaramba AUTOMOC sknewstuff.h sknewstuff.cpp superkarambasettings.kcfgc themelocale.cpp input.cpp sklineedit.cpp input_python.cpp - svcgrp_python.cpp - LINK tdeio-shared ${PYTHON_LIBRARIES} + svcgrp_python.cpp gpusensor.cpp + LINK tdeio-shared ${PYTHON_LIBRARIES} ${TDENEWSTUFF_LIBRARIES} ${LIBKVM_LIBRARIES} DESTINATION ${BIN_INSTALL_DIR} diff --git a/superkaramba/src/gpusensor.cpp b/superkaramba/src/gpusensor.cpp new file mode 100644 index 0000000..d95de59 --- /dev/null +++ b/superkaramba/src/gpusensor.cpp @@ -0,0 +1,129 @@ +/******************************************************************************* + GPU sensor + Copyright (C) 2024 Mavridis Philippe <mavridisf@gmail.com> + + This program is free software: you can redistribute it and/or modify it under + the terms of the GNU General Public License as published by the Free Software + Foundation, either version 3 of the License, or (at your option) any later + version. + + This program 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 General Public License for more details. + You should have received a copy of the GNU General Public License along with + this program. If not, see <http://www.gnu.org/licenses/>. + + Improvements and feedback are welcome! +*******************************************************************************/ + +// TQt +#include <tqregexp.h> + +// TDE +#include <kstandarddirs.h> +#include <kdebug.h> + +// Superkaramba +#include "gpusensor.h" + +GPUSensor::GPUSensor(TQString gpuDriver, TQString gpuId, int interval) + : Sensor(interval), m_gpuDriver(gpuDriver), m_gpuId(gpuId) +{ + if (m_gpuDriver.lower() == "nvidia") + { + TQString nvsmi = TDEStandardDirs::findExe("nvidia-smi"); + if (nvsmi.isNull()) + { + kdError() << "The NVidia System Management Interface software is not avaiable." << endl; + return; + } + + m_command << nvsmi << "--query-gpu" << "utilization.gpu" + << "--format=csv,noheader"; + + if (!m_gpuId.isNull()) + { + m_command << TQString("--id=%1").arg(m_gpuId); + } + } + + else + { + kdError() << "Unsupported driver specified for GPU sensor (" << m_gpuDriver << ");" << endl + << "\tSupported drivers are: nvidia" << endl; + } + + connect(&m_proc, TQ_SIGNAL(receivedStdout(TDEProcess*, char*, int)), + this, TQ_SLOT(receivedStdout(TDEProcess*, char*, int))); + connect(&m_proc, TQ_SIGNAL(processExited(TDEProcess*)), + this, TQ_SLOT(processExited(TDEProcess*))); +} + +GPUSensor::~GPUSensor() +{ +} + +void GPUSensor::update() +{ + if (m_command.isEmpty()) return; + + m_proc.clearArguments(); + m_proc << m_command; + + m_proc.start(TDEProcess::NotifyOnExit, TDEProcess::Stdout); +} + +void GPUSensor::receivedStdout(TDEProcess *proc, char *buffer, int buflen) +{ + buffer[buflen] = 0; + m_buffer += TQCString(buffer); +} + +TQString GPUSensor::getLoad() +{ + if (m_gpuDriver.lower() == "nvidia") + { + return m_buffer.left(m_buffer.length() - 3); + } + + return TQString::null; +} + +#define SUB_FORMAT_STR(fstring, value) \ + format.replace(TQRegExp(#fstring, false), value) +void GPUSensor::processExited(TDEProcess *proc) +{ + SensorParams *sp; + Meter *meter; + TQString format; + TQString load = getLoad(); + m_buffer = TQString::null; + + TQObjectListIt it(*objList); + while (it != 0) + { + sp = (SensorParams*)(*it); + meter = sp->getMeter(); + format = sp->getParam("FORMAT"); + + if( format.length() == 0) + { + format = "%v"; + } + + SUB_FORMAT_STR(%load, load); + SUB_FORMAT_STR(%v, load); + + meter->setValue(format); + ++it; + } +} + +#undef SUB_FORMAT_STR + +void GPUSensor::setMaxValue(SensorParams *sp) +{ + sp->getMeter()->setMax(100); +} + +#include "gpusensor.moc"
\ No newline at end of file diff --git a/superkaramba/src/gpusensor.h b/superkaramba/src/gpusensor.h new file mode 100644 index 0000000..178e52e --- /dev/null +++ b/superkaramba/src/gpusensor.h @@ -0,0 +1,52 @@ +/******************************************************************************* + GPU sensor + Copyright (C) 2024 Mavridis Philippe <mavridisf@gmail.com> + + This program is free software: you can redistribute it and/or modify it under + the terms of the GNU General Public License as published by the Free Software + Foundation, either version 3 of the License, or (at your option) any later + version. + + This program 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 General Public License for more details. + You should have received a copy of the GNU General Public License along with + this program. If not, see <http://www.gnu.org/licenses/>. + + Improvements and feedback are welcome! +*******************************************************************************/ + +#ifndef GPUSENSOR_H +#define GPUSENSOR_H + +// TDE +#include <kprocess.h> + +// Superkaramba +#include "sensor.h" + +class GPUSensor : public Sensor +{ + TQ_OBJECT + + public: + GPUSensor(TQString driver, TQString gpuId, int interval = 1000); + ~GPUSensor(); + + void update(); + void setMaxValue(SensorParams *sp); + + protected: + TQString getLoad(); + + private: + TQString m_gpuDriver, m_gpuId, m_buffer; + TQStringList m_command; + TDEProcess m_proc; + + public slots: + void receivedStdout(TDEProcess *proc, char *buffer, int buflen); + void processExited(TDEProcess *proc); +}; + +#endif // GPUSENSOR_H
\ No newline at end of file diff --git a/superkaramba/src/karamba.cpp b/superkaramba/src/karamba.cpp index d4a74f7..d828411 100644 --- a/superkaramba/src/karamba.cpp +++ b/superkaramba/src/karamba.cpp @@ -988,6 +988,26 @@ void karamba::setSensor(const LineParser& lineParser, Meter* meter) } + if (sens == "GPU") + { + TQString gpuId = lineParser.getString("GPU"); + TQString gpuDriver = lineParser.getString("DRIVER"); + TQString sensorId = TQString("GPU_%1_%2").arg(gpuDriver, gpuId); + sensor = sensorMap[sensorId]; + if (sensor == 0) + { + int interval = lineParser.getInt("INTERVAL"); + interval = (interval == 0) ? 1000 : interval; + sensor = (sensorMap[sensorId] = new GPUSensor(gpuDriver, gpuId, interval)); + sensorList->append(sensor); + } + + SensorParams *sp = new SensorParams(meter); + sp->addParam("FORMAT", m_theme.locale()->translate(lineParser.getString("FORMAT").ascii())); + sensor->addMeter(sp); + sensor->setMaxValue(sp); + } + if( sens == "MEMORY" ) { sensor = sensorMap["MEMORY"]; diff --git a/superkaramba/src/karamba.h b/superkaramba/src/karamba.h index 57843d7..77f8f5c 100644 --- a/superkaramba/src/karamba.h +++ b/superkaramba/src/karamba.h @@ -82,6 +82,7 @@ #include "uptimesensor.h" #include "memsensor.h" #include "cpusensor.h" +#include "gpusensor.h" #include "networksensor.h" #include "xmmssensor.h" #include "noatunsensor.h" @@ -112,7 +113,7 @@ class LineParser; class karamba : public TQWidget { TQ_OBJECT - + public: karamba(TQString fn, TQString name, bool reloading = false, @@ -327,7 +328,7 @@ private slots: class DesktopChangeSlot : public TQObject { TQ_OBJECT - + public: DesktopChangeSlot(TQObject *parent, int desktop_id); @@ -348,7 +349,7 @@ class DesktopChangeSlot : public TQObject class SignalBridge : public TQObject { TQ_OBJECT - + public: SignalBridge(TQObject* parent, TQString, TDEActionCollection*); diff --git a/superkaramba/src/memsensor.cpp b/superkaramba/src/memsensor.cpp index 1d6c4dc..d3ee39b 100644 --- a/superkaramba/src/memsensor.cpp +++ b/superkaramba/src/memsensor.cpp @@ -210,9 +210,9 @@ int MemSensor::getSwapTotal() mib[1] = VM_UVMEXP; ssize = sizeof(uvmexp); - if (sysctl(mib,2,&uvmexp,&ssize,NULL,0) != -1) { - pagesize = uvmexp.pagesize; - STotal = (pagesize*uvmexp.swpages) >> 10; + if (sysctl(mib, 2, &uvmexp, &ssize, NULL, 0) != -1) { + pagesize = uvmexp.pagesize; + STotal = (pagesize*uvmexp.swpages) >> 10; } return STotal; #else @@ -254,10 +254,10 @@ int MemSensor::getSwapFree() ssize = sizeof(uvmexp); if (sysctl(mib,2,&uvmexp,&ssize,NULL,0) != -1) { - pagesize = uvmexp.pagesize; - STotal = (pagesize*uvmexp.swpages) >> 10; - SUsed = (pagesize*uvmexp.swpginuse) >> 10; - SFree = STotal - SUsed; + pagesize = uvmexp.pagesize; + STotal = (pagesize*uvmexp.swpages) >> 10; + SUsed = (pagesize*uvmexp.swpginuse) >> 10; + SFree = STotal - SUsed; } return SFree; #else @@ -287,6 +287,9 @@ void MemSensor::readValues() #endif } +#define SUB_FORMAT_STR(fstring, value) \ + format.replace(TQRegExp(#fstring, false), TQString::number((int)(value))) + void MemSensor::update() { readValues(); @@ -319,27 +322,46 @@ void MemSensor::update() format = "%um"; } - format.replace( TQRegExp("%fmb", false), TQString::number( (int)(( totalMem - usedMemNoBuffers)/1024.0+0.5))); - format.replace( TQRegExp("%fm", false), TQString::number( (int)( ( totalMem - usedMem )/1024.0+0.5) )); - - format.replace( TQRegExp("%umb", false), TQString::number( (int)((usedMemNoBuffers)/1024.0+0.5))); - format.replace( TQRegExp("%um", false), TQString::number( (int)((usedMem)/1024.0+0.5 ))); - - format.replace( TQRegExp("%tm", false), TQString::number( (int)( (totalMem)/1024.0+0.5))); - - format.replace( TQRegExp("%fs", false), TQString::number( (int)((totalSwap - usedSwap)/1024.0+0.5))); - format.replace( TQRegExp("%us", false), TQString::number( (int)(usedSwap/1024.0+0.5))); - format.replace( TQRegExp("%ts", false), TQString::number( (int)(totalSwap/1024.0+0.5))); + SUB_FORMAT_STR(%fmbp, (totalMem - usedMemNoBuffers) * 100.0 / totalMem); + SUB_FORMAT_STR(%fmbg, (totalMem - usedMemNoBuffers) / 1024.0 / 1024.0 + 1); + SUB_FORMAT_STR(%fmb, (totalMem - usedMemNoBuffers) / 1024.0 + 0.5); + SUB_FORMAT_STR(%fmp, (totalMem - usedMem) * 100.0 / totalMem); + SUB_FORMAT_STR(%fmg, (totalMem - usedMem) / 1024.0 / 1024.0 + 1); + SUB_FORMAT_STR(%fm, (totalMem - usedMem) / 1024.0 + 0.5); + + SUB_FORMAT_STR(%umbp, usedMemNoBuffers * 100.0 / totalMem); + SUB_FORMAT_STR(%umbg, usedMemNoBuffers / 1024.0 / 1024.0 + 1); + SUB_FORMAT_STR(%umb, usedMemNoBuffers / 1024.0 + 0.5); + SUB_FORMAT_STR(%ump, usedMem * 100.0 / totalMem); + SUB_FORMAT_STR(%umg, usedMem / 1024.0 / 1024.0 + 1); + SUB_FORMAT_STR(%um, usedMem / 1024.0 + 0.5); + + SUB_FORMAT_STR(%tmg, totalMem / 1024.0 / 1024.0 + 1); + SUB_FORMAT_STR(%tm, totalMem / 1024.0 + 0.5); + + SUB_FORMAT_STR(%fsp, (totalSwap - usedSwap) * 100.0 / totalSwap); + SUB_FORMAT_STR(%fsg, (totalSwap - usedSwap) / 1024.0 / 1024.0 + 1); + SUB_FORMAT_STR(%fs, (totalSwap - usedSwap) / 1024.0 + 0.5); + SUB_FORMAT_STR(%usp, usedSwap * 100.0 / totalSwap); + SUB_FORMAT_STR(%usg, usedSwap / 1024.0 / 1024.0 + 1); + SUB_FORMAT_STR(%us, usedSwap / 1024.0 + 0.5); + + SUB_FORMAT_STR(%tsg, totalSwap / 1024.0 / 1024.0 + 1); + SUB_FORMAT_STR(%ts, totalSwap / 1024.0 + 0.5); meter->setValue(format); ++it; } #if defined(Q_OS_FREEBSD) && !(defined(__FreeBSD_version) && __FreeBSD_version >= 500018) if (set) + { MaxSet = true; + } #endif } +#undef SUB_FORMAT_STR + void MemSensor::setMaxValue( SensorParams *sp ) { Meter *meter; @@ -351,10 +373,22 @@ void MemSensor::setMaxValue( SensorParams *sp ) { f = "%um"; } - if( f=="%fm" || f== "%um" || f=="%fmb" || f=="%umb" ) - meter->setMax( getMemTotal() / 1024 ); - if( f=="%fs" || f== "%us" ) - meter->setMax( getSwapTotal() / 1024 ); + else if (f.endsWith("p")) + { + meter->setMax(100); + } + else if (f == "%fm" || f == "%um" || f == "%fmb" || f == "%umb") + { + meter->setMax(getMemTotal() / 1024); + } + else if (f == "%fmg" || f == "%umg" || f == "%fmbg" || f == "%umbg") + { + meter->setMax(getMemTotal() / 1024 / 1024); + } + else if (f == "%fs" || f == "%us" || f == "%fsg" || f == "%usg") + { + meter->setMax(getSwapTotal() / 1024); + } } #include "memsensor.moc" |