10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
70Sstevel@tonic-gate * with the License.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate * See the License for the specific language governing permissions
120Sstevel@tonic-gate * and limitations under the License.
130Sstevel@tonic-gate *
140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * CDDL HEADER END
210Sstevel@tonic-gate */
220Sstevel@tonic-gate /*
23*1414Scindi * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate *
260Sstevel@tonic-gate * stats.c -- simple stats tracking table module
270Sstevel@tonic-gate *
280Sstevel@tonic-gate */
290Sstevel@tonic-gate
300Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
310Sstevel@tonic-gate
320Sstevel@tonic-gate #include <sys/types.h>
330Sstevel@tonic-gate #include <strings.h>
340Sstevel@tonic-gate #include "stats.h"
350Sstevel@tonic-gate #include "alloc.h"
360Sstevel@tonic-gate #include "out.h"
370Sstevel@tonic-gate
380Sstevel@tonic-gate struct stats {
390Sstevel@tonic-gate struct stats *next;
400Sstevel@tonic-gate const char *name;
410Sstevel@tonic-gate const char *desc;
420Sstevel@tonic-gate enum stats_type {
430Sstevel@tonic-gate STATS_COUNTER = 3000,
440Sstevel@tonic-gate STATS_ELAPSE,
450Sstevel@tonic-gate STATS_STRING
460Sstevel@tonic-gate } t;
470Sstevel@tonic-gate union {
480Sstevel@tonic-gate int counter;
490Sstevel@tonic-gate struct {
500Sstevel@tonic-gate hrtime_t start;
510Sstevel@tonic-gate hrtime_t stop;
520Sstevel@tonic-gate } elapse;
530Sstevel@tonic-gate const char *string;
540Sstevel@tonic-gate } u;
550Sstevel@tonic-gate };
560Sstevel@tonic-gate
570Sstevel@tonic-gate static int Ext; /* true if extended stats are enabled */
580Sstevel@tonic-gate static struct stats *Statslist;
590Sstevel@tonic-gate static struct stats *Laststats;
600Sstevel@tonic-gate
610Sstevel@tonic-gate
620Sstevel@tonic-gate /*
630Sstevel@tonic-gate * stats_init -- initialize the stats module
640Sstevel@tonic-gate *
650Sstevel@tonic-gate */
660Sstevel@tonic-gate
670Sstevel@tonic-gate void
stats_init(int ext)680Sstevel@tonic-gate stats_init(int ext)
690Sstevel@tonic-gate {
700Sstevel@tonic-gate Ext = ext;
710Sstevel@tonic-gate }
720Sstevel@tonic-gate
730Sstevel@tonic-gate void
stats_fini(void)740Sstevel@tonic-gate stats_fini(void)
750Sstevel@tonic-gate {
760Sstevel@tonic-gate }
770Sstevel@tonic-gate
780Sstevel@tonic-gate static struct stats *
stats_new(const char * name,const char * desc,enum stats_type t)790Sstevel@tonic-gate stats_new(const char *name, const char *desc, enum stats_type t)
800Sstevel@tonic-gate {
810Sstevel@tonic-gate struct stats *ret = MALLOC(sizeof (*ret));
820Sstevel@tonic-gate
830Sstevel@tonic-gate bzero(ret, sizeof (*ret));
840Sstevel@tonic-gate ret->t = t;
850Sstevel@tonic-gate ret->name = STRDUP(name);
860Sstevel@tonic-gate ret->desc = STRDUP(desc);
870Sstevel@tonic-gate
880Sstevel@tonic-gate if (Laststats == NULL)
890Sstevel@tonic-gate Statslist = ret;
900Sstevel@tonic-gate else
910Sstevel@tonic-gate Laststats->next = ret;
920Sstevel@tonic-gate Laststats = ret;
930Sstevel@tonic-gate
940Sstevel@tonic-gate return (ret);
950Sstevel@tonic-gate }
960Sstevel@tonic-gate
970Sstevel@tonic-gate void
stats_delete(struct stats * sp)980Sstevel@tonic-gate stats_delete(struct stats *sp)
990Sstevel@tonic-gate {
1000Sstevel@tonic-gate struct stats *p, *s;
1010Sstevel@tonic-gate
1020Sstevel@tonic-gate if (sp == NULL)
1030Sstevel@tonic-gate return;
1040Sstevel@tonic-gate
1050Sstevel@tonic-gate for (p = NULL, s = Statslist; s != NULL; s = s->next)
1060Sstevel@tonic-gate if (s == sp)
1070Sstevel@tonic-gate break;
1080Sstevel@tonic-gate
1090Sstevel@tonic-gate if (s == NULL)
1100Sstevel@tonic-gate return;
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate if (p == NULL)
1130Sstevel@tonic-gate Statslist = s->next;
1140Sstevel@tonic-gate else
1150Sstevel@tonic-gate p->next = s->next;
1160Sstevel@tonic-gate
1170Sstevel@tonic-gate if (s == Laststats)
1180Sstevel@tonic-gate Laststats = p;
1190Sstevel@tonic-gate
1200Sstevel@tonic-gate FREE((void *)sp->name);
1210Sstevel@tonic-gate FREE((void *)sp->desc);
1220Sstevel@tonic-gate FREE(sp);
1230Sstevel@tonic-gate }
1240Sstevel@tonic-gate
1250Sstevel@tonic-gate struct stats *
stats_new_counter(const char * name,const char * desc,int ext)1260Sstevel@tonic-gate stats_new_counter(const char *name, const char *desc, int ext)
1270Sstevel@tonic-gate {
1280Sstevel@tonic-gate if (ext && !Ext)
1290Sstevel@tonic-gate return (NULL); /* extended stats not enabled */
1300Sstevel@tonic-gate
1310Sstevel@tonic-gate return (stats_new(name, desc, STATS_COUNTER));
1320Sstevel@tonic-gate }
1330Sstevel@tonic-gate
1340Sstevel@tonic-gate void
stats_counter_bump(struct stats * sp)1350Sstevel@tonic-gate stats_counter_bump(struct stats *sp)
1360Sstevel@tonic-gate {
1370Sstevel@tonic-gate if (sp == NULL)
1380Sstevel@tonic-gate return;
1390Sstevel@tonic-gate
1400Sstevel@tonic-gate ASSERT(sp->t == STATS_COUNTER);
1410Sstevel@tonic-gate
1420Sstevel@tonic-gate sp->u.counter++;
1430Sstevel@tonic-gate }
1440Sstevel@tonic-gate
1450Sstevel@tonic-gate void
stats_counter_add(struct stats * sp,int n)1460Sstevel@tonic-gate stats_counter_add(struct stats *sp, int n)
1470Sstevel@tonic-gate {
1480Sstevel@tonic-gate if (sp == NULL)
1490Sstevel@tonic-gate return;
1500Sstevel@tonic-gate
1510Sstevel@tonic-gate ASSERT(sp->t == STATS_COUNTER);
1520Sstevel@tonic-gate
1530Sstevel@tonic-gate sp->u.counter += n;
1540Sstevel@tonic-gate }
1550Sstevel@tonic-gate
156*1414Scindi void
stats_counter_reset(struct stats * sp)157*1414Scindi stats_counter_reset(struct stats *sp)
158*1414Scindi {
159*1414Scindi if (sp == NULL)
160*1414Scindi return;
161*1414Scindi
162*1414Scindi ASSERT(sp->t == STATS_COUNTER);
163*1414Scindi
164*1414Scindi sp->u.counter = 0;
165*1414Scindi }
166*1414Scindi
1670Sstevel@tonic-gate int
stats_counter_value(struct stats * sp)1680Sstevel@tonic-gate stats_counter_value(struct stats *sp)
1690Sstevel@tonic-gate {
1700Sstevel@tonic-gate if (sp == NULL)
1710Sstevel@tonic-gate return (0);
1720Sstevel@tonic-gate
1730Sstevel@tonic-gate ASSERT(sp->t == STATS_COUNTER);
1740Sstevel@tonic-gate
1750Sstevel@tonic-gate return (sp->u.counter);
1760Sstevel@tonic-gate }
1770Sstevel@tonic-gate
1780Sstevel@tonic-gate struct stats *
stats_new_elapse(const char * name,const char * desc,int ext)1790Sstevel@tonic-gate stats_new_elapse(const char *name, const char *desc, int ext)
1800Sstevel@tonic-gate {
1810Sstevel@tonic-gate if (ext && !Ext)
1820Sstevel@tonic-gate return (NULL); /* extended stats not enabled */
1830Sstevel@tonic-gate
1840Sstevel@tonic-gate return (stats_new(name, desc, STATS_ELAPSE));
1850Sstevel@tonic-gate }
1860Sstevel@tonic-gate
1870Sstevel@tonic-gate void
stats_elapse_start(struct stats * sp)1880Sstevel@tonic-gate stats_elapse_start(struct stats *sp)
1890Sstevel@tonic-gate {
1900Sstevel@tonic-gate if (sp == NULL)
1910Sstevel@tonic-gate return;
1920Sstevel@tonic-gate
1930Sstevel@tonic-gate ASSERT(sp->t == STATS_ELAPSE);
1940Sstevel@tonic-gate
1950Sstevel@tonic-gate sp->u.elapse.start = gethrtime();
1960Sstevel@tonic-gate }
1970Sstevel@tonic-gate
1980Sstevel@tonic-gate void
stats_elapse_stop(struct stats * sp)1990Sstevel@tonic-gate stats_elapse_stop(struct stats *sp)
2000Sstevel@tonic-gate {
2010Sstevel@tonic-gate if (sp == NULL)
2020Sstevel@tonic-gate return;
2030Sstevel@tonic-gate
2040Sstevel@tonic-gate ASSERT(sp->t == STATS_ELAPSE);
2050Sstevel@tonic-gate
2060Sstevel@tonic-gate sp->u.elapse.stop = gethrtime();
2070Sstevel@tonic-gate }
2080Sstevel@tonic-gate
2090Sstevel@tonic-gate struct stats *
stats_new_string(const char * name,const char * desc,int ext)2100Sstevel@tonic-gate stats_new_string(const char *name, const char *desc, int ext)
2110Sstevel@tonic-gate {
2120Sstevel@tonic-gate if (ext && !Ext)
2130Sstevel@tonic-gate return (NULL); /* extended stats not enabled */
2140Sstevel@tonic-gate
2150Sstevel@tonic-gate return (stats_new(name, desc, STATS_STRING));
2160Sstevel@tonic-gate }
2170Sstevel@tonic-gate
2180Sstevel@tonic-gate void
stats_string_set(struct stats * sp,const char * s)2190Sstevel@tonic-gate stats_string_set(struct stats *sp, const char *s)
2200Sstevel@tonic-gate {
2210Sstevel@tonic-gate if (sp == NULL)
2220Sstevel@tonic-gate return;
2230Sstevel@tonic-gate
2240Sstevel@tonic-gate ASSERT(sp->t == STATS_STRING);
2250Sstevel@tonic-gate
2260Sstevel@tonic-gate sp->u.string = s;
2270Sstevel@tonic-gate }
2280Sstevel@tonic-gate
2290Sstevel@tonic-gate /*
2300Sstevel@tonic-gate * stats_publish -- spew all stats
2310Sstevel@tonic-gate *
2320Sstevel@tonic-gate */
2330Sstevel@tonic-gate
2340Sstevel@tonic-gate void
stats_publish(void)2350Sstevel@tonic-gate stats_publish(void)
2360Sstevel@tonic-gate {
2370Sstevel@tonic-gate struct stats *sp;
2380Sstevel@tonic-gate
2390Sstevel@tonic-gate for (sp = Statslist; sp; sp = sp->next)
2400Sstevel@tonic-gate switch (sp->t) {
2410Sstevel@tonic-gate case STATS_COUNTER:
2420Sstevel@tonic-gate out(O_OK, "%32s %13d %s", sp->name,
2430Sstevel@tonic-gate sp->u.counter, sp->desc);
2440Sstevel@tonic-gate break;
2450Sstevel@tonic-gate
2460Sstevel@tonic-gate case STATS_ELAPSE:
2470Sstevel@tonic-gate if (sp->u.elapse.start && sp->u.elapse.stop) {
2480Sstevel@tonic-gate hrtime_t delta =
2490Sstevel@tonic-gate sp->u.elapse.stop - sp->u.elapse.start;
2500Sstevel@tonic-gate
2510Sstevel@tonic-gate out(O_OK, "%32s %11lldns %s", sp->name,
2520Sstevel@tonic-gate delta, sp->desc);
2530Sstevel@tonic-gate }
2540Sstevel@tonic-gate break;
2550Sstevel@tonic-gate
2560Sstevel@tonic-gate case STATS_STRING:
2570Sstevel@tonic-gate out(O_OK, "%32s %13s %s", sp->name, sp->u.string,
2580Sstevel@tonic-gate sp->desc);
2590Sstevel@tonic-gate break;
2600Sstevel@tonic-gate
2610Sstevel@tonic-gate default:
2620Sstevel@tonic-gate out(O_DIE, "stats_publish: unknown type %d", sp->t);
2630Sstevel@tonic-gate }
2640Sstevel@tonic-gate }
265