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