LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   'load average' return values from sysinfo() (https://www.linuxquestions.org/questions/programming-9/load-average-return-values-from-sysinfo-309720/)

bulliver 04-04-2005 05:19 PM

'load average' return values from sysinfo()
 
Hello all,

I am trying to sharpen my C skills. So as an exercise I have started rewriting some of the basic gnu utils from scratch. I am stuck on one small point in 'uptime' though.

I have it working pretty good so far, however, I am not sure what do do with the 'load average' figures.

The man page for sysinfo (which provides the load average figures) says:
Code:

unsigned long loads[3];  /* 1, 5, and 15 minute load averages */
Now, what's getting returned are ints such as:
354
4353
4545
0
75
etc etc....

My question is what do these numbers represent? I cannot format them into the typical "load average: 0.00, 0.00, 0.00" format used by uptime unless I understand what the figures actually represent....

Are these even the same load average figures represented by uptime?

Thanks for any insight.

alred 04-05-2005 11:49 AM

have you try to look at the source for sysinfo and see how they arrive at that values?

below is the getloadavg function that works on my linux,
if it suits your rewrites of gnu util ,maybe you can temporary use it until you found a way to get a "0.00" values from sysinfo :

Code:

 
double load[3];         
 if (getloadavg(load, 3) != -1)   
{   
 printf("load average : %f , %f , %f\n", load[0],load[1],load[2]); 
  }

good luck to your execise

bulliver 04-05-2005 03:31 PM

Quote:

have you try to look at the source for sysinfo and see how they arrive at that values?
No, I am really not sure where to find it. I will have to hunt through the kernel source I suppose (I am using kernel system call, not glibc).

As for your code, thanks but I already have the values written into my program, converted to floats as you have done here.

What I really need to know is what the figures represent...
Here's a run of my uptime and gnu uptime:

Code:

$ ./my_uptime
 12:25:37 up 0 days, 00:40, 2 users, load average: 7200.00, 5056.00, 5824.00
$ /usr/bin/uptime
 12:25:42 up 40 min,  2 users,  load average: 0.10, 0.08, 0.09

You can see that sysinf0() returned 7200, 5056, and 5824, which I simply represented here as floats. Obviously next to the gnu uptime output the figures require some more work. I think I am getting the right figures here because the numbers always match the gnu output as far as size of the numbers go...

7200 5056 5824
0.10 0.08 0.09

So my question remains, what do 7200, 5056, and 5824 actually represent? Instructions per second? Miles per hour? Rods per hogshead?

I'll try to find the function in the source...

EDIT:
well, after an hour or so I found it in /usr/src/linux/kernel/timer.c

from function sys_sysinfo:
Code:

                val.loads[0] = avenrun[0] << (SI_LOAD_SHIFT - FSHIFT);
                val.loads[1] = avenrun[1] << (SI_LOAD_SHIFT - FSHIFT);
                val.loads[2] = avenrun[2] << (SI_LOAD_SHIFT - FSHIFT);

I am not really sure what that SI_LOAD_SHIFT is doing. 'avenrun' gets it's value from the function calc_load:
Code:

static inline void calc_load(unsigned long ticks)
{
        unsigned long active_tasks; /* fixed-point */
        static int count = LOAD_FREQ;

        count -= ticks;
        if (count < 0) {
                count += LOAD_FREQ;
                active_tasks = count_active_tasks();
                CALC_LOAD(avenrun[0], EXP_1, active_tasks);
                CALC_LOAD(avenrun[1], EXP_5, active_tasks);
                CALC_LOAD(avenrun[2], EXP_15, active_tasks);
        }
}

So it looks as though the figures I am getting returned are snapshots of the current active tasks averaged over 1, 5, and 15 minutes. So far so good. Now I guess I need to figure out what the GNU figures represent :rolleyes:

alred 04-05-2005 09:51 PM

i look again at getloadavg.c , uptime.c and kind of blur for me.
getloadavg is using Proc file to get their values!

in BSD there's a DEFINE for :
Code:

#define LINUX_SYSINFO_LOADS_SCALE 65536
come up with this cheat code that works on my linux :

Code:


struct sysinfo Sinfo;

if ( sysinfo(&Sinfo) != -1)
    {
    printf("load average : %f , %f, %f\n",
    (  (Sinfo.loads[0])/65536.0)
    ,((Sinfo.loads[1])/65536.0)
    ,((Sinfo.loads[2])/65536.0)
    );
    }

as for those SI_LOAD_SHIFT and FSHIFT stuff , looks like kind of
doing a scaling of floating-point , am not too sure about it........

sorry can't help you with an exact answer:(
anybody out there looking at this ......?

bulliver 04-05-2005 10:02 PM

Quote:

getloadavg is using Proc file to get their values!
Yes, I did see that. Kind of seems like cheating to me :)

Anyway, that 65536 value you found seems to be the one I'm looking for. I tried dividing the load average by the machines bogomips, but the figures were still out of whack. The return values I'm getting divided by 65536 gives me the GNU output exactly (...rounded to 2 decimal places), so the mystery is solved...

Thanks for your help alred!


All times are GMT -5. The time now is 06:59 PM.