xref: /onnv-gate/usr/src/uts/common/os/kstat_fr.c (revision 2951:8e5048c1f58e)
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 /*
222723Scth  * Copyright 2006 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 
2920Sstevel@tonic-gate 	kz = kmem_alloc(sizeof (*kz), KM_SLEEP);
2930Sstevel@tonic-gate 	mutex_enter(&kstat_chain_lock);
2940Sstevel@tonic-gate 	kz->zoneid = zoneid;
2950Sstevel@tonic-gate 	kz->next = e->e_zone.next;
2960Sstevel@tonic-gate 	e->e_zone.next = kz;
2970Sstevel@tonic-gate 	kstat_chain_id++;
2980Sstevel@tonic-gate 	mutex_exit(&kstat_chain_lock);
2990Sstevel@tonic-gate }
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate /*
3020Sstevel@tonic-gate  * Compare the list of zones for the given kstats, returning 0 if they match
3030Sstevel@tonic-gate  * (ie, one list contains ALL_ZONES or both lists contain the same zoneid).
3040Sstevel@tonic-gate  * In practice, this is called indirectly by kstat_hold_byname(), so one of the
3050Sstevel@tonic-gate  * two lists always has one element, and this is an O(n) operation rather than
3060Sstevel@tonic-gate  * O(n^2).
3070Sstevel@tonic-gate  */
3080Sstevel@tonic-gate static int
3090Sstevel@tonic-gate kstat_zone_compare(ekstat_t *e1, ekstat_t *e2)
3100Sstevel@tonic-gate {
3110Sstevel@tonic-gate 	kstat_zone_t *kz1, *kz2;
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&kstat_chain_lock));
3140Sstevel@tonic-gate 	for (kz1 = &e1->e_zone; kz1 != NULL; kz1 = kz1->next) {
3150Sstevel@tonic-gate 		for (kz2 = &e2->e_zone; kz2 != NULL; kz2 = kz2->next) {
3160Sstevel@tonic-gate 			if (kz1->zoneid == ALL_ZONES ||
3170Sstevel@tonic-gate 			    kz2->zoneid == ALL_ZONES)
3180Sstevel@tonic-gate 				return (0);
3190Sstevel@tonic-gate 			if (kz1->zoneid == kz2->zoneid)
3200Sstevel@tonic-gate 				return (0);
3210Sstevel@tonic-gate 		}
3220Sstevel@tonic-gate 	}
3230Sstevel@tonic-gate 	return (e1->e_zone.zoneid < e2->e_zone.zoneid ? -1 : 1);
3240Sstevel@tonic-gate }
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate /*
3270Sstevel@tonic-gate  * Support for keeping kstats sorted in AVL trees for fast lookups.
3280Sstevel@tonic-gate  */
3290Sstevel@tonic-gate static int
3300Sstevel@tonic-gate kstat_compare_bykid(const void *a1, const void *a2)
3310Sstevel@tonic-gate {
3320Sstevel@tonic-gate 	const kstat_t *k1 = a1;
3330Sstevel@tonic-gate 	const kstat_t *k2 = a2;
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 	if (k1->ks_kid < k2->ks_kid)
3360Sstevel@tonic-gate 		return (-1);
3370Sstevel@tonic-gate 	if (k1->ks_kid > k2->ks_kid)
3380Sstevel@tonic-gate 		return (1);
3390Sstevel@tonic-gate 	return (kstat_zone_compare((ekstat_t *)k1, (ekstat_t *)k2));
3400Sstevel@tonic-gate }
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate static int
3430Sstevel@tonic-gate kstat_compare_byname(const void *a1, const void *a2)
3440Sstevel@tonic-gate {
3450Sstevel@tonic-gate 	const kstat_t *k1 = a1;
3460Sstevel@tonic-gate 	const kstat_t *k2 = a2;
3470Sstevel@tonic-gate 	int s;
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 	s = strcmp(k1->ks_module, k2->ks_module);
3500Sstevel@tonic-gate 	if (s > 0)
3510Sstevel@tonic-gate 		return (1);
3520Sstevel@tonic-gate 	if (s < 0)
3530Sstevel@tonic-gate 		return (-1);
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate 	if (k1->ks_instance < k2->ks_instance)
3560Sstevel@tonic-gate 		return (-1);
3570Sstevel@tonic-gate 	if (k1->ks_instance > k2->ks_instance)
3580Sstevel@tonic-gate 		return (1);
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate 	s = strcmp(k1->ks_name, k2->ks_name);
3610Sstevel@tonic-gate 	if (s > 0)
3620Sstevel@tonic-gate 		return (1);
3630Sstevel@tonic-gate 	if (s < 0)
3640Sstevel@tonic-gate 		return (-1);
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 	return (kstat_zone_compare((ekstat_t *)k1, (ekstat_t *)k2));
3670Sstevel@tonic-gate }
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate static kstat_t *
3700Sstevel@tonic-gate kstat_hold(avl_tree_t *t, ekstat_t *template)
3710Sstevel@tonic-gate {
3720Sstevel@tonic-gate 	kstat_t *ksp;
3730Sstevel@tonic-gate 	ekstat_t *e;
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate 	mutex_enter(&kstat_chain_lock);
3760Sstevel@tonic-gate 	for (;;) {
3770Sstevel@tonic-gate 		ksp = avl_find(t, template, NULL);
3780Sstevel@tonic-gate 		if (ksp == NULL)
3790Sstevel@tonic-gate 			break;
3800Sstevel@tonic-gate 		e = (ekstat_t *)ksp;
3810Sstevel@tonic-gate 		if (e->e_owner == NULL) {
3820Sstevel@tonic-gate 			e->e_owner = curthread;
3830Sstevel@tonic-gate 			break;
3840Sstevel@tonic-gate 		}
3850Sstevel@tonic-gate 		cv_wait(&e->e_cv, &kstat_chain_lock);
3860Sstevel@tonic-gate 	}
3870Sstevel@tonic-gate 	mutex_exit(&kstat_chain_lock);
3880Sstevel@tonic-gate 	return (ksp);
3890Sstevel@tonic-gate }
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate void
3920Sstevel@tonic-gate kstat_rele(kstat_t *ksp)
3930Sstevel@tonic-gate {
3940Sstevel@tonic-gate 	ekstat_t *e = (ekstat_t *)ksp;
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate 	mutex_enter(&kstat_chain_lock);
3970Sstevel@tonic-gate 	ASSERT(e->e_owner == curthread);
3980Sstevel@tonic-gate 	e->e_owner = NULL;
3990Sstevel@tonic-gate 	cv_broadcast(&e->e_cv);
4000Sstevel@tonic-gate 	mutex_exit(&kstat_chain_lock);
4010Sstevel@tonic-gate }
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate kstat_t *
4040Sstevel@tonic-gate kstat_hold_bykid(kid_t kid, zoneid_t zoneid)
4050Sstevel@tonic-gate {
4060Sstevel@tonic-gate 	ekstat_t e;
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate 	e.e_ks.ks_kid = kid;
4090Sstevel@tonic-gate 	e.e_zone.zoneid = zoneid;
4100Sstevel@tonic-gate 	e.e_zone.next = NULL;
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate 	return (kstat_hold(&kstat_avl_bykid, &e));
4130Sstevel@tonic-gate }
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate kstat_t *
416*2951Selowe kstat_hold_byname(const char *ks_module, int ks_instance, const char *ks_name,
4170Sstevel@tonic-gate     zoneid_t ks_zoneid)
4180Sstevel@tonic-gate {
4190Sstevel@tonic-gate 	ekstat_t e;
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate 	kstat_set_string(e.e_ks.ks_module, ks_module);
4220Sstevel@tonic-gate 	e.e_ks.ks_instance = ks_instance;
4230Sstevel@tonic-gate 	kstat_set_string(e.e_ks.ks_name, ks_name);
4240Sstevel@tonic-gate 	e.e_zone.zoneid = ks_zoneid;
4250Sstevel@tonic-gate 	e.e_zone.next = NULL;
4260Sstevel@tonic-gate 	return (kstat_hold(&kstat_avl_byname, &e));
4270Sstevel@tonic-gate }
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate static ekstat_t *
4300Sstevel@tonic-gate kstat_alloc(size_t size)
4310Sstevel@tonic-gate {
4320Sstevel@tonic-gate 	ekstat_t *e = NULL;
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate 	size = P2ROUNDUP(sizeof (ekstat_t) + size, KSTAT_ALIGN);
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate 	if (kstat_arena == NULL) {
4370Sstevel@tonic-gate 		if (size <= kstat_initial_avail) {
4380Sstevel@tonic-gate 			e = kstat_initial_ptr;
4390Sstevel@tonic-gate 			kstat_initial_ptr = (char *)kstat_initial_ptr + size;
4400Sstevel@tonic-gate 			kstat_initial_avail -= size;
4410Sstevel@tonic-gate 		}
4420Sstevel@tonic-gate 	} else {
4430Sstevel@tonic-gate 		e = vmem_alloc(kstat_arena, size, VM_NOSLEEP);
4440Sstevel@tonic-gate 	}
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate 	if (e != NULL) {
4470Sstevel@tonic-gate 		bzero(e, size);
4480Sstevel@tonic-gate 		e->e_size = size;
4490Sstevel@tonic-gate 		cv_init(&e->e_cv, NULL, CV_DEFAULT, NULL);
4500Sstevel@tonic-gate 	}
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 	return (e);
4530Sstevel@tonic-gate }
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate static void
4560Sstevel@tonic-gate kstat_free(ekstat_t *e)
4570Sstevel@tonic-gate {
4580Sstevel@tonic-gate 	cv_destroy(&e->e_cv);
4590Sstevel@tonic-gate 	vmem_free(kstat_arena, e, e->e_size);
4600Sstevel@tonic-gate }
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate /*
4630Sstevel@tonic-gate  * Create various system kstats.
4640Sstevel@tonic-gate  */
4650Sstevel@tonic-gate void
4660Sstevel@tonic-gate kstat_init(void)
4670Sstevel@tonic-gate {
4680Sstevel@tonic-gate 	kstat_t *ksp;
4690Sstevel@tonic-gate 	ekstat_t *e;
4700Sstevel@tonic-gate 	avl_tree_t *t = &kstat_avl_bykid;
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate 	/*
4730Sstevel@tonic-gate 	 * Set up the kstat vmem arena.
4740Sstevel@tonic-gate 	 */
4750Sstevel@tonic-gate 	kstat_arena = vmem_create("kstat",
4760Sstevel@tonic-gate 	    kstat_initial, sizeof (kstat_initial), KSTAT_ALIGN,
4770Sstevel@tonic-gate 	    segkmem_alloc, segkmem_free, heap_arena, 0, VM_SLEEP);
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 	/*
4800Sstevel@tonic-gate 	 * Make initial kstats appear as though they were allocated.
4810Sstevel@tonic-gate 	 */
4820Sstevel@tonic-gate 	for (e = avl_first(t); e != NULL; e = avl_walk(t, e, AVL_AFTER))
4830Sstevel@tonic-gate 		(void) vmem_xalloc(kstat_arena, e->e_size, KSTAT_ALIGN,
4840Sstevel@tonic-gate 		    0, 0, e, (char *)e + e->e_size,
4850Sstevel@tonic-gate 		    VM_NOSLEEP | VM_BESTFIT | VM_PANIC);
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 	/*
4880Sstevel@tonic-gate 	 * The mother of all kstats.  The first kstat in the system, which
4890Sstevel@tonic-gate 	 * always has KID 0, has the headers for all kstats (including itself)
4900Sstevel@tonic-gate 	 * as its data.  Thus, the kstat driver does not need any special
4910Sstevel@tonic-gate 	 * interface to extract the kstat chain.
4920Sstevel@tonic-gate 	 */
4930Sstevel@tonic-gate 	kstat_chain_id = 0;
4940Sstevel@tonic-gate 	ksp = kstat_create("unix", 0, "kstat_headers", "kstat", KSTAT_TYPE_RAW,
4950Sstevel@tonic-gate 		0, KSTAT_FLAG_VIRTUAL | KSTAT_FLAG_VAR_SIZE);
4960Sstevel@tonic-gate 	if (ksp) {
4970Sstevel@tonic-gate 		ksp->ks_lock = &kstat_chain_lock;
4980Sstevel@tonic-gate 		ksp->ks_update = header_kstat_update;
4990Sstevel@tonic-gate 		ksp->ks_snapshot = header_kstat_snapshot;
5000Sstevel@tonic-gate 		kstat_install(ksp);
5010Sstevel@tonic-gate 	} else {
5020Sstevel@tonic-gate 		panic("cannot create kstat 'kstat_headers'");
5030Sstevel@tonic-gate 	}
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate 	ksp = kstat_create("unix", 0, "kstat_types", "kstat",
5060Sstevel@tonic-gate 		KSTAT_TYPE_NAMED, KSTAT_NUM_TYPES, 0);
5070Sstevel@tonic-gate 	if (ksp) {
5080Sstevel@tonic-gate 		int i;
5090Sstevel@tonic-gate 		kstat_named_t *kn = KSTAT_NAMED_PTR(ksp);
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate 		for (i = 0; i < KSTAT_NUM_TYPES; i++) {
5120Sstevel@tonic-gate 			kstat_named_init(&kn[i], kstat_data_type[i].name,
5130Sstevel@tonic-gate 				KSTAT_DATA_ULONG);
5140Sstevel@tonic-gate 			kn[i].value.ul = i;
5150Sstevel@tonic-gate 		}
5160Sstevel@tonic-gate 		kstat_install(ksp);
5170Sstevel@tonic-gate 	}
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate 	ksp = kstat_create("unix", 0, "sysinfo", "misc", KSTAT_TYPE_RAW,
5200Sstevel@tonic-gate 		sizeof (sysinfo_t), KSTAT_FLAG_VIRTUAL);
5210Sstevel@tonic-gate 	if (ksp) {
5220Sstevel@tonic-gate 		ksp->ks_data = (void *) &sysinfo;
5230Sstevel@tonic-gate 		kstat_install(ksp);
5240Sstevel@tonic-gate 	}
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate 	ksp = kstat_create("unix", 0, "vminfo", "vm", KSTAT_TYPE_RAW,
5270Sstevel@tonic-gate 		sizeof (vminfo_t), KSTAT_FLAG_VIRTUAL);
5280Sstevel@tonic-gate 	if (ksp) {
5290Sstevel@tonic-gate 		ksp->ks_data = (void *) &vminfo;
5300Sstevel@tonic-gate 		kstat_install(ksp);
5310Sstevel@tonic-gate 	}
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate 	ksp = kstat_create("unix", 0, "segmap", "vm", KSTAT_TYPE_NAMED,
5340Sstevel@tonic-gate 		segmapcnt_ndata, KSTAT_FLAG_VIRTUAL);
5350Sstevel@tonic-gate 	if (ksp) {
5360Sstevel@tonic-gate 		ksp->ks_data = (void *) segmapcnt_ptr;
5370Sstevel@tonic-gate 		ksp->ks_update = segmap_kstat_update;
5380Sstevel@tonic-gate 		kstat_install(ksp);
5390Sstevel@tonic-gate 	}
5400Sstevel@tonic-gate 
5410Sstevel@tonic-gate 	ksp = kstat_create("unix", 0, "biostats", "misc", KSTAT_TYPE_NAMED,
5420Sstevel@tonic-gate 		biostats_ndata, KSTAT_FLAG_VIRTUAL);
5430Sstevel@tonic-gate 	if (ksp) {
5440Sstevel@tonic-gate 		ksp->ks_data = (void *) biostats_ptr;
5450Sstevel@tonic-gate 		kstat_install(ksp);
5460Sstevel@tonic-gate 	}
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate #ifdef VAC
5490Sstevel@tonic-gate 	ksp = kstat_create("unix", 0, "flushmeter", "hat", KSTAT_TYPE_RAW,
5500Sstevel@tonic-gate 		sizeof (struct flushmeter), KSTAT_FLAG_VIRTUAL);
5510Sstevel@tonic-gate 	if (ksp) {
5520Sstevel@tonic-gate 		ksp->ks_data = (void *) &flush_cnt;
5530Sstevel@tonic-gate 		kstat_install(ksp);
5540Sstevel@tonic-gate 	}
5550Sstevel@tonic-gate #endif	/* VAC */
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate 	ksp = kstat_create("unix", 0, "var", "misc", KSTAT_TYPE_RAW,
5580Sstevel@tonic-gate 		sizeof (struct var), KSTAT_FLAG_VIRTUAL);
5590Sstevel@tonic-gate 	if (ksp) {
5600Sstevel@tonic-gate 		ksp->ks_data = (void *) &v;
5610Sstevel@tonic-gate 		kstat_install(ksp);
5620Sstevel@tonic-gate 	}
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate 	ksp = kstat_create("unix", 0, "system_misc", "misc", KSTAT_TYPE_NAMED,
5650Sstevel@tonic-gate 		sizeof (system_misc_kstat) / sizeof (kstat_named_t),
5660Sstevel@tonic-gate 		KSTAT_FLAG_VIRTUAL);
5670Sstevel@tonic-gate 	if (ksp) {
5680Sstevel@tonic-gate 		ksp->ks_data = (void *) &system_misc_kstat;
5690Sstevel@tonic-gate 		ksp->ks_update = system_misc_kstat_update;
5700Sstevel@tonic-gate 		kstat_install(ksp);
5710Sstevel@tonic-gate 	}
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 	ksp = kstat_create("unix", 0, "system_pages", "pages", KSTAT_TYPE_NAMED,
5740Sstevel@tonic-gate 		sizeof (system_pages_kstat) / sizeof (kstat_named_t),
5750Sstevel@tonic-gate 		KSTAT_FLAG_VIRTUAL);
5760Sstevel@tonic-gate 	if (ksp) {
5770Sstevel@tonic-gate 		ksp->ks_data = (void *) &system_pages_kstat;
5780Sstevel@tonic-gate 		ksp->ks_update = system_pages_kstat_update;
5790Sstevel@tonic-gate 		kstat_install(ksp);
5800Sstevel@tonic-gate 	}
5810Sstevel@tonic-gate 
5820Sstevel@tonic-gate 	ksp = kstat_create("poll", 0, "pollstats", "misc", KSTAT_TYPE_NAMED,
5830Sstevel@tonic-gate 	    pollstats_ndata, KSTAT_FLAG_VIRTUAL | KSTAT_FLAG_WRITABLE);
5840Sstevel@tonic-gate 
5850Sstevel@tonic-gate 	if (ksp) {
5860Sstevel@tonic-gate 		ksp->ks_data = pollstats_ptr;
5870Sstevel@tonic-gate 		kstat_install(ksp);
5880Sstevel@tonic-gate 	}
5890Sstevel@tonic-gate }
5900Sstevel@tonic-gate 
5910Sstevel@tonic-gate /*
5920Sstevel@tonic-gate  * Caller of this should ensure that the string pointed by src
5930Sstevel@tonic-gate  * doesn't change while kstat's lock is held. Not doing so defeats
5940Sstevel@tonic-gate  * kstat's snapshot strategy as explained in <sys/kstat.h>
5950Sstevel@tonic-gate  */
5960Sstevel@tonic-gate void
5970Sstevel@tonic-gate kstat_named_setstr(kstat_named_t *knp, const char *src)
5980Sstevel@tonic-gate {
5990Sstevel@tonic-gate 	if (knp->data_type != KSTAT_DATA_STRING)
6000Sstevel@tonic-gate 		panic("kstat_named_setstr('%p', '%p'): "
6010Sstevel@tonic-gate 		    "named kstat is not of type KSTAT_DATA_STRING", knp, src);
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate 	KSTAT_NAMED_STR_PTR(knp) = (char *)src;
6040Sstevel@tonic-gate 	if (src != NULL)
6050Sstevel@tonic-gate 		KSTAT_NAMED_STR_BUFLEN(knp) = strlen(src) + 1;
6060Sstevel@tonic-gate 	else
6070Sstevel@tonic-gate 		KSTAT_NAMED_STR_BUFLEN(knp) = 0;
6080Sstevel@tonic-gate }
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate void
611*2951Selowe kstat_set_string(char *dst, const char *src)
6120Sstevel@tonic-gate {
6130Sstevel@tonic-gate 	bzero(dst, KSTAT_STRLEN);
6140Sstevel@tonic-gate 	(void) strncpy(dst, src, KSTAT_STRLEN - 1);
6150Sstevel@tonic-gate }
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate void
618*2951Selowe kstat_named_init(kstat_named_t *knp, const char *name, uchar_t data_type)
6190Sstevel@tonic-gate {
6200Sstevel@tonic-gate 	kstat_set_string(knp->name, name);
6210Sstevel@tonic-gate 	knp->data_type = data_type;
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate 	if (data_type == KSTAT_DATA_STRING)
6240Sstevel@tonic-gate 		kstat_named_setstr(knp, NULL);
6250Sstevel@tonic-gate }
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate void
628*2951Selowe kstat_timer_init(kstat_timer_t *ktp, const char *name)
6290Sstevel@tonic-gate {
6300Sstevel@tonic-gate 	kstat_set_string(ktp->name, name);
6310Sstevel@tonic-gate }
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate /* ARGSUSED */
6340Sstevel@tonic-gate static int
6350Sstevel@tonic-gate default_kstat_update(kstat_t *ksp, int rw)
6360Sstevel@tonic-gate {
6370Sstevel@tonic-gate 	uint_t i;
6380Sstevel@tonic-gate 	size_t len = 0;
6390Sstevel@tonic-gate 	kstat_named_t *knp;
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 	/*
6420Sstevel@tonic-gate 	 * Named kstats with variable-length long strings have a standard
6430Sstevel@tonic-gate 	 * way of determining how much space is needed to hold the snapshot:
6440Sstevel@tonic-gate 	 */
6450Sstevel@tonic-gate 	if (ksp->ks_data != NULL && ksp->ks_type == KSTAT_TYPE_NAMED &&
6460Sstevel@tonic-gate 	    (ksp->ks_flags & KSTAT_FLAG_VAR_SIZE)) {
6470Sstevel@tonic-gate 
6480Sstevel@tonic-gate 		/*
6490Sstevel@tonic-gate 		 * Add in the space required for the strings
6500Sstevel@tonic-gate 		 */
6510Sstevel@tonic-gate 		knp = KSTAT_NAMED_PTR(ksp);
6520Sstevel@tonic-gate 		for (i = 0; i < ksp->ks_ndata; i++, knp++) {
6530Sstevel@tonic-gate 			if (knp->data_type == KSTAT_DATA_STRING)
6540Sstevel@tonic-gate 				len += KSTAT_NAMED_STR_BUFLEN(knp);
6550Sstevel@tonic-gate 		}
6560Sstevel@tonic-gate 		ksp->ks_data_size =
6570Sstevel@tonic-gate 		    ksp->ks_ndata * sizeof (kstat_named_t) + len;
6580Sstevel@tonic-gate 	}
6590Sstevel@tonic-gate 	return (0);
6600Sstevel@tonic-gate }
6610Sstevel@tonic-gate 
6620Sstevel@tonic-gate static int
6630Sstevel@tonic-gate default_kstat_snapshot(kstat_t *ksp, void *buf, int rw)
6640Sstevel@tonic-gate {
6650Sstevel@tonic-gate 	kstat_io_t *kiop;
6660Sstevel@tonic-gate 	hrtime_t cur_time;
6670Sstevel@tonic-gate 	size_t	namedsz;
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 	ksp->ks_snaptime = cur_time = gethrtime();
6700Sstevel@tonic-gate 
6710Sstevel@tonic-gate 	if (rw == KSTAT_WRITE) {
6720Sstevel@tonic-gate 		if (!(ksp->ks_flags & KSTAT_FLAG_WRITABLE))
6730Sstevel@tonic-gate 			return (EACCES);
6740Sstevel@tonic-gate 		bcopy(buf, ksp->ks_data, ksp->ks_data_size);
6750Sstevel@tonic-gate 		return (0);
6760Sstevel@tonic-gate 	}
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate 	/*
6790Sstevel@tonic-gate 	 * KSTAT_TYPE_NAMED kstats are defined to have ks_ndata
6800Sstevel@tonic-gate 	 * number of kstat_named_t structures, followed by an optional
6810Sstevel@tonic-gate 	 * string segment. The ks_data generally holds only the
6820Sstevel@tonic-gate 	 * kstat_named_t structures. So we copy it first. The strings,
6830Sstevel@tonic-gate 	 * if any, are copied below. For other kstat types, ks_data holds the
6840Sstevel@tonic-gate 	 * entire buffer.
6850Sstevel@tonic-gate 	 */
6860Sstevel@tonic-gate 
6870Sstevel@tonic-gate 	namedsz = sizeof (kstat_named_t) * ksp->ks_ndata;
6880Sstevel@tonic-gate 	if (ksp->ks_type == KSTAT_TYPE_NAMED && ksp->ks_data_size > namedsz)
6890Sstevel@tonic-gate 		bcopy(ksp->ks_data, buf, namedsz);
6900Sstevel@tonic-gate 	else
6910Sstevel@tonic-gate 		bcopy(ksp->ks_data, buf, ksp->ks_data_size);
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate 	/*
6940Sstevel@tonic-gate 	 * Apply kstat type-specific data massaging
6950Sstevel@tonic-gate 	 */
6960Sstevel@tonic-gate 	switch (ksp->ks_type) {
6970Sstevel@tonic-gate 
6980Sstevel@tonic-gate 	case KSTAT_TYPE_IO:
6990Sstevel@tonic-gate 		/*
7000Sstevel@tonic-gate 		 * Normalize time units and deal with incomplete transactions
7010Sstevel@tonic-gate 		 */
7020Sstevel@tonic-gate 		kiop = (kstat_io_t *)buf;
7030Sstevel@tonic-gate 
7040Sstevel@tonic-gate 		scalehrtime(&kiop->wtime);
7050Sstevel@tonic-gate 		scalehrtime(&kiop->wlentime);
7060Sstevel@tonic-gate 		scalehrtime(&kiop->wlastupdate);
7070Sstevel@tonic-gate 		scalehrtime(&kiop->rtime);
7080Sstevel@tonic-gate 		scalehrtime(&kiop->rlentime);
7090Sstevel@tonic-gate 		scalehrtime(&kiop->rlastupdate);
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate 		if (kiop->wcnt != 0) {
7122723Scth 			/* like kstat_waitq_exit */
7130Sstevel@tonic-gate 			hrtime_t wfix = cur_time - kiop->wlastupdate;
7142723Scth 			kiop->wlastupdate = cur_time;
7150Sstevel@tonic-gate 			kiop->wlentime += kiop->wcnt * wfix;
7162723Scth 			kiop->wtime += wfix;
7170Sstevel@tonic-gate 		}
7182723Scth 
7190Sstevel@tonic-gate 		if (kiop->rcnt != 0) {
7202723Scth 			/* like kstat_runq_exit */
7210Sstevel@tonic-gate 			hrtime_t rfix = cur_time - kiop->rlastupdate;
7222723Scth 			kiop->rlastupdate = cur_time;
7230Sstevel@tonic-gate 			kiop->rlentime += kiop->rcnt * rfix;
7242723Scth 			kiop->rtime += rfix;
7250Sstevel@tonic-gate 		}
7260Sstevel@tonic-gate 		break;
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate 	case KSTAT_TYPE_NAMED:
7290Sstevel@tonic-gate 		/*
7300Sstevel@tonic-gate 		 * Massage any long strings in at the end of the buffer
7310Sstevel@tonic-gate 		 */
7320Sstevel@tonic-gate 		if (ksp->ks_data_size > namedsz) {
7330Sstevel@tonic-gate 			uint_t i;
7340Sstevel@tonic-gate 			kstat_named_t *knp = buf;
7350Sstevel@tonic-gate 			char *dst = (char *)(knp + ksp->ks_ndata);
7360Sstevel@tonic-gate 			/*
7370Sstevel@tonic-gate 			 * Copy strings and update pointers
7380Sstevel@tonic-gate 			 */
7390Sstevel@tonic-gate 			for (i = 0; i < ksp->ks_ndata; i++, knp++) {
7400Sstevel@tonic-gate 				if (knp->data_type == KSTAT_DATA_STRING &&
7410Sstevel@tonic-gate 				    KSTAT_NAMED_STR_PTR(knp) != NULL) {
7420Sstevel@tonic-gate 					bcopy(KSTAT_NAMED_STR_PTR(knp), dst,
7430Sstevel@tonic-gate 					    KSTAT_NAMED_STR_BUFLEN(knp));
7440Sstevel@tonic-gate 					KSTAT_NAMED_STR_PTR(knp) = dst;
7450Sstevel@tonic-gate 					dst += KSTAT_NAMED_STR_BUFLEN(knp);
7460Sstevel@tonic-gate 				}
7470Sstevel@tonic-gate 			}
7480Sstevel@tonic-gate 			ASSERT(dst <= ((char *)buf + ksp->ks_data_size));
7490Sstevel@tonic-gate 		}
7500Sstevel@tonic-gate 		break;
7510Sstevel@tonic-gate 	}
7520Sstevel@tonic-gate 	return (0);
7530Sstevel@tonic-gate }
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate static int
7560Sstevel@tonic-gate header_kstat_update(kstat_t *header_ksp, int rw)
7570Sstevel@tonic-gate {
7580Sstevel@tonic-gate 	int nkstats = 0;
7590Sstevel@tonic-gate 	ekstat_t *e;
7600Sstevel@tonic-gate 	avl_tree_t *t = &kstat_avl_bykid;
7610Sstevel@tonic-gate 	zoneid_t zoneid;
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
7640Sstevel@tonic-gate 		return (EACCES);
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&kstat_chain_lock));
7670Sstevel@tonic-gate 
7680Sstevel@tonic-gate 	zoneid = getzoneid();
7690Sstevel@tonic-gate 	for (e = avl_first(t); e != NULL; e = avl_walk(t, e, AVL_AFTER)) {
7700Sstevel@tonic-gate 		if (kstat_zone_find((kstat_t *)e, zoneid)) {
7710Sstevel@tonic-gate 			nkstats++;
7720Sstevel@tonic-gate 		}
7730Sstevel@tonic-gate 	}
7740Sstevel@tonic-gate 	header_ksp->ks_ndata = nkstats;
7750Sstevel@tonic-gate 	header_ksp->ks_data_size = nkstats * sizeof (kstat_t);
7760Sstevel@tonic-gate 	return (0);
7770Sstevel@tonic-gate }
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate /*
7800Sstevel@tonic-gate  * Copy out the data section of kstat 0, which consists of the list
7810Sstevel@tonic-gate  * of all kstat headers.  By specification, these headers must be
7820Sstevel@tonic-gate  * copied out in order of increasing KID.
7830Sstevel@tonic-gate  */
7840Sstevel@tonic-gate static int
7850Sstevel@tonic-gate header_kstat_snapshot(kstat_t *header_ksp, void *buf, int rw)
7860Sstevel@tonic-gate {
7870Sstevel@tonic-gate 	ekstat_t *e;
7880Sstevel@tonic-gate 	avl_tree_t *t = &kstat_avl_bykid;
7890Sstevel@tonic-gate 	zoneid_t zoneid;
7900Sstevel@tonic-gate 
7910Sstevel@tonic-gate 	header_ksp->ks_snaptime = gethrtime();
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
7940Sstevel@tonic-gate 		return (EACCES);
7950Sstevel@tonic-gate 
7960Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&kstat_chain_lock));
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate 	zoneid = getzoneid();
7990Sstevel@tonic-gate 	for (e = avl_first(t); e != NULL; e = avl_walk(t, e, AVL_AFTER)) {
8000Sstevel@tonic-gate 		if (kstat_zone_find((kstat_t *)e, zoneid)) {
8010Sstevel@tonic-gate 			bcopy(&e->e_ks, buf, sizeof (kstat_t));
8020Sstevel@tonic-gate 			buf = (char *)buf + sizeof (kstat_t);
8030Sstevel@tonic-gate 		}
8040Sstevel@tonic-gate 	}
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate 	return (0);
8070Sstevel@tonic-gate }
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate /* ARGSUSED */
8100Sstevel@tonic-gate static int
8110Sstevel@tonic-gate system_misc_kstat_update(kstat_t *ksp, int rw)
8120Sstevel@tonic-gate {
8130Sstevel@tonic-gate 	int myncpus = ncpus;
8140Sstevel@tonic-gate 	int *loadavgp = &avenrun[0];
8150Sstevel@tonic-gate 	int loadavg[LOADAVG_NSTATS];
8160Sstevel@tonic-gate 
8170Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
8180Sstevel@tonic-gate 		return (EACCES);
8190Sstevel@tonic-gate 
8200Sstevel@tonic-gate 	if (!INGLOBALZONE(curproc)) {
8210Sstevel@tonic-gate 		/*
8220Sstevel@tonic-gate 		 * Here we grab cpu_lock which is OK as long as no-one in the
8230Sstevel@tonic-gate 		 * future attempts to lookup this particular kstat
8240Sstevel@tonic-gate 		 * (unix:0:system_misc) while holding cpu_lock.
8250Sstevel@tonic-gate 		 */
8260Sstevel@tonic-gate 		mutex_enter(&cpu_lock);
8270Sstevel@tonic-gate 		if (pool_pset_enabled()) {
8280Sstevel@tonic-gate 			psetid_t mypsid = zone_pset_get(curproc->p_zone);
8290Sstevel@tonic-gate 			int error;
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate 			myncpus = zone_ncpus_get(curproc->p_zone);
8320Sstevel@tonic-gate 			ASSERT(myncpus > 0);
8330Sstevel@tonic-gate 			error = cpupart_get_loadavg(mypsid, &loadavg[0],
8340Sstevel@tonic-gate 			    LOADAVG_NSTATS);
8350Sstevel@tonic-gate 			ASSERT(error == 0);
8360Sstevel@tonic-gate 			loadavgp = &loadavg[0];
8370Sstevel@tonic-gate 		}
8380Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
8390Sstevel@tonic-gate 	}
8400Sstevel@tonic-gate 
8410Sstevel@tonic-gate 	system_misc_kstat.ncpus.value.ui32		= (uint32_t)myncpus;
8420Sstevel@tonic-gate 	system_misc_kstat.lbolt.value.ui32		= (uint32_t)lbolt;
8430Sstevel@tonic-gate 	system_misc_kstat.deficit.value.ui32		= (uint32_t)deficit;
8440Sstevel@tonic-gate 	system_misc_kstat.clk_intr.value.ui32		= (uint32_t)lbolt;
8450Sstevel@tonic-gate 	system_misc_kstat.vac.value.ui32		= (uint32_t)vac;
8460Sstevel@tonic-gate 	system_misc_kstat.nproc.value.ui32		= (uint32_t)nproc;
8470Sstevel@tonic-gate 	system_misc_kstat.avenrun_1min.value.ui32	= (uint32_t)loadavgp[0];
8480Sstevel@tonic-gate 	system_misc_kstat.avenrun_5min.value.ui32	= (uint32_t)loadavgp[1];
8490Sstevel@tonic-gate 	system_misc_kstat.avenrun_15min.value.ui32	= (uint32_t)loadavgp[2];
8500Sstevel@tonic-gate 	system_misc_kstat.boot_time.value.ui32		= (uint32_t)boot_time;
8510Sstevel@tonic-gate 	return (0);
8520Sstevel@tonic-gate }
8530Sstevel@tonic-gate 
8540Sstevel@tonic-gate #ifdef	__sparc
8550Sstevel@tonic-gate extern caddr_t	econtig32;
8560Sstevel@tonic-gate #else	/* !__sparc */
8570Sstevel@tonic-gate extern caddr_t	econtig;
8580Sstevel@tonic-gate #endif	/* __sparc */
8590Sstevel@tonic-gate 
8600Sstevel@tonic-gate extern struct vnode kvp;
8610Sstevel@tonic-gate 
8620Sstevel@tonic-gate /* ARGSUSED */
8630Sstevel@tonic-gate static int
8640Sstevel@tonic-gate system_pages_kstat_update(kstat_t *ksp, int rw)
8650Sstevel@tonic-gate {
8660Sstevel@tonic-gate 	kobj_stat_t kobj_stat;
8670Sstevel@tonic-gate 
8680Sstevel@tonic-gate 	if (rw == KSTAT_WRITE) {
8690Sstevel@tonic-gate 		return (EACCES);
8700Sstevel@tonic-gate 	}
8710Sstevel@tonic-gate 
8720Sstevel@tonic-gate 	kobj_stat_get(&kobj_stat);
8730Sstevel@tonic-gate 	system_pages_kstat.physmem.value.ul	= (ulong_t)physmem;
8740Sstevel@tonic-gate 	system_pages_kstat.nalloc.value.ul	= kobj_stat.nalloc;
8750Sstevel@tonic-gate 	system_pages_kstat.nfree.value.ul	= kobj_stat.nfree;
8760Sstevel@tonic-gate 	system_pages_kstat.nalloc_calls.value.ul = kobj_stat.nalloc_calls;
8770Sstevel@tonic-gate 	system_pages_kstat.nfree_calls.value.ul	= kobj_stat.nfree_calls;
8780Sstevel@tonic-gate 	system_pages_kstat.kernelbase.value.ul	= (ulong_t)KERNELBASE;
8790Sstevel@tonic-gate 
8800Sstevel@tonic-gate #ifdef	__sparc
8810Sstevel@tonic-gate 	/*
8820Sstevel@tonic-gate 	 * kstat should REALLY be modified to also report kmem64_base and
8830Sstevel@tonic-gate 	 * kmem64_end (see sun4u/os/startup.c), as the virtual address range
8840Sstevel@tonic-gate 	 * [ kernelbase .. econtig ] no longer is truly reflective of the
8850Sstevel@tonic-gate 	 * kernel's vallocs...
8860Sstevel@tonic-gate 	 */
8870Sstevel@tonic-gate 	system_pages_kstat.econtig.value.ul	= (ulong_t)econtig32;
8880Sstevel@tonic-gate #else	/* !__sparc */
8890Sstevel@tonic-gate 	system_pages_kstat.econtig.value.ul	= (ulong_t)econtig;
8900Sstevel@tonic-gate #endif	/* __sparc */
8910Sstevel@tonic-gate 
8920Sstevel@tonic-gate 	system_pages_kstat.freemem.value.ul	= (ulong_t)freemem;
8930Sstevel@tonic-gate 	system_pages_kstat.availrmem.value.ul	= (ulong_t)availrmem;
8940Sstevel@tonic-gate 	system_pages_kstat.lotsfree.value.ul	= (ulong_t)lotsfree;
8950Sstevel@tonic-gate 	system_pages_kstat.desfree.value.ul	= (ulong_t)desfree;
8960Sstevel@tonic-gate 	system_pages_kstat.minfree.value.ul	= (ulong_t)minfree;
8970Sstevel@tonic-gate 	system_pages_kstat.fastscan.value.ul	= (ulong_t)fastscan;
8980Sstevel@tonic-gate 	system_pages_kstat.slowscan.value.ul	= (ulong_t)slowscan;
8990Sstevel@tonic-gate 	system_pages_kstat.nscan.value.ul	= (ulong_t)nscan;
9000Sstevel@tonic-gate 	system_pages_kstat.desscan.value.ul	= (ulong_t)desscan;
9010Sstevel@tonic-gate 	system_pages_kstat.pagesfree.value.ul	= (ulong_t)freemem;
9020Sstevel@tonic-gate 	system_pages_kstat.pageslocked.value.ul	= (ulong_t)(availrmem_initial -
9030Sstevel@tonic-gate 	    availrmem);
9040Sstevel@tonic-gate 	system_pages_kstat.pagestotal.value.ul	= (ulong_t)total_pages;
9050Sstevel@tonic-gate 	/*
9060Sstevel@tonic-gate 	 * pp_kernel represents total pages used by the kernel since the
9070Sstevel@tonic-gate 	 * startup. This formula takes into account the boottime kernel
9080Sstevel@tonic-gate 	 * footprint and also considers the availrmem changes because of
9090Sstevel@tonic-gate 	 * user explicit page locking.
9100Sstevel@tonic-gate 	 */
9110Sstevel@tonic-gate 	system_pages_kstat.pp_kernel.value.ul   = (ulong_t)(physinstalled -
9120Sstevel@tonic-gate 		obp_pages - availrmem - k_anoninfo.ani_mem_resv -
9130Sstevel@tonic-gate 		anon_segkp_pages_locked - segvn_pages_locked -
9140Sstevel@tonic-gate 		pages_locked - pages_claimed - pages_useclaim);
9150Sstevel@tonic-gate 
9160Sstevel@tonic-gate 	return (0);
9170Sstevel@tonic-gate }
9180Sstevel@tonic-gate 
9190Sstevel@tonic-gate kstat_t *
920*2951Selowe kstat_create(const char *ks_module, int ks_instance, const char *ks_name,
921*2951Selowe     const char *ks_class, uchar_t ks_type, uint_t ks_ndata, uchar_t ks_flags)
9220Sstevel@tonic-gate {
9230Sstevel@tonic-gate 	return (kstat_create_zone(ks_module, ks_instance, ks_name, ks_class,
9240Sstevel@tonic-gate 		    ks_type, ks_ndata, ks_flags, ALL_ZONES));
9250Sstevel@tonic-gate }
9260Sstevel@tonic-gate 
9270Sstevel@tonic-gate /*
9280Sstevel@tonic-gate  * Allocate and initialize a kstat structure.  Or, if a dormant kstat with
9290Sstevel@tonic-gate  * the specified name exists, reactivate it.  Returns a pointer to the kstat
9300Sstevel@tonic-gate  * on success, NULL on failure.  The kstat will not be visible to the
9310Sstevel@tonic-gate  * kstat driver until kstat_install().
9320Sstevel@tonic-gate  */
9330Sstevel@tonic-gate kstat_t *
934*2951Selowe kstat_create_zone(const char *ks_module, int ks_instance, const char *ks_name,
935*2951Selowe     const char *ks_class, uchar_t ks_type, uint_t ks_ndata, uchar_t ks_flags,
9360Sstevel@tonic-gate     zoneid_t ks_zoneid)
9370Sstevel@tonic-gate {
9380Sstevel@tonic-gate 	size_t ks_data_size;
9390Sstevel@tonic-gate 	kstat_t *ksp;
9400Sstevel@tonic-gate 	ekstat_t *e;
9410Sstevel@tonic-gate 	avl_index_t where;
9420Sstevel@tonic-gate 	char namebuf[KSTAT_STRLEN + 16];
9430Sstevel@tonic-gate 
9440Sstevel@tonic-gate 	if (avl_numnodes(&kstat_avl_bykid) == 0) {
9450Sstevel@tonic-gate 		avl_create(&kstat_avl_bykid, kstat_compare_bykid,
9460Sstevel@tonic-gate 		    sizeof (ekstat_t), offsetof(struct ekstat, e_avl_bykid));
9470Sstevel@tonic-gate 
9480Sstevel@tonic-gate 		avl_create(&kstat_avl_byname, kstat_compare_byname,
9490Sstevel@tonic-gate 		    sizeof (ekstat_t), offsetof(struct ekstat, e_avl_byname));
9500Sstevel@tonic-gate 	}
9510Sstevel@tonic-gate 
9520Sstevel@tonic-gate 	/*
9530Sstevel@tonic-gate 	 * If ks_name == NULL, set the ks_name to <module><instance>.
9540Sstevel@tonic-gate 	 */
9550Sstevel@tonic-gate 	if (ks_name == NULL) {
9560Sstevel@tonic-gate 		char buf[KSTAT_STRLEN];
9570Sstevel@tonic-gate 		kstat_set_string(buf, ks_module);
9580Sstevel@tonic-gate 		(void) sprintf(namebuf, "%s%d", buf, ks_instance);
9590Sstevel@tonic-gate 		ks_name = namebuf;
9600Sstevel@tonic-gate 	}
9610Sstevel@tonic-gate 
9620Sstevel@tonic-gate 	/*
9630Sstevel@tonic-gate 	 * Make sure it's a valid kstat data type
9640Sstevel@tonic-gate 	 */
9650Sstevel@tonic-gate 	if (ks_type >= KSTAT_NUM_TYPES) {
9660Sstevel@tonic-gate 		cmn_err(CE_WARN, "kstat_create('%s', %d, '%s'): "
9670Sstevel@tonic-gate 			"invalid kstat type %d",
9680Sstevel@tonic-gate 			ks_module, ks_instance, ks_name, ks_type);
9690Sstevel@tonic-gate 		return (NULL);
9700Sstevel@tonic-gate 	}
9710Sstevel@tonic-gate 
9720Sstevel@tonic-gate 	/*
9730Sstevel@tonic-gate 	 * Don't allow persistent virtual kstats -- it makes no sense.
9740Sstevel@tonic-gate 	 * ks_data points to garbage when the client goes away.
9750Sstevel@tonic-gate 	 */
9760Sstevel@tonic-gate 	if ((ks_flags & KSTAT_FLAG_PERSISTENT) &&
9770Sstevel@tonic-gate 	    (ks_flags & KSTAT_FLAG_VIRTUAL)) {
9780Sstevel@tonic-gate 		cmn_err(CE_WARN, "kstat_create('%s', %d, '%s'): "
9790Sstevel@tonic-gate 			"cannot create persistent virtual kstat",
9800Sstevel@tonic-gate 			ks_module, ks_instance, ks_name);
9810Sstevel@tonic-gate 		return (NULL);
9820Sstevel@tonic-gate 	}
9830Sstevel@tonic-gate 
9840Sstevel@tonic-gate 	/*
9850Sstevel@tonic-gate 	 * Don't allow variable-size physical kstats, since the framework's
9860Sstevel@tonic-gate 	 * memory allocation for physical kstat data is fixed at creation time.
9870Sstevel@tonic-gate 	 */
9880Sstevel@tonic-gate 	if ((ks_flags & KSTAT_FLAG_VAR_SIZE) &&
9890Sstevel@tonic-gate 	    !(ks_flags & KSTAT_FLAG_VIRTUAL)) {
9900Sstevel@tonic-gate 		cmn_err(CE_WARN, "kstat_create('%s', %d, '%s'): "
9910Sstevel@tonic-gate 			"cannot create variable-size physical kstat",
9920Sstevel@tonic-gate 			ks_module, ks_instance, ks_name);
9930Sstevel@tonic-gate 		return (NULL);
9940Sstevel@tonic-gate 	}
9950Sstevel@tonic-gate 
9960Sstevel@tonic-gate 	/*
9970Sstevel@tonic-gate 	 * Make sure the number of data fields is within legal range
9980Sstevel@tonic-gate 	 */
9990Sstevel@tonic-gate 	if (ks_ndata < kstat_data_type[ks_type].min_ndata ||
10000Sstevel@tonic-gate 	    ks_ndata > kstat_data_type[ks_type].max_ndata) {
10010Sstevel@tonic-gate 		cmn_err(CE_WARN, "kstat_create('%s', %d, '%s'): "
10020Sstevel@tonic-gate 			"ks_ndata=%d out of range [%d, %d]",
10030Sstevel@tonic-gate 			ks_module, ks_instance, ks_name, (int)ks_ndata,
10040Sstevel@tonic-gate 			kstat_data_type[ks_type].min_ndata,
10050Sstevel@tonic-gate 			kstat_data_type[ks_type].max_ndata);
10060Sstevel@tonic-gate 		return (NULL);
10070Sstevel@tonic-gate 	}
10080Sstevel@tonic-gate 
10090Sstevel@tonic-gate 	ks_data_size = kstat_data_type[ks_type].size * ks_ndata;
10100Sstevel@tonic-gate 
10110Sstevel@tonic-gate 	/*
10120Sstevel@tonic-gate 	 * If the named kstat already exists and is dormant, reactivate it.
10130Sstevel@tonic-gate 	 */
10140Sstevel@tonic-gate 	ksp = kstat_hold_byname(ks_module, ks_instance, ks_name, ks_zoneid);
10150Sstevel@tonic-gate 	if (ksp != NULL) {
10160Sstevel@tonic-gate 		if (!(ksp->ks_flags & KSTAT_FLAG_DORMANT)) {
10170Sstevel@tonic-gate 			/*
10180Sstevel@tonic-gate 			 * The named kstat exists but is not dormant --
10190Sstevel@tonic-gate 			 * this is a kstat namespace collision.
10200Sstevel@tonic-gate 			 */
10210Sstevel@tonic-gate 			kstat_rele(ksp);
10220Sstevel@tonic-gate 			cmn_err(CE_WARN,
10230Sstevel@tonic-gate 			    "kstat_create('%s', %d, '%s'): namespace collision",
10240Sstevel@tonic-gate 			    ks_module, ks_instance, ks_name);
10250Sstevel@tonic-gate 			return (NULL);
10260Sstevel@tonic-gate 		}
10270Sstevel@tonic-gate 		if ((strcmp(ksp->ks_class, ks_class) != 0) ||
10280Sstevel@tonic-gate 		    (ksp->ks_type != ks_type) ||
10290Sstevel@tonic-gate 		    (ksp->ks_ndata != ks_ndata) ||
10300Sstevel@tonic-gate 		    (ks_flags & KSTAT_FLAG_VIRTUAL)) {
10310Sstevel@tonic-gate 			/*
10320Sstevel@tonic-gate 			 * The name is the same, but the other key parameters
10330Sstevel@tonic-gate 			 * differ from those of the dormant kstat -- bogus.
10340Sstevel@tonic-gate 			 */
10350Sstevel@tonic-gate 			kstat_rele(ksp);
10360Sstevel@tonic-gate 			cmn_err(CE_WARN, "kstat_create('%s', %d, '%s'): "
10370Sstevel@tonic-gate 				"invalid reactivation of dormant kstat",
10380Sstevel@tonic-gate 				ks_module, ks_instance, ks_name);
10390Sstevel@tonic-gate 			return (NULL);
10400Sstevel@tonic-gate 		}
10410Sstevel@tonic-gate 		/*
10420Sstevel@tonic-gate 		 * Return dormant kstat pointer to caller.  As usual,
10430Sstevel@tonic-gate 		 * the kstat is marked invalid until kstat_install().
10440Sstevel@tonic-gate 		 */
10450Sstevel@tonic-gate 		ksp->ks_flags |= KSTAT_FLAG_INVALID;
10460Sstevel@tonic-gate 		kstat_rele(ksp);
10470Sstevel@tonic-gate 		return (ksp);
10480Sstevel@tonic-gate 	}
10490Sstevel@tonic-gate 
10500Sstevel@tonic-gate 	/*
10510Sstevel@tonic-gate 	 * Allocate memory for the new kstat header and, if this is a physical
10520Sstevel@tonic-gate 	 * kstat, the data section.
10530Sstevel@tonic-gate 	 */
10540Sstevel@tonic-gate 	e = kstat_alloc(ks_flags & KSTAT_FLAG_VIRTUAL ? 0 : ks_data_size);
10550Sstevel@tonic-gate 	if (e == NULL) {
10560Sstevel@tonic-gate 		cmn_err(CE_NOTE, "kstat_create('%s', %d, '%s'): "
10570Sstevel@tonic-gate 			"insufficient kernel memory",
10580Sstevel@tonic-gate 			ks_module, ks_instance, ks_name);
10590Sstevel@tonic-gate 		return (NULL);
10600Sstevel@tonic-gate 	}
10610Sstevel@tonic-gate 
10620Sstevel@tonic-gate 	/*
10630Sstevel@tonic-gate 	 * Initialize as many fields as we can.  The caller may reset
10640Sstevel@tonic-gate 	 * ks_lock, ks_update, ks_private, and ks_snapshot as necessary.
10650Sstevel@tonic-gate 	 * Creators of virtual kstats may also reset ks_data.  It is
10660Sstevel@tonic-gate 	 * also up to the caller to initialize the kstat data section,
10670Sstevel@tonic-gate 	 * if necessary.  All initialization must be complete before
10680Sstevel@tonic-gate 	 * calling kstat_install().
10690Sstevel@tonic-gate 	 */
10700Sstevel@tonic-gate 	e->e_zone.zoneid = ks_zoneid;
10710Sstevel@tonic-gate 	e->e_zone.next = NULL;
10720Sstevel@tonic-gate 
10730Sstevel@tonic-gate 	ksp = &e->e_ks;
10740Sstevel@tonic-gate 	ksp->ks_crtime		= gethrtime();
10750Sstevel@tonic-gate 	kstat_set_string(ksp->ks_module, ks_module);
10760Sstevel@tonic-gate 	ksp->ks_instance	= ks_instance;
10770Sstevel@tonic-gate 	kstat_set_string(ksp->ks_name, ks_name);
10780Sstevel@tonic-gate 	ksp->ks_type		= ks_type;
10790Sstevel@tonic-gate 	kstat_set_string(ksp->ks_class, ks_class);
10800Sstevel@tonic-gate 	ksp->ks_flags		= ks_flags | KSTAT_FLAG_INVALID;
10810Sstevel@tonic-gate 	if (ks_flags & KSTAT_FLAG_VIRTUAL)
10820Sstevel@tonic-gate 		ksp->ks_data	= NULL;
10830Sstevel@tonic-gate 	else
10840Sstevel@tonic-gate 		ksp->ks_data	= (void *)(e + 1);
10850Sstevel@tonic-gate 	ksp->ks_ndata		= ks_ndata;
10860Sstevel@tonic-gate 	ksp->ks_data_size	= ks_data_size;
10870Sstevel@tonic-gate 	ksp->ks_snaptime	= ksp->ks_crtime;
10880Sstevel@tonic-gate 	ksp->ks_update		= default_kstat_update;
10890Sstevel@tonic-gate 	ksp->ks_private		= NULL;
10900Sstevel@tonic-gate 	ksp->ks_snapshot	= default_kstat_snapshot;
10910Sstevel@tonic-gate 	ksp->ks_lock		= NULL;
10920Sstevel@tonic-gate 
10930Sstevel@tonic-gate 	mutex_enter(&kstat_chain_lock);
10940Sstevel@tonic-gate 
10950Sstevel@tonic-gate 	/*
10960Sstevel@tonic-gate 	 * Add our kstat to the AVL trees.
10970Sstevel@tonic-gate 	 */
10980Sstevel@tonic-gate 	if (avl_find(&kstat_avl_byname, e, &where) != NULL) {
10990Sstevel@tonic-gate 		mutex_exit(&kstat_chain_lock);
11000Sstevel@tonic-gate 		cmn_err(CE_WARN,
11010Sstevel@tonic-gate 		    "kstat_create('%s', %d, '%s'): namespace collision",
11020Sstevel@tonic-gate 		    ks_module, ks_instance, ks_name);
11030Sstevel@tonic-gate 		kstat_free(e);
11040Sstevel@tonic-gate 		return (NULL);
11050Sstevel@tonic-gate 	}
11060Sstevel@tonic-gate 	avl_insert(&kstat_avl_byname, e, where);
11070Sstevel@tonic-gate 
11080Sstevel@tonic-gate 	/*
11090Sstevel@tonic-gate 	 * Loop around until we find an unused KID.
11100Sstevel@tonic-gate 	 */
11110Sstevel@tonic-gate 	do {
11120Sstevel@tonic-gate 		ksp->ks_kid = kstat_chain_id++;
11130Sstevel@tonic-gate 	} while (avl_find(&kstat_avl_bykid, e, &where) != NULL);
11140Sstevel@tonic-gate 	avl_insert(&kstat_avl_bykid, e, where);
11150Sstevel@tonic-gate 
11160Sstevel@tonic-gate 	mutex_exit(&kstat_chain_lock);
11170Sstevel@tonic-gate 
11180Sstevel@tonic-gate 	return (ksp);
11190Sstevel@tonic-gate }
11200Sstevel@tonic-gate 
11210Sstevel@tonic-gate /*
11220Sstevel@tonic-gate  * Activate a fully initialized kstat and make it visible to /dev/kstat.
11230Sstevel@tonic-gate  */
11240Sstevel@tonic-gate void
11250Sstevel@tonic-gate kstat_install(kstat_t *ksp)
11260Sstevel@tonic-gate {
11270Sstevel@tonic-gate 	zoneid_t zoneid = ((ekstat_t *)ksp)->e_zone.zoneid;
11280Sstevel@tonic-gate 
11290Sstevel@tonic-gate 	/*
11300Sstevel@tonic-gate 	 * If this is a variable-size kstat, it MUST provide kstat data locking
11310Sstevel@tonic-gate 	 * to prevent data-size races with kstat readers.
11320Sstevel@tonic-gate 	 */
11330Sstevel@tonic-gate 	if ((ksp->ks_flags & KSTAT_FLAG_VAR_SIZE) && ksp->ks_lock == NULL) {
11340Sstevel@tonic-gate 		panic("kstat_install('%s', %d, '%s'): "
11350Sstevel@tonic-gate 		    "cannot create variable-size kstat without data lock",
11360Sstevel@tonic-gate 		    ksp->ks_module, ksp->ks_instance, ksp->ks_name);
11370Sstevel@tonic-gate 	}
11380Sstevel@tonic-gate 
11390Sstevel@tonic-gate 	if (kstat_hold_bykid(ksp->ks_kid, zoneid) != ksp) {
11400Sstevel@tonic-gate 		cmn_err(CE_WARN, "kstat_install(%p): does not exist",
11410Sstevel@tonic-gate 		    (void *)ksp);
11420Sstevel@tonic-gate 		return;
11430Sstevel@tonic-gate 	}
11440Sstevel@tonic-gate 
11450Sstevel@tonic-gate 	if (ksp->ks_type == KSTAT_TYPE_NAMED && ksp->ks_data != NULL) {
11460Sstevel@tonic-gate 		int has_long_strings = 0;
11470Sstevel@tonic-gate 		uint_t i;
11480Sstevel@tonic-gate 		kstat_named_t *knp = KSTAT_NAMED_PTR(ksp);
11490Sstevel@tonic-gate 
11500Sstevel@tonic-gate 		for (i = 0; i < ksp->ks_ndata; i++, knp++) {
11510Sstevel@tonic-gate 			if (knp->data_type == KSTAT_DATA_STRING) {
11520Sstevel@tonic-gate 				has_long_strings = 1;
11530Sstevel@tonic-gate 				break;
11540Sstevel@tonic-gate 			}
11550Sstevel@tonic-gate 		}
11560Sstevel@tonic-gate 		/*
11570Sstevel@tonic-gate 		 * It is an error for a named kstat with fields of
11580Sstevel@tonic-gate 		 * KSTAT_DATA_STRING to be non-virtual.
11590Sstevel@tonic-gate 		 */
11600Sstevel@tonic-gate 		if (has_long_strings && !(ksp->ks_flags & KSTAT_FLAG_VIRTUAL)) {
11610Sstevel@tonic-gate 			panic("kstat_install('%s', %d, '%s'): "
11620Sstevel@tonic-gate 			    "named kstat containing KSTAT_DATA_STRING "
11630Sstevel@tonic-gate 			    "is not virtual",
11640Sstevel@tonic-gate 			    ksp->ks_module, ksp->ks_instance,
11650Sstevel@tonic-gate 			    ksp->ks_name);
11660Sstevel@tonic-gate 		}
11670Sstevel@tonic-gate 		/*
11680Sstevel@tonic-gate 		 * The default snapshot routine does not handle KSTAT_WRITE
11690Sstevel@tonic-gate 		 * for long strings.
11700Sstevel@tonic-gate 		 */
11710Sstevel@tonic-gate 		if (has_long_strings && (ksp->ks_flags & KSTAT_FLAG_WRITABLE) &&
11720Sstevel@tonic-gate 		    (ksp->ks_snapshot == default_kstat_snapshot)) {
11730Sstevel@tonic-gate 			panic("kstat_install('%s', %d, '%s'): "
11740Sstevel@tonic-gate 			    "named kstat containing KSTAT_DATA_STRING "
11750Sstevel@tonic-gate 			    "is writable but uses default snapshot routine",
11760Sstevel@tonic-gate 			    ksp->ks_module, ksp->ks_instance, ksp->ks_name);
11770Sstevel@tonic-gate 		}
11780Sstevel@tonic-gate 	}
11790Sstevel@tonic-gate 
11800Sstevel@tonic-gate 	if (ksp->ks_flags & KSTAT_FLAG_DORMANT) {
11810Sstevel@tonic-gate 
11820Sstevel@tonic-gate 		/*
11830Sstevel@tonic-gate 		 * We are reactivating a dormant kstat.  Initialize the
11840Sstevel@tonic-gate 		 * caller's underlying data to the value it had when the
11850Sstevel@tonic-gate 		 * kstat went dormant, and mark the kstat as active.
11860Sstevel@tonic-gate 		 * Grab the provider's kstat lock if it's not already held.
11870Sstevel@tonic-gate 		 */
11880Sstevel@tonic-gate 		kmutex_t *lp = ksp->ks_lock;
11890Sstevel@tonic-gate 		if (lp != NULL && MUTEX_NOT_HELD(lp)) {
11900Sstevel@tonic-gate 			mutex_enter(lp);
11910Sstevel@tonic-gate 			(void) KSTAT_UPDATE(ksp, KSTAT_WRITE);
11920Sstevel@tonic-gate 			mutex_exit(lp);
11930Sstevel@tonic-gate 		} else {
11940Sstevel@tonic-gate 			(void) KSTAT_UPDATE(ksp, KSTAT_WRITE);
11950Sstevel@tonic-gate 		}
11960Sstevel@tonic-gate 		ksp->ks_flags &= ~KSTAT_FLAG_DORMANT;
11970Sstevel@tonic-gate 	}
11980Sstevel@tonic-gate 
11990Sstevel@tonic-gate 	/*
12000Sstevel@tonic-gate 	 * Now that the kstat is active, make it visible to the kstat driver.
12010Sstevel@tonic-gate 	 */
12020Sstevel@tonic-gate 	ksp->ks_flags &= ~KSTAT_FLAG_INVALID;
12030Sstevel@tonic-gate 	kstat_rele(ksp);
12040Sstevel@tonic-gate }
12050Sstevel@tonic-gate 
12060Sstevel@tonic-gate /*
12070Sstevel@tonic-gate  * Remove a kstat from the system.  Or, if it's a persistent kstat,
12080Sstevel@tonic-gate  * just update the data and mark it as dormant.
12090Sstevel@tonic-gate  */
12100Sstevel@tonic-gate void
12110Sstevel@tonic-gate kstat_delete(kstat_t *ksp)
12120Sstevel@tonic-gate {
12130Sstevel@tonic-gate 	kmutex_t *lp;
12140Sstevel@tonic-gate 	ekstat_t *e = (ekstat_t *)ksp;
12150Sstevel@tonic-gate 	zoneid_t zoneid = e->e_zone.zoneid;
12160Sstevel@tonic-gate 	kstat_zone_t *kz;
12170Sstevel@tonic-gate 
12180Sstevel@tonic-gate 	if (ksp == NULL)
12190Sstevel@tonic-gate 		return;
12200Sstevel@tonic-gate 
12210Sstevel@tonic-gate 	lp = ksp->ks_lock;
12220Sstevel@tonic-gate 
12230Sstevel@tonic-gate 	if (lp != NULL && MUTEX_HELD(lp)) {
12240Sstevel@tonic-gate 		panic("kstat_delete(%p): caller holds data lock %p",
12250Sstevel@tonic-gate 		    (void *)ksp, (void *)lp);
12260Sstevel@tonic-gate 	}
12270Sstevel@tonic-gate 
12280Sstevel@tonic-gate 	if (kstat_hold_bykid(ksp->ks_kid, zoneid) != ksp) {
12290Sstevel@tonic-gate 		cmn_err(CE_WARN, "kstat_delete(%p): does not exist",
12300Sstevel@tonic-gate 		    (void *)ksp);
12310Sstevel@tonic-gate 		return;
12320Sstevel@tonic-gate 	}
12330Sstevel@tonic-gate 
12340Sstevel@tonic-gate 	if (ksp->ks_flags & KSTAT_FLAG_PERSISTENT) {
12350Sstevel@tonic-gate 		/*
12360Sstevel@tonic-gate 		 * Update the data one last time, so that all activity
12370Sstevel@tonic-gate 		 * prior to going dormant has been accounted for.
12380Sstevel@tonic-gate 		 */
12390Sstevel@tonic-gate 		KSTAT_ENTER(ksp);
12400Sstevel@tonic-gate 		(void) KSTAT_UPDATE(ksp, KSTAT_READ);
12410Sstevel@tonic-gate 		KSTAT_EXIT(ksp);
12420Sstevel@tonic-gate 
12430Sstevel@tonic-gate 		/*
12440Sstevel@tonic-gate 		 * Mark the kstat as dormant and restore caller-modifiable
12450Sstevel@tonic-gate 		 * fields to default values, so the kstat is readable during
12460Sstevel@tonic-gate 		 * the dormant phase.
12470Sstevel@tonic-gate 		 */
12480Sstevel@tonic-gate 		ksp->ks_flags |= KSTAT_FLAG_DORMANT;
12490Sstevel@tonic-gate 		ksp->ks_lock = NULL;
12500Sstevel@tonic-gate 		ksp->ks_update = default_kstat_update;
12510Sstevel@tonic-gate 		ksp->ks_private = NULL;
12520Sstevel@tonic-gate 		ksp->ks_snapshot = default_kstat_snapshot;
12530Sstevel@tonic-gate 		kstat_rele(ksp);
12540Sstevel@tonic-gate 		return;
12550Sstevel@tonic-gate 	}
12560Sstevel@tonic-gate 
12570Sstevel@tonic-gate 	/*
12580Sstevel@tonic-gate 	 * Remove the kstat from the framework's AVL trees,
12590Sstevel@tonic-gate 	 * free the allocated memory, and increment kstat_chain_id so
12600Sstevel@tonic-gate 	 * /dev/kstat clients can detect the event.
12610Sstevel@tonic-gate 	 */
12620Sstevel@tonic-gate 	mutex_enter(&kstat_chain_lock);
12630Sstevel@tonic-gate 	avl_remove(&kstat_avl_bykid, e);
12640Sstevel@tonic-gate 	avl_remove(&kstat_avl_byname, e);
12650Sstevel@tonic-gate 	kstat_chain_id++;
12660Sstevel@tonic-gate 	mutex_exit(&kstat_chain_lock);
12670Sstevel@tonic-gate 
12680Sstevel@tonic-gate 	kz = e->e_zone.next;
12690Sstevel@tonic-gate 	while (kz != NULL) {
12700Sstevel@tonic-gate 		kstat_zone_t *t = kz;
12710Sstevel@tonic-gate 
12720Sstevel@tonic-gate 		kz = kz->next;
12730Sstevel@tonic-gate 		kmem_free(t, sizeof (*t));
12740Sstevel@tonic-gate 	}
12750Sstevel@tonic-gate 	kstat_rele(ksp);
12760Sstevel@tonic-gate 	kstat_free(e);
12770Sstevel@tonic-gate }
12780Sstevel@tonic-gate 
12790Sstevel@tonic-gate void
1280*2951Selowe kstat_delete_byname_zone(const char *ks_module, int ks_instance,
1281*2951Selowe     const char *ks_name, zoneid_t ks_zoneid)
12820Sstevel@tonic-gate {
12830Sstevel@tonic-gate 	kstat_t *ksp;
12840Sstevel@tonic-gate 
12850Sstevel@tonic-gate 	ksp = kstat_hold_byname(ks_module, ks_instance, ks_name, ks_zoneid);
12860Sstevel@tonic-gate 	if (ksp != NULL) {
12870Sstevel@tonic-gate 		kstat_rele(ksp);
12880Sstevel@tonic-gate 		kstat_delete(ksp);
12890Sstevel@tonic-gate 	}
12900Sstevel@tonic-gate }
12910Sstevel@tonic-gate 
12920Sstevel@tonic-gate void
1293*2951Selowe kstat_delete_byname(const char *ks_module, int ks_instance, const char *ks_name)
12940Sstevel@tonic-gate {
12950Sstevel@tonic-gate 	kstat_delete_byname_zone(ks_module, ks_instance, ks_name, ALL_ZONES);
12960Sstevel@tonic-gate }
12970Sstevel@tonic-gate 
12980Sstevel@tonic-gate /*
12990Sstevel@tonic-gate  * The sparc V9 versions of these routines can be much cheaper than
13000Sstevel@tonic-gate  * the poor 32-bit compiler can comprehend, so they're in sparcv9_subr.s.
13010Sstevel@tonic-gate  * For simplicity, however, we always feed the C versions to lint.
13020Sstevel@tonic-gate  */
13030Sstevel@tonic-gate #if !defined(__sparc) || defined(lint) || defined(__lint)
13040Sstevel@tonic-gate 
13050Sstevel@tonic-gate void
13060Sstevel@tonic-gate kstat_waitq_enter(kstat_io_t *kiop)
13070Sstevel@tonic-gate {
13080Sstevel@tonic-gate 	hrtime_t new, delta;
13090Sstevel@tonic-gate 	ulong_t wcnt;
13100Sstevel@tonic-gate 
13110Sstevel@tonic-gate 	new = gethrtime_unscaled();
13120Sstevel@tonic-gate 	delta = new - kiop->wlastupdate;
13130Sstevel@tonic-gate 	kiop->wlastupdate = new;
13140Sstevel@tonic-gate 	wcnt = kiop->wcnt++;
13150Sstevel@tonic-gate 	if (wcnt != 0) {
13160Sstevel@tonic-gate 		kiop->wlentime += delta * wcnt;
13170Sstevel@tonic-gate 		kiop->wtime += delta;
13180Sstevel@tonic-gate 	}
13190Sstevel@tonic-gate }
13200Sstevel@tonic-gate 
13210Sstevel@tonic-gate void
13220Sstevel@tonic-gate kstat_waitq_exit(kstat_io_t *kiop)
13230Sstevel@tonic-gate {
13240Sstevel@tonic-gate 	hrtime_t new, delta;
13250Sstevel@tonic-gate 	ulong_t wcnt;
13260Sstevel@tonic-gate 
13270Sstevel@tonic-gate 	new = gethrtime_unscaled();
13280Sstevel@tonic-gate 	delta = new - kiop->wlastupdate;
13290Sstevel@tonic-gate 	kiop->wlastupdate = new;
13300Sstevel@tonic-gate 	wcnt = kiop->wcnt--;
13310Sstevel@tonic-gate 	ASSERT((int)wcnt > 0);
13320Sstevel@tonic-gate 	kiop->wlentime += delta * wcnt;
13330Sstevel@tonic-gate 	kiop->wtime += delta;
13340Sstevel@tonic-gate }
13350Sstevel@tonic-gate 
13360Sstevel@tonic-gate void
13370Sstevel@tonic-gate kstat_runq_enter(kstat_io_t *kiop)
13380Sstevel@tonic-gate {
13390Sstevel@tonic-gate 	hrtime_t new, delta;
13400Sstevel@tonic-gate 	ulong_t rcnt;
13410Sstevel@tonic-gate 
13420Sstevel@tonic-gate 	new = gethrtime_unscaled();
13430Sstevel@tonic-gate 	delta = new - kiop->rlastupdate;
13440Sstevel@tonic-gate 	kiop->rlastupdate = new;
13450Sstevel@tonic-gate 	rcnt = kiop->rcnt++;
13460Sstevel@tonic-gate 	if (rcnt != 0) {
13470Sstevel@tonic-gate 		kiop->rlentime += delta * rcnt;
13480Sstevel@tonic-gate 		kiop->rtime += delta;
13490Sstevel@tonic-gate 	}
13500Sstevel@tonic-gate }
13510Sstevel@tonic-gate 
13520Sstevel@tonic-gate void
13530Sstevel@tonic-gate kstat_runq_exit(kstat_io_t *kiop)
13540Sstevel@tonic-gate {
13550Sstevel@tonic-gate 	hrtime_t new, delta;
13560Sstevel@tonic-gate 	ulong_t rcnt;
13570Sstevel@tonic-gate 
13580Sstevel@tonic-gate 	new = gethrtime_unscaled();
13590Sstevel@tonic-gate 	delta = new - kiop->rlastupdate;
13600Sstevel@tonic-gate 	kiop->rlastupdate = new;
13610Sstevel@tonic-gate 	rcnt = kiop->rcnt--;
13620Sstevel@tonic-gate 	ASSERT((int)rcnt > 0);
13630Sstevel@tonic-gate 	kiop->rlentime += delta * rcnt;
13640Sstevel@tonic-gate 	kiop->rtime += delta;
13650Sstevel@tonic-gate }
13660Sstevel@tonic-gate 
13670Sstevel@tonic-gate void
13680Sstevel@tonic-gate kstat_waitq_to_runq(kstat_io_t *kiop)
13690Sstevel@tonic-gate {
13700Sstevel@tonic-gate 	hrtime_t new, delta;
13710Sstevel@tonic-gate 	ulong_t wcnt, rcnt;
13720Sstevel@tonic-gate 
13730Sstevel@tonic-gate 	new = gethrtime_unscaled();
13740Sstevel@tonic-gate 
13750Sstevel@tonic-gate 	delta = new - kiop->wlastupdate;
13760Sstevel@tonic-gate 	kiop->wlastupdate = new;
13770Sstevel@tonic-gate 	wcnt = kiop->wcnt--;
13780Sstevel@tonic-gate 	ASSERT((int)wcnt > 0);
13790Sstevel@tonic-gate 	kiop->wlentime += delta * wcnt;
13800Sstevel@tonic-gate 	kiop->wtime += delta;
13810Sstevel@tonic-gate 
13820Sstevel@tonic-gate 	delta = new - kiop->rlastupdate;
13830Sstevel@tonic-gate 	kiop->rlastupdate = new;
13840Sstevel@tonic-gate 	rcnt = kiop->rcnt++;
13850Sstevel@tonic-gate 	if (rcnt != 0) {
13860Sstevel@tonic-gate 		kiop->rlentime += delta * rcnt;
13870Sstevel@tonic-gate 		kiop->rtime += delta;
13880Sstevel@tonic-gate 	}
13890Sstevel@tonic-gate }
13900Sstevel@tonic-gate 
13910Sstevel@tonic-gate void
13920Sstevel@tonic-gate kstat_runq_back_to_waitq(kstat_io_t *kiop)
13930Sstevel@tonic-gate {
13940Sstevel@tonic-gate 	hrtime_t new, delta;
13950Sstevel@tonic-gate 	ulong_t wcnt, rcnt;
13960Sstevel@tonic-gate 
13970Sstevel@tonic-gate 	new = gethrtime_unscaled();
13980Sstevel@tonic-gate 
13990Sstevel@tonic-gate 	delta = new - kiop->rlastupdate;
14000Sstevel@tonic-gate 	kiop->rlastupdate = new;
14010Sstevel@tonic-gate 	rcnt = kiop->rcnt--;
14020Sstevel@tonic-gate 	ASSERT((int)rcnt > 0);
14030Sstevel@tonic-gate 	kiop->rlentime += delta * rcnt;
14040Sstevel@tonic-gate 	kiop->rtime += delta;
14050Sstevel@tonic-gate 
14060Sstevel@tonic-gate 	delta = new - kiop->wlastupdate;
14070Sstevel@tonic-gate 	kiop->wlastupdate = new;
14080Sstevel@tonic-gate 	wcnt = kiop->wcnt++;
14090Sstevel@tonic-gate 	if (wcnt != 0) {
14100Sstevel@tonic-gate 		kiop->wlentime += delta * wcnt;
14110Sstevel@tonic-gate 		kiop->wtime += delta;
14120Sstevel@tonic-gate 	}
14130Sstevel@tonic-gate }
14140Sstevel@tonic-gate 
14150Sstevel@tonic-gate #endif
14160Sstevel@tonic-gate 
14170Sstevel@tonic-gate void
14180Sstevel@tonic-gate kstat_timer_start(kstat_timer_t *ktp)
14190Sstevel@tonic-gate {
14200Sstevel@tonic-gate 	ktp->start_time = gethrtime();
14210Sstevel@tonic-gate }
14220Sstevel@tonic-gate 
14230Sstevel@tonic-gate void
14240Sstevel@tonic-gate kstat_timer_stop(kstat_timer_t *ktp)
14250Sstevel@tonic-gate {
14260Sstevel@tonic-gate 	hrtime_t	etime;
14270Sstevel@tonic-gate 	u_longlong_t	num_events;
14280Sstevel@tonic-gate 
14290Sstevel@tonic-gate 	ktp->stop_time = etime = gethrtime();
14300Sstevel@tonic-gate 	etime -= ktp->start_time;
14310Sstevel@tonic-gate 	num_events = ktp->num_events;
14320Sstevel@tonic-gate 	if (etime < ktp->min_time || num_events == 0)
14330Sstevel@tonic-gate 		ktp->min_time = etime;
14340Sstevel@tonic-gate 	if (etime > ktp->max_time)
14350Sstevel@tonic-gate 		ktp->max_time = etime;
14360Sstevel@tonic-gate 	ktp->elapsed_time += etime;
14370Sstevel@tonic-gate 	ktp->num_events = num_events + 1;
14380Sstevel@tonic-gate }
1439