info on kstat please

I am trying to use the kernal statistics interface

kstat for a mini-performance montoring suite.

Can anyone direct me to some more detailed information

and, if possible some worked examples ?#

I have seen the example code on the Solaris 2 Competitive Migration

Program

http://www.sun.com/smcc/solaris-migration/docs/courses/sysadminHTML/kernel.html but this seems to be out of date.

In particular I can't figure out just how the kstat_named structure works,

and how you find out what names to use.

[560 byte] By [fulmar] at [2007-11-14]
# 1

Fulmar,

check out enclosed tutorials.

I hope this helps.

Regards

Georg Edelmann

Sun Microsystems Ltd.

--

SYNOPSIS:A tutorial for kstat, the Solaris kernel statistics facility

OS: Solaris/SunOS

PLATFORM:Generic

OS VERSION: 7

TECH AREA:OS

PRODUCT:Kernel

KEYWORDS:kstat kernel statistic

DESCRIPTION:

The following is intended as a hands-on introduction to the Solaris

kernel statistic facility - kstat. Use this as a tutorial. The included

sample programs are fully functional.

SOLUTION:

NOTE : Before you start, make sure you read and understood kstat(3K) and

kstat(9S).

Introduction

kstat facility is a general-purpose mechanism for providing kernel

statistics to userland applications. Whenever a developer needs to gather

statistical data within the kernel and requires these data to be passed on

to an application, kstats are the solution.

The current Solaris implementation makes heavy use of kstat. Multiple

applications are used to read these statistics, including sar(1), netstat(1M),

nfsstat(1M), etc.

The following is a simple example to illustrate the use of kstat. It contains

two major components : the kernel module to generate a kstat

"kstat_drv", and

the application to read this kstat "kstat_app". Both are explained in

detail in

the following sections.

The kernel module "kstat_drv"

--

9

10 static struct modlmisc modlmisc = {

11 &mod_miscops, "A kernel statistics (kstat) example"

12 };

13

14 static struct modlinkage modlinkage = {

15 MODREV_1, (void *)&modlmisc, NULL

16 };

"modlmisc" and "modlinkage" define "kstat_drv" as a

kernel loadable module

of type "misc". This will allow us to use modload(1M)/modunload(1M) to

load/unload the module.

17

18 char *message= "A simple kstat example";

19

That's the kernel statistic we will export to the application. As you can

see there's no structure imposed on it, ie. it's what kstat refers to as

KSTAT_TYPE_RAW - a sequence of bytes.

24

25 ksp = kstat_create("kstat_drv", 0,

"kstat_list", "misc",

26KSTAT_TYPE_RAW, 1, KSTAT_FLAG_WRITABLE);

This is where it gets interesting. This call to kstat_create(9F) creates

a kernel statistic with the following characteristics :

provider's module : kstat_drv

instance : 0

kstat identifier : kstat_list

or name

class : misc

As mentioned, the string of byte in "message" is of kstat type

KSTAT_TYPE_RAW.

This kstat_create() initializes a kstat(9S) structure, allocates memory

for the entire kstat (header and data), initializes all header fields,

initializes the data section to all zeroes, assigns a unique kstat ID (KID)

and puts the kstat onto the system's kstat chain. The returned kstat in

"ksp" is still marked invalid because then we have not initialized the

data section.

27

28 if (ksp != NULL) {

29ksp->ks_data_size = strlen(message);

30ksp->ks_data = message;

31ksp->ks_lock = NULL;

32

33kstat_install(ksp);

34 }

35

Before we install the kstat with kstat_install(9F), we need to copy the

data into the data section of the kstat. This is what happens in line

29 and 30.

If you initialize the lock to NULL as in line 31, a kstat_read(3K) will

not try to acquire a mutex when reading the kstat.

38

39 int _fini(void)

40 {

41 kstat_delete(ksp);

42 return (mod_remove(&modlinkage));

43 }

kstat_delete(9F) cleans up the kstat, that is whenever you unload this module

kstat_delete() removes ksp from the kstat chain and frees all associated

system resources.

The application "kstat_app"

"kstat_app" reads

