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
5*5433Saf * Common Development and Distribution License (the "License").
6*5433Saf * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*5433Saf * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate *
250Sstevel@tonic-gate * stats.c -- simple stats tracking table module
260Sstevel@tonic-gate *
270Sstevel@tonic-gate * this version of stats.c links with eft and implements the
280Sstevel@tonic-gate * stats using the fmd's stats API.
290Sstevel@tonic-gate */
300Sstevel@tonic-gate
310Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
320Sstevel@tonic-gate
330Sstevel@tonic-gate #include <sys/types.h>
340Sstevel@tonic-gate #include <strings.h>
350Sstevel@tonic-gate #include "stats.h"
360Sstevel@tonic-gate #include "alloc.h"
370Sstevel@tonic-gate #include "out.h"
38*5433Saf #include "stats_impl.h"
390Sstevel@tonic-gate #include <fm/fmd_api.h>
400Sstevel@tonic-gate
410Sstevel@tonic-gate extern fmd_hdl_t *Hdl; /* handle from eft.c */
420Sstevel@tonic-gate
430Sstevel@tonic-gate static int Ext; /* true if extended stats are enabled */
440Sstevel@tonic-gate
450Sstevel@tonic-gate /*
460Sstevel@tonic-gate * stats_init -- initialize the stats module
470Sstevel@tonic-gate *
480Sstevel@tonic-gate */
490Sstevel@tonic-gate
500Sstevel@tonic-gate void
stats_init(int ext)510Sstevel@tonic-gate stats_init(int ext)
520Sstevel@tonic-gate {
530Sstevel@tonic-gate Ext = ext;
540Sstevel@tonic-gate }
550Sstevel@tonic-gate
560Sstevel@tonic-gate void
stats_fini(void)570Sstevel@tonic-gate stats_fini(void)
580Sstevel@tonic-gate {
590Sstevel@tonic-gate }
600Sstevel@tonic-gate
610Sstevel@tonic-gate static struct stats *
stats_new(const char * name,const char * desc,enum stats_type t)620Sstevel@tonic-gate stats_new(const char *name, const char *desc, enum stats_type t)
630Sstevel@tonic-gate {
640Sstevel@tonic-gate struct stats *ret = MALLOC(sizeof (*ret));
650Sstevel@tonic-gate
660Sstevel@tonic-gate bzero(ret, sizeof (*ret));
670Sstevel@tonic-gate ret->t = t;
680Sstevel@tonic-gate
690Sstevel@tonic-gate (void) strlcpy(ret->fmd_stats.fmds_desc, desc,
700Sstevel@tonic-gate sizeof (ret->fmd_stats.fmds_desc));
710Sstevel@tonic-gate
721414Scindi /* NULL name means generate a unique name */
731414Scindi if (name == NULL) {
741414Scindi static int uniqstat;
751414Scindi
761414Scindi (void) snprintf(ret->fmd_stats.fmds_name,
771414Scindi sizeof (ret->fmd_stats.fmds_name),
781414Scindi "stat.rules%d", uniqstat++);
791414Scindi } else {
801414Scindi (void) strlcpy(ret->fmd_stats.fmds_name, name,
811414Scindi sizeof (ret->fmd_stats.fmds_name));
821414Scindi }
831414Scindi
840Sstevel@tonic-gate switch (t) {
850Sstevel@tonic-gate case STATS_COUNTER:
860Sstevel@tonic-gate ret->fmd_stats.fmds_type = FMD_TYPE_INT32;
870Sstevel@tonic-gate break;
880Sstevel@tonic-gate
890Sstevel@tonic-gate case STATS_ELAPSE:
900Sstevel@tonic-gate ret->fmd_stats.fmds_type = FMD_TYPE_TIME;
910Sstevel@tonic-gate break;
920Sstevel@tonic-gate
930Sstevel@tonic-gate case STATS_STRING:
940Sstevel@tonic-gate ret->fmd_stats.fmds_type = FMD_TYPE_STRING;
950Sstevel@tonic-gate break;
960Sstevel@tonic-gate
970Sstevel@tonic-gate default:
980Sstevel@tonic-gate out(O_DIE, "stats_new: unknown type %d", t);
990Sstevel@tonic-gate }
1000Sstevel@tonic-gate
1010Sstevel@tonic-gate (void) fmd_stat_create(Hdl, FMD_STAT_NOALLOC, 1, &(ret->fmd_stats));
1020Sstevel@tonic-gate
1030Sstevel@tonic-gate return (ret);
1040Sstevel@tonic-gate }
1050Sstevel@tonic-gate
1060Sstevel@tonic-gate void
stats_delete(struct stats * sp)1070Sstevel@tonic-gate stats_delete(struct stats *sp)
1080Sstevel@tonic-gate {
1090Sstevel@tonic-gate if (sp == NULL)
1100Sstevel@tonic-gate return;
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate fmd_stat_destroy(Hdl, 1, &(sp->fmd_stats));
1130Sstevel@tonic-gate FREE(sp);
1140Sstevel@tonic-gate }
1150Sstevel@tonic-gate
1160Sstevel@tonic-gate struct stats *
stats_new_counter(const char * name,const char * desc,int ext)1170Sstevel@tonic-gate stats_new_counter(const char *name, const char *desc, int ext)
1180Sstevel@tonic-gate {
1190Sstevel@tonic-gate if (ext && !Ext)
1200Sstevel@tonic-gate return (NULL); /* extended stats not enabled */
1210Sstevel@tonic-gate
1220Sstevel@tonic-gate return (stats_new(name, desc, STATS_COUNTER));
1230Sstevel@tonic-gate }
1240Sstevel@tonic-gate
1250Sstevel@tonic-gate void
stats_counter_bump(struct stats * sp)1260Sstevel@tonic-gate stats_counter_bump(struct stats *sp)
1270Sstevel@tonic-gate {
1280Sstevel@tonic-gate if (sp == NULL)
1290Sstevel@tonic-gate return;
1300Sstevel@tonic-gate
1310Sstevel@tonic-gate ASSERT(sp->t == STATS_COUNTER);
1320Sstevel@tonic-gate
1330Sstevel@tonic-gate sp->fmd_stats.fmds_value.i32++;
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate
1360Sstevel@tonic-gate void
stats_counter_add(struct stats * sp,int n)1370Sstevel@tonic-gate stats_counter_add(struct stats *sp, int n)
1380Sstevel@tonic-gate {
1390Sstevel@tonic-gate if (sp == NULL)
1400Sstevel@tonic-gate return;
1410Sstevel@tonic-gate
1420Sstevel@tonic-gate ASSERT(sp->t == STATS_COUNTER);
1430Sstevel@tonic-gate
1440Sstevel@tonic-gate sp->fmd_stats.fmds_value.i32 += n;
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate
1471414Scindi void
stats_counter_reset(struct stats * sp)1481414Scindi stats_counter_reset(struct stats *sp)
1491414Scindi {
1501414Scindi if (sp == NULL)
1511414Scindi return;
1521414Scindi
1531414Scindi ASSERT(sp->t == STATS_COUNTER);
1541414Scindi
1551414Scindi sp->fmd_stats.fmds_value.i32 = 0;
1561414Scindi }
1571414Scindi
1580Sstevel@tonic-gate int
stats_counter_value(struct stats * sp)1590Sstevel@tonic-gate stats_counter_value(struct stats *sp)
1600Sstevel@tonic-gate {
1610Sstevel@tonic-gate if (sp == NULL)
1620Sstevel@tonic-gate return (0);
1630Sstevel@tonic-gate
1640Sstevel@tonic-gate ASSERT(sp->t == STATS_COUNTER);
1650Sstevel@tonic-gate
1660Sstevel@tonic-gate return (sp->fmd_stats.fmds_value.i32);
1670Sstevel@tonic-gate }
1680Sstevel@tonic-gate
1690Sstevel@tonic-gate struct stats *
stats_new_elapse(const char * name,const char * desc,int ext)1700Sstevel@tonic-gate stats_new_elapse(const char *name, const char *desc, int ext)
1710Sstevel@tonic-gate {
1720Sstevel@tonic-gate if (ext && !Ext)
1730Sstevel@tonic-gate return (NULL); /* extended stats not enabled */
1740Sstevel@tonic-gate
1750Sstevel@tonic-gate return (stats_new(name, desc, STATS_ELAPSE));
1760Sstevel@tonic-gate }
1770Sstevel@tonic-gate
1780Sstevel@tonic-gate void
stats_elapse_start(struct stats * sp)1790Sstevel@tonic-gate stats_elapse_start(struct stats *sp)
1800Sstevel@tonic-gate {
1810Sstevel@tonic-gate if (sp == NULL)
1820Sstevel@tonic-gate return;
1830Sstevel@tonic-gate
1840Sstevel@tonic-gate ASSERT(sp->t == STATS_ELAPSE);
1850Sstevel@tonic-gate
1860Sstevel@tonic-gate sp->start = gethrtime();
1870Sstevel@tonic-gate }
1880Sstevel@tonic-gate
1890Sstevel@tonic-gate void
stats_elapse_stop(struct stats * sp)1900Sstevel@tonic-gate stats_elapse_stop(struct stats *sp)
1910Sstevel@tonic-gate {
1920Sstevel@tonic-gate if (sp == NULL)
1930Sstevel@tonic-gate return;
1940Sstevel@tonic-gate
1950Sstevel@tonic-gate ASSERT(sp->t == STATS_ELAPSE);
1960Sstevel@tonic-gate
1970Sstevel@tonic-gate sp->stop = gethrtime();
1980Sstevel@tonic-gate sp->fmd_stats.fmds_value.ui64 = sp->stop - sp->start;
1990Sstevel@tonic-gate }
2000Sstevel@tonic-gate
2010Sstevel@tonic-gate struct stats *
stats_new_string(const char * name,const char * desc,int ext)2020Sstevel@tonic-gate stats_new_string(const char *name, const char *desc, int ext)
2030Sstevel@tonic-gate {
2040Sstevel@tonic-gate struct stats *r;
2050Sstevel@tonic-gate
2060Sstevel@tonic-gate if (ext && !Ext)
2070Sstevel@tonic-gate return (NULL); /* extended stats not enabled */
2080Sstevel@tonic-gate
2090Sstevel@tonic-gate r = stats_new(name, desc, STATS_STRING);
2100Sstevel@tonic-gate return (r);
2110Sstevel@tonic-gate }
2120Sstevel@tonic-gate
2130Sstevel@tonic-gate void
stats_string_set(struct stats * sp,const char * s)2140Sstevel@tonic-gate stats_string_set(struct stats *sp, const char *s)
2150Sstevel@tonic-gate {
2160Sstevel@tonic-gate if (sp == NULL)
2170Sstevel@tonic-gate return;
2180Sstevel@tonic-gate
2190Sstevel@tonic-gate ASSERT(sp->t == STATS_STRING);
2200Sstevel@tonic-gate
2210Sstevel@tonic-gate if (sp->fmd_stats.fmds_value.str)
2220Sstevel@tonic-gate fmd_hdl_strfree(Hdl, sp->fmd_stats.fmds_value.str);
2230Sstevel@tonic-gate sp->fmd_stats.fmds_value.str = fmd_hdl_strdup(Hdl, s, FMD_SLEEP);
2240Sstevel@tonic-gate }
2250Sstevel@tonic-gate
2260Sstevel@tonic-gate /*
2270Sstevel@tonic-gate * stats_publish -- spew all stats
2280Sstevel@tonic-gate *
2290Sstevel@tonic-gate */
2300Sstevel@tonic-gate
2310Sstevel@tonic-gate void
stats_publish(void)2320Sstevel@tonic-gate stats_publish(void)
2330Sstevel@tonic-gate {
2340Sstevel@tonic-gate /* nothing to do for eft */
2350Sstevel@tonic-gate }
236