xref: /illumos-gate/usr/src/uts/common/os/kstat_fr.c (revision c9bbee954bfabfc1fec46cc4c3753a98eb0f8264)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5a08731ecScth  * Common Development and Distribution License (the "License").
6a08731ecScth  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
2244c4f64bSJohn Levon  * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
23*c9bbee95SJerry Jelinek  * Copyright 2014, Joyent, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * Kernel statistics framework
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <sys/types.h>
317c478bd9Sstevel@tonic-gate #include <sys/time.h>
327c478bd9Sstevel@tonic-gate #include <sys/systm.h>
337c478bd9Sstevel@tonic-gate #include <sys/vmsystm.h>
347c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
357c478bd9Sstevel@tonic-gate #include <sys/param.h>
367c478bd9Sstevel@tonic-gate #include <sys/errno.h>
377c478bd9Sstevel@tonic-gate #include <sys/vmem.h>
387c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
397c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
407c478bd9Sstevel@tonic-gate #include <sys/kstat.h>
417c478bd9Sstevel@tonic-gate #include <sys/sysinfo.h>
427c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
437c478bd9Sstevel@tonic-gate #include <sys/fcntl.h>
447c478bd9Sstevel@tonic-gate #include <sys/flock.h>
457c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
467c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
477c478bd9Sstevel@tonic-gate #include <sys/dnlc.h>
487c478bd9Sstevel@tonic-gate #include <sys/var.h>
497c478bd9Sstevel@tonic-gate #include <sys/debug.h>
507c478bd9Sstevel@tonic-gate #include <sys/kobj.h>
517c478bd9Sstevel@tonic-gate #include <sys/avl.h>
527c478bd9Sstevel@tonic-gate #include <sys/pool_pset.h>
537c478bd9Sstevel@tonic-gate #include <sys/cpupart.h>
547c478bd9Sstevel@tonic-gate #include <sys/zone.h>
557c478bd9Sstevel@tonic-gate #include <sys/loadavg.h>
567c478bd9Sstevel@tonic-gate #include <vm/page.h>
577c478bd9Sstevel@tonic-gate #include <vm/anon.h>
587c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate /*
617c478bd9Sstevel@tonic-gate  * Global lock to protect the AVL trees and kstat_chain_id.
627c478bd9Sstevel@tonic-gate  */
637c478bd9Sstevel@tonic-gate static kmutex_t kstat_chain_lock;
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate /*
667c478bd9Sstevel@tonic-gate  * Every install/delete kstat bumps kstat_chain_id.  This is used by:
677c478bd9Sstevel@tonic-gate  *
687c478bd9Sstevel@tonic-gate  * (1)	/dev/kstat, to detect changes in the kstat chain across ioctls;
697c478bd9Sstevel@tonic-gate  *
707c478bd9Sstevel@tonic-gate  * (2)	kstat_create(), to assign a KID (kstat ID) to each new kstat.
717c478bd9Sstevel@tonic-gate  *	/dev/kstat uses the KID as a cookie for kstat lookups.
727c478bd9Sstevel@tonic-gate  *
737c478bd9Sstevel@tonic-gate  * We reserve the first two IDs because some kstats are created before
747c478bd9Sstevel@tonic-gate  * the well-known ones (kstat_headers = 0, kstat_types = 1).
757c478bd9Sstevel@tonic-gate  *
767c478bd9Sstevel@tonic-gate  * We also bump the kstat_chain_id if a zone is gaining or losing visibility
777c478bd9Sstevel@tonic-gate  * into a particular kstat, which is logically equivalent to a kstat being
787c478bd9Sstevel@tonic-gate  * installed/deleted.
797c478bd9Sstevel@tonic-gate  */
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate kid_t kstat_chain_id = 2;
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate /*
847c478bd9Sstevel@tonic-gate  * As far as zones are concerned, there are 3 types of kstat:
857c478bd9Sstevel@tonic-gate  *
867c478bd9Sstevel@tonic-gate  * 1) Those which have a well-known name, and which should return per-zone data
877c478bd9Sstevel@tonic-gate  * depending on which zone is doing the kstat_read().  sockfs:0:sock_unix_list
887c478bd9Sstevel@tonic-gate  * is an example of this type of kstat.
897c478bd9Sstevel@tonic-gate  *
907c478bd9Sstevel@tonic-gate  * 2) Those which should only be exported to a particular list of zones.
917c478bd9Sstevel@tonic-gate  * For example, in the case of nfs:*:mntinfo, we don't want zone A to be
927c478bd9Sstevel@tonic-gate  * able to see NFS mounts associated with zone B, while we want the
937c478bd9Sstevel@tonic-gate  * global zone to be able to see all mounts on the system.
947c478bd9Sstevel@tonic-gate  *
957c478bd9Sstevel@tonic-gate  * 3) Those that can be exported to all zones.  Most system-related
967c478bd9Sstevel@tonic-gate  * kstats fall within this category.
977c478bd9Sstevel@tonic-gate  *
987c478bd9Sstevel@tonic-gate  * An ekstat_t thus contains a list of kstats that the zone is to be
997c478bd9Sstevel@tonic-gate  * exported to.  The lookup of a name:instance:module thus translates to a
1007c478bd9Sstevel@tonic-gate  * lookup of name:instance:module:myzone; if the kstat is not exported
1017c478bd9Sstevel@tonic-gate  * to all zones, and does not have the caller's zoneid explicitly
1027c478bd9Sstevel@tonic-gate  * enumerated in the list of zones to be exported to, it is the same as
1037c478bd9Sstevel@tonic-gate  * if the kstat didn't exist.
1047c478bd9Sstevel@tonic-gate  *
1057c478bd9Sstevel@tonic-gate  * Writing to kstats is currently disallowed from within a non-global
1067c478bd9Sstevel@tonic-gate  * zone, although this restriction could be removed in the future.
1077c478bd9Sstevel@tonic-gate  */
1087c478bd9Sstevel@tonic-gate typedef struct kstat_zone {
1097c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
1107c478bd9Sstevel@tonic-gate 	struct kstat_zone *next;
1117c478bd9Sstevel@tonic-gate } kstat_zone_t;
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate /*
1147c478bd9Sstevel@tonic-gate  * Extended kstat structure -- for internal use only.
1157c478bd9Sstevel@tonic-gate  */
1167c478bd9Sstevel@tonic-gate typedef struct ekstat {
1177c478bd9Sstevel@tonic-gate 	kstat_t		e_ks;		/* the kstat itself */
1187c478bd9Sstevel@tonic-gate 	size_t		e_size;		/* total allocation size */
1197c478bd9Sstevel@tonic-gate 	kthread_t	*e_owner;	/* thread holding this kstat */
1207c478bd9Sstevel@tonic-gate 	kcondvar_t	e_cv;		/* wait for owner == NULL */
1217c478bd9Sstevel@tonic-gate 	avl_node_t	e_avl_bykid;	/* AVL tree to sort by KID */
1227c478bd9Sstevel@tonic-gate 	avl_node_t	e_avl_byname;	/* AVL tree to sort by name */
1237c478bd9Sstevel@tonic-gate 	kstat_zone_t	e_zone;		/* zone to export stats to */
1247c478bd9Sstevel@tonic-gate } ekstat_t;
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate static uint64_t kstat_initial[8192];
1277c478bd9Sstevel@tonic-gate static void *kstat_initial_ptr = kstat_initial;
1287c478bd9Sstevel@tonic-gate static size_t kstat_initial_avail = sizeof (kstat_initial);
1297c478bd9Sstevel@tonic-gate static vmem_t *kstat_arena;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate #define	KSTAT_ALIGN	(sizeof (uint64_t))
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate static avl_tree_t kstat_avl_bykid;
1347c478bd9Sstevel@tonic-gate static avl_tree_t kstat_avl_byname;
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate /*
1377c478bd9Sstevel@tonic-gate  * Various pointers we need to create kstats at boot time in kstat_init()
1387c478bd9Sstevel@tonic-gate  */
1397c478bd9Sstevel@tonic-gate extern	kstat_named_t	*segmapcnt_ptr;
1407c478bd9Sstevel@tonic-gate extern	uint_t		segmapcnt_ndata;
1417c478bd9Sstevel@tonic-gate extern	int		segmap_kstat_update(kstat_t *, int);
1427c478bd9Sstevel@tonic-gate extern	kstat_named_t	*biostats_ptr;
1437c478bd9Sstevel@tonic-gate extern	uint_t		biostats_ndata;
1447c478bd9Sstevel@tonic-gate extern	kstat_named_t	*pollstats_ptr;
1457c478bd9Sstevel@tonic-gate extern	uint_t		pollstats_ndata;
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate extern	int	vac;
1487c478bd9Sstevel@tonic-gate extern	uint_t	nproc;
1497c478bd9Sstevel@tonic-gate extern	time_t	boot_time;
1507c478bd9Sstevel@tonic-gate extern	sysinfo_t	sysinfo;
1517c478bd9Sstevel@tonic-gate extern	vminfo_t	vminfo;
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate struct {
1547c478bd9Sstevel@tonic-gate 	kstat_named_t ncpus;
1557c478bd9Sstevel@tonic-gate 	kstat_named_t lbolt;
1567c478bd9Sstevel@tonic-gate 	kstat_named_t deficit;
1577c478bd9Sstevel@tonic-gate 	kstat_named_t clk_intr;
1587c478bd9Sstevel@tonic-gate 	kstat_named_t vac;
1597c478bd9Sstevel@tonic-gate 	kstat_named_t nproc;
1607c478bd9Sstevel@tonic-gate 	kstat_named_t avenrun_1min;
1617c478bd9Sstevel@tonic-gate 	kstat_named_t avenrun_5min;
1627c478bd9Sstevel@tonic-gate 	kstat_named_t avenrun_15min;
1637c478bd9Sstevel@tonic-gate 	kstat_named_t boot_time;
1647c478bd9Sstevel@tonic-gate } system_misc_kstat = {
1657c478bd9Sstevel@tonic-gate 	{ "ncpus",		KSTAT_DATA_UINT32 },
1667c478bd9Sstevel@tonic-gate 	{ "lbolt",		KSTAT_DATA_UINT32 },
1677c478bd9Sstevel@tonic-gate 	{ "deficit",		KSTAT_DATA_UINT32 },
1687c478bd9Sstevel@tonic-gate 	{ "clk_intr",		KSTAT_DATA_UINT32 },
1697c478bd9Sstevel@tonic-gate 	{ "vac",		KSTAT_DATA_UINT32 },
1707c478bd9Sstevel@tonic-gate 	{ "nproc",		KSTAT_DATA_UINT32 },
1717c478bd9Sstevel@tonic-gate 	{ "avenrun_1min",	KSTAT_DATA_UINT32 },
1727c478bd9Sstevel@tonic-gate 	{ "avenrun_5min",	KSTAT_DATA_UINT32 },
1737c478bd9Sstevel@tonic-gate 	{ "avenrun_15min",	KSTAT_DATA_UINT32 },
1747c478bd9Sstevel@tonic-gate 	{ "boot_time",		KSTAT_DATA_UINT32 },
1757c478bd9Sstevel@tonic-gate };
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate struct {
1787c478bd9Sstevel@tonic-gate 	kstat_named_t physmem;
1797c478bd9Sstevel@tonic-gate 	kstat_named_t nalloc;
1807c478bd9Sstevel@tonic-gate 	kstat_named_t nfree;
1817c478bd9Sstevel@tonic-gate 	kstat_named_t nalloc_calls;
1827c478bd9Sstevel@tonic-gate 	kstat_named_t nfree_calls;
1837c478bd9Sstevel@tonic-gate 	kstat_named_t kernelbase;
1847c478bd9Sstevel@tonic-gate 	kstat_named_t econtig;
1857c478bd9Sstevel@tonic-gate 	kstat_named_t freemem;
1867c478bd9Sstevel@tonic-gate 	kstat_named_t availrmem;
1877c478bd9Sstevel@tonic-gate 	kstat_named_t lotsfree;
1887c478bd9Sstevel@tonic-gate 	kstat_named_t desfree;
1897c478bd9Sstevel@tonic-gate 	kstat_named_t minfree;
1907c478bd9Sstevel@tonic-gate 	kstat_named_t fastscan;
1917c478bd9Sstevel@tonic-gate 	kstat_named_t slowscan;
1927c478bd9Sstevel@tonic-gate 	kstat_named_t nscan;
1937c478bd9Sstevel@tonic-gate 	kstat_named_t desscan;
1947c478bd9Sstevel@tonic-gate 	kstat_named_t pp_kernel;
1957c478bd9Sstevel@tonic-gate 	kstat_named_t pagesfree;
1967c478bd9Sstevel@tonic-gate 	kstat_named_t pageslocked;
1977c478bd9Sstevel@tonic-gate 	kstat_named_t pagestotal;
1987c478bd9Sstevel@tonic-gate } system_pages_kstat = {
1997c478bd9Sstevel@tonic-gate 	{ "physmem",		KSTAT_DATA_ULONG },
2007c478bd9Sstevel@tonic-gate 	{ "nalloc",		KSTAT_DATA_ULONG },
2017c478bd9Sstevel@tonic-gate 	{ "nfree",		KSTAT_DATA_ULONG },
2027c478bd9Sstevel@tonic-gate 	{ "nalloc_calls",	KSTAT_DATA_ULONG },
2037c478bd9Sstevel@tonic-gate 	{ "nfree_calls",	KSTAT_DATA_ULONG },
2047c478bd9Sstevel@tonic-gate 	{ "kernelbase",		KSTAT_DATA_ULONG },
2057c478bd9Sstevel@tonic-gate 	{ "econtig", 		KSTAT_DATA_ULONG },
2067c478bd9Sstevel@tonic-gate 	{ "freemem", 		KSTAT_DATA_ULONG },
2077c478bd9Sstevel@tonic-gate 	{ "availrmem", 		KSTAT_DATA_ULONG },
2087c478bd9Sstevel@tonic-gate 	{ "lotsfree", 		KSTAT_DATA_ULONG },
2097c478bd9Sstevel@tonic-gate 	{ "desfree", 		KSTAT_DATA_ULONG },
2107c478bd9Sstevel@tonic-gate 	{ "minfree", 		KSTAT_DATA_ULONG },
2117c478bd9Sstevel@tonic-gate 	{ "fastscan", 		KSTAT_DATA_ULONG },
2127c478bd9Sstevel@tonic-gate 	{ "slowscan", 		KSTAT_DATA_ULONG },
2137c478bd9Sstevel@tonic-gate 	{ "nscan", 		KSTAT_DATA_ULONG },
2147c478bd9Sstevel@tonic-gate 	{ "desscan", 		KSTAT_DATA_ULONG },
2157c478bd9Sstevel@tonic-gate 	{ "pp_kernel", 		KSTAT_DATA_ULONG },
2167c478bd9Sstevel@tonic-gate 	{ "pagesfree", 		KSTAT_DATA_ULONG },
2177c478bd9Sstevel@tonic-gate 	{ "pageslocked", 	KSTAT_DATA_ULONG },
2187c478bd9Sstevel@tonic-gate 	{ "pagestotal",		KSTAT_DATA_ULONG },
2197c478bd9Sstevel@tonic-gate };
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate static int header_kstat_update(kstat_t *, int);
2227c478bd9Sstevel@tonic-gate static int header_kstat_snapshot(kstat_t *, void *, int);
2237c478bd9Sstevel@tonic-gate static int system_misc_kstat_update(kstat_t *, int);
2247c478bd9Sstevel@tonic-gate static int system_pages_kstat_update(kstat_t *, int);
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate static struct {
2277c478bd9Sstevel@tonic-gate 	char	name[KSTAT_STRLEN];
2287c478bd9Sstevel@tonic-gate 	size_t	size;
2297c478bd9Sstevel@tonic-gate 	uint_t	min_ndata;
2307c478bd9Sstevel@tonic-gate 	uint_t	max_ndata;
2317c478bd9Sstevel@tonic-gate } kstat_data_type[KSTAT_NUM_TYPES] = {
2327c478bd9Sstevel@tonic-gate 	{ "raw",		1,			0,	INT_MAX	},
2337c478bd9Sstevel@tonic-gate 	{ "name=value",		sizeof (kstat_named_t),	0,	INT_MAX	},
2347c478bd9Sstevel@tonic-gate 	{ "interrupt",		sizeof (kstat_intr_t),	1,	1	},
2357c478bd9Sstevel@tonic-gate 	{ "i/o",		sizeof (kstat_io_t),	1,	1	},
2367c478bd9Sstevel@tonic-gate 	{ "event_timer",	sizeof (kstat_timer_t),	0,	INT_MAX	},
2377c478bd9Sstevel@tonic-gate };
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate int
2407c478bd9Sstevel@tonic-gate kstat_zone_find(kstat_t *k, zoneid_t zoneid)
2417c478bd9Sstevel@tonic-gate {
2427c478bd9Sstevel@tonic-gate 	ekstat_t *e = (ekstat_t *)k;
2437c478bd9Sstevel@tonic-gate 	kstat_zone_t *kz;
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&kstat_chain_lock));
2467c478bd9Sstevel@tonic-gate 	for (kz = &e->e_zone; kz != NULL; kz = kz->next) {
2477c478bd9Sstevel@tonic-gate 		if (zoneid == ALL_ZONES || kz->zoneid == ALL_ZONES)
2487c478bd9Sstevel@tonic-gate 			return (1);
2497c478bd9Sstevel@tonic-gate 		if (zoneid == kz->zoneid)
2507c478bd9Sstevel@tonic-gate 			return (1);
2517c478bd9Sstevel@tonic-gate 	}
2527c478bd9Sstevel@tonic-gate 	return (0);
2537c478bd9Sstevel@tonic-gate }
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate void
2567c478bd9Sstevel@tonic-gate kstat_zone_remove(kstat_t *k, zoneid_t zoneid)
2577c478bd9Sstevel@tonic-gate {
2587c478bd9Sstevel@tonic-gate 	ekstat_t *e = (ekstat_t *)k;
2597c478bd9Sstevel@tonic-gate 	kstat_zone_t *kz, *t = NULL;
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	mutex_enter(&kstat_chain_lock);
2627c478bd9Sstevel@tonic-gate 	if (zoneid == e->e_zone.zoneid) {
2637c478bd9Sstevel@tonic-gate 		kz = e->e_zone.next;
2647c478bd9Sstevel@tonic-gate 		ASSERT(kz != NULL);
2657c478bd9Sstevel@tonic-gate 		e->e_zone.zoneid = kz->zoneid;
2667c478bd9Sstevel@tonic-gate 		e->e_zone.next = kz->next;
2677c478bd9Sstevel@tonic-gate 		goto out;
2687c478bd9Sstevel@tonic-gate 	}
2697c478bd9Sstevel@tonic-gate 	for (kz = &e->e_zone; kz->next != NULL; kz = kz->next) {
2707c478bd9Sstevel@tonic-gate 		if (kz->next->zoneid == zoneid) {
2717c478bd9Sstevel@tonic-gate 			t = kz->next;
2727c478bd9Sstevel@tonic-gate 			kz->next = t->next;
2737c478bd9Sstevel@tonic-gate 			break;
2747c478bd9Sstevel@tonic-gate 		}
2757c478bd9Sstevel@tonic-gate 	}
2767c478bd9Sstevel@tonic-gate 	ASSERT(t != NULL);	/* we removed something */
2777c478bd9Sstevel@tonic-gate 	kz = t;
2787c478bd9Sstevel@tonic-gate out:
2797c478bd9Sstevel@tonic-gate 	kstat_chain_id++;
2807c478bd9Sstevel@tonic-gate 	mutex_exit(&kstat_chain_lock);
2817c478bd9Sstevel@tonic-gate 	kmem_free(kz, sizeof (*kz));
2827c478bd9Sstevel@tonic-gate }
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate void
2857c478bd9Sstevel@tonic-gate kstat_zone_add(kstat_t *k, zoneid_t zoneid)
2867c478bd9Sstevel@tonic-gate {
2877c478bd9Sstevel@tonic-gate 	ekstat_t *e = (ekstat_t *)k;
2887c478bd9Sstevel@tonic-gate 	kstat_zone_t *kz;
2897c478bd9Sstevel@tonic-gate 
290c97ad5cdSakolb 	kz = kmem_alloc(sizeof (*kz), KM_NOSLEEP);
291c97ad5cdSakolb 	if (kz == NULL)
292c97ad5cdSakolb 		return;
2937c478bd9Sstevel@tonic-gate 	mutex_enter(&kstat_chain_lock);
2947c478bd9Sstevel@tonic-gate 	kz->zoneid = zoneid;
2957c478bd9Sstevel@tonic-gate 	kz->next = e->e_zone.next;
2967c478bd9Sstevel@tonic-gate 	e->e_zone.next = kz;
2977c478bd9Sstevel@tonic-gate 	kstat_chain_id++;
2987c478bd9Sstevel@tonic-gate 	mutex_exit(&kstat_chain_lock);
2997c478bd9Sstevel@tonic-gate }
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate /*
3027c478bd9Sstevel@tonic-gate  * Compare the list of zones for the given kstats, returning 0 if they match
3037c478bd9Sstevel@tonic-gate  * (ie, one list contains ALL_ZONES or both lists contain the same zoneid).
3047c478bd9Sstevel@tonic-gate  * In practice, this is called indirectly by kstat_hold_byname(), so one of the
3057c478bd9Sstevel@tonic-gate  * two lists always has one element, and this is an O(n) operation rather than
3067c478bd9Sstevel@tonic-gate  * O(n^2).
3077c478bd9Sstevel@tonic-gate  */
3087c478bd9Sstevel@tonic-gate static int
3097c478bd9Sstevel@tonic-gate kstat_zone_compare(ekstat_t *e1, ekstat_t *e2)
3107c478bd9Sstevel@tonic-gate {
3117c478bd9Sstevel@tonic-gate 	kstat_zone_t *kz1, *kz2;
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&kstat_chain_lock));
3147c478bd9Sstevel@tonic-gate 	for (kz1 = &e1->e_zone; kz1 != NULL; kz1 = kz1->next) {
3157c478bd9Sstevel@tonic-gate 		for (kz2 = &e2->e_zone; kz2 != NULL; kz2 = kz2->next) {
3167c478bd9Sstevel@tonic-gate 			if (kz1->zoneid == ALL_ZONES ||
3177c478bd9Sstevel@tonic-gate 			    kz2->zoneid == ALL_ZONES)
3187c478bd9Sstevel@tonic-gate 				return (0);
3197c478bd9Sstevel@tonic-gate 			if (kz1->zoneid == kz2->zoneid)
3207c478bd9Sstevel@tonic-gate 				return (0);
3217c478bd9Sstevel@tonic-gate 		}
3227c478bd9Sstevel@tonic-gate 	}
3237c478bd9Sstevel@tonic-gate 	return (e1->e_zone.zoneid < e2->e_zone.zoneid ? -1 : 1);
3247c478bd9Sstevel@tonic-gate }
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate /*
3277c478bd9Sstevel@tonic-gate  * Support for keeping kstats sorted in AVL trees for fast lookups.
3287c478bd9Sstevel@tonic-gate  */
3297c478bd9Sstevel@tonic-gate static int
3307c478bd9Sstevel@tonic-gate kstat_compare_bykid(const void *a1, const void *a2)
3317c478bd9Sstevel@tonic-gate {
3327c478bd9Sstevel@tonic-gate 	const kstat_t *k1 = a1;
3337c478bd9Sstevel@tonic-gate 	const kstat_t *k2 = a2;
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 	if (k1->ks_kid < k2->ks_kid)
3367c478bd9Sstevel@tonic-gate 		return (-1);
3377c478bd9Sstevel@tonic-gate 	if (k1->ks_kid > k2->ks_kid)
3387c478bd9Sstevel@tonic-gate 		return (1);
3397c478bd9Sstevel@tonic-gate 	return (kstat_zone_compare((ekstat_t *)k1, (ekstat_t *)k2));
3407c478bd9Sstevel@tonic-gate }
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate static int
3437c478bd9Sstevel@tonic-gate kstat_compare_byname(const void *a1, const void *a2)
3447c478bd9Sstevel@tonic-gate {
3457c478bd9Sstevel@tonic-gate 	const kstat_t *k1 = a1;
3467c478bd9Sstevel@tonic-gate 	const kstat_t *k2 = a2;
3477c478bd9Sstevel@tonic-gate 	int s;
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	s = strcmp(k1->ks_module, k2->ks_module);
3507c478bd9Sstevel@tonic-gate 	if (s > 0)
3517c478bd9Sstevel@tonic-gate 		return (1);
3527c478bd9Sstevel@tonic-gate 	if (s < 0)
3537c478bd9Sstevel@tonic-gate 		return (-1);
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 	if (k1->ks_instance < k2->ks_instance)
3567c478bd9Sstevel@tonic-gate 		return (-1);
3577c478bd9Sstevel@tonic-gate 	if (k1->ks_instance > k2->ks_instance)
3587c478bd9Sstevel@tonic-gate 		return (1);
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	s = strcmp(k1->ks_name, k2->ks_name);
3617c478bd9Sstevel@tonic-gate 	if (s > 0)
3627c478bd9Sstevel@tonic-gate 		return (1);
3637c478bd9Sstevel@tonic-gate 	if (s < 0)
3647c478bd9Sstevel@tonic-gate 		return (-1);
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 	return (kstat_zone_compare((ekstat_t *)k1, (ekstat_t *)k2));
3677c478bd9Sstevel@tonic-gate }
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate static kstat_t *
3707c478bd9Sstevel@tonic-gate kstat_hold(avl_tree_t *t, ekstat_t *template)
3717c478bd9Sstevel@tonic-gate {
3727c478bd9Sstevel@tonic-gate 	kstat_t *ksp;
3737c478bd9Sstevel@tonic-gate 	ekstat_t *e;
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	mutex_enter(&kstat_chain_lock);
3767c478bd9Sstevel@tonic-gate 	for (;;) {
3777c478bd9Sstevel@tonic-gate 		ksp = avl_find(t, template, NULL);
3787c478bd9Sstevel@tonic-gate 		if (ksp == NULL)
3797c478bd9Sstevel@tonic-gate 			break;
3807c478bd9Sstevel@tonic-gate 		e = (ekstat_t *)ksp;
3817c478bd9Sstevel@tonic-gate 		if (e->e_owner == NULL) {
3827c478bd9Sstevel@tonic-gate 			e->e_owner = curthread;
3837c478bd9Sstevel@tonic-gate 			break;
3847c478bd9Sstevel@tonic-gate 		}
3857c478bd9Sstevel@tonic-gate 		cv_wait(&e->e_cv, &kstat_chain_lock);
3867c478bd9Sstevel@tonic-gate 	}
3877c478bd9Sstevel@tonic-gate 	mutex_exit(&kstat_chain_lock);
3887c478bd9Sstevel@tonic-gate 	return (ksp);
3897c478bd9Sstevel@tonic-gate }
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate void
3927c478bd9Sstevel@tonic-gate kstat_rele(kstat_t *ksp)
3937c478bd9Sstevel@tonic-gate {
3947c478bd9Sstevel@tonic-gate 	ekstat_t *e = (ekstat_t *)ksp;
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 	mutex_enter(&kstat_chain_lock);
3977c478bd9Sstevel@tonic-gate 	ASSERT(e->e_owner == curthread);
3987c478bd9Sstevel@tonic-gate 	e->e_owner = NULL;
3997c478bd9Sstevel@tonic-gate 	cv_broadcast(&e->e_cv);
4007c478bd9Sstevel@tonic-gate 	mutex_exit(&kstat_chain_lock);
4017c478bd9Sstevel@tonic-gate }
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate kstat_t *
4047c478bd9Sstevel@tonic-gate kstat_hold_bykid(kid_t kid, zoneid_t zoneid)
4057c478bd9Sstevel@tonic-gate {
4067c478bd9Sstevel@tonic-gate 	ekstat_t e;
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 	e.e_ks.ks_kid = kid;
4097c478bd9Sstevel@tonic-gate 	e.e_zone.zoneid = zoneid;
4107c478bd9Sstevel@tonic-gate 	e.e_zone.next = NULL;
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 	return (kstat_hold(&kstat_avl_bykid, &e));
4137c478bd9Sstevel@tonic-gate }
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate kstat_t *
416d624471bSelowe kstat_hold_byname(const char *ks_module, int ks_instance, const char *ks_name,
4177c478bd9Sstevel@tonic-gate     zoneid_t ks_zoneid)
4187c478bd9Sstevel@tonic-gate {
4197c478bd9Sstevel@tonic-gate 	ekstat_t e;
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 	kstat_set_string(e.e_ks.ks_module, ks_module);
4227c478bd9Sstevel@tonic-gate 	e.e_ks.ks_instance = ks_instance;
4237c478bd9Sstevel@tonic-gate 	kstat_set_string(e.e_ks.ks_name, ks_name);
4247c478bd9Sstevel@tonic-gate 	e.e_zone.zoneid = ks_zoneid;
4257c478bd9Sstevel@tonic-gate 	e.e_zone.next = NULL;
4267c478bd9Sstevel@tonic-gate 	return (kstat_hold(&kstat_avl_byname, &e));
4277c478bd9Sstevel@tonic-gate }
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate static ekstat_t *
4307c478bd9Sstevel@tonic-gate kstat_alloc(size_t size)
4317c478bd9Sstevel@tonic-gate {
4327c478bd9Sstevel@tonic-gate 	ekstat_t *e = NULL;
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 	size = P2ROUNDUP(sizeof (ekstat_t) + size, KSTAT_ALIGN);
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 	if (kstat_arena == NULL) {
4377c478bd9Sstevel@tonic-gate 		if (size <= kstat_initial_avail) {
4387c478bd9Sstevel@tonic-gate 			e = kstat_initial_ptr;
4397c478bd9Sstevel@tonic-gate 			kstat_initial_ptr = (char *)kstat_initial_ptr + size;
4407c478bd9Sstevel@tonic-gate 			kstat_initial_avail -= size;
4417c478bd9Sstevel@tonic-gate 		}
4427c478bd9Sstevel@tonic-gate 	} else {
4437c478bd9Sstevel@tonic-gate 		e = vmem_alloc(kstat_arena, size, VM_NOSLEEP);
4447c478bd9Sstevel@tonic-gate 	}
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	if (e != NULL) {
4477c478bd9Sstevel@tonic-gate 		bzero(e, size);
4487c478bd9Sstevel@tonic-gate 		e->e_size = size;
4497c478bd9Sstevel@tonic-gate 		cv_init(&e->e_cv, NULL, CV_DEFAULT, NULL);
4507c478bd9Sstevel@tonic-gate 	}
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate 	return (e);
4537c478bd9Sstevel@tonic-gate }
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate static void
4567c478bd9Sstevel@tonic-gate kstat_free(ekstat_t *e)
4577c478bd9Sstevel@tonic-gate {
4587c478bd9Sstevel@tonic-gate 	cv_destroy(&e->e_cv);
4597c478bd9Sstevel@tonic-gate 	vmem_free(kstat_arena, e, e->e_size);
4607c478bd9Sstevel@tonic-gate }
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate /*
4637c478bd9Sstevel@tonic-gate  * Create various system kstats.
4647c478bd9Sstevel@tonic-gate  */
4657c478bd9Sstevel@tonic-gate void
4667c478bd9Sstevel@tonic-gate kstat_init(void)
4677c478bd9Sstevel@tonic-gate {
4687c478bd9Sstevel@tonic-gate 	kstat_t *ksp;
4697c478bd9Sstevel@tonic-gate 	ekstat_t *e;
4707c478bd9Sstevel@tonic-gate 	avl_tree_t *t = &kstat_avl_bykid;
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 	/*
4737c478bd9Sstevel@tonic-gate 	 * Set up the kstat vmem arena.
4747c478bd9Sstevel@tonic-gate 	 */
4757c478bd9Sstevel@tonic-gate 	kstat_arena = vmem_create("kstat",
4767c478bd9Sstevel@tonic-gate 	    kstat_initial, sizeof (kstat_initial), KSTAT_ALIGN,
4777c478bd9Sstevel@tonic-gate 	    segkmem_alloc, segkmem_free, heap_arena, 0, VM_SLEEP);
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 	/*
4807c478bd9Sstevel@tonic-gate 	 * Make initial kstats appear as though they were allocated.
4817c478bd9Sstevel@tonic-gate 	 */
4827c478bd9Sstevel@tonic-gate 	for (e = avl_first(t); e != NULL; e = avl_walk(t, e, AVL_AFTER))
4837c478bd9Sstevel@tonic-gate 		(void) vmem_xalloc(kstat_arena, e->e_size, KSTAT_ALIGN,
4847c478bd9Sstevel@tonic-gate 		    0, 0, e, (char *)e + e->e_size,
4857c478bd9Sstevel@tonic-gate 		    VM_NOSLEEP | VM_BESTFIT | VM_PANIC);
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 	/*
4887c478bd9Sstevel@tonic-gate 	 * The mother of all kstats.  The first kstat in the system, which
4897c478bd9Sstevel@tonic-gate 	 * always has KID 0, has the headers for all kstats (including itself)
4907c478bd9Sstevel@tonic-gate 	 * as its data.  Thus, the kstat driver does not need any special
4917c478bd9Sstevel@tonic-gate 	 * interface to extract the kstat chain.
4927c478bd9Sstevel@tonic-gate 	 */
4937c478bd9Sstevel@tonic-gate 	kstat_chain_id = 0;
4947c478bd9Sstevel@tonic-gate 	ksp = kstat_create("unix", 0, "kstat_headers", "kstat", KSTAT_TYPE_RAW,
4957c478bd9Sstevel@tonic-gate 	    0, KSTAT_FLAG_VIRTUAL | KSTAT_FLAG_VAR_SIZE);
4967c478bd9Sstevel@tonic-gate 	if (ksp) {
4977c478bd9Sstevel@tonic-gate 		ksp->ks_lock = &kstat_chain_lock;
4987c478bd9Sstevel@tonic-gate 		ksp->ks_update = header_kstat_update;
4997c478bd9Sstevel@tonic-gate 		ksp->ks_snapshot = header_kstat_snapshot;
5007c478bd9Sstevel@tonic-gate 		kstat_install(ksp);
5017c478bd9Sstevel@tonic-gate 	} else {
5027c478bd9Sstevel@tonic-gate 		panic("cannot create kstat 'kstat_headers'");
5037c478bd9Sstevel@tonic-gate 	}
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	ksp = kstat_create("unix", 0, "kstat_types", "kstat",
5067c478bd9Sstevel@tonic-gate 	    KSTAT_TYPE_NAMED, KSTAT_NUM_TYPES, 0);
5077c478bd9Sstevel@tonic-gate 	if (ksp) {
5087c478bd9Sstevel@tonic-gate 		int i;
5097c478bd9Sstevel@tonic-gate 		kstat_named_t *kn = KSTAT_NAMED_PTR(ksp);
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 		for (i = 0; i < KSTAT_NUM_TYPES; i++) {
5127c478bd9Sstevel@tonic-gate 			kstat_named_init(&kn[i], kstat_data_type[i].name,
5137c478bd9Sstevel@tonic-gate 			    KSTAT_DATA_ULONG);
5147c478bd9Sstevel@tonic-gate 			kn[i].value.ul = i;
5157c478bd9Sstevel@tonic-gate 		}
5167c478bd9Sstevel@tonic-gate 		kstat_install(ksp);
5177c478bd9Sstevel@tonic-gate 	}
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 	ksp = kstat_create("unix", 0, "sysinfo", "misc", KSTAT_TYPE_RAW,
5207c478bd9Sstevel@tonic-gate 	    sizeof (sysinfo_t), KSTAT_FLAG_VIRTUAL);
5217c478bd9Sstevel@tonic-gate 	if (ksp) {
5227c478bd9Sstevel@tonic-gate 		ksp->ks_data = (void *) &sysinfo;
5237c478bd9Sstevel@tonic-gate 		kstat_install(ksp);
5247c478bd9Sstevel@tonic-gate 	}
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 	ksp = kstat_create("unix", 0, "vminfo", "vm", KSTAT_TYPE_RAW,
5277c478bd9Sstevel@tonic-gate 	    sizeof (vminfo_t), KSTAT_FLAG_VIRTUAL);
5287c478bd9Sstevel@tonic-gate 	if (ksp) {
5297c478bd9Sstevel@tonic-gate 		ksp->ks_data = (void *) &vminfo;
5307c478bd9Sstevel@tonic-gate 		kstat_install(ksp);
5317c478bd9Sstevel@tonic-gate 	}
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 	ksp = kstat_create("unix", 0, "segmap", "vm", KSTAT_TYPE_NAMED,
5347c478bd9Sstevel@tonic-gate 	    segmapcnt_ndata, KSTAT_FLAG_VIRTUAL);
5357c478bd9Sstevel@tonic-gate 	if (ksp) {
5367c478bd9Sstevel@tonic-gate 		ksp->ks_data = (void *) segmapcnt_ptr;
5377c478bd9Sstevel@tonic-gate 		ksp->ks_update = segmap_kstat_update;
5387c478bd9Sstevel@tonic-gate 		kstat_install(ksp);
5397c478bd9Sstevel@tonic-gate 	}
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 	ksp = kstat_create("unix", 0, "biostats", "misc", KSTAT_TYPE_NAMED,
5427c478bd9Sstevel@tonic-gate 	    biostats_ndata, KSTAT_FLAG_VIRTUAL);
5437c478bd9Sstevel@tonic-gate 	if (ksp) {
5447c478bd9Sstevel@tonic-gate 		ksp->ks_data = (void *) biostats_ptr;
5457c478bd9Sstevel@tonic-gate 		kstat_install(ksp);
5467c478bd9Sstevel@tonic-gate 	}
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 	ksp = kstat_create("unix", 0, "var", "misc", KSTAT_TYPE_RAW,
5497c478bd9Sstevel@tonic-gate 	    sizeof (struct var), KSTAT_FLAG_VIRTUAL);
5507c478bd9Sstevel@tonic-gate 	if (ksp) {
5517c478bd9Sstevel@tonic-gate 		ksp->ks_data = (void *) &v;
5527c478bd9Sstevel@tonic-gate 		kstat_install(ksp);
5537c478bd9Sstevel@tonic-gate 	}
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 	ksp = kstat_create("unix", 0, "system_misc", "misc", KSTAT_TYPE_NAMED,
5567c478bd9Sstevel@tonic-gate 	    sizeof (system_misc_kstat) / sizeof (kstat_named_t),
5577c478bd9Sstevel@tonic-gate 	    KSTAT_FLAG_VIRTUAL);
5587c478bd9Sstevel@tonic-gate 	if (ksp) {
5597c478bd9Sstevel@tonic-gate 		ksp->ks_data = (void *) &system_misc_kstat;
5607c478bd9Sstevel@tonic-gate 		ksp->ks_update = system_misc_kstat_update;
5617c478bd9Sstevel@tonic-gate 		kstat_install(ksp);
5627c478bd9Sstevel@tonic-gate 	}
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 	ksp = kstat_create("unix", 0, "system_pages", "pages", KSTAT_TYPE_NAMED,
5657c478bd9Sstevel@tonic-gate 	    sizeof (system_pages_kstat) / sizeof (kstat_named_t),
5667c478bd9Sstevel@tonic-gate 	    KSTAT_FLAG_VIRTUAL);
5677c478bd9Sstevel@tonic-gate 	if (ksp) {
5687c478bd9Sstevel@tonic-gate 		ksp->ks_data = (void *) &system_pages_kstat;
5697c478bd9Sstevel@tonic-gate 		ksp->ks_update = system_pages_kstat_update;
5707c478bd9Sstevel@tonic-gate 		kstat_install(ksp);
5717c478bd9Sstevel@tonic-gate 	}
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 	ksp = kstat_create("poll", 0, "pollstats", "misc", KSTAT_TYPE_NAMED,
5747c478bd9Sstevel@tonic-gate 	    pollstats_ndata, KSTAT_FLAG_VIRTUAL | KSTAT_FLAG_WRITABLE);
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 	if (ksp) {
5777c478bd9Sstevel@tonic-gate 		ksp->ks_data = pollstats_ptr;
5787c478bd9Sstevel@tonic-gate 		kstat_install(ksp);
5797c478bd9Sstevel@tonic-gate 	}
5807c478bd9Sstevel@tonic-gate }
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate /*
5837c478bd9Sstevel@tonic-gate  * Caller of this should ensure that the string pointed by src
5847c478bd9Sstevel@tonic-gate  * doesn't change while kstat's lock is held. Not doing so defeats
5857c478bd9Sstevel@tonic-gate  * kstat's snapshot strategy as explained in <sys/kstat.h>
5867c478bd9Sstevel@tonic-gate  */
5877c478bd9Sstevel@tonic-gate void
5887c478bd9Sstevel@tonic-gate kstat_named_setstr(kstat_named_t *knp, const char *src)
5897c478bd9Sstevel@tonic-gate {
5907c478bd9Sstevel@tonic-gate 	if (knp->data_type != KSTAT_DATA_STRING)
5917c478bd9Sstevel@tonic-gate 		panic("kstat_named_setstr('%p', '%p'): "
592903a11ebSrh87107 		    "named kstat is not of type KSTAT_DATA_STRING",
593903a11ebSrh87107 		    (void *)knp, (void *)src);
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 	KSTAT_NAMED_STR_PTR(knp) = (char *)src;
5967c478bd9Sstevel@tonic-gate 	if (src != NULL)
5977c478bd9Sstevel@tonic-gate 		KSTAT_NAMED_STR_BUFLEN(knp) = strlen(src) + 1;
5987c478bd9Sstevel@tonic-gate 	else
5997c478bd9Sstevel@tonic-gate 		KSTAT_NAMED_STR_BUFLEN(knp) = 0;
6007c478bd9Sstevel@tonic-gate }
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate void
603d624471bSelowe kstat_set_string(char *dst, const char *src)
6047c478bd9Sstevel@tonic-gate {
6057c478bd9Sstevel@tonic-gate 	bzero(dst, KSTAT_STRLEN);
6067c478bd9Sstevel@tonic-gate 	(void) strncpy(dst, src, KSTAT_STRLEN - 1);
6077c478bd9Sstevel@tonic-gate }
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate void
610d624471bSelowe kstat_named_init(kstat_named_t *knp, const char *name, uchar_t data_type)
6117c478bd9Sstevel@tonic-gate {
6127c478bd9Sstevel@tonic-gate 	kstat_set_string(knp->name, name);
6137c478bd9Sstevel@tonic-gate 	knp->data_type = data_type;
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 	if (data_type == KSTAT_DATA_STRING)
6167c478bd9Sstevel@tonic-gate 		kstat_named_setstr(knp, NULL);
6177c478bd9Sstevel@tonic-gate }
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate void
620d624471bSelowe kstat_timer_init(kstat_timer_t *ktp, const char *name)
6217c478bd9Sstevel@tonic-gate {
6227c478bd9Sstevel@tonic-gate 	kstat_set_string(ktp->name, name);
6237c478bd9Sstevel@tonic-gate }
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate /* ARGSUSED */
6267c478bd9Sstevel@tonic-gate static int
6277c478bd9Sstevel@tonic-gate default_kstat_update(kstat_t *ksp, int rw)
6287c478bd9Sstevel@tonic-gate {
6297c478bd9Sstevel@tonic-gate 	uint_t i;
6307c478bd9Sstevel@tonic-gate 	size_t len = 0;
6317c478bd9Sstevel@tonic-gate 	kstat_named_t *knp;
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 	/*
6347c478bd9Sstevel@tonic-gate 	 * Named kstats with variable-length long strings have a standard
6357c478bd9Sstevel@tonic-gate 	 * way of determining how much space is needed to hold the snapshot:
6367c478bd9Sstevel@tonic-gate 	 */
6377c478bd9Sstevel@tonic-gate 	if (ksp->ks_data != NULL && ksp->ks_type == KSTAT_TYPE_NAMED &&
6387c478bd9Sstevel@tonic-gate 	    (ksp->ks_flags & KSTAT_FLAG_VAR_SIZE)) {
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 		/*
6417c478bd9Sstevel@tonic-gate 		 * Add in the space required for the strings
6427c478bd9Sstevel@tonic-gate 		 */
6437c478bd9Sstevel@tonic-gate 		knp = KSTAT_NAMED_PTR(ksp);
6447c478bd9Sstevel@tonic-gate 		for (i = 0; i < ksp->ks_ndata; i++, knp++) {
6457c478bd9Sstevel@tonic-gate 			if (knp->data_type == KSTAT_DATA_STRING)
6467c478bd9Sstevel@tonic-gate 				len += KSTAT_NAMED_STR_BUFLEN(knp);
6477c478bd9Sstevel@tonic-gate 		}
6487c478bd9Sstevel@tonic-gate 		ksp->ks_data_size =
6497c478bd9Sstevel@tonic-gate 		    ksp->ks_ndata * sizeof (kstat_named_t) + len;
6507c478bd9Sstevel@tonic-gate 	}
6517c478bd9Sstevel@tonic-gate 	return (0);
6527c478bd9Sstevel@tonic-gate }
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate static int
6557c478bd9Sstevel@tonic-gate default_kstat_snapshot(kstat_t *ksp, void *buf, int rw)
6567c478bd9Sstevel@tonic-gate {
6577c478bd9Sstevel@tonic-gate 	kstat_io_t *kiop;
6587c478bd9Sstevel@tonic-gate 	hrtime_t cur_time;
6597c478bd9Sstevel@tonic-gate 	size_t	namedsz;
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate 	ksp->ks_snaptime = cur_time = gethrtime();
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 	if (rw == KSTAT_WRITE) {
6647c478bd9Sstevel@tonic-gate 		if (!(ksp->ks_flags & KSTAT_FLAG_WRITABLE))
6657c478bd9Sstevel@tonic-gate 			return (EACCES);
6667c478bd9Sstevel@tonic-gate 		bcopy(buf, ksp->ks_data, ksp->ks_data_size);
6677c478bd9Sstevel@tonic-gate 		return (0);
6687c478bd9Sstevel@tonic-gate 	}
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 	/*
6717c478bd9Sstevel@tonic-gate 	 * KSTAT_TYPE_NAMED kstats are defined to have ks_ndata
6727c478bd9Sstevel@tonic-gate 	 * number of kstat_named_t structures, followed by an optional
6737c478bd9Sstevel@tonic-gate 	 * string segment. The ks_data generally holds only the
6747c478bd9Sstevel@tonic-gate 	 * kstat_named_t structures. So we copy it first. The strings,
6757c478bd9Sstevel@tonic-gate 	 * if any, are copied below. For other kstat types, ks_data holds the
6767c478bd9Sstevel@tonic-gate 	 * entire buffer.
6777c478bd9Sstevel@tonic-gate 	 */
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 	namedsz = sizeof (kstat_named_t) * ksp->ks_ndata;
6807c478bd9Sstevel@tonic-gate 	if (ksp->ks_type == KSTAT_TYPE_NAMED && ksp->ks_data_size > namedsz)
6817c478bd9Sstevel@tonic-gate 		bcopy(ksp->ks_data, buf, namedsz);
6827c478bd9Sstevel@tonic-gate 	else
6837c478bd9Sstevel@tonic-gate 		bcopy(ksp->ks_data, buf, ksp->ks_data_size);
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 	/*
6867c478bd9Sstevel@tonic-gate 	 * Apply kstat type-specific data massaging
6877c478bd9Sstevel@tonic-gate 	 */
6887c478bd9Sstevel@tonic-gate 	switch (ksp->ks_type) {
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 	case KSTAT_TYPE_IO:
6917c478bd9Sstevel@tonic-gate 		/*
6927c478bd9Sstevel@tonic-gate 		 * Normalize time units and deal with incomplete transactions
6937c478bd9Sstevel@tonic-gate 		 */
6947c478bd9Sstevel@tonic-gate 		kiop = (kstat_io_t *)buf;
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate 		scalehrtime(&kiop->wtime);
6977c478bd9Sstevel@tonic-gate 		scalehrtime(&kiop->wlentime);
6987c478bd9Sstevel@tonic-gate 		scalehrtime(&kiop->wlastupdate);
6997c478bd9Sstevel@tonic-gate 		scalehrtime(&kiop->rtime);
7007c478bd9Sstevel@tonic-gate 		scalehrtime(&kiop->rlentime);
7017c478bd9Sstevel@tonic-gate 		scalehrtime(&kiop->rlastupdate);
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate 		if (kiop->wcnt != 0) {
704a08731ecScth 			/* like kstat_waitq_exit */
7057c478bd9Sstevel@tonic-gate 			hrtime_t wfix = cur_time - kiop->wlastupdate;
7067c478bd9Sstevel@tonic-gate 			kiop->wlastupdate = cur_time;
707a08731ecScth 			kiop->wlentime += kiop->wcnt * wfix;
708a08731ecScth 			kiop->wtime += wfix;
7097c478bd9Sstevel@tonic-gate 		}
710a08731ecScth 
711a08731ecScth 		if (kiop->rcnt != 0) {
712a08731ecScth 			/* like kstat_runq_exit */
713a08731ecScth 			hrtime_t rfix = cur_time - kiop->rlastupdate;
7147c478bd9Sstevel@tonic-gate 			kiop->rlastupdate = cur_time;
715a08731ecScth 			kiop->rlentime += kiop->rcnt * rfix;
716a08731ecScth 			kiop->rtime += rfix;
717a08731ecScth 		}
7187c478bd9Sstevel@tonic-gate 		break;
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate 	case KSTAT_TYPE_NAMED:
7217c478bd9Sstevel@tonic-gate 		/*
7227c478bd9Sstevel@tonic-gate 		 * Massage any long strings in at the end of the buffer
7237c478bd9Sstevel@tonic-gate 		 */
7247c478bd9Sstevel@tonic-gate 		if (ksp->ks_data_size > namedsz) {
7257c478bd9Sstevel@tonic-gate 			uint_t i;
7267c478bd9Sstevel@tonic-gate 			kstat_named_t *knp = buf;
7277c478bd9Sstevel@tonic-gate 			char *dst = (char *)(knp + ksp->ks_ndata);
7287c478bd9Sstevel@tonic-gate 			/*
7297c478bd9Sstevel@tonic-gate 			 * Copy strings and update pointers
7307c478bd9Sstevel@tonic-gate 			 */
7317c478bd9Sstevel@tonic-gate 			for (i = 0; i < ksp->ks_ndata; i++, knp++) {
7327c478bd9Sstevel@tonic-gate 				if (knp->data_type == KSTAT_DATA_STRING &&
7337c478bd9Sstevel@tonic-gate 				    KSTAT_NAMED_STR_PTR(knp) != NULL) {
7347c478bd9Sstevel@tonic-gate 					bcopy(KSTAT_NAMED_STR_PTR(knp), dst,
7357c478bd9Sstevel@tonic-gate 					    KSTAT_NAMED_STR_BUFLEN(knp));
7367c478bd9Sstevel@tonic-gate 					KSTAT_NAMED_STR_PTR(knp) = dst;
7377c478bd9Sstevel@tonic-gate 					dst += KSTAT_NAMED_STR_BUFLEN(knp);
7387c478bd9Sstevel@tonic-gate 				}
7397c478bd9Sstevel@tonic-gate 			}
7407c478bd9Sstevel@tonic-gate 			ASSERT(dst <= ((char *)buf + ksp->ks_data_size));
7417c478bd9Sstevel@tonic-gate 		}
7427c478bd9Sstevel@tonic-gate 		break;
7437c478bd9Sstevel@tonic-gate 	}
7447c478bd9Sstevel@tonic-gate 	return (0);
7457c478bd9Sstevel@tonic-gate }
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate static int
7487c478bd9Sstevel@tonic-gate header_kstat_update(kstat_t *header_ksp, int rw)
7497c478bd9Sstevel@tonic-gate {
7507c478bd9Sstevel@tonic-gate 	int nkstats = 0;
7517c478bd9Sstevel@tonic-gate 	ekstat_t *e;
7527c478bd9Sstevel@tonic-gate 	avl_tree_t *t = &kstat_avl_bykid;
7537c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
7567c478bd9Sstevel@tonic-gate 		return (EACCES);
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&kstat_chain_lock));
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate 	zoneid = getzoneid();
7617c478bd9Sstevel@tonic-gate 	for (e = avl_first(t); e != NULL; e = avl_walk(t, e, AVL_AFTER)) {
762dc9df478SJerry Jelinek 		if (kstat_zone_find((kstat_t *)e, zoneid) &&
763dc9df478SJerry Jelinek 		    (e->e_ks.ks_flags & KSTAT_FLAG_INVALID) == 0) {
7647c478bd9Sstevel@tonic-gate 			nkstats++;
7657c478bd9Sstevel@tonic-gate 		}
7667c478bd9Sstevel@tonic-gate 	}
7677c478bd9Sstevel@tonic-gate 	header_ksp->ks_ndata = nkstats;
7687c478bd9Sstevel@tonic-gate 	header_ksp->ks_data_size = nkstats * sizeof (kstat_t);
7697c478bd9Sstevel@tonic-gate 	return (0);
7707c478bd9Sstevel@tonic-gate }
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate /*
7737c478bd9Sstevel@tonic-gate  * Copy out the data section of kstat 0, which consists of the list
7747c478bd9Sstevel@tonic-gate  * of all kstat headers.  By specification, these headers must be
7757c478bd9Sstevel@tonic-gate  * copied out in order of increasing KID.
7767c478bd9Sstevel@tonic-gate  */
7777c478bd9Sstevel@tonic-gate static int
7787c478bd9Sstevel@tonic-gate header_kstat_snapshot(kstat_t *header_ksp, void *buf, int rw)
7797c478bd9Sstevel@tonic-gate {
7807c478bd9Sstevel@tonic-gate 	ekstat_t *e;
7817c478bd9Sstevel@tonic-gate 	avl_tree_t *t = &kstat_avl_bykid;
7827c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 	header_ksp->ks_snaptime = gethrtime();
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
7877c478bd9Sstevel@tonic-gate 		return (EACCES);
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&kstat_chain_lock));
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 	zoneid = getzoneid();
7927c478bd9Sstevel@tonic-gate 	for (e = avl_first(t); e != NULL; e = avl_walk(t, e, AVL_AFTER)) {
793dc9df478SJerry Jelinek 		if (kstat_zone_find((kstat_t *)e, zoneid) &&
794dc9df478SJerry Jelinek 		    (e->e_ks.ks_flags & KSTAT_FLAG_INVALID) == 0) {
7957c478bd9Sstevel@tonic-gate 			bcopy(&e->e_ks, buf, sizeof (kstat_t));
7967c478bd9Sstevel@tonic-gate 			buf = (char *)buf + sizeof (kstat_t);
7977c478bd9Sstevel@tonic-gate 		}
7987c478bd9Sstevel@tonic-gate 	}
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate 	return (0);
8017c478bd9Sstevel@tonic-gate }
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate /* ARGSUSED */
8047c478bd9Sstevel@tonic-gate static int
8057c478bd9Sstevel@tonic-gate system_misc_kstat_update(kstat_t *ksp, int rw)
8067c478bd9Sstevel@tonic-gate {
8077c478bd9Sstevel@tonic-gate 	int myncpus = ncpus;
8087c478bd9Sstevel@tonic-gate 	int *loadavgp = &avenrun[0];
8097c478bd9Sstevel@tonic-gate 	int loadavg[LOADAVG_NSTATS];
810a34e7eeaSDhanaraj M 	time_t zone_boot_time;
811a34e7eeaSDhanaraj M 	clock_t zone_lbolt;
812a34e7eeaSDhanaraj M 	hrtime_t zone_hrtime;
8138cb09440SVamsi Nagineni 	size_t zone_nproc;
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
8167c478bd9Sstevel@tonic-gate 		return (EACCES);
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate 	if (!INGLOBALZONE(curproc)) {
8197c478bd9Sstevel@tonic-gate 		/*
8207c478bd9Sstevel@tonic-gate 		 * Here we grab cpu_lock which is OK as long as no-one in the
8217c478bd9Sstevel@tonic-gate 		 * future attempts to lookup this particular kstat
8227c478bd9Sstevel@tonic-gate 		 * (unix:0:system_misc) while holding cpu_lock.
8237c478bd9Sstevel@tonic-gate 		 */
8247c478bd9Sstevel@tonic-gate 		mutex_enter(&cpu_lock);
8257c478bd9Sstevel@tonic-gate 		if (pool_pset_enabled()) {
8267c478bd9Sstevel@tonic-gate 			psetid_t mypsid = zone_pset_get(curproc->p_zone);
8277c478bd9Sstevel@tonic-gate 			int error;
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate 			myncpus = zone_ncpus_get(curproc->p_zone);
8307c478bd9Sstevel@tonic-gate 			ASSERT(myncpus > 0);
8317c478bd9Sstevel@tonic-gate 			error = cpupart_get_loadavg(mypsid, &loadavg[0],
8327c478bd9Sstevel@tonic-gate 			    LOADAVG_NSTATS);
8337c478bd9Sstevel@tonic-gate 			ASSERT(error == 0);
8347c478bd9Sstevel@tonic-gate 			loadavgp = &loadavg[0];
8357c478bd9Sstevel@tonic-gate 		}
8367c478bd9Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
8377c478bd9Sstevel@tonic-gate 	}
8387c478bd9Sstevel@tonic-gate 
8398cb09440SVamsi Nagineni 	if (INGLOBALZONE(curproc)) {
840a34e7eeaSDhanaraj M 		zone_boot_time = boot_time;
841d3d50737SRafael Vanoni 		zone_lbolt = ddi_get_lbolt();
8428cb09440SVamsi Nagineni 		zone_nproc = nproc;
843a34e7eeaSDhanaraj M 	} else {
84486ad481cSJerry Jelinek 		zone_boot_time = curproc->p_zone->zone_boot_time;
845a34e7eeaSDhanaraj M 
846a34e7eeaSDhanaraj M 		zone_hrtime = gethrtime();
847a34e7eeaSDhanaraj M 		zone_lbolt = (clock_t)(NSEC_TO_TICK(zone_hrtime) -
848a34e7eeaSDhanaraj M 		    NSEC_TO_TICK(curproc->p_zone->zone_zsched->p_mstart));
8498cb09440SVamsi Nagineni 		mutex_enter(&curproc->p_zone->zone_nlwps_lock);
8508cb09440SVamsi Nagineni 		zone_nproc = curproc->p_zone->zone_nprocs;
8518cb09440SVamsi Nagineni 		mutex_exit(&curproc->p_zone->zone_nlwps_lock);
852a34e7eeaSDhanaraj M 	}
853a34e7eeaSDhanaraj M 
8547c478bd9Sstevel@tonic-gate 	system_misc_kstat.ncpus.value.ui32		= (uint32_t)myncpus;
855a34e7eeaSDhanaraj M 	system_misc_kstat.lbolt.value.ui32		= (uint32_t)zone_lbolt;
8567c478bd9Sstevel@tonic-gate 	system_misc_kstat.deficit.value.ui32		= (uint32_t)deficit;
857a34e7eeaSDhanaraj M 	system_misc_kstat.clk_intr.value.ui32		= (uint32_t)zone_lbolt;
8587c478bd9Sstevel@tonic-gate 	system_misc_kstat.vac.value.ui32		= (uint32_t)vac;
8598cb09440SVamsi Nagineni 	system_misc_kstat.nproc.value.ui32		= (uint32_t)zone_nproc;
8607c478bd9Sstevel@tonic-gate 	system_misc_kstat.avenrun_1min.value.ui32	= (uint32_t)loadavgp[0];
8617c478bd9Sstevel@tonic-gate 	system_misc_kstat.avenrun_5min.value.ui32	= (uint32_t)loadavgp[1];
8627c478bd9Sstevel@tonic-gate 	system_misc_kstat.avenrun_15min.value.ui32	= (uint32_t)loadavgp[2];
863a34e7eeaSDhanaraj M 	system_misc_kstat.boot_time.value.ui32		= (uint32_t)
864a34e7eeaSDhanaraj M 	    zone_boot_time;
8657c478bd9Sstevel@tonic-gate 	return (0);
8667c478bd9Sstevel@tonic-gate }
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate #ifdef	__sparc
8697c478bd9Sstevel@tonic-gate extern caddr_t	econtig32;
8707c478bd9Sstevel@tonic-gate #else	/* !__sparc */
8717c478bd9Sstevel@tonic-gate extern caddr_t	econtig;
8727c478bd9Sstevel@tonic-gate #endif	/* __sparc */
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate /* ARGSUSED */
8757c478bd9Sstevel@tonic-gate static int
8767c478bd9Sstevel@tonic-gate system_pages_kstat_update(kstat_t *ksp, int rw)
8777c478bd9Sstevel@tonic-gate {
8787c478bd9Sstevel@tonic-gate 	kobj_stat_t kobj_stat;
8797c478bd9Sstevel@tonic-gate 
8807c478bd9Sstevel@tonic-gate 	if (rw == KSTAT_WRITE) {
8817c478bd9Sstevel@tonic-gate 		return (EACCES);
8827c478bd9Sstevel@tonic-gate 	}
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate 	kobj_stat_get(&kobj_stat);
8857c478bd9Sstevel@tonic-gate 	system_pages_kstat.physmem.value.ul	= (ulong_t)physmem;
8867c478bd9Sstevel@tonic-gate 	system_pages_kstat.nalloc.value.ul	= kobj_stat.nalloc;
8877c478bd9Sstevel@tonic-gate 	system_pages_kstat.nfree.value.ul	= kobj_stat.nfree;
8887c478bd9Sstevel@tonic-gate 	system_pages_kstat.nalloc_calls.value.ul = kobj_stat.nalloc_calls;
8897c478bd9Sstevel@tonic-gate 	system_pages_kstat.nfree_calls.value.ul	= kobj_stat.nfree_calls;
8907c478bd9Sstevel@tonic-gate 	system_pages_kstat.kernelbase.value.ul	= (ulong_t)KERNELBASE;
8917c478bd9Sstevel@tonic-gate 
8927c478bd9Sstevel@tonic-gate #ifdef	__sparc
8937c478bd9Sstevel@tonic-gate 	/*
8947c478bd9Sstevel@tonic-gate 	 * kstat should REALLY be modified to also report kmem64_base and
8957c478bd9Sstevel@tonic-gate 	 * kmem64_end (see sun4u/os/startup.c), as the virtual address range
8967c478bd9Sstevel@tonic-gate 	 * [ kernelbase .. econtig ] no longer is truly reflective of the
8977c478bd9Sstevel@tonic-gate 	 * kernel's vallocs...
8987c478bd9Sstevel@tonic-gate 	 */
8997c478bd9Sstevel@tonic-gate 	system_pages_kstat.econtig.value.ul	= (ulong_t)econtig32;
9007c478bd9Sstevel@tonic-gate #else	/* !__sparc */
9017c478bd9Sstevel@tonic-gate 	system_pages_kstat.econtig.value.ul	= (ulong_t)econtig;
9027c478bd9Sstevel@tonic-gate #endif	/* __sparc */
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 	system_pages_kstat.freemem.value.ul	= (ulong_t)freemem;
9057c478bd9Sstevel@tonic-gate 	system_pages_kstat.availrmem.value.ul	= (ulong_t)availrmem;
9067c478bd9Sstevel@tonic-gate 	system_pages_kstat.lotsfree.value.ul	= (ulong_t)lotsfree;
9077c478bd9Sstevel@tonic-gate 	system_pages_kstat.desfree.value.ul	= (ulong_t)desfree;
9087c478bd9Sstevel@tonic-gate 	system_pages_kstat.minfree.value.ul	= (ulong_t)minfree;
9097c478bd9Sstevel@tonic-gate 	system_pages_kstat.fastscan.value.ul	= (ulong_t)fastscan;
9107c478bd9Sstevel@tonic-gate 	system_pages_kstat.slowscan.value.ul	= (ulong_t)slowscan;
9117c478bd9Sstevel@tonic-gate 	system_pages_kstat.nscan.value.ul	= (ulong_t)nscan;
9127c478bd9Sstevel@tonic-gate 	system_pages_kstat.desscan.value.ul	= (ulong_t)desscan;
9137c478bd9Sstevel@tonic-gate 	system_pages_kstat.pagesfree.value.ul	= (ulong_t)freemem;
9147c478bd9Sstevel@tonic-gate 	system_pages_kstat.pageslocked.value.ul	= (ulong_t)(availrmem_initial -
9157c478bd9Sstevel@tonic-gate 	    availrmem);
9167c478bd9Sstevel@tonic-gate 	system_pages_kstat.pagestotal.value.ul	= (ulong_t)total_pages;
9177c478bd9Sstevel@tonic-gate 	/*
9187c478bd9Sstevel@tonic-gate 	 * pp_kernel represents total pages used by the kernel since the
9197c478bd9Sstevel@tonic-gate 	 * startup. This formula takes into account the boottime kernel
9207c478bd9Sstevel@tonic-gate 	 * footprint and also considers the availrmem changes because of
9217c478bd9Sstevel@tonic-gate 	 * user explicit page locking.
9227c478bd9Sstevel@tonic-gate 	 */
9237c478bd9Sstevel@tonic-gate 	system_pages_kstat.pp_kernel.value.ul   = (ulong_t)(physinstalled -
9247c478bd9Sstevel@tonic-gate 	    obp_pages - availrmem - k_anoninfo.ani_mem_resv -
925a98e9dbfSaguzovsk 	    anon_segkp_pages_locked - pages_locked -
926a98e9dbfSaguzovsk 	    pages_claimed - pages_useclaim);
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate 	return (0);
9297c478bd9Sstevel@tonic-gate }
9307c478bd9Sstevel@tonic-gate 
9317c478bd9Sstevel@tonic-gate kstat_t *
932d624471bSelowe kstat_create(const char *ks_module, int ks_instance, const char *ks_name,
933d624471bSelowe     const char *ks_class, uchar_t ks_type, uint_t ks_ndata, uchar_t ks_flags)
9347c478bd9Sstevel@tonic-gate {
9357c478bd9Sstevel@tonic-gate 	return (kstat_create_zone(ks_module, ks_instance, ks_name, ks_class,
9367c478bd9Sstevel@tonic-gate 	    ks_type, ks_ndata, ks_flags, ALL_ZONES));
9377c478bd9Sstevel@tonic-gate }
9387c478bd9Sstevel@tonic-gate 
9397c478bd9Sstevel@tonic-gate /*
9407c478bd9Sstevel@tonic-gate  * Allocate and initialize a kstat structure.  Or, if a dormant kstat with
9417c478bd9Sstevel@tonic-gate  * the specified name exists, reactivate it.  Returns a pointer to the kstat
9427c478bd9Sstevel@tonic-gate  * on success, NULL on failure.  The kstat will not be visible to the
9437c478bd9Sstevel@tonic-gate  * kstat driver until kstat_install().
9447c478bd9Sstevel@tonic-gate  */
9457c478bd9Sstevel@tonic-gate kstat_t *
946d624471bSelowe kstat_create_zone(const char *ks_module, int ks_instance, const char *ks_name,
947d624471bSelowe     const char *ks_class, uchar_t ks_type, uint_t ks_ndata, uchar_t ks_flags,
9487c478bd9Sstevel@tonic-gate     zoneid_t ks_zoneid)
9497c478bd9Sstevel@tonic-gate {
9507c478bd9Sstevel@tonic-gate 	size_t ks_data_size;
9517c478bd9Sstevel@tonic-gate 	kstat_t *ksp;
9527c478bd9Sstevel@tonic-gate 	ekstat_t *e;
9537c478bd9Sstevel@tonic-gate 	avl_index_t where;
9547c478bd9Sstevel@tonic-gate 	char namebuf[KSTAT_STRLEN + 16];
9557c478bd9Sstevel@tonic-gate 
9567c478bd9Sstevel@tonic-gate 	if (avl_numnodes(&kstat_avl_bykid) == 0) {
9577c478bd9Sstevel@tonic-gate 		avl_create(&kstat_avl_bykid, kstat_compare_bykid,
9587c478bd9Sstevel@tonic-gate 		    sizeof (ekstat_t), offsetof(struct ekstat, e_avl_bykid));
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate 		avl_create(&kstat_avl_byname, kstat_compare_byname,
9617c478bd9Sstevel@tonic-gate 		    sizeof (ekstat_t), offsetof(struct ekstat, e_avl_byname));
9627c478bd9Sstevel@tonic-gate 	}
9637c478bd9Sstevel@tonic-gate 
9647c478bd9Sstevel@tonic-gate 	/*
9657c478bd9Sstevel@tonic-gate 	 * If ks_name == NULL, set the ks_name to <module><instance>.
9667c478bd9Sstevel@tonic-gate 	 */
9677c478bd9Sstevel@tonic-gate 	if (ks_name == NULL) {
9687c478bd9Sstevel@tonic-gate 		char buf[KSTAT_STRLEN];
9697c478bd9Sstevel@tonic-gate 		kstat_set_string(buf, ks_module);
9707c478bd9Sstevel@tonic-gate 		(void) sprintf(namebuf, "%s%d", buf, ks_instance);
9717c478bd9Sstevel@tonic-gate 		ks_name = namebuf;
9727c478bd9Sstevel@tonic-gate 	}
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate 	/*
9757c478bd9Sstevel@tonic-gate 	 * Make sure it's a valid kstat data type
9767c478bd9Sstevel@tonic-gate 	 */
9777c478bd9Sstevel@tonic-gate 	if (ks_type >= KSTAT_NUM_TYPES) {
9787c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "kstat_create('%s', %d, '%s'): "
9797c478bd9Sstevel@tonic-gate 		    "invalid kstat type %d",
9807c478bd9Sstevel@tonic-gate 		    ks_module, ks_instance, ks_name, ks_type);
9817c478bd9Sstevel@tonic-gate 		return (NULL);
9827c478bd9Sstevel@tonic-gate 	}
9837c478bd9Sstevel@tonic-gate 
9847c478bd9Sstevel@tonic-gate 	/*
9857c478bd9Sstevel@tonic-gate 	 * Don't allow persistent virtual kstats -- it makes no sense.
9867c478bd9Sstevel@tonic-gate 	 * ks_data points to garbage when the client goes away.
9877c478bd9Sstevel@tonic-gate 	 */
9887c478bd9Sstevel@tonic-gate 	if ((ks_flags & KSTAT_FLAG_PERSISTENT) &&
9897c478bd9Sstevel@tonic-gate 	    (ks_flags & KSTAT_FLAG_VIRTUAL)) {
9907c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "kstat_create('%s', %d, '%s'): "
9917c478bd9Sstevel@tonic-gate 		    "cannot create persistent virtual kstat",
9927c478bd9Sstevel@tonic-gate 		    ks_module, ks_instance, ks_name);
9937c478bd9Sstevel@tonic-gate 		return (NULL);
9947c478bd9Sstevel@tonic-gate 	}
9957c478bd9Sstevel@tonic-gate 
9967c478bd9Sstevel@tonic-gate 	/*
9977c478bd9Sstevel@tonic-gate 	 * Don't allow variable-size physical kstats, since the framework's
9987c478bd9Sstevel@tonic-gate 	 * memory allocation for physical kstat data is fixed at creation time.
9997c478bd9Sstevel@tonic-gate 	 */
10007c478bd9Sstevel@tonic-gate 	if ((ks_flags & KSTAT_FLAG_VAR_SIZE) &&
10017c478bd9Sstevel@tonic-gate 	    !(ks_flags & KSTAT_FLAG_VIRTUAL)) {
10027c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "kstat_create('%s', %d, '%s'): "
10037c478bd9Sstevel@tonic-gate 		    "cannot create variable-size physical kstat",
10047c478bd9Sstevel@tonic-gate 		    ks_module, ks_instance, ks_name);
10057c478bd9Sstevel@tonic-gate 		return (NULL);
10067c478bd9Sstevel@tonic-gate 	}
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate 	/*
10097c478bd9Sstevel@tonic-gate 	 * Make sure the number of data fields is within legal range
10107c478bd9Sstevel@tonic-gate 	 */
10117c478bd9Sstevel@tonic-gate 	if (ks_ndata < kstat_data_type[ks_type].min_ndata ||
10127c478bd9Sstevel@tonic-gate 	    ks_ndata > kstat_data_type[ks_type].max_ndata) {
10137c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "kstat_create('%s', %d, '%s'): "
10147c478bd9Sstevel@tonic-gate 		    "ks_ndata=%d out of range [%d, %d]",
10157c478bd9Sstevel@tonic-gate 		    ks_module, ks_instance, ks_name, (int)ks_ndata,
10167c478bd9Sstevel@tonic-gate 		    kstat_data_type[ks_type].min_ndata,
10177c478bd9Sstevel@tonic-gate 		    kstat_data_type[ks_type].max_ndata);
10187c478bd9Sstevel@tonic-gate 		return (NULL);
10197c478bd9Sstevel@tonic-gate 	}
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate 	ks_data_size = kstat_data_type[ks_type].size * ks_ndata;
10227c478bd9Sstevel@tonic-gate 
10237c478bd9Sstevel@tonic-gate 	/*
10247c478bd9Sstevel@tonic-gate 	 * If the named kstat already exists and is dormant, reactivate it.
10257c478bd9Sstevel@tonic-gate 	 */
10267c478bd9Sstevel@tonic-gate 	ksp = kstat_hold_byname(ks_module, ks_instance, ks_name, ks_zoneid);
10277c478bd9Sstevel@tonic-gate 	if (ksp != NULL) {
10287c478bd9Sstevel@tonic-gate 		if (!(ksp->ks_flags & KSTAT_FLAG_DORMANT)) {
10297c478bd9Sstevel@tonic-gate 			/*
10307c478bd9Sstevel@tonic-gate 			 * The named kstat exists but is not dormant --
10317c478bd9Sstevel@tonic-gate 			 * this is a kstat namespace collision.
10327c478bd9Sstevel@tonic-gate 			 */
10337c478bd9Sstevel@tonic-gate 			kstat_rele(ksp);
10347c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
10357c478bd9Sstevel@tonic-gate 			    "kstat_create('%s', %d, '%s'): namespace collision",
10367c478bd9Sstevel@tonic-gate 			    ks_module, ks_instance, ks_name);
10377c478bd9Sstevel@tonic-gate 			return (NULL);
10387c478bd9Sstevel@tonic-gate 		}
10397c478bd9Sstevel@tonic-gate 		if ((strcmp(ksp->ks_class, ks_class) != 0) ||
10407c478bd9Sstevel@tonic-gate 		    (ksp->ks_type != ks_type) ||
10417c478bd9Sstevel@tonic-gate 		    (ksp->ks_ndata != ks_ndata) ||
10427c478bd9Sstevel@tonic-gate 		    (ks_flags & KSTAT_FLAG_VIRTUAL)) {
10437c478bd9Sstevel@tonic-gate 			/*
10447c478bd9Sstevel@tonic-gate 			 * The name is the same, but the other key parameters
10457c478bd9Sstevel@tonic-gate 			 * differ from those of the dormant kstat -- bogus.
10467c478bd9Sstevel@tonic-gate 			 */
10477c478bd9Sstevel@tonic-gate 			kstat_rele(ksp);
10487c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "kstat_create('%s', %d, '%s'): "
10497c478bd9Sstevel@tonic-gate 			    "invalid reactivation of dormant kstat",
10507c478bd9Sstevel@tonic-gate 			    ks_module, ks_instance, ks_name);
10517c478bd9Sstevel@tonic-gate 			return (NULL);
10527c478bd9Sstevel@tonic-gate 		}
10537c478bd9Sstevel@tonic-gate 		/*
10547c478bd9Sstevel@tonic-gate 		 * Return dormant kstat pointer to caller.  As usual,
10557c478bd9Sstevel@tonic-gate 		 * the kstat is marked invalid until kstat_install().
10567c478bd9Sstevel@tonic-gate 		 */
10577c478bd9Sstevel@tonic-gate 		ksp->ks_flags |= KSTAT_FLAG_INVALID;
10587c478bd9Sstevel@tonic-gate 		kstat_rele(ksp);
10597c478bd9Sstevel@tonic-gate 		return (ksp);
10607c478bd9Sstevel@tonic-gate 	}
10617c478bd9Sstevel@tonic-gate 
10627c478bd9Sstevel@tonic-gate 	/*
10637c478bd9Sstevel@tonic-gate 	 * Allocate memory for the new kstat header and, if this is a physical
10647c478bd9Sstevel@tonic-gate 	 * kstat, the data section.
10657c478bd9Sstevel@tonic-gate 	 */
10667c478bd9Sstevel@tonic-gate 	e = kstat_alloc(ks_flags & KSTAT_FLAG_VIRTUAL ? 0 : ks_data_size);
10677c478bd9Sstevel@tonic-gate 	if (e == NULL) {
10687c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "kstat_create('%s', %d, '%s'): "
10697c478bd9Sstevel@tonic-gate 		    "insufficient kernel memory",
10707c478bd9Sstevel@tonic-gate 		    ks_module, ks_instance, ks_name);
10717c478bd9Sstevel@tonic-gate 		return (NULL);
10727c478bd9Sstevel@tonic-gate 	}
10737c478bd9Sstevel@tonic-gate 
10747c478bd9Sstevel@tonic-gate 	/*
10757c478bd9Sstevel@tonic-gate 	 * Initialize as many fields as we can.  The caller may reset
10767c478bd9Sstevel@tonic-gate 	 * ks_lock, ks_update, ks_private, and ks_snapshot as necessary.
10777c478bd9Sstevel@tonic-gate 	 * Creators of virtual kstats may also reset ks_data.  It is
10787c478bd9Sstevel@tonic-gate 	 * also up to the caller to initialize the kstat data section,
10797c478bd9Sstevel@tonic-gate 	 * if necessary.  All initialization must be complete before
10807c478bd9Sstevel@tonic-gate 	 * calling kstat_install().
10817c478bd9Sstevel@tonic-gate 	 */
10827c478bd9Sstevel@tonic-gate 	e->e_zone.zoneid = ks_zoneid;
10837c478bd9Sstevel@tonic-gate 	e->e_zone.next = NULL;
10847c478bd9Sstevel@tonic-gate 
10857c478bd9Sstevel@tonic-gate 	ksp = &e->e_ks;
10867c478bd9Sstevel@tonic-gate 	ksp->ks_crtime		= gethrtime();
10877c478bd9Sstevel@tonic-gate 	kstat_set_string(ksp->ks_module, ks_module);
10887c478bd9Sstevel@tonic-gate 	ksp->ks_instance	= ks_instance;
10897c478bd9Sstevel@tonic-gate 	kstat_set_string(ksp->ks_name, ks_name);
10907c478bd9Sstevel@tonic-gate 	ksp->ks_type		= ks_type;
10917c478bd9Sstevel@tonic-gate 	kstat_set_string(ksp->ks_class, ks_class);
10927c478bd9Sstevel@tonic-gate 	ksp->ks_flags		= ks_flags | KSTAT_FLAG_INVALID;
10937c478bd9Sstevel@tonic-gate 	if (ks_flags & KSTAT_FLAG_VIRTUAL)
10947c478bd9Sstevel@tonic-gate 		ksp->ks_data	= NULL;
10957c478bd9Sstevel@tonic-gate 	else
10967c478bd9Sstevel@tonic-gate 		ksp->ks_data	= (void *)(e + 1);
10977c478bd9Sstevel@tonic-gate 	ksp->ks_ndata		= ks_ndata;
10987c478bd9Sstevel@tonic-gate 	ksp->ks_data_size	= ks_data_size;
10997c478bd9Sstevel@tonic-gate 	ksp->ks_snaptime	= ksp->ks_crtime;
11007c478bd9Sstevel@tonic-gate 	ksp->ks_update		= default_kstat_update;
11017c478bd9Sstevel@tonic-gate 	ksp->ks_private		= NULL;
11027c478bd9Sstevel@tonic-gate 	ksp->ks_snapshot	= default_kstat_snapshot;
11037c478bd9Sstevel@tonic-gate 	ksp->ks_lock		= NULL;
11047c478bd9Sstevel@tonic-gate 
11057c478bd9Sstevel@tonic-gate 	mutex_enter(&kstat_chain_lock);
11067c478bd9Sstevel@tonic-gate 
11077c478bd9Sstevel@tonic-gate 	/*
11087c478bd9Sstevel@tonic-gate 	 * Add our kstat to the AVL trees.
11097c478bd9Sstevel@tonic-gate 	 */
11107c478bd9Sstevel@tonic-gate 	if (avl_find(&kstat_avl_byname, e, &where) != NULL) {
11117c478bd9Sstevel@tonic-gate 		mutex_exit(&kstat_chain_lock);
11127c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
11137c478bd9Sstevel@tonic-gate 		    "kstat_create('%s', %d, '%s'): namespace collision",
11147c478bd9Sstevel@tonic-gate 		    ks_module, ks_instance, ks_name);
11157c478bd9Sstevel@tonic-gate 		kstat_free(e);
11167c478bd9Sstevel@tonic-gate 		return (NULL);
11177c478bd9Sstevel@tonic-gate 	}
11187c478bd9Sstevel@tonic-gate 	avl_insert(&kstat_avl_byname, e, where);
11197c478bd9Sstevel@tonic-gate 
11207c478bd9Sstevel@tonic-gate 	/*
11217c478bd9Sstevel@tonic-gate 	 * Loop around until we find an unused KID.
11227c478bd9Sstevel@tonic-gate 	 */
11237c478bd9Sstevel@tonic-gate 	do {
11247c478bd9Sstevel@tonic-gate 		ksp->ks_kid = kstat_chain_id++;
11257c478bd9Sstevel@tonic-gate 	} while (avl_find(&kstat_avl_bykid, e, &where) != NULL);
11267c478bd9Sstevel@tonic-gate 	avl_insert(&kstat_avl_bykid, e, where);
11277c478bd9Sstevel@tonic-gate 
11287c478bd9Sstevel@tonic-gate 	mutex_exit(&kstat_chain_lock);
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate 	return (ksp);
11317c478bd9Sstevel@tonic-gate }
11327c478bd9Sstevel@tonic-gate 
11337c478bd9Sstevel@tonic-gate /*
11347c478bd9Sstevel@tonic-gate  * Activate a fully initialized kstat and make it visible to /dev/kstat.
11357c478bd9Sstevel@tonic-gate  */
11367c478bd9Sstevel@tonic-gate void
11377c478bd9Sstevel@tonic-gate kstat_install(kstat_t *ksp)
11387c478bd9Sstevel@tonic-gate {
11397c478bd9Sstevel@tonic-gate 	zoneid_t zoneid = ((ekstat_t *)ksp)->e_zone.zoneid;
11407c478bd9Sstevel@tonic-gate 
11417c478bd9Sstevel@tonic-gate 	/*
11427c478bd9Sstevel@tonic-gate 	 * If this is a variable-size kstat, it MUST provide kstat data locking
11437c478bd9Sstevel@tonic-gate 	 * to prevent data-size races with kstat readers.
11447c478bd9Sstevel@tonic-gate 	 */
11457c478bd9Sstevel@tonic-gate 	if ((ksp->ks_flags & KSTAT_FLAG_VAR_SIZE) && ksp->ks_lock == NULL) {
11467c478bd9Sstevel@tonic-gate 		panic("kstat_install('%s', %d, '%s'): "
11477c478bd9Sstevel@tonic-gate 		    "cannot create variable-size kstat without data lock",
11487c478bd9Sstevel@tonic-gate 		    ksp->ks_module, ksp->ks_instance, ksp->ks_name);
11497c478bd9Sstevel@tonic-gate 	}
11507c478bd9Sstevel@tonic-gate 
11517c478bd9Sstevel@tonic-gate 	if (kstat_hold_bykid(ksp->ks_kid, zoneid) != ksp) {
11527c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "kstat_install(%p): does not exist",
11537c478bd9Sstevel@tonic-gate 		    (void *)ksp);
11547c478bd9Sstevel@tonic-gate 		return;
11557c478bd9Sstevel@tonic-gate 	}
11567c478bd9Sstevel@tonic-gate 
11577c478bd9Sstevel@tonic-gate 	if (ksp->ks_type == KSTAT_TYPE_NAMED && ksp->ks_data != NULL) {
11587c478bd9Sstevel@tonic-gate 		int has_long_strings = 0;
11597c478bd9Sstevel@tonic-gate 		uint_t i;
11607c478bd9Sstevel@tonic-gate 		kstat_named_t *knp = KSTAT_NAMED_PTR(ksp);
11617c478bd9Sstevel@tonic-gate 
11627c478bd9Sstevel@tonic-gate 		for (i = 0; i < ksp->ks_ndata; i++, knp++) {
11637c478bd9Sstevel@tonic-gate 			if (knp->data_type == KSTAT_DATA_STRING) {
11647c478bd9Sstevel@tonic-gate 				has_long_strings = 1;
11657c478bd9Sstevel@tonic-gate 				break;
11667c478bd9Sstevel@tonic-gate 			}
11677c478bd9Sstevel@tonic-gate 		}
11687c478bd9Sstevel@tonic-gate 		/*
11697c478bd9Sstevel@tonic-gate 		 * It is an error for a named kstat with fields of
11707c478bd9Sstevel@tonic-gate 		 * KSTAT_DATA_STRING to be non-virtual.
11717c478bd9Sstevel@tonic-gate 		 */
11727c478bd9Sstevel@tonic-gate 		if (has_long_strings && !(ksp->ks_flags & KSTAT_FLAG_VIRTUAL)) {
11737c478bd9Sstevel@tonic-gate 			panic("kstat_install('%s', %d, '%s'): "
11747c478bd9Sstevel@tonic-gate 			    "named kstat containing KSTAT_DATA_STRING "
11757c478bd9Sstevel@tonic-gate 			    "is not virtual",
11767c478bd9Sstevel@tonic-gate 			    ksp->ks_module, ksp->ks_instance,
11777c478bd9Sstevel@tonic-gate 			    ksp->ks_name);
11787c478bd9Sstevel@tonic-gate 		}
11797c478bd9Sstevel@tonic-gate 		/*
11807c478bd9Sstevel@tonic-gate 		 * The default snapshot routine does not handle KSTAT_WRITE
11817c478bd9Sstevel@tonic-gate 		 * for long strings.
11827c478bd9Sstevel@tonic-gate 		 */
11837c478bd9Sstevel@tonic-gate 		if (has_long_strings && (ksp->ks_flags & KSTAT_FLAG_WRITABLE) &&
11847c478bd9Sstevel@tonic-gate 		    (ksp->ks_snapshot == default_kstat_snapshot)) {
11857c478bd9Sstevel@tonic-gate 			panic("kstat_install('%s', %d, '%s'): "
11867c478bd9Sstevel@tonic-gate 			    "named kstat containing KSTAT_DATA_STRING "
11877c478bd9Sstevel@tonic-gate 			    "is writable but uses default snapshot routine",
11887c478bd9Sstevel@tonic-gate 			    ksp->ks_module, ksp->ks_instance, ksp->ks_name);
11897c478bd9Sstevel@tonic-gate 		}
11907c478bd9Sstevel@tonic-gate 	}
11917c478bd9Sstevel@tonic-gate 
11927c478bd9Sstevel@tonic-gate 	if (ksp->ks_flags & KSTAT_FLAG_DORMANT) {
11937c478bd9Sstevel@tonic-gate 
11947c478bd9Sstevel@tonic-gate 		/*
11957c478bd9Sstevel@tonic-gate 		 * We are reactivating a dormant kstat.  Initialize the
11967c478bd9Sstevel@tonic-gate 		 * caller's underlying data to the value it had when the
11977c478bd9Sstevel@tonic-gate 		 * kstat went dormant, and mark the kstat as active.
11987c478bd9Sstevel@tonic-gate 		 * Grab the provider's kstat lock if it's not already held.
11997c478bd9Sstevel@tonic-gate 		 */
12007c478bd9Sstevel@tonic-gate 		kmutex_t *lp = ksp->ks_lock;
12017c478bd9Sstevel@tonic-gate 		if (lp != NULL && MUTEX_NOT_HELD(lp)) {
12027c478bd9Sstevel@tonic-gate 			mutex_enter(lp);
12037c478bd9Sstevel@tonic-gate 			(void) KSTAT_UPDATE(ksp, KSTAT_WRITE);
12047c478bd9Sstevel@tonic-gate 			mutex_exit(lp);
12057c478bd9Sstevel@tonic-gate 		} else {
12067c478bd9Sstevel@tonic-gate 			(void) KSTAT_UPDATE(ksp, KSTAT_WRITE);
12077c478bd9Sstevel@tonic-gate 		}
12087c478bd9Sstevel@tonic-gate 		ksp->ks_flags &= ~KSTAT_FLAG_DORMANT;
12097c478bd9Sstevel@tonic-gate 	}
12107c478bd9Sstevel@tonic-gate 
12117c478bd9Sstevel@tonic-gate 	/*
12127c478bd9Sstevel@tonic-gate 	 * Now that the kstat is active, make it visible to the kstat driver.
1213*c9bbee95SJerry Jelinek 	 * When copying out kstats the count is determined in
1214*c9bbee95SJerry Jelinek 	 * header_kstat_update() and actually copied into kbuf in
1215*c9bbee95SJerry Jelinek 	 * header_kstat_snapshot(). kstat_chain_lock is held across the two
1216*c9bbee95SJerry Jelinek 	 * calls to ensure that this list doesn't change. Thus, we need to
1217*c9bbee95SJerry Jelinek 	 * also take the lock to ensure that the we don't copy the new kstat
1218*c9bbee95SJerry Jelinek 	 * in the 2nd pass and overrun the buf.
12197c478bd9Sstevel@tonic-gate 	 */
1220*c9bbee95SJerry Jelinek 	mutex_enter(&kstat_chain_lock);
12217c478bd9Sstevel@tonic-gate 	ksp->ks_flags &= ~KSTAT_FLAG_INVALID;
1222*c9bbee95SJerry Jelinek 	mutex_exit(&kstat_chain_lock);
12237c478bd9Sstevel@tonic-gate 	kstat_rele(ksp);
12247c478bd9Sstevel@tonic-gate }
12257c478bd9Sstevel@tonic-gate 
12267c478bd9Sstevel@tonic-gate /*
12277c478bd9Sstevel@tonic-gate  * Remove a kstat from the system.  Or, if it's a persistent kstat,
12287c478bd9Sstevel@tonic-gate  * just update the data and mark it as dormant.
12297c478bd9Sstevel@tonic-gate  */
12307c478bd9Sstevel@tonic-gate void
12317c478bd9Sstevel@tonic-gate kstat_delete(kstat_t *ksp)
12327c478bd9Sstevel@tonic-gate {
12337c478bd9Sstevel@tonic-gate 	kmutex_t *lp;
12347c478bd9Sstevel@tonic-gate 	ekstat_t *e = (ekstat_t *)ksp;
12353c5c7d01Sbatschul 	zoneid_t zoneid;
12367c478bd9Sstevel@tonic-gate 	kstat_zone_t *kz;
12377c478bd9Sstevel@tonic-gate 
12383c5c7d01Sbatschul 	ASSERT(ksp != NULL);
12393c5c7d01Sbatschul 
12407c478bd9Sstevel@tonic-gate 	if (ksp == NULL)
12417c478bd9Sstevel@tonic-gate 		return;
12427c478bd9Sstevel@tonic-gate 
12433c5c7d01Sbatschul 	zoneid = e->e_zone.zoneid;
12443c5c7d01Sbatschul 
12457c478bd9Sstevel@tonic-gate 	lp = ksp->ks_lock;
12467c478bd9Sstevel@tonic-gate 
12477c478bd9Sstevel@tonic-gate 	if (lp != NULL && MUTEX_HELD(lp)) {
12487c478bd9Sstevel@tonic-gate 		panic("kstat_delete(%p): caller holds data lock %p",
12497c478bd9Sstevel@tonic-gate 		    (void *)ksp, (void *)lp);
12507c478bd9Sstevel@tonic-gate 	}
12517c478bd9Sstevel@tonic-gate 
12527c478bd9Sstevel@tonic-gate 	if (kstat_hold_bykid(ksp->ks_kid, zoneid) != ksp) {
12537c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "kstat_delete(%p): does not exist",
12547c478bd9Sstevel@tonic-gate 		    (void *)ksp);
12557c478bd9Sstevel@tonic-gate 		return;
12567c478bd9Sstevel@tonic-gate 	}
12577c478bd9Sstevel@tonic-gate 
12587c478bd9Sstevel@tonic-gate 	if (ksp->ks_flags & KSTAT_FLAG_PERSISTENT) {
12597c478bd9Sstevel@tonic-gate 		/*
12607c478bd9Sstevel@tonic-gate 		 * Update the data one last time, so that all activity
12617c478bd9Sstevel@tonic-gate 		 * prior to going dormant has been accounted for.
12627c478bd9Sstevel@tonic-gate 		 */
12637c478bd9Sstevel@tonic-gate 		KSTAT_ENTER(ksp);
12647c478bd9Sstevel@tonic-gate 		(void) KSTAT_UPDATE(ksp, KSTAT_READ);
12657c478bd9Sstevel@tonic-gate 		KSTAT_EXIT(ksp);
12667c478bd9Sstevel@tonic-gate 
12677c478bd9Sstevel@tonic-gate 		/*
12687c478bd9Sstevel@tonic-gate 		 * Mark the kstat as dormant and restore caller-modifiable
12697c478bd9Sstevel@tonic-gate 		 * fields to default values, so the kstat is readable during
12707c478bd9Sstevel@tonic-gate 		 * the dormant phase.
12717c478bd9Sstevel@tonic-gate 		 */
12727c478bd9Sstevel@tonic-gate 		ksp->ks_flags |= KSTAT_FLAG_DORMANT;
12737c478bd9Sstevel@tonic-gate 		ksp->ks_lock = NULL;
12747c478bd9Sstevel@tonic-gate 		ksp->ks_update = default_kstat_update;
12757c478bd9Sstevel@tonic-gate 		ksp->ks_private = NULL;
12767c478bd9Sstevel@tonic-gate 		ksp->ks_snapshot = default_kstat_snapshot;
12777c478bd9Sstevel@tonic-gate 		kstat_rele(ksp);
12787c478bd9Sstevel@tonic-gate 		return;
12797c478bd9Sstevel@tonic-gate 	}
12807c478bd9Sstevel@tonic-gate 
12817c478bd9Sstevel@tonic-gate 	/*
12827c478bd9Sstevel@tonic-gate 	 * Remove the kstat from the framework's AVL trees,
12837c478bd9Sstevel@tonic-gate 	 * free the allocated memory, and increment kstat_chain_id so
12847c478bd9Sstevel@tonic-gate 	 * /dev/kstat clients can detect the event.
12857c478bd9Sstevel@tonic-gate 	 */
12867c478bd9Sstevel@tonic-gate 	mutex_enter(&kstat_chain_lock);
12877c478bd9Sstevel@tonic-gate 	avl_remove(&kstat_avl_bykid, e);
12887c478bd9Sstevel@tonic-gate 	avl_remove(&kstat_avl_byname, e);
12897c478bd9Sstevel@tonic-gate 	kstat_chain_id++;
12907c478bd9Sstevel@tonic-gate 	mutex_exit(&kstat_chain_lock);
12917c478bd9Sstevel@tonic-gate 
12927c478bd9Sstevel@tonic-gate 	kz = e->e_zone.next;
12937c478bd9Sstevel@tonic-gate 	while (kz != NULL) {
12947c478bd9Sstevel@tonic-gate 		kstat_zone_t *t = kz;
12957c478bd9Sstevel@tonic-gate 
12967c478bd9Sstevel@tonic-gate 		kz = kz->next;
12977c478bd9Sstevel@tonic-gate 		kmem_free(t, sizeof (*t));
12987c478bd9Sstevel@tonic-gate 	}
12997c478bd9Sstevel@tonic-gate 	kstat_rele(ksp);
13007c478bd9Sstevel@tonic-gate 	kstat_free(e);
13017c478bd9Sstevel@tonic-gate }
13027c478bd9Sstevel@tonic-gate 
13037c478bd9Sstevel@tonic-gate void
1304d624471bSelowe kstat_delete_byname_zone(const char *ks_module, int ks_instance,
1305d624471bSelowe     const char *ks_name, zoneid_t ks_zoneid)
13067c478bd9Sstevel@tonic-gate {
13077c478bd9Sstevel@tonic-gate 	kstat_t *ksp;
13087c478bd9Sstevel@tonic-gate 
13097c478bd9Sstevel@tonic-gate 	ksp = kstat_hold_byname(ks_module, ks_instance, ks_name, ks_zoneid);
13107c478bd9Sstevel@tonic-gate 	if (ksp != NULL) {
13117c478bd9Sstevel@tonic-gate 		kstat_rele(ksp);
13127c478bd9Sstevel@tonic-gate 		kstat_delete(ksp);
13137c478bd9Sstevel@tonic-gate 	}
13147c478bd9Sstevel@tonic-gate }
13157c478bd9Sstevel@tonic-gate 
13167c478bd9Sstevel@tonic-gate void
1317d624471bSelowe kstat_delete_byname(const char *ks_module, int ks_instance, const char *ks_name)
13187c478bd9Sstevel@tonic-gate {
13197c478bd9Sstevel@tonic-gate 	kstat_delete_byname_zone(ks_module, ks_instance, ks_name, ALL_ZONES);
13207c478bd9Sstevel@tonic-gate }
13217c478bd9Sstevel@tonic-gate 
13227c478bd9Sstevel@tonic-gate /*
13237c478bd9Sstevel@tonic-gate  * The sparc V9 versions of these routines can be much cheaper than
13247c478bd9Sstevel@tonic-gate  * the poor 32-bit compiler can comprehend, so they're in sparcv9_subr.s.
13257c478bd9Sstevel@tonic-gate  * For simplicity, however, we always feed the C versions to lint.
13267c478bd9Sstevel@tonic-gate  */
13277c478bd9Sstevel@tonic-gate #if !defined(__sparc) || defined(lint) || defined(__lint)
13287c478bd9Sstevel@tonic-gate 
13297c478bd9Sstevel@tonic-gate void
13307c478bd9Sstevel@tonic-gate kstat_waitq_enter(kstat_io_t *kiop)
13317c478bd9Sstevel@tonic-gate {
13327c478bd9Sstevel@tonic-gate 	hrtime_t new, delta;
13337c478bd9Sstevel@tonic-gate 	ulong_t wcnt;
13347c478bd9Sstevel@tonic-gate 
13357c478bd9Sstevel@tonic-gate 	new = gethrtime_unscaled();
13367c478bd9Sstevel@tonic-gate 	delta = new - kiop->wlastupdate;
13377c478bd9Sstevel@tonic-gate 	kiop->wlastupdate = new;
13387c478bd9Sstevel@tonic-gate 	wcnt = kiop->wcnt++;
13397c478bd9Sstevel@tonic-gate 	if (wcnt != 0) {
13407c478bd9Sstevel@tonic-gate 		kiop->wlentime += delta * wcnt;
13417c478bd9Sstevel@tonic-gate 		kiop->wtime += delta;
13427c478bd9Sstevel@tonic-gate 	}
13437c478bd9Sstevel@tonic-gate }
13447c478bd9Sstevel@tonic-gate 
13457c478bd9Sstevel@tonic-gate void
13467c478bd9Sstevel@tonic-gate kstat_waitq_exit(kstat_io_t *kiop)
13477c478bd9Sstevel@tonic-gate {
13487c478bd9Sstevel@tonic-gate 	hrtime_t new, delta;
13497c478bd9Sstevel@tonic-gate 	ulong_t wcnt;
13507c478bd9Sstevel@tonic-gate 
13517c478bd9Sstevel@tonic-gate 	new = gethrtime_unscaled();
13527c478bd9Sstevel@tonic-gate 	delta = new - kiop->wlastupdate;
13537c478bd9Sstevel@tonic-gate 	kiop->wlastupdate = new;
13547c478bd9Sstevel@tonic-gate 	wcnt = kiop->wcnt--;
13557c478bd9Sstevel@tonic-gate 	ASSERT((int)wcnt > 0);
13567c478bd9Sstevel@tonic-gate 	kiop->wlentime += delta * wcnt;
13577c478bd9Sstevel@tonic-gate 	kiop->wtime += delta;
13587c478bd9Sstevel@tonic-gate }
13597c478bd9Sstevel@tonic-gate 
13607c478bd9Sstevel@tonic-gate void
13617c478bd9Sstevel@tonic-gate kstat_runq_enter(kstat_io_t *kiop)
13627c478bd9Sstevel@tonic-gate {
13637c478bd9Sstevel@tonic-gate 	hrtime_t new, delta;
13647c478bd9Sstevel@tonic-gate 	ulong_t rcnt;
13657c478bd9Sstevel@tonic-gate 
13667c478bd9Sstevel@tonic-gate 	new = gethrtime_unscaled();
13677c478bd9Sstevel@tonic-gate 	delta = new - kiop->rlastupdate;
13687c478bd9Sstevel@tonic-gate 	kiop->rlastupdate = new;
13697c478bd9Sstevel@tonic-gate 	rcnt = kiop->rcnt++;
13707c478bd9Sstevel@tonic-gate 	if (rcnt != 0) {
13717c478bd9Sstevel@tonic-gate 		kiop->rlentime += delta * rcnt;
13727c478bd9Sstevel@tonic-gate 		kiop->rtime += delta;
13737c478bd9Sstevel@tonic-gate 	}
13747c478bd9Sstevel@tonic-gate }
13757c478bd9Sstevel@tonic-gate 
13767c478bd9Sstevel@tonic-gate void
13777c478bd9Sstevel@tonic-gate kstat_runq_exit(kstat_io_t *kiop)
13787c478bd9Sstevel@tonic-gate {
13797c478bd9Sstevel@tonic-gate 	hrtime_t new, delta;
13807c478bd9Sstevel@tonic-gate 	ulong_t rcnt;
13817c478bd9Sstevel@tonic-gate 
13827c478bd9Sstevel@tonic-gate 	new = gethrtime_unscaled();
13837c478bd9Sstevel@tonic-gate 	delta = new - kiop->rlastupdate;
13847c478bd9Sstevel@tonic-gate 	kiop->rlastupdate = new;
13857c478bd9Sstevel@tonic-gate 	rcnt = kiop->rcnt--;
13867c478bd9Sstevel@tonic-gate 	ASSERT((int)rcnt > 0);
13877c478bd9Sstevel@tonic-gate 	kiop->rlentime += delta * rcnt;
13887c478bd9Sstevel@tonic-gate 	kiop->rtime += delta;
13897c478bd9Sstevel@tonic-gate }
13907c478bd9Sstevel@tonic-gate 
13917c478bd9Sstevel@tonic-gate void
13927c478bd9Sstevel@tonic-gate kstat_waitq_to_runq(kstat_io_t *kiop)
13937c478bd9Sstevel@tonic-gate {
13947c478bd9Sstevel@tonic-gate 	hrtime_t new, delta;
13957c478bd9Sstevel@tonic-gate 	ulong_t wcnt, rcnt;
13967c478bd9Sstevel@tonic-gate 
13977c478bd9Sstevel@tonic-gate 	new = gethrtime_unscaled();
13987c478bd9Sstevel@tonic-gate 
13997c478bd9Sstevel@tonic-gate 	delta = new - kiop->wlastupdate;
14007c478bd9Sstevel@tonic-gate 	kiop->wlastupdate = new;
14017c478bd9Sstevel@tonic-gate 	wcnt = kiop->wcnt--;
14027c478bd9Sstevel@tonic-gate 	ASSERT((int)wcnt > 0);
14037c478bd9Sstevel@tonic-gate 	kiop->wlentime += delta * wcnt;
14047c478bd9Sstevel@tonic-gate 	kiop->wtime += delta;
14057c478bd9Sstevel@tonic-gate 
14067c478bd9Sstevel@tonic-gate 	delta = new - kiop->rlastupdate;
14077c478bd9Sstevel@tonic-gate 	kiop->rlastupdate = new;
14087c478bd9Sstevel@tonic-gate 	rcnt = kiop->rcnt++;
14097c478bd9Sstevel@tonic-gate 	if (rcnt != 0) {
14107c478bd9Sstevel@tonic-gate 		kiop->rlentime += delta * rcnt;
14117c478bd9Sstevel@tonic-gate 		kiop->rtime += delta;
14127c478bd9Sstevel@tonic-gate 	}
14137c478bd9Sstevel@tonic-gate }
14147c478bd9Sstevel@tonic-gate 
14157c478bd9Sstevel@tonic-gate void
14167c478bd9Sstevel@tonic-gate kstat_runq_back_to_waitq(kstat_io_t *kiop)
14177c478bd9Sstevel@tonic-gate {
14187c478bd9Sstevel@tonic-gate 	hrtime_t new, delta;
14197c478bd9Sstevel@tonic-gate 	ulong_t wcnt, rcnt;
14207c478bd9Sstevel@tonic-gate 
14217c478bd9Sstevel@tonic-gate 	new = gethrtime_unscaled();
14227c478bd9Sstevel@tonic-gate 
14237c478bd9Sstevel@tonic-gate 	delta = new - kiop->rlastupdate;
14247c478bd9Sstevel@tonic-gate 	kiop->rlastupdate = new;
14257c478bd9Sstevel@tonic-gate 	rcnt = kiop->rcnt--;
14267c478bd9Sstevel@tonic-gate 	ASSERT((int)rcnt > 0);
14277c478bd9Sstevel@tonic-gate 	kiop->rlentime += delta * rcnt;
14287c478bd9Sstevel@tonic-gate 	kiop->rtime += delta;
14297c478bd9Sstevel@tonic-gate 
14307c478bd9Sstevel@tonic-gate 	delta = new - kiop->wlastupdate;
14317c478bd9Sstevel@tonic-gate 	kiop->wlastupdate = new;
14327c478bd9Sstevel@tonic-gate 	wcnt = kiop->wcnt++;
14337c478bd9Sstevel@tonic-gate 	if (wcnt != 0) {
14347c478bd9Sstevel@tonic-gate 		kiop->wlentime += delta * wcnt;
14357c478bd9Sstevel@tonic-gate 		kiop->wtime += delta;
14367c478bd9Sstevel@tonic-gate 	}
14377c478bd9Sstevel@tonic-gate }
14387c478bd9Sstevel@tonic-gate 
14397c478bd9Sstevel@tonic-gate #endif
14407c478bd9Sstevel@tonic-gate 
14417c478bd9Sstevel@tonic-gate void
14427c478bd9Sstevel@tonic-gate kstat_timer_start(kstat_timer_t *ktp)
14437c478bd9Sstevel@tonic-gate {
14447c478bd9Sstevel@tonic-gate 	ktp->start_time = gethrtime();
14457c478bd9Sstevel@tonic-gate }
14467c478bd9Sstevel@tonic-gate 
14477c478bd9Sstevel@tonic-gate void
14487c478bd9Sstevel@tonic-gate kstat_timer_stop(kstat_timer_t *ktp)
14497c478bd9Sstevel@tonic-gate {
14507c478bd9Sstevel@tonic-gate 	hrtime_t	etime;
14517c478bd9Sstevel@tonic-gate 	u_longlong_t	num_events;
14527c478bd9Sstevel@tonic-gate 
14537c478bd9Sstevel@tonic-gate 	ktp->stop_time = etime = gethrtime();
14547c478bd9Sstevel@tonic-gate 	etime -= ktp->start_time;
14557c478bd9Sstevel@tonic-gate 	num_events = ktp->num_events;
14567c478bd9Sstevel@tonic-gate 	if (etime < ktp->min_time || num_events == 0)
14577c478bd9Sstevel@tonic-gate 		ktp->min_time = etime;
14587c478bd9Sstevel@tonic-gate 	if (etime > ktp->max_time)
14597c478bd9Sstevel@tonic-gate 		ktp->max_time = etime;
14607c478bd9Sstevel@tonic-gate 	ktp->elapsed_time += etime;
14617c478bd9Sstevel@tonic-gate 	ktp->num_events = num_events + 1;
14627c478bd9Sstevel@tonic-gate }
1463