Commit e1fe8f46 authored by Arjen Hiemstra's avatar Arjen Hiemstra

Avoid having to reparse SMI query result when there are multiple GPUs

Also use std::vector for the container so we can use emplace()
parent 29d3c450
......@@ -41,7 +41,7 @@ void LinuxNvidiaGpu::initialize()
}
auto queryResult = s_smiProcess->query();
if (m_index >= queryResult.length()) {
if (m_index >= int(queryResult.size())) {
qWarning() << "Could not retrieve information for NVidia GPU" << m_index;
} else {
auto data = queryResult.at(m_index);
......
......@@ -19,12 +19,14 @@ bool NvidiaSmiProcess::isSupported() const
return !m_smiPath.isEmpty();
}
QVector<NvidiaSmiProcess::GpuQueryResult> NvidiaSmiProcess::query()
std::vector<NvidiaSmiProcess::GpuQueryResult> NvidiaSmiProcess::query()
{
QVector<NvidiaSmiProcess::GpuQueryResult> result;
if (!isSupported()) {
return result;
return m_queryResult;
}
if (!m_queryResult.empty()) {
return m_queryResult;
}
// Read and parse the result of "nvidia-smi query"
......@@ -38,7 +40,7 @@ QVector<NvidiaSmiProcess::GpuQueryResult> NvidiaSmiProcess::query()
queryProcess.start();
int gpuCounter = 0;
GpuQueryResult *data = &result[0];
auto data = m_queryResult.end();
bool readMemory = false;
bool readMaxClocks = false;
......@@ -50,13 +52,17 @@ QVector<NvidiaSmiProcess::GpuQueryResult> NvidiaSmiProcess::query()
auto line = queryProcess.readLine();
if (line.startsWith("GPU ")) {
// Start of GPU properties block.
result.append(GpuQueryResult{});
data = &result[gpuCounter];
// Start of GPU properties block. Ensure we have a new data object
// to write to.
data = m_queryResult.emplace(m_queryResult.end());
gpuCounter++;
}
if ((readMemory || readMaxClocks) && !line.startsWith(" ")) {
// Memory/clock information does not have a unique prefix but should
// be indented more than their "headers". So if the indentation is
// less, we are no longer in an info block and should treat it as
// such.
readMemory = false;
readMaxClocks = false;
}
......@@ -90,7 +96,7 @@ QVector<NvidiaSmiProcess::GpuQueryResult> NvidiaSmiProcess::query()
}
}
return result;
return m_queryResult;
}
void NvidiaSmiProcess::ref()
......
......@@ -37,7 +37,7 @@ public:
bool isSupported() const;
QVector<GpuQueryResult> query();
std::vector<GpuQueryResult> query();
void ref();
void unref();
......@@ -48,6 +48,7 @@ private:
void readStatisticsData();
QString m_smiPath;
std::vector<GpuQueryResult> m_queryResult;
std::unique_ptr<QProcess> m_process = nullptr;
int m_references = 0;
};
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