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