Can not understand load returned by sysinfo
Asked Answered
H

3

13

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

Hoopes answered 6/9, 2012 at 15:11 Comment(4)
What kernel are you running? What results do you get after divion by 1 << SI_LOAD_SHIFT? It works fine on my box (3.2.0-23-generic #36-Ubuntu SMP x86_64)Soporific
possible duplicate of sysinfo system call returns wrong load average values on linuxLongoria
Kernel version is Linux 3.0.0-21-generic #35~lucid1-Ubuntu SMP. If i divide load by (1 << SI_LOAD_SHIFT), I get output as 113, 49088, 2052.Hoopes
You can view my code at codepad.org/FuxWFd0PHoopes
U
7

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
}
Ultrasound answered 1/8, 2018 at 5:58 Comment(0)
P
3

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.

Patin answered 6/9, 2012 at 15:52 Comment(0)
H
0

I got the solution.

I forgot to call sysinfo(&sysinfo_obj) function.

Hoopes answered 13/9, 2012 at 10:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.