Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

cpu_stat (Solaris)

80 views
Skip to first unread message

een...@my-deja.com

unread,
Apr 26, 2000, 3:00:00 AM4/26/00
to

Hi,

I wrote this program to compute CPU utlization:

void
show_cpu()
{
kstat_ctl_t *kc;
kstat_t *ksp;
int cpu_count, c;
uint_t id, us, sy, w;
cpu_stat_t *cpustats;
cpu_sysinfo_t info;

kc = kstat_open();
if (kc == NULL) {
printf(" kstat_open() failed on device /dev/kstat\n");
return;
}

/*
* Find number of CPU's
*/
cpu_count = 0;
for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
if (strncmp(ksp->ks_name, "cpu_stat", 8) == 0) {
cpu_count++;
}
}

/*
* Fill in the CPU stat's structure
*/
cpustats = malloc(cpu_count * sizeof(kstat_t));

if (cpustats == NULL) {
kstat_close(kc);
printf(" out of memory\n");
return;
}
for (c = 0; c < cpu_count; c++) {
for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
if ( (strncmp(ksp->ks_name, "cpu_stat", 8) ==
0) &&
(kstat_read(kc, ksp, NULL) != 1)
) {

cpustats =
(struct cpu_stat
*)ksp->ks_data;
/*
* Print Values
*/
info = cpustats->cpu_sysinfo;
id = info.cpu[CPU_IDLE];
us = info.cpu[CPU_USER];
sy = info.cpu[CPU_KERNEL];
w = info.cpu[CPU_WAIT];
printf(" %3d %3u %3u %3u %3u\n",
c, id, us, sy, w);
}

}
}
/*
* close
*/
kstat_close(kc);
}

When I ran it, I obtained the following values:
0 212883589 73463934 6607879 17638273

AND THE VALUES FROM vmstat are
% vmstat
procs memory page disk faults
cpu
r b w swap free re mf pi po fr de sr dd dd f0 s2 in sy cs
us
sy id
0 0 4 6408 3968 0 18 14 38 95 0 40 1 1 0 2 331 502 263
24 2 74


Clearly there is something wrong with the way I am interpreting the
values from cpu_sysinfo structure. Can some tell me what else I need
to
do to get the correct values.

thanks a lot
eenadu

Sent via Deja.com http://www.deja.com/
Before you buy.

Mattias Engdegård

unread,
Apr 26, 2000, 3:00:00 AM4/26/00
to
In <8e5o3a$pfe$1...@nnrp1.deja.com> een...@my-deja.com writes:

> info = cpustats->cpu_sysinfo;
> id = info.cpu[CPU_IDLE];
> us = info.cpu[CPU_USER];
> sy = info.cpu[CPU_KERNEL];
> w = info.cpu[CPU_WAIT];
> printf(" %3d %3u %3u %3u %3u\n",
> c, id, us, sy, w);

> When I ran it, I obtained the following values:
> 0 212883589 73463934 6607879 17638273

Looks reasonable. With 10ms ticks, you have 212883589 / (100 * 86400) ~ 24.6
days of idle time. Usually the absolute values are not very interesting, but
the differences between subsequent samples taken a couple of seconds apart.


John Gorman

unread,
Apr 27, 2000, 3:00:00 AM4/27/00
to
Hello,

I have been messing around with your program and made some changes.

For one, you are display data twice. You find out how many cpu's
there are, then you display the data for each cpu twice. For instance
if you have 2 cpu's (cpu_stat1, cpu_stat3), you need to display
the info for each of them.

Also, I have not had time to try it yet, but I think if you
want to compare it vmstat, you would want to go against the
cpu_vminfo_t struct.

Here are my changes (for Solaris 2.5.1):

John Gorman

---begin cut------------------------------------------------------------
#include <stdio.h>
#include <sys/sysinfo.h>
#include <kstat.h>

void show_cpu()
{
int cpu_count;
int c;
ulong_t cpu_idle_secs; <<<
ulong_t cpu_user_secs; < ulong not uint
ulong_t cpu_sys_secs; <
ulong_t cpu_wait_secs; <<<

kstat_ctl_t *kc;
kstat_t *ksp;
cpu_stat_t *cpustats;
cpu_sysinfo_t info;

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


{
printf(" kstat_open() failed on device /dev/kstat\n");
return;
}

/*
* Allocate a cpustats structure
*/
if ( NULL == (cpustats = malloc(sizeof(kstat_t)) ) )


{
kstat_close(kc);
printf(" out of memory\n");
return;
}

printf(" cpu Idle User System Wait\n");

for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next)
{
if ( (strncmp(ksp->ks_name, "cpu_stat", 8) == 0) &&
(kstat_read(kc, ksp, NULL) != 1) )
{

c = ksp->ks_name[strlen(ksp->ks_name) - 1];

cpustats = (struct cpu_stat*)ksp->ks_data;


/*
* Print Values
*/
info = cpustats->cpu_sysinfo;

cpu_idle_secs = info.cpu[CPU_IDLE];
cpu_user_secs = info.cpu[CPU_USER];
cpu_sys_secs = info.cpu[CPU_KERNEL];
cpu_wait_secs = info.cpu[CPU_WAIT];

printf(" %3c %10ld %10ld %10ld %10ld\n",
c, cpu_idle_secs, cpu_user_secs,
cpu_sys_secs, cpu_wait_secs);
}
}

/*
* close
*/
kstat_close(kc);
}


int main()
{
show_cpu();

return(0);
}


---end cut--------------------------------------------------------------

> info = cpustats->cpu_sysinfo;
> id = info.cpu[CPU_IDLE];
> us = info.cpu[CPU_USER];
> sy = info.cpu[CPU_KERNEL];
> w = info.cpu[CPU_WAIT];
> printf(" %3d %3u %3u %3u %3u\n",
> c, id, us, sy, w);
> }
>
> }
> }

> /*
> * close
> */
> kstat_close(kc);
> }
>

> When I ran it, I obtained the following values:
> 0 212883589 73463934 6607879 17638273
>

0 new messages