xref: /titanic_50/usr/src/uts/common/os/kstat_fr.c (revision 8ef10d209f1443865a8ce5b1eb59838b64a64e67)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 #pragma ident	"%Z%%M%	%I%	%E% SMI"
26 
27 /*
28  * Kernel statistics framework
29  */
30 
31 #include <sys/types.h>
32 #include <sys/time.h>
33 #include <sys/systm.h>
34 #include <sys/vmsystm.h>
35 #include <sys/t_lock.h>
36 #include <sys/param.h>
37 #include <sys/errno.h>
38 #include <sys/vmem.h>
39 #include <sys/sysmacros.h>
40 #include <sys/cmn_err.h>
41 #include <sys/kstat.h>
42 #include <sys/sysinfo.h>
43 #include <sys/cpuvar.h>
44 #include <sys/fcntl.h>
45 #include <sys/flock.h>
46 #include <sys/vnode.h>
47 #include <sys/vfs.h>
48 #include <sys/dnlc.h>
49 #include <sys/var.h>
50 #include <sys/vmmeter.h>
51 #include <sys/debug.h>
52 #include <sys/kobj.h>
53 #include <sys/avl.h>
54 #include <sys/pool_pset.h>
55 #include <sys/cpupart.h>
56 #include <sys/zone.h>
57 #include <sys/loadavg.h>
58 #include <vm/page.h>
59 #include <vm/anon.h>
60 #include <vm/seg_kmem.h>
61 
62 /*
63  * Global lock to protect the AVL trees and kstat_chain_id.
64  */
65 static kmutex_t kstat_chain_lock;
66 
67 /*
68  * Every install/delete kstat bumps kstat_chain_id.  This is used by:
69  *
70  * (1)	/dev/kstat, to detect changes in the kstat chain across ioctls;
71  *
72  * (2)	kstat_create(), to assign a KID (kstat ID) to each new kstat.
73  *	/dev/kstat uses the KID as a cookie for kstat lookups.
74  *
75  * We reserve the first two IDs because some kstats are created before
76  * the well-known ones (kstat_headers = 0, kstat_types = 1).
77  *
78  * We also bump the kstat_chain_id if a zone is gaining or losing visibility
79  * into a particular kstat, which is logically equivalent to a kstat being
80  * installed/deleted.
81  */
82 
83 kid_t kstat_chain_id = 2;
84 
85 /*
86  * As far as zones are concerned, there are 3 types of kstat:
87  *
88  * 1) Those which have a well-known name, and which should return per-zone data
89  * depending on which zone is doing the kstat_read().  sockfs:0:sock_unix_list
90  * is an example of this type of kstat.
91  *
92  * 2) Those which should only be exported to a particular list of zones.
93  * For example, in the case of nfs:*:mntinfo, we don't want zone A to be
94  * able to see NFS mounts associated with zone B, while we want the
95  * global zone to be able to see all mounts on the system.
96  *
97  * 3) Those that can be exported to all zones.  Most system-related
98  * kstats fall within this category.
99  *
100  * An ekstat_t thus contains a list of kstats that the zone is to be
101  * exported to.  The lookup of a name:instance:module thus translates to a
102  * lookup of name:instance:module:myzone; if the kstat is not exported
103  * to all zones, and does not have the caller's zoneid explicitly
104  * enumerated in the list of zones to be exported to, it is the same as
105  * if the kstat didn't exist.
106  *
107  * Writing to kstats is currently disallowed from within a non-global
108  * zone, although this restriction could be removed in the future.
109  */
110 typedef struct kstat_zone {
111 	zoneid_t zoneid;
112 	struct kstat_zone *next;
113 } kstat_zone_t;
114 
115 /*
116  * Extended kstat structure -- for internal use only.
117  */
118 typedef struct ekstat {
119 	kstat_t		e_ks;		/* the kstat itself */
120 	size_t		e_size;		/* total allocation size */
121 	kthread_t	*e_owner;	/* thread holding this kstat */
122 	kcondvar_t	e_cv;		/* wait for owner == NULL */
123 	avl_node_t	e_avl_bykid;	/* AVL tree to sort by KID */
124 	avl_node_t	e_avl_byname;	/* AVL tree to sort by name */
125 	kstat_zone_t	e_zone;		/* zone to export stats to */
126 } ekstat_t;
127 
128 static uint64_t kstat_initial[8192];
129 static void *kstat_initial_ptr = kstat_initial;
130 static size_t kstat_initial_avail = sizeof (kstat_initial);
131 static vmem_t *kstat_arena;
132 
133 #define	KSTAT_ALIGN	(sizeof (uint64_t))
134 
135 static avl_tree_t kstat_avl_bykid;
136 static avl_tree_t kstat_avl_byname;
137 
138 /*
139  * Various pointers we need to create kstats at boot time in kstat_init()
140  */
141 extern	kstat_named_t	*segmapcnt_ptr;
142 extern	uint_t		segmapcnt_ndata;
143 extern	int		segmap_kstat_update(kstat_t *, int);
144 extern	kstat_named_t	*biostats_ptr;
145 extern	uint_t		biostats_ndata;
146 extern	kstat_named_t	*pollstats_ptr;
147 extern	uint_t		pollstats_ndata;
148 
149 extern	int	vac;
150 extern	uint_t	nproc;
151 extern	time_t	boot_time;
152 extern	sysinfo_t	sysinfo;
153 extern	vminfo_t	vminfo;
154 
155 struct {
156 	kstat_named_t ncpus;
157 	kstat_named_t lbolt;
158 	kstat_named_t deficit;
159 	kstat_named_t clk_intr;
160 	kstat_named_t vac;
161 	kstat_named_t nproc;
162 	kstat_named_t avenrun_1min;
163 	kstat_named_t avenrun_5min;
164 	kstat_named_t avenrun_15min;
165 	kstat_named_t boot_time;
166 } system_misc_kstat = {
167 	{ "ncpus",		KSTAT_DATA_UINT32 },
168 	{ "lbolt",		KSTAT_DATA_UINT32 },
169 	{ "deficit",		KSTAT_DATA_UINT32 },
170 	{ "clk_intr",		KSTAT_DATA_UINT32 },
171 	{ "vac",		KSTAT_DATA_UINT32 },
172 	{ "nproc",		KSTAT_DATA_UINT32 },
173 	{ "avenrun_1min",	KSTAT_DATA_UINT32 },
174 	{ "avenrun_5min",	KSTAT_DATA_UINT32 },
175 	{ "avenrun_15min",	KSTAT_DATA_UINT32 },
176 	{ "boot_time",		KSTAT_DATA_UINT32 },
177 };
178 
179 struct {
180 	kstat_named_t physmem;
181 	kstat_named_t nalloc;
182 	kstat_named_t nfree;
183 	kstat_named_t nalloc_calls;
184 	kstat_named_t nfree_calls;
185 	kstat_named_t kernelbase;
186 	kstat_named_t econtig;
187 	kstat_named_t freemem;
188 	kstat_named_t availrmem;
189 	kstat_named_t lotsfree;
190 	kstat_named_t desfree;
191 	kstat_named_t minfree;
192 	kstat_named_t fastscan;
193 	kstat_named_t slowscan;
194 	kstat_named_t nscan;
195 	kstat_named_t desscan;
196 	kstat_named_t pp_kernel;
197 	kstat_named_t pagesfree;
198 	kstat_named_t pageslocked;
199 	kstat_named_t pagestotal;
200 } system_pages_kstat = {
201 	{ "physmem",		KSTAT_DATA_ULONG },
202 	{ "nalloc",		KSTAT_DATA_ULONG },
203 	{ "nfree",		KSTAT_DATA_ULONG },
204 	{ "nalloc_calls",	KSTAT_DATA_ULONG },
205 	{ "nfree_calls",	KSTAT_DATA_ULONG },
206 	{ "kernelbase",		KSTAT_DATA_ULONG },
207 	{ "econtig", 		KSTAT_DATA_ULONG },
208 	{ "freemem", 		KSTAT_DATA_ULONG },
209 	{ "availrmem", 		KSTAT_DATA_ULONG },
210 	{ "lotsfree", 		KSTAT_DATA_ULONG },
211 	{ "desfree", 		KSTAT_DATA_ULONG },
212 	{ "minfree", 		KSTAT_DATA_ULONG },
213 	{ "fastscan", 		KSTAT_DATA_ULONG },
214 	{ "slowscan", 		KSTAT_DATA_ULONG },
215 	{ "nscan", 		KSTAT_DATA_ULONG },
216 	{ "desscan", 		KSTAT_DATA_ULONG },
217 	{ "pp_kernel", 		KSTAT_DATA_ULONG },
218 	{ "pagesfree", 		KSTAT_DATA_ULONG },
219 	{ "pageslocked", 	KSTAT_DATA_ULONG },
220 	{ "pagestotal",		KSTAT_DATA_ULONG },
221 };
222 
223 static int header_kstat_update(kstat_t *, int);
224 static int header_kstat_snapshot(kstat_t *, void *, int);
225 static int system_misc_kstat_update(kstat_t *, int);
226 static int system_pages_kstat_update(kstat_t *, int);
227 
228 static struct {
229 	char	name[KSTAT_STRLEN];
230 	size_t	size;
231 	uint_t	min_ndata;
232 	uint_t	max_ndata;
233 } kstat_data_type[KSTAT_NUM_TYPES] = {
234 	{ "raw",		1,			0,	INT_MAX	},
235 	{ "name=value",		sizeof (kstat_named_t),	0,	INT_MAX	},
236 	{ "interrupt",		sizeof (kstat_intr_t),	1,	1	},
237 	{ "i/o",		sizeof (kstat_io_t),	1,	1	},
238 	{ "event_timer",	sizeof (kstat_timer_t),	0,	INT_MAX	},
239 };
240 
241 int
242 kstat_zone_find(kstat_t *k, zoneid_t zoneid)
243 {
244 	ekstat_t *e = (ekstat_t *)k;
245 	kstat_zone_t *kz;
246 
247 	ASSERT(MUTEX_HELD(&kstat_chain_lock));
248 	for (kz = &e->e_zone; kz != NULL; kz = kz->next) {
249 		if (zoneid == ALL_ZONES || kz->zoneid == ALL_ZONES)
250 			return (1);
251 		if (zoneid == kz->zoneid)
252 			return (1);
253 	}
254 	return (0);
255 }
256 
257 void
258 kstat_zone_remove(kstat_t *k, zoneid_t zoneid)
259 {
260 	ekstat_t *e = (ekstat_t *)k;
261 	kstat_zone_t *kz, *t = NULL;
262 
263 	mutex_enter(&kstat_chain_lock);
264 	if (zoneid == e->e_zone.zoneid) {
265 		kz = e->e_zone.next;
266 		ASSERT(kz != NULL);
267 		e->e_zone.zoneid = kz->zoneid;
268 		e->e_zone.next = kz->next;
269 		goto out;
270 	}
271 	for (kz = &e->e_zone; kz->next != NULL; kz = kz->next) {
272 		if (kz->next->zoneid == zoneid) {
273 			t = kz->next;
274 			kz->next = t->next;
275 			break;
276 		}
277 	}
278 	ASSERT(t != NULL);	/* we removed something */
279 	kz = t;
280 out:
281 	kstat_chain_id++;
282 	mutex_exit(&kstat_chain_lock);
283 	kmem_free(kz, sizeof (*kz));
284 }
285 
286 void
287 kstat_zone_add(kstat_t *k, zoneid_t zoneid)
288 {
289 	ekstat_t *e = (ekstat_t *)k;
290 	kstat_zone_t *kz;
291 
292 	kz = kmem_alloc(sizeof (*kz), KM_NOSLEEP);
293 	if (kz == NULL)
294 		return;
295 	mutex_enter(&kstat_chain_lock);
296 	kz->zoneid = zoneid;
297 	kz->next = e->e_zone.next;
298 	e->e_zone.next = kz;
299 	kstat_chain_id++;
300 	mutex_exit(&kstat_chain_lock);
301 }
302 
303 /*
304  * Compare the list of zones for the given kstats, returning 0 if they match
305  * (ie, one list contains ALL_ZONES or both lists contain the same zoneid).
306  * In practice, this is called indirectly by kstat_hold_byname(), so one of the
307  * two lists always has one element, and this is an O(n) operation rather than
308  * O(n^2).
309  */
310 static int
311 kstat_zone_compare(ekstat_t *e1, ekstat_t *e2)
312 {
313 	kstat_zone_t *kz1, *kz2;
314 
315 	ASSERT(MUTEX_HELD(&kstat_chain_lock));
316 	for (kz1 = &e1->e_zone; kz1 != NULL; kz1 = kz1->next) {
317 		for (kz2 = &e2->e_zone; kz2 != NULL; kz2 = kz2->next) {
318 			if (kz1->zoneid == ALL_ZONES ||
319 			    kz2->zoneid == ALL_ZONES)
320 				return (0);
321 			if (kz1->zoneid == kz2->zoneid)
322 				return (0);
323 		}
324 	}
325 	return (e1->e_zone.zoneid < e2->e_zone.zoneid ? -1 : 1);
326 }
327 
328 /*
329  * Support for keeping kstats sorted in AVL trees for fast lookups.
330  */
331 static int
332 kstat_compare_bykid(const void *a1, const void *a2)
333 {
334 	const kstat_t *k1 = a1;
335 	const kstat_t *k2 = a2;
336 
337 	if (k1->ks_kid < k2->ks_kid)
338 		return (-1);
339 	if (k1->ks_kid > k2->ks_kid)
340 		return (1);
341 	return (kstat_zone_compare((ekstat_t *)k1, (ekstat_t *)k2));
342 }
343 
344 static int
345 kstat_compare_byname(const void *a1, const void *a2)
346 {
347 	const kstat_t *k1 = a1;
348 	const kstat_t *k2 = a2;
349 	int s;
350 
351 	s = strcmp(k1->ks_module, k2->ks_module);
352 	if (s > 0)
353 		return (1);
354 	if (s < 0)
355 		return (-1);
356 
357 	if (k1->ks_instance < k2->ks_instance)
358 		return (-1);
359 	if (k1->ks_instance > k2->ks_instance)
360 		return (1);
361 
362 	s = strcmp(k1->ks_name, k2->ks_name);
363 	if (s > 0)
364 		return (1);
365 	if (s < 0)
366 		return (-1);
367 
368 	return (kstat_zone_compare((ekstat_t *)k1, (ekstat_t *)k2));
369 }
370 
371 static kstat_t *
372 kstat_hold(avl_tree_t *t, ekstat_t *template)
373 {
374 	kstat_t *ksp;
375 	ekstat_t *e;
376 
377 	mutex_enter(&kstat_chain_lock);
378 	for (;;) {
379 		ksp = avl_find(t, template, NULL);
380 		if (ksp == NULL)
381 			break;
382 		e = (ekstat_t *)ksp;
383 		if (e->e_owner == NULL) {
384 			e->e_owner = curthread;
385 			break;
386 		}
387 		cv_wait(&e->e_cv, &kstat_chain_lock);
388 	}
389 	mutex_exit(&kstat_chain_lock);
390 	return (ksp);
391 }
392 
393 void
394 kstat_rele(kstat_t *ksp)
395 {
396 	ekstat_t *e = (ekstat_t *)ksp;
397 
398 	mutex_enter(&kstat_chain_lock);
399 	ASSERT(e->e_owner == curthread);
400 	e->e_owner = NULL;
401 	cv_broadcast(&e->e_cv);
402 	mutex_exit(&kstat_chain_lock);
403 }
404 
405 kstat_t *
406 kstat_hold_bykid(kid_t kid, zoneid_t zoneid)
407 {
408 	ekstat_t e;
409 
410 	e.e_ks.ks_kid = kid;
411 	e.e_zone.zoneid = zoneid;
412 	e.e_zone.next = NULL;
413 
414 	return (kstat_hold(&kstat_avl_bykid, &e));
415 }
416 
417 kstat_t *
418 kstat_hold_byname(const char *ks_module, int ks_instance, const char *ks_name,
419     zoneid_t ks_zoneid)
420 {
421 	ekstat_t e;
422 
423 	kstat_set_string(e.e_ks.ks_module, ks_module);
424 	e.e_ks.ks_instance = ks_instance;
425 	kstat_set_string(e.e_ks.ks_name, ks_name);
426 	e.e_zone.zoneid = ks_zoneid;
427 	e.e_zone.next = NULL;
428 	return (kstat_hold(&kstat_avl_byname, &e));
429 }
430 
431 static ekstat_t *
432 kstat_alloc(size_t size)
433 {
434 	ekstat_t *e = NULL;
435 
436 	size = P2ROUNDUP(sizeof (ekstat_t) + size, KSTAT_ALIGN);
437 
438 	if (kstat_arena == NULL) {
439 		if (size <= kstat_initial_avail) {
440 			e = kstat_initial_ptr;
441 			kstat_initial_ptr = (char *)kstat_initial_ptr + size;
442 			kstat_initial_avail -= size;
443 		}
444 	} else {
445 		e = vmem_alloc(kstat_arena, size, VM_NOSLEEP);
446 	}
447 
448 	if (e != NULL) {
449 		bzero(e, size);
450 		e->e_size = size;
451 		cv_init(&e->e_cv, NULL, CV_DEFAULT, NULL);
452 	}
453 
454 	return (e);
455 }
456 
457 static void
458 kstat_free(ekstat_t *e)
459 {
460 	cv_destroy(&e->e_cv);
461 	vmem_free(kstat_arena, e, e->e_size);
462 }
463 
464 /*
465  * Create various system kstats.
466  */
467 void
468 kstat_init(void)
469 {
470 	kstat_t *ksp;
471 	ekstat_t *e;
472 	avl_tree_t *t = &kstat_avl_bykid;
473 
474 	/*
475 	 * Set up the kstat vmem arena.
476 	 */
477 	kstat_arena = vmem_create("kstat",
478 	    kstat_initial, sizeof (kstat_initial), KSTAT_ALIGN,
479 	    segkmem_alloc, segkmem_free, heap_arena, 0, VM_SLEEP);
480 
481 	/*
482 	 * Make initial kstats appear as though they were allocated.
483 	 */
484 	for (e = avl_first(t); e != NULL; e = avl_walk(t, e, AVL_AFTER))
485 		(void) vmem_xalloc(kstat_arena, e->e_size, KSTAT_ALIGN,
486 		    0, 0, e, (char *)e + e->e_size,
487 		    VM_NOSLEEP | VM_BESTFIT | VM_PANIC);
488 
489 	/*
490 	 * The mother of all kstats.  The first kstat in the system, which
491 	 * always has KID 0, has the headers for all kstats (including itself)
492 	 * as its data.  Thus, the kstat driver does not need any special
493 	 * interface to extract the kstat chain.
494 	 */
495 	kstat_chain_id = 0;
496 	ksp = kstat_create("unix", 0, "kstat_headers", "kstat", KSTAT_TYPE_RAW,
497 		0, KSTAT_FLAG_VIRTUAL | KSTAT_FLAG_VAR_SIZE);
498 	if (ksp) {
499 		ksp->ks_lock = &kstat_chain_lock;
500 		ksp->ks_update = header_kstat_update;
501 		ksp->ks_snapshot = header_kstat_snapshot;
502 		kstat_install(ksp);
503 	} else {
504 		panic("cannot create kstat 'kstat_headers'");
505 	}
506 
507 	ksp = kstat_create("unix", 0, "kstat_types", "kstat",
508 		KSTAT_TYPE_NAMED, KSTAT_NUM_TYPES, 0);
509 	if (ksp) {
510 		int i;
511 		kstat_named_t *kn = KSTAT_NAMED_PTR(ksp);
512 
513 		for (i = 0; i < KSTAT_NUM_TYPES; i++) {
514 			kstat_named_init(&kn[i], kstat_data_type[i].name,
515 				KSTAT_DATA_ULONG);
516 			kn[i].value.ul = i;
517 		}
518 		kstat_install(ksp);
519 	}
520 
521 	ksp = kstat_create("unix", 0, "sysinfo", "misc", KSTAT_TYPE_RAW,
522 		sizeof (sysinfo_t), KSTAT_FLAG_VIRTUAL);
523 	if (ksp) {
524 		ksp->ks_data = (void *) &sysinfo;
525 		kstat_install(ksp);
526 	}
527 
528 	ksp = kstat_create("unix", 0, "vminfo", "vm", KSTAT_TYPE_RAW,
529 		sizeof (vminfo_t), KSTAT_FLAG_VIRTUAL);
530 	if (ksp) {
531 		ksp->ks_data = (void *) &vminfo;
532 		kstat_install(ksp);
533 	}
534 
535 	ksp = kstat_create("unix", 0, "segmap", "vm", KSTAT_TYPE_NAMED,
536 		segmapcnt_ndata, KSTAT_FLAG_VIRTUAL);
537 	if (ksp) {
538 		ksp->ks_data = (void *) segmapcnt_ptr;
539 		ksp->ks_update = segmap_kstat_update;
540 		kstat_install(ksp);
541 	}
542 
543 	ksp = kstat_create("unix", 0, "biostats", "misc", KSTAT_TYPE_NAMED,
544 		biostats_ndata, KSTAT_FLAG_VIRTUAL);
545 	if (ksp) {
546 		ksp->ks_data = (void *) biostats_ptr;
547 		kstat_install(ksp);
548 	}
549 
550 #ifdef VAC
551 	ksp = kstat_create("unix", 0, "flushmeter", "hat", KSTAT_TYPE_RAW,
552 		sizeof (struct flushmeter), KSTAT_FLAG_VIRTUAL);
553 	if (ksp) {
554 		ksp->ks_data = (void *) &flush_cnt;
555 		kstat_install(ksp);
556 	}
557 #endif	/* VAC */
558 
559 	ksp = kstat_create("unix", 0, "var", "misc", KSTAT_TYPE_RAW,
560 		sizeof (struct var), KSTAT_FLAG_VIRTUAL);
561 	if (ksp) {
562 		ksp->ks_data = (void *) &v;
563 		kstat_install(ksp);
564 	}
565 
566 	ksp = kstat_create("unix", 0, "system_misc", "misc", KSTAT_TYPE_NAMED,
567 		sizeof (system_misc_kstat) / sizeof (kstat_named_t),
568 		KSTAT_FLAG_VIRTUAL);
569 	if (ksp) {
570 		ksp->ks_data = (void *) &system_misc_kstat;
571 		ksp->ks_update = system_misc_kstat_update;
572 		kstat_install(ksp);
573 	}
574 
575 	ksp = kstat_create("unix", 0, "system_pages", "pages", KSTAT_TYPE_NAMED,
576 		sizeof (system_pages_kstat) / sizeof (kstat_named_t),
577 		KSTAT_FLAG_VIRTUAL);
578 	if (ksp) {
579 		ksp->ks_data = (void *) &system_pages_kstat;
580 		ksp->ks_update = system_pages_kstat_update;
581 		kstat_install(ksp);
582 	}
583 
584 	ksp = kstat_create("poll", 0, "pollstats", "misc", KSTAT_TYPE_NAMED,
585 	    pollstats_ndata, KSTAT_FLAG_VIRTUAL | KSTAT_FLAG_WRITABLE);
586 
587 	if (ksp) {
588 		ksp->ks_data = pollstats_ptr;
589 		kstat_install(ksp);
590 	}
591 }
592 
593 /*
594  * Caller of this should ensure that the string pointed by src
595  * doesn't change while kstat's lock is held. Not doing so defeats
596  * kstat's snapshot strategy as explained in <sys/kstat.h>
597  */
598 void
599 kstat_named_setstr(kstat_named_t *knp, const char *src)
600 {
601 	if (knp->data_type != KSTAT_DATA_STRING)
602 		panic("kstat_named_setstr('%p', '%p'): "
603 		    "named kstat is not of type KSTAT_DATA_STRING", knp, src);
604 
605 	KSTAT_NAMED_STR_PTR(knp) = (char *)src;
606 	if (src != NULL)
607 		KSTAT_NAMED_STR_BUFLEN(knp) = strlen(src) + 1;
608 	else
609 		KSTAT_NAMED_STR_BUFLEN(knp) = 0;
610 }
611 
612 void
613 kstat_set_string(char *dst, const char *src)
614 {
615 	bzero(dst, KSTAT_STRLEN);
616 	(void) strncpy(dst, src, KSTAT_STRLEN - 1);
617 }
618 
619 void
620 kstat_named_init(kstat_named_t *knp, const char *name, uchar_t data_type)
621 {
622 	kstat_set_string(knp->name, name);
623 	knp->data_type = data_type;
624 
625 	if (data_type == KSTAT_DATA_STRING)
626 		kstat_named_setstr(knp, NULL);
627 }
628 
629 void
630 kstat_timer_init(kstat_timer_t *ktp, const char *name)
631 {
632 	kstat_set_string(ktp->name, name);
633 }
634 
635 /* ARGSUSED */
636 static int
637 default_kstat_update(kstat_t *ksp, int rw)
638 {
639 	uint_t i;
640 	size_t len = 0;
641 	kstat_named_t *knp;
642 
643 	/*
644 	 * Named kstats with variable-length long strings have a standard
645 	 * way of determining how much space is needed to hold the snapshot:
646 	 */
647 	if (ksp->ks_data != NULL && ksp->ks_type == KSTAT_TYPE_NAMED &&
648 	    (ksp->ks_flags & KSTAT_FLAG_VAR_SIZE)) {
649 
650 		/*
651 		 * Add in the space required for the strings
652 		 */
653 		knp = KSTAT_NAMED_PTR(ksp);
654 		for (i = 0; i < ksp->ks_ndata; i++, knp++) {
655 			if (knp->data_type == KSTAT_DATA_STRING)
656 				len += KSTAT_NAMED_STR_BUFLEN(knp);
657 		}
658 		ksp->ks_data_size =
659 		    ksp->ks_ndata * sizeof (kstat_named_t) + len;
660 	}
661 	return (0);
662 }
663 
664 static int
665 default_kstat_snapshot(kstat_t *ksp, void *buf, int rw)
666 {
667 	kstat_io_t *kiop;
668 	hrtime_t cur_time;
669 	size_t	namedsz;
670 
671 	ksp->ks_snaptime = cur_time = gethrtime();
672 
673 	if (rw == KSTAT_WRITE) {
674 		if (!(ksp->ks_flags & KSTAT_FLAG_WRITABLE))
675 			return (EACCES);
676 		bcopy(buf, ksp->ks_data, ksp->ks_data_size);
677 		return (0);
678 	}
679 
680 	/*
681 	 * KSTAT_TYPE_NAMED kstats are defined to have ks_ndata
682 	 * number of kstat_named_t structures, followed by an optional
683 	 * string segment. The ks_data generally holds only the
684 	 * kstat_named_t structures. So we copy it first. The strings,
685 	 * if any, are copied below. For other kstat types, ks_data holds the
686 	 * entire buffer.
687 	 */
688 
689 	namedsz = sizeof (kstat_named_t) * ksp->ks_ndata;
690 	if (ksp->ks_type == KSTAT_TYPE_NAMED && ksp->ks_data_size > namedsz)
691 		bcopy(ksp->ks_data, buf, namedsz);
692 	else
693 		bcopy(ksp->ks_data, buf, ksp->ks_data_size);
694 
695 	/*
696 	 * Apply kstat type-specific data massaging
697 	 */
698 	switch (ksp->ks_type) {
699 
700 	case KSTAT_TYPE_IO:
701 		/*
702 		 * Normalize time units and deal with incomplete transactions
703 		 */
704 		kiop = (kstat_io_t *)buf;
705 
706 		scalehrtime(&kiop->wtime);
707 		scalehrtime(&kiop->wlentime);
708 		scalehrtime(&kiop->wlastupdate);
709 		scalehrtime(&kiop->rtime);
710 		scalehrtime(&kiop->rlentime);
711 		scalehrtime(&kiop->rlastupdate);
712 
713 		if (kiop->wcnt != 0) {
714 			/* like kstat_waitq_exit */
715 			hrtime_t wfix = cur_time - kiop->wlastupdate;
716 			kiop->wlastupdate = cur_time;
717 			kiop->wlentime += kiop->wcnt * wfix;
718 			kiop->wtime += wfix;
719 		}
720 
721 		if (kiop->rcnt != 0) {
722 			/* like kstat_runq_exit */
723 			hrtime_t rfix = cur_time - kiop->rlastupdate;
724 			kiop->rlastupdate = cur_time;
725 			kiop->rlentime += kiop->rcnt * rfix;
726 			kiop->rtime += rfix;
727 		}
728 		break;
729 
730 	case KSTAT_TYPE_NAMED:
731 		/*
732 		 * Massage any long strings in at the end of the buffer
733 		 */
734 		if (ksp->ks_data_size > namedsz) {
735 			uint_t i;
736 			kstat_named_t *knp = buf;
737 			char *dst = (char *)(knp + ksp->ks_ndata);
738 			/*
739 			 * Copy strings and update pointers
740 			 */
741 			for (i = 0; i < ksp->ks_ndata; i++, knp++) {
742 				if (knp->data_type == KSTAT_DATA_STRING &&
743 				    KSTAT_NAMED_STR_PTR(knp) != NULL) {
744 					bcopy(KSTAT_NAMED_STR_PTR(knp), dst,
745 					    KSTAT_NAMED_STR_BUFLEN(knp));
746 					KSTAT_NAMED_STR_PTR(knp) = dst;
747 					dst += KSTAT_NAMED_STR_BUFLEN(knp);
748 				}
749 			}
750 			ASSERT(dst <= ((char *)buf + ksp->ks_data_size));
751 		}
752 		break;
753 	}
754 	return (0);
755 }
756 
757 static int
758 header_kstat_update(kstat_t *header_ksp, int rw)
759 {
760 	int nkstats = 0;
761 	ekstat_t *e;
762 	avl_tree_t *t = &kstat_avl_bykid;
763 	zoneid_t zoneid;
764 
765 	if (rw == KSTAT_WRITE)
766 		return (EACCES);
767 
768 	ASSERT(MUTEX_HELD(&kstat_chain_lock));
769 
770 	zoneid = getzoneid();
771 	for (e = avl_first(t); e != NULL; e = avl_walk(t, e, AVL_AFTER)) {
772 		if (kstat_zone_find((kstat_t *)e, zoneid)) {
773 			nkstats++;
774 		}
775 	}
776 	header_ksp->ks_ndata = nkstats;
777 	header_ksp->ks_data_size = nkstats * sizeof (kstat_t);
778 	return (0);
779 }
780 
781 /*
782  * Copy out the data section of kstat 0, which consists of the list
783  * of all kstat headers.  By specification, these headers must be
784  * copied out in order of increasing KID.
785  */
786 static int
787 header_kstat_snapshot(kstat_t *header_ksp, void *buf, int rw)
788 {
789 	ekstat_t *e;
790 	avl_tree_t *t = &kstat_avl_bykid;
791 	zoneid_t zoneid;
792 
793 	header_ksp->ks_snaptime = gethrtime();
794 
795 	if (rw == KSTAT_WRITE)
796 		return (EACCES);
797 
798 	ASSERT(MUTEX_HELD(&kstat_chain_lock));
799 
800 	zoneid = getzoneid();
801 	for (e = avl_first(t); e != NULL; e = avl_walk(t, e, AVL_AFTER)) {
802 		if (kstat_zone_find((kstat_t *)e, zoneid)) {
803 			bcopy(&e->e_ks, buf, sizeof (kstat_t));
804 			buf = (char *)buf + sizeof (kstat_t);
805 		}
806 	}
807 
808 	return (0);
809 }
810 
811 /* ARGSUSED */
812 static int
813 system_misc_kstat_update(kstat_t *ksp, int rw)
814 {
815 	int myncpus = ncpus;
816 	int *loadavgp = &avenrun[0];
817 	int loadavg[LOADAVG_NSTATS];
818 
819 	if (rw == KSTAT_WRITE)
820 		return (EACCES);
821 
822 	if (!INGLOBALZONE(curproc)) {
823 		/*
824 		 * Here we grab cpu_lock which is OK as long as no-one in the
825 		 * future attempts to lookup this particular kstat
826 		 * (unix:0:system_misc) while holding cpu_lock.
827 		 */
828 		mutex_enter(&cpu_lock);
829 		if (pool_pset_enabled()) {
830 			psetid_t mypsid = zone_pset_get(curproc->p_zone);
831 			int error;
832 
833 			myncpus = zone_ncpus_get(curproc->p_zone);
834 			ASSERT(myncpus > 0);
835 			error = cpupart_get_loadavg(mypsid, &loadavg[0],
836 			    LOADAVG_NSTATS);
837 			ASSERT(error == 0);
838 			loadavgp = &loadavg[0];
839 		}
840 		mutex_exit(&cpu_lock);
841 	}
842 
843 	system_misc_kstat.ncpus.value.ui32		= (uint32_t)myncpus;
844 	system_misc_kstat.lbolt.value.ui32		= (uint32_t)lbolt;
845 	system_misc_kstat.deficit.value.ui32		= (uint32_t)deficit;
846 	system_misc_kstat.clk_intr.value.ui32		= (uint32_t)lbolt;
847 	system_misc_kstat.vac.value.ui32		= (uint32_t)vac;
848 	system_misc_kstat.nproc.value.ui32		= (uint32_t)nproc;
849 	system_misc_kstat.avenrun_1min.value.ui32	= (uint32_t)loadavgp[0];
850 	system_misc_kstat.avenrun_5min.value.ui32	= (uint32_t)loadavgp[1];
851 	system_misc_kstat.avenrun_15min.value.ui32	= (uint32_t)loadavgp[2];
852 	system_misc_kstat.boot_time.value.ui32		= (uint32_t)boot_time;
853 	return (0);
854 }
855 
856 #ifdef	__sparc
857 extern caddr_t	econtig32;
858 #else	/* !__sparc */
859 extern caddr_t	econtig;
860 #endif	/* __sparc */
861 
862 extern struct vnode kvp;
863 
864 /* ARGSUSED */
865 static int
866 system_pages_kstat_update(kstat_t *ksp, int rw)
867 {
868 	kobj_stat_t kobj_stat;
869 
870 	if (rw == KSTAT_WRITE) {
871 		return (EACCES);
872 	}
873 
874 	kobj_stat_get(&kobj_stat);
875 	system_pages_kstat.physmem.value.ul	= (ulong_t)physmem;
876 	system_pages_kstat.nalloc.value.ul	= kobj_stat.nalloc;
877 	system_pages_kstat.nfree.value.ul	= kobj_stat.nfree;
878 	system_pages_kstat.nalloc_calls.value.ul = kobj_stat.nalloc_calls;
879 	system_pages_kstat.nfree_calls.value.ul	= kobj_stat.nfree_calls;
880 	system_pages_kstat.kernelbase.value.ul	= (ulong_t)KERNELBASE;
881 
882 #ifdef	__sparc
883 	/*
884 	 * kstat should REALLY be modified to also report kmem64_base and
885 	 * kmem64_end (see sun4u/os/startup.c), as the virtual address range
886 	 * [ kernelbase .. econtig ] no longer is truly reflective of the
887 	 * kernel's vallocs...
888 	 */
889 	system_pages_kstat.econtig.value.ul	= (ulong_t)econtig32;
890 #else	/* !__sparc */
891 	system_pages_kstat.econtig.value.ul	= (ulong_t)econtig;
892 #endif	/* __sparc */
893 
894 	system_pages_kstat.freemem.value.ul	= (ulong_t)freemem;
895 	system_pages_kstat.availrmem.value.ul	= (ulong_t)availrmem;
896 	system_pages_kstat.lotsfree.value.ul	= (ulong_t)lotsfree;
897 	system_pages_kstat.desfree.value.ul	= (ulong_t)desfree;
898 	system_pages_kstat.minfree.value.ul	= (ulong_t)minfree;
899 	system_pages_kstat.fastscan.value.ul	= (ulong_t)fastscan;
900 	system_pages_kstat.slowscan.value.ul	= (ulong_t)slowscan;
901 	system_pages_kstat.nscan.value.ul	= (ulong_t)nscan;
902 	system_pages_kstat.desscan.value.ul	= (ulong_t)desscan;
903 	system_pages_kstat.pagesfree.value.ul	= (ulong_t)freemem;
904 	system_pages_kstat.pageslocked.value.ul	= (ulong_t)(availrmem_initial -
905 	    availrmem);
906 	system_pages_kstat.pagestotal.value.ul	= (ulong_t)total_pages;
907 	/*
908 	 * pp_kernel represents total pages used by the kernel since the
909 	 * startup. This formula takes into account the boottime kernel
910 	 * footprint and also considers the availrmem changes because of
911 	 * user explicit page locking.
912 	 */
913 	system_pages_kstat.pp_kernel.value.ul   = (ulong_t)(physinstalled -
914 		obp_pages - availrmem - k_anoninfo.ani_mem_resv -
915 		anon_segkp_pages_locked - segvn_pages_locked -
916 		pages_locked - pages_claimed - pages_useclaim);
917 
918 	return (0);
919 }
920 
921 kstat_t *
922 kstat_create(const char *ks_module, int ks_instance, const char *ks_name,
923     const char *ks_class, uchar_t ks_type, uint_t ks_ndata, uchar_t ks_flags)
924 {
925 	return (kstat_create_zone(ks_module, ks_instance, ks_name, ks_class,
926 		    ks_type, ks_ndata, ks_flags, ALL_ZONES));
927 }
928 
929 /*
930  * Allocate and initialize a kstat structure.  Or, if a dormant kstat with
931  * the specified name exists, reactivate it.  Returns a pointer to the kstat
932  * on success, NULL on failure.  The kstat will not be visible to the
933  * kstat driver until kstat_install().
934  */
935 kstat_t *
936 kstat_create_zone(const char *ks_module, int ks_instance, const char *ks_name,
937     const char *ks_class, uchar_t ks_type, uint_t ks_ndata, uchar_t ks_flags,
938     zoneid_t ks_zoneid)
939 {
940 	size_t ks_data_size;
941 	kstat_t *ksp;
942 	ekstat_t *e;
943 	avl_index_t where;
944 	char namebuf[KSTAT_STRLEN + 16];
945 
946 	if (avl_numnodes(&kstat_avl_bykid) == 0) {
947 		avl_create(&kstat_avl_bykid, kstat_compare_bykid,
948 		    sizeof (ekstat_t), offsetof(struct ekstat, e_avl_bykid));
949 
950 		avl_create(&kstat_avl_byname, kstat_compare_byname,
951 		    sizeof (ekstat_t), offsetof(struct ekstat, e_avl_byname));
952 	}
953 
954 	/*
955 	 * If ks_name == NULL, set the ks_name to <module><instance>.
956 	 */
957 	if (ks_name == NULL) {
958 		char buf[KSTAT_STRLEN];
959 		kstat_set_string(buf, ks_module);
960 		(void) sprintf(namebuf, "%s%d", buf, ks_instance);
961 		ks_name = namebuf;
962 	}
963 
964 	/*
965 	 * Make sure it's a valid kstat data type
966 	 */
967 	if (ks_type >= KSTAT_NUM_TYPES) {
968 		cmn_err(CE_WARN, "kstat_create('%s', %d, '%s'): "
969 			"invalid kstat type %d",
970 			ks_module, ks_instance, ks_name, ks_type);
971 		return (NULL);
972 	}
973 
974 	/*
975 	 * Don't allow persistent virtual kstats -- it makes no sense.
976 	 * ks_data points to garbage when the client goes away.
977 	 */
978 	if ((ks_flags & KSTAT_FLAG_PERSISTENT) &&
979 	    (ks_flags & KSTAT_FLAG_VIRTUAL)) {
980 		cmn_err(CE_WARN, "kstat_create('%s', %d, '%s'): "
981 			"cannot create persistent virtual kstat",
982 			ks_module, ks_instance, ks_name);
983 		return (NULL);
984 	}
985 
986 	/*
987 	 * Don't allow variable-size physical kstats, since the framework's
988 	 * memory allocation for physical kstat data is fixed at creation time.
989 	 */
990 	if ((ks_flags & KSTAT_FLAG_VAR_SIZE) &&
991 	    !(ks_flags & KSTAT_FLAG_VIRTUAL)) {
992 		cmn_err(CE_WARN, "kstat_create('%s', %d, '%s'): "
993 			"cannot create variable-size physical kstat",
994 			ks_module, ks_instance, ks_name);
995 		return (NULL);
996 	}
997 
998 	/*
999 	 * Make sure the number of data fields is within legal range
1000 	 */
1001 	if (ks_ndata < kstat_data_type[ks_type].min_ndata ||
1002 	    ks_ndata > kstat_data_type[ks_type].max_ndata) {
1003 		cmn_err(CE_WARN, "kstat_create('%s', %d, '%s'): "
1004 			"ks_ndata=%d out of range [%d, %d]",
1005 			ks_module, ks_instance, ks_name, (int)ks_ndata,
1006 			kstat_data_type[ks_type].min_ndata,
1007 			kstat_data_type[ks_type].max_ndata);
1008 		return (NULL);
1009 	}
1010 
1011 	ks_data_size = kstat_data_type[ks_type].size * ks_ndata;
1012 
1013 	/*
1014 	 * If the named kstat already exists and is dormant, reactivate it.
1015 	 */
1016 	ksp = kstat_hold_byname(ks_module, ks_instance, ks_name, ks_zoneid);
1017 	if (ksp != NULL) {
1018 		if (!(ksp->ks_flags & KSTAT_FLAG_DORMANT)) {
1019 			/*
1020 			 * The named kstat exists but is not dormant --
1021 			 * this is a kstat namespace collision.
1022 			 */
1023 			kstat_rele(ksp);
1024 			cmn_err(CE_WARN,
1025 			    "kstat_create('%s', %d, '%s'): namespace collision",
1026 			    ks_module, ks_instance, ks_name);
1027 			return (NULL);
1028 		}
1029 		if ((strcmp(ksp->ks_class, ks_class) != 0) ||
1030 		    (ksp->ks_type != ks_type) ||
1031 		    (ksp->ks_ndata != ks_ndata) ||
1032 		    (ks_flags & KSTAT_FLAG_VIRTUAL)) {
1033 			/*
1034 			 * The name is the same, but the other key parameters
1035 			 * differ from those of the dormant kstat -- bogus.
1036 			 */
1037 			kstat_rele(ksp);
1038 			cmn_err(CE_WARN, "kstat_create('%s', %d, '%s'): "
1039 				"invalid reactivation of dormant kstat",
1040 				ks_module, ks_instance, ks_name);
1041 			return (NULL);
1042 		}
1043 		/*
1044 		 * Return dormant kstat pointer to caller.  As usual,
1045 		 * the kstat is marked invalid until kstat_install().
1046 		 */
1047 		ksp->ks_flags |= KSTAT_FLAG_INVALID;
1048 		kstat_rele(ksp);
1049 		return (ksp);
1050 	}
1051 
1052 	/*
1053 	 * Allocate memory for the new kstat header and, if this is a physical
1054 	 * kstat, the data section.
1055 	 */
1056 	e = kstat_alloc(ks_flags & KSTAT_FLAG_VIRTUAL ? 0 : ks_data_size);
1057 	if (e == NULL) {
1058 		cmn_err(CE_NOTE, "kstat_create('%s', %d, '%s'): "
1059 			"insufficient kernel memory",
1060 			ks_module, ks_instance, ks_name);
1061 		return (NULL);
1062 	}
1063 
1064 	/*
1065 	 * Initialize as many fields as we can.  The caller may reset
1066 	 * ks_lock, ks_update, ks_private, and ks_snapshot as necessary.
1067 	 * Creators of virtual kstats may also reset ks_data.  It is
1068 	 * also up to the caller to initialize the kstat data section,
1069 	 * if necessary.  All initialization must be complete before
1070 	 * calling kstat_install().
1071 	 */
1072 	e->e_zone.zoneid = ks_zoneid;
1073 	e->e_zone.next = NULL;
1074 
1075 	ksp = &e->e_ks;
1076 	ksp->ks_crtime		= gethrtime();
1077 	kstat_set_string(ksp->ks_module, ks_module);
1078 	ksp->ks_instance	= ks_instance;
1079 	kstat_set_string(ksp->ks_name, ks_name);
1080 	ksp->ks_type		= ks_type;
1081 	kstat_set_string(ksp->ks_class, ks_class);
1082 	ksp->ks_flags		= ks_flags | KSTAT_FLAG_INVALID;
1083 	if (ks_flags & KSTAT_FLAG_VIRTUAL)
1084 		ksp->ks_data	= NULL;
1085 	else
1086 		ksp->ks_data	= (void *)(e + 1);
1087 	ksp->ks_ndata		= ks_ndata;
1088 	ksp->ks_data_size	= ks_data_size;
1089 	ksp->ks_snaptime	= ksp->ks_crtime;
1090 	ksp->ks_update		= default_kstat_update;
1091 	ksp->ks_private		= NULL;
1092 	ksp->ks_snapshot	= default_kstat_snapshot;
1093 	ksp->ks_lock		= NULL;
1094 
1095 	mutex_enter(&kstat_chain_lock);
1096 
1097 	/*
1098 	 * Add our kstat to the AVL trees.
1099 	 */
1100 	if (avl_find(&kstat_avl_byname, e, &where) != NULL) {
1101 		mutex_exit(&kstat_chain_lock);
1102 		cmn_err(CE_WARN,
1103 		    "kstat_create('%s', %d, '%s'): namespace collision",
1104 		    ks_module, ks_instance, ks_name);
1105 		kstat_free(e);
1106 		return (NULL);
1107 	}
1108 	avl_insert(&kstat_avl_byname, e, where);
1109 
1110 	/*
1111 	 * Loop around until we find an unused KID.
1112 	 */
1113 	do {
1114 		ksp->ks_kid = kstat_chain_id++;
1115 	} while (avl_find(&kstat_avl_bykid, e, &where) != NULL);
1116 	avl_insert(&kstat_avl_bykid, e, where);
1117 
1118 	mutex_exit(&kstat_chain_lock);
1119 
1120 	return (ksp);
1121 }
1122 
1123 /*
1124  * Activate a fully initialized kstat and make it visible to /dev/kstat.
1125  */
1126 void
1127 kstat_install(kstat_t *ksp)
1128 {
1129 	zoneid_t zoneid = ((ekstat_t *)ksp)->e_zone.zoneid;
1130 
1131 	/*
1132 	 * If this is a variable-size kstat, it MUST provide kstat data locking
1133 	 * to prevent data-size races with kstat readers.
1134 	 */
1135 	if ((ksp->ks_flags & KSTAT_FLAG_VAR_SIZE) && ksp->ks_lock == NULL) {
1136 		panic("kstat_install('%s', %d, '%s'): "
1137 		    "cannot create variable-size kstat without data lock",
1138 		    ksp->ks_module, ksp->ks_instance, ksp->ks_name);
1139 	}
1140 
1141 	if (kstat_hold_bykid(ksp->ks_kid, zoneid) != ksp) {
1142 		cmn_err(CE_WARN, "kstat_install(%p): does not exist",
1143 		    (void *)ksp);
1144 		return;
1145 	}
1146 
1147 	if (ksp->ks_type == KSTAT_TYPE_NAMED && ksp->ks_data != NULL) {
1148 		int has_long_strings = 0;
1149 		uint_t i;
1150 		kstat_named_t *knp = KSTAT_NAMED_PTR(ksp);
1151 
1152 		for (i = 0; i < ksp->ks_ndata; i++, knp++) {
1153 			if (knp->data_type == KSTAT_DATA_STRING) {
1154 				has_long_strings = 1;
1155 				break;
1156 			}
1157 		}
1158 		/*
1159 		 * It is an error for a named kstat with fields of
1160 		 * KSTAT_DATA_STRING to be non-virtual.
1161 		 */
1162 		if (has_long_strings && !(ksp->ks_flags & KSTAT_FLAG_VIRTUAL)) {
1163 			panic("kstat_install('%s', %d, '%s'): "
1164 			    "named kstat containing KSTAT_DATA_STRING "
1165 			    "is not virtual",
1166 			    ksp->ks_module, ksp->ks_instance,
1167 			    ksp->ks_name);
1168 		}
1169 		/*
1170 		 * The default snapshot routine does not handle KSTAT_WRITE
1171 		 * for long strings.
1172 		 */
1173 		if (has_long_strings && (ksp->ks_flags & KSTAT_FLAG_WRITABLE) &&
1174 		    (ksp->ks_snapshot == default_kstat_snapshot)) {
1175 			panic("kstat_install('%s', %d, '%s'): "
1176 			    "named kstat containing KSTAT_DATA_STRING "
1177 			    "is writable but uses default snapshot routine",
1178 			    ksp->ks_module, ksp->ks_instance, ksp->ks_name);
1179 		}
1180 	}
1181 
1182 	if (ksp->ks_flags & KSTAT_FLAG_DORMANT) {
1183 
1184 		/*
1185 		 * We are reactivating a dormant kstat.  Initialize the
1186 		 * caller's underlying data to the value it had when the
1187 		 * kstat went dormant, and mark the kstat as active.
1188 		 * Grab the provider's kstat lock if it's not already held.
1189 		 */
1190 		kmutex_t *lp = ksp->ks_lock;
1191 		if (lp != NULL && MUTEX_NOT_HELD(lp)) {
1192 			mutex_enter(lp);
1193 			(void) KSTAT_UPDATE(ksp, KSTAT_WRITE);
1194 			mutex_exit(lp);
1195 		} else {
1196 			(void) KSTAT_UPDATE(ksp, KSTAT_WRITE);
1197 		}
1198 		ksp->ks_flags &= ~KSTAT_FLAG_DORMANT;
1199 	}
1200 
1201 	/*
1202 	 * Now that the kstat is active, make it visible to the kstat driver.
1203 	 */
1204 	ksp->ks_flags &= ~KSTAT_FLAG_INVALID;
1205 	kstat_rele(ksp);
1206 }
1207 
1208 /*
1209  * Remove a kstat from the system.  Or, if it's a persistent kstat,
1210  * just update the data and mark it as dormant.
1211  */
1212 void
1213 kstat_delete(kstat_t *ksp)
1214 {
1215 	kmutex_t *lp;
1216 	ekstat_t *e = (ekstat_t *)ksp;
1217 	zoneid_t zoneid = e->e_zone.zoneid;
1218 	kstat_zone_t *kz;
1219 
1220 	if (ksp == NULL)
1221 		return;
1222 
1223 	lp = ksp->ks_lock;
1224 
1225 	if (lp != NULL && MUTEX_HELD(lp)) {
1226 		panic("kstat_delete(%p): caller holds data lock %p",
1227 		    (void *)ksp, (void *)lp);
1228 	}
1229 
1230 	if (kstat_hold_bykid(ksp->ks_kid, zoneid) != ksp) {
1231 		cmn_err(CE_WARN, "kstat_delete(%p): does not exist",
1232 		    (void *)ksp);
1233 		return;
1234 	}
1235 
1236 	if (ksp->ks_flags & KSTAT_FLAG_PERSISTENT) {
1237 		/*
1238 		 * Update the data one last time, so that all activity
1239 		 * prior to going dormant has been accounted for.
1240 		 */
1241 		KSTAT_ENTER(ksp);
1242 		(void) KSTAT_UPDATE(ksp, KSTAT_READ);
1243 		KSTAT_EXIT(ksp);
1244 
1245 		/*
1246 		 * Mark the kstat as dormant and restore caller-modifiable
1247 		 * fields to default values, so the kstat is readable during
1248 		 * the dormant phase.
1249 		 */
1250 		ksp->ks_flags |= KSTAT_FLAG_DORMANT;
1251 		ksp->ks_lock = NULL;
1252 		ksp->ks_update = default_kstat_update;
1253 		ksp->ks_private = NULL;
1254 		ksp->ks_snapshot = default_kstat_snapshot;
1255 		kstat_rele(ksp);
1256 		return;
1257 	}
1258 
1259 	/*
1260 	 * Remove the kstat from the framework's AVL trees,
1261 	 * free the allocated memory, and increment kstat_chain_id so
1262 	 * /dev/kstat clients can detect the event.
1263 	 */
1264 	mutex_enter(&kstat_chain_lock);
1265 	avl_remove(&kstat_avl_bykid, e);
1266 	avl_remove(&kstat_avl_byname, e);
1267 	kstat_chain_id++;
1268 	mutex_exit(&kstat_chain_lock);
1269 
1270 	kz = e->e_zone.next;
1271 	while (kz != NULL) {
1272 		kstat_zone_t *t = kz;
1273 
1274 		kz = kz->next;
1275 		kmem_free(t, sizeof (*t));
1276 	}
1277 	kstat_rele(ksp);
1278 	kstat_free(e);
1279 }
1280 
1281 void
1282 kstat_delete_byname_zone(const char *ks_module, int ks_instance,
1283     const char *ks_name, zoneid_t ks_zoneid)
1284 {
1285 	kstat_t *ksp;
1286 
1287 	ksp = kstat_hold_byname(ks_module, ks_instance, ks_name, ks_zoneid);
1288 	if (ksp != NULL) {
1289 		kstat_rele(ksp);
1290 		kstat_delete(ksp);
1291 	}
1292 }
1293 
1294 void
1295 kstat_delete_byname(const char *ks_module, int ks_instance, const char *ks_name)
1296 {
1297 	kstat_delete_byname_zone(ks_module, ks_instance, ks_name, ALL_ZONES);
1298 }
1299 
1300 /*
1301  * The sparc V9 versions of these routines can be much cheaper than
1302  * the poor 32-bit compiler can comprehend, so they're in sparcv9_subr.s.
1303  * For simplicity, however, we always feed the C versions to lint.
1304  */
1305 #if !defined(__sparc) || defined(lint) || defined(__lint)
1306 
1307 void
1308 kstat_waitq_enter(kstat_io_t *kiop)
1309 {
1310 	hrtime_t new, delta;
1311 	ulong_t wcnt;
1312 
1313 	new = gethrtime_unscaled();
1314 	delta = new - kiop->wlastupdate;
1315 	kiop->wlastupdate = new;
1316 	wcnt = kiop->wcnt++;
1317 	if (wcnt != 0) {
1318 		kiop->wlentime += delta * wcnt;
1319 		kiop->wtime += delta;
1320 	}
1321 }
1322 
1323 void
1324 kstat_waitq_exit(kstat_io_t *kiop)
1325 {
1326 	hrtime_t new, delta;
1327 	ulong_t wcnt;
1328 
1329 	new = gethrtime_unscaled();
1330 	delta = new - kiop->wlastupdate;
1331 	kiop->wlastupdate = new;
1332 	wcnt = kiop->wcnt--;
1333 	ASSERT((int)wcnt > 0);
1334 	kiop->wlentime += delta * wcnt;
1335 	kiop->wtime += delta;
1336 }
1337 
1338 void
1339 kstat_runq_enter(kstat_io_t *kiop)
1340 {
1341 	hrtime_t new, delta;
1342 	ulong_t rcnt;
1343 
1344 	new = gethrtime_unscaled();
1345 	delta = new - kiop->rlastupdate;
1346 	kiop->rlastupdate = new;
1347 	rcnt = kiop->rcnt++;
1348 	if (rcnt != 0) {
1349 		kiop->rlentime += delta * rcnt;
1350 		kiop->rtime += delta;
1351 	}
1352 }
1353 
1354 void
1355 kstat_runq_exit(kstat_io_t *kiop)
1356 {
1357 	hrtime_t new, delta;
1358 	ulong_t rcnt;
1359 
1360 	new = gethrtime_unscaled();
1361 	delta = new - kiop->rlastupdate;
1362 	kiop->rlastupdate = new;
1363 	rcnt = kiop->rcnt--;
1364 	ASSERT((int)rcnt > 0);
1365 	kiop->rlentime += delta * rcnt;
1366 	kiop->rtime += delta;
1367 }
1368 
1369 void
1370 kstat_waitq_to_runq(kstat_io_t *kiop)
1371 {
1372 	hrtime_t new, delta;
1373 	ulong_t wcnt, rcnt;
1374 
1375 	new = gethrtime_unscaled();
1376 
1377 	delta = new - kiop->wlastupdate;
1378 	kiop->wlastupdate = new;
1379 	wcnt = kiop->wcnt--;
1380 	ASSERT((int)wcnt > 0);
1381 	kiop->wlentime += delta * wcnt;
1382 	kiop->wtime += delta;
1383 
1384 	delta = new - kiop->rlastupdate;
1385 	kiop->rlastupdate = new;
1386 	rcnt = kiop->rcnt++;
1387 	if (rcnt != 0) {
1388 		kiop->rlentime += delta * rcnt;
1389 		kiop->rtime += delta;
1390 	}
1391 }
1392 
1393 void
1394 kstat_runq_back_to_waitq(kstat_io_t *kiop)
1395 {
1396 	hrtime_t new, delta;
1397 	ulong_t wcnt, rcnt;
1398 
1399 	new = gethrtime_unscaled();
1400 
1401 	delta = new - kiop->rlastupdate;
1402 	kiop->rlastupdate = new;
1403 	rcnt = kiop->rcnt--;
1404 	ASSERT((int)rcnt > 0);
1405 	kiop->rlentime += delta * rcnt;
1406 	kiop->rtime += delta;
1407 
1408 	delta = new - kiop->wlastupdate;
1409 	kiop->wlastupdate = new;
1410 	wcnt = kiop->wcnt++;
1411 	if (wcnt != 0) {
1412 		kiop->wlentime += delta * wcnt;
1413 		kiop->wtime += delta;
1414 	}
1415 }
1416 
1417 #endif
1418 
1419 void
1420 kstat_timer_start(kstat_timer_t *ktp)
1421 {
1422 	ktp->start_time = gethrtime();
1423 }
1424 
1425 void
1426 kstat_timer_stop(kstat_timer_t *ktp)
1427 {
1428 	hrtime_t	etime;
1429 	u_longlong_t	num_events;
1430 
1431 	ktp->stop_time = etime = gethrtime();
1432 	etime -= ktp->start_time;
1433 	num_events = ktp->num_events;
1434 	if (etime < ktp->min_time || num_events == 0)
1435 		ktp->min_time = etime;
1436 	if (etime > ktp->max_time)
1437 		ktp->max_time = etime;
1438 	ktp->elapsed_time += etime;
1439 	ktp->num_events = num_events + 1;
1440 }
1441