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