subr_iostat.c revision 1.4 1 1.3 yamt /* $NetBSD: subr_iostat.c,v 1.4 2006/04/21 13:49:32 yamt Exp $ */
2 1.1 blymn /* NetBSD: subr_disk.c,v 1.69 2005/05/29 22:24:15 christos Exp */
3 1.1 blymn
4 1.1 blymn /*-
5 1.1 blymn * Copyright (c) 1996, 1997, 1999, 2000 The NetBSD Foundation, Inc.
6 1.1 blymn * All rights reserved.
7 1.1 blymn *
8 1.1 blymn * This code is derived from software contributed to The NetBSD Foundation
9 1.1 blymn * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
10 1.1 blymn * NASA Ames Research Center.
11 1.1 blymn *
12 1.1 blymn * Redistribution and use in source and binary forms, with or without
13 1.1 blymn * modification, are permitted provided that the following conditions
14 1.1 blymn * are met:
15 1.1 blymn * 1. Redistributions of source code must retain the above copyright
16 1.1 blymn * notice, this list of conditions and the following disclaimer.
17 1.1 blymn * 2. Redistributions in binary form must reproduce the above copyright
18 1.1 blymn * notice, this list of conditions and the following disclaimer in the
19 1.1 blymn * documentation and/or other materials provided with the distribution.
20 1.1 blymn * 3. All advertising materials mentioning features or use of this software
21 1.1 blymn * must display the following acknowledgement:
22 1.1 blymn * This product includes software developed by the NetBSD
23 1.1 blymn * Foundation, Inc. and its contributors.
24 1.1 blymn * 4. Neither the name of The NetBSD Foundation nor the names of its
25 1.1 blymn * contributors may be used to endorse or promote products derived
26 1.1 blymn * from this software without specific prior written permission.
27 1.1 blymn *
28 1.1 blymn * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29 1.1 blymn * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30 1.1 blymn * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31 1.1 blymn * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32 1.1 blymn * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 1.1 blymn * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 1.1 blymn * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 1.1 blymn * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 1.1 blymn * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 1.1 blymn * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 1.1 blymn * POSSIBILITY OF SUCH DAMAGE.
39 1.1 blymn */
40 1.1 blymn
41 1.1 blymn /*
42 1.1 blymn * Copyright (c) 1982, 1986, 1988, 1993
43 1.1 blymn * The Regents of the University of California. All rights reserved.
44 1.1 blymn * (c) UNIX System Laboratories, Inc.
45 1.1 blymn * All or some portions of this file are derived from material licensed
46 1.1 blymn * to the University of California by American Telephone and Telegraph
47 1.1 blymn * Co. or Unix System Laboratories, Inc. and are reproduced herein with
48 1.1 blymn * the permission of UNIX System Laboratories, Inc.
49 1.1 blymn *
50 1.1 blymn * Redistribution and use in source and binary forms, with or without
51 1.1 blymn * modification, are permitted provided that the following conditions
52 1.1 blymn * are met:
53 1.1 blymn * 1. Redistributions of source code must retain the above copyright
54 1.1 blymn * notice, this list of conditions and the following disclaimer.
55 1.1 blymn * 2. Redistributions in binary form must reproduce the above copyright
56 1.1 blymn * notice, this list of conditions and the following disclaimer in the
57 1.1 blymn * documentation and/or other materials provided with the distribution.
58 1.1 blymn * 3. Neither the name of the University nor the names of its contributors
59 1.1 blymn * may be used to endorse or promote products derived from this software
60 1.1 blymn * without specific prior written permission.
61 1.1 blymn *
62 1.1 blymn * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
63 1.1 blymn * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
64 1.1 blymn * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
65 1.1 blymn * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
66 1.1 blymn * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
67 1.1 blymn * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
68 1.1 blymn * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
69 1.1 blymn * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
70 1.1 blymn * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
71 1.1 blymn * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
72 1.1 blymn * SUCH DAMAGE.
73 1.1 blymn *
74 1.1 blymn * @(#)ufs_disksubr.c 8.5 (Berkeley) 1/21/94
75 1.1 blymn */
76 1.1 blymn
77 1.1 blymn #include <sys/cdefs.h>
78 1.3 yamt __KERNEL_RCSID(0, "$NetBSD: subr_iostat.c,v 1.4 2006/04/21 13:49:32 yamt Exp $");
79 1.1 blymn
80 1.1 blymn #include "opt_compat_netbsd.h"
81 1.1 blymn
82 1.1 blymn #include <sys/param.h>
83 1.1 blymn #include <sys/kernel.h>
84 1.1 blymn #include <sys/malloc.h>
85 1.1 blymn #include <sys/buf.h>
86 1.1 blymn #include <sys/bufq.h>
87 1.1 blymn #include <sys/syslog.h>
88 1.1 blymn #include <sys/disklabel.h>
89 1.1 blymn #include <sys/disk.h>
90 1.1 blymn #include <sys/sysctl.h>
91 1.1 blymn #include <lib/libkern/libkern.h>
92 1.1 blymn
93 1.1 blymn /*
94 1.1 blymn * Function prototypes for sysctl nodes
95 1.1 blymn */
96 1.1 blymn static int sysctl_hw_disknames(SYSCTLFN_PROTO);
97 1.1 blymn static int sysctl_hw_iostatnames(SYSCTLFN_PROTO);
98 1.1 blymn static int sysctl_hw_iostats(SYSCTLFN_PROTO);
99 1.1 blymn
100 1.1 blymn static int
101 1.1 blymn iostati_getnames(int disk_only, char *oldp, size_t *oldlenp, const void *newp,
102 1.1 blymn u_int namelen);
103 1.1 blymn
104 1.1 blymn /*
105 1.1 blymn * A global list of all drives attached to the system. May grow or
106 1.1 blymn * shrink over time.
107 1.1 blymn */
108 1.3 yamt struct iostatlist_head iostatlist = TAILQ_HEAD_INITIALIZER(iostatlist);
109 1.3 yamt int iostat_count; /* number of drives in global drivelist */
110 1.1 blymn struct simplelock iostatlist_slock = SIMPLELOCK_INITIALIZER;
111 1.1 blymn
112 1.1 blymn /*
113 1.1 blymn * Searches the iostatlist for the iostat corresponding to the
114 1.1 blymn * name provided.
115 1.1 blymn */
116 1.1 blymn struct io_stats *
117 1.1 blymn iostat_find(char *name)
118 1.1 blymn {
119 1.1 blymn struct io_stats *iostatp;
120 1.1 blymn
121 1.1 blymn if ((name == NULL) || (iostat_count <= 0))
122 1.1 blymn return (NULL);
123 1.1 blymn
124 1.1 blymn simple_lock(&iostatlist_slock);
125 1.1 blymn for (iostatp = TAILQ_FIRST(&iostatlist); iostatp != NULL;
126 1.2 blymn iostatp = TAILQ_NEXT(iostatp, io_link))
127 1.2 blymn if (strcmp(iostatp->io_name, name) == 0) {
128 1.1 blymn simple_unlock(&iostatlist_slock);
129 1.1 blymn return (iostatp);
130 1.1 blymn }
131 1.1 blymn simple_unlock(&iostatlist_slock);
132 1.1 blymn
133 1.1 blymn return (NULL);
134 1.1 blymn }
135 1.1 blymn
136 1.1 blymn /*
137 1.1 blymn * Allocate and initialise memory for the i/o statistics.
138 1.1 blymn */
139 1.3 yamt struct io_stats *
140 1.3 yamt iostat_alloc(int32_t type)
141 1.1 blymn {
142 1.1 blymn int s;
143 1.1 blymn struct io_stats *stats;
144 1.1 blymn
145 1.1 blymn stats = malloc(sizeof(struct io_stats), M_DEVBUF, M_NOWAIT);
146 1.1 blymn
147 1.1 blymn if (stats == NULL)
148 1.4 yamt panic("iostat_alloc: cannot allocate memory for stats buffer");
149 1.1 blymn
150 1.2 blymn stats->io_type = type;
151 1.2 blymn stats->io_rxfer = 0;
152 1.2 blymn stats->io_rbytes = 0;
153 1.2 blymn stats->io_wxfer = 0;
154 1.2 blymn stats->io_wbytes = 0;
155 1.1 blymn
156 1.1 blymn /*
157 1.1 blymn * Set the attached timestamp.
158 1.1 blymn */
159 1.1 blymn s = splclock();
160 1.2 blymn stats->io_attachtime = mono_time;
161 1.1 blymn splx(s);
162 1.1 blymn
163 1.2 blymn timerclear(&stats->io_time);
164 1.2 blymn timerclear(&stats->io_timestamp);
165 1.1 blymn
166 1.1 blymn /*
167 1.1 blymn * Link into the drivelist.
168 1.1 blymn */
169 1.1 blymn simple_lock(&iostatlist_slock);
170 1.2 blymn TAILQ_INSERT_TAIL(&iostatlist, stats, io_link);
171 1.1 blymn iostat_count++;
172 1.1 blymn simple_unlock(&iostatlist_slock);
173 1.1 blymn
174 1.1 blymn return stats;
175 1.1 blymn }
176 1.1 blymn
177 1.1 blymn /*
178 1.1 blymn * Remove i/o from stats collection.
179 1.1 blymn */
180 1.1 blymn void
181 1.1 blymn iostat_free(struct io_stats *stats)
182 1.1 blymn {
183 1.1 blymn
184 1.3 yamt /*
185 1.3 yamt * Remove from the iostat list.
186 1.3 yamt */
187 1.1 blymn if (iostat_count == 0)
188 1.1 blymn panic("iostat_free: iostat_count == 0");
189 1.1 blymn simple_lock(&iostatlist_slock);
190 1.2 blymn TAILQ_REMOVE(&iostatlist, stats, io_link);
191 1.1 blymn iostat_count--;
192 1.1 blymn simple_unlock(&iostatlist_slock);
193 1.1 blymn free(stats, M_DEVBUF);
194 1.1 blymn }
195 1.1 blymn
196 1.1 blymn /*
197 1.1 blymn * Increment a iostat busy counter. If the counter is going from
198 1.1 blymn * 0 to 1, set the timestamp.
199 1.1 blymn */
200 1.1 blymn void
201 1.1 blymn iostat_busy(struct io_stats *stats)
202 1.1 blymn {
203 1.1 blymn int s;
204 1.1 blymn
205 1.1 blymn /*
206 1.1 blymn * XXX We'd like to use something as accurate as microtime(),
207 1.1 blymn * but that doesn't depend on the system TOD clock.
208 1.1 blymn */
209 1.2 blymn if (stats->io_busy++ == 0) {
210 1.1 blymn s = splclock();
211 1.2 blymn stats->io_timestamp = mono_time;
212 1.1 blymn splx(s);
213 1.1 blymn }
214 1.1 blymn }
215 1.1 blymn
216 1.1 blymn /*
217 1.1 blymn * Decrement a iostat busy counter, increment the byte count, total busy
218 1.1 blymn * time, and reset the timestamp.
219 1.1 blymn */
220 1.1 blymn void
221 1.1 blymn iostat_unbusy(struct io_stats *stats, long bcount, int read)
222 1.1 blymn {
223 1.1 blymn int s;
224 1.1 blymn struct timeval dv_time, diff_time;
225 1.1 blymn
226 1.2 blymn if (stats->io_busy-- == 0) {
227 1.2 blymn printf("%s: busy < 0\n", stats->io_name);
228 1.1 blymn panic("iostat_unbusy");
229 1.1 blymn }
230 1.1 blymn
231 1.1 blymn s = splclock();
232 1.1 blymn dv_time = mono_time;
233 1.1 blymn splx(s);
234 1.1 blymn
235 1.2 blymn timersub(&dv_time, &stats->io_timestamp, &diff_time);
236 1.2 blymn timeradd(&stats->io_time, &diff_time, &stats->io_time);
237 1.1 blymn
238 1.2 blymn stats->io_timestamp = dv_time;
239 1.1 blymn if (bcount > 0) {
240 1.1 blymn if (read) {
241 1.2 blymn stats->io_rbytes += bcount;
242 1.2 blymn stats->io_rxfer++;
243 1.1 blymn } else {
244 1.2 blymn stats->io_wbytes += bcount;
245 1.2 blymn stats->io_wxfer++;
246 1.1 blymn }
247 1.1 blymn }
248 1.1 blymn }
249 1.1 blymn
250 1.1 blymn /*
251 1.1 blymn * Increment the seek counter. This does look almost redundant but it
252 1.1 blymn * abstracts the stats gathering.
253 1.1 blymn */
254 1.1 blymn void
255 1.1 blymn iostat_seek(struct io_stats *stats)
256 1.1 blymn {
257 1.3 yamt
258 1.2 blymn stats->io_seek++;
259 1.1 blymn }
260 1.1 blymn
261 1.1 blymn static int
262 1.1 blymn sysctl_hw_disknames(SYSCTLFN_ARGS)
263 1.1 blymn {
264 1.3 yamt
265 1.1 blymn return iostati_getnames(1, oldp, oldlenp, newp, namelen);
266 1.1 blymn }
267 1.1 blymn
268 1.1 blymn static int
269 1.1 blymn sysctl_hw_iostatnames(SYSCTLFN_ARGS)
270 1.1 blymn {
271 1.3 yamt
272 1.1 blymn return iostati_getnames(0, oldp, oldlenp, newp, namelen);
273 1.1 blymn }
274 1.1 blymn
275 1.1 blymn static int
276 1.1 blymn iostati_getnames(int disk_only, char *oldp, size_t *oldlenp, const void *newp,
277 1.1 blymn u_int namelen)
278 1.1 blymn {
279 1.1 blymn char bf[IOSTATNAMELEN + 1];
280 1.1 blymn char *where = oldp;
281 1.1 blymn struct io_stats *stats;
282 1.1 blymn size_t needed, left, slen;
283 1.1 blymn int error, first;
284 1.1 blymn
285 1.1 blymn if (newp != NULL)
286 1.1 blymn return (EPERM);
287 1.1 blymn if (namelen != 0)
288 1.1 blymn return (EINVAL);
289 1.1 blymn
290 1.1 blymn first = 1;
291 1.1 blymn error = 0;
292 1.1 blymn needed = 0;
293 1.1 blymn left = *oldlenp;
294 1.1 blymn
295 1.1 blymn simple_lock(&iostatlist_slock);
296 1.1 blymn for (stats = TAILQ_FIRST(&iostatlist); stats != NULL;
297 1.2 blymn stats = TAILQ_NEXT(stats, io_link)) {
298 1.2 blymn if ((disk_only == 1) && (stats->io_type != IOSTAT_DISK))
299 1.1 blymn continue;
300 1.1 blymn
301 1.1 blymn if (where == NULL)
302 1.2 blymn needed += strlen(stats->io_name) + 1;
303 1.1 blymn else {
304 1.1 blymn memset(bf, 0, sizeof(bf));
305 1.1 blymn if (first) {
306 1.2 blymn strncpy(bf, stats->io_name, sizeof(bf));
307 1.1 blymn first = 0;
308 1.1 blymn } else {
309 1.1 blymn bf[0] = ' ';
310 1.2 blymn strncpy(bf + 1, stats->io_name,
311 1.1 blymn sizeof(bf) - 1);
312 1.1 blymn }
313 1.1 blymn bf[IOSTATNAMELEN] = '\0';
314 1.1 blymn slen = strlen(bf);
315 1.1 blymn if (left < slen + 1)
316 1.1 blymn break;
317 1.1 blymn /* +1 to copy out the trailing NUL byte */
318 1.1 blymn error = copyout(bf, where, slen + 1);
319 1.1 blymn if (error)
320 1.1 blymn break;
321 1.1 blymn where += slen;
322 1.1 blymn needed += slen;
323 1.1 blymn left -= slen;
324 1.1 blymn }
325 1.1 blymn }
326 1.1 blymn simple_unlock(&iostatlist_slock);
327 1.1 blymn *oldlenp = needed;
328 1.1 blymn return (error);
329 1.1 blymn }
330 1.1 blymn
331 1.1 blymn static int
332 1.1 blymn sysctl_hw_iostats(SYSCTLFN_ARGS)
333 1.1 blymn {
334 1.1 blymn struct io_sysctl sdrive;
335 1.1 blymn struct io_stats *stats;
336 1.1 blymn char *where = oldp;
337 1.1 blymn size_t tocopy, left;
338 1.1 blymn int error;
339 1.1 blymn
340 1.1 blymn if (newp != NULL)
341 1.1 blymn return (EPERM);
342 1.1 blymn
343 1.1 blymn /*
344 1.1 blymn * The original hw.diskstats call was broken and did not require
345 1.1 blymn * the userland to pass in it's size of struct disk_sysctl. This
346 1.1 blymn * was fixed after NetBSD 1.6 was released, and any applications
347 1.1 blymn * that do not pass in the size are given an error only, unless
348 1.1 blymn * we care about 1.6 compatibility.
349 1.1 blymn */
350 1.1 blymn if (namelen == 0)
351 1.1 blymn #ifdef COMPAT_16
352 1.1 blymn tocopy = offsetof(struct io_sysctl, busy);
353 1.1 blymn #else
354 1.1 blymn return (EINVAL);
355 1.1 blymn #endif
356 1.1 blymn else
357 1.1 blymn tocopy = name[0];
358 1.1 blymn
359 1.1 blymn if (where == NULL) {
360 1.1 blymn *oldlenp = iostat_count * tocopy;
361 1.1 blymn return (0);
362 1.1 blymn }
363 1.1 blymn
364 1.1 blymn error = 0;
365 1.1 blymn left = *oldlenp;
366 1.1 blymn memset(&sdrive, 0, sizeof(sdrive));
367 1.1 blymn *oldlenp = 0;
368 1.1 blymn
369 1.1 blymn simple_lock(&iostatlist_slock);
370 1.2 blymn TAILQ_FOREACH(stats, &iostatlist, io_link) {
371 1.1 blymn if (left < tocopy)
372 1.1 blymn break;
373 1.2 blymn strncpy(sdrive.name, stats->io_name, sizeof(sdrive.name));
374 1.2 blymn sdrive.xfer = stats->io_rxfer + stats->io_wxfer;
375 1.2 blymn sdrive.rxfer = stats->io_rxfer;
376 1.2 blymn sdrive.wxfer = stats->io_wxfer;
377 1.2 blymn sdrive.seek = stats->io_seek;
378 1.2 blymn sdrive.bytes = stats->io_rbytes + stats->io_wbytes;
379 1.2 blymn sdrive.rbytes = stats->io_rbytes;
380 1.2 blymn sdrive.wbytes = stats->io_wbytes;
381 1.2 blymn sdrive.attachtime_sec = stats->io_attachtime.tv_sec;
382 1.2 blymn sdrive.attachtime_usec = stats->io_attachtime.tv_usec;
383 1.2 blymn sdrive.timestamp_sec = stats->io_timestamp.tv_sec;
384 1.2 blymn sdrive.timestamp_usec = stats->io_timestamp.tv_usec;
385 1.2 blymn sdrive.time_sec = stats->io_time.tv_sec;
386 1.2 blymn sdrive.time_usec = stats->io_time.tv_usec;
387 1.2 blymn sdrive.busy = stats->io_busy;
388 1.1 blymn
389 1.1 blymn error = copyout(&sdrive, where, min(tocopy, sizeof(sdrive)));
390 1.1 blymn if (error)
391 1.1 blymn break;
392 1.1 blymn where += tocopy;
393 1.1 blymn *oldlenp += tocopy;
394 1.1 blymn left -= tocopy;
395 1.1 blymn }
396 1.1 blymn simple_unlock(&iostatlist_slock);
397 1.1 blymn return (error);
398 1.1 blymn }
399 1.1 blymn
400 1.1 blymn SYSCTL_SETUP(sysctl_io_stats_setup, "sysctl i/o stats setup")
401 1.1 blymn {
402 1.3 yamt
403 1.1 blymn sysctl_createv(clog, 0, NULL, NULL,
404 1.1 blymn CTLFLAG_PERMANENT,
405 1.1 blymn CTLTYPE_STRING, "disknames",
406 1.1 blymn SYSCTL_DESCR("List of disk drives present"),
407 1.1 blymn sysctl_hw_disknames, 0, NULL, 0,
408 1.1 blymn CTL_HW, HW_DISKNAMES, CTL_EOL);
409 1.1 blymn sysctl_createv(clog, 0, NULL, NULL,
410 1.1 blymn CTLFLAG_PERMANENT,
411 1.1 blymn CTLTYPE_STRING, "iostatnames",
412 1.1 blymn SYSCTL_DESCR("I/O stats are being collected for these"
413 1.1 blymn " devices"),
414 1.1 blymn sysctl_hw_iostatnames, 0, NULL, 0,
415 1.1 blymn CTL_HW, HW_IOSTATNAMES, CTL_EOL);
416 1.1 blymn sysctl_createv(clog, 0, NULL, NULL,
417 1.1 blymn CTLFLAG_PERMANENT,
418 1.1 blymn CTLTYPE_STRUCT, "drivestats",
419 1.1 blymn SYSCTL_DESCR("Statistics on device I/O operations"),
420 1.1 blymn sysctl_hw_iostats, 0, NULL, 0,
421 1.1 blymn CTL_HW, HW_IOSTATS, CTL_EOL);
422 1.1 blymn }
423