Skip to content

Instantly share code, notes, and snippets.

@AlexBaranowski
Last active November 4, 2020 17:08
Show Gist options
  • Save AlexBaranowski/68a1549a3f2f29d5e26165bfae496f28 to your computer and use it in GitHub Desktop.
Save AlexBaranowski/68a1549a3f2f29d5e26165bfae496f28 to your computer and use it in GitHub Desktop.
loadavg.c
#include<sys/sysinfo.h>
#include<stdio.h>
// NOTE: FSHIFT might differ
#define FSHIFT 16
#define FIXED_1 (1<<FSHIFT) /* 1.0 as fixed-point */
#define LOAD_INT(x) ((x) >> FSHIFT)
#define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
int main(){
struct sysinfo info;
sysinfo(&info);
printf("%lu.%02lu\n", LOAD_INT(info.loads[0]), LOAD_FRAC(info.loads[0]));
printf("%lu.%02lu\n", LOAD_INT(info.loads[1]), LOAD_FRAC(info.loads[1]));
printf("%lu.%02lu\n", LOAD_INT(info.loads[2]), LOAD_FRAC(info.loads[2]));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment