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