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