sg_get_cpu_stats(3)
okay so bestie, welcome to sg_get_cpu_stats(3) β the libstatgrab page about getting cpu usage πβ¨. It's giving βI know my countersβ energy, and we're keeping every single detail.
πΈ NAME πΈ
sg_get_cpu_stats, sg_get_cpu_stats_r, sg_get_cpu_stats_diff, sg_get_cpu_stats_diff_between, sg_get_cpu_percents, sg_get_cpu_percents_of, sg_get_cpu_percents_r, sg_free_cpu_stats β get cpu usage. That's the whole squad, and yes, they're all here. π
β¨ SYNOPSIS β¨
#include <statgrab.h>
sg_cpu_percents *sg_get_cpu_percents (size_t *entries);
sg_cpu_percents *sg_get_cpu_percents_of (sg_cpu_percent_source cps,
size_t *entries);
sg_cpu_percents *sg_get_cpu_percents_r (const sg_cpu_stats *whereof,
size_t *entries);
void sg_free_cpu_percents (sg_cpu_percents *data);
sg_cpu_stats *sg_get_cpu_stats (size_t *entries);
sg_cpu_stats *sg_get_cpu_stats_diff (size_t *entries);
sg_cpu_stats *sg_get_cpu_stats_r (size_t *entries);
sg_cpu_stats *sg_get_cpu_stats_diff_between (const sg_cpu_stats
*cpu_now, const sg_cpu_stats *cpu_last, size_t
*entries);
sg_error sg_free_cpu_stats (sg_cpu_stats *data);
that's the lowkey blueprint β code stays exactly as written, no remixing. π
π DESCRIPTION π
okay so the cpu stats module hands you these categories of data, and the names are the whole map: πΊοΈ
sg_get_cpu_stats()andsg_get_cpu_stats_r()β deliver native cpu counters since the machine has been started.sg_get_cpu_stats_diff()andsg_get_cpu_stats_diff_between()β deliver native cpu counters between twosg_get_cpu_stats()calls.sg_get_cpu_percents_of()andsg_get_cpu_percents_r()β deliver correlated relative cpu counters (where total is100%).
π API Shortcut π
here's who returns what and who owns the data β no cap, this matters:
sg_get_cpu_statsβ returnssg_cpu_stats *, data owner:libstatgrab (thread local).sg_get_cpu_stats_rβ returnssg_cpu_stats *, data owner:caller.sg_get_cpu_stats_diffβ returnssg_cpu_stats *, data owner:libstatgrab (thread local).sg_get_cpu_stats_diff_betweenβ returnssg_cpu_stats *, data owner:caller.sg_get_cpu_percents_ofβ returnssg_cpu_percents *, data owner:libstatgrab (thread local).sg_get_cpu_percents_rβ returnssg_cpu_percents *, data owner:caller.
memory tea β: the sg_cpu_stats buffer you get from sg_get_cpu_stats_r() and from sg_get_cpu_stats_diff_between(), plus the sg_cpu_percents buffer from sg_get_cpu_percents_r(), must be freed using sg_free_cpu_stats() or sg_free_cpu_percents(), respectively, when you don't need it anymore. The caller is responsible for doing it. π
tick check β±οΈ: the value stored (the "ticks") will vary between operating systems. For example, Solaris has a total of 100 per second, while Linux has substantially more. Also, different operating systems store different information β you won't find nice cpu on Solaris, for example. π
Modern systems shall provide information about the clock tick resolution by invoking sysconf(_SC_CLK_TCK). π§
π RETURN VALUES π
There are two structures returned by the CPU statistics functions β here's the exact shape of them:
typedef struct {
unsigned long long user;
unsigned long long kernel;
unsigned long long idle;
unsigned long long iowait;
unsigned long long swap;
unsigned long long nice;
unsigned long long total;
unsigned long long context_switches;
unsigned long long voluntary_context_switches;
unsigned long long involuntary_context_switches;
unsigned long long syscalls;
unsigned long long interrupts;
unsigned long long soft_interrupts;
time_t systime;
} sg_cpu_stats;
typedef struct {
double user;
double kernel;
double idle;
double iowait;
double swap;
double nice;
time_t time_taken;
} sg_cpu_percents;
the field tea π΅:
user,kernel,idle,iowait,swap,nice,totalβ the different CPU states.context_switches,voluntary_context_switches,involuntary_context_switches,syscalls,interrupts,soft_interruptsβ the different program actions on CPU.systime,time_takenβ the time taken in seconds since the last call of the function, or the system time.
π₯ SEE ALSO π₯
statgrab(3) β go read that one too, bestie. π«Ά
π WEBSITE π
β¨https://libstatgrab.org/β© β that's the site, exactly as the page gives it. β¨
xoxo bestie π β this page is from libstatgrab, dated 2019-03-08. periodt. π
π¦ where this page comes from
- alpine 3.24: libstatgrab-doc 0.92.1-r1 Β· LGPL-2.0-or-later
- arch rolling: libstatgrab 0.92.1-2 Β· LGPL AND GPL
- fedora 43: libstatgrab-devel 1:0.92.1-14.fc43 Β· LGPL-2.1-or-later
- fedora 43: libstatgrab-devel 1:0.92.1-14.fc43 Β· LGPL-2.1-or-later
- fedora 44: libstatgrab-devel 1:0.92.1-15.fc44 Β· LGPL-2.1-or-later
- fedora 44: libstatgrab-devel 1:0.92.1-15.fc44 Β· LGPL-2.1-or-later
- nixos 26.05: libstatgrab 0.92.1
- nixpkgs unstable: libstatgrab 0.92.1
- opensuse-leap 16.0: libstatgrab-devel 0.92.1-bp160.1.14 Β· LGPL-2.1-or-later
- opensuse-leap 16.0: libstatgrab-devel 0.92.1-bp160.1.14 Β· LGPL-2.1-or-later
- opensuse-leap 16.0: libstatgrab-devel 0.92.1-bp160.1.14 Β· LGPL-2.1-or-later
- opensuse-leap 16.0: libstatgrab-devel 0.92.1-bp160.1.14 Β· LGPL-2.1-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms