To find the load average in linux I use sys/sysinfo.h which include linux/kernel.h, where the following structure is defined:
struct sysinfo {
long uptime; /* Seconds since boot */
unsigned long loads[3]; /* 1, 5, and 15 minute load averages */
unsigned long totalram; /* Total usable main memory size */
unsigned long freeram; /* Available memory size */
unsigned long sharedram; /* Amount of shared memory */
unsigned long bufferram; /* Memory used by buffers */
unsigned long totalswap; /* Total swap space size */
unsigned long freeswap; /* swap space still available */
unsigned short procs; /* Number of current processes */
unsigned short pad; /* explicit padding for m68k */
unsigned long totalhigh; /* Total high memory size */
unsigned long freehigh; /* Available high memory size */
unsigned int mem_unit; /* Memory unit size in bytes */
char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding: libc5 uses this.. */
};
but I think it doesn't give true load.
output : 2552402, 3214049236, 134513148
What does this value mean?
We can find current load using the uptime
command:
$uptime
13:00:14 up 35 min, 2 users, load average: 1.07, 0.95, 0.80
I can't find any connection between above the two outputs.
I have searched on internet. This says that divide it by 2^16 (65536). and I have tried it too. (or do shift 1 by SI_LOAD_SHIFT i.e. 1 << SI_LOAD_SHIFT. because 65536 = 1 << 16)
I use computer which has four i3-2120 processor. Output of 'upitime' has connection with number of cpus. Wikipedia load_average
Looking at the link you gave, you should scale the loads according to the SI_LOAD_SHIFT
constant (included by sys/sysinfo.h
), and introduce the number of available CPUs to convert it into CPU usage %:
struct sysinfo sysinf;
memset(&sysinf, 0, sizeof sysinf);
if (!sysinfo(&sysinf)) {
float f_load = 1.f / (1 << SI_LOAD_SHIFT);
printf("load average (1 min): %.2f (%.0f%% CPU)\n",
sysinf.loads[0] * f_load,
sysinf.loads[0] * f_load * 100/get_nprocs());
// process other loads as well of you need
}
The load averages are represented in fixed point arithmetic. See here for the relevant constants. We see that the last FSHIFT bits are used to represent the fractional part. To get the load averages, you can always do it like /proc/loadavg.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With