kstat programming example...


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
Jose
9/2/2003 3:46:26 PM
comp.unix.solaris 25868 articles. 89 followers. Post Follow

5 Replies
715 Views

Similar Articles

[PageSpeed] 52
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
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
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
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
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
apommer
9/3/2003 12:11:34 PM
Reply:
Similar Artilces:

Re: Program or be Programmed !
Paul E Bennett wrote: > WJ wrote: > >> Paul E Bennett wrote: >> >>> I was displaying a sine-wave of >>> asterix characters scrolling continuously down the screen. 3 lines of >>> code generated the sine-wave (and cosine) numbers and the other 30 or so >>> lines did scaling and arrangements for its display. I was showing the >>> source code in a separate window. >> >> Gauche Scheme: >> >> (define line-length 79) >> (define mid-point (div line-length 2)) >> (define one-line (stri...

A Pythonic Way to Measure and Improve Your Programming Skills?
(inspired by a thread on the psf-members list about Python certification) Those who know me know I am no fan of the programmer certification industry and I agree that your typical certificate arrangement, e.g. to show HR to get that box checked in your candidate file, so a company can skip responsibility in vetting a candidate, is un-Pythonic. However, I do think we need a good answer to the engineering manager or team leader who asks, "how do I know this person knows Python?", and to the candidate who thinks, "I believe I'm good at Python but am I as good as I think...

kstat/getextmntinfo nfs question
Hi i'm hoping someone more knowledgeable will give me a helping hand ... i'm tring to tie the kstat_mntinfo data obtained from kstat_read(3KSTAT) to its corresponding NFS mounted directory. in other words, i would like to be able to display the kstat_io_t and kstat_mntinfo data for every NFS mounted directory path. what prevents me from doing so is that there is no "join key" on these two sets of data (getextmntinfo(3C) + kstat stuff). i could join on dev_t if that were available anywhere in kstat, but it doesn't seem available (or i am just blind and can't see it)...

advice on adapting program for parallel execution
Brain trust -- I need advice on the most straightforward way of adapting an existing program for parallel operation on two cores of a processor. (I'm using IVF 11.1.) This code creates a dll that is called by an executable (originally written in C) to allow real-time listening to an DSP-based audio processor written in Fortran. This is prototype code for algorithm development; the algorithms will eventually be ported to dedicated DSP chips and written in assembler. At the moment, the basic structure of the Fortran code is as follows: Module Common_data Integer::Cont...

Help with Sudoku program on CPC, please?
Hi. I have been contacted by a CPC user in the UK - John Shaw - who has produced a Sudoku puzzle solver. It very nearly works, except at one point it fails, showing a "full memory" error. As he does not have e-mail, and I don't think acting as middleman for technical enquiries is my forte, could anyone who may be able to help John by *phone* please email me at brian@imagebus.co.uk and I'll supply his phone number? TIA. -- Brian Hello, Brian! > Hi. I have been contacted by a CPC user in the UK - John Shaw - who has > produced a Sudoku pu...

how to enter BIOS setup program?
Dell Optiplex GX240. Red Hat Linux 9. Linux 2.4.20-8smp #1 SMP Thu Mar 13 17:45:54 EST 2003 i686 i686 i386 GNU/Linux. I need to change the default boot device from hard drive to CD-ROM. The document says: ========= To change your BIOS settings on an x86, AMD64, or Intel´┐Ż EM64T system, watch the instructions provided on your display when your computer first boots. A line of text appears, telling you which key to press to enter the BIOS settings. ======== But when the machine boots, I don't see any text telling me which key to press to enter the BIOS settings. It only stopped at a...

Shell programming
Hi, how can I read the program output that is executed as a bacrgound process in shell? This works but the program is not executed as a bacground process and I need to know how can I do it. #!/bin/bash /path/to/the/program | while read line do echo $line done -- Kuosmanen Excuse me. If the output should be read by a living human, you, the process should run foreground. Or, am I wrong? I am creating a startup script for the program which runs in background and I need to read the program output to determine did it started correctly. Only way to know did the program started correctly ...

About programming...
Hello, You have to understand me more, as you have noticed i have invented and implemented many algorithms an libraries, and i have come to a conclusion that i want to share with you, i have saw many programmers on the C and C++ and Java newsgroups discussing programming , but i have saw them also focusing more on how to factor the code using objects, generics and/or Lambda expressions, and managing the complexity of the code by using objects and generics and/or Lambda expressions , also i saw them focusing on how to learn more how to respect the standards to be able to r...

Context-controllable content summarization example for the Raiders of the Lost Ark movie plot
http://www.genopharmix.com/TuataraSum/example_context_control.html ...

HP 35s Programming/Solutions Manual
Is anyone aware of a Programming/Solutions Manual for the HP 35s? I noticed that a company called "D'Zign" has a Solutions Manual for the HP 33s, but I have the 35s. I am seeking licensure as a Professional Land Surveyor, and the 35s is one of the few calculators allow on the exams for becoming a Professional Land Surveyor or Professional Engineer. If a manual does not exist, does anyone have plans to make one anytime soon? I'm certain there are a lot of surveyors/engineers who would purchase such a book. Jon K. Perhaps you could search in http://www.hpmuseum.org/ for surv...

EDA, PCB, Mentor Graphics programs 2006
EDA, PCB, Mentor Graphics programs 2006 - , programs, RSLogix5000.Firmaware.KIT.12.01 1CD, AND RSLOGIX 5000 VERSION 12.01-EcHoS,1 CD, CD NR 17 188 AND RS Logix 5 v5.0, 1 CD, CD NR 9952 ======================================================= 2006/03/06 CadSoft.Eagle.v4.16 2006/02/28 Electronics.Workbench.Multisim.v9.0.155 2006/02/28 Electronics.Workbench.Ultiboard.v9.0.155 2006/01/19 Altium Designer v6.0 2CD 2005/11/07 Altera.Quartus.II.V5.1 1CD, Proteus 6 Professional, labcenter.co.uk, IAR visualSTATE v5.0.7.88, IAR Embedded.Workbench for 68HC12.V2.44A, ARM.V4.11A, Atmel.AVR.V3.20A, ...

Wanted: Program Writer, GPLE, GALE, ES-CAPE, etc.
I am looking for full copies and/or just manuals for Beagle Bros Program Writer and Global Program Line Editor (GPLE), MicroSparc Global Applesoft Line Editor (GALE), Extended S-C Applesoft Editor (ES- CAPE), and any other Apple II line/text editors. ...

Blinky Program
Hello again Everyone, I spent a small amount of time recently writing a program that "blinks" text. I did it by first having a user input a string, sys.stdout.write-ing the string, then backing up judging by the number of characters the string has, re-stdout's over it with blank spaces (judged again by it's character count), and then backs up again in a never ending while loop. It keeps each blink .5 seconds spaced as well, and all in the same position. Here is the code. [start code] #blinky.py #Make a blinking user inputed (strings) character by using self made "bl...

Ruby fork from C program
As a learning exercise I'm goofing around with ruby's fork within a C program. I've posted my current testing code here: http://pastie.org/591998 It seems to give me the output I was expecting however, it waits the entire 10 seconds before it proceeds to the next fork call. I thought maybe sleep is somehow blocking or there are likely other things I don't really understand. Can anyone give me any ideas? I'm using ruby 1.9. The goal is to launch ruby processes from this C program. Thanks for any help you might provide. Mike B. On Aug 25, 10:20=A0am, barjunk <barj...

Re: Restricting how many users can access a program #3
Gary, That is a great suggestion, and I implement where I can. Sometimes, that is not an option. -Craig --- On Wed, 7/1/09, Gary Sigsworth <gsigs@SHAW.CA> wrote: > From: Gary Sigsworth <gsigs@SHAW.CA> > Subject: Re: Restricting how many users can access a program > To: HP3000-L@RAVEN.UTC.EDU > Date: Wednesday, July 1, 2009, 1:57 PM > How about running Suprtool in the DQ > or even EQ? > > * To join/leave the list, search archives, change list > settings, * > * etc., please visit http://raven.utc.edu/archives/hp3000-l.html * &...

Quick AutoCAD programming resource (remote)
The 'Four Dimensions' e-Programmer services Thank you for your continuing support and patronage. One of our premier programming services is the Four Dimension Technologies e-Programmer services. As the name indicates, the e-Programmer provides a service for AutoCAD users to gain access to a rapid AutoCAD GIS/Mapping programming source over the Internet. Picture this! You have a deadline to meet and there is a whole lot of project work to do. You have identified the CAD procedures to follow and hired the CAD operators to do the work. Now, they could have done it much fast...

Looking for a program to turn mp3 audio into text??
I need to transcribe some audio interviews, if I do it myself it might take years. Do you know of any programs that will turn my mp3 audio into text? Thanks, Amy <amy_bugbee@yahoo.com> wrote in message news:1140389998.792743.46080@g47g2000cwa.googlegroups.com... >I need to transcribe some audio interviews, if I do it myself it might > take years. Do you know of any programs that will turn my mp3 audio > into text? > Thanks, > Amy For a Mac Good Luck. In article <6P6Kf.45$7P1.36@fe10.lga>, "Homey@" <Homey@homeyland.org> wrote: > <amy_b...

Speech synthesis program program interface?
I am developing some educational software. One of the applications tests users on spelling. The program currently uses recorded sound clips saying the word and asks the user to type it in. For the word "capital", for example, the human would record something like, "Capital. What is the capital of North Dakota? Capital." I would like to look into storing that text in the database and having some speech synthesis program speak it to the user. It would be a nice feature if the user could select from several voices (at least male or female). I've done a little research a...

mdb and kstats
I'd like to know how to get some kstats with mdb (or anything else) from a kernel crash dump. In particular, I'm interested in the equivalent of "kstat -m e1000g". If it needs to be more specific, then: kstat -m e1000g -n e1000g1 -i 1 kstat -m e1000g -n statistics -i 1 Hearthbeat for a cluster node failed, so that node was evicted and then clusterware there crashed the kernel (which is normal). I'd like to see interface error counters prior to the kernel crash. I suppose they are burried somewhere in the crash dump, unless they are calculated on read...

Vertex Shader and Fragment Shader in one Assembly file / ARB program ?
Hello, I am wondering if it's possible to have "vertex shader assembly" and "pixel shader assembly" in one file or arb program... because right now it seems CGFX files need to be compiled twice with cgc... one time to extract the vertex assembly... and one time to extract the fragment assembly ?!? Leading to duplicate things which worries me a little bit... for now I will assume it's not a problem and opengl/cg will share these semantics and fields... if not then maybe it's not much of a problem... (?). So I am wondering if there is an "as...

Blather-Adjusting Programs
You folks mean to tell me there's no program out there that would take a file of simple text and blatherise it to the level of another "reference" file? Or Adjust the "Fog Index" of a text file up or down from its input level. I'm totally serious. I'm tired of fighting. I just want to "get along". Message-ID: <dn55rq$mov$1@reader2.panix.com> I'm sure I've seen programs that generate blather but now I can't find one. I'm stuck in a wierd situation that comes up often enough: Some third-worlders insist you write pretentiou...

Optimisation of program
Hello, have u got ideas to optimize this program, in order to reduce the time execution. thanks. proc sql; delete from &__PrestaPop. where ( ClePrePop in (select CleRejet from EchRejet) ); alter table &__PrestaPop. drop column cleprepop; quit; thanks. ash007. into hash, is it possible ? thanks. ash007. ...

Unpredictable programming
"Bruno Desthuilliers" <onurb@xiludom.gro> wrote in message news:44a2b543$0$6509$626a54ce@news.free.fr... > > What bother me here is not about OO being born from needs for simulation > - FWIW, at least part of Bob's assertion seems perfectly and obviously > true: > > <quote> > (OO) is a > computational model comprising a collection of features useful for > constructing large unpredictable state machines from small > predictable state machines > </quote> > Hi Bruno and all, (I originally write this deep in some other thread bu...

conversion of COBOL85 program to ECOBOL program
Hello.,I am currently running a cobol program which is close to 14KLOC and 3 identification divisions. Splitting up further is really difficult. Now, when ever i add more code, i get **** BINDER ERROR 17 **** Code space overflow in proc: MEASTCM. Code block length is: 64997 PAGE 318 \<SOURCE code location> Compaq COBOL85 for NonStop Himalaya Systems No object file created. *** 1 Binder Error i have used ?LESS-CODE 1, ?ICODE, ?CHECK 3, ?COMPACT directives and SET USERLIBRARY ON bind command. Since I also want to use code coverage tool, i wanted t...

Accessing Oracle database on Solaris from C program
I need to perform queries on Oracle database from C/C++ program on Solaris. I have very extensive experience with C/C++ and MS Windows OS but close to none with Solaris or Oracle. Our database adminstrator told me that we do not have any documentation, he does not know if we have any C libraries for accessing Oracle database. The only information he provided me with is that we have Oracle version 9.2.0.7 (Oracle 9i) and Solaris 10. Any help with where to get Oracle C libraries (header and lib files) and any sample source code how to acess it would be greatly appreciated. If this is not the bes...