2 .\" Copyright (c) 2007, Sun Microsystems, Inc. All Rights Reserved.
3 .\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License"). You may not use this file except in compliance with the License.
4 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing. See the License for the specific language governing permissions and limitations under the License.
5 .\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE. If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
6 .TH KSTAT 3KSTAT "Jan 29, 2007"
8 kstat \- kernel statistics facility
12 The \fBkstat\fR facility is a general-purpose mechanism for providing kernel
17 The kernel maintains a linked list of statistics structures, or kstats. Each
18 kstat has a common header section and a type-specific data section. The header
19 section is defined by the \fBkstat_t\fR structure:
24 typedef int kid_t; /* unique kstat id */
26 typedef struct kstat {
28 * Fields relevant to both kernel and user
30 hrtime_t ks_crtime; /* creation time */
31 struct kstat *ks_next; /* kstat chain linkage */
32 kid_t ks_kid; /* unique kstat ID */
33 char ks_module[KSTAT_STRLEN]; /* module name */
34 uchar_t ks_resv; /* reserved */
35 int ks_instance; /* module's instance */
36 char ks_name[KSTAT_STRLEN]; /* kstat name */
37 uchar_t ks_type; /* kstat data type */
38 char ks_class[KSTAT_STRLEN]; /* kstat class */
39 uchar_t ks_flags; /* kstat flags */
40 void *ks_data; /* kstat type-specific
42 uint_t ks_ndata; /* # of data records */
43 size_t ks_data_size; /* size of kstat data
45 hrtime_t ks_snaptime; /* time of last data
49 * Fields relevant to kernel only
51 int(*ks_update)(struct kstat *, int);
53 int(*ks_snapshot)(struct kstat *, void *, int);
61 The fields that are of significance to the user are:
68 The time the kstat was created. This allows you to compute the rates of various
69 counters since the kstat was created; "rate since boot" is replaced by the more
70 general concept of "rate since kstat creation". All times associated with
71 kstats (such as creation time, last snapshot time, \fBkstat_timer_t\fR and
72 \fBkstat_io_t\fR timestamps, and the like) are 64-bit nanosecond values. The
73 accuracy of kstat timestamps is machine dependent, but the precision (units) is
74 the same across all platforms. See \fBgethrtime\fR(3C) for general information
75 about high-resolution timestamps.
84 kstats are stored as a linked list, or chain. \fBks_next\fR points to the next
94 A unique identifier for the kstat.
100 \fB\fBks_module\fR,\fR
104 \fB\fBks_instance\fR\fR
107 contain the name and instance of the module that created the kstat. In cases
108 where there can only be one instance, \fBks_instance\fR is 0.
117 gives a meaningful name to a kstat. The full kstat namespace is
118 <\fBks_module\fR,\fBks_instance\fR,\fBks_name\fR>, so the name only need be
119 unique within a module.
128 The type of data in this kstat. kstat data types are discussed below.
137 Each kstat can be characterized as belonging to some broad class of statistics,
138 such as disk, tape, net, vm, and streams. This field can be used as a filter to
139 extract related kstats. The following values are currently in use: \fBdisk\fR,
140 \fBtape\fR, \fBcontroller\fR, \fBnet\fR, \fBrpc\fR, \fBvm\fR, \fBkvm\fR,
141 \fBhat\fR, \fBstreams\fR, \fBkmem\fR, \fBkmem_cache\fR, \fBkstat\fR, and
142 \fBmisc\fR. (The kstat class encompasses things like \fIkstat_types\fR.)
152 \fB\fBks_ndata\fR,\fR
156 \fB\fBks_data_size\fR\fR
159 \fBks_data\fR is a pointer to the kstat's data section. The type of data stored
160 there depends on \fBks_type\fR. \fBks_ndata\fR indicates the number of data
161 records. Only some kstat types support multiple data records. Currently,
162 \fBKSTAT_TYPE_RAW\fR, \fBKSTAT_TYPE_NAMED\fR and \fBKSTAT_TYPE_TIMER\fR kstats
163 support multiple data records. \fBKSTAT_TYPE_INTR\fR and \fBKSTAT_TYPE_IO\fR
164 kstats support only one data record. \fBks_data_size\fR is the total size of
165 the data section, in bytes.
171 \fB\fBks_snaptime\fR\fR
174 The timestamp for the last data snapshot. This allows you to compute activity
177 \fBrate = (new_count - old_count) / (new_snaptime - old_snaptime);\fR
180 .SS "kstat data types"
183 The following types of kstats are currently available:
187 #define KSTAT_TYPE_RAW 0 /* can be anything */
188 #define KSTAT_TYPE_NAMED 1 /* name/value pairs */
189 #define KSTAT_TYPE_INTR 2 /* interrupt statistics */
190 #define KSTAT_TYPE_IO 3 /* I/O statistics */
191 #define KSTAT_TYPE_TIMER 4 /* event timers */
197 To get a list of all kstat types currently supported in the system, tools can
198 read out the standard system kstat \fIkstat_types\fR (full name spec is
199 \fI<``unix'', 0, ``kstat_types''>\fR). This is a \fBKSTAT_TYPE_NAMED\fR kstat
200 in which the \fIname\fR field describes the type of kstat, and the \fIvalue\fR
201 field is the kstat type number (for example, \fBKSTAT_TYPE_IO\fR is type 3 --
207 \fB\fBKSTAT_TYPE_RAW\fR\fR
215 The "raw" kstat type is just treated as an array of bytes. This is generally
216 used to export well-known structures, like \fIsysinfo\fR.
217 .SS "Name=value kstat"
221 \fB\fBKSTAT_TYPE_NAMED\fR\fR
224 A list of arbitrary \fIname=value\fR statistics.
230 typedef struct kstat_named {
231 char name[KSTAT_STRLEN]; /* name of counter */
232 uchar_t data_type; /* data type */
234 charc[16]; /* enough for 128-bit ints */
237 char *ptr; /* NULL-terminated string */
239 uint32_t len; /* length of string */
246 /* These structure members are obsolete */
252 } value; /* value of counter */
255 /* The following types are Stable
265 /* The following type is Evolving */
269 /* The following types are Obsolete */
280 Some devices need to publish strings that exceed the maximum value for
281 \fBKSTAT_DATA_CHAR\fR in length; \fBKSTAT_DATA_STRING\fR is a data type that
282 allows arbitrary-length strings to be associated with a named kstat. The macros
283 below are the supported means to read the pointer to the string and its length.
287 #define KSTAT_NAMED_STR_PTR(knptr) ((knptr)->value.str.addr.ptr)
288 #define KSTAT_NAMED_STR_BUFLEN(knptr) ((knptr)->value.str.len)
295 \fBKSTAT_NAMED_STR_BUFLEN()\fR returns the number of bytes required to store
296 the string pointed to by \fBKSTAT_NAMED_STR_PTR()\fR; that is,
297 \fBstrlen(KSTAT_NAMED_STR_PTR()) + 1\fR.
298 .SS "Interrupt kstat"
302 \fB\fBKSTAT_TYPE_INTR\fR\fR
305 Interrupt statistics.
310 An interrupt is a hard interrupt (sourced from the hardware device itself), a
311 soft interrupt (induced by the system via the use of some system interrupt
312 source), a watchdog interrupt (induced by a periodic timer call), spurious (an
313 interrupt entry point was entered but there was no interrupt to service), or
314 multiple service (an interrupt was detected and serviced just prior to
315 returning from any of the other types).
319 #define KSTAT_INTR_HARD 0
320 #define KSTAT_INTR_SOFT 1
321 #define KSTAT_INTR_WATCHDOG 2
322 #define KSTAT_INTR_SPURIOUS 3
323 #define KSTAT_INTR_MULTSVC 4
324 #define KSTAT_NUM_INTRS 5
326 typedef struct kstat_intr {
327 uint_t intrs[KSTAT_NUM_INTRS]; /* interrupt counters */
332 .SS "Event timer kstat"
336 \fB\fBKSTAT_TYPE_TIMER\fR\fR
339 Event timer statistics.
344 These provide basic counting and timing information for any type of event.
348 typedef struct kstat_timer {
349 char name[KSTAT_STRLEN]; /* event name */
350 uchar_t resv; /* reserved */
351 u_longlong_t num_events; /* number of events */
352 hrtime_t elapsed_time; /* cumulative elapsed time */
353 hrtime_t min_time; /* shortest event duration */
354 hrtime_t max_time; /* longest event duration */
355 hrtime_t start_time; /* previous event start time */
356 hrtime_t stop_time; /* previous event stop time */
365 \fB\fBKSTAT_TYPE_IO\fR\fR
374 typedef struct kstat_io {
378 u_longlong_t nread; /* number of bytes read */
379 u_longlong_t nwritten; /* number of bytes written */
380 uint_t reads; /* number of read operations */
381 uint_t writes; /* number of write operations */
383 * Accumulated time and queue length statistics.
385 * Time statistics are kept as a running sum of "active" time.
386 * Queue length statistics are kept as a running sum of the
387 * product of queue length and elapsed time at that length --
388 * that is, a Riemann sum for queue length integrated against time.
395 * Length | _________ | |
400 * |_______________________________|
403 * At each change of state (entry or exit from the queue),
404 * we add the elapsed time (since the previous state change)
405 * to the active time if the queue length was non-zero during
406 * that interval; and we add the product of the elapsed time
407 * times the queue length to the running length*time sum.
409 * This method is generalizable to measuring residency
410 * in any defined system: instead of queue lengths, think
411 * of "outstanding RPC calls to server X".
413 * A large number of I/O subsystems have at least two basic
414 * "lists" of transactions they manage: one for transactions
415 * that have been accepted for processing but for which processing
416 * has yet to begin, and one for transactions which are actively
417 * being processed (but not done). For this reason, two cumulative
418 * time statistics are defined here: pre-service (wait) time,
419 * and service (run) time.
421 * The units of cumulative busy time are accumulated nanoseconds.
422 * The units of cumulative length*time products are elapsed time
423 * times queue length.
425 hrtime_t wtime; /* cumulative wait (pre-service) time */
426 hrtime_t wlentime; /* cumulative wait length*time product*/
427 hrtime_t wlastupdate; /* last time wait queue changed */
428 hrtime_t rtime; /* cumulative run (service) time */
429 hrtime_t rlentime; /* cumulative run length*time product */
430 hrtime_t rlastupdate; /* last time run queue changed */
431 uint_t wcnt; /* count of elements in wait state */
432 uint_t rcnt; /* count of elements in run state */
441 The kstat library, \fBlibkstat\fR, defines the user interface (API) to the
442 system's kstat facility.
445 You begin by opening libkstat with \fBkstat_open\fR(3KSTAT), which returns a
446 pointer to a fully initialized kstat control structure. This is your ticket to
447 subsequent libkstat operations:
451 typedef struct kstat_ctl {
452 kid_t kc_chain_id; /* current kstat chain ID */
453 kstat_t *kc_chain; /* pointer to kstat chain */
454 int kc_kd; /* /dev/kstat descriptor */
461 Only the first two fields, \fBkc_chain_id\fR and \fBkc_chain\fR, are of
462 interest to \fBlibkstat\fR clients. (\fIkc_kd\fR is the descriptor for
463 \fB/dev/kstat\fR, the kernel statistics driver. libkstat functions are built on
464 top of \fB/dev/kstat\fR \fBioctl\fR(2) primitives. Direct interaction with
465 \fB/dev/kstat\fR is strongly discouraged, since it is \fInot\fR a public
469 \fBkc_chain\fR points to your copy of the kstat chain. You typically walk the
470 chain to find and process a certain kind of kstat. For example, to display all
480 for (ksp = kc->kc_chain; ksp != NULL; ksp = ksp->ks_next) {
481 if (ksp->ks_type == KSTAT_TYPE_IO) {
482 kstat_read(kc, ksp, &kio);
491 \fBkc_chain_id\fR is the kstat chain \fBID\fR, or \fBKCID\fR, of your copy of
492 the kstat chain. See \fBkstat_chain_update\fR(3KSTAT) for an explanation of
498 \fB\fB/dev/kstat\fR\fR
501 kernel statistics driver
507 \fB\fB/usr/include/kstat.h\fR\fR
516 \fB\fB/usr/include/sys/kstat.h\fR\fR
525 \fBioctl\fR(2), \fBgethrtime\fR(3C), \fBgetloadavg\fR(3C),
526 \fBkstat_chain_update\fR(3KSTAT), \fBkstat_close\fR(3KSTAT),
527 \fBkstat_data_lookup\fR(3KSTAT), \fBkstat_lookup\fR(3KSTAT),
528 \fBkstat_open\fR(3KSTAT), \fBkstat_read\fR(3KSTAT), \fBkstat_write\fR(3KSTAT),