summaryrefslogtreecommitdiffstats
path: root/src/monitor-lib/processstatus.cpp
blob: de80064ccaa09234fbebd489fefe003f5ed17ade (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/****************************************************************************
**
** Copyright (C) 2019 Luxoft Sweden AB
** Copyright (C) 2018 Pelagicore AG
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Luxoft Application Manager.
**
** $QT_BEGIN_LICENSE:LGPL-QTAS$
** Commercial License Usage
** Licensees holding valid commercial Qt Automotive Suite licenses may use
** this file in accordance with the commercial license agreement provided
** with the Software or, alternatively, in accordance with the terms
** contained in a written agreement between you and The Qt Company.  For
** licensing terms and conditions see https://www.qt.io/terms-conditions.
** For further information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
** SPDX-License-Identifier: LGPL-3.0
**
****************************************************************************/

#include "processstatus.h"

#include <QCoreApplication>
#include <QtQml/qqmlinfo.h>


#include "abstractruntime.h"
#include "applicationmanager.h"
#include "logging.h"

#include <limits>

/*!
    \qmltype ProcessStatus
    \inqmlmodule QtApplicationManager.SystemUI
    \ingroup system-ui
    \brief Provides information on the status of an application process.

    ProcessStatus provides information about the process of a given application.

    You can use it alongside a Timer for instance to periodically query the status of an
    application process.

    \qml
    import QtQuick 2.11
    import QtApplicationManager 2.0
    import QtApplicationManager.SystemUI 2.0

    Item {
        id: root
        property var application: ApplicationManager.get(0)
        ...
        ProcessStatus {
            id: processStatus
            applicationId: root.application.id
        }
        Timer {
            interval: 1000
            running: root.visible && root.application.runState === Am.Running
            repeat: true
            onTriggered: processStatus.update()
        }
        Text {
            text: "PSS.total: " + (processStatus.memoryPss.total / 1e6).toFixed(0) + " MB"
        }
    }
    \endqml

    You can also use this component as a MonitorModel data source if you want to plot its
    previous values over time:

    \qml
    import QtQuick 2.11
    import QtApplicationManager 2.0
    import QtApplicationManager.SystemUI 2.0
    ...
    MonitorModel {
        running: true
        ProcessStatus {
            applicationId: "some.app.id"
        }
    }
    \endqml

    These are the supported keys in the memory properties (\c memoryVirtual, \c memoryRss and \c memoryPss):

    \table
    \header
        \li Key
        \li Description
    \row
        \li \c total
        \li The amount of memory used in total in bytes.
    \row
        \li \c text
        \li The amount of memory used by the code section in bytes.
    \row
        \li \c heap
        \li The amount of memory used by the heap in bytes. This is private, dynamically allocated
            memory (for example through \c malloc or \c mmap on Linux).
    \endtable
*/

QT_USE_NAMESPACE_AM

QThread *ProcessStatus::m_workerThread = nullptr;

ProcessStatus::ProcessStatus(QObject *parent)
    : QObject(parent)
{
    if (!m_workerThread) {
        m_workerThread = new QThread;
        m_workerThread->start();
    }

    m_reader.reset(new ProcessReader);
    connect(m_reader.data(), &ProcessReader::updated, this, [this]() {
        emit cpuLoadChanged();
        fetchMemoryReadings();
        m_pendingUpdate = false;
    });
    connect(this, &ProcessStatus::processIdChanged, m_reader.data(), &ProcessReader::setProcessId);
}

/*!
    \qmlmethod ProcessStatus::update

    Updates the properties cpuLoad, memoryVirtual, memoryRss and memoryPss.
*/
void ProcessStatus::update()
{
    if (!m_pendingUpdate) {
        m_pendingUpdate = true;
        QMetaObject::invokeMethod(m_reader.data(), &ProcessReader::update);
    }
}

/*!
    \qmlproperty string ProcessStatus::applicationId

    \l{ApplicationObject::id}{Id} of the \l{ApplicationObject}{application} whose process is to
    be monitored.

    \sa ApplicationObject
*/
QString ProcessStatus::applicationId() const
{
    return m_appId;
}

void ProcessStatus::setApplicationId(const QString &appId)
{
    if (m_appId != appId || m_appId.isNull()) {
        if (m_application) {
            disconnect(m_application, nullptr, this, nullptr);
            m_application = nullptr;
        }
        m_appId = appId;
        if (!appId.isEmpty()) {
            int appIndex = ApplicationManager::instance()->indexOfApplication(appId);
            if (appIndex < 0) {
                qmlWarning(this) << "Invalid application ID:" << appId;
            } else {
                m_application = ApplicationManager::instance()->application(appIndex);
                connect(m_application, &AbstractApplication::runStateChanged, this, &ProcessStatus::onRunStateChanged);
            }
        }
        determinePid();
        emit applicationIdChanged(appId);
    }
}

void ProcessStatus::onRunStateChanged(Am::RunState state)
{
    if (state == Am::Running || state == Am::NotRunning)
        determinePid();
}

void ProcessStatus::determinePid()
{
    qint64 newId;
    if (m_appId.isEmpty()) {
        newId = QCoreApplication::applicationPid();
    } else {
        Q_ASSERT(m_application);
        if (ApplicationManager::instance()->isSingleProcess())
            newId = 0;
        else
            newId = m_application->currentRuntime() ? m_application->currentRuntime()->applicationProcessId() : 0;
    }

    if (newId != m_pid) {
        m_pid = newId;
        emit processIdChanged(m_pid);
    }
}

/*!
    \qmlproperty int ProcessStatus::processId
    \readonly

    This property holds the OS specific process identifier (PID) that is monitored. This can be
    used by external tools for example. The property is 0, if there is no process associated with
    the \l applicationId. In particular, if the application-manager runs in single-process mode,
    only the System-UI (identified by an empty \l applicationId) will have an associated process.
*/
qint64 ProcessStatus::processId() const
{
    return m_pid;
}

/*!
    \qmlproperty real ProcessStatus::cpuLoad
    \readonly

    The process's CPU utilization when update() was last called. A value of 0 means
    that the process was idle, a value of 1 means it fully used the equivalent of one core
    (which may be split over several ones).

    \sa ProcessStatus::update
*/
qreal ProcessStatus::cpuLoad()
{
    quint32 value = m_reader->cpuLoad.load();
    return ((qreal)value) / ((qreal)std::numeric_limits<quint32>::max());
}

void ProcessStatus::fetchMemoryReadings()
{
    // Although smaps claims to report kB it's actually KiB (2^10 = 1024 Bytes)
    m_memoryVirtual[qSL("total")] = quint64(m_reader->totalVm.load()) << 10;
    m_memoryVirtual[qSL("text")] = quint64(m_reader->textVm.load()) << 10;
    m_memoryVirtual[qSL("heap")] = quint64(m_reader->heapVm.load()) << 10;
    m_memoryRss[qSL("total")] = quint64(m_reader->totalRss.load()) << 10;
    m_memoryRss[qSL("text")] = quint64(m_reader->textRss.load()) << 10;
    m_memoryRss[qSL("heap")] = quint64(m_reader->heapRss.load()) << 10;
    m_memoryPss[qSL("total")] = quint64(m_reader->totalPss.load()) << 10;
    m_memoryPss[qSL("text")] = quint64(m_reader->textPss.load()) << 10;
    m_memoryPss[qSL("heap")] = quint64(m_reader->heapPss.load()) << 10;

    emit memoryReportingChanged(m_memoryVirtual, m_memoryRss, m_memoryPss);
}

/*!
    \qmlproperty var ProcessStatus::memoryVirtual
    \readonly

    A map of the process's virtual memory usage. See ProcessStatus description for a list of supported keys.
    The total amount of virtual memory is provided through \c memoryVirtual.total for
    example.

    The value of this property is updated when ProcessStatus::update is called.

    \sa ProcessStatus::update
*/
QVariantMap ProcessStatus::memoryVirtual() const
{
    return m_memoryVirtual;
}

/*!
    \qmlproperty var ProcessStatus::memoryRss
    \readonly

    A map of the process's RSS (Resident Set Size) memory usage. This is the amount of memory that is
    actually mapped to physical RAM. See ProcessStatus description for a list of supported keys.

    The value of this property is updated when ProcessStatus::update is called.

    \sa ProcessStatus::update
*/
QVariantMap ProcessStatus::memoryRss() const
{
    return m_memoryRss;
}

/*!
    \qmlproperty var ProcessStatus::memoryPss
    \readonly

    A map of the process's PSS (Proportional Set Size) memory usage. This is the
    proportional share of the RSS value above. For instance if two processes share 2 MB
    the RSS value will be 2 MB for each process and the PSS value 1 MB for each process.
    As the name implies, the code section of shared libraries is generally shared between
    processes. Memory may also be shared by other means provided by the OS (e.g. through
    \c mmap on Linux). See ProcessStatus description for a list of supported keys.

    The value of this property is updated when ProcessStatus::update is called.

    \sa ProcessStatus::update
*/
QVariantMap ProcessStatus::memoryPss() const
{
    return m_memoryPss;
}

/*!
    \qmlproperty list<string> ProcessStatus::roleNames
    \readonly

    Names of the roles provided by ProcessStatus when used as a MonitorModel data source.

    \sa MonitorModel
*/
QStringList ProcessStatus::roleNames() const
{
    return { qSL("cpuLoad"), qSL("memoryVirtual"), qSL("memoryRss"), qSL("memoryPss") };
}