Annotation of src/usr.bin/vmstat/dkstats.c, Revision 1.13
1.13 ! angelos 1: /* $OpenBSD: dkstats.c,v 1.12 2001/05/14 07:40:39 angelos Exp $ */
1.2 deraadt 2: /* $NetBSD: dkstats.c,v 1.1 1996/05/10 23:19:27 thorpej Exp $ */
1.1 tholo 3:
4: /*
1.2 deraadt 5: * Copyright (c) 1996 John M. Vinopal
1.1 tholo 6: * All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
16: * 3. All advertising materials mentioning features or use of this software
17: * must display the following acknowledgement:
18: * This product includes software developed for the NetBSD Project
19: * by John M. Vinopal.
20: * 4. The name of the author may not be used to endorse or promote products
21: * derived from this software without specific prior written permission.
22: *
23: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28: * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30: * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33: * SUCH DAMAGE.
34: */
35:
1.10 angelos 36: #include <sys/param.h>
1.1 tholo 37: #include <sys/dkstat.h>
38: #include <sys/time.h>
39: #include <sys/disk.h>
1.10 angelos 40: #include <sys/sysctl.h>
41: #include <sys/tty.h>
1.1 tholo 42:
43: #include <err.h>
44: #include <fcntl.h>
45: #include <kvm.h>
46: #include <limits.h>
47: #include <nlist.h>
48: #include <stdio.h>
49: #include <stdlib.h>
50: #include <string.h>
51: #include <unistd.h>
52: #include "dkstats.h"
53:
54: static struct nlist namelist[] = {
55: #define X_TK_NIN 0
56: { "_tk_nin" }, /* tty characters in */
57: #define X_TK_NOUT 1
58: { "_tk_nout" }, /* tty characters out */
59: #define X_CP_TIME 2
60: { "_cp_time" }, /* system timer ticks */
61: #define X_HZ 3
62: { "_hz" }, /* ticks per second */
63: #define X_STATHZ 4
64: { "_stathz" },
65: #define X_DISK_COUNT 5
66: { "_disk_count" }, /* number of disks */
67: #define X_DISKLIST 6
68: { "_disklist" }, /* TAILQ of disks */
69: { NULL },
70: };
71:
72: /* Structures to hold the statistics. */
73: struct _disk cur, last;
74:
75: /* Kernel pointers: nlistf and memf defined in calling program. */
76: static kvm_t *kd = NULL;
77: extern char *nlistf;
78: extern char *memf;
79:
80: /* Pointer to list of disks. */
81: static struct disk *dk_drivehead = NULL;
82:
83: /* Backward compatibility references. */
84: int dk_ndrive = 0;
85: int *dk_select;
86: char **dr_name;
87:
88: #define KVM_ERROR(_string) { \
1.8 aaron 89: warnx("%s", (_string)); \
1.7 millert 90: errx(1, "%s", kvm_geterr(kd)); \
1.1 tholo 91: }
92:
93: /*
94: * Dereference the namelist pointer `v' and fill in the local copy
95: * 'p' which is of size 's'.
96: */
97: #define deref_nl(v, p, s) deref_kptr((void *)namelist[(v)].n_value, (p), (s));
98:
99: /* Missing from <sys/time.h> */
100: #define timerset(tvp, uvp) ((uvp)->tv_sec = (tvp)->tv_sec); \
101: ((uvp)->tv_usec = (tvp)->tv_usec)
102:
1.2 deraadt 103: static void deref_kptr __P((void *, void *, size_t));
1.1 tholo 104:
105: /*
106: * Take the delta between the present values and the last recorded
107: * values, storing the present values in the 'last' structure, and
108: * the delta values in the 'cur' structure.
109: */
110: void
111: dkswap()
112: {
113: #define SWAP(fld) tmp = cur.fld; \
114: cur.fld -= last.fld; \
115: last.fld = tmp
116: u_int64_t tmp;
117: int i;
118:
119: for (i = 0; i < dk_ndrive; i++) {
120: struct timeval tmp_timer;
121:
122: if (!cur.dk_select[i])
123: continue;
124:
125: /* Delta Values. */
126: SWAP(dk_xfer[i]);
127: SWAP(dk_seek[i]);
128: SWAP(dk_bytes[i]);
129:
130: /* Delta Time. */
131: timerclear(&tmp_timer);
132: timerset(&(cur.dk_time[i]), &tmp_timer);
133: timersub(&tmp_timer, &(last.dk_time[i]), &(cur.dk_time[i]));
134: timerclear(&(last.dk_time[i]));
135: timerset(&tmp_timer, &(last.dk_time[i]));
136: }
137: for (i = 0; i < CPUSTATES; i++) {
138: SWAP(cp_time[i]);
139: }
140: SWAP(tk_nin);
141: SWAP(tk_nout);
142:
143: #undef SWAP
144: }
145:
146: /*
147: * Read the disk statistics for each disk in the disk list.
148: * Also collect statistics for tty i/o and cpu ticks.
149: */
150: void
151: dkreadstats()
152: {
153: struct disk cur_disk, *p;
1.10 angelos 154: int i, mib[3];
155: size_t size;
1.13 ! angelos 156: struct diskstats *q;
1.1 tholo 157:
1.10 angelos 158: if (nlistf == NULL && memf == NULL) {
1.13 ! angelos 159: size = dk_ndrive * sizeof(struct diskstats);
1.10 angelos 160: mib[0] = CTL_HW;
161: mib[1] = HW_DISKSTATS;
1.13 ! angelos 162: q = malloc(size);
! 163: if (q == NULL)
1.10 angelos 164: err(1, NULL);
1.13 ! angelos 165: if (sysctl(mib, 2, q, &size, NULL, 0) < 0) {
1.10 angelos 166: warn("could not read hw.diskstats");
1.13 ! angelos 167: bzero(q, dk_ndrive * sizeof(struct disk));
1.10 angelos 168: }
169:
170: for (i = 0; i < dk_ndrive; i++) {
1.13 ! angelos 171: cur.dk_xfer[i] = q[i].ds_xfer;
! 172: cur.dk_seek[i] = q[i].ds_seek;
! 173: cur.dk_bytes[i] = q[i].ds_bytes;
! 174: timerset(&(q[i].ds_time), &(cur.dk_time[i]));
1.10 angelos 175: }
176:
177: size = sizeof(cur.cp_time);
178: mib[0] = CTL_KERN;
179: mib[1] = KERN_CPTIME;
180: if (sysctl(mib, 2, cur.cp_time, &size, NULL, 0) < 0) {
1.11 angelos 181: warn("could not read kern.cp_time");
1.10 angelos 182: bzero(cur.cp_time, sizeof(cur.cp_time));
183: }
184: size = sizeof(cur.tk_nin);
185: mib[0] = CTL_KERN;
186: mib[1] = KERN_TTY;
187: mib[2] = KERN_TTY_TKNIN;
188: if (sysctl(mib, 3, &cur.tk_nin, &size, NULL, 0) < 0) {
1.11 angelos 189: warn("could not read kern.tty.tk_nin");
1.10 angelos 190: cur.tk_nin = 0;
191: }
192: size = sizeof(cur.tk_nin);
193: mib[0] = CTL_KERN;
194: mib[1] = KERN_TTY;
195: mib[2] = KERN_TTY_TKNOUT;
196: if (sysctl(mib, 3, &cur.tk_nout, &size, NULL, 0) < 0) {
1.11 angelos 197: warn("could not read kern.tty.tk_nout");
1.10 angelos 198: cur.tk_nout = 0;
199: }
200: } else {
201: p = dk_drivehead;
202:
203: for (i = 0; i < dk_ndrive; i++) {
204: deref_kptr(p, &cur_disk, sizeof(cur_disk));
205: cur.dk_xfer[i] = cur_disk.dk_xfer;
206: cur.dk_seek[i] = cur_disk.dk_seek;
207: cur.dk_bytes[i] = cur_disk.dk_bytes;
208: timerset(&(cur_disk.dk_time), &(cur.dk_time[i]));
209: p = cur_disk.dk_link.tqe_next;
210: }
211:
212: deref_nl(X_CP_TIME, cur.cp_time, sizeof(cur.cp_time));
213: deref_nl(X_TK_NIN, &cur.tk_nin, sizeof(cur.tk_nin));
214: deref_nl(X_TK_NOUT, &cur.tk_nout, sizeof(cur.tk_nout));
1.1 tholo 215: }
216: }
217:
218: /*
219: * Perform all of the initialization and memory allocation needed to
220: * track disk statistics.
221: */
222: int
223: dkinit(select)
224: int select;
225: {
226: struct disklist_head disk_head;
227: struct disk cur_disk, *p;
228: char errbuf[_POSIX2_LINE_MAX];
229: static int once = 0;
230: extern int hz;
1.10 angelos 231: int i, mib[2];
232: size_t size;
233: struct clockinfo clkinfo;
234: char *disknames, *name, *bufpp;
1.1 tholo 235:
236: if (once)
237: return(1);
238:
1.10 angelos 239: if (nlistf != NULL || memf != NULL) {
240: /* Open the kernel. */
241: if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY,
242: errbuf)) == NULL)
243: errx(1, "kvm_openfiles: %s", errbuf);
244:
245: /* Obtain the namelist symbols from the kernel. */
246: if (kvm_nlist(kd, namelist))
247: KVM_ERROR("kvm_nlist failed to read symbols.");
248:
249: /* Get the number of attached drives. */
250: deref_nl(X_DISK_COUNT, &dk_ndrive, sizeof(dk_ndrive));
251:
252: if (dk_ndrive < 0)
253: errx(1, "invalid _disk_count %d.", dk_ndrive);
254:
1.1 tholo 255: /* Get a pointer to the first disk. */
256: deref_nl(X_DISKLIST, &disk_head, sizeof(disk_head));
257: dk_drivehead = disk_head.tqh_first;
1.10 angelos 258:
259: /* Get ticks per second. */
260: deref_nl(X_STATHZ, &hz, sizeof(hz));
261: if (!hz)
262: deref_nl(X_HZ, &hz, sizeof(hz));
263: } else {
264: /* Get the number of attached drives. */
265: mib[0] = CTL_HW;
266: mib[1] = HW_DISKCOUNT;
267: size = sizeof(dk_ndrive);
268: if (sysctl(mib, 2, &dk_ndrive, &size, NULL, 0) < 0 ) {
1.11 angelos 269: warn("could not read hw.diskcount");
1.10 angelos 270: dk_ndrive = 0;
271: }
272:
273: /* Get ticks per second. */
274: mib[0] = CTL_KERN;
275: mib[1] = KERN_CLOCKRATE;
276: size = sizeof(clkinfo);
277: if (sysctl(mib, 2, &clkinfo, &size, NULL, 0) < 0) {
1.11 angelos 278: warn("could not read kern.clockrate");
1.10 angelos 279: hz = 0;
280: } else
281: hz = clkinfo.stathz;
1.1 tholo 282: }
283:
284: /* allocate space for the statistics */
285: cur.dk_time = calloc(dk_ndrive, sizeof(struct timeval));
286: cur.dk_xfer = calloc(dk_ndrive, sizeof(u_int64_t));
287: cur.dk_seek = calloc(dk_ndrive, sizeof(u_int64_t));
288: cur.dk_bytes = calloc(dk_ndrive, sizeof(u_int64_t));
289: last.dk_time = calloc(dk_ndrive, sizeof(struct timeval));
290: last.dk_xfer = calloc(dk_ndrive, sizeof(u_int64_t));
291: last.dk_seek = calloc(dk_ndrive, sizeof(u_int64_t));
292: last.dk_bytes = calloc(dk_ndrive, sizeof(u_int64_t));
293: cur.dk_select = calloc(dk_ndrive, sizeof(int));
294: cur.dk_name = calloc(dk_ndrive, sizeof(char *));
295:
1.9 deraadt 296: if (!cur.dk_time || !cur.dk_xfer || !cur.dk_seek || !cur.dk_bytes ||
297: !last.dk_time || !last.dk_xfer || !last.dk_seek ||
298: !last.dk_bytes || !cur.dk_select || !cur.dk_name)
1.1 tholo 299: errx(1, "Memory allocation failure.");
300:
301: /* Set up the compatibility interfaces. */
302: dk_select = cur.dk_select;
303: dr_name = cur.dk_name;
304:
305: /* Read the disk names and set intial selection. */
1.10 angelos 306: if (nlistf == NULL && memf == NULL) {
307: mib[0] = CTL_HW;
308: mib[1] = HW_DISKNAMES;
309: size = 0;
310: if (sysctl(mib, 2, NULL, &size, NULL, 0) < 0)
311: err(1, "can't get hw.disknames");
1.12 angelos 312: disknames = malloc(size);
1.10 angelos 313: if (disknames == NULL)
314: err(1, NULL);
315: if (sysctl(mib, 2, disknames, &size, NULL, 0) < 0)
316: err(1, "can't get hw.disknames");
317: bufpp = disknames;
318: i = 0;
319: while ((name = strsep(&bufpp, ",")) != NULL) {
320: cur.dk_name[i] = name;
321: cur.dk_select[i++] = select;
322: }
323: } else {
324: p = dk_drivehead;
325: for (i = 0; i < dk_ndrive; i++) {
326: char buf[10];
327: deref_kptr(p, &cur_disk, sizeof(cur_disk));
328: deref_kptr(cur_disk.dk_name, buf, sizeof(buf));
329: cur.dk_name[i] = strdup(buf);
330: if (!cur.dk_name[i])
331: errx(1, "Memory allocation failure.");
332: cur.dk_select[i] = select;
1.1 tholo 333:
1.10 angelos 334: p = cur_disk.dk_link.tqe_next;
335: }
1.1 tholo 336: }
337:
338: /* Never do this initalization again. */
339: once = 1;
340: return(1);
341: }
342:
343: /*
344: * Dereference the kernel pointer `kptr' and fill in the local copy
345: * pointed to by `ptr'. The storage space must be pre-allocated,
346: * and the size of the copy passed in `len'.
347: */
348: static void
349: deref_kptr(kptr, ptr, len)
350: void *kptr, *ptr;
351: size_t len;
352: {
353: char buf[128];
354:
1.6 art 355: if (kvm_read(kd, (u_long)kptr, ptr, len) != len) {
1.1 tholo 356: bzero(buf, sizeof(buf));
357: snprintf(buf, (sizeof(buf) - 1),
1.2 deraadt 358: "can't dereference kptr 0x%lx", (u_long)kptr);
1.1 tholo 359: KVM_ERROR(buf);
360: }
361: }