Phosphor
Qt6 / Wayland library suite for window-management tools
Loading...
Searching...
No Matches
PhosphorShell::SystemUsage Class Reference

CPU and memory utilisation for status bar widgets. More...

#include <PhosphorShell/SystemUsage.h>

Inheritance diagram for PhosphorShell::SystemUsage:
[legend]

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 &current)
 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.

Detailed Description

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 + "%" } 

Constructor & Destructor Documentation

◆ SystemUsage()

PhosphorShell::SystemUsage::SystemUsage ( QObject * parent = nullptr)
explicit

◆ ~SystemUsage()

PhosphorShell::SystemUsage::~SystemUsage ( )
override

Member Function Documentation

◆ cpuPercent()

int PhosphorShell::SystemUsage::cpuPercent ( ) const
nodiscard

◆ cpuPercentBetween()

int PhosphorShell::SystemUsage::cpuPercentBetween ( const CpuTotals & prev,
const CpuTotals & current )
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.

◆ cpuPercentChanged

void PhosphorShell::SystemUsage::cpuPercentChanged ( )
signal

◆ enabled()

bool PhosphorShell::SystemUsage::enabled ( ) const
nodiscard

◆ enabledChanged

void PhosphorShell::SystemUsage::enabledChanged ( )
signal

◆ interval()

int PhosphorShell::SystemUsage::interval ( ) const
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.

◆ intervalChanged

void PhosphorShell::SystemUsage::intervalChanged ( )
signal

◆ memoryPercent()

int PhosphorShell::SystemUsage::memoryPercent ( ) const
nodiscard

◆ memoryPercentChanged

void PhosphorShell::SystemUsage::memoryPercentChanged ( )
signal

◆ parseCpuTotals()

bool PhosphorShell::SystemUsage::parseCpuTotals ( const QByteArray & content,
CpuTotals * out )
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.

◆ parseMemoryTotals()

bool PhosphorShell::SystemUsage::parseMemoryTotals ( const QByteArray & content,
MemoryTotals * out )
staticnodiscard

Parse MemTotal / MemAvailable from a /proc/meminfo body.

Returns false unless both are present and numeric, leaving out untouched.

◆ setEnabled()

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.

◆ setInterval()

void PhosphorShell::SystemUsage::setInterval ( int intervalMs)

Member Data Documentation

◆ MinIntervalMs

int PhosphorShell::SystemUsage::MinIntervalMs = 100
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.


The documentation for this class was generated from the following file: