Unix Technical Forum

Getting loadaverage into a C program on Solaris

This is a discussion on Getting loadaverage into a C program on Solaris within the Sun Solaris Hardware forums, part of the Solaris Operating System category; --> As said in the "Subject: " header, I'm trying to figure out how to read the load average into ...


Go Back   Unix Technical Forum > Unix Operating Systems > Solaris Operating System > Sun Solaris Hardware

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-16-2008, 04:37 PM
DoN. Nichols
 
Posts: n/a
Default Getting loadaverage into a C program on Solaris

As said in the "Subject: " header, I'm trying to figure out how to read
the load average into a program in C on Solaris (in particular, Solaris
2.6, but if it will work in Solaris 10 as well, that would be nice).

There are nice library routines for the purpose in BSD,
including the BSD-flavored SunOs 4.1.x. But I can't find out how to do
it under Solaris.

Any pointers, please?

Yes -- I know that this is more appropriate for a software
newsgroup, but I don't follow any of those, and I hope that someone here
can give me a pointer.

Thanks,
DoN.

--
Email: <dnichols@d-and-d.com> | Voice (all times): (703) 938-4564
(too) near Washington D.C. | http://www.d-and-d.com/dnichols/DoN.html
--- Black Holes are where God is dividing by zero ---
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 01-16-2008, 04:37 PM
Casper H.S. Dik
 
Posts: n/a
Default Re: Getting loadaverage into a C program on Solaris

dnichols@d-and-d.com (DoN. Nichols) writes:

>As said in the "Subject: " header, I'm trying to figure out how to read
>the load average into a program in C on Solaris (in particular, Solaris
>2.6, but if it will work in Solaris 10 as well, that would be nice).


> There are nice library routines for the purpose in BSD,
>including the BSD-flavored SunOs 4.1.x. But I can't find out how to do
>it under Solaris.


> Any pointers, please?


> Yes -- I know that this is more appropriate for a software
>newsgroup, but I don't follow any of those, and I hope that someone here
>can give me a pointer.



Well, Solaris 10 has:

#include <sys/loadavg.h>

int getloadavg(double loadavg[], int nelem);


But for older releases you want something like (you need to get all
three values)

/*
* get loadaverage thru kstat
*
* Casper Dik
*/
#include <sys/param.h>
#include <kstat.h>
#include <string.h>
#include <stdio.h>

main(void)
{
kstat_ctl_t *kc;
kstat_t *ksp;
kstat_named_t *kn;


kc = kstat_open();
if (kc == 0) {
perror("kstat_open");
exit(1);
}

ksp = kstat_lookup(kc, "unix", 0, "system_misc");
if (ksp == 0) {
perror("kstat_lookup");
exit(1);
}
while (1){
if (kstat_read(kc, ksp,0) == -1) {
perror("kstat_read");
exit(1);
}

/* choose one of avenrun_1min, avenrun_5min, avenrun_15min */
kn = kstat_data_lookup(ksp, "avenrun_1min");
if (kn == 0) {
fprintf(stderr,"not found\n");
exit(1);
}
printf("%s %.2f\n", kn->name, (double)kn->value.ul/FSCALE);
sleep(2);
}
exit(0);
}

--
Expressed in this posting are my opinions. They are in no way related
to opinions held by my employer, Sun Microsystems.
Statements on Sun products included here are not gospel and may
be fiction rather than truth.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 01-16-2008, 04:37 PM
DoN. Nichols
 
Posts: n/a
Default Re: Getting loadaverage into a C program on Solaris

According to Casper H.S. Dik <Casper.Dik@Sun.COM>:

I figured that you were likely to be the one who answered first
and best. Thanks.

> dnichols@d-and-d.com (DoN. Nichols) writes:
>
> >As said in the "Subject: " header, I'm trying to figure out how to read
> >the load average into a program in C on Solaris (in particular, Solaris
> >2.6, but if it will work in Solaris 10 as well, that would be nice).


[ ... ]

> > Any pointers, please?


[ ... ]

> Well, Solaris 10 has:
>
> #include <sys/loadavg.h>
>
> int getloadavg(double loadavg[], int nelem);


Ah! I had missed that, because I was working on the older
systems where I really *need* the program. Any clue why Solaris took so
long to add a library routine for this?

>
> But for older releases you want something like (you need to get all
> three values)


This is very close to what I need -- just a bit of tweaking.

