subr_iostat.c revision 1.12 1 /* $NetBSD: subr_iostat.c,v 1.12 2006/12/07 20:23:38 ad 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.12 2006/12/07 20:23:38 ad 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/iostat.h>
86 #include <sys/sysctl.h>
87
88 /*
89 * Function prototypes for sysctl nodes
90 */
91 static int sysctl_hw_disknames(SYSCTLFN_PROTO);
92 static int sysctl_hw_iostatnames(SYSCTLFN_PROTO);
93 static int sysctl_hw_iostats(SYSCTLFN_PROTO);
94
95 static int
96 iostati_getnames(int disk_only, char *oldp, size_t *oldlenp, const void *newp,
97 u_int namelen);
98
99 /*
100 * A global list of all drives attached to the system. May grow or
101 * shrink over time.
102 */
103 struct iostatlist_head iostatlist = TAILQ_HEAD_INITIALIZER(iostatlist);
104 int iostat_count; /* number of drives in global drivelist */
105 struct lock iostatlist_lock;
106
107 /*
108 * Initialise the iostat subsystem.
109 */
110 void
111 iostat_init(void)
112 {
113
114 lockinit(&iostatlist_lock, PWAIT, "iostatlk", 0, 0);
115 }
116
117 /*
118 * Searches the iostatlist for the iostat corresponding to the
119 * name provided.
120 */
121 struct io_stats *
122 iostat_find(const char *name)
123 {
124 struct io_stats *iostatp;
125
126 KASSERT(name != NULL);
127
128 lockmgr(&iostatlist_lock, LK_SHARED, NULL);
129 TAILQ_FOREACH(iostatp, &iostatlist, io_link) {
130 if (strcmp(iostatp->io_name, name) == 0) {
131 break;
132 }
133 }
134 lockmgr(&iostatlist_lock, LK_RELEASE, NULL);
135
136 return iostatp;
137 }
138
139 /*
140 * Allocate and initialise memory for the i/o statistics.
141 */
142 struct io_stats *
143 iostat_alloc(int32_t type, void *parent, const char *name)
144 {
145 struct io_stats *stats;
146
147 stats = malloc(sizeof(struct io_stats), M_DEVBUF, M_WAITOK|M_ZERO);
148 if (stats == NULL)
149 panic("iostat_alloc: cannot allocate memory for stats buffer");
150
151 stats->io_type = type;
152 stats->io_parent = parent;
153 (void)strlcpy(stats->io_name, name, sizeof(stats->io_name));
154
155 /*
156 * Set the attached timestamp.
157 */
158 getmicrouptime(&stats->io_attachtime);
159
160 /*
161 * Link into the drivelist.
162 */
163 lockmgr(&iostatlist_lock, LK_EXCLUSIVE, NULL);
164 TAILQ_INSERT_TAIL(&iostatlist, stats, io_link);
165 iostat_count++;
166 lockmgr(&iostatlist_lock, LK_RELEASE, NULL);
167
168 return stats;
169 }
170
171 /*
172 * Remove i/o from stats collection.
173 */
174 void
175 iostat_free(struct io_stats *stats)
176 {
177
178 /*
179 * Remove from the iostat list.
180 */
181 if (iostat_count == 0)
182 panic("iostat_free: iostat_count == 0");
183 lockmgr(&iostatlist_lock, LK_EXCLUSIVE, NULL);
184 TAILQ_REMOVE(&iostatlist, stats, io_link);
185 iostat_count--;
186 lockmgr(&iostatlist_lock, LK_RELEASE, NULL);
187 free(stats, M_DEVBUF);
188 }
189
190 /*
191 * Increment a iostat busy counter. If the counter is going from
192 * 0 to 1, set the timestamp.
193 */
194 void
195 iostat_busy(struct io_stats *stats)
196 {
197
198 if (stats->io_busy++ == 0)
199 getmicrouptime(&stats->io_timestamp);
200 }
201
202 /*
203 * Decrement a iostat busy counter, increment the byte count, total busy
204 * time, and reset the timestamp.
205 */
206 void
207 iostat_unbusy(struct io_stats *stats, long bcount, int read)
208 {
209 struct timeval dv_time, diff_time;
210
211 if (stats->io_busy-- == 0) {
212 printf("%s: busy < 0\n", stats->io_name);
213 panic("iostat_unbusy");
214 }
215
216 getmicrouptime(&dv_time);
217
218 timersub(&dv_time, &stats->io_timestamp, &diff_time);
219 timeradd(&stats->io_time, &diff_time, &stats->io_time);
220
221 stats->io_timestamp = dv_time;
222 if (bcount > 0) {
223 if (read) {
224 stats->io_rbytes += bcount;
225 stats->io_rxfer++;
226 } else {
227 stats->io_wbytes += bcount;
228 stats->io_wxfer++;
229 }
230 }
231 }
232
233 /*
234 * Increment the seek counter. This does look almost redundant but it
235 * abstracts the stats gathering.
236 */
237 void
238 iostat_seek(struct io_stats *stats)
239 {
240
241 stats->io_seek++;
242 }
243
244 static int
245 sysctl_hw_disknames(SYSCTLFN_ARGS)
246 {
247
248 return iostati_getnames(1, oldp, oldlenp, newp, namelen);
249 }
250
251 static int
252 sysctl_hw_iostatnames(SYSCTLFN_ARGS)
253 {
254
255 return iostati_getnames(0, oldp, oldlenp, newp, namelen);
256 }
257
258 static int
259 iostati_getnames(int disk_only, char *oldp, size_t *oldlenp, const void *newp,
260 u_int namelen)
261 {
262 char bf[IOSTATNAMELEN + 1];
263 char *where = oldp;
264 struct io_stats *stats;
265 size_t needed, left, slen;
266 int error, first;
267
268 if (newp != NULL)
269 return (EPERM);
270 if (namelen != 0)
271 return (EINVAL);
272
273 first = 1;
274 error = 0;
275 needed = 0;
276 left = *oldlenp;
277
278 lockmgr(&iostatlist_lock, LK_SHARED, NULL);
279 for (stats = TAILQ_FIRST(&iostatlist); stats != NULL;
280 stats = TAILQ_NEXT(stats, io_link)) {
281 if ((disk_only == 1) && (stats->io_type != IOSTAT_DISK))
282 continue;
283
284 if (where == NULL)
285 needed += strlen(stats->io_name) + 1;
286 else {
287 memset(bf, 0, sizeof(bf));
288 if (first) {
289 strncpy(bf, stats->io_name, sizeof(bf));
290 first = 0;
291 } else {
292 bf[0] = ' ';
293 strncpy(bf + 1, stats->io_name,
294 sizeof(bf) - 1);
295 }
296 bf[IOSTATNAMELEN] = '\0';
297 slen = strlen(bf);
298 if (left < slen + 1)
299 break;
300 /* +1 to copy out the trailing NUL byte */
301 error = copyout(bf, where, slen + 1);
302 if (error)
303 break;
304 where += slen;
305 needed += slen;
306 left -= slen;
307 }
308 }
309 lockmgr(&iostatlist_lock, LK_RELEASE, NULL);
310 *oldlenp = needed;
311 return (error);
312 }
313
314 static int
315 sysctl_hw_iostats(SYSCTLFN_ARGS)
316 {
317 struct io_sysctl sdrive;
318 struct io_stats *stats;
319 char *where = oldp;
320 size_t tocopy, left;
321 int error;
322
323 if (newp != NULL)
324 return (EPERM);
325
326 /*
327 * The original hw.diskstats call was broken and did not require
328 * the userland to pass in it's size of struct disk_sysctl. This
329 * was fixed after NetBSD 1.6 was released, and any applications
330 * that do not pass in the size are given an error only, unless
331 * we care about 1.6 compatibility.
332 */
333 if (namelen == 0)
334 #ifdef COMPAT_16
335 tocopy = offsetof(struct io_sysctl, busy);
336 #else
337 return (EINVAL);
338 #endif
339 else
340 tocopy = name[0];
341
342 if (where == NULL) {
343 *oldlenp = iostat_count * tocopy;
344 return (0);
345 }
346
347 error = 0;
348 left = *oldlenp;
349 memset(&sdrive, 0, sizeof(sdrive));
350 *oldlenp = 0;
351
352 lockmgr(&iostatlist_lock, LK_SHARED, NULL);
353 TAILQ_FOREACH(stats, &iostatlist, io_link) {
354 if (left < tocopy)
355 break;
356 strncpy(sdrive.name, stats->io_name, sizeof(sdrive.name));
357 sdrive.xfer = stats->io_rxfer + stats->io_wxfer;
358 sdrive.rxfer = stats->io_rxfer;
359 sdrive.wxfer = stats->io_wxfer;
360 sdrive.seek = stats->io_seek;
361 sdrive.bytes = stats->io_rbytes + stats->io_wbytes;
362 sdrive.rbytes = stats->io_rbytes;
363 sdrive.wbytes = stats->io_wbytes;
364 sdrive.attachtime_sec = stats->io_attachtime.tv_sec;
365 sdrive.attachtime_usec = stats->io_attachtime.tv_usec;
366 sdrive.timestamp_sec = stats->io_timestamp.tv_sec;
367 sdrive.timestamp_usec = stats->io_timestamp.tv_usec;
368 sdrive.time_sec = stats->io_time.tv_sec;
369 sdrive.time_usec = stats->io_time.tv_usec;
370 sdrive.busy = stats->io_busy;
371
372 error = copyout(&sdrive, where, min(tocopy, sizeof(sdrive)));
373 if (error)
374 break;
375 where += tocopy;
376 *oldlenp += tocopy;
377 left -= tocopy;
378 }
379 lockmgr(&iostatlist_lock, LK_RELEASE, NULL);
380 return (error);
381 }
382
383 SYSCTL_SETUP(sysctl_io_stats_setup, "sysctl i/o stats setup")
384 {
385
386 sysctl_createv(clog, 0, NULL, NULL,
387 CTLFLAG_PERMANENT,
388 CTLTYPE_STRING, "disknames",
389 SYSCTL_DESCR("List of disk drives present"),
390 sysctl_hw_disknames, 0, NULL, 0,
391 CTL_HW, HW_DISKNAMES, CTL_EOL);
392 sysctl_createv(clog, 0, NULL, NULL,
393 CTLFLAG_PERMANENT,
394 CTLTYPE_STRING, "iostatnames",
395 SYSCTL_DESCR("I/O stats are being collected for these"
396 " devices"),
397 sysctl_hw_iostatnames, 0, NULL, 0,
398 CTL_HW, HW_IOSTATNAMES, CTL_EOL);
399 sysctl_createv(clog, 0, NULL, NULL,
400 CTLFLAG_PERMANENT,
401 CTLTYPE_STRUCT, "iostats",
402 SYSCTL_DESCR("Statistics on device I/O operations"),
403 sysctl_hw_iostats, 0, NULL, 0,
404 CTL_HW, HW_IOSTATS, CTL_EOL);
405 }
406