2016-08-02

C: Linux sysinfo loads interpretation

There's this handy syscall in Linux, the sysinfo (kernel source), which returns some metrics about the current process.

What immediately stands out, is the load averages array. They are long values, how to make them to be our beloved fractional numbers?

These three values are scaled up by 65536, or more precisely by SI_LOAD_SHIFT. So we just divide them:

#include <sys/sysinfo.h>

// ...

struct sysinfo memInfo;
sysinfo(&memInfo);
printf("sysinfo: load1 = %2.2f\n", (float)memInfo.loads[0] / (float)(1 << SI_LOAD_SHIFT) );

No comments :

Post a Comment