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 25980 articles. 1 followers. Post Follow

5 Replies
925 Views

Similar Articles

[PageSpeed] 0

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:

Is "System Programing in Unix" the same as programing in C in Unix?
Hi, I have taken a course in C (at a university in another state) a while back. I can pick up the langauge easily. I have never used Unix or Linux operating system but plan to install Linux in the near future. Currently, I am taking an online course in Linux (actually two 8 week online course; each is 1 unit): the book used it "A Guide to Unix using Linux" from course technology. I really want to become comfrotable with Unix/Linux I thought that while I have access to a Linux account, I should also start studying programming with C in on the side on my own so that I can claim th...

compile C programs with UNIX system calls (= Unix Programs??)
If the C programs have UNIX system calls such as fork(), alarm(), etc.., we should call it UNIX programs, not traditional C programs? We couldn't compile the programs with system calls using VC++ compiler. I need to compile it under UNIX platform. correct? any other alternatives?? Please advise. Thanks!! On Mon, 3 Jan 2005 jrefactors@hotmail.com wrote: > If the C programs have UNIX system calls such as fork(), alarm(), > etc.., we should call it UNIX programs, not traditional C programs? WHo calls them C programs? Personally, I'd call the UNIX programs. > We couldn'...

What is the difference between solaris patch version(Example :142900-15) and solaris update version (Example: u8). When do we apply
What is the difference between solaris patch version(Example :142900-15) and solaris update version (Example: u8 0r u9 ). When do we apply solaris patch and when do we apply solaris update version. any help greatly appreciated :) On Apr 20, 8:12=A0am, solaris...@gmail.com wrote: > What is the difference between solaris patch version(Example :142900-15) = and solaris update version (Example: u8 0r u9 ). When do we apply solaris p= atch and when do we apply solaris update version. any help greatly apprecia= ted :) The gist of the difference is described like this: The only differe...

A Newbie's Question about Unix Programming Examples
Dear Friends, I am not sure whether I should ask questions in this group, but I really don't know a better place. I just begin to learn programming under unix and was trying to find good websites about Unix/Linux Programming with C or C++, but to my disapointment, I can hardly find some good resources. Now I am reading Richard Stevens's Book "Advanced Programming in the UNIX Environment". But I always have a problem that I don't know how to use the functions and methods he taught in the book. It sounds absurd, but it is really my situation. So I am wondering whe...

Good references for Java programming in Unix(Solaris) environments
Hi all, I am will be working on an assignment which requires me to use Java to do some programming in Unix(Solaris) environment. I was wondering if anyone has done this before and know of any good references on the net for me to start with. Stuff related to processes spawning, concurrency and distributed system would be helpful. Thanks. Thuan Seah ...

Want to run a Solaris Unix compiled C++ program in linux
I got in trouble, When I am trying to run a Solaris Unix compiled C++ program [no GUI I guess, only 90KB after compiled] in linux, I got an error: "this binary code can not run" The problem is that I don't have the source code. What I know is this code is compiled in Unix Solaris system, I am curious and need a solution. How to run this code? will cygwin help? Need I install a Solaris software? Thanks a lot Cheers fAnS. "fAnSKyer" <fanskyer@gmail.com> writes: > I got in trouble, When I am trying to run a Solaris Unix compiled C++ > program [no GUI I g...

SN#17961 Using kstat From Within a Program in the Solaris[TM] OS
SYSTEM NEWS FOR SUN USERS Vol 110 Issue 3 2007-04-16 Article 17961 from section "SysAdmin's Section" libkstat APIs VS. the system() call A quick guide offers examples of the libkstat APIs to do performance analysis and extract operating system metrics. Weisner recommends using the libkstat API instead of the system() call. Details at http://sun.systemnews.com/g?A=17961 Have a custom version of 'System News for Sun Users' delivered to you via email each week in PDF, text or HTML. Only the sections that you select will be included i...

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...

Can an SGI IRIS compiled program run on Solaris 10 or a version of UNIX that can be freely installed on Microsoft virtual PC ?
Hi everyone, I would like to ask if anyone could please help me with the following problem. I want to use a free program that seems to be compiled for an SGI IRIS OS. The source files are available here... http://www.cim.mcgill.ca/ftp/rmsl/usycams/ Is there a free OS that I can install on Microsoft Virtual PC so that I can use this program on a XP host machine? Alternatively, is there any type of software emulator I can install that would allow me to use this program on a windows XP machine ? Is it difficult or even possible to compile the source to run on windows XP or a UNIX based OS tha...

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
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...

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...

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 ...

Program or be Programmed
http://www.rushkoff.com/program-or-be-programmed/ The debate over whether the Net is good or bad for us fills the airwaves and the blogosphere. But for all the heat of claim and counter-claim, the argument is essentially beside the point: it's here; it's everywhere. The real question is, do we direct technology, or do we let ourselves be directed by it and those who have mastered it? ?Choose the former,? writes Rushkoff, ?and you gain access to the control panel of civilization. Choose the latter, and it could be the last real choice you get to make.? ...

Web resources about - kstat programming example... - comp.unix.solaris

Mercury (programming language) - Wikipedia, the free encyclopedia
11.07.2 (August 3, 2012 ; 2 months ago ( 2012-08-03 ) ) Mercury is a functional logic programming language geared towards real-world applications. ...

Programming Note/Self Aggrandizement — Vulcan/Planet Nine Edition
Hey, all: if you’ve got a moment this afternoon, I’ll be talking with Ira Flatow on Science Friday about The Hunt for Vulcan in the context of ...

The sheer volume and quality of Netflix’s original programming is unprecedented
... on its laurels, Netflix CEO Reed Hastings said in an earnings press release that Netflix plans to launch at least 600 hours of original programming ...

Fox News' debate counter-programming #misogynyfest
Fox News' debate counter-programming by digby During last night's debate, the question of women in the military came up and several of the candidates ...

The Future of Programming
I learned how to program on this Apple II over 30 years ago. Over that time period – as a first approximation – programming has stayed the same. ...

Nina Lederman Joins All3Media America As Head Of Scripted Programming
... is signaling a push in the original scripted arena with the appointment of former Lifetime executive Nina Lederman as EVP of Scripted Programming, ...

Mob Programming
I came across the idea of mob programming on an Elixir podcast, Elixir Fountain . Its pair programming on steroids, where you sit together and ...

Amazon's Audible stocks up on original radio programming
Amazon really wants you to listen to its podcast and audio-book service, Audible. And it's hoping to win you over by grabbing comedians like ...

OpenMarket Launches Global SMS Programming Interface
With Automatic Originator Selection, OpenMarket will automatically select the correct messaging originator per the destination address.

Verizon's CFO: Custom TV will 'refresh' to comply with programming contracts
Verizon CFO Fran Shammo said his company will make changes to its FiOS Custom TV product, which granularly controls which programming networks ...

Resources last updated: 2/11/2016 3:23:52 AM