xref: /titanic_44/usr/src/uts/common/os/kstat_fr.c (revision 18c2aff776a775d34a4c9893a4c72e0434d68e36)
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 2006 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_SLEEP);
293 	mutex_enter(&kstat_chain_lock);
294 	kz->zoneid = zoneid;
295 	kz->next = e->e_zone.next;
296 	e->e_zone.next = kz;
297 	kstat_chain_id++;
298 	mutex_exit(&kstat_chain_lock);
299 }
300 
301 /*
302  * Compare the list of zones for the given kstats, returning 0 if they match
303  * (ie, one list contains ALL_ZONES or both lists contain the same zoneid).
304  * In practice, this is called indirectly by kstat_hold_byname(), so one of the
305  * two lists always has one element, and this is an O(n) operation rather than
306  * O(n^2).
307  */
308 static int
309 kstat_zone_compare(ekstat_t *e1, ekstat_t *e2)
310 {
311 	kstat_zone_t *kz1, *kz2;
312 
313 	ASSERT(MUTEX_HELD(&kstat_chain_lock));
314 	for (kz1 = &e1->e_zone; kz1 != NULL; kz1 = kz1->next) {
315 		for (kz2 = &e2->e_zone; kz2 != NULL; kz2 = kz2->next) {
316 			if (kz1->zoneid == ALL_ZONES ||
317 			    kz2->zoneid == ALL_ZONES)
318 				return (0);
319 			if (kz1->zoneid == kz2->zoneid)
320 				return (0);
321 		}
322 	}
323 	return (e1->e_zone.zoneid < e2->e_zone.zoneid ? -1 : 1);
324 }
325 
326 /*
327  * Support for keeping kstats sorted in AVL trees for fast lookups.
328  */
329 static int
330 kstat_compare_bykid(const void *a1, const void *a2)
331 {
332 	const kstat_t *k1 = a1;
333 	const kstat_t *k2 = a2;
334 
335 	if (k1->ks_kid < k2->ks_kid)
336 		return (-1);
337 	if (k1->ks_kid > k2->ks_kid)
338 		return (1);
339 	return (kstat_zone_compare((ekstat_t *)k1, (ekstat_t *)k2));
340 }
341 
342 static int
343 kstat_compare_byname(const void *a1, const void *a2)
344 {
345 	const kstat_t *k1 = a1;
346 	const kstat_t *k2 = a2;
347 	int s;
348 
349 	s = strcmp(k1->ks_module, k2->ks_module);
350 	if (s > 0)
351 		return (1);
352 	if (s < 0)
353 		return (-1);
354 
355 	if (k1->ks_instance < k2->ks_instance)
356 		return (-1);
357 	if (k1->ks_instance > k2->ks_instance)
358 		return (1);
359 
360 	s = strcmp(k1->ks_name, k2->ks_name);
361 	if (s > 0)
362 		return (1);
363 	if (s < 0)
364 		return (-1);
365 
366 	return (kstat_zone_compare((ekstat_t *)k1, (ekstat_t *)k2));
367 }
368 
369 static kstat_t *
370 kstat_hold(avl_tree_t *t, ekstat_t *template)
371 {
372 	kstat_t *ksp;
373 	ekstat_t *e;
374 
375 	mutex_enter(&kstat_chain_lock);
376 	for (;;) {
377 		ksp = avl_find(t, template, NULL);
378 		if (ksp == NULL)
379 			break;
380 		e = (ekstat_t *)ksp;
381 		if (e->e_owner == NULL) {
382 			e->e_owner = curthread;
383 			break;
384 		}
385 		cv_wait(&e->e_cv, &kstat_chain_lock);
386 	}
387 	mutex_exit(&kstat_chain_lock);
388 	return (ksp);
389 }
390 
391 void
392 kstat_rele(kstat_t *ksp)
393 {
394 	ekstat_t *e = (ekstat_t *)ksp;
395 
396 	mutex_enter(&kstat_chain_lock);
397 	ASSERT(e->e_owner == curthread);
398 	e->e_owner = NULL;
399 	cv_broadcast(&e->e_cv);
400 	mutex_exit(&kstat_chain_lock);
401 }
402 
403 kstat_t *
404 kstat_hold_bykid(kid_t kid, zoneid_t zoneid)
405 {
406 	ekstat_t e;
407 
408 	e.e_ks.ks_kid = kid;
409 	e.e_zone.zoneid = zoneid;
410 	e.e_zone.next = NULL;
411 
412 	return (kstat_hold(&kstat_avl_bykid, &e));
413 }
414 
415 kstat_t *
416 kstat_hold_byname(char *ks_module, int ks_instance, char *ks_name,
417     zoneid_t ks_zoneid)
418 {
419 	ekstat_t e;
420 
421 	kstat_set_string(e.e_ks.ks_module, ks_module);
422 	e.e_ks.ks_instance = ks_instance;
423 	kstat_set_string(e.e_ks.ks_name, ks_name);
424 	e.e_zone.zoneid = ks_zoneid;
425 	e.e_zone.next = NULL;
426 	return (kstat_hold(&kstat_avl_byname, &e));
427 }
428 
429 static ekstat_t *
430 kstat_alloc(size_t size)
431 {
432 	ekstat_t *e = NULL;
433 
434 	size = P2ROUNDUP(sizeof (ekstat_t) + size, KSTAT_ALIGN);
435 
436 	if (kstat_arena == NULL) {
437 		if (size <= kstat_initial_avail) {
438 			e = kstat_initial_ptr;
439 			kstat_initial_ptr = (char *)kstat_initial_ptr + size;
440 			kstat_initial_avail -= size;
441 		}
442 	} else {
443 		e = vmem_alloc(kstat_arena, size, VM_NOSLEEP);
444 	}
445 
446 	if (e != NULL) {
447 		bzero(e, size);
448 		e->e_size = size;
449 		cv_init(&e->e_cv, NULL, CV_DEFAULT, NULL);
450 	}
451 
452 	return (e);
453 }
454 
455 static void
456 kstat_free(ekstat_t *e)
457 {
458 	cv_destroy(&e->e_cv);
459 	vmem_free(kstat_arena, e, e->e_size);
460 }
461 
462 /*
463  * Create various system kstats.
464  */
465 void
466 kstat_init(void)
467 {
468 	kstat_t *ksp;
469 	ekstat_t *e;
470 	avl_tree_t *t = &kstat_avl_bykid;
471 
472 	/*
473 	 * Set up the kstat vmem arena.
474 	 */
475 	kstat_arena = vmem_create("kstat",
476 	    kstat_initial, sizeof (kstat_initial), KSTAT_ALIGN,
477 	    segkmem_alloc, segkmem_free, heap_arena, 0, VM_SLEEP);
478 
479 	/*
480 	 * Make initial kstats appear as though they were allocated.
481 	 */
482 	for (e = avl_first(t); e != NULL; e = avl_walk(t, e, AVL_AFTER))
483 		(void) vmem_xalloc(kstat_arena, e->e_size, KSTAT_ALIGN,
484 		    0, 0, e, (char *)e + e->e_size,
485 		    VM_NOSLEEP | VM_BESTFIT | VM_PANIC);
486 
487 	/*
488 	 * The mother of all kstats.  The first kstat in the system, which
489 	 * always has KID 0, has the headers for all kstats (including itself)
490 	 * as its data.  Thus, the kstat driver does not need any special
491 	 * interface to extract the kstat chain.
492 	 */
493 	kstat_chain_id = 0;
494 	ksp = kstat_create("unix", 0, "kstat_headers", "kstat", KSTAT_TYPE_RAW,
495 		0, KSTAT_FLAG_VIRTUAL | KSTAT_FLAG_VAR_SIZE);
496 	if (ksp) {
497 		ksp->ks_lock = &kstat_chain_lock;
498 		ksp->ks_update = header_kstat_update;
499 		ksp->ks_snapshot = header_kstat_snapshot;
500 		kstat_install(ksp);
501 	} else {
502 		panic("cannot create kstat 'kstat_headers'");
503 	}
504 
505 	ksp = kstat_create("unix", 0, "kstat_types", "kstat",
506 		KSTAT_TYPE_NAMED, KSTAT_NUM_TYPES, 0);
507 	if (ksp) {
508 		int i;
509 		kstat_named_t *kn = KSTAT_NAMED_PTR(ksp);
510 
511 		for (i = 0; i < KSTAT_NUM_TYPES; i++) {
512 			kstat_named_init(&kn[i], kstat_data_type[i].name,
513 				KSTAT_DATA_ULONG);
514 			kn[i].value.ul = i;
515 		}
516 		kstat_install(ksp);
517 	}
518 
519 	ksp = kstat_create("unix", 0, "sysinfo", "misc", KSTAT_TYPE_RAW,
520 		sizeof (sysinfo_t), KSTAT_FLAG_VIRTUAL);
521 	if (ksp) {
522 		ksp->ks_data = (void *) &sysinfo;
523 		kstat_install(ksp);
524 	}
525 
526 	ksp = kstat_create("unix", 0, "vminfo", "vm", KSTAT_TYPE_RAW,
527 		sizeof (vminfo_t), KSTAT_FLAG_VIRTUAL);
528 	if (ksp) {
529 		ksp->ks_data = (void *) &vminfo;
530 		kstat_install(ksp);
531 	}
532 
533 	ksp = kstat_create("unix", 0, "segmap", "vm", KSTAT_TYPE_NAMED,
534 		segmapcnt_ndata, KSTAT_FLAG_VIRTUAL);
535 	if (ksp) {
536 		ksp->ks_data = (void *) segmapcnt_ptr;
537 		ksp->ks_update = segmap_kstat_update;
538 		kstat_install(ksp);
539 	}
540 
541 	ksp = kstat_create("unix", 0, "biostats", "misc", KSTAT_TYPE_NAMED,
542 		biostats_ndata, KSTAT_FLAG_VIRTUAL);
543 	if (ksp) {
544 		ksp->ks_data = (void *) biostats_ptr;
545 		kstat_install(ksp);
546 	}
547 
548 #ifdef VAC
549 	ksp = kstat_create("unix", 0, "flushmeter", "hat", KSTAT_TYPE_RAW,
550 		sizeof (struct flushmeter), KSTAT_FLAG_VIRTUAL);
551 	if (ksp) {
552 		ksp->ks_data = (void *) &flush_cnt;
553 		kstat_install(ksp);
554 	}
555 #endif	/* VAC */
556 
557 	ksp = kstat_create("unix", 0, "var", "misc", KSTAT_TYPE_RAW,
558 		sizeof (struct var), KSTAT_FLAG_VIRTUAL);
559 	if (ksp) {
560 		ksp->ks_data = (void *) &v;
561 		kstat_install(ksp);
562 	}
563 
564 	ksp = kstat_create("unix", 0, "system_misc", "misc", KSTAT_TYPE_NAMED,
565 		sizeof (system_misc_kstat) / sizeof (kstat_named_t),
566 		KSTAT_FLAG_VIRTUAL);
567 	if (ksp) {
568 		ksp->ks_data = (void *) &system_misc_kstat;
569 		ksp->ks_update = system_misc_kstat_update;
570 		kstat_install(ksp);
571 	}
572 
573 	ksp = kstat_create("unix", 0, "system_pages", "pages", KSTAT_TYPE_NAMED,
574 		sizeof (system_pages_kstat) / sizeof (kstat_named_t),
575 		KSTAT_FLAG_VIRTUAL);
576 	if (ksp) {
577 		ksp->ks_data = (void *) &system_pages_kstat;
578 		ksp->ks_update = system_pages_kstat_update;
579 		kstat_install(ksp);
580 	}
581 
582 	ksp = kstat_create("poll", 0, "pollstats", "misc", KSTAT_TYPE_NAMED,
583 	    pollstats_ndata, KSTAT_FLAG_VIRTUAL | KSTAT_FLAG_WRITABLE);
584 
585 	if (ksp) {
586 		ksp->ks_data = pollstats_ptr;
587 		kstat_install(ksp);
588 	}
589 }
590 
591 /*
592  * Caller of this should ensure that the string pointed by src
593  * doesn't change while kstat's lock is held. Not doing so defeats
594  * kstat's snapshot strategy as explained in <sys/kstat.h>
595  */
596 void
597 kstat_named_setstr(kstat_named_t *knp, const char *src)
598 {
599 	if (knp->data_type != KSTAT_DATA_STRING)
600 		panic("kstat_named_setstr('%p', '%p'): "
601 		    "named kstat is not of type KSTAT_DATA_STRING", knp, src);
602 
603 	KSTAT_NAMED_STR_PTR(knp) = (char *)src;
604 	if (src != NULL)
605 		KSTAT_NAMED_STR_BUFLEN(knp) = strlen(src) + 1;
606 	else
607 		KSTAT_NAMED_STR_BUFLEN(knp) = 0;
608 }
609 
610 void
611 kstat_set_string(char *dst, char *src)
612 {
613 	bzero(dst, KSTAT_STRLEN);
614 	(void) strncpy(dst, src, KSTAT_STRLEN - 1);
615 }
616 
617 void
618 kstat_named_init(kstat_named_t *knp, char *name, uchar_t data_type)
619 {
620 	kstat_set_string(knp->name, name);
621 	knp->data_type = data_type;
622 
623 	if (data_type == KSTAT_DATA_STRING)
624 		kstat_named_setstr(knp, NULL);
625 }
626 
627 void
628 kstat_timer_init(kstat_timer_t *ktp, char *name)
629 {
630 	kstat_set_string(ktp->name, name);
631 }
632 
633 /* ARGSUSED */
634 static int
635 default_kstat_update(kstat_t *ksp, int rw)
636 {
637 	uint_t i;
638 	size_t len = 0;
639 	kstat_named_t *knp;
640 
641 	/*
642 	 * Named kstats with variable-length long strings have a standard
643 	 * way of determining how much space is needed to hold the snapshot:
644 	 */
645 	if (ksp->ks_data != NULL && ksp->ks_type == KSTAT_TYPE_NAMED &&
646 	    (ksp->ks_flags & KSTAT_FLAG_VAR_SIZE)) {
647 
648 		/*
649 		 * Add in the space required for the strings
650 		 */
651 		knp = KSTAT_NAMED_PTR(ksp);
652 		for (i = 0; i < ksp->ks_ndata; i++, knp++) {
653 			if (knp->data_type == KSTAT_DATA_STRING)
654 				len += KSTAT_NAMED_STR_BUFLEN(knp);
655 		}
656 		ksp->ks_data_size =
657 		    ksp->ks_ndata * sizeof (kstat_named_t) + len;
658 	}
659 	return (0);
660 }
661 
662 static int
663 default_kstat_snapshot(kstat_t *ksp, void *buf, int rw)
664 {
665 	kstat_io_t *kiop;
666 	hrtime_t cur_time;
667 	size_t	namedsz;
668 
669 	ksp->ks_snaptime = cur_time = gethrtime();
670 
671 	if (rw == KSTAT_WRITE) {
672 		if (!(ksp->ks_flags & KSTAT_FLAG_WRITABLE))
673 			return (EACCES);
674 		bcopy(buf, ksp->ks_data, ksp->ks_data_size);
675 		return (0);
676 	}
677 
678 	/*
679 	 * KSTAT_TYPE_NAMED kstats are defined to have ks_ndata
680 	 * number of kstat_named_t structures, followed by an optional
681 	 * string segment. The ks_data generally holds only the
682 	 * kstat_named_t structures. So we copy it first. The strings,
683 	 * if any, are copied below. For other kstat types, ks_data holds the
684 	 * entire buffer.
685 	 */
686 
687 	namedsz = sizeof (kstat_named_t) * ksp->ks_ndata;
688 	if (ksp->ks_type == KSTAT_TYPE_NAMED && ksp->ks_data_size > namedsz)
689 		bcopy(ksp->ks_data, buf, namedsz);
690 	else
691 		bcopy(ksp->ks_data, buf, ksp->ks_data_size);
692 
693 	/*
694 	 * Apply kstat type-specific data massaging
695 	 */
696 	switch (ksp->ks_type) {
697 
698 	case KSTAT_TYPE_IO:
699 		/*
700 		 * Normalize time units and deal with incomplete transactions
701 		 */
702 		kiop = (kstat_io_t *)buf;
703 
704 		scalehrtime(&kiop->wtime);
705 		scalehrtime(&kiop->wlentime);
706 		scalehrtime(&kiop->wlastupdate);
707 		scalehrtime(&kiop->rtime);
708 		scalehrtime(&kiop->rlentime);
709 		scalehrtime(&kiop->rlastupdate);
710 
711 		if (kiop->wcnt != 0) {
712 			/* like kstat_waitq_exit */
713 			hrtime_t wfix = cur_time - kiop->wlastupdate;
714 			kiop->wlastupdate = cur_time;
715 			kiop->wlentime += kiop->wcnt * wfix;
716 			kiop->wtime += wfix;
717 		}
718 
719 		if (kiop->rcnt != 0) {
720 			/* like kstat_runq_exit */
721 			hrtime_t rfix = cur_time - kiop->rlastupdate;
722 			kiop->rlastupdate = cur_time;
723 			kiop->rlentime += kiop->rcnt * rfix;
724 			kiop->rtime += rfix;
725 		}
726 		break;
727 
728 	case KSTAT_TYPE_NAMED:
729 		/*
730 		 * Massage any long strings in at the end of the buffer
731 		 */
732 		if (ksp->ks_data_size > namedsz) {
733 			uint_t i;
734 			kstat_named_t *knp = buf;
735 			char *dst = (char *)(knp + ksp->ks_ndata);
736 			/*
737 			 * Copy strings and update pointers
738 			 */
739 			for (i = 0; i < ksp->ks_ndata; i++, knp++) {
740 				if (knp->data_type == KSTAT_DATA_STRING &&
741 				    KSTAT_NAMED_STR_PTR(knp) != NULL) {
742 					bcopy(KSTAT_NAMED_STR_PTR(knp), dst,
743 					    KSTAT_NAMED_STR_BUFLEN(knp));
744 					KSTAT_NAMED_STR_PTR(knp) = dst;
745 					dst += KSTAT_NAMED_STR_BUFLEN(knp);
746 				}
747 			}
748 			ASSERT(dst <= ((char *)buf + ksp->ks_data_size));
749 		}
750 		break;
751 	}
752 	return (0);
753 }
754 
755 static int
756 header_kstat_update(kstat_t *header_ksp, int rw)
757 {
758 	int nkstats = 0;
759 	ekstat_t *e;
760 	avl_tree_t *t = &kstat_avl_bykid;
761 	zoneid_t zoneid;
762 
763 	if (rw == KSTAT_WRITE)
764 		return (EACCES);
765 
766 	ASSERT(MUTEX_HELD(&kstat_chain_lock));
767 
768 	zoneid = getzoneid();
769 	for (e = avl_first(t); e != NULL; e = avl_walk(t, e, AVL_AFTER)) {
770 		if (kstat_zone_find((kstat_t *)e, zoneid)) {
771 			nkstats++;
772 		}
773 	}
774 	header_ksp->ks_ndata = nkstats;
775 	header_ksp->ks_data_size = nkstats * sizeof (kstat_t);
776 	return (0);
777 }
778 
779 /*
780  * Copy out the data section of kstat 0, which consists of the list
781  * of all kstat headers.  By specification, these headers must be
782  * copied out in order of increasing KID.
783  */
784 static int
785 header_kstat_snapshot(kstat_t *header_ksp, void *buf, int rw)
786 {
787 	ekstat_t *e;
788 	avl_tree_t *t = &kstat_avl_bykid;
789 	zoneid_t zoneid;
790 
791 	header_ksp->ks_snaptime = gethrtime();
792 
793 	if (rw == KSTAT_WRITE)
794 		return (EACCES);
795 
796 	ASSERT(MUTEX_HELD(&kstat_chain_lock));
797 
798 	zoneid = getzoneid();
799 	for (e = avl_first(t); e != NULL; e = avl_walk(t, e, AVL_AFTER)) {
800 		if (kstat_zone_find((kstat_t *)e, zoneid)) {
801 			bcopy(&e->e_ks, buf, sizeof (kstat_t));
802 			buf = (char *)buf + sizeof (kstat_t);
803 		}
804 	}
805 
806 	return (0);
807 }
808 
809 /* ARGSUSED */
810 static int
811 system_misc_kstat_update(kstat_t *ksp, int rw)
812 {
813 	int myncpus = ncpus;
814 	int *loadavgp = &avenrun[0];
815 	int loadavg[LOADAVG_NSTATS];
816 
817 	if (rw == KSTAT_WRITE)
818 		return (EACCES);
819 
820 	if (!INGLOBALZONE(curproc)) {
821 		/*
822 		 * Here we grab cpu_lock which is OK as long as no-one in the
823 		 * future attempts to lookup this particular kstat
824 		 * (unix:0:system_misc) while holding cpu_lock.
825 		 */
826 		mutex_enter(&cpu_lock);
827 		if (pool_pset_enabled()) {
828 			psetid_t mypsid = zone_pset_get(curproc->p_zone);
829 			int error;
830 
831 			myncpus = zone_ncpus_get(curproc->p_zone);
832 			ASSERT(myncpus > 0);
833 			error = cpupart_get_loadavg(mypsid, &loadavg[0],
834 			    LOADAVG_NSTATS);
835 			ASSERT(error == 0);
836 			loadavgp = &loadavg[0];
837 		}
838 		mutex_exit(&cpu_lock);
839 	}
840 
841 	system_misc_kstat.ncpus.value.ui32		= (uint32_t)myncpus;
842 	system_misc_kstat.lbolt.value.ui32		= (uint32_t)lbolt;
843 	system_misc_kstat.deficit.value.ui32		= (uint32_t)deficit;
844 	system_misc_kstat.clk_intr.value.ui32		= (uint32_t)lbolt;
845 	system_misc_kstat.vac.value.ui32		= (uint32_t)vac;
846 	system_misc_kstat.nproc.value.ui32		= (uint32_t)nproc;
847 	system_misc_kstat.avenrun_1min.value.ui32	= (uint32_t)loadavgp[0];
848 	system_misc_kstat.avenrun_5min.value.ui32	= (uint32_t)loadavgp[1];
849 	system_misc_kstat.avenrun_15min.value.ui32	= (uint32_t)loadavgp[2];
850 	system_misc_kstat.boot_time.value.ui32		= (uint32_t)boot_time;
851 	return (0);
852 }
853 
854 #ifdef	__sparc
855 extern caddr_t	econtig32;
856 #else	/* !__sparc */
857 extern caddr_t	econtig;
858 #endif	/* __sparc */
859 
860 extern struct vnode kvp;
861 
862 /* ARGSUSED */
863 static int
864 system_pages_kstat_update(kstat_t *ksp, int rw)
865 {
866 	kobj_stat_t kobj_stat;
867 
868 	if (rw == KSTAT_WRITE) {
869 		return (EACCES);
870 	}
871 
872 	kobj_stat_get(&kobj_stat);
873 	system_pages_kstat.physmem.value.ul	= (ulong_t)physmem;
874 	system_pages_kstat.nalloc.value.ul	= kobj_stat.nalloc;
875 	system_pages_kstat.nfree.value.ul	= kobj_stat.nfree;
876 	system_pages_kstat.nalloc_calls.value.ul = kobj_stat.nalloc_calls;
877 	system_pages_kstat.nfree_calls.value.ul	= kobj_stat.nfree_calls;
878 	system_pages_kstat.kernelbase.value.ul	= (ulong_t)KERNELBASE;
879 
880 #ifdef	__sparc
881 	/*
882 	 * kstat should REALLY be modified to also report kmem64_base and
883 	 * kmem64_end (see sun4u/os/startup.c), as the virtual address range
884 	 * [ kernelbase .. econtig ] no longer is truly reflective of the
885 	 * kernel's vallocs...
886 	 */
887 	system_pages_kstat.econtig.value.ul	= (ulong_t)econtig32;
888 #else	/* !__sparc */
889 	system_pages_kstat.econtig.value.ul	= (ulong_t)econtig;
890 #endif	/* __sparc */
891 
892 	system_pages_kstat.freemem.value.ul	= (ulong_t)freemem;
893 	system_pages_kstat.availrmem.value.ul	= (ulong_t)availrmem;
894 	system_pages_kstat.lotsfree.value.ul	= (ulong_t)lotsfree;
895 	system_pages_kstat.desfree.value.ul	= (ulong_t)desfree;
896 	system_pages_kstat.minfree.value.ul	= (ulong_t)minfree;
897 	system_pages_kstat.fastscan.value.ul	= (ulong_t)fastscan;
898 	system_pages_kstat.slowscan.value.ul	= (ulong_t)slowscan;
899 	system_pages_kstat.nscan.value.ul	= (ulong_t)nscan;
900 	system_pages_kstat.desscan.value.ul	= (ulong_t)desscan;
901 	system_pages_kstat.pagesfree.value.ul	= (ulong_t)freemem;
902 	system_pages_kstat.pageslocked.value.ul	= (ulong_t)(availrmem_initial -
903 	    availrmem);
904 	system_pages_kstat.pagestotal.value.ul	= (ulong_t)total_pages;
905 	/*
906 	 * pp_kernel represents total pages used by the kernel since the
907 	 * startup. This formula takes into account the boottime kernel
908 	 * footprint and also considers the availrmem changes because of
909 	 * user explicit page locking.
910 	 */
911 	system_pages_kstat.pp_kernel.value.ul   = (ulong_t)(physinstalled -
912 		obp_pages - availrmem - k_anoninfo.ani_mem_resv -
913 		anon_segkp_pages_locked - segvn_pages_locked -
914 		pages_locked - pages_claimed - pages_useclaim);
915 
916 	return (0);
917 }
918 
919 kstat_t *
920 kstat_create(char *ks_module, int ks_instance, char *ks_name, char *ks_class,
921     uchar_t ks_type, uint_t ks_ndata, uchar_t ks_flags)
922 {
923 	return (kstat_create_zone(ks_module, ks_instance, ks_name, ks_class,
924 		    ks_type, ks_ndata, ks_flags, ALL_ZONES));
925 }
926 
927 /*
928  * Allocate and initialize a kstat structure.  Or, if a dormant kstat with
929  * the specified name exists, reactivate it.  Returns a pointer to the kstat
930  * on success, NULL on failure.  The kstat will not be visible to the
931  * kstat driver until kstat_install().
932  */
933 kstat_t *
934 kstat_create_zone(char *ks_module, int ks_instance, char *ks_name,
935     char *ks_class, uchar_t ks_type, uint_t ks_ndata, uchar_t ks_flags,
936     zoneid_t ks_zoneid)
937 {
938 	size_t ks_data_size;
939 	kstat_t *ksp;
940 	ekstat_t *e;
941 	avl_index_t where;
942 	char namebuf[KSTAT_STRLEN + 16];
943 
944 	if (avl_numnodes(&kstat_avl_bykid) == 0) {
945 		avl_create(&kstat_avl_bykid, kstat_compare_bykid,
946 		    sizeof (ekstat_t), offsetof(struct ekstat, e_avl_bykid));
947 
948 		avl_create(&kstat_avl_byname, kstat_compare_byname,
949 		    sizeof (ekstat_t), offsetof(struct ekstat, e_avl_byname));
950 	}
951 
952 	/*
953 	 * If ks_name == NULL, set the ks_name to <module><instance>.
954 	 */
955 	if (ks_name == NULL) {
956 		char buf[KSTAT_STRLEN];
957 		kstat_set_string(buf, ks_module);
958 		(void) sprintf(namebuf, "%s%d", buf, ks_instance);
959 		ks_name = namebuf;
960 	}
961 
962 	/*
963 	 * Make sure it's a valid kstat data type
964 	 */
965 	if (ks_type >= KSTAT_NUM_TYPES) {
966 		cmn_err(CE_WARN, "kstat_create('%s', %d, '%s'): "
967 			"invalid kstat type %d",
968 			ks_module, ks_instance, ks_name, ks_type);
969 		return (NULL);
970 	}
971 
972 	/*
973 	 * Don't allow persistent virtual kstats -- it makes no sense.
974 	 * ks_data points to garbage when the client goes away.
975 	 */
976 	if ((ks_flags & KSTAT_FLAG_PERSISTENT) &&
977 	    (ks_flags & KSTAT_FLAG_VIRTUAL)) {
978 		cmn_err(CE_WARN, "kstat_create('%s', %d, '%s'): "
979 			"cannot create persistent virtual kstat",
980 			ks_module, ks_instance, ks_name);
981 		return (NULL);
982 	}
983 
984 	/*
985 	 * Don't allow variable-size physical kstats, since the framework's
986 	 * memory allocation for physical kstat data is fixed at creation time.
987 	 */
988 	if ((ks_flags & KSTAT_FLAG_VAR_SIZE) &&
989 	    !(ks_flags & KSTAT_FLAG_VIRTUAL)) {
990 		cmn_err(CE_WARN, "kstat_create('%s', %d, '%s'): "
991 			"cannot create variable-size physical kstat",
992 			ks_module, ks_instance, ks_name);
993 		return (NULL);
994 	}
995 
996 	/*
997 	 * Make sure the number of data fields is within legal range
998 	 */
999 	if (ks_ndata < kstat_data_type[ks_type].min_ndata ||
1000 	    ks_ndata > kstat_data_type[ks_type].max_ndata) {
1001 		cmn_err(CE_WARN, "kstat_create('%s', %d, '%s'): "
1002 			"ks_ndata=%d out of range [%d, %d]",
1003 			ks_module, ks_instance, ks_name, (int)ks_ndata,
1004 			kstat_data_type[ks_type].min_ndata,
1005 			kstat_data_type[ks_type].max_ndata);
1006 		return (NULL);
1007 	}
1008 
1009 	ks_data_size = kstat_data_type[ks_type].size * ks_ndata;
1010 
1011 	/*
1012 	 * If the named kstat already exists and is dormant, reactivate it.
1013 	 */
1014 	ksp = kstat_hold_byname(ks_module, ks_instance, ks_name, ks_zoneid);
1015 	if (ksp != NULL) {
1016 		if (!(ksp->ks_flags & KSTAT_FLAG_DORMANT)) {
1017 			/*
1018 			 * The named kstat exists but is not dormant --
1019 			 * this is a kstat namespace collision.
1020 			 */
1021 			kstat_rele(ksp);
1022 			cmn_err(CE_WARN,
1023 			    "kstat_create('%s', %d, '%s'): namespace collision",
1024 			    ks_module, ks_instance, ks_name);
1025 			return (NULL);
1026 		}
1027 		if ((strcmp(ksp->ks_class, ks_class) != 0) ||
1028 		    (ksp->ks_type != ks_type) ||
1029 		    (ksp->ks_ndata != ks_ndata) ||
1030 		    (ks_flags & KSTAT_FLAG_VIRTUAL)) {
1031 			/*
1032 			 * The name is the same, but the other key parameters
1033 			 * differ from those of the dormant kstat -- bogus.
1034 			 */
1035 			kstat_rele(ksp);
1036 			cmn_err(CE_WARN, "kstat_create('%s', %d, '%s'): "
1037 				"invalid reactivation of dormant kstat",
1038 				ks_module, ks_instance, ks_name);
1039 			return (NULL);
1040 		}
1041 		/*
1042 		 * Return dormant kstat pointer to caller.  As usual,
1043 		 * the kstat is marked invalid until kstat_install().
1044 		 */
1045 		ksp->ks_flags |= KSTAT_FLAG_INVALID;
1046 		kstat_rele(ksp);
1047 		return (ksp);
1048 	}
1049 
1050 	/*
1051 	 * Allocate memory for the new kstat header and, if this is a physical
1052 	 * kstat, the data section.
1053 	 */
1054 	e = kstat_alloc(ks_flags & KSTAT_FLAG_VIRTUAL ? 0 : ks_data_size);
1055 	if (e == NULL) {
1056 		cmn_err(CE_NOTE, "kstat_create('%s', %d, '%s'): "
1057 			"insufficient kernel memory",
1058 			ks_module, ks_instance, ks_name);
1059 		return (NULL);
1060 	}
1061 
1062 	/*
1063 	 * Initialize as many fields as we can.  The caller may reset
1064 	 * ks_lock, ks_update, ks_private, and ks_snapshot as necessary.
1065 	 * Creators of virtual kstats may also reset ks_data.  It is
1066 	 * also up to the caller to initialize the kstat data section,
1067 	 * if necessary.  All initialization must be complete before
1068 	 * calling kstat_install().
1069 	 */
1070 	e->e_zone.zoneid = ks_zoneid;
1071 	e->e_zone.next = NULL;
1072 
1073 	ksp = &e->e_ks;
1074 	ksp->ks_crtime		= gethrtime();
1075 	kstat_set_string(ksp->ks_module, ks_module);
1076 	ksp->ks_instance	= ks_instance;
1077 	kstat_set_string(ksp->ks_name, ks_name);
1078 	ksp->ks_type		= ks_type;
1079 	kstat_set_string(ksp->ks_class, ks_class);
1080 	ksp->ks_flags		= ks_flags | KSTAT_FLAG_INVALID;
1081 	if (ks_flags & KSTAT_FLAG_VIRTUAL)
1082 		ksp->ks_data	= NULL;
1083 	else
1084 		ksp->ks_data	= (void *)(e + 1);
1085 	ksp->ks_ndata		= ks_ndata;
1086 	ksp->ks_data_size	= ks_data_size;
1087 	ksp->ks_snaptime	= ksp->ks_crtime;
1088 	ksp->ks_update		= default_kstat_update;
1089 	ksp->ks_private		= NULL;
1090 	ksp->ks_snapshot	= default_kstat_snapshot;
1091 	ksp->ks_lock		= NULL;
1092 
1093 	mutex_enter(&kstat_chain_lock);
1094 
1095 	/*
1096 	 * Add our kstat to the AVL trees.
1097 	 */
1098 	if (avl_find(&kstat_avl_byname, e, &where) != NULL) {
1099 		mutex_exit(&kstat_chain_lock);
1100 		cmn_err(CE_WARN,
1101 		    "kstat_create('%s', %d, '%s'): namespace collision",
1102 		    ks_module, ks_instance, ks_name);
1103 		kstat_free(e);
1104 		return (NULL);
1105 	}
1106 	avl_insert(&kstat_avl_byname, e, where);
1107 
1108 	/*
1109 	 * Loop around until we find an unused KID.
1110 	 */
1111 	do {
1112 		ksp->ks_kid = kstat_chain_id++;
1113 	} while (avl_find(&kstat_avl_bykid, e, &where) != NULL);
1114 	avl_insert(&kstat_avl_bykid, e, where);
1115 
1116 	mutex_exit(&kstat_chain_lock);
1117 
1118 	return (ksp);
1119 }
1120 
1121 /*
1122  * Activate a fully initialized kstat and make it visible to /dev/kstat.
1123  */
1124 void
1125 kstat_install(kstat_t *ksp)
1126 {
1127 	zoneid_t zoneid = ((ekstat_t *)ksp)->e_zone.zoneid;
1128 
1129 	/*
1130 	 * If this is a variable-size kstat, it MUST provide kstat data locking
1131 	 * to prevent data-size races with kstat readers.
1132 	 */
1133 	if ((ksp->ks_flags & KSTAT_FLAG_VAR_SIZE) && ksp->ks_lock == NULL) {
1134 		panic("kstat_install('%s', %d, '%s'): "
1135 		    "cannot create variable-size kstat without data lock",
1136 		    ksp->ks_module, ksp->ks_instance, ksp->ks_name);
1137 	}
1138 
1139 	if (kstat_hold_bykid(ksp->ks_kid, zoneid) != ksp) {
1140 		cmn_err(CE_WARN, "kstat_install(%p): does not exist",
1141 		    (void *)ksp);
1142 		return;
1143 	}
1144 
1145 	if (ksp->ks_type == KSTAT_TYPE_NAMED && ksp->ks_data != NULL) {
1146 		int has_long_strings = 0;
1147 		uint_t i;
1148 		kstat_named_t *knp = KSTAT_NAMED_PTR(ksp);
1149 
1150 		for (i = 0; i < ksp->ks_ndata; i++, knp++) {
1151 			if (knp->data_type == KSTAT_DATA_STRING) {
1152 				has_long_strings = 1;
1153 				break;
1154 			}
1155 		}
1156 		/*
1157 		 * It is an error for a named kstat with fields of
1158 		 * KSTAT_DATA_STRING to be non-virtual.
1159 		 */
1160 		if (has_long_strings && !(ksp->ks_flags & KSTAT_FLAG_VIRTUAL)) {
1161 			panic("kstat_install('%s', %d, '%s'): "
1162 			    "named kstat containing KSTAT_DATA_STRING "
1163 			    "is not virtual",
1164 			    ksp->ks_module, ksp->ks_instance,
1165 			    ksp->ks_name);
1166 		}
1167 		/*
1168 		 * The default snapshot routine does not handle KSTAT_WRITE
1169 		 * for long strings.
1170 		 */
1171 		if (has_long_strings && (ksp->ks_flags & KSTAT_FLAG_WRITABLE) &&
1172 		    (ksp->ks_snapshot == default_kstat_snapshot)) {
1173 			panic("kstat_install('%s', %d, '%s'): "
1174 			    "named kstat containing KSTAT_DATA_STRING "
1175 			    "is writable but uses default snapshot routine",
1176 			    ksp->ks_module, ksp->ks_instance, ksp->ks_name);
1177 		}
1178 	}
1179 
1180 	if (ksp->ks_flags & KSTAT_FLAG_DORMANT) {
1181 
1182 		/*
1183 		 * We are reactivating a dormant kstat.  Initialize the
1184 		 * caller's underlying data to the value it had when the
1185 		 * kstat went dormant, and mark the kstat as active.
1186 		 * Grab the provider's kstat lock if it's not already held.
1187 		 */
1188 		kmutex_t *lp = ksp->ks_lock;
1189 		if (lp != NULL && MUTEX_NOT_HELD(lp)) {
1190 			mutex_enter(lp);
1191 			(void) KSTAT_UPDATE(ksp, KSTAT_WRITE);
1192 			mutex_exit(lp);
1193 		} else {
1194 			(void) KSTAT_UPDATE(ksp, KSTAT_WRITE);
1195 		}
1196 		ksp->ks_flags &= ~KSTAT_FLAG_DORMANT;
1197 	}
1198 
1199 	/*
1200 	 * Now that the kstat is active, make it visible to the kstat driver.
1201 	 */
1202 	ksp->ks_flags &= ~KSTAT_FLAG_INVALID;
1203 	kstat_rele(ksp);
1204 }
1205 
1206 /*
1207  * Remove a kstat from the system.  Or, if it's a persistent kstat,
1208  * just update the data and mark it as dormant.
1209  */
1210 void
1211 kstat_delete(kstat_t *ksp)
1212 {
1213 	kmutex_t *lp;
1214 	ekstat_t *e = (ekstat_t *)ksp;
1215 	zoneid_t zoneid = e->e_zone.zoneid;
1216 	kstat_zone_t *kz;
1217 
1218 	if (ksp == NULL)
1219 		return;
1220 
1221 	lp = ksp->ks_lock;
1222 
1223 	if (lp != NULL && MUTEX_HELD(lp)) {
1224 		panic("kstat_delete(%p): caller holds data lock %p",
1225 		    (void *)ksp, (void *)lp);
1226 	}
1227 
1228 	if (kstat_hold_bykid(ksp->ks_kid, zoneid) != ksp) {
1229 		cmn_err(CE_WARN, "kstat_delete(%p): does not exist",
1230 		    (void *)ksp);
1231 		return;
1232 	}
1233 
1234 	if (ksp->ks_flags & KSTAT_FLAG_PERSISTENT) {
1235 		/*
1236 		 * Update the data one last time, so that all activity
1237 		 * prior to going dormant has been accounted for.
1238 		 */
1239 		KSTAT_ENTER(ksp);
1240 		(void) KSTAT_UPDATE(ksp, KSTAT_READ);
1241 		KSTAT_EXIT(ksp);
1242 
1243 		/*
1244 		 * Mark the kstat as dormant and restore caller-modifiable
1245 		 * fields to default values, so the kstat is readable during
1246 		 * the dormant phase.
1247 		 */
1248 		ksp->ks_flags |= KSTAT_FLAG_DORMANT;
1249 		ksp->ks_lock = NULL;
1250 		ksp->ks_update = default_kstat_update;
1251 		ksp->ks_private = NULL;
1252 		ksp->ks_snapshot = default_kstat_snapshot;
1253 		kstat_rele(ksp);
1254 		return;
1255 	}
1256 
1257 	/*
1258 	 * Remove the kstat from the framework's AVL trees,
1259 	 * free the allocated memory, and increment kstat_chain_id so
1260 	 * /dev/kstat clients can detect the event.
1261 	 */
1262 	mutex_enter(&kstat_chain_lock);
1263 	avl_remove(&kstat_avl_bykid, e);
1264 	avl_remove(&kstat_avl_byname, e);
1265 	kstat_chain_id++;
1266 	mutex_exit(&kstat_chain_lock);
1267 
1268 	kz = e->e_zone.next;
1269 	while (kz != NULL) {
1270 		kstat_zone_t *t = kz;
1271 
1272 		kz = kz->next;
1273 		kmem_free(t, sizeof (*t));
1274 	}
1275 	kstat_rele(ksp);
1276 	kstat_free(e);
1277 }
1278 
1279 void
1280 kstat_delete_byname_zone(char *ks_module, int ks_instance, char *ks_name,
1281     zoneid_t ks_zoneid)
1282 {
1283 	kstat_t *ksp;
1284 
1285 	ksp = kstat_hold_byname(ks_module, ks_instance, ks_name, ks_zoneid);
1286 	if (ksp != NULL) {
1287 		kstat_rele(ksp);
1288 		kstat_delete(ksp);
1289 	}
1290 }
1291 
1292 void
1293 kstat_delete_byname(char *ks_module, int ks_instance, char *ks_name)
1294 {
1295 	kstat_delete_byname_zone(ks_module, ks_instance, ks_name, ALL_ZONES);
1296 }
1297 
1298 /*
1299  * The sparc V9 versions of these routines can be much cheaper than
1300  * the poor 32-bit compiler can comprehend, so they're in sparcv9_subr.s.
1301  * For simplicity, however, we always feed the C versions to lint.
1302  */
1303 #if !defined(__sparc) || defined(lint) || defined(__lint)
1304 
1305 void
1306 kstat_waitq_enter(kstat_io_t *kiop)
1307 {
1308 	hrtime_t new, delta;
1309 	ulong_t wcnt;
1310 
1311 	new = gethrtime_unscaled();
1312 	delta = new - kiop->wlastupdate;
1313 	kiop->wlastupdate = new;
1314 	wcnt = kiop->wcnt++;
1315 	if (wcnt != 0) {
1316 		kiop->wlentime += delta * wcnt;
1317 		kiop->wtime += delta;
1318 	}
1319 }
1320 
1321 void
1322 kstat_waitq_exit(kstat_io_t *kiop)
1323 {
1324 	hrtime_t new, delta;
1325 	ulong_t wcnt;
1326 
1327 	new = gethrtime_unscaled();
1328 	delta = new - kiop->wlastupdate;
1329 	kiop->wlastupdate = new;
1330 	wcnt = kiop->wcnt--;
1331 	ASSERT((int)wcnt > 0);
1332 	kiop->wlentime += delta * wcnt;
1333 	kiop->wtime += delta;
1334 }
1335 
1336 void
1337 kstat_runq_enter(kstat_io_t *kiop)
1338 {
1339 	hrtime_t new, delta;
1340 	ulong_t rcnt;
1341 
1342 	new = gethrtime_unscaled();
1343 	delta = new - kiop->rlastupdate;
1344 	kiop->rlastupdate = new;
1345 	rcnt = kiop->rcnt++;
1346 	if (rcnt != 0) {
1347 		kiop->rlentime += delta * rcnt;
1348 		kiop->rtime += delta;
1349 	}
1350 }
1351 
1352 void
1353 kstat_runq_exit(kstat_io_t *kiop)
1354 {
1355 	hrtime_t new, delta;
1356 	ulong_t rcnt;
1357 
1358 	new = gethrtime_unscaled();
1359 	delta = new - kiop->rlastupdate;
1360 	kiop->rlastupdate = new;
1361 	rcnt = kiop->rcnt--;
1362 	ASSERT((int)rcnt > 0);
1363 	kiop->rlentime += delta * rcnt;
1364 	kiop->rtime += delta;
1365 }
1366 
1367 void
1368 kstat_waitq_to_runq(kstat_io_t *kiop)
1369 {
1370 	hrtime_t new, delta;
1371 	ulong_t wcnt, rcnt;
1372 
1373 	new = gethrtime_unscaled();
1374 
1375 	delta = new - kiop->wlastupdate;
1376 	kiop->wlastupdate = new;
1377 	wcnt = kiop->wcnt--;
1378 	ASSERT((int)wcnt > 0);
1379 	kiop->wlentime += delta * wcnt;
1380 	kiop->wtime += delta;
1381 
1382 	delta = new - kiop->rlastupdate;
1383 	kiop->rlastupdate = new;
1384 	rcnt = kiop->rcnt++;
1385 	if (rcnt != 0) {
1386 		kiop->rlentime += delta * rcnt;
1387 		kiop->rtime += delta;
1388 	}
1389 }
1390 
1391 void
1392 kstat_runq_back_to_waitq(kstat_io_t *kiop)
1393 {
1394 	hrtime_t new, delta;
1395 	ulong_t wcnt, rcnt;
1396 
1397 	new = gethrtime_unscaled();
1398 
1399 	delta = new - kiop->rlastupdate;
1400 	kiop->rlastupdate = new;
1401 	rcnt = kiop->rcnt--;
1402 	ASSERT((int)rcnt > 0);
1403 	kiop->rlentime += delta * rcnt;
1404 	kiop->rtime += delta;
1405 
1406 	delta = new - kiop->wlastupdate;
1407 	kiop->wlastupdate = new;
1408 	wcnt = kiop->wcnt++;
1409 	if (wcnt != 0) {
1410 		kiop->wlentime += delta * wcnt;
1411 		kiop->wtime += delta;
1412 	}
1413 }
1414 
1415 #endif
1416 
1417 void
1418 kstat_timer_start(kstat_timer_t *ktp)
1419 {
1420 	ktp->start_time = gethrtime();
1421 }
1422 
1423 void
1424 kstat_timer_stop(kstat_timer_t *ktp)
1425 {
1426 	hrtime_t	etime;
1427 	u_longlong_t	num_events;
1428 
1429 	ktp->stop_time = etime = gethrtime();
1430 	etime -= ktp->start_time;
1431 	num_events = ktp->num_events;
1432 	if (etime < ktp->min_time || num_events == 0)
1433 		ktp->min_time = etime;
1434 	if (etime > ktp->max_time)
1435 		ktp->max_time = etime;
1436 	ktp->elapsed_time += etime;
1437 	ktp->num_events = num_events + 1;
1438 }
1439