summaryrefslogtreecommitdiffstats
path: root/src/monitor-lib/processreader.h
blob: 1481c2568446ef09e79db0d0eb4f0928d28bdd59 (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
// Copyright (C) 2021 The Qt Company Ltd.
// Copyright (C) 2019 Luxoft Sweden AB
// Copyright (C) 2018 Pelagicore AG
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#ifndef PROCESSREADER_H
#define PROCESSREADER_H

#include <QtCore/QMutex>
#include <QtCore/QElapsedTimer>
#include <QtCore/QObject>

#include <QtAppManCommon/global.h>

#if defined(Q_OS_LINUX)
#  include <memory>
#  include <QtAppManMonitor/sysfsreader.h>
#endif

QT_BEGIN_NAMESPACE_AM

class ProcessReader : public QObject {
    Q_OBJECT

public:
    QMutex mutex;
    qreal cpuLoad = 0.0;
    struct Memory {
        quint32 totalVm = 0;
        quint32 totalRss = 0;
        quint32 totalPss = 0;
        quint32 textVm = 0;
        quint32 textRss = 0;
        quint32 textPss = 0;
        quint32 heapVm = 0;
        quint32 heapRss = 0;
        quint32 heapPss = 0;
    } memory;

#if defined(Q_OS_LINUX)
    // solely for testing purposes
    bool testReadSmaps(const QByteArray &smapsFile);
#endif

public slots:
    void update();
    void setProcessId(qint64 pid);
    void enableMemoryReporting(bool enabled);

signals:
    void updated();

private:
    void openCpuLoad();
    qreal readCpuLoad();
    bool readMemory(Memory &mem);

#if defined(Q_OS_LINUX)
    bool readSmaps(const QByteArray &smapsFile, Memory &mem);

    std::unique_ptr<SysFsReader> m_statReader;
    QElapsedTimer m_elapsedTime;
    quint64 m_lastCpuUsage = 0.0;
#endif

    qint64 m_pid = 0;
    bool m_memoryReportingEnabled = true;
};

QT_END_NAMESPACE_AM

#endif // PROCESSREADER_H