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