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
SYNOPSIS
FC#include <statgrab.h>F[0]
FCsg_cpu_percents *sg_get_cpu_percentsF[0] FC(size_t *entries);F[0]
FCsg_cpu_percents *sg_get_cpu_percents_ofF[0] FC(sg_cpu_percent_source cps, size_t *entries);F[0]
FCsg_cpu_percents *sg_get_cpu_percents_rF[0] FC(const sg_cpu_stats *whereof, size_t *entries);F[0]
FCvoid sg_free_cpu_percentsF[0] FC(sg_cpu_percents *data);F[0]
FCsg_cpu_stats *sg_get_cpu_statsF[0] FC(size_t *entries);F[0]
FCsg_cpu_stats *sg_get_cpu_stats_diffF[0] FC(size_t *entries);F[0]
FCsg_cpu_stats *sg_get_cpu_stats_rF[0] FC(size_t *entries);F[0]
FCsg_cpu_stats *sg_get_cpu_stats_diff_betweenF[0] FC(const sg_cpu_stats *cpu_now, const sg_cpu_stats *cpu_last, size_t *entries);F[0]
FCsg_error sg_free_cpu_statsF[0] FC(sg_cpu_stats *data);F[0]
DESCRIPTION
These are the categories of data delivered by the cpu stats module: FCsg_get_cpu_statsF[0]() and FCsg_get_cpu_stats_rF[0]() deliver native cpu counters since the machine has been started, FCsg_get_cpu_stats_diffF[0]() and FCsg_get_cpu_stats_diff_betweenF[0]() deliver native cpu counters between two FCsg_get_cpu_statsF[0]() calls and FCsg_get_cpu_percents_ofF[0]() and FCsg_get_cpu_percents_rF[0]() deliver correlated relative cpu counters (where total is 100%).
API Shortcut
| function | returns | data owner |
| sg_get_cpu_stats | FCsg_cpu_statsF[0] * | libstatgrab (thread local) |
| sg_get_cpu_stats_r | FCsg_cpu_statsF[0] * | caller |
| sg_get_cpu_stats_diff | FCsg_cpu_statsF[0] * | libstatgrab (thread local) |
| sg_get_cpu_stats_diff_between | FCsg_cpu_statsF[0] * | caller |
| sg_get_cpu_percents_of | FCsg_cpu_percentsF[0] * | libstatgrab (thread local) |
| sg_get_cpu_percents_r | FCsg_cpu_percentsF[0] * | caller |
The FCsg_cpu_statsF[0] buffer received from FCsg_get_cpu_stats_rF[0]() and the FCsg_get_cpu_stats_diff_betweenF[0]() as well as the FCsg_cpu_percentsF[0] buffer received from FCsg_get_cpu_percents_rF[0]() must be freed using FCsg_free_cpu_statsF[0]() or the FCsg_free_cpu_percentsF[0](), respectively, when not needed any more. The caller is responsible for doing it.
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 FCsysconf(_SC_CLK_TCK)F[0].
RETURN VALUES
There are two structures returned by the CPU statistics functions.
FC
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;
F[0]
FC
typedef struct {
double user;
double kernel;
double idle;
double iowait;
double swap;
double nice;
time_t time_taken;
} sg_cpu_percents;
F[0]
- FCuserF[0] FCkernelF[0] FCidleF[0] FCiowaitF[0] FCswapF[0] FCniceF[0] FCtotalF[0]
-
The different CPU states.
- FCcontext_switchesF[0] FCvoluntary_context_switchesF[0] FCinvoluntary_context_switchesF[0] FCsyscallsF[0] FCinterruptsF[0] FCsoft_interruptsF[0]
-
The different program actions on CPU.
- FCsystimeF[0] FCtime_takenF[0]
-
The time taken in seconds since the last call of the function, or the system time.
SEE ALSO
WEBSITE
⟨https://libstatgrab.org/⟩