17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*7aec1d6eScindi  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  *
267c478bd9Sstevel@tonic-gate  * stats.c -- simple stats tracking table module
277c478bd9Sstevel@tonic-gate  *
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <sys/types.h>
317c478bd9Sstevel@tonic-gate #include <strings.h>
327c478bd9Sstevel@tonic-gate #include "stats.h"
337c478bd9Sstevel@tonic-gate #include "alloc.h"
347c478bd9Sstevel@tonic-gate #include "out.h"
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate struct stats {
377c478bd9Sstevel@tonic-gate 	struct stats *next;
387c478bd9Sstevel@tonic-gate 	const char *name;
397c478bd9Sstevel@tonic-gate 	const char *desc;
407c478bd9Sstevel@tonic-gate 	enum stats_type {
417c478bd9Sstevel@tonic-gate 		STATS_COUNTER = 3000,
427c478bd9Sstevel@tonic-gate 		STATS_ELAPSE,
437c478bd9Sstevel@tonic-gate 		STATS_STRING
447c478bd9Sstevel@tonic-gate 	} t;
457c478bd9Sstevel@tonic-gate 	union {
467c478bd9Sstevel@tonic-gate 		int counter;
477c478bd9Sstevel@tonic-gate 		struct {
487c478bd9Sstevel@tonic-gate 			hrtime_t start;
497c478bd9Sstevel@tonic-gate 			hrtime_t stop;
507c478bd9Sstevel@tonic-gate 		} elapse;
517c478bd9Sstevel@tonic-gate 		const char *string;
527c478bd9Sstevel@tonic-gate 	} u;
537c478bd9Sstevel@tonic-gate };
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate static int Ext;			/* true if extended stats are enabled */
567c478bd9Sstevel@tonic-gate static struct stats *Statslist;
577c478bd9Sstevel@tonic-gate static struct stats *Laststats;
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate /*
617c478bd9Sstevel@tonic-gate  * stats_init -- initialize the stats module
627c478bd9Sstevel@tonic-gate  *
637c478bd9Sstevel@tonic-gate  */
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate void
stats_init(int ext)667c478bd9Sstevel@tonic-gate stats_init(int ext)
677c478bd9Sstevel@tonic-gate {
687c478bd9Sstevel@tonic-gate 	Ext = ext;
697c478bd9Sstevel@tonic-gate }
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate void
stats_fini(void)727c478bd9Sstevel@tonic-gate stats_fini(void)
737c478bd9Sstevel@tonic-gate {
747c478bd9Sstevel@tonic-gate }
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate static struct stats *
stats_new(const char * name,const char * desc,enum stats_type t)777c478bd9Sstevel@tonic-gate stats_new(const char *name, const char *desc, enum stats_type t)
787c478bd9Sstevel@tonic-gate {
797c478bd9Sstevel@tonic-gate 	struct stats *ret = MALLOC(sizeof (*ret));
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 	bzero(ret, sizeof (*ret));
827c478bd9Sstevel@tonic-gate 	ret->t = t;
837c478bd9Sstevel@tonic-gate 	ret->name = STRDUP(name);
847c478bd9Sstevel@tonic-gate 	ret->desc = STRDUP(desc);
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	if (Laststats == NULL)
877c478bd9Sstevel@tonic-gate 		Statslist = ret;
887c478bd9Sstevel@tonic-gate 	else
897c478bd9Sstevel@tonic-gate 		Laststats->next = ret;
907c478bd9Sstevel@tonic-gate 	Laststats = ret;
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	return (ret);
937c478bd9Sstevel@tonic-gate }
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate void
stats_delete(struct stats * sp)967c478bd9Sstevel@tonic-gate stats_delete(struct stats *sp)
977c478bd9Sstevel@tonic-gate {
987c478bd9Sstevel@tonic-gate 	struct stats *p, *s;
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	if (sp == NULL)
1017c478bd9Sstevel@tonic-gate 		return;
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	for (p = NULL, s = Statslist; s != NULL; s = s->next)
1047c478bd9Sstevel@tonic-gate 		if (s == sp)
1057c478bd9Sstevel@tonic-gate 			break;
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	if (s == NULL)
1087c478bd9Sstevel@tonic-gate 		return;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	if (p == NULL)
1117c478bd9Sstevel@tonic-gate 		Statslist = s->next;
1127c478bd9Sstevel@tonic-gate 	else
1137c478bd9Sstevel@tonic-gate 		p->next = s->next;
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	if (s == Laststats)
1167c478bd9Sstevel@tonic-gate 		Laststats = p;
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	FREE((void *)sp->name);
1197c478bd9Sstevel@tonic-gate 	FREE((void *)sp->desc);
1207c478bd9Sstevel@tonic-gate 	FREE(sp);
1217c478bd9Sstevel@tonic-gate }
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate struct stats *
stats_new_counter(const char * name,const char * desc,int ext)1247c478bd9Sstevel@tonic-gate stats_new_counter(const char *name, const char *desc, int ext)
1257c478bd9Sstevel@tonic-gate {
1267c478bd9Sstevel@tonic-gate 	if (ext && !Ext)
1277c478bd9Sstevel@tonic-gate 		return (NULL);		/* extended stats not enabled */
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	return (stats_new(name, desc, STATS_COUNTER));
1307c478bd9Sstevel@tonic-gate }
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate void
stats_counter_bump(struct stats * sp)1337c478bd9Sstevel@tonic-gate stats_counter_bump(struct stats *sp)
1347c478bd9Sstevel@tonic-gate {
1357c478bd9Sstevel@tonic-gate 	if (sp == NULL)
1367c478bd9Sstevel@tonic-gate 		return;
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	ASSERT(sp->t == STATS_COUNTER);
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	sp->u.counter++;
1417c478bd9Sstevel@tonic-gate }
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate void
stats_counter_add(struct stats * sp,int n)1447c478bd9Sstevel@tonic-gate stats_counter_add(struct stats *sp, int n)
1457c478bd9Sstevel@tonic-gate {
1467c478bd9Sstevel@tonic-gate 	if (sp == NULL)
1477c478bd9Sstevel@tonic-gate 		return;
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	ASSERT(sp->t == STATS_COUNTER);
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	sp->u.counter += n;
1527c478bd9Sstevel@tonic-gate }
1537c478bd9Sstevel@tonic-gate 
154*7aec1d6eScindi void
stats_counter_reset(struct stats * sp)155*7aec1d6eScindi stats_counter_reset(struct stats *sp)
156*7aec1d6eScindi {
157*7aec1d6eScindi 	if (sp == NULL)
158*7aec1d6eScindi 		return;
159*7aec1d6eScindi 
160*7aec1d6eScindi 	ASSERT(sp->t == STATS_COUNTER);
161*7aec1d6eScindi 
162*7aec1d6eScindi 	sp->u.counter = 0;
163*7aec1d6eScindi }
164*7aec1d6eScindi 
1657c478bd9Sstevel@tonic-gate int
stats_counter_value(struct stats * sp)1667c478bd9Sstevel@tonic-gate stats_counter_value(struct stats *sp)
1677c478bd9Sstevel@tonic-gate {
1687c478bd9Sstevel@tonic-gate 	if (sp == NULL)
1697c478bd9Sstevel@tonic-gate 		return (0);
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	ASSERT(sp->t == STATS_COUNTER);
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	return (sp->u.counter);
1747c478bd9Sstevel@tonic-gate }
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate struct stats *
stats_new_elapse(const char * name,const char * desc,int ext)1777c478bd9Sstevel@tonic-gate stats_new_elapse(const char *name, const char *desc, int ext)
1787c478bd9Sstevel@tonic-gate {
1797c478bd9Sstevel@tonic-gate 	if (ext && !Ext)
1807c478bd9Sstevel@tonic-gate 		return (NULL);		/* extended stats not enabled */
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	return (stats_new(name, desc, STATS_ELAPSE));
1837c478bd9Sstevel@tonic-gate }
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate void
stats_elapse_start(struct stats * sp)1867c478bd9Sstevel@tonic-gate stats_elapse_start(struct stats *sp)
1877c478bd9Sstevel@tonic-gate {
1887c478bd9Sstevel@tonic-gate 	if (sp == NULL)
1897c478bd9Sstevel@tonic-gate 		return;
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	ASSERT(sp->t == STATS_ELAPSE);
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	sp->u.elapse.start = gethrtime();
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate void
stats_elapse_stop(struct stats * sp)1977c478bd9Sstevel@tonic-gate stats_elapse_stop(struct stats *sp)
1987c478bd9Sstevel@tonic-gate {
1997c478bd9Sstevel@tonic-gate 	if (sp == NULL)
2007c478bd9Sstevel@tonic-gate 		return;
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	ASSERT(sp->t == STATS_ELAPSE);
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	sp->u.elapse.stop = gethrtime();
2057c478bd9Sstevel@tonic-gate }
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate struct stats *
stats_new_string(const char * name,const char * desc,int ext)2087c478bd9Sstevel@tonic-gate stats_new_string(const char *name, const char *desc, int ext)
2097c478bd9Sstevel@tonic-gate {
2107c478bd9Sstevel@tonic-gate 	if (ext && !Ext)
2117c478bd9Sstevel@tonic-gate 		return (NULL);		/* extended stats not enabled */
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	return (stats_new(name, desc, STATS_STRING));
2147c478bd9Sstevel@tonic-gate }
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate void
stats_string_set(struct stats * sp,const char * s)2177c478bd9Sstevel@tonic-gate stats_string_set(struct stats *sp, const char *s)
2187c478bd9Sstevel@tonic-gate {
2197c478bd9Sstevel@tonic-gate 	if (sp == NULL)
2207c478bd9Sstevel@tonic-gate 		return;
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	ASSERT(sp->t == STATS_STRING);
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	sp->u.string = s;
2257c478bd9Sstevel@tonic-gate }
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate /*
2287c478bd9Sstevel@tonic-gate  * stats_publish -- spew all stats
2297c478bd9Sstevel@tonic-gate  *
2307c478bd9Sstevel@tonic-gate  */
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate void
stats_publish(void)2337c478bd9Sstevel@tonic-gate stats_publish(void)
2347c478bd9Sstevel@tonic-gate {
2357c478bd9Sstevel@tonic-gate 	struct stats *sp;
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	for (sp = Statslist; sp; sp = sp->next)
2387c478bd9Sstevel@tonic-gate 		switch (sp->t) {
2397c478bd9Sstevel@tonic-gate 		case STATS_COUNTER:
2407c478bd9Sstevel@tonic-gate 			out(O_OK, "%32s %13d %s", sp->name,
2417c478bd9Sstevel@tonic-gate 			    sp->u.counter, sp->desc);
2427c478bd9Sstevel@tonic-gate 			break;
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 		case STATS_ELAPSE:
2457c478bd9Sstevel@tonic-gate 			if (sp->u.elapse.start && sp->u.elapse.stop) {
2467c478bd9Sstevel@tonic-gate 				hrtime_t delta =
2477c478bd9Sstevel@tonic-gate 				    sp->u.elapse.stop - sp->u.elapse.start;
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 				out(O_OK, "%32s %11lldns %s", sp->name,
2507c478bd9Sstevel@tonic-gate 				    delta, sp->desc);
2517c478bd9Sstevel@tonic-gate 			}
2527c478bd9Sstevel@tonic-gate 			break;
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 		case STATS_STRING:
2557c478bd9Sstevel@tonic-gate 			out(O_OK, "%32s %13s %s", sp->name, sp->u.string,
2567c478bd9Sstevel@tonic-gate 			    sp->desc);
2577c478bd9Sstevel@tonic-gate 			break;
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 		default:
2607c478bd9Sstevel@tonic-gate 			out(O_DIE, "stats_publish: unknown type %d", sp->t);
2617c478bd9Sstevel@tonic-gate 		}
2627c478bd9Sstevel@tonic-gate }
263