Branch data Line data Source code
1 : : // Copyright (C) 2021 The Qt Company Ltd.
2 : : // Copyright (C) 2019 Luxoft Sweden AB
3 : : // Copyright (C) 2018 Pelagicore AG
4 : : // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
5 : :
6 : : #include "gpustatus.h"
7 : :
8 : : using namespace Qt::StringLiterals;
9 : :
10 : : /*!
11 : : \qmltype GpuStatus
12 : : \inqmlmodule QtApplicationManager
13 : : \ingroup common-instantiatable
14 : : \brief Provides information on the GPU status.
15 : :
16 : : GpuStatus provides information on the status of the GPU. Its property values
17 : : are updated whenever the method update() is called.
18 : :
19 : : You can use it alongside a Timer for instance to periodically query the status of the GPU:
20 : :
21 : : \qml
22 : : import QtQuick
23 : : import QtApplicationManager
24 : : ...
25 : : GpuStatus { id: gpuStatus }
26 : : Timer {
27 : : interval: 500
28 : : running: true
29 : : repeat: true
30 : : onTriggered: gpuStatus.update()
31 : : }
32 : : Text {
33 : : property string loadPercent: Number(gpuStatus.gpuLoad * 100).toLocaleString(Qt.locale("en_US"), 'f', 1)
34 : : text: "GPU load: " + loadPercent + "%"
35 : : }
36 : : \endqml
37 : :
38 : : You can also use this component as a MonitorModel data source if you want to plot its
39 : : previous values over time:
40 : :
41 : : \qml
42 : : import QtQuick
43 : : import QtApplicationManager
44 : : ...
45 : : MonitorModel {
46 : : GpuStatus {}
47 : : }
48 : : \endqml
49 : :
50 : : */
51 : :
52 : : QT_USE_NAMESPACE_AM
53 : :
54 : 2 : GpuStatus::GpuStatus(QObject *parent)
55 : : : QObject(parent)
56 [ + - ]: 2 : , m_gpuReader(new GpuReader)
57 [ + - ]: 2 : , m_gpuLoad(0)
58 : : {
59 [ + - ]: 2 : m_gpuReader->setActive(true);
60 : 2 : }
61 : :
62 : 2 : GpuStatus::~GpuStatus()
63 : : {
64 : 2 : m_gpuReader->setActive(false);
65 : 2 : }
66 : :
67 : : /*!
68 : : \qmlproperty real GpuStatus::gpuLoad
69 : : \readonly
70 : :
71 : : GPU utilization when update() was last called, as a value ranging from 0 (inclusive,
72 : : completely idle) to 1 (inclusive, fully busy).
73 : :
74 : : \note This is dependent on tools from the graphics hardware vendor and might not work on
75 : : every system.
76 : :
77 : : Currently, this only works on \e Linux with either \e Intel or \e NVIDIA chipsets, plus the
78 : : tools from the respective vendors have to be installed:
79 : :
80 : : \table
81 : : \header
82 : : \li Hardware
83 : : \li Tool
84 : : \li Notes
85 : : \row
86 : : \li NVIDIA
87 : : \li \c nvidia-smi
88 : : \li The utilization will only be shown for the first GPU of the system, in case multiple GPUs
89 : : are installed.
90 : : \row
91 : : \li Intel
92 : : \li \c intel_gpu_top
93 : : \li The binary has to be made set-UID root, e.g. via \c{sudo chmod +s $(which intel_gpu_top)},
94 : : or the application manager has to be run as the \c root user.
95 : : \endtable
96 : :
97 : : \sa update
98 : : */
99 : 48 : qreal GpuStatus::gpuLoad() const
100 : : {
101 : 48 : return m_gpuLoad;
102 : : }
103 : :
104 : : /*!
105 : : \qmlmethod GpuStatus::update
106 : :
107 : : Updates the gpuLoad property.
108 : :
109 : : \sa gpuLoad
110 : : */
111 : 46 : void GpuStatus::update()
112 : : {
113 : 46 : qreal newLoad = m_gpuReader->readLoadValue();
114 [ - + ]: 46 : if (!qFuzzyCompare(newLoad, m_gpuLoad)) {
115 : 0 : m_gpuLoad = newLoad;
116 : 0 : emit gpuLoadChanged();
117 : : }
118 : 46 : }
119 : :
120 : : /*!
121 : : \qmlproperty list<string> GpuStatus::roleNames
122 : : \readonly
123 : :
124 : : Names of the roles provided by GpuStatus when used as a MonitorModel data source.
125 : :
126 : : \sa MonitorModel
127 : : */
128 : 2 : QStringList GpuStatus::roleNames() const
129 : : {
130 [ + + ]: 4 : return { u"gpuLoad"_s };
131 : : }
132 : :
133 : :
134 : : #include "moc_gpustatus.cpp"
|