Can not understand load returned by sysinfo

[亡魂溺海] 提交于 2021-02-04 15:13:28

问题


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


回答1:


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.




回答2:


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
}



回答3:


I got the solution.

I forgot to call sysinfo(&sysinfo_obj) function.



来源:https://stackoverflow.com/questions/12303141/can-not-understand-load-returned-by-sysinfo

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!