CPU and memory utilisation for status bar widgets. More...
#include <PhosphorShell/SystemUsage.h>
Classes | |
| struct | CpuTotals |
| Cumulative busy / idle jiffies parsed from a /proc/stat body. More... | |
| struct | MemoryTotals |
| Total / available kilobytes parsed from a /proc/meminfo body. More... | |
Signals | |
| void | enabledChanged () |
| void | intervalChanged () |
| void | cpuPercentChanged () |
| void | memoryPercentChanged () |
Public Member Functions | |
| SystemUsage (QObject *parent=nullptr) | |
| ~SystemUsage () override | |
| bool | enabled () const |
| void | setEnabled (bool enabled) |
| Enabling re-baselines and samples immediately, so the first reading after a pause covers one interval rather than the whole gap. | |
| int | interval () const |
| Sampling period in milliseconds, clamped to at least MinIntervalMs. | |
| void | setInterval (int intervalMs) |
| int | cpuPercent () const |
| int | memoryPercent () const |
Static Public Member Functions | |
| static bool | parseCpuTotals (const QByteArray &content, CpuTotals *out) |
| Parse the aggregate "cpu" line of a /proc/stat body. | |
| static bool | parseMemoryTotals (const QByteArray &content, MemoryTotals *out) |
| Parse MemTotal / MemAvailable from a /proc/meminfo body. | |
| static int | cpuPercentBetween (const CpuTotals &prev, const CpuTotals ¤t) |
| Busy percentage between two cumulative snapshots, or -1 when the pair yields no usable rate: counters that did not advance, or that went backwards (a suspend/resume snapshot, a namespace reset, a wrap). | |
Static Public Attributes | |
| static constexpr int | MinIntervalMs = 100 |
| Shortest accepted sampling period. | |
CPU and memory utilisation for status bar widgets.
Samples /proc/stat and /proc/meminfo in-process on a timer, so a bar readout costs no subprocess per tick. CPU is the busy fraction between consecutive samples (the kernel exports cumulative jiffies, so a single sample cannot yield a rate). The constructor takes the first baseline, so cpuPercent is already meaningful on the first timer tick. Memory is 1 - MemAvailable / MemTotal.
Both values are whole percents clamped to 0..100. A malformed or unexpected /proc layout (exotic architecture, namespaced procfs, a kernel without MemAvailable) leaves the previous value in place and warns, once per failure kind per episode, rather than publishing a bogus reading. The two differ on recovery: memory republishes on the next good sample, while CPU also re-baselines, so its first sample after a failure publishes no rate and the readout holds one tick longer. That is deliberate, since the alternative is reporting the whole outage as if it were one interval.
Usage from QML:
import Phosphor.Shell
SystemUsage {
id: usage
interval: 2000
enabled: root.visible
}
Text { text: "CPU " + usage.cpuPercent + "%" }
|
explicit |
|
override |
|
nodiscard |
|
staticnodiscard |
Busy percentage between two cumulative snapshots, or -1 when the pair yields no usable rate: counters that did not advance, or that went backwards (a suspend/resume snapshot, a namespace reset, a wrap).
The caller publishes nothing for -1 and re-baselines instead.
Static and snapshot-taking for the same reason as the parsers: it makes the delta arithmetic pinnable without waiting on real jiffies, which advance too slowly to exercise from a test.
|
signal |
|
nodiscard |
|
signal |
|
nodiscard |
Sampling period in milliseconds, clamped to at least MinIntervalMs.
A shorter request is clamped (and warned about) rather than ignored, so the property and the binding that wrote it agree.
|
signal |
|
nodiscard |
|
signal |
|
staticnodiscard |
Parse the aggregate "cpu" line of a /proc/stat body.
Returns false for a missing/short/non-numeric line, leaving out untouched.
Static and body-taking so the arithmetic is testable without a filesystem: this is the logic the bar widget used to carry in QML.
|
staticnodiscard |
Parse MemTotal / MemAvailable from a /proc/meminfo body.
Returns false unless both are present and numeric, leaving out untouched.
| void PhosphorShell::SystemUsage::setEnabled | ( | bool | enabled | ) |
Enabling re-baselines and samples immediately, so the first reading after a pause covers one interval rather than the whole gap.
| void PhosphorShell::SystemUsage::setInterval | ( | int | intervalMs | ) |
|
staticconstexpr |
Shortest accepted sampling period.
Below roughly this the sampler costs more CPU than it measures and the reading becomes largely a measurement of itself.