diff options
author | Daniel d'Andrada <daniel.dandrada@luxoft.com> | 2018-11-02 13:43:34 +0100 |
---|---|---|
committer | Robert Griebl <robert.griebl@pelagicore.com> | 2018-12-11 13:55:31 +0000 |
commit | adb23a6ea8ba07c648bfc436c355a56db5cc7e2e (patch) | |
tree | 7a91a15dec4458d8110eaaae425060a8afe14505 | |
parent | 322f7eace9fa7457cad3e5fd5bb5032c4abfca95 (diff) |
Get vendor detection code out of GpuTool onto a separate class
So that it can be used elsewhere as well.
Change-Id: Icaeb53ea46b778f9df1a00b7b64c45d493638c4d
Reviewed-by: Robert Griebl <robert.griebl@pelagicore.com>
-rw-r--r-- | src/manager-lib/systemreader.cpp | 90 | ||||
-rw-r--r-- | src/manager-lib/systemreader.h | 14 |
2 files changed, 62 insertions, 42 deletions
diff --git a/src/manager-lib/systemreader.cpp b/src/manager-lib/systemreader.cpp index 96cca7b8..a7499445 100644 --- a/src/manager-lib/systemreader.cpp +++ b/src/manager-lib/systemreader.cpp @@ -153,42 +153,60 @@ qreal CpuReader::readLoadValue() return m_load; } +GpuVendor::Vendor GpuVendor::s_vendor = GpuVendor::Undefined; + +GpuVendor::Vendor GpuVendor::get() +{ + if (s_vendor == Undefined) + fetch(); + + return s_vendor; +} + +void GpuVendor::fetch() +{ + QByteArray vendor; +# if !defined(AM_HEADLESS) + auto readVendor = [&vendor](QOpenGLContext *c) { + const GLubyte *p = c->functions()->glGetString(GL_VENDOR); + if (p) + vendor = QByteArray(reinterpret_cast<const char *>(p)).toLower(); + }; + + if (QOpenGLContext::currentContext()) { + readVendor(QOpenGLContext::currentContext()); + } else { + QOpenGLContext context; + if (context.create()) { + QOffscreenSurface surface; + surface.setFormat(context.format()); + surface.create(); + context.makeCurrent(&surface); + readVendor(&context); + context.doneCurrent(); + } + } +# endif + if (vendor.contains("intel")) + s_vendor = Intel; + else if (vendor.contains("nvidia")) + s_vendor = Nvidia; + else + s_vendor = Unsupported; +} + class GpuTool : protected QProcess { public: GpuTool() : QProcess(qApp) { - QByteArray vendor; -# if !defined(AM_HEADLESS) - auto readVendor = [&vendor](QOpenGLContext *c) { - const GLubyte *p = c->functions()->glGetString(GL_VENDOR); - if (p) - vendor = QByteArray(reinterpret_cast<const char *>(p)).toLower(); - }; - - if (QOpenGLContext::currentContext()) { - readVendor(QOpenGLContext::currentContext()); - } else { - QOpenGLContext c; - if (c.create()) { - QOffscreenSurface s; - s.setFormat(c.format()); - s.create(); - c.makeCurrent(&s); - readVendor(&c); - c.doneCurrent(); - } - } -# endif - if (vendor.contains("intel")) { + if (GpuVendor::get() == GpuVendor::Intel) { setProgram(qSL("intel_gpu_top")); setArguments({ qSL("-o-"), qSL("-s 1000") }); - m_vendor = Intel; - } else if (vendor.contains("nvidia")) { + } else if (GpuVendor::get() == GpuVendor::Nvidia) { setProgram(qSL("nvidia-smi")); setArguments({ qSL("dmon"), qSL("--select"), qSL("u") }); - m_vendor = Nvidia; } QObject::connect(this, static_cast<void(QProcess::*)(int)>(&QProcess::finished), this, [this](int exitCode) { @@ -224,12 +242,12 @@ public: pos = int(endPtr - str.constData() + 1); } - switch (m_vendor) { - case Intel: + switch (GpuVendor::get()) { + case GpuVendor::Intel: if (values.size() >= 2) m_lastValue = values.at(1) / 100; break; - case Nvidia: + case GpuVendor::Nvidia: if (values.size() >= 2) { if (qFuzzyIsNull(values.at(0))) // hardcoded to first gfx card m_lastValue = values.at(1) / 100; @@ -272,11 +290,6 @@ public: return (state() == QProcess::Running); } - bool isSupported() const - { - return m_vendor != Unsupported; - } - qreal loadValue() const { return m_lastValue; @@ -285,13 +298,6 @@ public: private: QAtomicInteger<int> m_refCount; qreal m_lastValue = 0; - - enum Vendor { - Unsupported = 0, - Intel, - Nvidia - }; - Vendor m_vendor = Unsupported; }; GpuTool *GpuReader::s_gpuToolProcess = nullptr; @@ -304,7 +310,7 @@ void GpuReader::setActive(bool enabled) if (!s_gpuToolProcess) s_gpuToolProcess = new GpuTool(); - if (!s_gpuToolProcess->isSupported()) { + if (GpuVendor::get() == GpuVendor::Unsupported) { qCWarning(LogSystem) << "GPU monitoring is not supported on this platform."; } else { if (enabled) diff --git a/src/manager-lib/systemreader.h b/src/manager-lib/systemreader.h index 73747a92..775353d2 100644 --- a/src/manager-lib/systemreader.h +++ b/src/manager-lib/systemreader.h @@ -71,6 +71,20 @@ private: Q_DISABLE_COPY(CpuReader) }; +class GpuVendor { +public: + enum Vendor { + Undefined = 0, // didn't try to determine the vendor yet + Unsupported, + Intel, + Nvidia + }; + static Vendor get(); +private: + static void fetch(); + static Vendor s_vendor; +}; + class GpuTool; class GpuReader |