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