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