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