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