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