Phosphor
Qt6 / Wayland library suite for window-management tools
Loading...
Searching...
No Matches
SystemUsage.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 fuddlesworth
2// SPDX-License-Identifier: LGPL-2.1-or-later
3
4#pragma once
5
6#include <PhosphorShell/phosphorshell_export.h>
7
8#include <QByteArray>
9#include <QObject>
10
11QT_BEGIN_NAMESPACE
12class QTimer;
13QT_END_NAMESPACE
14
15namespace PhosphorShell {
16
45class PHOSPHORSHELL_EXPORT SystemUsage : public QObject
46{
47 Q_OBJECT
48
49 Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged)
50 Q_PROPERTY(int interval READ interval WRITE setInterval NOTIFY intervalChanged)
51 Q_PROPERTY(int cpuPercent READ cpuPercent NOTIFY cpuPercentChanged)
52 Q_PROPERTY(int memoryPercent READ memoryPercent NOTIFY memoryPercentChanged)
53
54public:
58 static constexpr int MinIntervalMs = 100;
59
61 struct CpuTotals
62 {
63 double total = 0;
64 double idle = 0;
65 };
66
69 {
70 double totalKb = 0;
71 double availableKb = 0;
72 };
73
74 explicit SystemUsage(QObject* parent = nullptr);
75 ~SystemUsage() override;
76
77 [[nodiscard]] bool enabled() const;
80 void setEnabled(bool enabled);
81
85 [[nodiscard]] int interval() const;
86 void setInterval(int intervalMs);
87
88 [[nodiscard]] int cpuPercent() const;
89 [[nodiscard]] int memoryPercent() const;
90
96 [[nodiscard]] static bool parseCpuTotals(const QByteArray& content, CpuTotals* out);
97
100 [[nodiscard]] static bool parseMemoryTotals(const QByteArray& content, MemoryTotals* out);
101
110 [[nodiscard]] static int cpuPercentBetween(const CpuTotals& prev, const CpuTotals& current);
111
112Q_SIGNALS:
117
118private:
119 void sample();
120 void sampleCpu();
121 void sampleMemory();
122 void restartBaseline();
123
124 QTimer* m_timer = nullptr;
125 bool m_enabled = true;
126 int m_interval = 2000;
127 int m_cpuPercent = 0;
128 int m_memoryPercent = 0;
129 // Previous cumulative jiffy totals; the CPU rate is their delta.
130 // Doubles because the totals exceed 32-bit range on a multi-core box
131 // within days at 100 Hz.
132 CpuTotals m_prev;
133 bool m_haveCpuBaseline = false;
134 // One warning per failure kind, cleared on a successful sample so a
135 // recurrence after recovery is reported again rather than silenced for
136 // the life of the process.
137 bool m_warnedCpuRead = false;
138 bool m_warnedCpuParse = false;
139 bool m_warnedMemoryRead = false;
140 bool m_warnedMemoryParse = false;
141};
142
143} // namespace PhosphorShell
void setInterval(int intervalMs)
void setEnabled(bool enabled)
Enabling re-baselines and samples immediately, so the first reading after a pause covers one interval...
SystemUsage(QObject *parent=nullptr)
int interval() const
Sampling period in milliseconds, clamped to at least MinIntervalMs.
static bool parseCpuTotals(const QByteArray &content, CpuTotals *out)
Parse the aggregate "cpu" line of a /proc/stat body.
static int cpuPercentBetween(const CpuTotals &prev, const CpuTotals &current)
Busy percentage between two cumulative snapshots, or -1 when the pair yields no usable rate: counters...
static constexpr int MinIntervalMs
Shortest accepted sampling period.
Definition SystemUsage.h:58
static bool parseMemoryTotals(const QByteArray &content, MemoryTotals *out)
Parse MemTotal / MemAvailable from a /proc/meminfo body.
Definition Environment.h:11
Cumulative busy / idle jiffies parsed from a /proc/stat body.
Definition SystemUsage.h:62
double idle
Definition SystemUsage.h:64
double total
Definition SystemUsage.h:63
Total / available kilobytes parsed from a /proc/meminfo body.
Definition SystemUsage.h:69
double availableKb
Definition SystemUsage.h:71
double totalKb
Definition SystemUsage.h:70