• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • superkaramba
 

superkaramba

  • superkaramba
  • src
gpusensor.cpp
1/*******************************************************************************
2 GPU sensor
3 Copyright (C) 2024 Mavridis Philippe <mavridisf@gmail.com>
4
5 This program is free software: you can redistribute it and/or modify it under
6 the terms of the GNU General Public License as published by the Free Software
7 Foundation, either version 3 of the License, or (at your option) any later
8 version.
9
10 This program is distributed in the hope that it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License along with
14 this program. If not, see <http://www.gnu.org/licenses/>.
15
16 Improvements and feedback are welcome!
17*******************************************************************************/
18
19// TQt
20#include <tqregexp.h>
21
22// TDE
23#include <kstandarddirs.h>
24#include <kdebug.h>
25
26// Superkaramba
27#include "gpusensor.h"
28
29GPUSensor::GPUSensor(TQString gpuDriver, TQString gpuId, int interval)
30 : Sensor(interval), m_gpuDriver(gpuDriver), m_gpuId(gpuId)
31{
32 if (m_gpuDriver.lower() == "nvidia")
33 {
34 TQString nvsmi = TDEStandardDirs::findExe("nvidia-smi");
35 if (nvsmi.isNull())
36 {
37 kdError() << "The NVidia System Management Interface software is not avaiable." << endl;
38 return;
39 }
40
41 m_command << nvsmi << "--query-gpu" << "utilization.gpu"
42 << "--format=csv,noheader";
43
44 if (!m_gpuId.isNull())
45 {
46 m_command << TQString("--id=%1").arg(m_gpuId);
47 }
48 }
49
50 else
51 {
52 kdError() << "Unsupported driver specified for GPU sensor (" << m_gpuDriver << ");" << endl
53 << "\tSupported drivers are: nvidia" << endl;
54 }
55
56 connect(&m_proc, TQ_SIGNAL(receivedStdout(TDEProcess*, char*, int)),
57 this, TQ_SLOT(receivedStdout(TDEProcess*, char*, int)));
58 connect(&m_proc, TQ_SIGNAL(processExited(TDEProcess*)),
59 this, TQ_SLOT(processExited(TDEProcess*)));
60}
61
62GPUSensor::~GPUSensor()
63{
64}
65
66void GPUSensor::update()
67{
68 if (m_command.isEmpty()) return;
69
70 m_proc.clearArguments();
71 m_proc << m_command;
72
73 m_proc.start(TDEProcess::NotifyOnExit, TDEProcess::Stdout);
74}
75
76void GPUSensor::receivedStdout(TDEProcess *proc, char *buffer, int buflen)
77{
78 buffer[buflen] = 0;
79 m_buffer += TQCString(buffer);
80}
81
82TQString GPUSensor::getLoad()
83{
84 if (m_gpuDriver.lower() == "nvidia")
85 {
86 return m_buffer.left(m_buffer.length() - 3);
87 }
88
89 return TQString::null;
90}
91
92#define SUB_FORMAT_STR(fstring, value) \
93 format.replace(TQRegExp(#fstring, false), value)
94void GPUSensor::processExited(TDEProcess *proc)
95{
96 SensorParams *sp;
97 Meter *meter;
98 TQString format;
99 TQString load = getLoad();
100 m_buffer = TQString::null;
101
102 TQObjectListIt it(*objList);
103 while (it != 0)
104 {
105 sp = (SensorParams*)(*it);
106 meter = sp->getMeter();
107 format = sp->getParam("FORMAT");
108
109 if( format.length() == 0)
110 {
111 format = "%v";
112 }
113
114 SUB_FORMAT_STR(%load, load);
115 SUB_FORMAT_STR(%v, load);
116
117 meter->setValue(format);
118 ++it;
119 }
120}
121
122#undef SUB_FORMAT_STR
123
124void GPUSensor::setMaxValue(SensorParams *sp)
125{
126 sp->getMeter()->setMax(100);
127}
128
129#include "gpusensor.moc"
SensorParams
Hans Karlsson.
Definition: sensorparams.h:32

superkaramba

Skip menu "superkaramba"
  • Main Page
  • Alphabetical List
  • Class List
  • File List
  • Class Members

superkaramba

Skip menu "superkaramba"
  • kcalc
  •   knumber
  • superkaramba
Generated for superkaramba by doxygen 1.9.4
This website is maintained by Timothy Pearson.