[SunHELP] mpstat output shows negative number

Lumpkin, Buddy Buddy.Lumpkin at nordstrom.com
Mon Jan 26 23:52:49 CST 2004


How long has the system been up? Use the uptime command to find out. I
bet it's been up for a very long time.

Most kernel statistics are tracked by counters stored in signed and
unsigned data types. The values in vmstat are signed data types so they
will eventually overflow as the counter gets larger. To see what I mean,
run this on a Solaris 8 or newer host:

kstat -m cpu_stat -c misc -i 0 -n cpu_stat0 -s wait_io 5

Note the value stored in wait_io increments so long as there is some I/O
wait reported in the mpstat column for CPU0. Not all kstats are
counters, but in order to get the first line of mpstat, vmstat,iostat,
etc. (since they are the average value since the system was booted) you
have to use the counter and the number of samples since boot to generate
the average values (until the data type overflows).

I have some kstat code that I extend every time I want to retrieve some
new kernel stats. If your curious what it takes to grab this kind of
information, I extended it to grab the value of the iowait on CPU0, note
that it's littered with code for displaying VM stats as well, which was
my original reason for hacking it together. Anyway, I thought I would be
getting the value in the counter, but it seems im not getting the
historical data, only the last sample. Im not sure why, but it's late so
I will look into this later.

--Buddy

------------------------------------------------------------------------
---
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <kstat.h>
#include <sys/types.h>
#include <sys/sysinfo.h>

int main()
{

    kstat_ctl_t    *kc;
    kstat_t        *ksp;
    kstat_named_t  *buf;
    cpu_stat_t     *cpu_buf;
    char           *tmp;
    int            x;


/*
kstat -m cpu_stat -c misc -i 0 -n cpu_stat0 -s wait_io 5
*/

    kc = kstat_open();
    for (ksp = kc->kc_chain; ksp != NULL; ksp = ksp->ks_next)
    {
   /* - VM search commented out - displays values of lotsfree, freemem,
etc.
        if (    (strcmp(ksp->ks_module, "unix") == 0) &&\
                (strcmp(ksp->ks_class, "pages") == 0) &&\
                (strcmp(ksp->ks_name, "system_pages") == 0)
           )
   */
        if (    (strcmp(ksp->ks_module, "cpu_stat") == 0) &&\
                (strcmp(ksp->ks_class, "misc") == 0) &&\
                (strcmp(ksp->ks_name, "cpu_stat0") == 0)
           )
        {
            buf = malloc(ksp->ks_data_size);
            kstat_read(kc, ksp, buf);
            switch ( ksp->ks_type )
            {
                case KSTAT_TYPE_RAW:
                /*  printf("Kstat type is KSTAT_TYPE_RAW\n"); */
                    cpu_buf = (cpu_stat_t *)buf;
                    printf("The value of iowait for CPU 0 is: %d\n",
cpu_buf->cpu_syswait.iowait);
                    break;
                case KSTAT_TYPE_NAMED:
                    for ( x=0; x < ksp->ks_ndata ; x++ )
                    {
                        switch ( buf[x].data_type )
                        {
                            case KSTAT_DATA_CHAR:
                                printf("Not sure how to print char c[16]
(128 bit int) datatype\n");
                                break;
                            case KSTAT_DATA_INT32:
                                printf("%s: %d\n", buf[x].name,
buf[x].value.i32);
                                break;
                            case KSTAT_DATA_UINT32:
                                printf("%s: %u\n", buf[x].name,
buf[x].value.ui32);
                                break;
                            case KSTAT_DATA_INT64:
                                printf("%s: %ll\n", buf[x].name,
buf[x].value.i64);
                                break;
                            case KSTAT_DATA_UINT64:
                                printf("%s: %llu\n", buf[x].name,
buf[x].value.ui64);
                                break;
                            default:
                                printf("ERROR: Data type is
undetermined\n");
                                exit(0);
                                break;
                        }
                    }
                    printf("Kstat type is KSTAT_TYPE_NAMED\n");
                    break;
                case KSTAT_TYPE_TIMER:
                    printf("Kstat type is KSTAT_TYPE_TIMER\n");
                    break;
                case KSTAT_TYPE_INTR:
                    printf("Kstat type is KSTAT_TYPE_INTR\n");
                    break;
                case KSTAT_TYPE_IO:
                    printf("Kstat type is KSTAT_TYPE_IO\n");
                    break;
                default:
                    printf("Not sure what Kstat type is ...\n");
            }

            free(buf);
        }
    }
}
------------------------------------------------------------------------
-

-----Original Message-----
From: Phil Stracchino [mailto:alaric at caerllewys.net]
Sent: Monday, January 26, 2004 11:36 AM
To: sunhelp at sunhelp.org
Subject: Re: [SunHELP] mpstat output shows negative number

On Mon, Jan 26, 2004 at 12:11:07PM -0500, Nguyen, David M wrote:
> I run command "mpstat" to check cpu status and notice it shows
negative
> number.  Why does it show negative number and what does it mean?
>
> system1#  mpstat
>
> CPU minf mjf xcal  intr ithr  csw icsw migr smtx  srw syscl  usr sys
wt
> idl
>   0 -111 -14  -42  -259 -116  -86  -84 -359 -205  -14  -372  -65 -48
-21
> -182
>   1 -136 -13 -375  -232 -316 -415  -55  -16 -207  -14  -264  -65 -52
-21
> -178
>   2 -132 -12 -289  -238 -320 -397  -61  -24 -194  -14    -2  -62 -49
-21
> -185
>   3 -129 -10 -124  -111 -210 -228 -113 -414 -137  -12  -330  -60 -49
-18
> -190


That's really bizarre.  I've never seen mpstat do that.


--
 .*********  Fight Back!  It may not be just YOUR life at risk.
*********.
 : phil stracchino : unix ronin : renaissance man : mystic zen biker
geek :
 : alaric at caerllewys.net|alaric-ruthven at earthlink.net|alaric at novylen.net
:
 :   2000 CBR929RR, 1991 VFR750F3 (foully murdered), 1986 VF500F (sold)
:
 :    Linux Now!   ...Because friends don't let friends use Microsoft.
:
_______________________________________________
SunHELP maillist  -  SunHELP at sunhelp.org
http://www.sunhelp.org/mailman/listinfo/sunhelp



More information about the SunHELP mailing list