I sort of though that the kstat routines were what I needed, but
I could not find where to dig to get the right parameters to search for
in the kernel.

> /*
> * get loadaverage thru kstat
> *


[ ... ]

> /* choose one of avenrun_1min, avenrun_5min, avenrun_15min */
> kn = kstat_data_lookup(ksp, "avenrun_1min");


In particular -- I would never have guessed the avenrun_* names.
Where would I look to find the list of variables down in the kernel, and
what they do next time I need to do this? (Perhaps I should download
the sources for Solaris 10 to discover this -- at least for Solaris 10.)

What I intend to do is to write a small program which would b e
called something like "loadav-over", and pass a parameter to it on the
command line. It would produce on printed output, but simply return a
true or false status, so I could then do something appropriated. (In
this case, "appropriate" is to turn on or off incoming e-mail -- the
qmail-smtpd entry in /etc/inetd.conf -- to throttle the current flood,
since some spammer has forged my domain (with tons of bogus usernames)
on some amazing number of spams, based on the number of bounces which
are flooding in, and then back out as "invalid username".

Without my scripts, the two systems (SS-5s) are climbing to a
load average of 256, and then pretty much becoming useless.

I managed to do it all in shell scripts, but when the
loadaverage is that high, running "uptime", and piping the results
through two instances of "cut" and one of "sed" to select the first
entry (which moves around as the time string and the number of days
change).

I intend to make the program as close to static as I can --
though without a libc.a in current Solaris versions, that will be
limited. :-)

Thanks much,
DoN.
--
Email: <dnichols@d-and-d.com> | Voice (all times): (703) 938-4564
(too) near Washington D.C. | http://www.d-and-d.com/dnichols/DoN.html
--- Black Holes are where God is dividing by zero ---
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 01-16-2008, 04:37 PM
Casper H.S. Dik
 
Posts: n/a
Default Re: Getting loadaverage into a C program on Solaris

dnichols@d-and-d.com (DoN. Nichols) writes:

> In particular -- I would never have guessed the avenrun_* names.
>Where would I look to find the list of variables down in the kernel, and
>what they do next time I need to do this? (Perhaps I should download
>the sources for Solaris 10 to discover this -- at least for Solaris 10.)


"kstat -p"

The kstat interface allows for enumeration of all defined values.

Casper
--
Expressed in this posting are my opinions. They are in no way related
to opinions held by my employer, Sun Microsystems.
Statements on Sun products included here are not gospel and may
be fiction rather than truth.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 01-16-2008, 04:38 PM
DoN. Nichols
 
Posts: n/a
Default Re: Getting loadaverage into a C program on Solaris

According to Casper H.S. Dik <Casper.Dik@Sun.COM>:
> dnichols@d-and-d.com (DoN. Nichols) writes:
>
> > In particular -- I would never have guessed the avenrun_* names.
> >Where would I look to find the list of variables down in the kernel, and
> >what they do next time I need to do this? (Perhaps I should download
> >the sources for Solaris 10 to discover this -- at least for Solaris 10.)

>
> "kstat -p"
>
> The kstat interface allows for enumeration of all defined values.


