Changeset View
Changeset View
Standalone View
Standalone View
processplugins/nvidia/nvidia.cpp
- This file was added.
1 | /* This file is part of the KDE project | ||||
---|---|---|---|---|---|
2 | | ||||
3 | Copyright (C) 2019 David Edmundson <davidedmundson@kde.org> | ||||
4 | | ||||
5 | This library is free software; you can redistribute it and/or | ||||
6 | modify it under the terms of the GNU Library General Public | ||||
7 | License as published by the Free Software Foundation; either | ||||
8 | version 2 of the License, or (at your option) any later version. | ||||
9 | | ||||
10 | This library is distributed in the hope that it will be useful, | ||||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
13 | Library General Public License for more details. | ||||
14 | | ||||
15 | You should have received a copy of the GNU Library General Public License | ||||
16 | along with this library; see the file COPYING.LIB. If not, write to | ||||
17 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||||
18 | Boston, MA 02110-1301, USA. | ||||
19 | */ | ||||
20 | | ||||
21 | #include "nvidia.h" | ||||
22 | | ||||
23 | #include <QStandardPaths> | ||||
24 | #include <QProcess> | ||||
25 | #include <QDebug> | ||||
26 | | ||||
27 | #include <KLocalizedString> | ||||
28 | #include <KPluginFactory> | ||||
29 | | ||||
30 | #include <processcore/process.h> | ||||
31 | | ||||
32 | using namespace KSysGuard; | ||||
33 | | ||||
34 | NvidiaPlugin::NvidiaPlugin(QObject *parent, const QVariantList &args) | ||||
35 | : ProcessDataProvider(parent, args) | ||||
36 | { | ||||
37 | m_sniExecutablePath = QStandardPaths::findExecutable(QStringLiteral("nvidia-smi")); | ||||
38 | if (m_sniExecutablePath.isEmpty()) { | ||||
39 | return; | ||||
40 | } | ||||
41 | | ||||
42 | m_usage = new ProcessAttribute(QStringLiteral("nvidia_usage"), i18n("GPU Usage"), this); | ||||
43 | m_usage->setUnit(KSysGuard::UnitPercent); | ||||
44 | m_memory = new ProcessAttribute(QStringLiteral("nvidia_memory"), i18n("GPU Memory"), this); | ||||
45 | m_memory->setUnit(KSysGuard::UnitPercent); | ||||
46 | | ||||
47 | addProcessAttribute(m_usage); | ||||
48 | addProcessAttribute(m_memory); | ||||
49 | } | ||||
50 | | ||||
51 | void NvidiaPlugin::handleEnabledChanged(bool enabled) | ||||
52 | { | ||||
53 | if (enabled) { | ||||
54 | if (!m_process) { | ||||
55 | setup(); | ||||
56 | } | ||||
57 | m_process->start(); | ||||
58 | } else { | ||||
59 | if (m_process) { | ||||
60 | m_process->terminate(); | ||||
61 | } | ||||
62 | } | ||||
63 | } | ||||
64 | | ||||
65 | void NvidiaPlugin::setup() | ||||
66 | { | ||||
67 | m_process = new QProcess(this); | ||||
68 | m_process->setProgram(m_sniExecutablePath); | ||||
69 | m_process->setArguments({QStringLiteral("pmon")}); | ||||
70 | | ||||
71 | connect(m_process, &QProcess::readyReadStandardOutput, this, [=]() { | ||||
72 | while (m_process->canReadLine()) { | ||||
73 | const QString line = QString::fromLatin1(m_process->readLine()); | ||||
74 | if (line.startsWith(QLatin1Char('#'))) { //comment line | ||||
75 | if (line != QLatin1String("# gpu pid type sm mem enc dec command\n") && | ||||
76 | line != QLatin1String("# Idx # C/G % % % % name\n")) { | ||||
77 | //header format doesn't match what we expected, bail before we send any garbage | ||||
78 | m_process->terminate(); | ||||
79 | } | ||||
80 | continue; | ||||
81 | } | ||||
82 | const auto parts = line.splitRef(QLatin1Char(' '), QString::SkipEmptyParts); | ||||
83 | | ||||
84 | // format at time of writing is | ||||
85 | // # gpu pid type sm mem enc dec command | ||||
86 | if (parts.count() != 9) { | ||||
87 | continue; | ||||
88 | } | ||||
89 | | ||||
90 | long pid = parts[1].toUInt(); | ||||
91 | int sm = parts[3].toUInt(); | ||||
92 | int mem = parts[4].toUInt(); | ||||
93 | | ||||
94 | KSysGuard::Process *process = getProcess(pid); | ||||
95 | if(!process) { | ||||
96 | continue; //can in race condition etc | ||||
97 | } | ||||
98 | m_usage->setData(process, sm); | ||||
99 | m_memory->setData(process, mem); | ||||
100 | } | ||||
101 | }); | ||||
102 | } | ||||
103 | | ||||
104 | K_PLUGIN_FACTORY_WITH_JSON(PluginFactory, "nvidia.json", registerPlugin<NvidiaPlugin>();) | ||||
105 | | ||||
106 | #include "nvidia.moc" |