dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / fm / eversholt / common / stats.c
blob4501b8909d94335f00a316df05b84c4a5d74f721
1 /*
2 * CDDL HEADER START
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
7 * with the License.
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]
20 * CDDL HEADER END
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>
33 #include <strings.h>
34 #include "stats.h"
35 #include "alloc.h"
36 #include "out.h"
38 struct stats {
39 struct stats *next;
40 const char *name;
41 const char *desc;
42 enum stats_type {
43 STATS_COUNTER = 3000,
44 STATS_ELAPSE,
45 STATS_STRING
46 } t;
47 union {
48 int counter;
49 struct {
50 hrtime_t start;
51 hrtime_t stop;
52 } elapse;
53 const char *string;
54 } u;
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
67 void
68 stats_init(int ext)
70 Ext = ext;
73 void
74 stats_fini(void)
78 static struct stats *
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));
84 ret->t = t;
85 ret->name = STRDUP(name);
86 ret->desc = STRDUP(desc);
88 if (Laststats == NULL)
89 Statslist = ret;
90 else
91 Laststats->next = ret;
92 Laststats = ret;
94 return (ret);
97 void
98 stats_delete(struct stats *sp)
100 struct stats *p, *s;
102 if (sp == NULL)
103 return;
105 for (p = NULL, s = Statslist; s != NULL; s = s->next)
106 if (s == sp)
107 break;
109 if (s == NULL)
110 return;
112 if (p == NULL)
113 Statslist = s->next;
114 else
115 p->next = s->next;
117 if (s == Laststats)
118 Laststats = p;
120 FREE((void *)sp->name);
121 FREE((void *)sp->desc);
122 FREE(sp);
125 struct stats *
126 stats_new_counter(const char *name, const char *desc, int ext)
128 if (ext && !Ext)
129 return (NULL); /* extended stats not enabled */
131 return (stats_new(name, desc, STATS_COUNTER));
134 void
135 stats_counter_bump(struct stats *sp)
137 if (sp == NULL)
138 return;
140 ASSERT(sp->t == STATS_COUNTER);
142 sp->u.counter++;
145 void
146 stats_counter_add(struct stats *sp, int n)
148 if (sp == NULL)
149 return;
151 ASSERT(sp->t == STATS_COUNTER);
153 sp->u.counter += n;
156 void
157 stats_counter_reset(struct stats *sp)
159 if (sp == NULL)
160 return;
162 ASSERT(sp->t == STATS_COUNTER);
164 sp->u.counter = 0;
168 stats_counter_value(struct stats *sp)
170 if (sp == NULL)
171 return (0);
173 ASSERT(sp->t == STATS_COUNTER);
175 return (sp->u.counter);
178 struct stats *
179 stats_new_elapse(const char *name, const char *desc, int ext)
181 if (ext && !Ext)
182 return (NULL); /* extended stats not enabled */
184 return (stats_new(name, desc, STATS_ELAPSE));
187 void
188 stats_elapse_start(struct stats *sp)
190 if (sp == NULL)
191 return;
193 ASSERT(sp->t == STATS_ELAPSE);
195 sp->u.elapse.start = gethrtime();
198 void
199 stats_elapse_stop(struct stats *sp)
201 if (sp == NULL)
202 return;
204 ASSERT(sp->t == STATS_ELAPSE);
206 sp->u.elapse.stop = gethrtime();
209 struct stats *
210 stats_new_string(const char *name, const char *desc, int ext)
212 if (ext && !Ext)
213 return (NULL); /* extended stats not enabled */
215 return (stats_new(name, desc, STATS_STRING));
218 void
219 stats_string_set(struct stats *sp, const char *s)
221 if (sp == NULL)
222 return;
224 ASSERT(sp->t == STATS_STRING);
226 sp->u.string = s;
230 * stats_publish -- spew all stats
234 void
235 stats_publish(void)
237 struct stats *sp;
239 for (sp = Statslist; sp; sp = sp->next)
240 switch (sp->t) {
241 case STATS_COUNTER:
242 out(O_OK, "%32s %13d %s", sp->name,
243 sp->u.counter, sp->desc);
244 break;
246 case STATS_ELAPSE:
247 if (sp->u.elapse.start && sp->u.elapse.stop) {
248 hrtime_t delta =
249 sp->u.elapse.stop - sp->u.elapse.start;
251 out(O_OK, "%32s %11lldns %s", sp->name,
252 delta, sp->desc);
254 break;
256 case STATS_STRING:
257 out(O_OK, "%32s %13s %s", sp->name, sp->u.string,
258 sp->desc);
259 break;
261 default:
262 out(O_DIE, "stats_publish: unknown type %d", sp->t);