Thanks. That works on Solaris 10, but the command is not
present on Solaris 2.6 (or even on Solaris 8), so it sounds as though I
would have to write a program to do that. (Hmm ... I see that it is a
perl script, but it still needs things which appear to not be present in
Solaris 2.6 -- probably more perl libraries.

And even with what "kstat -p" gives on Solaris 10 -- it still
lacks the magic bit to tell me that "avenrun" is associated with load
average.

Sorry -- I'm still trying to figure out where this knowledge
comes from when you are digging in the hard way.

In any case -- my original problem was solved, and I thank you
for that.

Thanks much,
DoN.

--
Email: <dnichols@d-and-d.com> | Voice (all times): (703) 938-4564
(too) near Washington D.C. | http://www.d-and-d.com/dnichols/DoN.html
--- Black Holes are where God is dividing by zero ---
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 01-16-2008, 04:38 PM
Casper H.S. Dik
 
Posts: n/a
Default Re: Getting loadaverage into a C program on Solaris

dnichols@d-and-d.com (DoN. Nichols) writes:

> Thanks. That works on Solaris 10, but the command is not
>present on Solaris 2.6 (or even on Solaris 8), so it sounds as though I
>would have to write a program to do that. (Hmm ... I see that it is a
>perl script, but it still needs things which appear to not be present in
>Solaris 2.6 -- probably more perl libraries.


Right, but the underlying mechanisms are all present in 2.6.

> And even with what "kstat -p" gives on Solaris 10 -- it still
>lacks the magic bit to tell me that "avenrun" is associated with load
>average.



Point taken (avenrun is the traditional name of the array in the kernel)

Casper
--
Expressed in this posting are my opinions. They are in no way related
to opinions held by my employer, Sun Microsystems.
Statements on Sun products included here are not gospel and may
be fiction rather than truth.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 01-16-2008, 04:38 PM
DoN. Nichols
 
Posts: n/a
Default Re: Getting loadaverage into a C program on Solaris

According to Casper H.S. Dik <Casper.Dik@Sun.COM>:
> dnichols@d-and-d.com (DoN. Nichols) writes:
>
> > Thanks. That works on Solaris 10, but the command is not
> >present on Solaris 2.6 (or even on Solaris 8), so it sounds as though I
> >would have to write a program to do that. (Hmm ... I see that it is a
> >perl script, but it still needs things which appear to not be present in
> >Solaris 2.6 -- probably more perl libraries.

>
> Right, but the underlying mechanisms are all present in 2.6.


I figured that -- and just have not yet gotten around to
attempting to make it run on 2.6.

> > And even with what "kstat -p" gives on Solaris 10 -- it still
> >lacks the magic bit to tell me that "avenrun" is associated with load
> >average.

>
>
> Point taken (avenrun is the traditional name of the array in the kernel)


Had I known that, I would have been ahead of the game.

However, in OpenBSD (to which I do have source access) it is
called "loadinfo.ldavg[i]".

And SysV-based kernels have traditionally been rather difficult
to get the source for. :-)

I guess that I should go ahead and figure out how to download
the sources for Solaris 10, just so I can have a reference for all of
this. Thank goodness that it is open source now.

Thanks for all your help,
DoN.

--
Email: <dnichols@d-and-d.com> | Voice (all times): (703) 938-4564
(too) near Washington D.C. | http://www.d-and-d.com/dnichols/DoN.html
--- Black Holes are where God is dividing by zero ---
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 01-16-2008, 04:38 PM
Casper H.S. Dik
 
Posts: n/a
Default Re: Getting loadaverage into a C program on Solaris

dnichols@d-and-d.com (DoN. Nichols) writes:

> Had I known that, I would have been ahead of the game.


> However, in OpenBSD (to which I do have source access) it is
>called "loadinfo.ldavg[i]".


Newfangled nonsense .....

> And SysV-based kernels have traditionally been rather difficult
>to get the source for. :-)


Well, the Solaris source code is available in cvs.opensolaris.org

Casper
--
Expressed in this posting are my opinions. They are in no way related
to opinions held by my employer, Sun Microsystems.
Statements on Sun products included here are not gospel and may
be fiction rather than truth.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 01-16-2008, 04:38 PM
DoN. Nichols
 
Posts: n/a
Default Re: Getting loadaverage into a C program on Solaris

According to Casper H.S. Dik <Casper.Dik@Sun.COM>:
> dnichols@d-and-d.com (DoN. Nichols) writes:
>
> > Had I known that, I would have been ahead of the game.

>
> > However, in OpenBSD (to which I do have source access) it is
> >called "loadinfo.ldavg[i]".

>
> Newfangled nonsense .....


What was it in SunOs 4.1.4 and before? Those were BSD based.
(And I can't access that particular source, either.) But if it was
"loadinfo.ldavg[i]", then perhaps Solaris becomes the "newfangled"one as
far as Sun is concerned, at least.

I haven't bothered to dig up my copy of the "Lions" book to see
what it was in version 6 -- or even if load average was tracked back
then. :-)

> > And SysV-based kernels have traditionally been rather difficult
> >to get the source for. :-)

>
> Well, the Solaris source code is available in cvs.opensolaris.org


I did say "traditionally", after all. :-) This availability is
something rather recent, after all.

I'm currently part-way through registering for this. I note
that the username/password for downloading the Solaris 10 binaries was
different and will not work here -- and I am now waiting for the server
to finish accepting my registration. it is running very slowly, so far. :-(

Once that is done, I will start the download.

Thanks much,
DoN.
--
Email: <dnichols@d-and-d.com> | Voice (all times): (703) 938-4564
(too) near Washington D.C. | http://www.d-and-d.com/dnichols/DoN.html
--- Black Holes are where God is dividing by zero ---
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 07:10 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com