LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 04-04-2005, 06:19 PM   #1
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86_64; Gentoo PPC; FreeBSD; OS X 10.9.4
Posts: 3,760
Blog Entries: 4

Rep: Reputation: 77
'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.
 
Old 04-05-2005, 12:49 PM   #2
alred
Member
 
Registered: Mar 2005
Location: singapore
Distribution: puppy and Ubuntu and ... erh ... redhat(sort of) :( ... + the venerable bsd and solaris ^_^
Posts: 658
Blog Entries: 8

Rep: Reputation: 31
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
 
Old 04-05-2005, 04:31 PM   #3
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86_64; Gentoo PPC; FreeBSD; OS X 10.9.4
Posts: 3,760
Blog Entries: 4

Original Poster
Rep: Reputation: 77
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

Last edited by bulliver; 04-05-2005 at 05:58 PM.
 
Old 04-05-2005, 10:51 PM   #4
alred
Member
 
Registered: Mar 2005
Location: singapore
Distribution: puppy and Ubuntu and ... erh ... redhat(sort of) :( ... + the venerable bsd and solaris ^_^
Posts: 658
Blog Entries: 8

Rep: Reputation: 31
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 ......?
 
Old 04-05-2005, 11:02 PM   #5
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86_64; Gentoo PPC; FreeBSD; OS X 10.9.4
Posts: 3,760
Blog Entries: 4

Original Poster
Rep: Reputation: 77
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!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
load average? ampex189 Linux - Newbie 2 03-06-2005 08:17 PM
How to use return values from system functions? kpachopoulos Programming 2 07-30-2004 04:09 AM
How to return values into an array using awk Helene Programming 1 05-01-2004 11:05 PM
How can get values return from a system call quenn Programming 1 09-17-2003 12:05 AM
Average load Cyth Linux - General 1 01-22-2002 04:33 PM


All times are GMT -5. The time now is 03:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration