kstat programming example...

  • Permalink
  • submit to reddit
  • Email
  • Follow



Hello,

I'm trying to write a piece of code to read cpu statistics, mainly cpu load (idle, kernel, 
user, ...) - Solaris 9 (but it shall work under other releases).

I read all the doc I'v under my hands, but I can't find what I need.

I experimented some things, but It doesn't works. It seems to me that I need to read 
module cpu_stats and all instances (one for each cpu), and get the values.

I can get the kstat_t structures, but it seems that the kind of data is KSTAT_TYPE_RAW. 
So, I can't figure out what's the structure of the data on it.

Does someone have a simple example, or a reference where I can look for the information I 
need ?

Thanks,.

Jose-Marcio

-- 
  ---------------------------------------------------------------
  Jose Marcio MARTINS DA CRUZ           Tel. :(33) 01.40.51.93.41
  Ecole des Mines de Paris              http://j-chkmail.ensmp.fr
  60, bd Saint Michel                http://www.ensmp.fr/~martins
  75272 - PARIS CEDEX 06      mailto:Jose-Marcio.Martins@ensmp.fr

0
Reply Jose 9/2/2003 3:46:26 PM

See related articles to this posting


Jose Marcio Martins da Cruz <Jose-Marcio.Martins@ensmp.fr> wrote:
|...
| Does someone have a simple example, or a reference where I can look for the information I 
| need ?

Here's a little example for getting the load average. Hope this helps
Thomas


/*
 * kstat demo
 * cc -O -o kstat_example kstat_example.c -lkstat
 */
#include <stdio.h>
#include <stdlib.h>
#include <kstat.h>

#ifndef	FSCALE
#define	FSHIFT			8			/* bits to right of fixed binary point */
#define	FSCALE			(1<<FSHIFT)
#endif

#define	LOADDOUBLE(value)	((double)(value) / FSCALE)

/* ----------------------------------------------------------------------
 * get dynamic system information like load...
 * must be called AFTER OS_GetSystemStaticInformation()
 */
int main(int argc, char *argv[]) {
	kstat_ctl_t		*KstatCtl;			/* handle */
	kstat_t			*MiscKsp;			/* some pointer */
	kstat_named_t	*av1, *av5, *av15;	/* load average */

		/* open kstat() interface */
	if ((KstatCtl = kstat_open()) == NULL ||
		(MiscKsp = kstat_lookup(KstatCtl, "unix", 0, "system_misc")) == NULL) {
		fprintf(stderr, "kstat_open() failed\n");
		exit(1);
	}

		/* update load average */
	if (kstat_read(KstatCtl, MiscKsp, NULL) == -1) {
		fprintf(stderr, "kstat_read() failed\n");
		exit(1);
	}
	av1 = (kstat_named_t *) kstat_data_lookup(MiscKsp, "avenrun_1min");
	av5 = (kstat_named_t *) kstat_data_lookup(MiscKsp, "avenrun_5min");
	av15 = (kstat_named_t *) kstat_data_lookup(MiscKsp, "avenrun_15min");

		/* print values */
	printf("%.1f %.1f %.1f\n",
		LOADDOUBLE(av1->value.ui32),
		LOADDOUBLE(av5->value.ui32),
		LOADDOUBLE(av15->value.ui32));

	kstat_close(KstatCtl);
}

0
Reply Thomas 9/3/2003 6:20:27 AM

Thanks Thomas,

This works for me, but I'd prefer to get the values of "user", "kernel", ...

This seems to more interesting than your example, as avenrun_ values means the mean number 
of process in the run queue, and this includes processes waiting for io, so processes 
which aren't wasting CPU cycles.

May be I'm wrong, but, the values of "user", "kernel", in the cpu_stats data is more 
significative.

The problem I'm having is that your example works for KSTAT_TYPE_NAMED data, not for 
KSTAT_RAW_DATA. As the data I'm getting is of the last kind, I need to know how the data 
is structured and kstat_data_lookup isn't usable for RAW data.

I got an example from top, but it's a little difficult to find yourself on the top code..

Thomas Nau wrote:
> Jose Marcio Martins da Cruz <Jose-Marcio.Martins@ensmp.fr> wrote:
> |...
> | Does someone have a simple example, or a reference where I can look for the information I 
> | need ?
> 
> Here's a little example for getting the load average. Hope this helps
> Thomas
> 
> 
> /*
>  * kstat demo
>  * cc -O -o kstat_example kstat_example.c -lkstat
>  */
> #include <stdio.h>
> #include <stdlib.h>
> #include <kstat.h>
> 
> #ifndef	FSCALE
> #define	FSHIFT			8			/* bits to right of fixed binary point */
> #define	FSCALE			(1<<FSHIFT)
> #endif
> 
> #define	LOADDOUBLE(value)	((double)(value) / FSCALE)
> 
> /* ----------------------------------------------------------------------
>  * get dynamic system information like load...
>  * must be called AFTER OS_GetSystemStaticInformation()
>  */
> int main(int argc, char *argv[]) {
> 	kstat_ctl_t		*KstatCtl;			/* handle */
> 	kstat_t			*MiscKsp;			/* some pointer */
> 	kstat_named_t	*av1, *av5, *av15;	/* load average */
> 
> 		/* open kstat() interface */
> 	if ((KstatCtl = kstat_open()) == NULL ||
> 		(MiscKsp = kstat_lookup(KstatCtl, "unix", 0, "system_misc")) == NULL) {
> 		fprintf(stderr, "kstat_open() failed\n");
> 		exit(1);
> 	}
> 
> 		/* update load average */
> 	if (kstat_read(KstatCtl, MiscKsp, NULL) == -1) {
> 		fprintf(stderr, "kstat_read() failed\n");
> 		exit(1);
> 	}
> 	av1 = (kstat_named_t *) kstat_data_lookup(MiscKsp, "avenrun_1min");
> 	av5 = (kstat_named_t *) kstat_data_lookup(MiscKsp, "avenrun_5min");
> 	av15 = (kstat_named_t *) kstat_data_lookup(MiscKsp, "avenrun_15min");
> 
> 		/* print values */
> 	printf("%.1f %.1f %.1f\n",
> 		LOADDOUBLE(av1->value.ui32),
> 		LOADDOUBLE(av5->value.ui32),
> 		LOADDOUBLE(av15->value.ui32));
> 
> 	kstat_close(KstatCtl);
> }
> 


-- 
  ---------------------------------------------------------------
  Jose Marcio MARTINS DA CRUZ           Tel. :(33) 01.40.51.93.41
  Ecole des Mines de Paris              http://j-chkmail.ensmp.fr
  60, bd Saint Michel                http://www.ensmp.fr/~martins
  75272 - PARIS CEDEX 06      mailto:Jose-Marcio.Martins@ensmp.fr

0
Reply Jose 9/3/2003 8:14:59 AM

Jose Marcio Martins da Cruz wrote:
[..]
> May be I'm wrong, but, the values of "user", "kernel", in the cpu_stats data is more 
> significative.
> 
> The problem I'm having is that your example works for KSTAT_TYPE_NAMED data, not for 
> KSTAT_RAW_DATA. As the data I'm getting is of the last kind, I need to know how the data 
> is structured and kstat_data_lookup isn't usable for RAW data.

look into system-dependent include files like /usr/include/sys/sysinfo.h

There you see structures which have names very similar to the names in
the entries in the kstat-lists. typecast your ks_data pointer to the
appropriate structure and you should have access to the counters.

Andreas

0
Reply apommer 9/3/2003 10:19:44 AM

Hi,

Great !


It's cpu_sysinfo_t.cpu[CPU_STATES]

Thanks a  lot !

Otherwise, It will be integrated on a daemon, so, perl isn't what I need.

Thanks again for everyone who answered...

Jose-Marcio

Andreas Pommer wrote:
> Jose Marcio Martins da Cruz wrote:
> 
> 
> look into system-dependent include files like /usr/include/sys/sysinfo.h
> 
....

> 
> Andreas
> 
/*
  *	System Information.
  */
#define CPU_IDLE	0
#define CPU_USER	1
#define CPU_KERNEL	2
#define CPU_WAIT	3
#define CPU_STATES	4

#define W_IO		0
#define W_SWAP		1
#define W_PIO		2
#define W_STATES	3

typedef struct cpu_sysinfo {
	uint_t	cpu[CPU_STATES]; /* CPU utilization			*/
	uint_t	wait[W_STATES]; /* CPU wait time breakdown		*/
	uint_t	bread;		/* physical block reads			*/
	uint_t	bwrite;		/* physical block writes (sync+async)	*/


-- 
  ---------------------------------------------------------------
  Jose Marcio MARTINS DA CRUZ           Tel. :(33) 01.40.51.93.41
  Ecole des Mines de Paris              http://j-chkmail.ensmp.fr
  60, bd Saint Michel                http://www.ensmp.fr/~martins
  75272 - PARIS CEDEX 06      mailto:Jose-Marcio.Martins@ensmp.fr

0
Reply Jose 9/3/2003 10:55:08 AM

Jose Marcio Martins da Cruz wrote:
[..]
> Great !

I'm happy to see this :-)

> Thanks a  lot !
> Otherwise, It will be integrated on a daemon, [...]

Several years ago I wrote an addon for the ucd-snmp daemon which covered
most of the data that kstat provided. Unfortunately due to some
communication and licence problems it never made its way into the
official version (and I doubt it will integrate with a current version
without problems).

But at that time I faced the same problem so now the answer was quite
easy :-)

Andreas

0
Reply apommer 9/3/2003 12:11:34 PM
comp.unix.solaris 25792 articles. 87 followers. Post

5 Replies
570 Views

Similar Articles

[PageSpeed] 31


  • Permalink
  • submit to reddit
  • Email
  • Follow


Reply:

Similar Artilces:

Looking for programming examples in any programming language
I run a website called Rosetta Code, where the goal is to enable comparison and learning of programming languages by seeing how a programming task is implemented in multiple languages. I started it back in January as a MediaWiki setup, hoping it would grow into an encyclopedia of programming knowledge. It never became an encyclopedia, but it *did* accumulate a hefty amount of code, with 94 tasks solved using 64 programming languages. (Mind you, not all tasks are solved in all programming languages.) I've been particularly pleased to see experimental and rare programming languages...

program not working properly. book example. program included.
The program is an Example from Deitel and Deitel, chapter 6. I typed it in carefully, I believe. The program seems to freeze in the function median, where median calls printArray(). printArray() prints only 80 elements and then it seems to be in a loop of some type running the cpu at 100%. well, freeze may not be the right terminology. really it happens in the printArray(), because printArray() does not finish printing. output i get: ******** Mean ******** The mean is the average value of the data items. The mean is equal to the of all the data items divided by the n...

programming examples
http://ccreweb.org/software/kforth/kforth4.html Lots of good code here for me to learn from, but I can't access it from where I am in the world. Is there a mirror anywhere I could try? *** Free account sponsored by SecureIX.com *** *** Encrypt your Internet usage with a free VPN account from http://www.SecureIX.com *** Colin MacIntyre wrote: > http://ccreweb.org/software/kforth/kforth4.html > > Lots of good code here for me to learn from, but I can't access it from > where I am in the world. Is there a mirror anywhere I could try? > > > *** Free account spon...

Programming Examples
Is there anybody out there having an URL to a resource where Programming Fortran/MS SQL is explained ? Or just send me some samples ! Affe affe17@hotmail.com I suppose this is not quite what you want, but I have been working on a Fortran interface to the SQLite database system (http://www.sqlite.org). That particular site at least explains how to use SQL and in combination with my library you can use the database system from a regular Fortran program. Regards, Arjen Alf Nilsen wrote: | Is there anybody out there having an URL to a resource where Programming | Fortran/MS SQL is expla...

example program
Hi all, can any one give a example program where recursive version is faster than iterative version ? aarklon@gmail.com wrote: > Hi all, > > can any one give a example program where recursive version is faster > than iterative version ? For trivial programs there may or may not be examples where recursion is faster. But recursion is a natural fit for many kinds of programming which would be a pain to implement with iterative methods. Especially with making arrangements to save/restore complex data which may well end up slower than just using recursion. But even if recursi...

example of program
good morning. &nbsp; Does anybody have an exemple of programme using the librairy visa.h . It is only to help to know how to use simply the function of the librairy. I have to communicate with a ANRITSU spectrum analyser via RJ45. For example, if I want to change the frequency, the document provided by ANRITSU tells me to send&nbsp; " [:SENSE]:FREQUENCY:START 1MHZ " . But i don't know if I write directly this command on C++, or if I have to save it into a buffer with " sprintf(buffer,"[:SENSE]:FREQUENCY:START 1MHZ ") and use the function of the visa32.lib ...

AppleSoft Programming Examples?
Hi All, I want to "re-learn" AppleSoft Basic after being away from it for 25+ years. Are there any places on the web that have programming examples (apps, games, etc...) I did a search on Google and couldn't find anything. Thanks! On Aug 30, 10:17 pm, Jcarlson1701 <jcarlson1...@gmail.com> wrote: > Hi All, > > I want to "re-learn" AppleSoft Basic after being away from it for 25+ > years. Are there any places on the web that have programming examples > (apps, games, etc...) I did a search on Google and couldn't find > anything. It kind of d...

NetCDF example programs
The UNIDATA Program http://www.unidata.ucar.edu/ developers of the netCDF library http://www.unidata.ucar.edu/software/netcdf/ makes available a small set of example programs http://www.unidata.ucar.edu/software/netcdf/examples/programs/ in multiple languages as part of their netCDF tutorial. http://www.unidata.ucar.edu/software/netcdf/docs_beta/netcdf-tutorial.html I have written IDL versions of each of the six sample programs. You can download the programs and example netCDF files here http://idl.tamu.edu/downloads/netcdf_examples.zip (a 16 kB zip file). I have ...

Programming examples sought...
Can anyone provide me with sample code (commented) on how to create a window with a few controls? I'd really like to know how to do it programmatically, without using resources. Also how to set & get values of the controls, labels, text boxes, check boxes, radio buttons, etc... All the examples that come with the various programming languages show how to create a window, and draw text or shapes in the window...but none show how to put buttons, drop-down lists, text boxes, etc on the form... Also, what is the best graphical resource editor for the GS? I want to use the Resource edi...

Insecure Programming by example
Hi folks, found an interesting C link here:: http://community.core-sdi.com/~gera/InsecureProgramming/ aarklon@gmail.com said: > Hi folks, > > found an interesting C link here:: > http://community.core-sdi.com/~gera/InsecureProgramming/ I looked at the first example. Anyone programming like that needs a good kicking. -- Richard Heathfield "Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk email: rjh at above domain (but drop the www, obviously) Richard Heathfield wrote: > aarklon@gmail.com said: > > >>Hi folks, >> >&g...

match program example
Does anyone have a simple example of a COBOL match program. I need to become more familiar with the logic. Thank you. Read File1 Read File2 do until File1-EOF and File2-EOF if File1-key < File2-key Perform File1-Process Read File1 else if File2-key < File1-key Perform File2-Process Read File2 else Perform FileEqual-Process Read File1 Read File2 end-if end-do Obviously pseudo-code, not real COBOL. The issue of handling end-of-file I leave as an exercise for you. HTH, Paul <gregjohnson@comcast.net> wrote...

Gui programming example
I'm absolutely new to ruby but I can program in a lot of other languages. To get to know this language better I would like to animate a robot walking to a maze, does anyone of you have pointers to examples on GUI programming with ruby? I'll learn the rest ;-) Thanks! -- "May the source be with you" Sounds like you need something with the power of Tk's Canvas widget. I've been playing with that lately and like it a lot. I'm not familiar with the details of other GUI toolkits for Ruby. I'd like to hear if they have something equivalent to Tk's Canvas w...

QoS example program
hello.. everybody.. I want to make example code of QoS using TCP. I don't understand ACE's QoS example. if you make server/client code of Qos using TCP, please send me~~ *^^* please help.. me ...

QAM Example Programs?
Are there any good/simple QAM example programs online that I can look a and learn how to use QAM. Thanks, DspN00b. On Aug 1, 6:26 am, "glazarev" <g...@magpieti.com> wrote: > Are there any good/simple QAM example programs online that I can look at > and learn how to use QAM. > > Thanks, > > DspN00b. This question is hard to answer without considering a few prerequisite questions: 1. Is this a homework problem? 2. What do you expect the input and output to be? Is it a bunch of symbols and are they in complex baseband? Or do you want the pas...

Example Pop11 programs
I have put a few example programs at: http://www.math.uni.wroc.pl/~p-wyk4/pop11_en -- Waldek Hebisch hebisch@math.uni.wroc.pl ...

Linear Programming Examples
This is a multi-part message in MIME format. ------=_NextPart_000_00A9_01C596BB.310885A0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Hi, I use wgnuplot (4.0) on MS Windows. I see that gnuplot can shade areas = bound by functions. I am tryinng to use gnuplot to shade feasible regions bound by, e.g., x+y>=3D 100 4x+2y>=3D 300 0<=3D x <=3D 75 0<=3D y <=3D 80 Minimize z=3D12x+10y subject to the above constraints. Any relevant example will be much appreciated. Thanks - Danke Shoene! Y...

Examples of win32 programs
Hi. I am looking for example of program which shows how to draw lines, ellipses and rectangles using mouse (written in C without MFC). SzymeX "SzymeX" <szymex@vp.pl> wrote in message news:cvt23t$2vf$1@plaza.suomi.net... > Hi. > I am looking for example of program which shows how to draw lines, ellipses > and rectangles using mouse (written in C without MFC). MSDN has such examples as does many books. Also using Google would give you several examples. To draw use GDI functions such as MoveToEx() and LineToEx(), Polyline(), Ellipse() or Rectan...

openMP: no speedup with example program
I'm using the Portland Group Workshop f95 compiler on a 4 dual-core Opteron 865s machine (i.e. 8 total cores), and am having trouble geting any speedup on a PGI example program.... the following program comes from pp. 136-137 of the "PGI� User's Guide -Parallel Fortran, C and C++ for Scientists and Engineers" I've got 20 MB of shared RAM available on this node. 64-bit Fedora core 3, pgf95 6.1-1 --- begin fortran program ---- PROGRAM CRITICAL_USE USE OMP_LIB REAL*8 A(10000,10000), MX, LMX INTEGER I, J MX = -1.0 LMX = -1.0 ...

Socket Programming HOWTO example
Hi. I read the Gordon McMillan's "Socket Programming Howto". I tried to use the example in this howto but this doesn't work. The code is class mysocket: '''classe solamente dimostrativa - codificata per chiarezza, non per efficenza''' def __init__(self, sock=None): if sock is None: self.sock = socket.socket( socket.AF_INET, socket.SOCK_STREAM) else: self.sock = sock def connect(host, port): self.sock.connect((host, port)) de...

where to see mathlab examples programming?
Please advise. On Sun, 05 Nov 2006 09:50:45 -0500, shermaine wrote: > Please advise. Read this newsgroup. Look for Matlab Central at www.mathworks.com. Dan shermaine wrote: > Please advise. Try the MATLAB section at LiteratePrograms: http://en.literateprograms.org/Category:Programming_language:MATLAB -Will Dwinnell http://will.dwinnell.com shermaine wrote: > Please advise. I will immodestly suggest my new Web log, Data Mining in MATLAB, at: http://matlabdatamining.blogspot.com/ -Will Dwinnell http://will.dwinnell.com ...

Useful examples of computer programs
Hello, everybody, The page http://www.naturalprogramming.com/all_example_programs.html provides links to more than 400 simple computer programs that are written with the Java, C#, C++, and Python programming languages. These programs might be useful for people who want to see how things are done with different languages. For example, there is a program named Game.java, and then there are the programs Game.cs, game.cpp, and Game.py which show how this simple program is written with the other three languages. Best regards, ...

Programming languages: Calculator examples?
I'm curious about how a simple desk calculator is implemented in various languages, implemented using what most might consider the best techniques available in that language. It's easy to find examples, it's more difficult to know whether they'd be regarded as good. E.g., I found this Java Swing example: http://www.1javastreet.com/vb/scripts/ShowCode.asp?txtCodeId=3794&lngWId=2. To my eye it looks absolutely hideous. I wonder if most programmers would regard this as a representative use of Swing. A better-looking example is in Ruby: http://ruby-gnome.sourceforge.ne...

Optimizing, examples of very fast programs ;)
Hi, Last time i'm interested in optimizing small c programs. On my studies we are sending the programs using the web interface to online judge. The person who will wrote the faster program get the bonus score. This are usually simple problems, like sorting small numbers, parsing the text and checking something etc... To get the bonus the good algorithm is not everything, you have to do a lot of optimizations on c level (or asembler sometimes). Ok..., so usually my programs are very fast, but "the best" students are sendings programs few seconds/miliseconds faster. Because i like...

Need examples of C program
Hi all of you can anybody show me examples of C program that can calculate polynomial equations dmp wrote: > > can anybody show me examples of C program that can calculate > polynomial equations Yes, we can. -- Chuck F (cbfalconer at maineline dot net) Available for consulting/temporary embedded and systems. <http://cbfalconer.home.att.net> -- Posted via a free Usenet account from http://www.teranews.com dmp wrote: > Hi all of you > > can anybody show me examples of C program that can calculate > polynomial equations > Have a look at the C p...