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