1 /* 2 * CDDL HEADER START 3 * 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. 8 * 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. 13 * 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] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 * 26 * stats.c -- simple stats tracking table module 27 * 28 */ 29 30 #pragma ident "%Z%%M% %I% %E% SMI" 31 32 #include <sys/types.h> 33 #include <strings.h> 34 #include "stats.h" 35 #include "alloc.h" 36 #include "out.h" 37 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; 55 }; 56 57 static int Ext; /* true if extended stats are enabled */ 58 static struct stats *Statslist; 59 static struct stats *Laststats; 60 61 62 /* 63 * stats_init -- initialize the stats module 64 * 65 */ 66 67 void 68 stats_init(int ext) 69 { 70 Ext = ext; 71 } 72 73 void 74 stats_fini(void) 75 { 76 } 77 78 static struct stats * 79 stats_new(const char *name, const char *desc, enum stats_type t) 80 { 81 struct stats *ret = MALLOC(sizeof (*ret)); 82 83 bzero(ret, sizeof (*ret)); 84 ret->t = t; 85 ret->name = STRDUP(name); 86 ret->desc = STRDUP(desc); 87 88 if (Laststats == NULL) 89 Statslist = ret; 90 else 91 Laststats->next = ret; 92 Laststats = ret; 93 94 return (ret); 95 } 96 97 void 98 stats_delete(struct stats *sp) 99 { 100 struct stats *p, *s; 101 102 if (sp == NULL) 103 return; 104 105 for (p = NULL, s = Statslist; s != NULL; s = s->next) 106 if (s == sp) 107 break; 108 109 if (s == NULL) 110 return; 111 112 if (p == NULL) 113 Statslist = s->next; 114 else 115 p->next = s->next; 116 117 if (s == Laststats) 118 Laststats = p; 119 120 FREE((void *)sp->name); 121 FREE((void *)sp->desc); 122 FREE(sp); 123 } 124 125 struct stats * 126 stats_new_counter(const char *name, const char *desc, int ext) 127 { 128 if (ext && !Ext) 129 return (NULL); /* extended stats not enabled */ 130 131 return (stats_new(name, desc, STATS_COUNTER)); 132 } 133 134 void 135 stats_counter_bump(struct stats *sp) 136 { 137 if (sp == NULL) 138 return; 139 140 ASSERT(sp->t == STATS_COUNTER); 141 142 sp->u.counter++; 143 } 144 145 void 146 stats_counter_add(struct stats *sp, int n) 147 { 148 if (sp == NULL) 149 return; 150 151 ASSERT(sp->t == STATS_COUNTER); 152 153 sp->u.counter += n; 154 } 155 156 int 157 stats_counter_value(struct stats *sp) 158 { 159 if (sp == NULL) 160 return (0); 161 162 ASSERT(sp->t == STATS_COUNTER); 163 164 return (sp->u.counter); 165 } 166 167 struct stats * 168 stats_new_elapse(const char *name, const char *desc, int ext) 169 { 170 if (ext && !Ext) 171 return (NULL); /* extended stats not enabled */ 172 173 return (stats_new(name, desc, STATS_ELAPSE)); 174 } 175 176 void 177 stats_elapse_start(struct stats *sp) 178 { 179 if (sp == NULL) 180 return; 181 182 ASSERT(sp->t == STATS_ELAPSE); 183 184 sp->u.elapse.start = gethrtime(); 185 } 186 187 void 188 stats_elapse_stop(struct stats *sp) 189 { 190 if (sp == NULL) 191 return; 192 193 ASSERT(sp->t == STATS_ELAPSE); 194 195 sp->u.elapse.stop = gethrtime(); 196 } 197 198 struct stats * 199 stats_new_string(const char *name, const char *desc, int ext) 200 { 201 if (ext && !Ext) 202 return (NULL); /* extended stats not enabled */ 203 204 return (stats_new(name, desc, STATS_STRING)); 205 } 206 207 void 208 stats_string_set(struct stats *sp, const char *s) 209 { 210 if (sp == NULL) 211 return; 212 213 ASSERT(sp->t == STATS_STRING); 214 215 sp->u.string = s; 216 } 217 218 /* 219 * stats_publish -- spew all stats 220 * 221 */ 222 223 void 224 stats_publish(void) 225 { 226 struct stats *sp; 227 228 for (sp = Statslist; sp; sp = sp->next) 229 switch (sp->t) { 230 case STATS_COUNTER: 231 out(O_OK, "%32s %13d %s", sp->name, 232 sp->u.counter, sp->desc); 233 break; 234 235 case STATS_ELAPSE: 236 if (sp->u.elapse.start && sp->u.elapse.stop) { 237 hrtime_t delta = 238 sp->u.elapse.stop - sp->u.elapse.start; 239 240 out(O_OK, "%32s %11lldns %s", sp->name, 241 delta, sp->desc); 242 } 243 break; 244 245 case STATS_STRING: 246 out(O_OK, "%32s %13s %s", sp->name, sp->u.string, 247 sp->desc); 248 break; 249 250 default: 251 out(O_DIE, "stats_publish: unknown type %d", sp->t); 252 } 253 } 254