|
1 | 1 | #include "gpumonitor.h" |
2 | 2 | #include "system/commandrunner.h" |
3 | 3 |
|
| 4 | +#include <QDir> |
| 5 | +#include <QFile> |
4 | 6 | #include <QRegularExpression> |
5 | 7 | #include <algorithm> |
6 | 8 |
|
@@ -37,6 +39,119 @@ bool parseMetricInt(const QString &field, int *value) { |
37 | 39 | return true; |
38 | 40 | } |
39 | 41 |
|
| 42 | +QString drmRootPath() { |
| 43 | + const QString overridePath = |
| 44 | + qEnvironmentVariable("RO_CONTROL_DRM_ROOT").trimmed(); |
| 45 | + return overridePath.isEmpty() ? QStringLiteral("/sys/class/drm") |
| 46 | + : overridePath; |
| 47 | +} |
| 48 | + |
| 49 | +QString readFileText(const QString &path) { |
| 50 | + QFile file(path); |
| 51 | + if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { |
| 52 | + return {}; |
| 53 | + } |
| 54 | + |
| 55 | + return QString::fromUtf8(file.readAll()).trimmed(); |
| 56 | +} |
| 57 | + |
| 58 | +bool readIntegerFile(const QString &path, qint64 *value) { |
| 59 | + if (value == nullptr) { |
| 60 | + return false; |
| 61 | + } |
| 62 | + |
| 63 | + bool ok = false; |
| 64 | + const qint64 parsedValue = readFileText(path).toLongLong(&ok); |
| 65 | + if (!ok) { |
| 66 | + return false; |
| 67 | + } |
| 68 | + |
| 69 | + *value = parsedValue; |
| 70 | + return true; |
| 71 | +} |
| 72 | + |
| 73 | +bool readFirstTemperatureFromHwmon(const QString &basePath, int *value) { |
| 74 | + const QFileInfoList hwmonEntries = |
| 75 | + QDir(basePath).entryInfoList({QStringLiteral("hwmon*")}, |
| 76 | + QDir::Dirs | QDir::NoDotAndDotDot, |
| 77 | + QDir::Name); |
| 78 | + for (const QFileInfo &entry : hwmonEntries) { |
| 79 | + const QFileInfoList inputs = QDir(entry.absoluteFilePath()) |
| 80 | + .entryInfoList({QStringLiteral("temp*_input")}, |
| 81 | + QDir::Files, QDir::Name); |
| 82 | + for (const QFileInfo &input : inputs) { |
| 83 | + qint64 milliC = 0; |
| 84 | + if (readIntegerFile(input.absoluteFilePath(), &milliC) && milliC > 0) { |
| 85 | + *value = static_cast<int>(milliC / 1000); |
| 86 | + return true; |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + return false; |
| 92 | +} |
| 93 | + |
| 94 | +bool readGenericLinuxGpuMetrics(int *temperatureC, int *utilizationPercent, |
| 95 | + int *memoryUsedMiB, int *memoryTotalMiB) { |
| 96 | + const QFileInfoList cardEntries = |
| 97 | + QDir(drmRootPath()).entryInfoList({QStringLiteral("card*")}, |
| 98 | + QDir::Dirs | QDir::NoDotAndDotDot, |
| 99 | + QDir::Name); |
| 100 | + |
| 101 | + bool anyMetric = false; |
| 102 | + for (const QFileInfo &cardEntry : cardEntries) { |
| 103 | + const QString devicePath = |
| 104 | + cardEntry.absoluteFilePath() + QStringLiteral("/device"); |
| 105 | + if (!QFile::exists(devicePath)) { |
| 106 | + continue; |
| 107 | + } |
| 108 | + |
| 109 | + int tempValue = 0; |
| 110 | + if (temperatureC != nullptr && |
| 111 | + readFirstTemperatureFromHwmon(devicePath, &tempValue)) { |
| 112 | + *temperatureC = tempValue; |
| 113 | + anyMetric = true; |
| 114 | + } |
| 115 | + |
| 116 | + qint64 busyPercent = 0; |
| 117 | + if (utilizationPercent != nullptr && |
| 118 | + readIntegerFile(devicePath + QStringLiteral("/gpu_busy_percent"), |
| 119 | + &busyPercent)) { |
| 120 | + *utilizationPercent = |
| 121 | + std::clamp(static_cast<int>(busyPercent), 0, 100); |
| 122 | + anyMetric = true; |
| 123 | + } |
| 124 | + |
| 125 | + qint64 usedBytes = 0; |
| 126 | + qint64 totalBytes = 0; |
| 127 | + const bool usedOk = |
| 128 | + readIntegerFile(devicePath + QStringLiteral("/mem_info_vram_used"), |
| 129 | + &usedBytes); |
| 130 | + const bool totalOk = |
| 131 | + readIntegerFile(devicePath + QStringLiteral("/mem_info_vram_total"), |
| 132 | + &totalBytes); |
| 133 | + if (usedOk && totalOk && totalBytes > 0) { |
| 134 | + if (memoryUsedMiB != nullptr) { |
| 135 | + *memoryUsedMiB = |
| 136 | + std::max(0, static_cast<int>(static_cast<qint64>(usedBytes) / |
| 137 | + (1024 * 1024))); |
| 138 | + } |
| 139 | + if (memoryTotalMiB != nullptr) { |
| 140 | + *memoryTotalMiB = |
| 141 | + std::max(0, static_cast<int>(static_cast<qint64>(totalBytes) / |
| 142 | + (1024 * 1024))); |
| 143 | + } |
| 144 | + anyMetric = true; |
| 145 | + } |
| 146 | + |
| 147 | + if (anyMetric) { |
| 148 | + return true; |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + return false; |
| 153 | +} |
| 154 | + |
40 | 155 | } // namespace |
41 | 156 |
|
42 | 157 | GpuMonitor::GpuMonitor(QObject *parent) : QObject(parent) { |
@@ -80,8 +195,48 @@ void GpuMonitor::refresh() { |
80 | 195 | options); |
81 | 196 |
|
82 | 197 | if (!result.success()) { |
83 | | - setAvailable(false); |
84 | | - clearMetrics(); |
| 198 | + int nextTemp = 0; |
| 199 | + int nextUtil = 0; |
| 200 | + int nextUsed = 0; |
| 201 | + int nextTotal = 0; |
| 202 | + |
| 203 | + if (!readGenericLinuxGpuMetrics(&nextTemp, &nextUtil, &nextUsed, |
| 204 | + &nextTotal)) { |
| 205 | + setAvailable(false); |
| 206 | + clearMetrics(); |
| 207 | + return; |
| 208 | + } |
| 209 | + |
| 210 | + if (m_temperatureC != nextTemp) { |
| 211 | + m_temperatureC = nextTemp; |
| 212 | + emit temperatureCChanged(); |
| 213 | + } |
| 214 | + if (m_utilizationPercent != nextUtil) { |
| 215 | + m_utilizationPercent = nextUtil; |
| 216 | + emit utilizationPercentChanged(); |
| 217 | + } |
| 218 | + if (m_memoryUsedMiB != nextUsed) { |
| 219 | + m_memoryUsedMiB = nextUsed; |
| 220 | + emit memoryUsedMiBChanged(); |
| 221 | + } |
| 222 | + if (m_memoryTotalMiB != nextTotal) { |
| 223 | + m_memoryTotalMiB = nextTotal; |
| 224 | + emit memoryTotalMiBChanged(); |
| 225 | + } |
| 226 | + |
| 227 | + const int usagePercent = |
| 228 | + nextTotal > 0 |
| 229 | + ? std::clamp(static_cast<int>((static_cast<double>(nextUsed) / |
| 230 | + static_cast<double>(nextTotal)) * |
| 231 | + 100.0), |
| 232 | + 0, 100) |
| 233 | + : 0; |
| 234 | + if (m_memoryUsagePercent != usagePercent) { |
| 235 | + m_memoryUsagePercent = usagePercent; |
| 236 | + emit memoryUsagePercentChanged(); |
| 237 | + } |
| 238 | + |
| 239 | + setAvailable(true); |
85 | 240 | return; |
86 | 241 | } |
87 | 242 |
|
|
0 commit comments