sg_get_mem_stats(3)

πŸ“¦
version 0.92.1 Β· 2 versions
, in alpine 3.24, arch rolling, fedora 43, fedora 44, nixos 26.05, nixpkgs unstable, opensuse-leap 16.0

✨ 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. This is the manual page for sg_get_mem_stats(3), bestie, and it is literally all about VM stats πŸ’–

🌸 SYNOPSIS 🌸

#include <statgrab.h>

sg_mem_stats *sg_get_mem_stats (size_t *entries);

sg_mem_stats *sg_get_mem_stats_r (size_t *entries);

sg_error sg_free_mem_stats (sg_mem_stats *data);

sg_swap_stats *sg_get_swap_stats (size_t *entries);

sg_swap_stats *sg_get_swap_stats_r (size_t *entries);

sg_error sg_free_swap_stats (sg_swap_stats *data);

πŸ¦‹ DESCRIPTION πŸ¦‹

okay so memory stats come through sg_get_mem_stats() and sg_get_mem_stats_r(). Both of them hand you back a pointer to a sg_mem_stats buffer. Quick note bestie: this statistic doesn't need to map the entire physical memory usage β€” it represents the real memory usable by the operating system 🫢

Swap stats come through sg_get_swap_stats() and sg_get_swap_stats_r(). Both return a pointer to a sg_swap_stats buffer ✨

🌈 API Shortcut 🌈

  • sg_get_mem_stats β€” returns sg_mem_stats *; data owner: libstatgrab (thread local)
  • sg_get_mem_stats_r β€” returns sg_mem_stats *; data owner: caller
  • sg_get_swap_stats β€” returns sg_swap_stats *; data owner: libstatgrab (thread local)
  • sg_get_swap_stats_r β€” returns sg_swap_stats *; data owner: caller

The sg_mem_stats buffer you get from sg_get_mem_stats_r() and the sg_swap_stats buffer you get from sg_get_swap_stats_r() must be freed using sg_free_mem_stats() or sg_free_swap_stats(), respectively, when they're not needed any more. The caller is responsible for doing it β€” that's you, bestie πŸ’…

🚨 FreeBSD heads-up: 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 sg_mem_stats or a sg_swap_stats πŸ‘‘

✨ sg_mem_stats ✨

typedef struct{
        unsigned long long total;
        unsigned long long free;
        unsigned long long used;
        unsigned long long cache;
        time_t systime;
} sg_mem_stats;
  • total β€” the total amount of real memory, in bytes
  • free β€” the free amount of real memory, in bytes
  • used β€” the used amount of real memory, in bytes
  • cache β€” the amount of real memory in bytes used for caching
  • systime β€” the timestamp when those stats got collected, in seconds since epoch

πŸŽ€ sg_swap_stats πŸŽ€

typedef struct {
        unsigned long long total;
        unsigned long long used;
        unsigned long long free;
        time_t systime;
} sg_swap_stats;
  • total β€” the total swap space, in bytes
  • used β€” the used swap, in bytes
  • free β€” the free swap, in bytes
  • systime β€” the timestamp when those stats got collected, in seconds since epoch

🫢 TODO 🫢

TODO time, bestie: 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) β€” go check it out, iykyk 🌟

🌐 WEBSITE 🌐

⟨https://libstatgrab.org/⟩ β€” that's the website, no cap πŸ¦‹


okay that's the whole vibe: this page is from libstatgrab, dated 2019-03-08, and it's literally sg_get_mem_stats(3). bye bestie πŸ’…βœ¨πŸ«Ά