edelmannG at 2007-7-5 > top of java,Solaris Operating System,Solaris Essentials - General Technical Questions...
# 2

Fulmar,

check out enclosed tutorials. This time hopefully properly formatted.

I hope this helps.

Regards

Georg Edelmann

Sun Microsystems Ltd.

-

<pre>

DOCUMENT ID: 2486-02

SYNOPSIS:How to get a list of all kstat types currently supported in the

system

OS: Solaris/SunOS

PLATFORM:Generic

OS VERSION: 2.6

TECH AREA:OS

PRODUCT:Libraries

KEYWORDS:kstat kstat_read kstat_types

DESCRIPTION:

kstat - kernel statistics facility - defines various kstat data types. How can

I find out which are the ones defined on my system ?

SOLUTION:

To get a list of all kstat types currently supported in the system, tools can

read out the

standard system kstat kstat_types (full name spec is <"unix", 0,

"kstat_types">

). This is

a KSTAT_TYPE_NAMED kstat in which the name field describes the type of kstat,

and the value

field is the kstat type number.

Here's an example program :

#include <stdio.h>

#include <kstat.h>

main()

{

kstat_ctl_t*kc;

kstat_t *ksp;

kstat_named_t*kstat_named;

int i=0;

if ((kc = kstat_open()) == NULL)

{

perror("kstat_open failed"); exit(1);

}

if ((ksp = kstat_lookup(kc, "unix", 0, "kstat_types"))

== NULL)

{

perror("kstat_lookup failed"); exit(1);

}

if (kstat_read(kc, ksp, NULL) < 0)

{

perror("kstat_read failed"); exit(1);

}

kstat_named = ksp->ks_data;

printf("List of all kstat types currently supported : ");

for(i=0; i<ksp->ks_ndata; i++)

{

printf("%s ", kstat_named.name);

}

printf("\n");

}

-

DOCUMENT ID: 2639-99

SYNOPSIS:A tutorial for kstat, the Solaris kernel statistics facility

OS: Solaris/SunOS

PLATFORM:Generic

OS VERSION: 7

TECH AREA:OS

PRODUCT:Kernel

KEYWORDS:kstat kernel statistic

DESCRIPTION:

The following is intended as a hands-on introduction to the Solaris

kernel statistic facility - kstat. Use this as a tutorial. The included

sample programs are fully functional.

SOLUTION:

NOTE : Before you start, make sure you read and understood kstat(3K) and

kstat(9S).

Introduction

kstat facility is a general-purpose mechanism for providing kernel

statistics to userland applications. Whenever a developer needs to gather

statistical data within the kernel and requires these data to be passed on

to an application, kstats are the solution.

The current Solaris implementation makes heavy use of kstat. Multiple

applications are used to read these statistics, including sar(1), netstat(1M),

nfsstat(1M), etc.

The following is a simple example to illustrate the use of kstat. It contains

two major components : the kernel module to generate a kstat

"kstat_drv", and

the application to read this kstat "kstat_app". Both are explained in

detail in

the following sections.

The kernel module "kstat_drv"

--

9

10 static struct modlmisc modlmisc = {

11 &mod_miscops, "A kernel statistics (kstat) example"

12 };

13

14 static struct modlinkage modlinkage = {

15 MODREV_1, (void *)&modlmisc, NULL

16 };

"modlmisc" and "modlinkage" define "kstat_drv" as a

kernel loadable module

of type "misc". This will allow us to use modload(1M)/modunload(1M) to

load/unload the module.

17

18 char *message= "A simple kstat example";

19

That's the kernel statistic we will export to the application. As you can

see there's no structure imposed on it, ie. it's what kstat refers to as

KSTAT_TYPE_RAW - a sequence of byte

edelmannG at 2007-7-5 > top of java,Solaris Operating System,Solaris Essentials - General Technical Questions...
# 3
The following document also describes the layout of the kstat data structures:www.sun.com/smcc/solaris-migration/docs/courses/sysadminHTML/kernal.htmlI also later found by accident that an undocumented option to netstat (-k) also seemsto print the kstat cells.
fulmar at 2007-7-5 > top of java,Solaris Operating System,Solaris Essentials - General Technical Questions...