xref: /onnv-gate/usr/src/uts/common/os/kstat_fr.c (revision 3792:57ba782523b7)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52723Scth  * Common Development and Distribution License (the "License").
62723Scth  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*3792Sakolb  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*
280Sstevel@tonic-gate  * Kernel statistics framework
290Sstevel@tonic-gate  */
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #include <sys/types.h>
320Sstevel@tonic-gate #include <sys/time.h>
330Sstevel@tonic-gate #include <sys/systm.h>
340Sstevel@tonic-gate #include <sys/vmsystm.h>
350Sstevel@tonic-gate #include <sys/t_lock.h>
360Sstevel@tonic-gate #include <sys/param.h>
370Sstevel@tonic-gate #include <sys/errno.h>
380Sstevel@tonic-gate #include <sys/vmem.h>
390Sstevel@tonic-gate #include <sys/sysmacros.h>
400Sstevel@tonic-gate #include <sys/cmn_err.h>
410Sstevel@tonic-gate #include <sys/kstat.h>
420Sstevel@tonic-gate #include <sys/sysinfo.h>
430Sstevel@tonic-gate #include <sys/cpuvar.h>
440Sstevel@tonic-gate #include <sys/fcntl.h>
450Sstevel@tonic-gate #include <sys/flock.h>
460Sstevel@tonic-gate #include <sys/vnode.h>
470Sstevel@tonic-gate #include <sys/vfs.h>
480Sstevel@tonic-gate #include <sys/dnlc.h>
490Sstevel@tonic-gate #include <sys/var.h>
500Sstevel@tonic-gate #include <sys/vmmeter.h>
510Sstevel@tonic-gate #include <sys/debug.h>
520Sstevel@tonic-gate #include <sys/kobj.h>
530Sstevel@tonic-gate #include <sys/avl.h>
540Sstevel@tonic-gate #include <sys/pool_pset.h>
550Sstevel@tonic-gate #include <sys/cpupart.h>
560Sstevel@tonic-gate #include <sys/zone.h>
570Sstevel@tonic-gate #include <sys/loadavg.h>
580Sstevel@tonic-gate #include <vm/page.h>
590Sstevel@tonic-gate #include <vm/anon.h>
600Sstevel@tonic-gate #include <vm/seg_kmem.h>
610Sstevel@tonic-gate 
620Sstevel@tonic-gate /*
630Sstevel@tonic-gate  * Global lock to protect the AVL trees and kstat_chain_id.
640Sstevel@tonic-gate  */
650Sstevel@tonic-gate static kmutex_t kstat_chain_lock;
660Sstevel@tonic-gate 
670Sstevel@tonic-gate /*
680Sstevel@tonic-gate  * Every install/delete kstat bumps kstat_chain_id.  This is used by:
690Sstevel@tonic-gate  *
700Sstevel@tonic-gate  * (1)	/dev/kstat, to detect changes in the kstat chain across ioctls;
710Sstevel@tonic-gate  *
720Sstevel@tonic-gate  * (2)	kstat_create(), to assign a KID (kstat ID) to each new kstat.
730Sstevel@tonic-gate  *	/dev/kstat uses the KID as a cookie for kstat lookups.
740Sstevel@tonic-gate  *
750Sstevel@tonic-gate  * We reserve the first two IDs because some kstats are created before
760Sstevel@tonic-gate  * the well-known ones (kstat_headers = 0, kstat_types = 1).
770Sstevel@tonic-gate  *
780Sstevel@tonic-gate  * We also bump the kstat_chain_id if a zone is gaining or losing visibility
790Sstevel@tonic-gate  * into a particular kstat, which is logically equivalent to a kstat being
800Sstevel@tonic-gate  * installed/deleted.
810Sstevel@tonic-gate  */
820Sstevel@tonic-gate 
830Sstevel@tonic-gate kid_t kstat_chain_id = 2;
840Sstevel@tonic-gate 
850Sstevel@tonic-gate /*
860Sstevel@tonic-gate  * As far as zones are concerned, there are 3 types of kstat:
870Sstevel@tonic-gate  *
880Sstevel@tonic-gate  * 1) Those which have a well-known name, and which should return per-zone data
890Sstevel@tonic-gate  * depending on which zone is doing the kstat_read().  sockfs:0:sock_unix_list
900Sstevel@tonic-gate  * is an example of this type of kstat.
910Sstevel@tonic-gate  *
920Sstevel@tonic-gate  * 2) Those which should only be exported to a particular list of zones.
930Sstevel@tonic-gate  * For example, in the case of nfs:*:mntinfo, we don't want zone A to be
940Sstevel@tonic-gate  * able to see NFS mounts associated with zone B, while we want the
950Sstevel@tonic-gate  * global zone to be able to see all mounts on the system.
960Sstevel@tonic-gate  *
970Sstevel@tonic-gate  * 3) Those that can be exported to all zones.  Most system-related
980Sstevel@tonic-gate  * kstats fall within this category.
990Sstevel@tonic-gate  *
1000Sstevel@tonic-gate  * An ekstat_t thus contains a list of kstats that the zone is to be
1010Sstevel@tonic-gate  * exported to.  The lookup of a name:instance:module thus translates to a
1020Sstevel@tonic-gate  * lookup of name:instance:module:myzone; if the kstat is not exported
1030Sstevel@tonic-gate  * to all zones, and does not have the caller's zoneid explicitly
1040Sstevel@tonic-gate  * enumerated in the list of zones to be exported to, it is the same as
1050Sstevel@tonic-gate  * if the kstat didn't exist.
1060Sstevel@tonic-gate  *
1070Sstevel@tonic-gate  * Writing to kstats is currently disallowed from within a non-global
1080Sstevel@tonic-gate  * zone, although this restriction could be removed in the future.
1090Sstevel@tonic-gate  */
1100Sstevel@tonic-gate typedef struct kstat_zone {
1110Sstevel@tonic-gate 	zoneid_t zoneid;
1120Sstevel@tonic-gate 	struct kstat_zone *next;
1130Sstevel@tonic-gate } kstat_zone_t;
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate /*
1160Sstevel@tonic-gate  * Extended kstat structure -- for internal use only.
1170Sstevel@tonic-gate  */
1180Sstevel@tonic-gate typedef struct ekstat {
1190Sstevel@tonic-gate 	kstat_t		e_ks;		/* the kstat itself */
1200Sstevel@tonic-gate 	size_t		e_size;		/* total allocation size */
1210Sstevel@tonic-gate 	kthread_t	*e_owner;	/* thread holding this kstat */
1220Sstevel@tonic-gate 	kcondvar_t	e_cv;		/* wait for owner == NULL */
1230Sstevel@tonic-gate 	avl_node_t	e_avl_bykid;	/* AVL tree to sort by KID */
1240Sstevel@tonic-gate 	avl_node_t	e_avl_byname;	/* AVL tree to sort by name */
1250Sstevel@tonic-gate 	kstat_zone_t	e_zone;		/* zone to export stats to */
1260Sstevel@tonic-gate } ekstat_t;
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate static uint64_t kstat_initial[8192];
1290Sstevel@tonic-gate static void *kstat_initial_ptr = kstat_initial;
1300Sstevel@tonic-gate static size_t kstat_initial_avail = sizeof (kstat_initial);
1310Sstevel@tonic-gate static vmem_t *kstat_arena;
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate #define	KSTAT_ALIGN	(sizeof (uint64_t))
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate static avl_tree_t kstat_avl_bykid;
1360Sstevel@tonic-gate static avl_tree_t kstat_avl_byname;
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate /*
1390Sstevel@tonic-gate  * Various pointers we need to create kstats at boot time in kstat_init()
1400Sstevel@tonic-gate  */
1410Sstevel@tonic-gate extern	kstat_named_t	*segmapcnt_ptr;
1420Sstevel@tonic-gate extern	uint_t		segmapcnt_ndata;
1430Sstevel@tonic-gate extern	int		segmap_kstat_update(kstat_t *, int);
1440Sstevel@tonic-gate extern	kstat_named_t	*biostats_ptr;
1450Sstevel@tonic-gate extern	uint_t		biostats_ndata;
1460Sstevel@tonic-gate extern	kstat_named_t	*pollstats_ptr;
1470Sstevel@tonic-gate extern	uint_t		pollstats_ndata;
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate extern	int	vac;
1500Sstevel@tonic-gate extern	uint_t	nproc;
1510Sstevel@tonic-gate extern	time_t	boot_time;
1520Sstevel@tonic-gate extern	sysinfo_t	sysinfo;
1530Sstevel@tonic-gate extern	vminfo_t	vminfo;
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate struct {
1560Sstevel@tonic-gate 	kstat_named_t ncpus;
1570Sstevel@tonic-gate 	kstat_named_t lbolt;
1580Sstevel@tonic-gate 	kstat_named_t deficit;
1590Sstevel@tonic-gate 	kstat_named_t clk_intr;
1600Sstevel@tonic-gate 	kstat_named_t vac;
1610Sstevel@tonic-gate 	kstat_named_t nproc;
1620Sstevel@tonic-gate 	kstat_named_t avenrun_1min;
1630Sstevel@tonic-gate 	kstat_named_t avenrun_5min;
1640Sstevel@tonic-gate 	kstat_named_t avenrun_15min;
1650Sstevel@tonic-gate 	kstat_named_t boot_time;
1660Sstevel@tonic-gate } system_misc_kstat = {
1670Sstevel@tonic-gate 	{ "ncpus",		KSTAT_DATA_UINT32 },
1680Sstevel@tonic-gate 	{ "lbolt",		KSTAT_DATA_UINT32 },
1690Sstevel@tonic-gate 	{ "deficit",		KSTAT_DATA_UINT32 },
1700Sstevel@tonic-gate 	{ "clk_intr",		KSTAT_DATA_UINT32 },
1710Sstevel@tonic-gate 	{ "vac",		KSTAT_DATA_UINT32 },
1720Sstevel@tonic-gate 	{ "nproc",		KSTAT_DATA_UINT32 },
1730Sstevel@tonic-gate 	{ "avenrun_1min",	KSTAT_DATA_UINT32 },
1740Sstevel@tonic-gate 	{ "avenrun_5min",	KSTAT_DATA_UINT32 },
1750Sstevel@tonic-gate 	{ "avenrun_15min",	KSTAT_DATA_UINT32 },
1760Sstevel@tonic-gate 	{ "boot_time",		KSTAT_DATA_UINT32 },
1770Sstevel@tonic-gate };
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate struct {
1800Sstevel@tonic-gate 	kstat_named_t physmem;
1810Sstevel@tonic-gate 	kstat_named_t nalloc;
1820Sstevel@tonic-gate 	kstat_named_t nfree;
1830Sstevel@tonic-gate 	kstat_named_t nalloc_calls;
1840Sstevel@tonic-gate 	kstat_named_t nfree_calls;
1850Sstevel@tonic-gate 	kstat_named_t kernelbase;
1860Sstevel@tonic-gate 	kstat_named_t econtig;
1870Sstevel@tonic-gate 	kstat_named_t freemem;
1880Sstevel@tonic-gate 	kstat_named_t availrmem;
1890Sstevel@tonic-gate 	kstat_named_t lotsfree;
1900Sstevel@tonic-gate 	kstat_named_t desfree;
1910Sstevel@tonic-gate 	kstat_named_t minfree;
1920Sstevel@tonic-gate 	kstat_named_t fastscan;
1930Sstevel@tonic-gate 	kstat_named_t slowscan;
1940Sstevel@tonic-gate 	kstat_named_t nscan;
1950Sstevel@tonic-gate 	kstat_named_t desscan;
1960Sstevel@tonic-gate 	kstat_named_t pp_kernel;
1970Sstevel@tonic-gate 	kstat_named_t pagesfree;
1980Sstevel@tonic-gate 	kstat_named_t pageslocked;
1990Sstevel@tonic-gate 	kstat_named_t pagestotal;
2000Sstevel@tonic-gate } system_pages_kstat = {
2010Sstevel@tonic-gate 	{ "physmem",		KSTAT_DATA_ULONG },
2020Sstevel@tonic-gate 	{ "nalloc",		KSTAT_DATA_ULONG },
2030Sstevel@tonic-gate 	{ "nfree",		KSTAT_DATA_ULONG },
2040Sstevel@tonic-gate 	{ "nalloc_calls",	KSTAT_DATA_ULONG },
2050Sstevel@tonic-gate 	{ "nfree_calls",	KSTAT_DATA_ULONG },
2060Sstevel@tonic-gate 	{ "kernelbase",		KSTAT_DATA_ULONG },
2070Sstevel@tonic-gate 	{ "econtig", 		KSTAT_DATA_ULONG },
2080Sstevel@tonic-gate 	{ "freemem", 		KSTAT_DATA_ULONG },
2090Sstevel@tonic-gate 	{ "availrmem", 		KSTAT_DATA_ULONG },
2100Sstevel@tonic-gate 	{ "lotsfree", 		KSTAT_DATA_ULONG },
2110Sstevel@tonic-gate 	{ "desfree", 		KSTAT_DATA_ULONG },
2120Sstevel@tonic-gate 	{ "minfree", 		KSTAT_DATA_ULONG },
2130Sstevel@tonic-gate 	{ "fastscan", 		KSTAT_DATA_ULONG },
2140Sstevel@tonic-gate 	{ "slowscan", 		KSTAT_DATA_ULONG },
2150Sstevel@tonic-gate 	{ "nscan", 		KSTAT_DATA_ULONG },
2160Sstevel@tonic-gate 	{ "desscan", 		KSTAT_DATA_ULONG },
2170Sstevel@tonic-gate 	{ "pp_kernel", 		KSTAT_DATA_ULONG },
2180Sstevel@tonic-gate 	{ "pagesfree", 		KSTAT_DATA_ULONG },
2190Sstevel@tonic-gate 	{ "pageslocked", 	KSTAT_DATA_ULONG },
2200Sstevel@tonic-gate 	{ "pagestotal",		KSTAT_DATA_ULONG },
2210Sstevel@tonic-gate };
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate static int header_kstat_update(kstat_t *, int);
2240Sstevel@tonic-gate static int header_kstat_snapshot(kstat_t *, void *, int);
2250Sstevel@tonic-gate static int system_misc_kstat_update(kstat_t *, int);
2260Sstevel@tonic-gate static int system_pages_kstat_update(kstat_t *, int);
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate static struct {
2290Sstevel@tonic-gate 	char	name[KSTAT_STRLEN];
2300Sstevel@tonic-gate 	size_t	size;
2310Sstevel@tonic-gate 	uint_t	min_ndata;
2320Sstevel@tonic-gate 	uint_t	max_ndata;
2330Sstevel@tonic-gate } kstat_data_type[KSTAT_NUM_TYPES] = {
2340Sstevel@tonic-gate 	{ "raw",		1,			0,	INT_MAX	},
2350Sstevel@tonic-gate 	{ "name=value",		sizeof (kstat_named_t),	0,	INT_MAX	},
2360Sstevel@tonic-gate 	{ "interrupt",		sizeof (kstat_intr_t),	1,	1	},
2370Sstevel@tonic-gate 	{ "i/o",		sizeof (kstat_io_t),	1,	1	},
2380Sstevel@tonic-gate 	{ "event_timer",	sizeof (kstat_timer_t),	0,	INT_MAX	},
2390Sstevel@tonic-gate };
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate int
2420Sstevel@tonic-gate kstat_zone_find(kstat_t *k, zoneid_t zoneid)
2430Sstevel@tonic-gate {
2440Sstevel@tonic-gate 	ekstat_t *e = (ekstat_t *)k;
2450Sstevel@tonic-gate 	kstat_zone_t *kz;
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&kstat_chain_lock));
2480Sstevel@tonic-gate 	for (kz = &e->e_zone; kz != NULL; kz = kz->next) {
2490Sstevel@tonic-gate 		if (zoneid == ALL_ZONES || kz->zoneid == ALL_ZONES)
2500Sstevel@tonic-gate 			return (1);
2510Sstevel@tonic-gate 		if (zoneid == kz->zoneid)
2520Sstevel@tonic-gate 			return (1);
2530Sstevel@tonic-gate 	}
2540Sstevel@tonic-gate 	return (0);
2550Sstevel@tonic-gate }
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate void
2580Sstevel@tonic-gate kstat_zone_remove(kstat_t *k, zoneid_t zoneid)
2590Sstevel@tonic-gate {
2600Sstevel@tonic-gate 	ekstat_t *e = (ekstat_t *)k;
2610Sstevel@tonic-gate 	kstat_zone_t *kz, *t = NULL;
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate 	mutex_enter(&kstat_chain_lock);
2640Sstevel@tonic-gate 	if (zoneid == e->e_zone.zoneid) {
2650Sstevel@tonic-gate 		kz = e->e_zone.next;
2660Sstevel@tonic-gate 		ASSERT(kz != NULL);
2670Sstevel@tonic-gate 		e->e_zone.zoneid = kz->zoneid;
2680Sstevel@tonic-gate 		e->e_zone.next = kz->next;
2690Sstevel@tonic-gate 		goto out;
2700Sstevel@tonic-gate 	}
2710Sstevel@tonic-gate 	for (kz = &e->e_zone; kz->next != NULL; kz = kz->next) {
2720Sstevel@tonic-gate 		if (kz->next->zoneid == zoneid) {
2730Sstevel@tonic-gate 			t = kz->next;
2740Sstevel@tonic-gate 			kz->next = t->next;
2750Sstevel@tonic-gate 			break;
2760Sstevel@tonic-gate 		}
2770Sstevel@tonic-gate 	}
2780Sstevel@tonic-gate 	ASSERT(t != NULL);	/* we removed something */
2790Sstevel@tonic-gate 	kz = t;
2800Sstevel@tonic-gate out:
2810Sstevel@tonic-gate 	kstat_chain_id++;
2820Sstevel@tonic-gate 	mutex_exit(&kstat_chain_lock);
2830Sstevel@tonic-gate 	kmem_free(kz, sizeof (*kz));
2840Sstevel@tonic-gate }
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate void
2870Sstevel@tonic-gate kstat_zone_add(kstat_t *k, zoneid_t zoneid)
2880Sstevel@tonic-gate {
2890Sstevel@tonic-gate 	ekstat_t *e = (ekstat_t *)k;
2900Sstevel@tonic-gate 	kstat_zone_t *kz;
2910Sstevel@tonic-gate 
292*3792Sakolb 	kz = kmem_alloc(sizeof (*kz), KM_NOSLEEP);
293*3792Sakolb 	if (kz == NULL)
294*3792Sakolb 		return;
2950Sstevel@tonic-gate 	mutex_enter(&kstat_chain_lock);
2960Sstevel@tonic-gate 	kz->zoneid = zoneid;
2970Sstevel@tonic-gate 	kz->next = e->e_zone.next;
2980Sstevel@tonic-gate 	e->e_zone.next = kz;
2990Sstevel@tonic-gate 	kstat_chain_id++;
3000Sstevel@tonic-gate 	mutex_exit(&kstat_chain_lock);
3010Sstevel@tonic-gate }
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate /*
3040Sstevel@tonic-gate  * Compare the list of zones for the given kstats, returning 0 if they match
3050Sstevel@tonic-gate  * (ie, one list contains ALL_ZONES or both lists contain the same zoneid).
3060Sstevel@tonic-gate  * In practice, this is called indirectly by kstat_hold_byname(), so one of the
3070Sstevel@tonic-gate  * two lists always has one element, and this is an O(n) operation rather than
3080Sstevel@tonic-gate  * O(n^2).
3090Sstevel@tonic-gate  */
3100Sstevel@tonic-gate static int
3110Sstevel@tonic-gate kstat_zone_compare(ekstat_t *e1, ekstat_t *e2)
3120Sstevel@tonic-gate {
3130Sstevel@tonic-gate 	kstat_zone_t *kz1, *kz2;
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&kstat_chain_lock));
3160Sstevel@tonic-gate 	for (kz1 = &e1->e_zone; kz1 != NULL; kz1 = kz1->next) {
3170Sstevel@tonic-gate 		for (kz2 = &e2->e_zone; kz2 != NULL; kz2 = kz2->next) {
3180Sstevel@tonic-gate 			if (kz1->zoneid == ALL_ZONES ||
3190Sstevel@tonic-gate 			    kz2->zoneid == ALL_ZONES)
3200Sstevel@tonic-gate 				return (0);
3210Sstevel@tonic-gate 			if (kz1->zoneid == kz2->zoneid)
3220Sstevel@tonic-gate 				return (0);
3230Sstevel@tonic-gate 		}
3240Sstevel@tonic-gate 	}
3250Sstevel@tonic-gate 	return (e1->e_zone.zoneid < e2->e_zone.zoneid ? -1 : 1);
3260Sstevel@tonic-gate }
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate /*
3290Sstevel@tonic-gate  * Support for keeping kstats sorted in AVL trees for fast lookups.
3300Sstevel@tonic-gate  */
3310Sstevel@tonic-gate static int
3320Sstevel@tonic-gate kstat_compare_bykid(const void *a1, const void *a2)
3330Sstevel@tonic-gate {
3340Sstevel@tonic-gate 	const kstat_t *k1 = a1;
3350Sstevel@tonic-gate 	const kstat_t *k2 = a2;
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 	if (k1->ks_kid < k2->ks_kid)
3380Sstevel@tonic-gate 		return (-1);
3390Sstevel@tonic-gate 	if (k1->ks_kid > k2->ks_kid)
3400Sstevel@tonic-gate 		return (1);
3410Sstevel@tonic-gate 	return (kstat_zone_compare((ekstat_t *)k1, (ekstat_t *)k2));
3420Sstevel@tonic-gate }
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate static int
3450Sstevel@tonic-gate kstat_compare_byname(const void *a1, const void *a2)
3460Sstevel@tonic-gate {
3470Sstevel@tonic-gate 	const kstat_t *k1 = a1;
3480Sstevel@tonic-gate 	const kstat_t *k2 = a2;
3490Sstevel@tonic-gate 	int s;
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 	s = strcmp(k1->ks_module, k2->ks_module);
3520Sstevel@tonic-gate 	if (s > 0)
3530Sstevel@tonic-gate 		return (1);
3540Sstevel@tonic-gate 	if (s < 0)
3550Sstevel@tonic-gate 		return (-1);
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate 	if (k1->ks_instance < k2->ks_instance)
3580Sstevel@tonic-gate 		return (-1);
3590Sstevel@tonic-gate 	if (k1->ks_instance > k2->ks_instance)
3600Sstevel@tonic-gate 		return (1);
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 	s = strcmp(k1->ks_name, k2->ks_name);
3630Sstevel@tonic-gate 	if (s > 0)
3640Sstevel@tonic-gate 		return (1);
3650Sstevel@tonic-gate 	if (s < 0)
3660Sstevel@tonic-gate 		return (-1);
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 	return (kstat_zone_compare((ekstat_t *)k1, (ekstat_t *)k2));
3690Sstevel@tonic-gate }
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate static kstat_t *
3720Sstevel@tonic-gate kstat_hold(avl_tree_t *t, ekstat_t *template)
3730Sstevel@tonic-gate {
3740Sstevel@tonic-gate 	kstat_t *ksp;
3750Sstevel@tonic-gate 	ekstat_t *e;
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate 	mutex_enter(&kstat_chain_lock);
3780Sstevel@tonic-gate 	for (;;) {
3790Sstevel@tonic-gate 		ksp = avl_find(t, template, NULL);
3800Sstevel@tonic-gate 		if (ksp == NULL)
3810Sstevel@tonic-gate 			break;
3820Sstevel@tonic-gate 		e = (ekstat_t *)ksp;
3830Sstevel@tonic-gate 		if (e->e_owner == NULL) {
3840Sstevel@tonic-gate 			e->e_owner = curthread;
3850Sstevel@tonic-gate 			break;
3860Sstevel@tonic-gate 		}
3870Sstevel@tonic-gate 		cv_wait(&e->e_cv, &kstat_chain_lock);
3880Sstevel@tonic-gate 	}
3890Sstevel@tonic-gate 	mutex_exit(&kstat_chain_lock);
3900Sstevel@tonic-gate 	return (ksp);
3910Sstevel@tonic-gate }
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate void
3940Sstevel@tonic-gate kstat_rele(kstat_t *ksp)
3950Sstevel@tonic-gate {
3960Sstevel@tonic-gate 	ekstat_t *e = (ekstat_t *)ksp;
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate 	mutex_enter(&kstat_chain_lock);
3990Sstevel@tonic-gate 	ASSERT(e->e_owner == curthread);
4000Sstevel@tonic-gate 	e->e_owner = NULL;
4010Sstevel@tonic-gate 	cv_broadcast(&e->e_cv);
4020Sstevel@tonic-gate 	mutex_exit(&kstat_chain_lock);
4030Sstevel@tonic-gate }
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate kstat_t *
4060Sstevel@tonic-gate kstat_hold_bykid(kid_t kid, zoneid_t zoneid)
4070Sstevel@tonic-gate {
4080Sstevel@tonic-gate 	ekstat_t e;
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate 	e.e_ks.ks_kid = kid;
4110Sstevel@tonic-gate 	e.e_zone.zoneid = zoneid;
4120Sstevel@tonic-gate 	e.e_zone.next = NULL;
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate 	return (kstat_hold(&kstat_avl_bykid, &e));
4150Sstevel@tonic-gate }
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate kstat_t *
4182951Selowe kstat_hold_byname(const char *ks_module, int ks_instance, const char *ks_name,
4190Sstevel@tonic-gate     zoneid_t ks_zoneid)
4200Sstevel@tonic-gate {
4210Sstevel@tonic-gate 	ekstat_t e;
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate 	kstat_set_string(e.e_ks.ks_module, ks_module);
4240Sstevel@tonic-gate 	e.e_ks.ks_instance = ks_instance;
4250Sstevel@tonic-gate 	kstat_set_string(e.e_ks.ks_name, ks_name);
4260Sstevel@tonic-gate 	e.e_zone.zoneid = ks_zoneid;
4270Sstevel@tonic-gate 	e.e_zone.next = NULL;
4280Sstevel@tonic-gate 	return (kstat_hold(&kstat_avl_byname, &e));
4290Sstevel@tonic-gate }
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate static ekstat_t *
4320Sstevel@tonic-gate kstat_alloc(size_t size)
4330Sstevel@tonic-gate {
4340Sstevel@tonic-gate 	ekstat_t *e = NULL;
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate 	size = P2ROUNDUP(sizeof (ekstat_t) + size, KSTAT_ALIGN);
4370Sstevel@tonic-gate 
4380Sstevel@tonic-gate 	if (kstat_arena == NULL) {
4390Sstevel@tonic-gate 		if (size <= kstat_initial_avail) {
4400Sstevel@tonic-gate 			e = kstat_initial_ptr;
4410Sstevel@tonic-gate 			kstat_initial_ptr = (char *)kstat_initial_ptr + size;
4420Sstevel@tonic-gate 			kstat_initial_avail -= size;
4430Sstevel@tonic-gate 		}
4440Sstevel@tonic-gate 	} else {
4450Sstevel@tonic-gate 		e = vmem_alloc(kstat_arena, size, VM_NOSLEEP);
4460Sstevel@tonic-gate 	}
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate 	if (e != NULL) {
4490Sstevel@tonic-gate 		bzero(e, size);
4500Sstevel@tonic-gate 		e->e_size = size;
4510Sstevel@tonic-gate 		cv_init(&e->e_cv, NULL, CV_DEFAULT, NULL);
4520Sstevel@tonic-gate 	}
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate 	return (e);
4550Sstevel@tonic-gate }
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate static void
4580Sstevel@tonic-gate kstat_free(ekstat_t *e)
4590Sstevel@tonic-gate {
4600Sstevel@tonic-gate 	cv_destroy(&e->e_cv);
4610Sstevel@tonic-gate 	vmem_free(kstat_arena, e, e->e_size);
4620Sstevel@tonic-gate }
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate /*
4650Sstevel@tonic-gate  * Create various system kstats.
4660Sstevel@tonic-gate  */
4670Sstevel@tonic-gate void
4680Sstevel@tonic-gate kstat_init(void)
4690Sstevel@tonic-gate {
4700Sstevel@tonic-gate 	kstat_t *ksp;
4710Sstevel@tonic-gate 	ekstat_t *e;
4720Sstevel@tonic-gate 	avl_tree_t *t = &kstat_avl_bykid;
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 	/*
4750Sstevel@tonic-gate 	 * Set up the kstat vmem arena.
4760Sstevel@tonic-gate 	 */
4770Sstevel@tonic-gate 	kstat_arena = vmem_create("kstat",
4780Sstevel@tonic-gate 	    kstat_initial, sizeof (kstat_initial), KSTAT_ALIGN,
4790Sstevel@tonic-gate 	    segkmem_alloc, segkmem_free, heap_arena, 0, VM_SLEEP);
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate 	/*
4820Sstevel@tonic-gate 	 * Make initial kstats appear as though they were allocated.
4830Sstevel@tonic-gate 	 */
4840Sstevel@tonic-gate 	for (e = avl_first(t); e != NULL; e = avl_walk(t, e, AVL_AFTER))
4850Sstevel@tonic-gate 		(void) vmem_xalloc(kstat_arena, e->e_size, KSTAT_ALIGN,
4860Sstevel@tonic-gate 		    0, 0, e, (char *)e + e->e_size,
4870Sstevel@tonic-gate 		    VM_NOSLEEP | VM_BESTFIT | VM_PANIC);
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 	/*
4900Sstevel@tonic-gate 	 * The mother of all kstats.  The first kstat in the system, which
4910Sstevel@tonic-gate 	 * always has KID 0, has the headers for all kstats (including itself)
4920Sstevel@tonic-gate 	 * as its data.  Thus, the kstat driver does not need any special
4930Sstevel@tonic-gate 	 * interface to extract the kstat chain.
4940Sstevel@tonic-gate 	 */
4950Sstevel@tonic-gate 	kstat_chain_id = 0;
4960Sstevel@tonic-gate 	ksp = kstat_create("unix", 0, "kstat_headers", "kstat", KSTAT_TYPE_RAW,
4970Sstevel@tonic-gate 		0, KSTAT_FLAG_VIRTUAL | KSTAT_FLAG_VAR_SIZE);
4980Sstevel@tonic-gate 	if (ksp) {
4990Sstevel@tonic-gate 		ksp->ks_lock = &kstat_chain_lock;
5000Sstevel@tonic-gate 		ksp->ks_update = header_kstat_update;
5010Sstevel@tonic-gate 		ksp->ks_snapshot = header_kstat_snapshot;
5020Sstevel@tonic-gate 		kstat_install(ksp);
5030Sstevel@tonic-gate 	} else {
5040Sstevel@tonic-gate 		panic("cannot create kstat 'kstat_headers'");
5050Sstevel@tonic-gate 	}
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate 	ksp = kstat_create("unix", 0, "kstat_types", "kstat",
5080Sstevel@tonic-gate 		KSTAT_TYPE_NAMED, KSTAT_NUM_TYPES, 0);
5090Sstevel@tonic-gate 	if (ksp) {
5100Sstevel@tonic-gate 		int i;
5110Sstevel@tonic-gate 		kstat_named_t *kn = KSTAT_NAMED_PTR(ksp);
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate 		for (i = 0; i < KSTAT_NUM_TYPES; i++) {
5140Sstevel@tonic-gate 			kstat_named_init(&kn[i], kstat_data_type[i].name,
5150Sstevel@tonic-gate 				KSTAT_DATA_ULONG);
5160Sstevel@tonic-gate 			kn[i].value.ul = i;
5170Sstevel@tonic-gate 		}
5180Sstevel@tonic-gate 		kstat_install(ksp);
5190Sstevel@tonic-gate 	}
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate 	ksp = kstat_create("unix", 0, "sysinfo", "misc", KSTAT_TYPE_RAW,
5220Sstevel@tonic-gate 		sizeof (sysinfo_t), KSTAT_FLAG_VIRTUAL);
5230Sstevel@tonic-gate 	if (ksp) {
5240Sstevel@tonic-gate 		ksp->ks_data = (void *) &sysinfo;
5250Sstevel@tonic-gate 		kstat_install(ksp);
5260Sstevel@tonic-gate 	}
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate 	ksp = kstat_create("unix", 0, "vminfo", "vm", KSTAT_TYPE_RAW,
5290Sstevel@tonic-gate 		sizeof (vminfo_t), KSTAT_FLAG_VIRTUAL);
5300Sstevel@tonic-gate 	if (ksp) {
5310Sstevel@tonic-gate 		ksp->ks_data = (void *) &vminfo;
5320Sstevel@tonic-gate 		kstat_install(ksp);
5330Sstevel@tonic-gate 	}
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate 	ksp = kstat_create("unix", 0, "segmap", "vm", KSTAT_TYPE_NAMED,
5360Sstevel@tonic-gate 		segmapcnt_ndata, KSTAT_FLAG_VIRTUAL);
5370Sstevel@tonic-gate 	if (ksp) {
5380Sstevel@tonic-gate 		ksp->ks_data = (void *) segmapcnt_ptr;
5390Sstevel@tonic-gate 		ksp->ks_update = segmap_kstat_update;
5400Sstevel@tonic-gate 		kstat_install(ksp);
5410Sstevel@tonic-gate 	}
5420Sstevel@tonic-gate 
5430Sstevel@tonic-gate 	ksp = kstat_create("unix", 0, "biostats", "misc", KSTAT_TYPE_NAMED,
5440Sstevel@tonic-gate 		biostats_ndata, KSTAT_FLAG_VIRTUAL);
5450Sstevel@tonic-gate 	if (ksp) {
5460Sstevel@tonic-gate 		ksp->ks_data = (void *) biostats_ptr;
5470Sstevel@tonic-gate 		kstat_install(ksp);
5480Sstevel@tonic-gate 	}
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate #ifdef VAC
5510Sstevel@tonic-gate 	ksp = kstat_create("unix", 0, "flushmeter", "hat", KSTAT_TYPE_RAW,
5520Sstevel@tonic-gate 		sizeof (struct flushmeter), KSTAT_FLAG_VIRTUAL);
5530Sstevel@tonic-gate 	if (ksp) {
5540Sstevel@tonic-gate 		ksp->ks_data = (void *) &flush_cnt;
5550Sstevel@tonic-gate 		kstat_install(ksp);
5560Sstevel@tonic-gate 	}
5570Sstevel@tonic-gate #endif	/* VAC */
5580Sstevel@tonic-gate 
5590Sstevel@tonic-gate 	ksp = kstat_create("unix", 0, "var", "misc", KSTAT_TYPE_RAW,
5600Sstevel@tonic-gate 		sizeof (struct var), KSTAT_FLAG_VIRTUAL);
5610Sstevel@tonic-gate 	if (ksp) {
5620Sstevel@tonic-gate 		ksp->ks_data = (void *) &v;
5630Sstevel@tonic-gate 		kstat_install(ksp);
5640Sstevel@tonic-gate 	}
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate 	ksp = kstat_create("unix", 0, "system_misc", "misc", KSTAT_TYPE_NAMED,
5670Sstevel@tonic-gate 		sizeof (system_misc_kstat) / sizeof (kstat_named_t),
5680Sstevel@tonic-gate 		KSTAT_FLAG_VIRTUAL);
5690Sstevel@tonic-gate 	if (ksp) {
5700Sstevel@tonic-gate 		ksp->ks_data = (void *) &system_misc_kstat;
5710Sstevel@tonic-gate 		ksp->ks_update = system_misc_kstat_update;
5720Sstevel@tonic-gate 		kstat_install(ksp);
5730Sstevel@tonic-gate 	}
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate 	ksp = kstat_create("unix", 0, "system_pages", "pages", KSTAT_TYPE_NAMED,
5760Sstevel@tonic-gate 		sizeof (system_pages_kstat) / sizeof (kstat_named_t),
5770Sstevel@tonic-gate 		KSTAT_FLAG_VIRTUAL);
5780Sstevel@tonic-gate 	if (ksp) {
5790Sstevel@tonic-gate 		ksp->ks_data = (void *) &system_pages_kstat;
5800Sstevel@tonic-gate 		ksp->ks_update = system_pages_kstat_update;
5810Sstevel@tonic-gate 		kstat_install(ksp);
5820Sstevel@tonic-gate 	}
5830Sstevel@tonic-gate 
5840Sstevel@tonic-gate 	ksp = kstat_create("poll", 0, "pollstats", "misc", KSTAT_TYPE_NAMED,
5850Sstevel@tonic-gate 	    pollstats_ndata, KSTAT_FLAG_VIRTUAL | KSTAT_FLAG_WRITABLE);
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate 	if (ksp) {
5880Sstevel@tonic-gate 		ksp->ks_data = pollstats_ptr;
5890Sstevel@tonic-gate 		kstat_install(ksp);
5900Sstevel@tonic-gate 	}
5910Sstevel@tonic-gate }
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate /*
5940Sstevel@tonic-gate  * Caller of this should ensure that the string pointed by src
5950Sstevel@tonic-gate  * doesn't change while kstat's lock is held. Not doing so defeats
5960Sstevel@tonic-gate  * kstat's snapshot strategy as explained in <sys/kstat.h>
5970Sstevel@tonic-gate  */
5980Sstevel@tonic-gate void
5990Sstevel@tonic-gate kstat_named_setstr(kstat_named_t *knp, const char *src)
6000Sstevel@tonic-gate {
6010Sstevel@tonic-gate 	if (knp->data_type != KSTAT_DATA_STRING)
6020Sstevel@tonic-gate 		panic("kstat_named_setstr('%p', '%p'): "
6030Sstevel@tonic-gate 		    "named kstat is not of type KSTAT_DATA_STRING", knp, src);
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate 	KSTAT_NAMED_STR_PTR(knp) = (char *)src;
6060Sstevel@tonic-gate 	if (src != NULL)
6070Sstevel@tonic-gate 		KSTAT_NAMED_STR_BUFLEN(knp) = strlen(src) + 1;
6080Sstevel@tonic-gate 	else
6090Sstevel@tonic-gate 		KSTAT_NAMED_STR_BUFLEN(knp) = 0;
6100Sstevel@tonic-gate }
6110Sstevel@tonic-gate 
6120Sstevel@tonic-gate void
6132951Selowe kstat_set_string(char *dst, const char *src)
6140Sstevel@tonic-gate {
6150Sstevel@tonic-gate 	bzero(dst, KSTAT_STRLEN);
6160Sstevel@tonic-gate 	(void) strncpy(dst, src, KSTAT_STRLEN - 1);
6170Sstevel@tonic-gate }
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate void
6202951Selowe kstat_named_init(kstat_named_t *knp, const char *name, uchar_t data_type)
6210Sstevel@tonic-gate {
6220Sstevel@tonic-gate 	kstat_set_string(knp->name, name);
6230Sstevel@tonic-gate 	knp->data_type = data_type;
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate 	if (data_type == KSTAT_DATA_STRING)
6260Sstevel@tonic-gate 		kstat_named_setstr(knp, NULL);
6270Sstevel@tonic-gate }
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate void
6302951Selowe kstat_timer_init(kstat_timer_t *ktp, const char *name)
6310Sstevel@tonic-gate {
6320Sstevel@tonic-gate 	kstat_set_string(ktp->name, name);
6330Sstevel@tonic-gate }
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate /* ARGSUSED */
6360Sstevel@tonic-gate static int
6370Sstevel@tonic-gate default_kstat_update(kstat_t *ksp, int rw)
6380Sstevel@tonic-gate {
6390Sstevel@tonic-gate 	uint_t i;
6400Sstevel@tonic-gate 	size_t len = 0;
6410Sstevel@tonic-gate 	kstat_named_t *knp;
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate 	/*
6440Sstevel@tonic-gate 	 * Named kstats with variable-length long strings have a standard
6450Sstevel@tonic-gate 	 * way of determining how much space is needed to hold the snapshot:
6460Sstevel@tonic-gate 	 */
6470Sstevel@tonic-gate 	if (ksp->ks_data != NULL && ksp->ks_type == KSTAT_TYPE_NAMED &&
6480Sstevel@tonic-gate 	    (ksp->ks_flags & KSTAT_FLAG_VAR_SIZE)) {
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate 		/*
6510Sstevel@tonic-gate 		 * Add in the space required for the strings
6520Sstevel@tonic-gate 		 */
6530Sstevel@tonic-gate 		knp = KSTAT_NAMED_PTR(ksp);
6540Sstevel@tonic-gate 		for (i = 0; i < ksp->ks_ndata; i++, knp++) {
6550Sstevel@tonic-gate 			if (knp->data_type == KSTAT_DATA_STRING)
6560Sstevel@tonic-gate 				len += KSTAT_NAMED_STR_BUFLEN(knp);
6570Sstevel@tonic-gate 		}
6580Sstevel@tonic-gate 		ksp->ks_data_size =
6590Sstevel@tonic-gate 		    ksp->ks_ndata * sizeof (kstat_named_t) + len;
6600Sstevel@tonic-gate 	}
6610Sstevel@tonic-gate 	return (0);
6620Sstevel@tonic-gate }
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate static int
6650Sstevel@tonic-gate default_kstat_snapshot(kstat_t *ksp, void *buf, int rw)
6660Sstevel@tonic-gate {
6670Sstevel@tonic-gate 	kstat_io_t *kiop;
6680Sstevel@tonic-gate 	hrtime_t cur_time;
6690Sstevel@tonic-gate 	size_t	namedsz;
6700Sstevel@tonic-gate 
6710Sstevel@tonic-gate 	ksp->ks_snaptime = cur_time = gethrtime();
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate 	if (rw == KSTAT_WRITE) {
6740Sstevel@tonic-gate 		if (!(ksp->ks_flags & KSTAT_FLAG_WRITABLE))
6750Sstevel@tonic-gate 			return (EACCES);
6760Sstevel@tonic-gate 		bcopy(buf, ksp->ks_data, ksp->ks_data_size);
6770Sstevel@tonic-gate 		return (0);
6780Sstevel@tonic-gate 	}
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate 	/*
6810Sstevel@tonic-gate 	 * KSTAT_TYPE_NAMED kstats are defined to have ks_ndata
6820Sstevel@tonic-gate 	 * number of kstat_named_t structures, followed by an optional
6830Sstevel@tonic-gate 	 * string segment. The ks_data generally holds only the
6840Sstevel@tonic-gate 	 * kstat_named_t structures. So we copy it first. The strings,
6850Sstevel@tonic-gate 	 * if any, are copied below. For other kstat types, ks_data holds the
6860Sstevel@tonic-gate 	 * entire buffer.
6870Sstevel@tonic-gate 	 */
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate 	namedsz = sizeof (kstat_named_t) * ksp->ks_ndata;
6900Sstevel@tonic-gate 	if (ksp->ks_type == KSTAT_TYPE_NAMED && ksp->ks_data_size > namedsz)
6910Sstevel@tonic-gate 		bcopy(ksp->ks_data, buf, namedsz);
6920Sstevel@tonic-gate 	else
6930Sstevel@tonic-gate 		bcopy(ksp->ks_data, buf, ksp->ks_data_size);
6940Sstevel@tonic-gate 
6950Sstevel@tonic-gate 	/*
6960Sstevel@tonic-gate 	 * Apply kstat type-specific data massaging
6970Sstevel@tonic-gate 	 */
6980Sstevel@tonic-gate 	switch (ksp->ks_type) {
6990Sstevel@tonic-gate 
7000Sstevel@tonic-gate 	case KSTAT_TYPE_IO:
7010Sstevel@tonic-gate 		/*
7020Sstevel@tonic-gate 		 * Normalize time units and deal with incomplete transactions
7030Sstevel@tonic-gate 		 */
7040Sstevel@tonic-gate 		kiop = (kstat_io_t *)buf;
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate 		scalehrtime(&kiop->wtime);
7070Sstevel@tonic-gate 		scalehrtime(&kiop->wlentime);
7080Sstevel@tonic-gate 		scalehrtime(&kiop->wlastupdate);
7090Sstevel@tonic-gate 		scalehrtime(&kiop->rtime);
7100Sstevel@tonic-gate 		scalehrtime(&kiop->rlentime);
7110Sstevel@tonic-gate 		scalehrtime(&kiop->rlastupdate);
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate 		if (kiop->wcnt != 0) {
7142723Scth 			/* like kstat_waitq_exit */
7150Sstevel@tonic-gate 			hrtime_t wfix = cur_time - kiop->wlastupdate;
7162723Scth 			kiop->wlastupdate = cur_time;
7170Sstevel@tonic-gate 			kiop->wlentime += kiop->wcnt * wfix;
7182723Scth 			kiop->wtime += wfix;
7190Sstevel@tonic-gate 		}
7202723Scth 
7210Sstevel@tonic-gate 		if (kiop->rcnt != 0) {
7222723Scth 			/* like kstat_runq_exit */
7230Sstevel@tonic-gate 			hrtime_t rfix = cur_time - kiop->rlastupdate;
7242723Scth 			kiop->rlastupdate = cur_time;
7250Sstevel@tonic-gate 			kiop->rlentime += kiop->rcnt * rfix;
7262723Scth 			kiop->rtime += rfix;
7270Sstevel@tonic-gate 		}
7280Sstevel@tonic-gate 		break;
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate 	case KSTAT_TYPE_NAMED:
7310Sstevel@tonic-gate 		/*
7320Sstevel@tonic-gate 		 * Massage any long strings in at the end of the buffer
7330Sstevel@tonic-gate 		 */
7340Sstevel@tonic-gate 		if (ksp->ks_data_size > namedsz) {
7350Sstevel@tonic-gate 			uint_t i;
7360Sstevel@tonic-gate 			kstat_named_t *knp = buf;
7370Sstevel@tonic-gate 			char *dst = (char *)(knp + ksp->ks_ndata);
7380Sstevel@tonic-gate 			/*
7390Sstevel@tonic-gate 			 * Copy strings and update pointers
7400Sstevel@tonic-gate 			 */
7410Sstevel@tonic-gate 			for (i = 0; i < ksp->ks_ndata; i++, knp++) {
7420Sstevel@tonic-gate 				if (knp->data_type == KSTAT_DATA_STRING &&
7430Sstevel@tonic-gate 				    KSTAT_NAMED_STR_PTR(knp) != NULL) {
7440Sstevel@tonic-gate 					bcopy(KSTAT_NAMED_STR_PTR(knp), dst,
7450Sstevel@tonic-gate 					    KSTAT_NAMED_STR_BUFLEN(knp));
7460Sstevel@tonic-gate 					KSTAT_NAMED_STR_PTR(knp) = dst;
7470Sstevel@tonic-gate 					dst += KSTAT_NAMED_STR_BUFLEN(knp);
7480Sstevel@tonic-gate 				}
7490Sstevel@tonic-gate 			}
7500Sstevel@tonic-gate 			ASSERT(dst <= ((char *)buf + ksp->ks_data_size));
7510Sstevel@tonic-gate 		}
7520Sstevel@tonic-gate 		break;
7530Sstevel@tonic-gate 	}
7540Sstevel@tonic-gate 	return (0);
7550Sstevel@tonic-gate }
7560Sstevel@tonic-gate 
7570Sstevel@tonic-gate static int
7580Sstevel@tonic-gate header_kstat_update(kstat_t *header_ksp, int rw)
7590Sstevel@tonic-gate {
7600Sstevel@tonic-gate 	int nkstats = 0;
7610Sstevel@tonic-gate 	ekstat_t *e;
7620Sstevel@tonic-gate 	avl_tree_t *t = &kstat_avl_bykid;
7630Sstevel@tonic-gate 	zoneid_t zoneid;
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
7660Sstevel@tonic-gate 		return (EACCES);
7670Sstevel@tonic-gate 
7680Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&kstat_chain_lock));
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate 	zoneid = getzoneid();
7710Sstevel@tonic-gate 	for (e = avl_first(t); e != NULL; e = avl_walk(t, e, AVL_AFTER)) {
7720Sstevel@tonic-gate 		if (kstat_zone_find((kstat_t *)e, zoneid)) {
7730Sstevel@tonic-gate 			nkstats++;
7740Sstevel@tonic-gate 		}
7750Sstevel@tonic-gate 	}
7760Sstevel@tonic-gate 	header_ksp->ks_ndata = nkstats;
7770Sstevel@tonic-gate 	header_ksp->ks_data_size = nkstats * sizeof (kstat_t);
7780Sstevel@tonic-gate 	return (0);
7790Sstevel@tonic-gate }
7800Sstevel@tonic-gate 
7810Sstevel@tonic-gate /*
7820Sstevel@tonic-gate  * Copy out the data section of kstat 0, which consists of the list
7830Sstevel@tonic-gate  * of all kstat headers.  By specification, these headers must be
7840Sstevel@tonic-gate  * copied out in order of increasing KID.
7850Sstevel@tonic-gate  */
7860Sstevel@tonic-gate static int
7870Sstevel@tonic-gate header_kstat_snapshot(kstat_t *header_ksp, void *buf, int rw)
7880Sstevel@tonic-gate {
7890Sstevel@tonic-gate 	ekstat_t *e;
7900Sstevel@tonic-gate 	avl_tree_t *t = &kstat_avl_bykid;
7910Sstevel@tonic-gate 	zoneid_t zoneid;
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate 	header_ksp->ks_snaptime = gethrtime();
7940Sstevel@tonic-gate 
7950Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
7960Sstevel@tonic-gate 		return (EACCES);
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&kstat_chain_lock));
7990Sstevel@tonic-gate 
8000Sstevel@tonic-gate 	zoneid = getzoneid();
8010Sstevel@tonic-gate 	for (e = avl_first(t); e != NULL; e = avl_walk(t, e, AVL_AFTER)) {
8020Sstevel@tonic-gate 		if (kstat_zone_find((kstat_t *)e, zoneid)) {
8030Sstevel@tonic-gate 			bcopy(&e->e_ks, buf, sizeof (kstat_t));
8040Sstevel@tonic-gate 			buf = (char *)buf + sizeof (kstat_t);
8050Sstevel@tonic-gate 		}
8060Sstevel@tonic-gate 	}
8070Sstevel@tonic-gate 
8080Sstevel@tonic-gate 	return (0);
8090Sstevel@tonic-gate }
8100Sstevel@tonic-gate 
8110Sstevel@tonic-gate /* ARGSUSED */
8120Sstevel@tonic-gate static int
8130Sstevel@tonic-gate system_misc_kstat_update(kstat_t *ksp, int rw)
8140Sstevel@tonic-gate {
8150Sstevel@tonic-gate 	int myncpus = ncpus;
8160Sstevel@tonic-gate 	int *loadavgp = &avenrun[0];
8170Sstevel@tonic-gate 	int loadavg[LOADAVG_NSTATS];
8180Sstevel@tonic-gate 
8190Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
8200Sstevel@tonic-gate 		return (EACCES);
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate 	if (!INGLOBALZONE(curproc)) {
8230Sstevel@tonic-gate 		/*
8240Sstevel@tonic-gate 		 * Here we grab cpu_lock which is OK as long as no-one in the
8250Sstevel@tonic-gate 		 * future attempts to lookup this particular kstat
8260Sstevel@tonic-gate 		 * (unix:0:system_misc) while holding cpu_lock.
8270Sstevel@tonic-gate 		 */
8280Sstevel@tonic-gate 		mutex_enter(&cpu_lock);
8290Sstevel@tonic-gate 		if (pool_pset_enabled()) {
8300Sstevel@tonic-gate 			psetid_t mypsid = zone_pset_get(curproc->p_zone);
8310Sstevel@tonic-gate 			int error;
8320Sstevel@tonic-gate 
8330Sstevel@tonic-gate 			myncpus = zone_ncpus_get(curproc->p_zone);
8340Sstevel@tonic-gate 			ASSERT(myncpus > 0);
8350Sstevel@tonic-gate 			error = cpupart_get_loadavg(mypsid, &loadavg[0],
8360Sstevel@tonic-gate 			    LOADAVG_NSTATS);
8370Sstevel@tonic-gate 			ASSERT(error == 0);
8380Sstevel@tonic-gate 			loadavgp = &loadavg[0];
8390Sstevel@tonic-gate 		}
8400Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
8410Sstevel@tonic-gate 	}
8420Sstevel@tonic-gate 
8430Sstevel@tonic-gate 	system_misc_kstat.ncpus.value.ui32		= (uint32_t)myncpus;
8440Sstevel@tonic-gate 	system_misc_kstat.lbolt.value.ui32		= (uint32_t)lbolt;
8450Sstevel@tonic-gate 	system_misc_kstat.deficit.value.ui32		= (uint32_t)deficit;
8460Sstevel@tonic-gate 	system_misc_kstat.clk_intr.value.ui32		= (uint32_t)lbolt;
8470Sstevel@tonic-gate 	system_misc_kstat.vac.value.ui32		= (uint32_t)vac;
8480Sstevel@tonic-gate 	system_misc_kstat.nproc.value.ui32		= (uint32_t)nproc;
8490Sstevel@tonic-gate 	system_misc_kstat.avenrun_1min.value.ui32	= (uint32_t)loadavgp[0];
8500Sstevel@tonic-gate 	system_misc_kstat.avenrun_5min.value.ui32	= (uint32_t)loadavgp[1];
8510Sstevel@tonic-gate 	system_misc_kstat.avenrun_15min.value.ui32	= (uint32_t)loadavgp[2];
8520Sstevel@tonic-gate 	system_misc_kstat.boot_time.value.ui32		= (uint32_t)boot_time;
8530Sstevel@tonic-gate 	return (0);
8540Sstevel@tonic-gate }
8550Sstevel@tonic-gate 
8560Sstevel@tonic-gate #ifdef	__sparc
8570Sstevel@tonic-gate extern caddr_t	econtig32;
8580Sstevel@tonic-gate #else	/* !__sparc */
8590Sstevel@tonic-gate extern caddr_t	econtig;
8600Sstevel@tonic-gate #endif	/* __sparc */
8610Sstevel@tonic-gate 
8620Sstevel@tonic-gate extern struct vnode kvp;
8630Sstevel@tonic-gate 
8640Sstevel@tonic-gate /* ARGSUSED */
8650Sstevel@tonic-gate static int
8660Sstevel@tonic-gate system_pages_kstat_update(kstat_t *ksp, int rw)
8670Sstevel@tonic-gate {
8680Sstevel@tonic-gate 	kobj_stat_t kobj_stat;
8690Sstevel@tonic-gate 
8700Sstevel@tonic-gate 	if (rw == KSTAT_WRITE) {
8710Sstevel@tonic-gate 		return (EACCES);
8720Sstevel@tonic-gate 	}
8730Sstevel@tonic-gate 
8740Sstevel@tonic-gate 	kobj_stat_get(&kobj_stat);
8750Sstevel@tonic-gate 	system_pages_kstat.physmem.value.ul	= (ulong_t)physmem;
8760Sstevel@tonic-gate 	system_pages_kstat.nalloc.value.ul	= kobj_stat.nalloc;
8770Sstevel@tonic-gate 	system_pages_kstat.nfree.value.ul	= kobj_stat.nfree;
8780Sstevel@tonic-gate 	system_pages_kstat.nalloc_calls.value.ul = kobj_stat.nalloc_calls;
8790Sstevel@tonic-gate 	system_pages_kstat.nfree_calls.value.ul	= kobj_stat.nfree_calls;
8800Sstevel@tonic-gate 	system_pages_kstat.kernelbase.value.ul	= (ulong_t)KERNELBASE;
8810Sstevel@tonic-gate 
8820Sstevel@tonic-gate #ifdef	__sparc
8830Sstevel@tonic-gate 	/*
8840Sstevel@tonic-gate 	 * kstat should REALLY be modified to also report kmem64_base and
8850Sstevel@tonic-gate 	 * kmem64_end (see sun4u/os/startup.c), as the virtual address range
8860Sstevel@tonic-gate 	 * [ kernelbase .. econtig ] no longer is truly reflective of the
8870Sstevel@tonic-gate 	 * kernel's vallocs...
8880Sstevel@tonic-gate 	 */
8890Sstevel@tonic-gate 	system_pages_kstat.econtig.value.ul	= (ulong_t)econtig32;
8900Sstevel@tonic-gate #else	/* !__sparc */
8910Sstevel@tonic-gate 	system_pages_kstat.econtig.value.ul	= (ulong_t)econtig;
8920Sstevel@tonic-gate #endif	/* __sparc */
8930Sstevel@tonic-gate 
8940Sstevel@tonic-gate 	system_pages_kstat.freemem.value.ul	= (ulong_t)freemem;
8950Sstevel@tonic-gate 	system_pages_kstat.availrmem.value.ul	= (ulong_t)availrmem;
8960Sstevel@tonic-gate 	system_pages_kstat.lotsfree.value.ul	= (ulong_t)lotsfree;
8970Sstevel@tonic-gate 	system_pages_kstat.desfree.value.ul	= (ulong_t)desfree;
8980Sstevel@tonic-gate 	system_pages_kstat.minfree.value.ul	= (ulong_t)minfree;
8990Sstevel@tonic-gate 	system_pages_kstat.fastscan.value.ul	= (ulong_t)fastscan;
9000Sstevel@tonic-gate 	system_pages_kstat.slowscan.value.ul	= (ulong_t)slowscan;
9010Sstevel@tonic-gate 	system_pages_kstat.nscan.value.ul	= (ulong_t)nscan;
9020Sstevel@tonic-gate 	system_pages_kstat.desscan.value.ul	= (ulong_t)desscan;
9030Sstevel@tonic-gate 	system_pages_kstat.pagesfree.value.ul	= (ulong_t)freemem;
9040Sstevel@tonic-gate 	system_pages_kstat.pageslocked.value.ul	= (ulong_t)(availrmem_initial -
9050Sstevel@tonic-gate 	    availrmem);
9060Sstevel@tonic-gate 	system_pages_kstat.pagestotal.value.ul	= (ulong_t)total_pages;
9070Sstevel@tonic-gate 	/*
9080Sstevel@tonic-gate 	 * pp_kernel represents total pages used by the kernel since the
9090Sstevel@tonic-gate 	 * startup. This formula takes into account the boottime kernel
9100Sstevel@tonic-gate 	 * footprint and also considers the availrmem changes because of
9110Sstevel@tonic-gate 	 * user explicit page locking.
9120Sstevel@tonic-gate 	 */
9130Sstevel@tonic-gate 	system_pages_kstat.pp_kernel.value.ul   = (ulong_t)(physinstalled -
9140Sstevel@tonic-gate 		obp_pages - availrmem - k_anoninfo.ani_mem_resv -
9150Sstevel@tonic-gate 		anon_segkp_pages_locked - segvn_pages_locked -
9160Sstevel@tonic-gate 		pages_locked - pages_claimed - pages_useclaim);
9170Sstevel@tonic-gate 
9180Sstevel@tonic-gate 	return (0);
9190Sstevel@tonic-gate }
9200Sstevel@tonic-gate 
9210Sstevel@tonic-gate kstat_t *
9222951Selowe kstat_create(const char *ks_module, int ks_instance, const char *ks_name,
9232951Selowe     const char *ks_class, uchar_t ks_type, uint_t ks_ndata, uchar_t ks_flags)
9240Sstevel@tonic-gate {
9250Sstevel@tonic-gate 	return (kstat_create_zone(ks_module, ks_instance, ks_name, ks_class,
9260Sstevel@tonic-gate 		    ks_type, ks_ndata, ks_flags, ALL_ZONES));
9270Sstevel@tonic-gate }
9280Sstevel@tonic-gate 
9290Sstevel@tonic-gate /*
9300Sstevel@tonic-gate  * Allocate and initialize a kstat structure.  Or, if a dormant kstat with
9310Sstevel@tonic-gate  * the specified name exists, reactivate it.  Returns a pointer to the kstat
9320Sstevel@tonic-gate  * on success, NULL on failure.  The kstat will not be visible to the
9330Sstevel@tonic-gate  * kstat driver until kstat_install().
9340Sstevel@tonic-gate  */
9350Sstevel@tonic-gate kstat_t *
9362951Selowe kstat_create_zone(const char *ks_module, int ks_instance, const char *ks_name,
9372951Selowe     const char *ks_class, uchar_t ks_type, uint_t ks_ndata, uchar_t ks_flags,
9380Sstevel@tonic-gate     zoneid_t ks_zoneid)
9390Sstevel@tonic-gate {
9400Sstevel@tonic-gate 	size_t ks_data_size;
9410Sstevel@tonic-gate 	kstat_t *ksp;
9420Sstevel@tonic-gate 	ekstat_t *e;
9430Sstevel@tonic-gate 	avl_index_t where;
9440Sstevel@tonic-gate 	char namebuf[KSTAT_STRLEN + 16];
9450Sstevel@tonic-gate 
9460Sstevel@tonic-gate 	if (avl_numnodes(&kstat_avl_bykid) == 0) {
9470Sstevel@tonic-gate 		avl_create(&kstat_avl_bykid, kstat_compare_bykid,
9480Sstevel@tonic-gate 		    sizeof (ekstat_t), offsetof(struct ekstat, e_avl_bykid));
9490Sstevel@tonic-gate 
9500Sstevel@tonic-gate 		avl_create(&kstat_avl_byname, kstat_compare_byname,
9510Sstevel@tonic-gate 		    sizeof (ekstat_t), offsetof(struct ekstat, e_avl_byname));
9520Sstevel@tonic-gate 	}
9530Sstevel@tonic-gate 
9540Sstevel@tonic-gate 	/*
9550Sstevel@tonic-gate 	 * If ks_name == NULL, set the ks_name to <module><instance>.
9560Sstevel@tonic-gate 	 */
9570Sstevel@tonic-gate 	if (ks_name == NULL) {
9580Sstevel@tonic-gate 		char buf[KSTAT_STRLEN];
9590Sstevel@tonic-gate 		kstat_set_string(buf, ks_module);
9600Sstevel@tonic-gate 		(void) sprintf(namebuf, "%s%d", buf, ks_instance);
9610Sstevel@tonic-gate 		ks_name = namebuf;
9620Sstevel@tonic-gate 	}
9630Sstevel@tonic-gate 
9640Sstevel@tonic-gate 	/*
9650Sstevel@tonic-gate 	 * Make sure it's a valid kstat data type
9660Sstevel@tonic-gate 	 */
9670Sstevel@tonic-gate 	if (ks_type >= KSTAT_NUM_TYPES) {
9680Sstevel@tonic-gate 		cmn_err(CE_WARN, "kstat_create('%s', %d, '%s'): "
9690Sstevel@tonic-gate 			"invalid kstat type %d",
9700Sstevel@tonic-gate 			ks_module, ks_instance, ks_name, ks_type);
9710Sstevel@tonic-gate 		return (NULL);
9720Sstevel@tonic-gate 	}
9730Sstevel@tonic-gate 
9740Sstevel@tonic-gate 	/*
9750Sstevel@tonic-gate 	 * Don't allow persistent virtual kstats -- it makes no sense.
9760Sstevel@tonic-gate 	 * ks_data points to garbage when the client goes away.
9770Sstevel@tonic-gate 	 */
9780Sstevel@tonic-gate 	if ((ks_flags & KSTAT_FLAG_PERSISTENT) &&
9790Sstevel@tonic-gate 	    (ks_flags & KSTAT_FLAG_VIRTUAL)) {
9800Sstevel@tonic-gate 		cmn_err(CE_WARN, "kstat_create('%s', %d, '%s'): "
9810Sstevel@tonic-gate 			"cannot create persistent virtual kstat",
9820Sstevel@tonic-gate 			ks_module, ks_instance, ks_name);
9830Sstevel@tonic-gate 		return (NULL);
9840Sstevel@tonic-gate 	}
9850Sstevel@tonic-gate 
9860Sstevel@tonic-gate 	/*
9870Sstevel@tonic-gate 	 * Don't allow variable-size physical kstats, since the framework's
9880Sstevel@tonic-gate 	 * memory allocation for physical kstat data is fixed at creation time.
9890Sstevel@tonic-gate 	 */
9900Sstevel@tonic-gate 	if ((ks_flags & KSTAT_FLAG_VAR_SIZE) &&
9910Sstevel@tonic-gate 	    !(ks_flags & KSTAT_FLAG_VIRTUAL)) {
9920Sstevel@tonic-gate 		cmn_err(CE_WARN, "kstat_create('%s', %d, '%s'): "
9930Sstevel@tonic-gate 			"cannot create variable-size physical kstat",
9940Sstevel@tonic-gate 			ks_module, ks_instance, ks_name);
9950Sstevel@tonic-gate 		return (NULL);
9960Sstevel@tonic-gate 	}
9970Sstevel@tonic-gate 
9980Sstevel@tonic-gate 	/*
9990Sstevel@tonic-gate 	 * Make sure the number of data fields is within legal range
10000Sstevel@tonic-gate 	 */
10010Sstevel@tonic-gate 	if (ks_ndata < kstat_data_type[ks_type].min_ndata ||
10020Sstevel@tonic-gate 	    ks_ndata > kstat_data_type[ks_type].max_ndata) {
10030Sstevel@tonic-gate 		cmn_err(CE_WARN, "kstat_create('%s', %d, '%s'): "
10040Sstevel@tonic-gate 			"ks_ndata=%d out of range [%d, %d]",
10050Sstevel@tonic-gate 			ks_module, ks_instance, ks_name, (int)ks_ndata,
10060Sstevel@tonic-gate 			kstat_data_type[ks_type].min_ndata,
10070Sstevel@tonic-gate 			kstat_data_type[ks_type].max_ndata);
10080Sstevel@tonic-gate 		return (NULL);
10090Sstevel@tonic-gate 	}
10100Sstevel@tonic-gate 
10110Sstevel@tonic-gate 	ks_data_size = kstat_data_type[ks_type].size * ks_ndata;
10120Sstevel@tonic-gate 
10130Sstevel@tonic-gate 	/*
10140Sstevel@tonic-gate 	 * If the named kstat already exists and is dormant, reactivate it.
10150Sstevel@tonic-gate 	 */
10160Sstevel@tonic-gate 	ksp = kstat_hold_byname(ks_module, ks_instance, ks_name, ks_zoneid);
10170Sstevel@tonic-gate 	if (ksp != NULL) {
10180Sstevel@tonic-gate 		if (!(ksp->ks_flags & KSTAT_FLAG_DORMANT)) {
10190Sstevel@tonic-gate 			/*
10200Sstevel@tonic-gate 			 * The named kstat exists but is not dormant --
10210Sstevel@tonic-gate 			 * this is a kstat namespace collision.
10220Sstevel@tonic-gate 			 */
10230Sstevel@tonic-gate 			kstat_rele(ksp);
10240Sstevel@tonic-gate 			cmn_err(CE_WARN,
10250Sstevel@tonic-gate 			    "kstat_create('%s', %d, '%s'): namespace collision",
10260Sstevel@tonic-gate 			    ks_module, ks_instance, ks_name);
10270Sstevel@tonic-gate 			return (NULL);
10280Sstevel@tonic-gate 		}
10290Sstevel@tonic-gate 		if ((strcmp(ksp->ks_class, ks_class) != 0) ||
10300Sstevel@tonic-gate 		    (ksp->ks_type != ks_type) ||
10310Sstevel@tonic-gate 		    (ksp->ks_ndata != ks_ndata) ||
10320Sstevel@tonic-gate 		    (ks_flags & KSTAT_FLAG_VIRTUAL)) {
10330Sstevel@tonic-gate 			/*
10340Sstevel@tonic-gate 			 * The name is the same, but the other key parameters
10350Sstevel@tonic-gate 			 * differ from those of the dormant kstat -- bogus.
10360Sstevel@tonic-gate 			 */
10370Sstevel@tonic-gate 			kstat_rele(ksp);
10380Sstevel@tonic-gate 			cmn_err(CE_WARN, "kstat_create('%s', %d, '%s'): "
10390Sstevel@tonic-gate 				"invalid reactivation of dormant kstat",
10400Sstevel@tonic-gate 				ks_module, ks_instance, ks_name);
10410Sstevel@tonic-gate 			return (NULL);
10420Sstevel@tonic-gate 		}
10430Sstevel@tonic-gate 		/*
10440Sstevel@tonic-gate 		 * Return dormant kstat pointer to caller.  As usual,
10450Sstevel@tonic-gate 		 * the kstat is marked invalid until kstat_install().
10460Sstevel@tonic-gate 		 */
10470Sstevel@tonic-gate 		ksp->ks_flags |= KSTAT_FLAG_INVALID;
10480Sstevel@tonic-gate 		kstat_rele(ksp);
10490Sstevel@tonic-gate 		return (ksp);
10500Sstevel@tonic-gate 	}
10510Sstevel@tonic-gate 
10520Sstevel@tonic-gate 	/*
10530Sstevel@tonic-gate 	 * Allocate memory for the new kstat header and, if this is a physical
10540Sstevel@tonic-gate 	 * kstat, the data section.
10550Sstevel@tonic-gate 	 */
10560Sstevel@tonic-gate 	e = kstat_alloc(ks_flags & KSTAT_FLAG_VIRTUAL ? 0 : ks_data_size);
10570Sstevel@tonic-gate 	if (e == NULL) {
10580Sstevel@tonic-gate 		cmn_err(CE_NOTE, "kstat_create('%s', %d, '%s'): "
10590Sstevel@tonic-gate 			"insufficient kernel memory",
10600Sstevel@tonic-gate 			ks_module, ks_instance, ks_name);
10610Sstevel@tonic-gate 		return (NULL);
10620Sstevel@tonic-gate 	}
10630Sstevel@tonic-gate 
10640Sstevel@tonic-gate 	/*
10650Sstevel@tonic-gate 	 * Initialize as many fields as we can.  The caller may reset
10660Sstevel@tonic-gate 	 * ks_lock, ks_update, ks_private, and ks_snapshot as necessary.
10670Sstevel@tonic-gate 	 * Creators of virtual kstats may also reset ks_data.  It is
10680Sstevel@tonic-gate 	 * also up to the caller to initialize the kstat data section,
10690Sstevel@tonic-gate 	 * if necessary.  All initialization must be complete before
10700Sstevel@tonic-gate 	 * calling kstat_install().
10710Sstevel@tonic-gate 	 */
10720Sstevel@tonic-gate 	e->e_zone.zoneid = ks_zoneid;
10730Sstevel@tonic-gate 	e->e_zone.next = NULL;
10740Sstevel@tonic-gate 
10750Sstevel@tonic-gate 	ksp = &e->e_ks;
10760Sstevel@tonic-gate 	ksp->ks_crtime		= gethrtime();
10770Sstevel@tonic-gate 	kstat_set_string(ksp->ks_module, ks_module);
10780Sstevel@tonic-gate 	ksp->ks_instance	= ks_instance;
10790Sstevel@tonic-gate 	kstat_set_string(ksp->ks_name, ks_name);
10800Sstevel@tonic-gate 	ksp->ks_type		= ks_type;
10810Sstevel@tonic-gate 	kstat_set_string(ksp->ks_class, ks_class);
10820Sstevel@tonic-gate 	ksp->ks_flags		= ks_flags | KSTAT_FLAG_INVALID;
10830Sstevel@tonic-gate 	if (ks_flags & KSTAT_FLAG_VIRTUAL)
10840Sstevel@tonic-gate 		ksp->ks_data	= NULL;
10850Sstevel@tonic-gate 	else
10860Sstevel@tonic-gate 		ksp->ks_data	= (void *)(e + 1);
10870Sstevel@tonic-gate 	ksp->ks_ndata		= ks_ndata;
10880Sstevel@tonic-gate 	ksp->ks_data_size	= ks_data_size;
10890Sstevel@tonic-gate 	ksp->ks_snaptime	= ksp->ks_crtime;
10900Sstevel@tonic-gate 	ksp->ks_update		= default_kstat_update;
10910Sstevel@tonic-gate 	ksp->ks_private		= NULL;
10920Sstevel@tonic-gate 	ksp->ks_snapshot	= default_kstat_snapshot;
10930Sstevel@tonic-gate 	ksp->ks_lock		= NULL;
10940Sstevel@tonic-gate 
10950Sstevel@tonic-gate 	mutex_enter(&kstat_chain_lock);
10960Sstevel@tonic-gate 
10970Sstevel@tonic-gate 	/*
10980Sstevel@tonic-gate 	 * Add our kstat to the AVL trees.
10990Sstevel@tonic-gate 	 */
11000Sstevel@tonic-gate 	if (avl_find(&kstat_avl_byname, e, &where) != NULL) {
11010Sstevel@tonic-gate 		mutex_exit(&kstat_chain_lock);
11020Sstevel@tonic-gate 		cmn_err(CE_WARN,
11030Sstevel@tonic-gate 		    "kstat_create('%s', %d, '%s'): namespace collision",
11040Sstevel@tonic-gate 		    ks_module, ks_instance, ks_name);
11050Sstevel@tonic-gate 		kstat_free(e);
11060Sstevel@tonic-gate 		return (NULL);
11070Sstevel@tonic-gate 	}
11080Sstevel@tonic-gate 	avl_insert(&kstat_avl_byname, e, where);
11090Sstevel@tonic-gate 
11100Sstevel@tonic-gate 	/*
11110Sstevel@tonic-gate 	 * Loop around until we find an unused KID.
11120Sstevel@tonic-gate 	 */
11130Sstevel@tonic-gate 	do {
11140Sstevel@tonic-gate 		ksp->ks_kid = kstat_chain_id++;
11150Sstevel@tonic-gate 	} while (avl_find(&kstat_avl_bykid, e, &where) != NULL);
11160Sstevel@tonic-gate 	avl_insert(&kstat_avl_bykid, e, where);
11170Sstevel@tonic-gate 
11180Sstevel@tonic-gate 	mutex_exit(&kstat_chain_lock);
11190Sstevel@tonic-gate 
11200Sstevel@tonic-gate 	return (ksp);
11210Sstevel@tonic-gate }
11220Sstevel@tonic-gate 
11230Sstevel@tonic-gate /*
11240Sstevel@tonic-gate  * Activate a fully initialized kstat and make it visible to /dev/kstat.
11250Sstevel@tonic-gate  */
11260Sstevel@tonic-gate void
11270Sstevel@tonic-gate kstat_install(kstat_t *ksp)
11280Sstevel@tonic-gate {
11290Sstevel@tonic-gate 	zoneid_t zoneid = ((ekstat_t *)ksp)->e_zone.zoneid;
11300Sstevel@tonic-gate 
11310Sstevel@tonic-gate 	/*
11320Sstevel@tonic-gate 	 * If this is a variable-size kstat, it MUST provide kstat data locking
11330Sstevel@tonic-gate 	 * to prevent data-size races with kstat readers.
11340Sstevel@tonic-gate 	 */
11350Sstevel@tonic-gate 	if ((ksp->ks_flags & KSTAT_FLAG_VAR_SIZE) && ksp->ks_lock == NULL) {
11360Sstevel@tonic-gate 		panic("kstat_install('%s', %d, '%s'): "
11370Sstevel@tonic-gate 		    "cannot create variable-size kstat without data lock",
11380Sstevel@tonic-gate 		    ksp->ks_module, ksp->ks_instance, ksp->ks_name);
11390Sstevel@tonic-gate 	}
11400Sstevel@tonic-gate 
11410Sstevel@tonic-gate 	if (kstat_hold_bykid(ksp->ks_kid, zoneid) != ksp) {
11420Sstevel@tonic-gate 		cmn_err(CE_WARN, "kstat_install(%p): does not exist",
11430Sstevel@tonic-gate 		    (void *)ksp);
11440Sstevel@tonic-gate 		return;
11450Sstevel@tonic-gate 	}
11460Sstevel@tonic-gate 
11470Sstevel@tonic-gate 	if (ksp->ks_type == KSTAT_TYPE_NAMED && ksp->ks_data != NULL) {
11480Sstevel@tonic-gate 		int has_long_strings = 0;
11490Sstevel@tonic-gate 		uint_t i;
11500Sstevel@tonic-gate 		kstat_named_t *knp = KSTAT_NAMED_PTR(ksp);
11510Sstevel@tonic-gate 
11520Sstevel@tonic-gate 		for (i = 0; i < ksp->ks_ndata; i++, knp++) {
11530Sstevel@tonic-gate 			if (knp->data_type == KSTAT_DATA_STRING) {
11540Sstevel@tonic-gate 				has_long_strings = 1;
11550Sstevel@tonic-gate 				break;
11560Sstevel@tonic-gate 			}
11570Sstevel@tonic-gate 		}
11580Sstevel@tonic-gate 		/*
11590Sstevel@tonic-gate 		 * It is an error for a named kstat with fields of
11600Sstevel@tonic-gate 		 * KSTAT_DATA_STRING to be non-virtual.
11610Sstevel@tonic-gate 		 */
11620Sstevel@tonic-gate 		if (has_long_strings && !(ksp->ks_flags & KSTAT_FLAG_VIRTUAL)) {
11630Sstevel@tonic-gate 			panic("kstat_install('%s', %d, '%s'): "
11640Sstevel@tonic-gate 			    "named kstat containing KSTAT_DATA_STRING "
11650Sstevel@tonic-gate 			    "is not virtual",
11660Sstevel@tonic-gate 			    ksp->ks_module, ksp->ks_instance,
11670Sstevel@tonic-gate 			    ksp->ks_name);
11680Sstevel@tonic-gate 		}
11690Sstevel@tonic-gate 		/*
11700Sstevel@tonic-gate 		 * The default snapshot routine does not handle KSTAT_WRITE
11710Sstevel@tonic-gate 		 * for long strings.
11720Sstevel@tonic-gate 		 */
11730Sstevel@tonic-gate 		if (has_long_strings && (ksp->ks_flags & KSTAT_FLAG_WRITABLE) &&
11740Sstevel@tonic-gate 		    (ksp->ks_snapshot == default_kstat_snapshot)) {
11750Sstevel@tonic-gate 			panic("kstat_install('%s', %d, '%s'): "
11760Sstevel@tonic-gate 			    "named kstat containing KSTAT_DATA_STRING "
11770Sstevel@tonic-gate 			    "is writable but uses default snapshot routine",
11780Sstevel@tonic-gate 			    ksp->ks_module, ksp->ks_instance, ksp->ks_name);
11790Sstevel@tonic-gate 		}
11800Sstevel@tonic-gate 	}
11810Sstevel@tonic-gate 
11820Sstevel@tonic-gate 	if (ksp->ks_flags & KSTAT_FLAG_DORMANT) {
11830Sstevel@tonic-gate 
11840Sstevel@tonic-gate 		/*
11850Sstevel@tonic-gate 		 * We are reactivating a dormant kstat.  Initialize the
11860Sstevel@tonic-gate 		 * caller's underlying data to the value it had when the
11870Sstevel@tonic-gate 		 * kstat went dormant, and mark the kstat as active.
11880Sstevel@tonic-gate 		 * Grab the provider's kstat lock if it's not already held.
11890Sstevel@tonic-gate 		 */
11900Sstevel@tonic-gate 		kmutex_t *lp = ksp->ks_lock;
11910Sstevel@tonic-gate 		if (lp != NULL && MUTEX_NOT_HELD(lp)) {
11920Sstevel@tonic-gate 			mutex_enter(lp);
11930Sstevel@tonic-gate 			(void) KSTAT_UPDATE(ksp, KSTAT_WRITE);
11940Sstevel@tonic-gate 			mutex_exit(lp);
11950Sstevel@tonic-gate 		} else {
11960Sstevel@tonic-gate 			(void) KSTAT_UPDATE(ksp, KSTAT_WRITE);
11970Sstevel@tonic-gate 		}
11980Sstevel@tonic-gate 		ksp->ks_flags &= ~KSTAT_FLAG_DORMANT;
11990Sstevel@tonic-gate 	}
12000Sstevel@tonic-gate 
12010Sstevel@tonic-gate 	/*
12020Sstevel@tonic-gate 	 * Now that the kstat is active, make it visible to the kstat driver.
12030Sstevel@tonic-gate 	 */
12040Sstevel@tonic-gate 	ksp->ks_flags &= ~KSTAT_FLAG_INVALID;
12050Sstevel@tonic-gate 	kstat_rele(ksp);
12060Sstevel@tonic-gate }
12070Sstevel@tonic-gate 
12080Sstevel@tonic-gate /*
12090Sstevel@tonic-gate  * Remove a kstat from the system.  Or, if it's a persistent kstat,
12100Sstevel@tonic-gate  * just update the data and mark it as dormant.
12110Sstevel@tonic-gate  */
12120Sstevel@tonic-gate void
12130Sstevel@tonic-gate kstat_delete(kstat_t *ksp)
12140Sstevel@tonic-gate {
12150Sstevel@tonic-gate 	kmutex_t *lp;
12160Sstevel@tonic-gate 	ekstat_t *e = (ekstat_t *)ksp;
12170Sstevel@tonic-gate 	zoneid_t zoneid = e->e_zone.zoneid;
12180Sstevel@tonic-gate 	kstat_zone_t *kz;
12190Sstevel@tonic-gate 
12200Sstevel@tonic-gate 	if (ksp == NULL)
12210Sstevel@tonic-gate 		return;
12220Sstevel@tonic-gate 
12230Sstevel@tonic-gate 	lp = ksp->ks_lock;
12240Sstevel@tonic-gate 
12250Sstevel@tonic-gate 	if (lp != NULL && MUTEX_HELD(lp)) {
12260Sstevel@tonic-gate 		panic("kstat_delete(%p): caller holds data lock %p",
12270Sstevel@tonic-gate 		    (void *)ksp, (void *)lp);
12280Sstevel@tonic-gate 	}
12290Sstevel@tonic-gate 
12300Sstevel@tonic-gate 	if (kstat_hold_bykid(ksp->ks_kid, zoneid) != ksp) {
12310Sstevel@tonic-gate 		cmn_err(CE_WARN, "kstat_delete(%p): does not exist",
12320Sstevel@tonic-gate 		    (void *)ksp);
12330Sstevel@tonic-gate 		return;
12340Sstevel@tonic-gate 	}
12350Sstevel@tonic-gate 
12360Sstevel@tonic-gate 	if (ksp->ks_flags & KSTAT_FLAG_PERSISTENT) {
12370Sstevel@tonic-gate 		/*
12380Sstevel@tonic-gate 		 * Update the data one last time, so that all activity
12390Sstevel@tonic-gate 		 * prior to going dormant has been accounted for.
12400Sstevel@tonic-gate 		 */
12410Sstevel@tonic-gate 		KSTAT_ENTER(ksp);
12420Sstevel@tonic-gate 		(void) KSTAT_UPDATE(ksp, KSTAT_READ);
12430Sstevel@tonic-gate 		KSTAT_EXIT(ksp);
12440Sstevel@tonic-gate 
12450Sstevel@tonic-gate 		/*
12460Sstevel@tonic-gate 		 * Mark the kstat as dormant and restore caller-modifiable
12470Sstevel@tonic-gate 		 * fields to default values, so the kstat is readable during
12480Sstevel@tonic-gate 		 * the dormant phase.
12490Sstevel@tonic-gate 		 */
12500Sstevel@tonic-gate 		ksp->ks_flags |= KSTAT_FLAG_DORMANT;
12510Sstevel@tonic-gate 		ksp->ks_lock = NULL;
12520Sstevel@tonic-gate 		ksp->ks_update = default_kstat_update;
12530Sstevel@tonic-gate 		ksp->ks_private = NULL;
12540Sstevel@tonic-gate 		ksp->ks_snapshot = default_kstat_snapshot;
12550Sstevel@tonic-gate 		kstat_rele(ksp);
12560Sstevel@tonic-gate 		return;
12570Sstevel@tonic-gate 	}
12580Sstevel@tonic-gate 
12590Sstevel@tonic-gate 	/*
12600Sstevel@tonic-gate 	 * Remove the kstat from the framework's AVL trees,
12610Sstevel@tonic-gate 	 * free the allocated memory, and increment kstat_chain_id so
12620Sstevel@tonic-gate 	 * /dev/kstat clients can detect the event.
12630Sstevel@tonic-gate 	 */
12640Sstevel@tonic-gate 	mutex_enter(&kstat_chain_lock);
12650Sstevel@tonic-gate 	avl_remove(&kstat_avl_bykid, e);
12660Sstevel@tonic-gate 	avl_remove(&kstat_avl_byname, e);
12670Sstevel@tonic-gate 	kstat_chain_id++;
12680Sstevel@tonic-gate 	mutex_exit(&kstat_chain_lock);
12690Sstevel@tonic-gate 
12700Sstevel@tonic-gate 	kz = e->e_zone.next;
12710Sstevel@tonic-gate 	while (kz != NULL) {
12720Sstevel@tonic-gate 		kstat_zone_t *t = kz;
12730Sstevel@tonic-gate 
12740Sstevel@tonic-gate 		kz = kz->next;
12750Sstevel@tonic-gate 		kmem_free(t, sizeof (*t));
12760Sstevel@tonic-gate 	}
12770Sstevel@tonic-gate 	kstat_rele(ksp);
12780Sstevel@tonic-gate 	kstat_free(e);
12790Sstevel@tonic-gate }
12800Sstevel@tonic-gate 
12810Sstevel@tonic-gate void
12822951Selowe kstat_delete_byname_zone(const char *ks_module, int ks_instance,
12832951Selowe     const char *ks_name, zoneid_t ks_zoneid)
12840Sstevel@tonic-gate {
12850Sstevel@tonic-gate 	kstat_t *ksp;
12860Sstevel@tonic-gate 
12870Sstevel@tonic-gate 	ksp = kstat_hold_byname(ks_module, ks_instance, ks_name, ks_zoneid);
12880Sstevel@tonic-gate 	if (ksp != NULL) {
12890Sstevel@tonic-gate 		kstat_rele(ksp);
12900Sstevel@tonic-gate 		kstat_delete(ksp);
12910Sstevel@tonic-gate 	}
12920Sstevel@tonic-gate }
12930Sstevel@tonic-gate 
12940Sstevel@tonic-gate void
12952951Selowe kstat_delete_byname(const char *ks_module, int ks_instance, const char *ks_name)
12960Sstevel@tonic-gate {
12970Sstevel@tonic-gate 	kstat_delete_byname_zone(ks_module, ks_instance, ks_name, ALL_ZONES);
12980Sstevel@tonic-gate }
12990Sstevel@tonic-gate 
13000Sstevel@tonic-gate /*
13010Sstevel@tonic-gate  * The sparc V9 versions of these routines can be much cheaper than
13020Sstevel@tonic-gate  * the poor 32-bit compiler can comprehend, so they're in sparcv9_subr.s.
13030Sstevel@tonic-gate  * For simplicity, however, we always feed the C versions to lint.
13040Sstevel@tonic-gate  */
13050Sstevel@tonic-gate #if !defined(__sparc) || defined(lint) || defined(__lint)
13060Sstevel@tonic-gate 
13070Sstevel@tonic-gate void
13080Sstevel@tonic-gate kstat_waitq_enter(kstat_io_t *kiop)
13090Sstevel@tonic-gate {
13100Sstevel@tonic-gate 	hrtime_t new, delta;
13110Sstevel@tonic-gate 	ulong_t wcnt;
13120Sstevel@tonic-gate 
13130Sstevel@tonic-gate 	new = gethrtime_unscaled();
13140Sstevel@tonic-gate 	delta = new - kiop->wlastupdate;
13150Sstevel@tonic-gate 	kiop->wlastupdate = new;
13160Sstevel@tonic-gate 	wcnt = kiop->wcnt++;
13170Sstevel@tonic-gate 	if (wcnt != 0) {
13180Sstevel@tonic-gate 		kiop->wlentime += delta * wcnt;
13190Sstevel@tonic-gate 		kiop->wtime += delta;
13200Sstevel@tonic-gate 	}
13210Sstevel@tonic-gate }
13220Sstevel@tonic-gate 
13230Sstevel@tonic-gate void
13240Sstevel@tonic-gate kstat_waitq_exit(kstat_io_t *kiop)
13250Sstevel@tonic-gate {
13260Sstevel@tonic-gate 	hrtime_t new, delta;
13270Sstevel@tonic-gate 	ulong_t wcnt;
13280Sstevel@tonic-gate 
13290Sstevel@tonic-gate 	new = gethrtime_unscaled();
13300Sstevel@tonic-gate 	delta = new - kiop->wlastupdate;
13310Sstevel@tonic-gate 	kiop->wlastupdate = new;
13320Sstevel@tonic-gate 	wcnt = kiop->wcnt--;
13330Sstevel@tonic-gate 	ASSERT((int)wcnt > 0);
13340Sstevel@tonic-gate 	kiop->wlentime += delta * wcnt;
13350Sstevel@tonic-gate 	kiop->wtime += delta;
13360Sstevel@tonic-gate }
13370Sstevel@tonic-gate 
13380Sstevel@tonic-gate void
13390Sstevel@tonic-gate kstat_runq_enter(kstat_io_t *kiop)
13400Sstevel@tonic-gate {
13410Sstevel@tonic-gate 	hrtime_t new, delta;
13420Sstevel@tonic-gate 	ulong_t rcnt;
13430Sstevel@tonic-gate 
13440Sstevel@tonic-gate 	new = gethrtime_unscaled();
13450Sstevel@tonic-gate 	delta = new - kiop->rlastupdate;
13460Sstevel@tonic-gate 	kiop->rlastupdate = new;
13470Sstevel@tonic-gate 	rcnt = kiop->rcnt++;
13480Sstevel@tonic-gate 	if (rcnt != 0) {
13490Sstevel@tonic-gate 		kiop->rlentime += delta * rcnt;
13500Sstevel@tonic-gate 		kiop->rtime += delta;
13510Sstevel@tonic-gate 	}
13520Sstevel@tonic-gate }
13530Sstevel@tonic-gate 
13540Sstevel@tonic-gate void
13550Sstevel@tonic-gate kstat_runq_exit(kstat_io_t *kiop)
13560Sstevel@tonic-gate {
13570Sstevel@tonic-gate 	hrtime_t new, delta;
13580Sstevel@tonic-gate 	ulong_t rcnt;
13590Sstevel@tonic-gate 
13600Sstevel@tonic-gate 	new = gethrtime_unscaled();
13610Sstevel@tonic-gate 	delta = new - kiop->rlastupdate;
13620Sstevel@tonic-gate 	kiop->rlastupdate = new;
13630Sstevel@tonic-gate 	rcnt = kiop->rcnt--;
13640Sstevel@tonic-gate 	ASSERT((int)rcnt > 0);
13650Sstevel@tonic-gate 	kiop->rlentime += delta * rcnt;
13660Sstevel@tonic-gate 	kiop->rtime += delta;
13670Sstevel@tonic-gate }
13680Sstevel@tonic-gate 
13690Sstevel@tonic-gate void
13700Sstevel@tonic-gate kstat_waitq_to_runq(kstat_io_t *kiop)
13710Sstevel@tonic-gate {
13720Sstevel@tonic-gate 	hrtime_t new, delta;
13730Sstevel@tonic-gate 	ulong_t wcnt, rcnt;
13740Sstevel@tonic-gate 
13750Sstevel@tonic-gate 	new = gethrtime_unscaled();
13760Sstevel@tonic-gate 
13770Sstevel@tonic-gate 	delta = new - kiop->wlastupdate;
13780Sstevel@tonic-gate 	kiop->wlastupdate = new;
13790Sstevel@tonic-gate 	wcnt = kiop->wcnt--;
13800Sstevel@tonic-gate 	ASSERT((int)wcnt > 0);
13810Sstevel@tonic-gate 	kiop->wlentime += delta * wcnt;
13820Sstevel@tonic-gate 	kiop->wtime += delta;
13830Sstevel@tonic-gate 
13840Sstevel@tonic-gate 	delta = new - kiop->rlastupdate;
13850Sstevel@tonic-gate 	kiop->rlastupdate = new;
13860Sstevel@tonic-gate 	rcnt = kiop->rcnt++;
13870Sstevel@tonic-gate 	if (rcnt != 0) {
13880Sstevel@tonic-gate 		kiop->rlentime += delta * rcnt;
13890Sstevel@tonic-gate 		kiop->rtime += delta;
13900Sstevel@tonic-gate 	}
13910Sstevel@tonic-gate }
13920Sstevel@tonic-gate 
13930Sstevel@tonic-gate void
13940Sstevel@tonic-gate kstat_runq_back_to_waitq(kstat_io_t *kiop)
13950Sstevel@tonic-gate {
13960Sstevel@tonic-gate 	hrtime_t new, delta;
13970Sstevel@tonic-gate 	ulong_t wcnt, rcnt;
13980Sstevel@tonic-gate 
13990Sstevel@tonic-gate 	new = gethrtime_unscaled();
14000Sstevel@tonic-gate 
14010Sstevel@tonic-gate 	delta = new - kiop->rlastupdate;
14020Sstevel@tonic-gate 	kiop->rlastupdate = new;
14030Sstevel@tonic-gate 	rcnt = kiop->rcnt--;
14040Sstevel@tonic-gate 	ASSERT((int)rcnt > 0);
14050Sstevel@tonic-gate 	kiop->rlentime += delta * rcnt;
14060Sstevel@tonic-gate 	kiop->rtime += delta;
14070Sstevel@tonic-gate 
14080Sstevel@tonic-gate 	delta = new - kiop->wlastupdate;
14090Sstevel@tonic-gate 	kiop->wlastupdate = new;
14100Sstevel@tonic-gate 	wcnt = kiop->wcnt++;
14110Sstevel@tonic-gate 	if (wcnt != 0) {
14120Sstevel@tonic-gate 		kiop->wlentime += delta * wcnt;
14130Sstevel@tonic-gate 		kiop->wtime += delta;
14140Sstevel@tonic-gate 	}
14150Sstevel@tonic-gate }
14160Sstevel@tonic-gate 
14170Sstevel@tonic-gate #endif
14180Sstevel@tonic-gate 
14190Sstevel@tonic-gate void
14200Sstevel@tonic-gate kstat_timer_start(kstat_timer_t *ktp)
14210Sstevel@tonic-gate {
14220Sstevel@tonic-gate 	ktp->start_time = gethrtime();
14230Sstevel@tonic-gate }
14240Sstevel@tonic-gate 
14250Sstevel@tonic-gate void
14260Sstevel@tonic-gate kstat_timer_stop(kstat_timer_t *ktp)
14270Sstevel@tonic-gate {
14280Sstevel@tonic-gate 	hrtime_t	etime;
14290Sstevel@tonic-gate 	u_longlong_t	num_events;
14300Sstevel@tonic-gate 
14310Sstevel@tonic-gate 	ktp->stop_time = etime = gethrtime();
14320Sstevel@tonic-gate 	etime -= ktp->start_time;
14330Sstevel@tonic-gate 	num_events = ktp->num_events;
14340Sstevel@tonic-gate 	if (etime < ktp->min_time || num_events == 0)
14350Sstevel@tonic-gate 		ktp->min_time = etime;
14360Sstevel@tonic-gate 	if (etime > ktp->max_time)
14370Sstevel@tonic-gate 		ktp->max_time = etime;
14380Sstevel@tonic-gate 	ktp->elapsed_time += etime;
14390Sstevel@tonic-gate 	ktp->num_events = num_events + 1;
14400Sstevel@tonic-gate }
1441