4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
26 * stats.c -- simple stats tracking table module
30 #pragma ident "%Z%%M% %I% %E% SMI"
32 #include <sys/types.h>
57 static int Ext
; /* true if extended stats are enabled */
58 static struct stats
*Statslist
;
59 static struct stats
*Laststats
;
63 * stats_init -- initialize the stats module
79 stats_new(const char *name
, const char *desc
, enum stats_type t
)
81 struct stats
*ret
= MALLOC(sizeof (*ret
));
83 bzero(ret
, sizeof (*ret
));
85 ret
->name
= STRDUP(name
);
86 ret
->desc
= STRDUP(desc
);
88 if (Laststats
== NULL
)
91 Laststats
->next
= ret
;
98 stats_delete(struct stats
*sp
)
105 for (p
= NULL
, s
= Statslist
; s
!= NULL
; s
= s
->next
)
120 FREE((void *)sp
->name
);
121 FREE((void *)sp
->desc
);
126 stats_new_counter(const char *name
, const char *desc
, int ext
)
129 return (NULL
); /* extended stats not enabled */
131 return (stats_new(name
, desc
, STATS_COUNTER
));
135 stats_counter_bump(struct stats
*sp
)
140 ASSERT(sp
->t
== STATS_COUNTER
);
146 stats_counter_add(struct stats
*sp
, int n
)
151 ASSERT(sp
->t
== STATS_COUNTER
);
157 stats_counter_reset(struct stats
*sp
)
162 ASSERT(sp
->t
== STATS_COUNTER
);
168 stats_counter_value(struct stats
*sp
)
173 ASSERT(sp
->t
== STATS_COUNTER
);
175 return (sp
->u
.counter
);
179 stats_new_elapse(const char *name
, const char *desc
, int ext
)
182 return (NULL
); /* extended stats not enabled */
184 return (stats_new(name
, desc
, STATS_ELAPSE
));
188 stats_elapse_start(struct stats
*sp
)
193 ASSERT(sp
->t
== STATS_ELAPSE
);
195 sp
->u
.elapse
.start
= gethrtime();
199 stats_elapse_stop(struct stats
*sp
)
204 ASSERT(sp
->t
== STATS_ELAPSE
);
206 sp
->u
.elapse
.stop
= gethrtime();
210 stats_new_string(const char *name
, const char *desc
, int ext
)
213 return (NULL
); /* extended stats not enabled */
215 return (stats_new(name
, desc
, STATS_STRING
));
219 stats_string_set(struct stats
*sp
, const char *s
)
224 ASSERT(sp
->t
== STATS_STRING
);
230 * stats_publish -- spew all stats
239 for (sp
= Statslist
; sp
; sp
= sp
->next
)
242 out(O_OK
, "%32s %13d %s", sp
->name
,
243 sp
->u
.counter
, sp
->desc
);
247 if (sp
->u
.elapse
.start
&& sp
->u
.elapse
.stop
) {
249 sp
->u
.elapse
.stop
- sp
->u
.elapse
.start
;
251 out(O_OK
, "%32s %11lldns %s", sp
->name
,
257 out(O_OK
, "%32s %13s %s", sp
->name
, sp
->u
.string
,
262 out(O_DIE
, "stats_publish: unknown type %d", sp
->t
);