1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate * 26*7c478bd9Sstevel@tonic-gate * Common routines for acquiring snapshots of kstats for 27*7c478bd9Sstevel@tonic-gate * iostat, mpstat, and vmstat. 28*7c478bd9Sstevel@tonic-gate */ 29*7c478bd9Sstevel@tonic-gate 30*7c478bd9Sstevel@tonic-gate #ifndef _STATCOMMON_H 31*7c478bd9Sstevel@tonic-gate #define _STATCOMMON_H 32*7c478bd9Sstevel@tonic-gate 33*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 34*7c478bd9Sstevel@tonic-gate 35*7c478bd9Sstevel@tonic-gate #ifdef __cplusplus 36*7c478bd9Sstevel@tonic-gate extern "C" { 37*7c478bd9Sstevel@tonic-gate #endif 38*7c478bd9Sstevel@tonic-gate 39*7c478bd9Sstevel@tonic-gate #include <stdio.h> 40*7c478bd9Sstevel@tonic-gate #include <kstat.h> 41*7c478bd9Sstevel@tonic-gate #include <sys/time.h> 42*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 43*7c478bd9Sstevel@tonic-gate #include <sys/buf.h> 44*7c478bd9Sstevel@tonic-gate #include <sys/dnlc.h> 45*7c478bd9Sstevel@tonic-gate #include <sys/sysinfo.h> 46*7c478bd9Sstevel@tonic-gate #include <sys/vmmeter.h> 47*7c478bd9Sstevel@tonic-gate #include <sys/processor.h> 48*7c478bd9Sstevel@tonic-gate #include <sys/pset.h> 49*7c478bd9Sstevel@tonic-gate 50*7c478bd9Sstevel@tonic-gate /* No CPU present at this CPU position */ 51*7c478bd9Sstevel@tonic-gate #define ID_NO_CPU -1 52*7c478bd9Sstevel@tonic-gate /* CPU belongs to no pset (we number this as "pset 0") */ 53*7c478bd9Sstevel@tonic-gate #define ID_NO_PSET 0 54*7c478bd9Sstevel@tonic-gate /* CPU is usable */ 55*7c478bd9Sstevel@tonic-gate #define CPU_ONLINE(s) ((s) == P_ONLINE || (s) == P_NOINTR) 56*7c478bd9Sstevel@tonic-gate /* will the CPU have kstats */ 57*7c478bd9Sstevel@tonic-gate #define CPU_ACTIVE(c) (CPU_ONLINE((c)->cs_state) && (c)->cs_id != ID_NO_CPU) 58*7c478bd9Sstevel@tonic-gate /* IO device has no identified ID */ 59*7c478bd9Sstevel@tonic-gate #define IODEV_NO_ID -1 60*7c478bd9Sstevel@tonic-gate /* no limit to iodevs to collect */ 61*7c478bd9Sstevel@tonic-gate #define UNLIMITED_IODEVS ((size_t)-1) 62*7c478bd9Sstevel@tonic-gate 63*7c478bd9Sstevel@tonic-gate enum snapshot_types { 64*7c478bd9Sstevel@tonic-gate /* All CPUs separately */ 65*7c478bd9Sstevel@tonic-gate SNAP_CPUS = 1 << 0, 66*7c478bd9Sstevel@tonic-gate /* Aggregated processor sets */ 67*7c478bd9Sstevel@tonic-gate SNAP_PSETS = 1 << 1, 68*7c478bd9Sstevel@tonic-gate /* sys-wide stats including aggregated CPU stats */ 69*7c478bd9Sstevel@tonic-gate SNAP_SYSTEM = 1 << 2, 70*7c478bd9Sstevel@tonic-gate /* interrupt sources and counts */ 71*7c478bd9Sstevel@tonic-gate SNAP_INTERRUPTS = 1 << 3, 72*7c478bd9Sstevel@tonic-gate /* cache flushes */ 73*7c478bd9Sstevel@tonic-gate SNAP_FLUSHES = 1 << 4, 74*7c478bd9Sstevel@tonic-gate /* disk etc. stats */ 75*7c478bd9Sstevel@tonic-gate SNAP_IODEVS = 1 << 5, 76*7c478bd9Sstevel@tonic-gate /* disk controller aggregates */ 77*7c478bd9Sstevel@tonic-gate SNAP_CONTROLLERS = 1 << 6, 78*7c478bd9Sstevel@tonic-gate /* mpxio (multipath) paths */ 79*7c478bd9Sstevel@tonic-gate SNAP_IOPATHS = 1 << 7, 80*7c478bd9Sstevel@tonic-gate /* disk error stats */ 81*7c478bd9Sstevel@tonic-gate SNAP_IODEV_ERRORS = 1 << 8, 82*7c478bd9Sstevel@tonic-gate /* pretty names for iodevs */ 83*7c478bd9Sstevel@tonic-gate SNAP_IODEV_PRETTY = 1 << 9 84*7c478bd9Sstevel@tonic-gate }; 85*7c478bd9Sstevel@tonic-gate 86*7c478bd9Sstevel@tonic-gate struct cpu_snapshot { 87*7c478bd9Sstevel@tonic-gate /* may be ID_NO_CPU if no CPU present */ 88*7c478bd9Sstevel@tonic-gate processorid_t cs_id; 89*7c478bd9Sstevel@tonic-gate /* may be ID_NO_PSET if no pset */ 90*7c478bd9Sstevel@tonic-gate psetid_t cs_pset_id; 91*7c478bd9Sstevel@tonic-gate /* as in p_online(2) */ 92*7c478bd9Sstevel@tonic-gate int cs_state; 93*7c478bd9Sstevel@tonic-gate /* stats for this CPU */ 94*7c478bd9Sstevel@tonic-gate kstat_t cs_vm; 95*7c478bd9Sstevel@tonic-gate kstat_t cs_sys; 96*7c478bd9Sstevel@tonic-gate }; 97*7c478bd9Sstevel@tonic-gate 98*7c478bd9Sstevel@tonic-gate struct pset_snapshot { 99*7c478bd9Sstevel@tonic-gate /* ID may be zero to indicate the "none set" */ 100*7c478bd9Sstevel@tonic-gate psetid_t ps_id; 101*7c478bd9Sstevel@tonic-gate /* number of CPUs in set */ 102*7c478bd9Sstevel@tonic-gate size_t ps_nr_cpus; 103*7c478bd9Sstevel@tonic-gate /* the CPUs in this set */ 104*7c478bd9Sstevel@tonic-gate struct cpu_snapshot **ps_cpus; 105*7c478bd9Sstevel@tonic-gate }; 106*7c478bd9Sstevel@tonic-gate 107*7c478bd9Sstevel@tonic-gate struct intr_snapshot { 108*7c478bd9Sstevel@tonic-gate /* name of interrupt source */ 109*7c478bd9Sstevel@tonic-gate char is_name[KSTAT_STRLEN]; 110*7c478bd9Sstevel@tonic-gate /* total number of interrupts from this source */ 111*7c478bd9Sstevel@tonic-gate ulong_t is_total; 112*7c478bd9Sstevel@tonic-gate }; 113*7c478bd9Sstevel@tonic-gate 114*7c478bd9Sstevel@tonic-gate struct sys_snapshot { 115*7c478bd9Sstevel@tonic-gate sysinfo_t ss_sysinfo; 116*7c478bd9Sstevel@tonic-gate vminfo_t ss_vminfo; 117*7c478bd9Sstevel@tonic-gate struct nc_stats ss_nc; 118*7c478bd9Sstevel@tonic-gate /* vm/sys stats aggregated across all CPUs */ 119*7c478bd9Sstevel@tonic-gate kstat_t ss_agg_vm; 120*7c478bd9Sstevel@tonic-gate kstat_t ss_agg_sys; 121*7c478bd9Sstevel@tonic-gate /* ticks since boot */ 122*7c478bd9Sstevel@tonic-gate ulong_t ss_ticks; 123*7c478bd9Sstevel@tonic-gate long ss_deficit; 124*7c478bd9Sstevel@tonic-gate }; 125*7c478bd9Sstevel@tonic-gate 126*7c478bd9Sstevel@tonic-gate /* order is significant (see sort_before()) */ 127*7c478bd9Sstevel@tonic-gate enum iodev_type { 128*7c478bd9Sstevel@tonic-gate IODEV_CONTROLLER = 1 << 0, 129*7c478bd9Sstevel@tonic-gate IODEV_DISK = 1 << 1, 130*7c478bd9Sstevel@tonic-gate IODEV_PARTITION = 1 << 2, 131*7c478bd9Sstevel@tonic-gate IODEV_TAPE = 1 << 3, 132*7c478bd9Sstevel@tonic-gate IODEV_NFS = 1 << 4, 133*7c478bd9Sstevel@tonic-gate IODEV_IOPATH = 1 << 5, 134*7c478bd9Sstevel@tonic-gate IODEV_UNKNOWN = 1 << 6 135*7c478bd9Sstevel@tonic-gate }; 136*7c478bd9Sstevel@tonic-gate 137*7c478bd9Sstevel@tonic-gate /* identify a disk, partition, etc. */ 138*7c478bd9Sstevel@tonic-gate struct iodev_id { 139*7c478bd9Sstevel@tonic-gate int id; 140*7c478bd9Sstevel@tonic-gate /* target id (for disks) */ 141*7c478bd9Sstevel@tonic-gate char tid[KSTAT_STRLEN]; 142*7c478bd9Sstevel@tonic-gate }; 143*7c478bd9Sstevel@tonic-gate 144*7c478bd9Sstevel@tonic-gate /* 145*7c478bd9Sstevel@tonic-gate * Used for disks, partitions, tapes, nfs, controllers, iopaths 146*7c478bd9Sstevel@tonic-gate * Each entry can be a branch of a tree; for example, the disks 147*7c478bd9Sstevel@tonic-gate * of a controller constitute the children of the controller 148*7c478bd9Sstevel@tonic-gate * iodev_snapshot. This relationship is not strictly maintained 149*7c478bd9Sstevel@tonic-gate * if is_pretty can't be found. 150*7c478bd9Sstevel@tonic-gate */ 151*7c478bd9Sstevel@tonic-gate struct iodev_snapshot { 152*7c478bd9Sstevel@tonic-gate /* original kstat name */ 153*7c478bd9Sstevel@tonic-gate char is_name[KSTAT_STRLEN]; 154*7c478bd9Sstevel@tonic-gate enum iodev_type is_type; 155*7c478bd9Sstevel@tonic-gate /* ID if meaningful */ 156*7c478bd9Sstevel@tonic-gate struct iodev_id is_id; 157*7c478bd9Sstevel@tonic-gate /* parent ID if meaningful */ 158*7c478bd9Sstevel@tonic-gate struct iodev_id is_parent_id; 159*7c478bd9Sstevel@tonic-gate /* user-friendly name if found */ 160*7c478bd9Sstevel@tonic-gate char *is_pretty; 161*7c478bd9Sstevel@tonic-gate /* device ID if applicable */ 162*7c478bd9Sstevel@tonic-gate char *is_devid; 163*7c478bd9Sstevel@tonic-gate /* mount-point if applicable */ 164*7c478bd9Sstevel@tonic-gate char *is_dname; 165*7c478bd9Sstevel@tonic-gate /* number of direct children */ 166*7c478bd9Sstevel@tonic-gate int is_nr_children; 167*7c478bd9Sstevel@tonic-gate /* children of this I/O device */ 168*7c478bd9Sstevel@tonic-gate struct iodev_snapshot *is_children; 169*7c478bd9Sstevel@tonic-gate /* standard I/O stats */ 170*7c478bd9Sstevel@tonic-gate kstat_io_t is_stats; 171*7c478bd9Sstevel@tonic-gate /* iodev error stats */ 172*7c478bd9Sstevel@tonic-gate kstat_t is_errors; 173*7c478bd9Sstevel@tonic-gate /* creation time of the stats */ 174*7c478bd9Sstevel@tonic-gate hrtime_t is_crtime; 175*7c478bd9Sstevel@tonic-gate /* time at which iodev snapshot was taken */ 176*7c478bd9Sstevel@tonic-gate hrtime_t is_snaptime; 177*7c478bd9Sstevel@tonic-gate /* kstat module */ 178*7c478bd9Sstevel@tonic-gate char is_module[KSTAT_STRLEN]; 179*7c478bd9Sstevel@tonic-gate /* kstat instance */ 180*7c478bd9Sstevel@tonic-gate int is_instance; 181*7c478bd9Sstevel@tonic-gate /* kstat (only used temporarily) */ 182*7c478bd9Sstevel@tonic-gate kstat_t *is_ksp; 183*7c478bd9Sstevel@tonic-gate struct iodev_snapshot *is_prev; 184*7c478bd9Sstevel@tonic-gate struct iodev_snapshot *is_next; 185*7c478bd9Sstevel@tonic-gate }; 186*7c478bd9Sstevel@tonic-gate 187*7c478bd9Sstevel@tonic-gate /* which iodevs to show. */ 188*7c478bd9Sstevel@tonic-gate struct iodev_filter { 189*7c478bd9Sstevel@tonic-gate /* nr. of iodevs to choose */ 190*7c478bd9Sstevel@tonic-gate size_t if_max_iodevs; 191*7c478bd9Sstevel@tonic-gate /* bit mask of enum io_types to allow */ 192*7c478bd9Sstevel@tonic-gate int if_allowed_types; 193*7c478bd9Sstevel@tonic-gate /* should we show floppy ? if_names can override this */ 194*7c478bd9Sstevel@tonic-gate int if_skip_floppy; 195*7c478bd9Sstevel@tonic-gate /* nr. of named iodevs */ 196*7c478bd9Sstevel@tonic-gate size_t if_nr_names; 197*7c478bd9Sstevel@tonic-gate char **if_names; 198*7c478bd9Sstevel@tonic-gate }; 199*7c478bd9Sstevel@tonic-gate 200*7c478bd9Sstevel@tonic-gate /* The primary structure of a system snapshot. */ 201*7c478bd9Sstevel@tonic-gate struct snapshot { 202*7c478bd9Sstevel@tonic-gate /* what types were *requested* */ 203*7c478bd9Sstevel@tonic-gate enum snapshot_types s_types; 204*7c478bd9Sstevel@tonic-gate size_t s_nr_cpus; 205*7c478bd9Sstevel@tonic-gate struct cpu_snapshot *s_cpus; 206*7c478bd9Sstevel@tonic-gate size_t s_nr_psets; 207*7c478bd9Sstevel@tonic-gate struct pset_snapshot *s_psets; 208*7c478bd9Sstevel@tonic-gate size_t s_nr_intrs; 209*7c478bd9Sstevel@tonic-gate struct intr_snapshot *s_intrs; 210*7c478bd9Sstevel@tonic-gate size_t s_nr_iodevs; 211*7c478bd9Sstevel@tonic-gate struct iodev_snapshot *s_iodevs; 212*7c478bd9Sstevel@tonic-gate struct sys_snapshot s_sys; 213*7c478bd9Sstevel@tonic-gate struct biostats s_biostats; 214*7c478bd9Sstevel@tonic-gate struct flushmeter s_flushes; 215*7c478bd9Sstevel@tonic-gate }; 216*7c478bd9Sstevel@tonic-gate 217*7c478bd9Sstevel@tonic-gate /* print a message and exit with failure */ 218*7c478bd9Sstevel@tonic-gate void fail(int do_perror, char *message, ...); 219*7c478bd9Sstevel@tonic-gate 220*7c478bd9Sstevel@tonic-gate /* strdup str, or exit with failure */ 221*7c478bd9Sstevel@tonic-gate char *safe_strdup(char *str); 222*7c478bd9Sstevel@tonic-gate 223*7c478bd9Sstevel@tonic-gate /* malloc successfully, or exit with failure */ 224*7c478bd9Sstevel@tonic-gate void *safe_alloc(size_t size); 225*7c478bd9Sstevel@tonic-gate 226*7c478bd9Sstevel@tonic-gate /* 227*7c478bd9Sstevel@tonic-gate * Copy a kstat from src to dst. If the source kstat contains no data, 228*7c478bd9Sstevel@tonic-gate * then set the destination kstat data to NULL and size to zero. 229*7c478bd9Sstevel@tonic-gate * Returns 0 on success. 230*7c478bd9Sstevel@tonic-gate */ 231*7c478bd9Sstevel@tonic-gate int kstat_copy(const kstat_t *src, kstat_t *dst); 232*7c478bd9Sstevel@tonic-gate 233*7c478bd9Sstevel@tonic-gate /* 234*7c478bd9Sstevel@tonic-gate * Look up the named kstat, and give the ui64 difference i.e. 235*7c478bd9Sstevel@tonic-gate * new - old, or if old is NULL, return new. 236*7c478bd9Sstevel@tonic-gate */ 237*7c478bd9Sstevel@tonic-gate uint64_t kstat_delta(kstat_t *old, kstat_t *new, char *name); 238*7c478bd9Sstevel@tonic-gate 239*7c478bd9Sstevel@tonic-gate /* 240*7c478bd9Sstevel@tonic-gate * Add the integer-valued stats from "src" to the 241*7c478bd9Sstevel@tonic-gate * existing ones in "dst". If "dst" does not contain 242*7c478bd9Sstevel@tonic-gate * stats, then a kstat_copy() is performed. 243*7c478bd9Sstevel@tonic-gate */ 244*7c478bd9Sstevel@tonic-gate int kstat_add(const kstat_t *src, kstat_t *dst); 245*7c478bd9Sstevel@tonic-gate 246*7c478bd9Sstevel@tonic-gate /* return the number of CPUs with kstats (i.e. present and online) */ 247*7c478bd9Sstevel@tonic-gate int nr_active_cpus(struct snapshot *ss); 248*7c478bd9Sstevel@tonic-gate 249*7c478bd9Sstevel@tonic-gate /* 250*7c478bd9Sstevel@tonic-gate * Return the difference in CPU ticks between the two sys 251*7c478bd9Sstevel@tonic-gate * kstats. 252*7c478bd9Sstevel@tonic-gate */ 253*7c478bd9Sstevel@tonic-gate uint64_t cpu_ticks_delta(kstat_t *old, kstat_t *new); 254*7c478bd9Sstevel@tonic-gate 255*7c478bd9Sstevel@tonic-gate /* 256*7c478bd9Sstevel@tonic-gate * Open the kstat chain. Cannot fail. 257*7c478bd9Sstevel@tonic-gate */ 258*7c478bd9Sstevel@tonic-gate kstat_ctl_t *open_kstat(void); 259*7c478bd9Sstevel@tonic-gate 260*7c478bd9Sstevel@tonic-gate /* 261*7c478bd9Sstevel@tonic-gate * Return a struct snapshot based on the snapshot_types parameter 262*7c478bd9Sstevel@tonic-gate * passed in. iodev_filter may be NULL in which case all iodevs 263*7c478bd9Sstevel@tonic-gate * are selected if SNAP_IODEVS is passed. 264*7c478bd9Sstevel@tonic-gate */ 265*7c478bd9Sstevel@tonic-gate struct snapshot *acquire_snapshot(kstat_ctl_t *, int, struct iodev_filter *); 266*7c478bd9Sstevel@tonic-gate 267*7c478bd9Sstevel@tonic-gate /* free a snapshot */ 268*7c478bd9Sstevel@tonic-gate void free_snapshot(struct snapshot *ss); 269*7c478bd9Sstevel@tonic-gate 270*7c478bd9Sstevel@tonic-gate typedef void (*snapshot_cb)(void *old, void *new, void *data); 271*7c478bd9Sstevel@tonic-gate 272*7c478bd9Sstevel@tonic-gate /* 273*7c478bd9Sstevel@tonic-gate * Call the call back for each pair of data items of the given type, 274*7c478bd9Sstevel@tonic-gate * passing the data pointer passed in as well. If an item has been 275*7c478bd9Sstevel@tonic-gate * added, the first pointer will be NULL; if removed, the second pointer 276*7c478bd9Sstevel@tonic-gate * will be NULL. 277*7c478bd9Sstevel@tonic-gate * 278*7c478bd9Sstevel@tonic-gate * A non-zero return value indicates configuration has changed. 279*7c478bd9Sstevel@tonic-gate */ 280*7c478bd9Sstevel@tonic-gate int snapshot_walk(enum snapshot_types type, struct snapshot *old, 281*7c478bd9Sstevel@tonic-gate struct snapshot *new, snapshot_cb cb, void *data); 282*7c478bd9Sstevel@tonic-gate 283*7c478bd9Sstevel@tonic-gate /* 284*7c478bd9Sstevel@tonic-gate * Output a line detailing any configuration changes such as a CPU 285*7c478bd9Sstevel@tonic-gate * brought online, etc, bracketed by << >>. 286*7c478bd9Sstevel@tonic-gate */ 287*7c478bd9Sstevel@tonic-gate void snapshot_report_changes(struct snapshot *old, struct snapshot *new); 288*7c478bd9Sstevel@tonic-gate 289*7c478bd9Sstevel@tonic-gate /* Return non-zero if configuration has changed. */ 290*7c478bd9Sstevel@tonic-gate int snapshot_has_changed(struct snapshot *old, struct snapshot *new); 291*7c478bd9Sstevel@tonic-gate 292*7c478bd9Sstevel@tonic-gate /* free the given iodev */ 293*7c478bd9Sstevel@tonic-gate void free_iodev(struct iodev_snapshot *iodev); 294*7c478bd9Sstevel@tonic-gate 295*7c478bd9Sstevel@tonic-gate /* acquire the I/O devices */ 296*7c478bd9Sstevel@tonic-gate int acquire_iodevs(struct snapshot *ss, kstat_ctl_t *kc, 297*7c478bd9Sstevel@tonic-gate struct iodev_filter *df); 298*7c478bd9Sstevel@tonic-gate 299*7c478bd9Sstevel@tonic-gate /* strcmp-style I/O device comparator */ 300*7c478bd9Sstevel@tonic-gate int iodev_cmp(struct iodev_snapshot *io1, struct iodev_snapshot *io2); 301*7c478bd9Sstevel@tonic-gate 302*7c478bd9Sstevel@tonic-gate #ifdef __cplusplus 303*7c478bd9Sstevel@tonic-gate } 304*7c478bd9Sstevel@tonic-gate #endif 305*7c478bd9Sstevel@tonic-gate 306*7c478bd9Sstevel@tonic-gate #endif /* _STATCOMMON_H */ 307