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