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