10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*2951Selowe * Common Development and Distribution License (the "License"). 6*2951Selowe * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 22*2951Selowe * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #ifndef _SYS_KSTAT_H 270Sstevel@tonic-gate #define _SYS_KSTAT_H 280Sstevel@tonic-gate 290Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 300Sstevel@tonic-gate 310Sstevel@tonic-gate /* 320Sstevel@tonic-gate * Definition of general kernel statistics structures and /dev/kstat ioctls 330Sstevel@tonic-gate */ 340Sstevel@tonic-gate 350Sstevel@tonic-gate #include <sys/types.h> 360Sstevel@tonic-gate #include <sys/time.h> 370Sstevel@tonic-gate 380Sstevel@tonic-gate #ifdef __cplusplus 390Sstevel@tonic-gate extern "C" { 400Sstevel@tonic-gate #endif 410Sstevel@tonic-gate 420Sstevel@tonic-gate typedef int kid_t; /* unique kstat id */ 430Sstevel@tonic-gate 440Sstevel@tonic-gate /* 450Sstevel@tonic-gate * Kernel statistics driver (/dev/kstat) ioctls 460Sstevel@tonic-gate */ 470Sstevel@tonic-gate 480Sstevel@tonic-gate #define KSTAT_IOC_BASE ('K' << 8) 490Sstevel@tonic-gate 500Sstevel@tonic-gate #define KSTAT_IOC_CHAIN_ID KSTAT_IOC_BASE | 0x01 510Sstevel@tonic-gate #define KSTAT_IOC_READ KSTAT_IOC_BASE | 0x02 520Sstevel@tonic-gate #define KSTAT_IOC_WRITE KSTAT_IOC_BASE | 0x03 530Sstevel@tonic-gate 540Sstevel@tonic-gate /* 550Sstevel@tonic-gate * /dev/kstat ioctl usage (kd denotes /dev/kstat descriptor): 560Sstevel@tonic-gate * 570Sstevel@tonic-gate * kcid = ioctl(kd, KSTAT_IOC_CHAIN_ID, NULL); 580Sstevel@tonic-gate * kcid = ioctl(kd, KSTAT_IOC_READ, kstat_t *); 590Sstevel@tonic-gate * kcid = ioctl(kd, KSTAT_IOC_WRITE, kstat_t *); 600Sstevel@tonic-gate */ 610Sstevel@tonic-gate 620Sstevel@tonic-gate #define KSTAT_STRLEN 31 /* 30 chars + NULL; must be 16 * n - 1 */ 630Sstevel@tonic-gate 640Sstevel@tonic-gate /* 650Sstevel@tonic-gate * The generic kstat header 660Sstevel@tonic-gate */ 670Sstevel@tonic-gate 680Sstevel@tonic-gate typedef struct kstat { 690Sstevel@tonic-gate /* 700Sstevel@tonic-gate * Fields relevant to both kernel and user 710Sstevel@tonic-gate */ 720Sstevel@tonic-gate hrtime_t ks_crtime; /* creation time (from gethrtime()) */ 730Sstevel@tonic-gate struct kstat *ks_next; /* kstat chain linkage */ 740Sstevel@tonic-gate kid_t ks_kid; /* unique kstat ID */ 750Sstevel@tonic-gate char ks_module[KSTAT_STRLEN]; /* provider module name */ 760Sstevel@tonic-gate uchar_t ks_resv; /* reserved, currently just padding */ 770Sstevel@tonic-gate int ks_instance; /* provider module's instance */ 780Sstevel@tonic-gate char ks_name[KSTAT_STRLEN]; /* kstat name */ 790Sstevel@tonic-gate uchar_t ks_type; /* kstat data type */ 800Sstevel@tonic-gate char ks_class[KSTAT_STRLEN]; /* kstat class */ 810Sstevel@tonic-gate uchar_t ks_flags; /* kstat flags */ 820Sstevel@tonic-gate void *ks_data; /* kstat type-specific data */ 830Sstevel@tonic-gate uint_t ks_ndata; /* # of type-specific data records */ 840Sstevel@tonic-gate size_t ks_data_size; /* total size of kstat data section */ 850Sstevel@tonic-gate hrtime_t ks_snaptime; /* time of last data shapshot */ 860Sstevel@tonic-gate /* 870Sstevel@tonic-gate * Fields relevant to kernel only 880Sstevel@tonic-gate */ 890Sstevel@tonic-gate int (*ks_update)(struct kstat *, int); /* dynamic update */ 900Sstevel@tonic-gate void *ks_private; /* arbitrary provider-private data */ 910Sstevel@tonic-gate int (*ks_snapshot)(struct kstat *, void *, int); 920Sstevel@tonic-gate void *ks_lock; /* protects this kstat's data */ 930Sstevel@tonic-gate } kstat_t; 940Sstevel@tonic-gate 950Sstevel@tonic-gate #ifdef _SYSCALL32 960Sstevel@tonic-gate 970Sstevel@tonic-gate typedef int32_t kid32_t; 980Sstevel@tonic-gate 990Sstevel@tonic-gate typedef struct kstat32 { 1000Sstevel@tonic-gate /* 1010Sstevel@tonic-gate * Fields relevant to both kernel and user 1020Sstevel@tonic-gate */ 1030Sstevel@tonic-gate hrtime_t ks_crtime; 1040Sstevel@tonic-gate caddr32_t ks_next; /* struct kstat pointer */ 1050Sstevel@tonic-gate kid32_t ks_kid; 1060Sstevel@tonic-gate char ks_module[KSTAT_STRLEN]; 1070Sstevel@tonic-gate uint8_t ks_resv; 1080Sstevel@tonic-gate int32_t ks_instance; 1090Sstevel@tonic-gate char ks_name[KSTAT_STRLEN]; 1100Sstevel@tonic-gate uint8_t ks_type; 1110Sstevel@tonic-gate char ks_class[KSTAT_STRLEN]; 1120Sstevel@tonic-gate uint8_t ks_flags; 1130Sstevel@tonic-gate caddr32_t ks_data; /* type-specific data */ 1140Sstevel@tonic-gate uint32_t ks_ndata; 1150Sstevel@tonic-gate size32_t ks_data_size; 1160Sstevel@tonic-gate hrtime_t ks_snaptime; 1170Sstevel@tonic-gate /* 1180Sstevel@tonic-gate * Fields relevant to kernel only (only needed here for padding) 1190Sstevel@tonic-gate */ 1200Sstevel@tonic-gate int32_t _ks_update; 1210Sstevel@tonic-gate caddr32_t _ks_private; 1220Sstevel@tonic-gate int32_t _ks_snapshot; 1230Sstevel@tonic-gate caddr32_t _ks_lock; 1240Sstevel@tonic-gate } kstat32_t; 1250Sstevel@tonic-gate 1260Sstevel@tonic-gate #endif /* _SYSCALL32 */ 1270Sstevel@tonic-gate 1280Sstevel@tonic-gate /* 1290Sstevel@tonic-gate * kstat structure and locking strategy 1300Sstevel@tonic-gate * 1310Sstevel@tonic-gate * Each kstat consists of a header section (a kstat_t) and a data section. 1320Sstevel@tonic-gate * The system maintains a set of kstats, protected by kstat_chain_lock. 1330Sstevel@tonic-gate * kstat_chain_lock protects all additions to/deletions from this set, 1340Sstevel@tonic-gate * as well as all changes to kstat headers. kstat data sections are 1350Sstevel@tonic-gate * *optionally* protected by the per-kstat ks_lock. If ks_lock is non-NULL, 1360Sstevel@tonic-gate * kstat clients (e.g. /dev/kstat) will acquire this lock for all of their 1370Sstevel@tonic-gate * operations on that kstat. It is up to the kstat provider to decide whether 1380Sstevel@tonic-gate * guaranteeing consistent data to kstat clients is sufficiently important 1390Sstevel@tonic-gate * to justify the locking cost. Note, however, that most statistic updates 1400Sstevel@tonic-gate * already occur under one of the provider's mutexes, so if the provider sets 1410Sstevel@tonic-gate * ks_lock to point to that mutex, then kstat data locking is free. 1420Sstevel@tonic-gate * 1430Sstevel@tonic-gate * NOTE: variable-size kstats MUST employ kstat data locking, to prevent 1440Sstevel@tonic-gate * data-size races with kstat clients. 1450Sstevel@tonic-gate * 1460Sstevel@tonic-gate * NOTE: ks_lock is really of type (kmutex_t *); it is declared as (void *) 1470Sstevel@tonic-gate * in the kstat header so that users don't have to be exposed to all of the 1480Sstevel@tonic-gate * kernel's lock-related data structures. 1490Sstevel@tonic-gate */ 1500Sstevel@tonic-gate 1510Sstevel@tonic-gate #if defined(_KERNEL) 1520Sstevel@tonic-gate 1530Sstevel@tonic-gate #define KSTAT_ENTER(k) \ 1540Sstevel@tonic-gate { kmutex_t *lp = (k)->ks_lock; if (lp) mutex_enter(lp); } 1550Sstevel@tonic-gate 1560Sstevel@tonic-gate #define KSTAT_EXIT(k) \ 1570Sstevel@tonic-gate { kmutex_t *lp = (k)->ks_lock; if (lp) mutex_exit(lp); } 1580Sstevel@tonic-gate 1590Sstevel@tonic-gate #define KSTAT_UPDATE(k, rw) (*(k)->ks_update)((k), (rw)) 1600Sstevel@tonic-gate 1610Sstevel@tonic-gate #define KSTAT_SNAPSHOT(k, buf, rw) (*(k)->ks_snapshot)((k), (buf), (rw)) 1620Sstevel@tonic-gate 1630Sstevel@tonic-gate #endif /* defined(_KERNEL) */ 1640Sstevel@tonic-gate 1650Sstevel@tonic-gate /* 1660Sstevel@tonic-gate * kstat time 1670Sstevel@tonic-gate * 1680Sstevel@tonic-gate * All times associated with kstats (e.g. creation time, snapshot time, 1690Sstevel@tonic-gate * kstat_timer_t and kstat_io_t timestamps, etc.) are 64-bit nanosecond values, 1700Sstevel@tonic-gate * as returned by gethrtime(). The accuracy of these timestamps is machine 1710Sstevel@tonic-gate * dependent, but the precision (units) is the same across all platforms. 1720Sstevel@tonic-gate */ 1730Sstevel@tonic-gate 1740Sstevel@tonic-gate /* 1750Sstevel@tonic-gate * kstat identity (KID) 1760Sstevel@tonic-gate * 1770Sstevel@tonic-gate * Each kstat is assigned a unique KID (kstat ID) when it is added to the 1780Sstevel@tonic-gate * global kstat chain. The KID is used as a cookie by /dev/kstat to 1790Sstevel@tonic-gate * request information about the corresponding kstat. There is also 1800Sstevel@tonic-gate * an identity associated with the entire kstat chain, kstat_chain_id, 1810Sstevel@tonic-gate * which is bumped each time a kstat is added or deleted. /dev/kstat uses 1820Sstevel@tonic-gate * the chain ID to detect changes in the kstat chain (e.g., a new disk 1830Sstevel@tonic-gate * coming online) between ioctl()s. 1840Sstevel@tonic-gate */ 1850Sstevel@tonic-gate 1860Sstevel@tonic-gate /* 1870Sstevel@tonic-gate * kstat module, kstat instance 1880Sstevel@tonic-gate * 1890Sstevel@tonic-gate * ks_module and ks_instance contain the name and instance of the module 1900Sstevel@tonic-gate * that created the kstat. In cases where there can only be one instance, 1910Sstevel@tonic-gate * ks_instance is 0. The kernel proper (/kernel/unix) uses "unix" as its 1920Sstevel@tonic-gate * module name. 1930Sstevel@tonic-gate */ 1940Sstevel@tonic-gate 1950Sstevel@tonic-gate /* 1960Sstevel@tonic-gate * kstat name 1970Sstevel@tonic-gate * 1980Sstevel@tonic-gate * ks_name gives a meaningful name to a kstat. The full kstat namespace 1990Sstevel@tonic-gate * is module.instance.name, so the name only need be unique within a 2000Sstevel@tonic-gate * module. kstat_create() will fail if you try to create a kstat with 2010Sstevel@tonic-gate * an already-used (ks_module, ks_instance, ks_name) triplet. Spaces are 2020Sstevel@tonic-gate * allowed in kstat names, but strongly discouraged, since they hinder 2030Sstevel@tonic-gate * awk-style processing at user level. 2040Sstevel@tonic-gate */ 2050Sstevel@tonic-gate 2060Sstevel@tonic-gate /* 2070Sstevel@tonic-gate * kstat type 2080Sstevel@tonic-gate * 2090Sstevel@tonic-gate * The kstat mechanism provides several flavors of kstat data, defined 2100Sstevel@tonic-gate * below. The "raw" kstat type is just treated as an array of bytes; you 2110Sstevel@tonic-gate * can use this to export any kind of data you want. 2120Sstevel@tonic-gate * 2130Sstevel@tonic-gate * Some kstat types allow multiple data structures per kstat, e.g. 2140Sstevel@tonic-gate * KSTAT_TYPE_NAMED; others do not. This is part of the spec for each 2150Sstevel@tonic-gate * kstat data type. 2160Sstevel@tonic-gate * 2170Sstevel@tonic-gate * User-level tools should *not* rely on the #define KSTAT_NUM_TYPES. To 2180Sstevel@tonic-gate * get this information, read out the standard system kstat "kstat_types". 2190Sstevel@tonic-gate */ 2200Sstevel@tonic-gate 2210Sstevel@tonic-gate #define KSTAT_TYPE_RAW 0 /* can be anything */ 2220Sstevel@tonic-gate /* ks_ndata >= 1 */ 2230Sstevel@tonic-gate #define KSTAT_TYPE_NAMED 1 /* name/value pair */ 2240Sstevel@tonic-gate /* ks_ndata >= 1 */ 2250Sstevel@tonic-gate #define KSTAT_TYPE_INTR 2 /* interrupt statistics */ 2260Sstevel@tonic-gate /* ks_ndata == 1 */ 2270Sstevel@tonic-gate #define KSTAT_TYPE_IO 3 /* I/O statistics */ 2280Sstevel@tonic-gate /* ks_ndata == 1 */ 2290Sstevel@tonic-gate #define KSTAT_TYPE_TIMER 4 /* event timer */ 2300Sstevel@tonic-gate /* ks_ndata >= 1 */ 2310Sstevel@tonic-gate 2320Sstevel@tonic-gate #define KSTAT_NUM_TYPES 5 2330Sstevel@tonic-gate 2340Sstevel@tonic-gate /* 2350Sstevel@tonic-gate * kstat class 2360Sstevel@tonic-gate * 2370Sstevel@tonic-gate * Each kstat can be characterized as belonging to some broad class 2380Sstevel@tonic-gate * of statistics, e.g. disk, tape, net, vm, streams, etc. This field 2390Sstevel@tonic-gate * can be used as a filter to extract related kstats. The following 2400Sstevel@tonic-gate * values are currently in use: disk, tape, net, controller, vm, kvm, 2410Sstevel@tonic-gate * hat, streams, kstat, and misc. (The kstat class encompasses things 2420Sstevel@tonic-gate * like kstat_types.) 2430Sstevel@tonic-gate */ 2440Sstevel@tonic-gate 2450Sstevel@tonic-gate /* 2460Sstevel@tonic-gate * kstat flags 2470Sstevel@tonic-gate * 2480Sstevel@tonic-gate * Any of the following flags may be passed to kstat_create(). They are 2490Sstevel@tonic-gate * all zero by default. 2500Sstevel@tonic-gate * 2510Sstevel@tonic-gate * KSTAT_FLAG_VIRTUAL: 2520Sstevel@tonic-gate * 2530Sstevel@tonic-gate * Tells kstat_create() not to allocate memory for the 2540Sstevel@tonic-gate * kstat data section; instead, you will set the ks_data 2550Sstevel@tonic-gate * field to point to the data you wish to export. This 2560Sstevel@tonic-gate * provides a convenient way to export existing data 2570Sstevel@tonic-gate * structures. 2580Sstevel@tonic-gate * 2590Sstevel@tonic-gate * KSTAT_FLAG_VAR_SIZE: 2600Sstevel@tonic-gate * 2610Sstevel@tonic-gate * The size of the kstat you are creating will vary over time. 2620Sstevel@tonic-gate * For example, you may want to use the kstat mechanism to 2630Sstevel@tonic-gate * export a linked list. NOTE: The kstat framework does not 2640Sstevel@tonic-gate * manage the data section, so all variable-size kstats must be 2650Sstevel@tonic-gate * virtual kstats. Moreover, variable-size kstats MUST employ 2660Sstevel@tonic-gate * kstat data locking to prevent data-size races with kstat 2670Sstevel@tonic-gate * clients. See the section on "kstat snapshot" for details. 2680Sstevel@tonic-gate * 2690Sstevel@tonic-gate * KSTAT_FLAG_WRITABLE: 2700Sstevel@tonic-gate * 2710Sstevel@tonic-gate * Makes the kstat's data section writable by root. 2720Sstevel@tonic-gate * The ks_snapshot routine (see below) does not need to check for 2730Sstevel@tonic-gate * this; permission checking is handled in the kstat driver. 2740Sstevel@tonic-gate * 2750Sstevel@tonic-gate * KSTAT_FLAG_PERSISTENT: 2760Sstevel@tonic-gate * 2770Sstevel@tonic-gate * Indicates that this kstat is to be persistent over time. 2780Sstevel@tonic-gate * For persistent kstats, kstat_delete() simply marks the 2790Sstevel@tonic-gate * kstat as dormant; a subsequent kstat_create() reactivates 2800Sstevel@tonic-gate * the kstat. This feature is provided so that statistics 2810Sstevel@tonic-gate * are not lost across driver close/open (e.g., raw disk I/O 2820Sstevel@tonic-gate * on a disk with no mounted partitions.) 2830Sstevel@tonic-gate * NOTE: Persistent kstats cannot be virtual, since ks_data 2840Sstevel@tonic-gate * points to garbage as soon as the driver goes away. 2850Sstevel@tonic-gate * 2860Sstevel@tonic-gate * The following flags are maintained by the kstat framework: 2870Sstevel@tonic-gate * 2880Sstevel@tonic-gate * KSTAT_FLAG_DORMANT: 2890Sstevel@tonic-gate * 2900Sstevel@tonic-gate * For persistent kstats, indicates that the kstat is in the 2910Sstevel@tonic-gate * dormant state (e.g., the corresponding device is closed). 2920Sstevel@tonic-gate * 2930Sstevel@tonic-gate * KSTAT_FLAG_INVALID: 2940Sstevel@tonic-gate * 2950Sstevel@tonic-gate * This flag is set when a kstat is in a transitional state, 2960Sstevel@tonic-gate * e.g. between kstat_create() and kstat_install(). 2970Sstevel@tonic-gate * kstat clients must not attempt to access the kstat's data 2980Sstevel@tonic-gate * if this flag is set. 2990Sstevel@tonic-gate */ 3000Sstevel@tonic-gate 3010Sstevel@tonic-gate #define KSTAT_FLAG_VIRTUAL 0x01 3020Sstevel@tonic-gate #define KSTAT_FLAG_VAR_SIZE 0x02 3030Sstevel@tonic-gate #define KSTAT_FLAG_WRITABLE 0x04 3040Sstevel@tonic-gate #define KSTAT_FLAG_PERSISTENT 0x08 3050Sstevel@tonic-gate #define KSTAT_FLAG_DORMANT 0x10 3060Sstevel@tonic-gate #define KSTAT_FLAG_INVALID 0x20 3070Sstevel@tonic-gate 3080Sstevel@tonic-gate /* 3090Sstevel@tonic-gate * Dynamic update support 3100Sstevel@tonic-gate * 3110Sstevel@tonic-gate * The kstat mechanism allows for an optional ks_update function to update 3120Sstevel@tonic-gate * kstat data. This is useful for drivers where the underlying device 3130Sstevel@tonic-gate * keeps cheap hardware stats, but extraction is expensive. Instead of 3140Sstevel@tonic-gate * constantly keeping the kstat data section up to date, you can supply a 3150Sstevel@tonic-gate * ks_update function which updates the kstat's data section on demand. 3160Sstevel@tonic-gate * To take advantage of this feature, simply set the ks_update field before 3170Sstevel@tonic-gate * calling kstat_install(). 3180Sstevel@tonic-gate * 3190Sstevel@tonic-gate * The ks_update function, if supplied, must have the following structure: 3200Sstevel@tonic-gate * 3210Sstevel@tonic-gate * int 3220Sstevel@tonic-gate * foo_kstat_update(kstat_t *ksp, int rw) 3230Sstevel@tonic-gate * { 3240Sstevel@tonic-gate * if (rw == KSTAT_WRITE) { 3250Sstevel@tonic-gate * ... update the native stats from ksp->ks_data; 3260Sstevel@tonic-gate * return EACCES if you don't support this 3270Sstevel@tonic-gate * } else { 3280Sstevel@tonic-gate * ... update ksp->ks_data from the native stats 3290Sstevel@tonic-gate * } 3300Sstevel@tonic-gate * } 3310Sstevel@tonic-gate * 3320Sstevel@tonic-gate * The ks_update return codes are: 0 for success, EACCES if you don't allow 3330Sstevel@tonic-gate * KSTAT_WRITE, and EIO for any other type of error. 3340Sstevel@tonic-gate * 3350Sstevel@tonic-gate * In general, the ks_update function may need to refer to provider-private 3360Sstevel@tonic-gate * data; for example, it may need a pointer to the provider's raw statistics. 3370Sstevel@tonic-gate * The ks_private field is available for this purpose. Its use is entirely 3380Sstevel@tonic-gate * at the provider's discretion. 3390Sstevel@tonic-gate * 3400Sstevel@tonic-gate * All variable-size kstats MUST supply a ks_update routine, which computes 3410Sstevel@tonic-gate * and sets ks_data_size (and ks_ndata if that is meaningful), since these 3420Sstevel@tonic-gate * are needed to perform kstat snapshots (see below). 3430Sstevel@tonic-gate * 3440Sstevel@tonic-gate * No kstat locking should be done inside the ks_update routine. The caller 3450Sstevel@tonic-gate * will already be holding the kstat's ks_lock (to ensure consistent data). 3460Sstevel@tonic-gate */ 3470Sstevel@tonic-gate 3480Sstevel@tonic-gate #define KSTAT_READ 0 3490Sstevel@tonic-gate #define KSTAT_WRITE 1 3500Sstevel@tonic-gate 3510Sstevel@tonic-gate /* 3520Sstevel@tonic-gate * Kstat snapshot 3530Sstevel@tonic-gate * 3540Sstevel@tonic-gate * In order to get a consistent view of a kstat's data, clients must obey 3550Sstevel@tonic-gate * the kstat's locking strategy. However, these clients may need to perform 3560Sstevel@tonic-gate * operations on the data which could cause a fault (e.g. copyout()), or 3570Sstevel@tonic-gate * operations which are simply expensive. Doing so could cause deadlock 3580Sstevel@tonic-gate * (e.g. if you're holding a disk's kstat lock which is ultimately required 3590Sstevel@tonic-gate * to resolve a copyout() fault), performance degradation (since the providers' 3600Sstevel@tonic-gate * activity is serialized at the kstat lock), device timing problems, etc. 3610Sstevel@tonic-gate * 3620Sstevel@tonic-gate * To avoid these problems, kstat data is provided via snapshots. Taking 3630Sstevel@tonic-gate * a snapshot is a simple process: allocate a wired-down kernel buffer, 3640Sstevel@tonic-gate * acquire the kstat's data lock, copy the data into the buffer ("take the 3650Sstevel@tonic-gate * snapshot"), and release the lock. This ensures that the kstat's data lock 3660Sstevel@tonic-gate * will be held as briefly as possible, and that no faults will occur while 3670Sstevel@tonic-gate * the lock is held. 3680Sstevel@tonic-gate * 3690Sstevel@tonic-gate * Normally, the snapshot is taken by default_kstat_snapshot(), which 3700Sstevel@tonic-gate * timestamps the data (sets ks_snaptime), copies it, and does a little 3710Sstevel@tonic-gate * massaging to deal with incomplete transactions on i/o kstats. However, 3720Sstevel@tonic-gate * this routine only works for kstats with contiguous data (the typical case). 3730Sstevel@tonic-gate * If you create a kstat whose data is, say, a linked list, you must provide 3740Sstevel@tonic-gate * your own ks_snapshot routine. The routine you supply must have the 3750Sstevel@tonic-gate * following prototype (replace "foo" with something appropriate): 3760Sstevel@tonic-gate * 3770Sstevel@tonic-gate * int foo_kstat_snapshot(kstat_t *ksp, void *buf, int rw); 3780Sstevel@tonic-gate * 3790Sstevel@tonic-gate * The minimal snapshot routine -- one which copies contiguous data that 3800Sstevel@tonic-gate * doesn't need any massaging -- would be this: 3810Sstevel@tonic-gate * 3820Sstevel@tonic-gate * ksp->ks_snaptime = gethrtime(); 3830Sstevel@tonic-gate * if (rw == KSTAT_WRITE) 3840Sstevel@tonic-gate * bcopy(buf, ksp->ks_data, ksp->ks_data_size); 3850Sstevel@tonic-gate * else 3860Sstevel@tonic-gate * bcopy(ksp->ks_data, buf, ksp->ks_data_size); 3870Sstevel@tonic-gate * return (0); 3880Sstevel@tonic-gate * 3890Sstevel@tonic-gate * A more illuminating example is taking a snapshot of a linked list: 3900Sstevel@tonic-gate * 3910Sstevel@tonic-gate * ksp->ks_snaptime = gethrtime(); 3920Sstevel@tonic-gate * if (rw == KSTAT_WRITE) 3930Sstevel@tonic-gate * return (EACCES); ... See below ... 3940Sstevel@tonic-gate * for (foo = first_foo; foo; foo = foo->next) { 3950Sstevel@tonic-gate * bcopy((char *) foo, (char *) buf, sizeof (struct foo)); 3960Sstevel@tonic-gate * buf = ((struct foo *) buf) + 1; 3970Sstevel@tonic-gate * } 3980Sstevel@tonic-gate * return (0); 3990Sstevel@tonic-gate * 4000Sstevel@tonic-gate * In the example above, we have decided that we don't want to allow 4010Sstevel@tonic-gate * KSTAT_WRITE access, so we return EACCES if this is attempted. 4020Sstevel@tonic-gate * 4030Sstevel@tonic-gate * The key points are: 4040Sstevel@tonic-gate * 4050Sstevel@tonic-gate * (1) ks_snaptime must be set (via gethrtime()) to timestamp the data. 4060Sstevel@tonic-gate * (2) Data gets copied from the kstat to the buffer on KSTAT_READ, 4070Sstevel@tonic-gate * and from the buffer to the kstat on KSTAT_WRITE. 4080Sstevel@tonic-gate * (3) ks_snapshot return values are: 0 for success, EACCES if you 4090Sstevel@tonic-gate * don't allow KSTAT_WRITE, and EIO for any other type of error. 4100Sstevel@tonic-gate * 4110Sstevel@tonic-gate * Named kstats (see section on "Named statistics" below) containing long 4120Sstevel@tonic-gate * strings (KSTAT_DATA_STRING) need special handling. The kstat driver 4130Sstevel@tonic-gate * assumes that all strings are copied into the buffer after the array of 4140Sstevel@tonic-gate * named kstats, and the pointers (KSTAT_NAMED_STR_PTR()) are updated to point 4150Sstevel@tonic-gate * into the copy within the buffer. The default snapshot routine does this, 4160Sstevel@tonic-gate * but overriding routines should contain at least the following: 4170Sstevel@tonic-gate * 4180Sstevel@tonic-gate * if (rw == KSTAT_READ) { 4190Sstevel@tonic-gate * kstat_named_t *knp = buf; 4200Sstevel@tonic-gate * char *end = knp + ksp->ks_ndata; 4210Sstevel@tonic-gate * uint_t i; 4220Sstevel@tonic-gate * 4230Sstevel@tonic-gate * ... Do the regular copy ... 4240Sstevel@tonic-gate * bcopy(ksp->ks_data, buf, sizeof (kstat_named_t) * ksp->ks_ndata); 4250Sstevel@tonic-gate * 4260Sstevel@tonic-gate * for (i = 0; i < ksp->ks_ndata; i++, knp++) { 4270Sstevel@tonic-gate * if (knp[i].data_type == KSTAT_DATA_STRING && 4280Sstevel@tonic-gate * KSTAT_NAMED_STR_PTR(knp) != NULL) { 4290Sstevel@tonic-gate * bcopy(KSTAT_NAMED_STR_PTR(knp), end, 4300Sstevel@tonic-gate * KSTAT_NAMED_STR_BUFLEN(knp)); 4310Sstevel@tonic-gate * KSTAT_NAMED_STR_PTR(knp) = end; 4320Sstevel@tonic-gate * end += KSTAT_NAMED_STR_BUFLEN(knp); 4330Sstevel@tonic-gate * } 4340Sstevel@tonic-gate * } 4350Sstevel@tonic-gate */ 4360Sstevel@tonic-gate 4370Sstevel@tonic-gate /* 4380Sstevel@tonic-gate * Named statistics. 4390Sstevel@tonic-gate * 4400Sstevel@tonic-gate * List of arbitrary name=value statistics. 4410Sstevel@tonic-gate */ 4420Sstevel@tonic-gate 4430Sstevel@tonic-gate typedef struct kstat_named { 4440Sstevel@tonic-gate char name[KSTAT_STRLEN]; /* name of counter */ 4450Sstevel@tonic-gate uchar_t data_type; /* data type */ 4460Sstevel@tonic-gate union { 4470Sstevel@tonic-gate char c[16]; /* enough for 128-bit ints */ 4480Sstevel@tonic-gate int32_t i32; 4490Sstevel@tonic-gate uint32_t ui32; 4500Sstevel@tonic-gate struct { 4510Sstevel@tonic-gate union { 4520Sstevel@tonic-gate char *ptr; /* NULL-term string */ 4530Sstevel@tonic-gate #if defined(_KERNEL) && defined(_MULTI_DATAMODEL) 4540Sstevel@tonic-gate caddr32_t ptr32; 4550Sstevel@tonic-gate #endif 4560Sstevel@tonic-gate char __pad[8]; /* 64-bit padding */ 4570Sstevel@tonic-gate } addr; 4580Sstevel@tonic-gate uint32_t len; /* # bytes for strlen + '\0' */ 459457Sbmc } str; 4600Sstevel@tonic-gate /* 4610Sstevel@tonic-gate * The int64_t and uint64_t types are not valid for a maximally conformant 4620Sstevel@tonic-gate * 32-bit compilation environment (cc -Xc) using compilers prior to the 4630Sstevel@tonic-gate * introduction of C99 conforming compiler (reference ISO/IEC 9899:1990). 4640Sstevel@tonic-gate * In these cases, the visibility of i64 and ui64 is only permitted for 4650Sstevel@tonic-gate * 64-bit compilation environments or 32-bit non-maximally conformant 4660Sstevel@tonic-gate * C89 or C90 ANSI C compilation environments (cc -Xt and cc -Xa). In the 4670Sstevel@tonic-gate * C99 ANSI C compilation environment, the long long type is supported. 4680Sstevel@tonic-gate * The _INT64_TYPE is defined by the implementation (see sys/int_types.h). 4690Sstevel@tonic-gate */ 4700Sstevel@tonic-gate #if defined(_INT64_TYPE) 4710Sstevel@tonic-gate int64_t i64; 4720Sstevel@tonic-gate uint64_t ui64; 4730Sstevel@tonic-gate #endif 4740Sstevel@tonic-gate long l; 4750Sstevel@tonic-gate ulong_t ul; 4760Sstevel@tonic-gate 4770Sstevel@tonic-gate /* These structure members are obsolete */ 4780Sstevel@tonic-gate 4790Sstevel@tonic-gate longlong_t ll; 4800Sstevel@tonic-gate u_longlong_t ull; 4810Sstevel@tonic-gate float f; 4820Sstevel@tonic-gate double d; 4830Sstevel@tonic-gate } value; /* value of counter */ 4840Sstevel@tonic-gate } kstat_named_t; 4850Sstevel@tonic-gate 4860Sstevel@tonic-gate #define KSTAT_DATA_CHAR 0 4870Sstevel@tonic-gate #define KSTAT_DATA_INT32 1 4880Sstevel@tonic-gate #define KSTAT_DATA_UINT32 2 4890Sstevel@tonic-gate #define KSTAT_DATA_INT64 3 4900Sstevel@tonic-gate #define KSTAT_DATA_UINT64 4 4910Sstevel@tonic-gate 4920Sstevel@tonic-gate #if !defined(_LP64) 4930Sstevel@tonic-gate #define KSTAT_DATA_LONG KSTAT_DATA_INT32 4940Sstevel@tonic-gate #define KSTAT_DATA_ULONG KSTAT_DATA_UINT32 4950Sstevel@tonic-gate #else 4960Sstevel@tonic-gate #if !defined(_KERNEL) 4970Sstevel@tonic-gate #define KSTAT_DATA_LONG KSTAT_DATA_INT64 4980Sstevel@tonic-gate #define KSTAT_DATA_ULONG KSTAT_DATA_UINT64 4990Sstevel@tonic-gate #else 5000Sstevel@tonic-gate #define KSTAT_DATA_LONG 7 /* only visible to the kernel */ 5010Sstevel@tonic-gate #define KSTAT_DATA_ULONG 8 /* only visible to the kernel */ 5020Sstevel@tonic-gate #endif /* !_KERNEL */ 5030Sstevel@tonic-gate #endif /* !_LP64 */ 5040Sstevel@tonic-gate 5050Sstevel@tonic-gate /* 5060Sstevel@tonic-gate * Statistics exporting named kstats with long strings (KSTAT_DATA_STRING) 5070Sstevel@tonic-gate * may not make the assumption that ks_data_size is equal to (ks_ndata * sizeof 5080Sstevel@tonic-gate * (kstat_named_t)). ks_data_size in these cases is equal to the sum of the 5090Sstevel@tonic-gate * amount of space required to store the strings (ie, the sum of 5100Sstevel@tonic-gate * KSTAT_NAMED_STR_BUFLEN() for all KSTAT_DATA_STRING statistics) plus the 5110Sstevel@tonic-gate * space required to store the kstat_named_t's. 5120Sstevel@tonic-gate * 5130Sstevel@tonic-gate * The default update routine will update ks_data_size automatically for 5140Sstevel@tonic-gate * variable-length kstats containing long strings (using the default update 5150Sstevel@tonic-gate * routine only makes sense if the string is the only thing that is changing 5160Sstevel@tonic-gate * in size, and ks_ndata is constant). Fixed-length kstats containing long 5170Sstevel@tonic-gate * strings must explicitly change ks_data_size (after creation but before 5180Sstevel@tonic-gate * initialization) to reflect the correct amount of space required for the 5190Sstevel@tonic-gate * long strings and the kstat_named_t's. 5200Sstevel@tonic-gate */ 5210Sstevel@tonic-gate #define KSTAT_DATA_STRING 9 5220Sstevel@tonic-gate 5230Sstevel@tonic-gate /* These types are obsolete */ 5240Sstevel@tonic-gate 5250Sstevel@tonic-gate #define KSTAT_DATA_LONGLONG KSTAT_DATA_INT64 5260Sstevel@tonic-gate #define KSTAT_DATA_ULONGLONG KSTAT_DATA_UINT64 5270Sstevel@tonic-gate #define KSTAT_DATA_FLOAT 5 5280Sstevel@tonic-gate #define KSTAT_DATA_DOUBLE 6 5290Sstevel@tonic-gate 5300Sstevel@tonic-gate #define KSTAT_NAMED_PTR(kptr) ((kstat_named_t *)(kptr)->ks_data) 5310Sstevel@tonic-gate 5320Sstevel@tonic-gate /* 5330Sstevel@tonic-gate * Retrieve the pointer of the string contained in the given named kstat. 5340Sstevel@tonic-gate */ 535457Sbmc #define KSTAT_NAMED_STR_PTR(knptr) ((knptr)->value.str.addr.ptr) 5360Sstevel@tonic-gate 5370Sstevel@tonic-gate /* 5380Sstevel@tonic-gate * Retrieve the length of the buffer required to store the string in the given 5390Sstevel@tonic-gate * named kstat. 5400Sstevel@tonic-gate */ 541457Sbmc #define KSTAT_NAMED_STR_BUFLEN(knptr) ((knptr)->value.str.len) 5420Sstevel@tonic-gate 5430Sstevel@tonic-gate /* 5440Sstevel@tonic-gate * Interrupt statistics. 5450Sstevel@tonic-gate * 5460Sstevel@tonic-gate * An interrupt is a hard interrupt (sourced from the hardware device 5470Sstevel@tonic-gate * itself), a soft interrupt (induced by the system via the use of 5480Sstevel@tonic-gate * some system interrupt source), a watchdog interrupt (induced by 5490Sstevel@tonic-gate * a periodic timer call), spurious (an interrupt entry point was 5500Sstevel@tonic-gate * entered but there was no interrupt condition to service), 5510Sstevel@tonic-gate * or multiple service (an interrupt condition was detected and 5520Sstevel@tonic-gate * serviced just prior to returning from any of the other types). 5530Sstevel@tonic-gate * 5540Sstevel@tonic-gate * Measurement of the spurious class of interrupts is useful for 5550Sstevel@tonic-gate * autovectored devices in order to pinpoint any interrupt latency 5560Sstevel@tonic-gate * problems in a particular system configuration. 5570Sstevel@tonic-gate * 5580Sstevel@tonic-gate * Devices that have more than one interrupt of the same 5590Sstevel@tonic-gate * type should use multiple structures. 5600Sstevel@tonic-gate */ 5610Sstevel@tonic-gate 5620Sstevel@tonic-gate #define KSTAT_INTR_HARD 0 5630Sstevel@tonic-gate #define KSTAT_INTR_SOFT 1 5640Sstevel@tonic-gate #define KSTAT_INTR_WATCHDOG 2 5650Sstevel@tonic-gate #define KSTAT_INTR_SPURIOUS 3 5660Sstevel@tonic-gate #define KSTAT_INTR_MULTSVC 4 5670Sstevel@tonic-gate 5680Sstevel@tonic-gate #define KSTAT_NUM_INTRS 5 5690Sstevel@tonic-gate 5700Sstevel@tonic-gate typedef struct kstat_intr { 5710Sstevel@tonic-gate uint_t intrs[KSTAT_NUM_INTRS]; /* interrupt counters */ 5720Sstevel@tonic-gate } kstat_intr_t; 5730Sstevel@tonic-gate 5740Sstevel@tonic-gate #define KSTAT_INTR_PTR(kptr) ((kstat_intr_t *)(kptr)->ks_data) 5750Sstevel@tonic-gate 5760Sstevel@tonic-gate /* 5770Sstevel@tonic-gate * I/O statistics. 5780Sstevel@tonic-gate */ 5790Sstevel@tonic-gate 5800Sstevel@tonic-gate typedef struct kstat_io { 5810Sstevel@tonic-gate 5820Sstevel@tonic-gate /* 5830Sstevel@tonic-gate * Basic counters. 5840Sstevel@tonic-gate * 5850Sstevel@tonic-gate * The counters should be updated at the end of service 5860Sstevel@tonic-gate * (e.g., just prior to calling biodone()). 5870Sstevel@tonic-gate */ 5880Sstevel@tonic-gate 5890Sstevel@tonic-gate u_longlong_t nread; /* number of bytes read */ 5900Sstevel@tonic-gate u_longlong_t nwritten; /* number of bytes written */ 5910Sstevel@tonic-gate uint_t reads; /* number of read operations */ 5920Sstevel@tonic-gate uint_t writes; /* number of write operations */ 5930Sstevel@tonic-gate 5940Sstevel@tonic-gate /* 5950Sstevel@tonic-gate * Accumulated time and queue length statistics. 5960Sstevel@tonic-gate * 5970Sstevel@tonic-gate * Accumulated time statistics are kept as a running sum 5980Sstevel@tonic-gate * of "active" time. Queue length statistics are kept as a 5990Sstevel@tonic-gate * running sum of the product of queue length and elapsed time 6000Sstevel@tonic-gate * at that length -- i.e., a Riemann sum for queue length 6010Sstevel@tonic-gate * integrated against time. (You can also think of the active time 6020Sstevel@tonic-gate * as a Riemann sum, for the boolean function (queue_length > 0) 6030Sstevel@tonic-gate * integrated against time, or you can think of it as the 6040Sstevel@tonic-gate * Lebesgue measure of the set on which queue_length > 0.) 6050Sstevel@tonic-gate * 6060Sstevel@tonic-gate * ^ 6070Sstevel@tonic-gate * | _________ 6080Sstevel@tonic-gate * 8 | i4 | 6090Sstevel@tonic-gate * | | | 6100Sstevel@tonic-gate * Queue 6 | | 6110Sstevel@tonic-gate * Length | _________ | | 6120Sstevel@tonic-gate * 4 | i2 |_______| | 6130Sstevel@tonic-gate * | | i3 | 6140Sstevel@tonic-gate * 2_______| | 6150Sstevel@tonic-gate * | i1 | 6160Sstevel@tonic-gate * |_______________________________| 6170Sstevel@tonic-gate * Time-> t1 t2 t3 t4 6180Sstevel@tonic-gate * 6190Sstevel@tonic-gate * At each change of state (entry or exit from the queue), 6200Sstevel@tonic-gate * we add the elapsed time (since the previous state change) 6210Sstevel@tonic-gate * to the active time if the queue length was non-zero during 6220Sstevel@tonic-gate * that interval; and we add the product of the elapsed time 6230Sstevel@tonic-gate * times the queue length to the running length*time sum. 6240Sstevel@tonic-gate * 6250Sstevel@tonic-gate * This method is generalizable to measuring residency 6260Sstevel@tonic-gate * in any defined system: instead of queue lengths, think 6270Sstevel@tonic-gate * of "outstanding RPC calls to server X". 6280Sstevel@tonic-gate * 6290Sstevel@tonic-gate * A large number of I/O subsystems have at least two basic 6300Sstevel@tonic-gate * "lists" of transactions they manage: one for transactions 6310Sstevel@tonic-gate * that have been accepted for processing but for which processing 6320Sstevel@tonic-gate * has yet to begin, and one for transactions which are actively 6330Sstevel@tonic-gate * being processed (but not done). For this reason, two cumulative 6340Sstevel@tonic-gate * time statistics are defined here: wait (pre-service) time, 6350Sstevel@tonic-gate * and run (service) time. 6360Sstevel@tonic-gate * 6370Sstevel@tonic-gate * All times are 64-bit nanoseconds (hrtime_t), as returned by 6380Sstevel@tonic-gate * gethrtime(). 6390Sstevel@tonic-gate * 6400Sstevel@tonic-gate * The units of cumulative busy time are accumulated nanoseconds. 6410Sstevel@tonic-gate * The units of cumulative length*time products are elapsed time 6420Sstevel@tonic-gate * times queue length. 6430Sstevel@tonic-gate * 6440Sstevel@tonic-gate * Updates to the fields below are performed implicitly by calls to 6450Sstevel@tonic-gate * these five functions: 6460Sstevel@tonic-gate * 6470Sstevel@tonic-gate * kstat_waitq_enter() 6480Sstevel@tonic-gate * kstat_waitq_exit() 6490Sstevel@tonic-gate * kstat_runq_enter() 6500Sstevel@tonic-gate * kstat_runq_exit() 6510Sstevel@tonic-gate * 6520Sstevel@tonic-gate * kstat_waitq_to_runq() (see below) 6530Sstevel@tonic-gate * kstat_runq_back_to_waitq() (see below) 6540Sstevel@tonic-gate * 6550Sstevel@tonic-gate * Since kstat_waitq_exit() is typically followed immediately 6560Sstevel@tonic-gate * by kstat_runq_enter(), there is a single kstat_waitq_to_runq() 6570Sstevel@tonic-gate * function which performs both operations. This is a performance 6580Sstevel@tonic-gate * win since only one timestamp is required. 6590Sstevel@tonic-gate * 6600Sstevel@tonic-gate * In some instances, it may be necessary to move a request from 6610Sstevel@tonic-gate * the run queue back to the wait queue, e.g. for write throttling. 6620Sstevel@tonic-gate * For these situations, call kstat_runq_back_to_waitq(). 6630Sstevel@tonic-gate * 6640Sstevel@tonic-gate * These fields should never be updated by any other means. 6650Sstevel@tonic-gate */ 6660Sstevel@tonic-gate 6670Sstevel@tonic-gate hrtime_t wtime; /* cumulative wait (pre-service) time */ 6680Sstevel@tonic-gate hrtime_t wlentime; /* cumulative wait length*time product */ 6690Sstevel@tonic-gate hrtime_t wlastupdate; /* last time wait queue changed */ 6700Sstevel@tonic-gate hrtime_t rtime; /* cumulative run (service) time */ 6710Sstevel@tonic-gate hrtime_t rlentime; /* cumulative run length*time product */ 6720Sstevel@tonic-gate hrtime_t rlastupdate; /* last time run queue changed */ 6730Sstevel@tonic-gate 6740Sstevel@tonic-gate uint_t wcnt; /* count of elements in wait state */ 6750Sstevel@tonic-gate uint_t rcnt; /* count of elements in run state */ 6760Sstevel@tonic-gate 6770Sstevel@tonic-gate } kstat_io_t; 6780Sstevel@tonic-gate 6790Sstevel@tonic-gate #define KSTAT_IO_PTR(kptr) ((kstat_io_t *)(kptr)->ks_data) 6800Sstevel@tonic-gate 6810Sstevel@tonic-gate /* 6820Sstevel@tonic-gate * Event timer statistics - cumulative elapsed time and number of events. 6830Sstevel@tonic-gate * 6840Sstevel@tonic-gate * Updates to these fields are performed implicitly by calls to 6850Sstevel@tonic-gate * kstat_timer_start() and kstat_timer_stop(). 6860Sstevel@tonic-gate */ 6870Sstevel@tonic-gate 6880Sstevel@tonic-gate typedef struct kstat_timer { 6890Sstevel@tonic-gate char name[KSTAT_STRLEN]; /* event name */ 6900Sstevel@tonic-gate uchar_t resv; /* reserved */ 6910Sstevel@tonic-gate u_longlong_t num_events; /* number of events */ 6920Sstevel@tonic-gate hrtime_t elapsed_time; /* cumulative elapsed time */ 6930Sstevel@tonic-gate hrtime_t min_time; /* shortest event duration */ 6940Sstevel@tonic-gate hrtime_t max_time; /* longest event duration */ 6950Sstevel@tonic-gate hrtime_t start_time; /* previous event start time */ 6960Sstevel@tonic-gate hrtime_t stop_time; /* previous event stop time */ 6970Sstevel@tonic-gate } kstat_timer_t; 6980Sstevel@tonic-gate 6990Sstevel@tonic-gate #define KSTAT_TIMER_PTR(kptr) ((kstat_timer_t *)(kptr)->ks_data) 7000Sstevel@tonic-gate 7010Sstevel@tonic-gate #if defined(_KERNEL) 7020Sstevel@tonic-gate 7030Sstevel@tonic-gate #include <sys/t_lock.h> 7040Sstevel@tonic-gate 7050Sstevel@tonic-gate extern kid_t kstat_chain_id; /* bumped at each state change */ 7060Sstevel@tonic-gate extern void kstat_init(void); /* initialize kstat framework */ 7070Sstevel@tonic-gate 7080Sstevel@tonic-gate /* 7090Sstevel@tonic-gate * Adding and deleting kstats. 7100Sstevel@tonic-gate * 7110Sstevel@tonic-gate * The typical sequence to add a kstat is: 7120Sstevel@tonic-gate * 7130Sstevel@tonic-gate * ksp = kstat_create(module, instance, name, class, type, ndata, flags); 7140Sstevel@tonic-gate * if (ksp) { 7150Sstevel@tonic-gate * ... provider initialization, if necessary 7160Sstevel@tonic-gate * kstat_install(ksp); 7170Sstevel@tonic-gate * } 7180Sstevel@tonic-gate * 7190Sstevel@tonic-gate * There are three logically distinct steps here: 7200Sstevel@tonic-gate * 7210Sstevel@tonic-gate * Step 1: System Initialization (kstat_create) 7220Sstevel@tonic-gate * 7230Sstevel@tonic-gate * kstat_create() performs system initialization. kstat_create() 7240Sstevel@tonic-gate * allocates memory for the entire kstat (header plus data), initializes 7250Sstevel@tonic-gate * all header fields, initializes the data section to all zeroes, assigns 7260Sstevel@tonic-gate * a unique KID, and puts the kstat onto the system's kstat chain. 7270Sstevel@tonic-gate * The returned kstat is marked invalid (KSTAT_FLAG_INVALID is set), 7280Sstevel@tonic-gate * because the provider (caller) has not yet had a chance to initialize 7290Sstevel@tonic-gate * the data section. 7300Sstevel@tonic-gate * 7310Sstevel@tonic-gate * By default, kstats are exported to all zones on the system. A kstat may be 7320Sstevel@tonic-gate * created via kstat_create_zone() to specify a zone to which the statistics 7330Sstevel@tonic-gate * should be exported. kstat_zone_add() may be used to specify additional 7340Sstevel@tonic-gate * zones to which the statistics are to be exported. 7350Sstevel@tonic-gate * 7360Sstevel@tonic-gate * Step 2: Provider Initialization 7370Sstevel@tonic-gate * 7380Sstevel@tonic-gate * The provider performs any necessary initialization of the data section, 7390Sstevel@tonic-gate * e.g. setting the name fields in a KSTAT_TYPE_NAMED. Virtual kstats set 7400Sstevel@tonic-gate * the ks_data field at this time. The provider may also set the ks_update, 7410Sstevel@tonic-gate * ks_snapshot, ks_private, and ks_lock fields if necessary. 7420Sstevel@tonic-gate * 7430Sstevel@tonic-gate * Step 3: Installation (kstat_install) 7440Sstevel@tonic-gate * 7450Sstevel@tonic-gate * Once the kstat is completely initialized, kstat_install() clears the 7460Sstevel@tonic-gate * INVALID flag, thus making the kstat accessible to the outside world. 7470Sstevel@tonic-gate * kstat_install() also clears the DORMANT flag for persistent kstats. 7480Sstevel@tonic-gate * 7490Sstevel@tonic-gate * Removing a kstat from the system 7500Sstevel@tonic-gate * 7510Sstevel@tonic-gate * kstat_delete(ksp) removes ksp from the kstat chain and frees all 7520Sstevel@tonic-gate * associated system resources. NOTE: When you call kstat_delete(), 7530Sstevel@tonic-gate * you must NOT be holding that kstat's ks_lock. Otherwise, you may 7540Sstevel@tonic-gate * deadlock with a kstat reader. 7550Sstevel@tonic-gate * 7560Sstevel@tonic-gate * Persistent kstats 7570Sstevel@tonic-gate * 7580Sstevel@tonic-gate * From the provider's point of view, persistence is transparent. The only 7590Sstevel@tonic-gate * difference between ephemeral (normal) kstats and persistent kstats 7600Sstevel@tonic-gate * is that you pass KSTAT_FLAG_PERSISTENT to kstat_create(). Magically, 7610Sstevel@tonic-gate * this has the effect of making your data visible even when you're 7620Sstevel@tonic-gate * not home. Persistence is important to tools like iostat, which want 7630Sstevel@tonic-gate * to get a meaningful picture of disk activity. Without persistence, 7640Sstevel@tonic-gate * raw disk i/o statistics could never accumulate: they would come and 7650Sstevel@tonic-gate * go with each open/close of the raw device. 7660Sstevel@tonic-gate * 7670Sstevel@tonic-gate * The magic of persistence works by slightly altering the behavior of 7680Sstevel@tonic-gate * kstat_create() and kstat_delete(). The first call to kstat_create() 7690Sstevel@tonic-gate * creates a new kstat, as usual. However, kstat_delete() does not 7700Sstevel@tonic-gate * actually delete the kstat: it performs one final update of the data 7710Sstevel@tonic-gate * (i.e., calls the ks_update routine), marks the kstat as dormant, and 7720Sstevel@tonic-gate * sets the ks_lock, ks_update, ks_private, and ks_snapshot fields back 7730Sstevel@tonic-gate * to their default values (since they might otherwise point to garbage, 7740Sstevel@tonic-gate * e.g. if the provider is going away). kstat clients can still access 7750Sstevel@tonic-gate * the dormant kstat just like a live kstat; they just continue to see 7760Sstevel@tonic-gate * the final data values as long as the kstat remains dormant. 7770Sstevel@tonic-gate * All subsequent kstat_create() calls simply find the already-existing, 7780Sstevel@tonic-gate * dormant kstat and return a pointer to it, without altering any fields. 7790Sstevel@tonic-gate * The provider then performs its usual initialization sequence, and 7800Sstevel@tonic-gate * calls kstat_install(). kstat_install() uses the old data values to 7810Sstevel@tonic-gate * initialize the native data (i.e., ks_update is called with KSTAT_WRITE), 7820Sstevel@tonic-gate * thus making it seem like you were never gone. 7830Sstevel@tonic-gate */ 7840Sstevel@tonic-gate 785*2951Selowe extern kstat_t *kstat_create(const char *, int, const char *, const char *, 786*2951Selowe uchar_t, uint_t, uchar_t); 787*2951Selowe extern kstat_t *kstat_create_zone(const char *, int, const char *, 788*2951Selowe const char *, uchar_t, uint_t, uchar_t, zoneid_t); 7890Sstevel@tonic-gate extern void kstat_install(kstat_t *); 7900Sstevel@tonic-gate extern void kstat_delete(kstat_t *); 7910Sstevel@tonic-gate extern void kstat_named_setstr(kstat_named_t *knp, const char *src); 792*2951Selowe extern void kstat_set_string(char *, const char *); 793*2951Selowe extern void kstat_delete_byname(const char *, int, const char *); 794*2951Selowe extern void kstat_delete_byname_zone(const char *, int, const char *, zoneid_t); 795*2951Selowe extern void kstat_named_init(kstat_named_t *, const char *, uchar_t); 796*2951Selowe extern void kstat_timer_init(kstat_timer_t *, const char *); 7970Sstevel@tonic-gate extern void kstat_waitq_enter(kstat_io_t *); 7980Sstevel@tonic-gate extern void kstat_waitq_exit(kstat_io_t *); 7990Sstevel@tonic-gate extern void kstat_runq_enter(kstat_io_t *); 8000Sstevel@tonic-gate extern void kstat_runq_exit(kstat_io_t *); 8010Sstevel@tonic-gate extern void kstat_waitq_to_runq(kstat_io_t *); 8020Sstevel@tonic-gate extern void kstat_runq_back_to_waitq(kstat_io_t *); 8030Sstevel@tonic-gate extern void kstat_timer_start(kstat_timer_t *); 8040Sstevel@tonic-gate extern void kstat_timer_stop(kstat_timer_t *); 8050Sstevel@tonic-gate 8060Sstevel@tonic-gate extern void kstat_zone_add(kstat_t *, zoneid_t); 8070Sstevel@tonic-gate extern void kstat_zone_remove(kstat_t *, zoneid_t); 8080Sstevel@tonic-gate extern int kstat_zone_find(kstat_t *, zoneid_t); 8090Sstevel@tonic-gate 8100Sstevel@tonic-gate extern kstat_t *kstat_hold_bykid(kid_t kid, zoneid_t); 811*2951Selowe extern kstat_t *kstat_hold_byname(const char *, int, const char *, zoneid_t); 8120Sstevel@tonic-gate extern void kstat_rele(kstat_t *); 8130Sstevel@tonic-gate 8140Sstevel@tonic-gate #endif /* defined(_KERNEL) */ 8150Sstevel@tonic-gate 8160Sstevel@tonic-gate #ifdef __cplusplus 8170Sstevel@tonic-gate } 8180Sstevel@tonic-gate #endif 8190Sstevel@tonic-gate 8200Sstevel@tonic-gate #endif /* _SYS_KSTAT_H */ 821