sg_get_mem_stats(3)

NAME
sg_get_mem_stats, sg_get_mem_stats_r, sg_free_mem_stats, sg_get_swap_stats, sg_get_swap_stats_r, sg_free_swap_stats - get VM statistics

SYNOPSIS
\FC#include <statgrab.h>\F[]

\FCsg_mem_stats *sg_get_mem_stats\F[]
\FC(size_t *entries);\F[]

\FCsg_mem_stats *sg_get_mem_stats_r\F[]
\FC(size_t *entries);\F[]

\FCsg_error sg_free_mem_stats\F[]
\FC(sg_mem_stats *data);\F[]

\FCsg_swap_stats *sg_get_swap_stats\F[]
\FC(size_t *entries);\F[]

\FCsg_swap_stats *sg_get_swap_stats_r\F[]
\FC(size_t *entries);\F[]

\FCsg_error sg_free_swap_stats\F[]
\FC(sg_swap_stats *data);\F[]

DESCRIPTION
Memory statistics are accessed through the \FCsg_get_mem_stats\F[]() function and the \FCsg_get_mem_stats_r\F[]() function. Both return a pointer to a \FCsg_mem_stats\F[] buffer. Note that this statistic doesn't need to map the entire physical memory usage, it represents the real memory usable by the operating system.

Memory statistics are accessed through the \FCsg_get_swap_stats\F[]() function and the \FCsg_get_swap_stats_r\F[]() function. Both return a pointer to a \FCsg_swap_stats\F[] buffer.

API Shortcut
function | returns | data owner
sg_get_mem_stats | \FCsg_mem_stats\F[] * | libstatgrab (thread local)
sg_get_mem_stats_r | \FCsg_mem_stats\F[] * | caller
sg_get_swap_stats | \FCsg_swap_stats\F[] * | libstatgrab (thread local)
sg_get_swap_stats_r | \FCsg_swap_stats\F[] * | caller

The \FCsg_mem_stats\F[] buffer received from \FCsg_get_mem_stats_r\F[]() and the \FCsg_swap_stats\F[] buffer received from \FCsg_get_swap_stats_r\F[]() must be freed using \FCsg_free_mem_stats\F[]() or the \FCsg_free_swap_stats\F[](), respectively, when not needed any more. The caller is responsible for doing it.

On the FreeBSD operating system elevated privileges are required to access the swap statistics. Making the program setgid kmem should be sufficient. Programs running as root will not have this problem.

RETURN VALUES
The VM system calls can return a pointer to either a \FCsg_mem_stats\F[] or a \FCsg_swap_stats\F[].

\FC
typedef struct{
        unsigned long long total;
        unsigned long long free;
        unsigned long long used;
        unsigned long long cache;
        time_t systime;
} sg_mem_stats;
    \F[]

\FCtotal\F[]
    The total amount of real memory in bytes.

\FCfree\F[]
    The free amount of real memory in bytes.

\FCused\F[]
    The used amount of real memory in bytes.

\FCcache\F[]
    The amount of real memory in bytes used for caching.

\FCsystime\F[]
    the timestamp when the above stats where collected in seconds since epoch

\FC
typedef struct {
        unsigned long long total;
        unsigned long long used;
        unsigned long long free;
        time_t systime;
} sg_swap_stats;
    \F[]

\FCtotal\F[]
    The total swap space in bytes.

\FCused\F[]
    The used swap in bytes.

\FCfree\F[]
    The free swap in bytes.

\FCsystime\F[]
    The timestamp when the above stats where collected in seconds since epoch

TODO
Add a function to hold open the file descriptor to the kernel memory structures. Doing this would allow the elevated privileges to be dropped early on.

SEE ALSO
statgrab(3)

WEBSITE
⟨https://libstatgrab.org/⟩