xref: /onnv-gate/usr/src/cmd/fm/modules/common/eversholt/stats.c (revision 1414:b4126407ac5b)
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  * this version of stats.c links with eft and implements the
290Sstevel@tonic-gate  * stats using the fmd's stats API.
300Sstevel@tonic-gate  */
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #include <sys/types.h>
350Sstevel@tonic-gate #include <strings.h>
360Sstevel@tonic-gate #include "stats.h"
370Sstevel@tonic-gate #include "alloc.h"
380Sstevel@tonic-gate #include "out.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 struct stats {
440Sstevel@tonic-gate 	fmd_stat_t fmd_stats;
450Sstevel@tonic-gate 	enum stats_type {
460Sstevel@tonic-gate 		STATS_COUNTER = 3000,
470Sstevel@tonic-gate 		STATS_ELAPSE,
480Sstevel@tonic-gate 		STATS_STRING
490Sstevel@tonic-gate 	} t;
500Sstevel@tonic-gate 
510Sstevel@tonic-gate 	/* used for STATS_ELAPSE */
520Sstevel@tonic-gate 	hrtime_t start;
530Sstevel@tonic-gate 	hrtime_t stop;
540Sstevel@tonic-gate };
550Sstevel@tonic-gate 
560Sstevel@tonic-gate static int Ext;			/* true if extended stats are enabled */
570Sstevel@tonic-gate 
580Sstevel@tonic-gate /*
590Sstevel@tonic-gate  * stats_init -- initialize the stats module
600Sstevel@tonic-gate  *
610Sstevel@tonic-gate  */
620Sstevel@tonic-gate 
630Sstevel@tonic-gate void
640Sstevel@tonic-gate stats_init(int ext)
650Sstevel@tonic-gate {
660Sstevel@tonic-gate 	Ext = ext;
670Sstevel@tonic-gate }
680Sstevel@tonic-gate 
690Sstevel@tonic-gate void
700Sstevel@tonic-gate stats_fini(void)
710Sstevel@tonic-gate {
720Sstevel@tonic-gate }
730Sstevel@tonic-gate 
740Sstevel@tonic-gate static struct stats *
750Sstevel@tonic-gate stats_new(const char *name, const char *desc, enum stats_type t)
760Sstevel@tonic-gate {
770Sstevel@tonic-gate 	struct stats *ret = MALLOC(sizeof (*ret));
780Sstevel@tonic-gate 
790Sstevel@tonic-gate 	bzero(ret, sizeof (*ret));
800Sstevel@tonic-gate 	ret->t = t;
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 	(void) strlcpy(ret->fmd_stats.fmds_desc, desc,
830Sstevel@tonic-gate 	    sizeof (ret->fmd_stats.fmds_desc));
840Sstevel@tonic-gate 
85*1414Scindi 	/* NULL name means generate a unique name */
86*1414Scindi 	if (name == NULL) {
87*1414Scindi 		static int uniqstat;
88*1414Scindi 
89*1414Scindi 		(void) snprintf(ret->fmd_stats.fmds_name,
90*1414Scindi 		    sizeof (ret->fmd_stats.fmds_name),
91*1414Scindi 		    "stat.rules%d", uniqstat++);
92*1414Scindi 	} else {
93*1414Scindi 		(void) strlcpy(ret->fmd_stats.fmds_name, name,
94*1414Scindi 		    sizeof (ret->fmd_stats.fmds_name));
95*1414Scindi 	}
96*1414Scindi 
970Sstevel@tonic-gate 	switch (t) {
980Sstevel@tonic-gate 	case STATS_COUNTER:
990Sstevel@tonic-gate 		ret->fmd_stats.fmds_type = FMD_TYPE_INT32;
1000Sstevel@tonic-gate 		break;
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate 	case STATS_ELAPSE:
1030Sstevel@tonic-gate 		ret->fmd_stats.fmds_type = FMD_TYPE_TIME;
1040Sstevel@tonic-gate 		break;
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 	case STATS_STRING:
1070Sstevel@tonic-gate 		ret->fmd_stats.fmds_type = FMD_TYPE_STRING;
1080Sstevel@tonic-gate 		break;
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate 	default:
1110Sstevel@tonic-gate 		out(O_DIE, "stats_new: unknown type %d", t);
1120Sstevel@tonic-gate 	}
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 	(void) fmd_stat_create(Hdl, FMD_STAT_NOALLOC, 1, &(ret->fmd_stats));
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	return (ret);
1170Sstevel@tonic-gate }
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate void
1200Sstevel@tonic-gate stats_delete(struct stats *sp)
1210Sstevel@tonic-gate {
1220Sstevel@tonic-gate 	if (sp == NULL)
1230Sstevel@tonic-gate 		return;
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 	fmd_stat_destroy(Hdl, 1, &(sp->fmd_stats));
1260Sstevel@tonic-gate 	FREE(sp);
1270Sstevel@tonic-gate }
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate struct stats *
1300Sstevel@tonic-gate stats_new_counter(const char *name, const char *desc, int ext)
1310Sstevel@tonic-gate {
1320Sstevel@tonic-gate 	if (ext && !Ext)
1330Sstevel@tonic-gate 		return (NULL);		/* extended stats not enabled */
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 	return (stats_new(name, desc, STATS_COUNTER));
1360Sstevel@tonic-gate }
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate void
1390Sstevel@tonic-gate stats_counter_bump(struct stats *sp)
1400Sstevel@tonic-gate {
1410Sstevel@tonic-gate 	if (sp == NULL)
1420Sstevel@tonic-gate 		return;
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	ASSERT(sp->t == STATS_COUNTER);
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 	sp->fmd_stats.fmds_value.i32++;
1470Sstevel@tonic-gate }
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate void
1500Sstevel@tonic-gate stats_counter_add(struct stats *sp, int n)
1510Sstevel@tonic-gate {
1520Sstevel@tonic-gate 	if (sp == NULL)
1530Sstevel@tonic-gate 		return;
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 	ASSERT(sp->t == STATS_COUNTER);
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 	sp->fmd_stats.fmds_value.i32 += n;
1580Sstevel@tonic-gate }
1590Sstevel@tonic-gate 
160*1414Scindi void
161*1414Scindi stats_counter_reset(struct stats *sp)
162*1414Scindi {
163*1414Scindi 	if (sp == NULL)
164*1414Scindi 		return;
165*1414Scindi 
166*1414Scindi 	ASSERT(sp->t == STATS_COUNTER);
167*1414Scindi 
168*1414Scindi 	sp->fmd_stats.fmds_value.i32 = 0;
169*1414Scindi }
170*1414Scindi 
1710Sstevel@tonic-gate int
1720Sstevel@tonic-gate stats_counter_value(struct stats *sp)
1730Sstevel@tonic-gate {
1740Sstevel@tonic-gate 	if (sp == NULL)
1750Sstevel@tonic-gate 		return (0);
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	ASSERT(sp->t == STATS_COUNTER);
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	return (sp->fmd_stats.fmds_value.i32);
1800Sstevel@tonic-gate }
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate struct stats *
1830Sstevel@tonic-gate stats_new_elapse(const char *name, const char *desc, int ext)
1840Sstevel@tonic-gate {
1850Sstevel@tonic-gate 	if (ext && !Ext)
1860Sstevel@tonic-gate 		return (NULL);		/* extended stats not enabled */
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	return (stats_new(name, desc, STATS_ELAPSE));
1890Sstevel@tonic-gate }
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate void
1920Sstevel@tonic-gate stats_elapse_start(struct stats *sp)
1930Sstevel@tonic-gate {
1940Sstevel@tonic-gate 	if (sp == NULL)
1950Sstevel@tonic-gate 		return;
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 	ASSERT(sp->t == STATS_ELAPSE);
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 	sp->start = gethrtime();
2000Sstevel@tonic-gate }
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate void
2030Sstevel@tonic-gate stats_elapse_stop(struct stats *sp)
2040Sstevel@tonic-gate {
2050Sstevel@tonic-gate 	if (sp == NULL)
2060Sstevel@tonic-gate 		return;
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 	ASSERT(sp->t == STATS_ELAPSE);
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 	sp->stop = gethrtime();
2110Sstevel@tonic-gate 	sp->fmd_stats.fmds_value.ui64 = sp->stop - sp->start;
2120Sstevel@tonic-gate }
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate struct stats *
2150Sstevel@tonic-gate stats_new_string(const char *name, const char *desc, int ext)
2160Sstevel@tonic-gate {
2170Sstevel@tonic-gate 	struct stats *r;
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	if (ext && !Ext)
2200Sstevel@tonic-gate 		return (NULL);		/* extended stats not enabled */
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	r = stats_new(name, desc, STATS_STRING);
2230Sstevel@tonic-gate 	return (r);
2240Sstevel@tonic-gate }
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate void
2270Sstevel@tonic-gate stats_string_set(struct stats *sp, const char *s)
2280Sstevel@tonic-gate {
2290Sstevel@tonic-gate 	if (sp == NULL)
2300Sstevel@tonic-gate 		return;
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	ASSERT(sp->t == STATS_STRING);
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	if (sp->fmd_stats.fmds_value.str)
2350Sstevel@tonic-gate 		fmd_hdl_strfree(Hdl, sp->fmd_stats.fmds_value.str);
2360Sstevel@tonic-gate 	sp->fmd_stats.fmds_value.str = fmd_hdl_strdup(Hdl, s, FMD_SLEEP);
2370Sstevel@tonic-gate }
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate /*
2400Sstevel@tonic-gate  * stats_publish -- spew all stats
2410Sstevel@tonic-gate  *
2420Sstevel@tonic-gate  */
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate void
2450Sstevel@tonic-gate stats_publish(void)
2460Sstevel@tonic-gate {
2470Sstevel@tonic-gate 	/* nothing to do for eft */
2480Sstevel@tonic-gate }
249