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