Annotation of src/usr.bin/vmstat/dkstats.c, Revision 1.21
1.21 ! deraadt 1: /* $OpenBSD: dkstats.c,v 1.20 2002/08/04 00:51:01 deraadt 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:
1.16 deraadt 54: #if !defined(NOKVM)
1.1 tholo 55: static struct nlist namelist[] = {
1.18 deraadt 56: #define X_TK_NIN 0 /* sysctl */
57: { "_tk_nin" },
58: #define X_TK_NOUT 1 /* sysctl */
59: { "_tk_nout" },
60: #define X_CP_TIME 2 /* sysctl */
61: { "_cp_time" },
62: #define X_HZ 3 /* sysctl */
63: { "_hz" },
64: #define X_STATHZ 4 /* sysctl */
1.1 tholo 65: { "_stathz" },
1.18 deraadt 66: #define X_DISK_COUNT 5 /* sysctl */
67: { "_disk_count" },
68: #define X_DISKLIST 6 /* sysctl */
69: { "_disklist" },
1.1 tholo 70: { NULL },
71: };
1.16 deraadt 72: #define KVM_ERROR(_string) { \
73: warnx("%s", (_string)); \
74: errx(1, "%s", kvm_geterr(kd)); \
75: }
76:
77: /*
78: * Dereference the namelist pointer `v' and fill in the local copy
79: * 'p' which is of size 's'.
80: */
81: #define deref_nl(v, p, s) deref_kptr((void *)namelist[(v)].n_value, (p), (s));
82: static void deref_kptr(void *, void *, size_t);
83: #endif /* !defined(NOKVM) */
1.1 tholo 84:
85: /* Structures to hold the statistics. */
86: struct _disk cur, last;
87:
88: /* Kernel pointers: nlistf and memf defined in calling program. */
1.19 deraadt 89: #if !defined(NOKVM)
1.20 deraadt 90: extern kvm_t *kd;
1.19 deraadt 91: #endif
1.1 tholo 92: extern char *nlistf;
93: extern char *memf;
94:
1.19 deraadt 95: #if !defined(NOKVM)
1.1 tholo 96: /* Pointer to list of disks. */
97: static struct disk *dk_drivehead = NULL;
1.19 deraadt 98: #endif
1.1 tholo 99:
100: /* Backward compatibility references. */
101: int dk_ndrive = 0;
102: int *dk_select;
103: char **dr_name;
104:
105: /* Missing from <sys/time.h> */
1.16 deraadt 106: #define timerset(tvp, uvp) \
107: ((uvp)->tv_sec = (tvp)->tv_sec); \
108: ((uvp)->tv_usec = (tvp)->tv_usec)
109:
110: #define SWAP(fld) tmp = cur.fld; \
111: cur.fld -= last.fld; \
112: last.fld = tmp
1.1 tholo 113:
114: /*
115: * Take the delta between the present values and the last recorded
116: * values, storing the present values in the 'last' structure, and
117: * the delta values in the 'cur' structure.
118: */
119: void
1.21 ! deraadt 120: dkswap(void)
1.1 tholo 121: {
122: u_int64_t tmp;
123: int i;
124:
125: for (i = 0; i < dk_ndrive; i++) {
126: struct timeval tmp_timer;
127:
128: if (!cur.dk_select[i])
129: continue;
130:
131: /* Delta Values. */
132: SWAP(dk_xfer[i]);
133: SWAP(dk_seek[i]);
134: SWAP(dk_bytes[i]);
135:
136: /* Delta Time. */
137: timerclear(&tmp_timer);
138: timerset(&(cur.dk_time[i]), &tmp_timer);
139: timersub(&tmp_timer, &(last.dk_time[i]), &(cur.dk_time[i]));
140: timerclear(&(last.dk_time[i]));
141: timerset(&tmp_timer, &(last.dk_time[i]));
142: }
143: for (i = 0; i < CPUSTATES; i++) {
144: SWAP(cp_time[i]);
145: }
146: SWAP(tk_nin);
147: SWAP(tk_nout);
148:
149: #undef SWAP
150: }
151:
152: /*
153: * Read the disk statistics for each disk in the disk list.
154: * Also collect statistics for tty i/o and cpu ticks.
155: */
156: void
1.21 ! deraadt 157: dkreadstats(void)
1.1 tholo 158: {
1.19 deraadt 159: #if !defined(NOKVM)
1.1 tholo 160: struct disk cur_disk, *p;
1.19 deraadt 161: #endif
1.10 angelos 162: int i, mib[3];
163: size_t size;
1.13 angelos 164: struct diskstats *q;
1.1 tholo 165:
1.10 angelos 166: if (nlistf == NULL && memf == NULL) {
1.13 angelos 167: size = dk_ndrive * sizeof(struct diskstats);
1.10 angelos 168: mib[0] = CTL_HW;
169: mib[1] = HW_DISKSTATS;
1.13 angelos 170: q = malloc(size);
171: if (q == NULL)
1.10 angelos 172: err(1, NULL);
1.13 angelos 173: if (sysctl(mib, 2, q, &size, NULL, 0) < 0) {
1.10 angelos 174: warn("could not read hw.diskstats");
1.14 deraadt 175: bzero(q, dk_ndrive * sizeof(struct diskstats));
1.10 angelos 176: }
177:
178: for (i = 0; i < dk_ndrive; i++) {
1.13 angelos 179: cur.dk_xfer[i] = q[i].ds_xfer;
180: cur.dk_seek[i] = q[i].ds_seek;
181: cur.dk_bytes[i] = q[i].ds_bytes;
182: timerset(&(q[i].ds_time), &(cur.dk_time[i]));
1.10 angelos 183: }
1.14 deraadt 184: free(q);
1.10 angelos 185:
186: size = sizeof(cur.cp_time);
187: mib[0] = CTL_KERN;
188: mib[1] = KERN_CPTIME;
189: if (sysctl(mib, 2, cur.cp_time, &size, NULL, 0) < 0) {
1.11 angelos 190: warn("could not read kern.cp_time");
1.10 angelos 191: bzero(cur.cp_time, sizeof(cur.cp_time));
192: }
193: size = sizeof(cur.tk_nin);
194: mib[0] = CTL_KERN;
195: mib[1] = KERN_TTY;
196: mib[2] = KERN_TTY_TKNIN;
197: if (sysctl(mib, 3, &cur.tk_nin, &size, NULL, 0) < 0) {
1.11 angelos 198: warn("could not read kern.tty.tk_nin");
1.10 angelos 199: cur.tk_nin = 0;
200: }
201: size = sizeof(cur.tk_nin);
202: mib[0] = CTL_KERN;
203: mib[1] = KERN_TTY;
204: mib[2] = KERN_TTY_TKNOUT;
205: if (sysctl(mib, 3, &cur.tk_nout, &size, NULL, 0) < 0) {
1.11 angelos 206: warn("could not read kern.tty.tk_nout");
1.10 angelos 207: cur.tk_nout = 0;
208: }
209: } else {
1.16 deraadt 210: #if !defined(NOKVM)
1.10 angelos 211: p = dk_drivehead;
212:
213: for (i = 0; i < dk_ndrive; i++) {
214: deref_kptr(p, &cur_disk, sizeof(cur_disk));
215: cur.dk_xfer[i] = cur_disk.dk_xfer;
216: cur.dk_seek[i] = cur_disk.dk_seek;
217: cur.dk_bytes[i] = cur_disk.dk_bytes;
218: timerset(&(cur_disk.dk_time), &(cur.dk_time[i]));
219: p = cur_disk.dk_link.tqe_next;
220: }
221: deref_nl(X_CP_TIME, cur.cp_time, sizeof(cur.cp_time));
222: deref_nl(X_TK_NIN, &cur.tk_nin, sizeof(cur.tk_nin));
223: deref_nl(X_TK_NOUT, &cur.tk_nout, sizeof(cur.tk_nout));
1.16 deraadt 224: #endif /* !defined(NOKVM) */
1.1 tholo 225: }
226: }
227:
228: /*
229: * Perform all of the initialization and memory allocation needed to
230: * track disk statistics.
231: */
232: int
1.21 ! deraadt 233: dkinit(int select)
1.1 tholo 234: {
1.19 deraadt 235: #if !defined(NOKVM)
1.1 tholo 236: struct disklist_head disk_head;
237: struct disk cur_disk, *p;
238: char errbuf[_POSIX2_LINE_MAX];
1.19 deraadt 239: #endif
1.1 tholo 240: static int once = 0;
241: extern int hz;
1.10 angelos 242: int i, mib[2];
243: size_t size;
244: struct clockinfo clkinfo;
245: char *disknames, *name, *bufpp;
1.1 tholo 246:
247: if (once)
248: return(1);
249:
1.10 angelos 250: if (nlistf != NULL || memf != NULL) {
1.16 deraadt 251: #if !defined(NOKVM)
1.20 deraadt 252: if (memf != NULL) {
253: setegid(getgid());
254: setgid(getgid());
255: }
256:
1.10 angelos 257: /* Open the kernel. */
1.20 deraadt 258: if (kd == NULL &&
259: (kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY,
1.10 angelos 260: errbuf)) == NULL)
261: errx(1, "kvm_openfiles: %s", errbuf);
1.20 deraadt 262:
263: setegid(getgid());
264: setgid(getgid());
1.10 angelos 265:
266: /* Obtain the namelist symbols from the kernel. */
267: if (kvm_nlist(kd, namelist))
268: KVM_ERROR("kvm_nlist failed to read symbols.");
269:
270: /* Get the number of attached drives. */
271: deref_nl(X_DISK_COUNT, &dk_ndrive, sizeof(dk_ndrive));
272:
273: if (dk_ndrive < 0)
274: errx(1, "invalid _disk_count %d.", dk_ndrive);
275:
1.1 tholo 276: /* Get a pointer to the first disk. */
277: deref_nl(X_DISKLIST, &disk_head, sizeof(disk_head));
278: dk_drivehead = disk_head.tqh_first;
1.10 angelos 279:
280: /* Get ticks per second. */
281: deref_nl(X_STATHZ, &hz, sizeof(hz));
282: if (!hz)
283: deref_nl(X_HZ, &hz, sizeof(hz));
1.16 deraadt 284: #endif /* !defined(NOKVM) */
1.10 angelos 285: } else {
286: /* Get the number of attached drives. */
287: mib[0] = CTL_HW;
288: mib[1] = HW_DISKCOUNT;
289: size = sizeof(dk_ndrive);
290: if (sysctl(mib, 2, &dk_ndrive, &size, NULL, 0) < 0 ) {
1.11 angelos 291: warn("could not read hw.diskcount");
1.10 angelos 292: dk_ndrive = 0;
293: }
294:
295: /* Get ticks per second. */
296: mib[0] = CTL_KERN;
297: mib[1] = KERN_CLOCKRATE;
298: size = sizeof(clkinfo);
299: if (sysctl(mib, 2, &clkinfo, &size, NULL, 0) < 0) {
1.11 angelos 300: warn("could not read kern.clockrate");
1.10 angelos 301: hz = 0;
302: } else
303: hz = clkinfo.stathz;
1.1 tholo 304: }
305:
306: /* allocate space for the statistics */
307: cur.dk_time = calloc(dk_ndrive, sizeof(struct timeval));
308: cur.dk_xfer = calloc(dk_ndrive, sizeof(u_int64_t));
309: cur.dk_seek = calloc(dk_ndrive, sizeof(u_int64_t));
310: cur.dk_bytes = calloc(dk_ndrive, sizeof(u_int64_t));
311: last.dk_time = calloc(dk_ndrive, sizeof(struct timeval));
312: last.dk_xfer = calloc(dk_ndrive, sizeof(u_int64_t));
313: last.dk_seek = calloc(dk_ndrive, sizeof(u_int64_t));
314: last.dk_bytes = calloc(dk_ndrive, sizeof(u_int64_t));
315: cur.dk_select = calloc(dk_ndrive, sizeof(int));
316: cur.dk_name = calloc(dk_ndrive, sizeof(char *));
317:
1.9 deraadt 318: if (!cur.dk_time || !cur.dk_xfer || !cur.dk_seek || !cur.dk_bytes ||
319: !last.dk_time || !last.dk_xfer || !last.dk_seek ||
320: !last.dk_bytes || !cur.dk_select || !cur.dk_name)
1.1 tholo 321: errx(1, "Memory allocation failure.");
322:
323: /* Set up the compatibility interfaces. */
324: dk_select = cur.dk_select;
325: dr_name = cur.dk_name;
326:
327: /* Read the disk names and set intial selection. */
1.10 angelos 328: if (nlistf == NULL && memf == NULL) {
329: mib[0] = CTL_HW;
330: mib[1] = HW_DISKNAMES;
331: size = 0;
332: if (sysctl(mib, 2, NULL, &size, NULL, 0) < 0)
333: err(1, "can't get hw.disknames");
1.12 angelos 334: disknames = malloc(size);
1.10 angelos 335: if (disknames == NULL)
336: err(1, NULL);
337: if (sysctl(mib, 2, disknames, &size, NULL, 0) < 0)
338: err(1, "can't get hw.disknames");
339: bufpp = disknames;
340: i = 0;
341: while ((name = strsep(&bufpp, ",")) != NULL) {
342: cur.dk_name[i] = name;
343: cur.dk_select[i++] = select;
344: }
345: } else {
1.16 deraadt 346: #if !defined(NOKVM)
1.10 angelos 347: p = dk_drivehead;
348: for (i = 0; i < dk_ndrive; i++) {
349: char buf[10];
1.16 deraadt 350:
1.10 angelos 351: deref_kptr(p, &cur_disk, sizeof(cur_disk));
352: deref_kptr(cur_disk.dk_name, buf, sizeof(buf));
353: cur.dk_name[i] = strdup(buf);
354: if (!cur.dk_name[i])
355: errx(1, "Memory allocation failure.");
356: cur.dk_select[i] = select;
1.1 tholo 357:
1.10 angelos 358: p = cur_disk.dk_link.tqe_next;
359: }
1.16 deraadt 360: #endif /* !defined(NOKVM) */
1.1 tholo 361: }
362:
363: /* Never do this initalization again. */
364: once = 1;
365: return(1);
366: }
367:
1.16 deraadt 368: #if !defined(NOKVM)
1.1 tholo 369: /*
370: * Dereference the kernel pointer `kptr' and fill in the local copy
371: * pointed to by `ptr'. The storage space must be pre-allocated,
372: * and the size of the copy passed in `len'.
373: */
374: static void
1.21 ! deraadt 375: deref_kptr(void *kptr, void *ptr, size_t len)
1.1 tholo 376: {
377: char buf[128];
378:
1.6 art 379: if (kvm_read(kd, (u_long)kptr, ptr, len) != len) {
1.1 tholo 380: bzero(buf, sizeof(buf));
381: snprintf(buf, (sizeof(buf) - 1),
1.2 deraadt 382: "can't dereference kptr 0x%lx", (u_long)kptr);
1.1 tholo 383: KVM_ERROR(buf);
384: }
385: }
1.17 drahn 386: #endif /* !defined(NOKVM) */