---
name: "sg_get_cpu_stats_diff_between"
section: "3"
platform: "linux"
package: "libstatgrab"
version: "0.92.1-2"
release: "Arch Linux 2026-11-01"
platforms: ["linux"]
url: "https://new.carta.tech/sg_get_cpu_stats_diff_between"
---

# sg\_get\_cpu\_stats\_diff\_between(3)

get cpu usage

## 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\_percents**F\[0\]  FC(size\_t \**entries*);F\[0\]

FCsg\_cpu\_percents \***sg\_get\_cpu\_percents\_of**F\[0\]  FC(sg\_cpu\_percent\_source *cps*, size\_t \**entries*);F\[0\]

FCsg\_cpu\_percents \***sg\_get\_cpu\_percents\_r**F\[0\]  FC(const sg\_cpu\_stats \**whereof*, size\_t \**entries*);F\[0\]

FCvoid **sg\_free\_cpu\_percents**F\[0\]  FC(sg\_cpu\_percents \**data*);F\[0\]

FCsg\_cpu\_stats \***sg\_get\_cpu\_stats**F\[0\]  FC(size\_t \**entries*);F\[0\]

FCsg\_cpu\_stats \***sg\_get\_cpu\_stats\_diff**F\[0\]  FC(size\_t \**entries*);F\[0\]

FCsg\_cpu\_stats \***sg\_get\_cpu\_stats\_r**F\[0\]  FC(size\_t \**entries*);F\[0\]

FCsg\_cpu\_stats \***sg\_get\_cpu\_stats\_diff\_between**F\[0\]  FC(const sg\_cpu\_stats \**cpu\_now*, const sg\_cpu\_stats \**cpu\_last*, size\_t \**entries*);F\[0\]

FCsg\_error **sg\_free\_cpu\_stats**F\[0\]  FC(sg\_cpu\_stats \**data*);F\[0\]

## DESCRIPTION

These are the categories of data delivered by the cpu stats module: FC**sg\_get\_cpu\_stats**F\[0\]() and FC**sg\_get\_cpu\_stats\_r**F\[0\]() deliver native cpu counters since the machine has been started, FC**sg\_get\_cpu\_stats\_diff**F\[0\]() and FC**sg\_get\_cpu\_stats\_diff\_between**F\[0\]() deliver native cpu counters between two FC**sg\_get\_cpu\_stats**F\[0\]() calls and FC**sg\_get\_cpu\_percents\_of**F\[0\]() and FC**sg\_get\_cpu\_percents\_r**F\[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 FC**sg\_get\_cpu\_stats\_r**F\[0\]() and the FC**sg\_get\_cpu\_stats\_diff\_between**F\[0\]() as well as the FCsg\_cpu\_percentsF\[0\] buffer received from FC**sg\_get\_cpu\_percents\_r**F\[0\]() must be freed using FC**sg\_free\_cpu\_stats**F\[0\]() or the FC**sg\_free\_cpu\_percents**F\[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

[**statgrab**(3)](/statgrab/3.md)

## WEBSITE

⟨https://libstatgrab.org/⟩
