1 /* 2 * Copyright (c) 1997, 1998 Kenneth D. Merry. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include <sys/types.h> 33 #include <sys/sysctl.h> 34 #include <sys/errno.h> 35 #include <sys/dkstat.h> 36 #include <sys/queue.h> 37 38 #include <ctype.h> 39 #include <err.h> 40 #include <fcntl.h> 41 #include <limits.h> 42 #include <stdio.h> 43 #include <stdlib.h> 44 #include <string.h> 45 #include <stdarg.h> 46 #include <kvm.h> 47 48 #include "devstat.h" 49 50 typedef enum { 51 DEVSTAT_ARG_NOTYPE, 52 DEVSTAT_ARG_UINT64, 53 DEVSTAT_ARG_LD, 54 DEVSTAT_ARG_SKIP 55 } devstat_arg_type; 56 57 char devstat_errbuf[DEVSTAT_ERRBUF_SIZE]; 58 59 /* 60 * Table to match descriptive strings with device types. These are in 61 * order from most common to least common to speed search time. 62 */ 63 struct devstat_match_table match_table[] = { 64 {"da", DEVSTAT_TYPE_DIRECT, DEVSTAT_MATCH_TYPE}, 65 {"cd", DEVSTAT_TYPE_CDROM, DEVSTAT_MATCH_TYPE}, 66 {"scsi", DEVSTAT_TYPE_IF_SCSI, DEVSTAT_MATCH_IF}, 67 {"ide", DEVSTAT_TYPE_IF_IDE, DEVSTAT_MATCH_IF}, 68 {"other", DEVSTAT_TYPE_IF_OTHER, DEVSTAT_MATCH_IF}, 69 {"worm", DEVSTAT_TYPE_WORM, DEVSTAT_MATCH_TYPE}, 70 {"sa", DEVSTAT_TYPE_SEQUENTIAL,DEVSTAT_MATCH_TYPE}, 71 {"pass", DEVSTAT_TYPE_PASS, DEVSTAT_MATCH_PASS}, 72 {"optical", DEVSTAT_TYPE_OPTICAL, DEVSTAT_MATCH_TYPE}, 73 {"array", DEVSTAT_TYPE_STORARRAY, DEVSTAT_MATCH_TYPE}, 74 {"changer", DEVSTAT_TYPE_CHANGER, DEVSTAT_MATCH_TYPE}, 75 {"scanner", DEVSTAT_TYPE_SCANNER, DEVSTAT_MATCH_TYPE}, 76 {"printer", DEVSTAT_TYPE_PRINTER, DEVSTAT_MATCH_TYPE}, 77 {"floppy", DEVSTAT_TYPE_FLOPPY, DEVSTAT_MATCH_TYPE}, 78 {"proc", DEVSTAT_TYPE_PROCESSOR, DEVSTAT_MATCH_TYPE}, 79 {"comm", DEVSTAT_TYPE_COMM, DEVSTAT_MATCH_TYPE}, 80 {"enclosure", DEVSTAT_TYPE_ENCLOSURE, DEVSTAT_MATCH_TYPE}, 81 {NULL, 0, 0} 82 }; 83 84 struct devstat_args { 85 devstat_metric metric; 86 devstat_arg_type argtype; 87 } devstat_arg_list[] = { 88 { DSM_NONE, DEVSTAT_ARG_NOTYPE }, 89 { DSM_TOTAL_BYTES, DEVSTAT_ARG_UINT64 }, 90 { DSM_TOTAL_BYTES_READ, DEVSTAT_ARG_UINT64 }, 91 { DSM_TOTAL_BYTES_WRITE, DEVSTAT_ARG_UINT64 }, 92 { DSM_TOTAL_TRANSFERS, DEVSTAT_ARG_UINT64 }, 93 { DSM_TOTAL_TRANSFERS_READ, DEVSTAT_ARG_UINT64 }, 94 { DSM_TOTAL_TRANSFERS_WRITE, DEVSTAT_ARG_UINT64 }, 95 { DSM_TOTAL_TRANSFERS_OTHER, DEVSTAT_ARG_UINT64 }, 96 { DSM_TOTAL_BLOCKS, DEVSTAT_ARG_UINT64 }, 97 { DSM_TOTAL_BLOCKS_READ, DEVSTAT_ARG_UINT64 }, 98 { DSM_TOTAL_BLOCKS_WRITE, DEVSTAT_ARG_UINT64 }, 99 { DSM_KB_PER_TRANSFER, DEVSTAT_ARG_LD }, 100 { DSM_KB_PER_TRANSFER_READ, DEVSTAT_ARG_LD }, 101 { DSM_KB_PER_TRANSFER_WRITE, DEVSTAT_ARG_LD }, 102 { DSM_TRANSFERS_PER_SECOND, DEVSTAT_ARG_LD }, 103 { DSM_TRANSFERS_PER_SECOND_READ, DEVSTAT_ARG_LD }, 104 { DSM_TRANSFERS_PER_SECOND_WRITE, DEVSTAT_ARG_LD }, 105 { DSM_TRANSFERS_PER_SECOND_OTHER, DEVSTAT_ARG_LD }, 106 { DSM_MB_PER_SECOND, DEVSTAT_ARG_LD }, 107 { DSM_MB_PER_SECOND_READ, DEVSTAT_ARG_LD }, 108 { DSM_MB_PER_SECOND_WRITE, DEVSTAT_ARG_LD }, 109 { DSM_BLOCKS_PER_SECOND, DEVSTAT_ARG_LD }, 110 { DSM_BLOCKS_PER_SECOND_READ, DEVSTAT_ARG_LD }, 111 { DSM_BLOCKS_PER_SECOND_WRITE, DEVSTAT_ARG_LD }, 112 { DSM_MS_PER_TRANSACTION, DEVSTAT_ARG_LD }, 113 { DSM_MS_PER_TRANSACTION_READ, DEVSTAT_ARG_LD }, 114 { DSM_MS_PER_TRANSACTION_WRITE, DEVSTAT_ARG_LD }, 115 { DSM_SKIP, DEVSTAT_ARG_SKIP } 116 }; 117 118 static char *namelist[] = { 119 #define X_NUMDEVS 0 120 "_devstat_num_devs", 121 #define X_GENERATION 1 122 "_devstat_generation", 123 #define X_VERSION 2 124 "_devstat_version", 125 #define X_DEVICE_STATQ 3 126 "_device_statq", 127 #define X_END 4 128 }; 129 130 /* 131 * Local function declarations. 132 */ 133 static int compare_select(const void *arg1, const void *arg2); 134 static int readkmem(kvm_t *kd, unsigned long addr, void *buf, size_t nbytes); 135 static int readkmem_nl(kvm_t *kd, char *name, void *buf, size_t nbytes); 136 static char *get_devstat_kvm(kvm_t *kd); 137 138 #define KREADNL(kd, var, val) \ 139 readkmem_nl(kd, namelist[var], &val, sizeof(val)) 140 141 int 142 devstat_getnumdevs(kvm_t *kd) 143 { 144 size_t numdevsize; 145 int numdevs; 146 char *func_name = "devstat_getnumdevs"; 147 148 numdevsize = sizeof(int); 149 150 /* 151 * Find out how many devices we have in the system. 152 */ 153 if (kd == NULL) { 154 if (sysctlbyname("kern.devstat.numdevs", &numdevs, 155 &numdevsize, NULL, 0) == -1) { 156 snprintf(devstat_errbuf, sizeof(devstat_errbuf), 157 "%s: error getting number of devices\n" 158 "%s: %s", func_name, func_name, 159 strerror(errno)); 160 return(-1); 161 } else 162 return(numdevs); 163 } else { 164 if (KREADNL(kd, X_NUMDEVS, numdevs) == -1) 165 return(-1); 166 else 167 return(numdevs); 168 } 169 } 170 171 /* 172 * This is an easy way to get the generation number, but the generation is 173 * supplied in a more atmoic manner by the kern.devstat.all sysctl. 174 * Because this generation sysctl is separate from the statistics sysctl, 175 * the device list and the generation could change between the time that 176 * this function is called and the device list is retreived. 177 */ 178 long 179 devstat_getgeneration(kvm_t *kd) 180 { 181 size_t gensize; 182 long generation; 183 char *func_name = "devstat_getgeneration"; 184 185 gensize = sizeof(long); 186 187 /* 188 * Get the current generation number. 189 */ 190 if (kd == NULL) { 191 if (sysctlbyname("kern.devstat.generation", &generation, 192 &gensize, NULL, 0) == -1) { 193 snprintf(devstat_errbuf, sizeof(devstat_errbuf), 194 "%s: error getting devstat generation\n%s: %s", 195 func_name, func_name, strerror(errno)); 196 return(-1); 197 } else 198 return(generation); 199 } else { 200 if (KREADNL(kd, X_GENERATION, generation) == -1) 201 return(-1); 202 else 203 return(generation); 204 } 205 } 206 207 /* 208 * Get the current devstat version. The return value of this function 209 * should be compared with DEVSTAT_VERSION, which is defined in 210 * sys/devicestat.h. This will enable userland programs to determine 211 * whether they are out of sync with the kernel. 212 */ 213 int 214 devstat_getversion(kvm_t *kd) 215 { 216 size_t versize; 217 int version; 218 char *func_name = "devstat_getversion"; 219 220 versize = sizeof(int); 221 222 /* 223 * Get the current devstat version. 224 */ 225 if (kd == NULL) { 226 if (sysctlbyname("kern.devstat.version", &version, &versize, 227 NULL, 0) == -1) { 228 snprintf(devstat_errbuf, sizeof(devstat_errbuf), 229 "%s: error getting devstat version\n%s: %s", 230 func_name, func_name, strerror(errno)); 231 return(-1); 232 } else 233 return(version); 234 } else { 235 if (KREADNL(kd, X_VERSION, version) == -1) 236 return(-1); 237 else 238 return(version); 239 } 240 } 241 242 /* 243 * Check the devstat version we know about against the devstat version the 244 * kernel knows about. If they don't match, print an error into the 245 * devstat error buffer, and return -1. If they match, return 0. 246 */ 247 int 248 devstat_checkversion(kvm_t *kd) 249 { 250 char *func_name = "devstat_checkversion"; 251 int buflen, res, retval = 0, version; 252 253 version = devstat_getversion(kd); 254 255 if (version != DEVSTAT_VERSION) { 256 /* 257 * If getversion() returns an error (i.e. -1), then it 258 * has printed an error message in the buffer. Therefore, 259 * we need to add a \n to the end of that message before we 260 * print our own message in the buffer. 261 */ 262 if (version == -1) 263 buflen = strlen(devstat_errbuf); 264 else 265 buflen = 0; 266 267 res = snprintf(devstat_errbuf + buflen, 268 DEVSTAT_ERRBUF_SIZE - buflen, 269 "%s%s: userland devstat version %d is not " 270 "the same as the kernel\n%s: devstat " 271 "version %d\n", version == -1 ? "\n" : "", 272 func_name, DEVSTAT_VERSION, func_name, version); 273 274 if (res < 0) 275 devstat_errbuf[buflen] = '\0'; 276 277 buflen = strlen(devstat_errbuf); 278 if (version < DEVSTAT_VERSION) 279 res = snprintf(devstat_errbuf + buflen, 280 DEVSTAT_ERRBUF_SIZE - buflen, 281 "%s: libdevstat newer than kernel\n", 282 func_name); 283 else 284 res = snprintf(devstat_errbuf + buflen, 285 DEVSTAT_ERRBUF_SIZE - buflen, 286 "%s: kernel newer than libdevstat\n", 287 func_name); 288 289 if (res < 0) 290 devstat_errbuf[buflen] = '\0'; 291 292 retval = -1; 293 } 294 295 return(retval); 296 } 297 298 /* 299 * Get the current list of devices and statistics, and the current 300 * generation number. 301 * 302 * Return values: 303 * -1 -- error 304 * 0 -- device list is unchanged 305 * 1 -- device list has changed 306 */ 307 int 308 devstat_getdevs(kvm_t *kd, struct statinfo *stats) 309 { 310 int error; 311 size_t dssize; 312 int oldnumdevs; 313 long oldgeneration; 314 int retval = 0; 315 struct devinfo *dinfo; 316 char *func_name = "devstat_getdevs"; 317 318 dinfo = stats->dinfo; 319 320 if (dinfo == NULL) { 321 snprintf(devstat_errbuf, sizeof(devstat_errbuf), 322 "%s: stats->dinfo was NULL", func_name); 323 return(-1); 324 } 325 326 oldnumdevs = dinfo->numdevs; 327 oldgeneration = dinfo->generation; 328 329 /* Get the current time when we get the stats */ 330 gettimeofday(&stats->busy_time, NULL); 331 332 if (kd == NULL) { 333 /* If this is our first time through, mem_ptr will be null. */ 334 if (dinfo->mem_ptr == NULL) { 335 /* 336 * Get the number of devices. If it's negative, it's an 337 * error. Don't bother setting the error string, since 338 * getnumdevs() has already done that for us. 339 */ 340 if ((dinfo->numdevs = getnumdevs()) < 0) 341 return(-1); 342 343 /* 344 * The kern.devstat.all sysctl returns the current 345 * generation number, as well as all the devices. 346 * So we need four bytes more. 347 */ 348 dssize = (dinfo->numdevs * sizeof(struct devstat)) + 349 sizeof(long); 350 dinfo->mem_ptr = (u_int8_t *)malloc(dssize); 351 } else 352 dssize = (dinfo->numdevs * sizeof(struct devstat)) + 353 sizeof(long); 354 355 /* 356 * Request all of the devices. We only really allow for one 357 * ENOMEM failure. It would, of course, be possible to just go 358 * in a loop and keep reallocing the device structure until we 359 * don't get ENOMEM back. I'm not sure it's worth it, though. 360 * If devices are being added to the system that quickly, maybe 361 * the user can just wait until all devices are added. 362 */ 363 if ((error = sysctlbyname("kern.devstat.all", dinfo->mem_ptr, 364 &dssize, NULL, 0)) == -1) { 365 /* 366 * If we get ENOMEM back, that means that there are 367 * more devices now, so we need to allocate more 368 * space for the device array. 369 */ 370 if (errno == ENOMEM) { 371 /* 372 * No need to set the error string here, 373 * getnumdevs() will do that if it fails. 374 */ 375 if ((dinfo->numdevs = getnumdevs()) < 0) 376 return(-1); 377 378 dssize = (dinfo->numdevs * 379 sizeof(struct devstat)) + sizeof(long); 380 dinfo->mem_ptr = (u_int8_t *) 381 realloc(dinfo->mem_ptr, dssize); 382 if ((error = sysctlbyname("kern.devstat.all", 383 dinfo->mem_ptr, &dssize, NULL, 0)) == -1) { 384 snprintf(devstat_errbuf, 385 sizeof(devstat_errbuf), 386 "%s: error getting device " 387 "stats\n%s: %s", func_name, 388 func_name, strerror(errno)); 389 return(-1); 390 } 391 } else { 392 snprintf(devstat_errbuf, sizeof(devstat_errbuf), 393 "%s: error getting device stats\n" 394 "%s: %s", func_name, func_name, 395 strerror(errno)); 396 return(-1); 397 } 398 } 399 400 } else { 401 /* 402 * This is of course non-atomic, but since we are working 403 * on a core dump, the generation is unlikely to change 404 */ 405 if ((dinfo->numdevs = getnumdevs()) == -1) 406 return(-1); 407 if ((dinfo->mem_ptr = get_devstat_kvm(kd)) == NULL) 408 return(-1); 409 } 410 /* 411 * The sysctl spits out the generation as the first four bytes, 412 * then all of the device statistics structures. 413 */ 414 dinfo->generation = *(long *)dinfo->mem_ptr; 415 416 /* 417 * If the generation has changed, and if the current number of 418 * devices is not the same as the number of devices recorded in the 419 * devinfo structure, it is likely that the device list has shrunk. 420 * The reason that it is likely that the device list has shrunk in 421 * this case is that if the device list has grown, the sysctl above 422 * will return an ENOMEM error, and we will reset the number of 423 * devices and reallocate the device array. If the second sysctl 424 * fails, we will return an error and therefore never get to this 425 * point. If the device list has shrunk, the sysctl will not 426 * return an error since we have more space allocated than is 427 * necessary. So, in the shrinkage case, we catch it here and 428 * reallocate the array so that we don't use any more space than is 429 * necessary. 430 */ 431 if (oldgeneration != dinfo->generation) { 432 if (getnumdevs() != dinfo->numdevs) { 433 if ((dinfo->numdevs = getnumdevs()) < 0) 434 return(-1); 435 dssize = (dinfo->numdevs * sizeof(struct devstat)) + 436 sizeof(long); 437 dinfo->mem_ptr = (u_int8_t *)realloc(dinfo->mem_ptr, 438 dssize); 439 } 440 retval = 1; 441 } 442 443 dinfo->devices = (struct devstat *)(dinfo->mem_ptr + sizeof(long)); 444 445 return(retval); 446 } 447 448 /* 449 * selectdevs(): 450 * 451 * Devices are selected/deselected based upon the following criteria: 452 * - devices specified by the user on the command line 453 * - devices matching any device type expressions given on the command line 454 * - devices with the highest I/O, if 'top' mode is enabled 455 * - the first n unselected devices in the device list, if maxshowdevs 456 * devices haven't already been selected and if the user has not 457 * specified any devices on the command line and if we're in "add" mode. 458 * 459 * Input parameters: 460 * - device selection list (dev_select) 461 * - current number of devices selected (num_selected) 462 * - total number of devices in the selection list (num_selections) 463 * - devstat generation as of the last time selectdevs() was called 464 * (select_generation) 465 * - current devstat generation (current_generation) 466 * - current list of devices and statistics (devices) 467 * - number of devices in the current device list (numdevs) 468 * - compiled version of the command line device type arguments (matches) 469 * - This is optional. If the number of devices is 0, this will be ignored. 470 * - The matching code pays attention to the current selection mode. So 471 * if you pass in a matching expression, it will be evaluated based 472 * upon the selection mode that is passed in. See below for details. 473 * - number of device type matching expressions (num_matches) 474 * - Set to 0 to disable the matching code. 475 * - list of devices specified on the command line by the user (dev_selections) 476 * - number of devices selected on the command line by the user 477 * (num_dev_selections) 478 * - Our selection mode. There are four different selection modes: 479 * - add mode. (DS_SELECT_ADD) Any devices matching devices explicitly 480 * selected by the user or devices matching a pattern given by the 481 * user will be selected in addition to devices that are already 482 * selected. Additional devices will be selected, up to maxshowdevs 483 * number of devices. 484 * - only mode. (DS_SELECT_ONLY) Only devices matching devices 485 * explicitly given by the user or devices matching a pattern 486 * given by the user will be selected. No other devices will be 487 * selected. 488 * - addonly mode. (DS_SELECT_ADDONLY) This is similar to add and 489 * only. Basically, this will not de-select any devices that are 490 * current selected, as only mode would, but it will also not 491 * gratuitously select up to maxshowdevs devices as add mode would. 492 * - remove mode. (DS_SELECT_REMOVE) Any devices matching devices 493 * explicitly selected by the user or devices matching a pattern 494 * given by the user will be de-selected. 495 * - maximum number of devices we can select (maxshowdevs) 496 * - flag indicating whether or not we're in 'top' mode (perf_select) 497 * 498 * Output data: 499 * - the device selection list may be modified and passed back out 500 * - the number of devices selected and the total number of items in the 501 * device selection list may be changed 502 * - the selection generation may be changed to match the current generation 503 * 504 * Return values: 505 * -1 -- error 506 * 0 -- selected devices are unchanged 507 * 1 -- selected devices changed 508 */ 509 int 510 devstat_selectdevs(struct device_selection **dev_select, int *num_selected, 511 int *num_selections, long *select_generation, 512 long current_generation, struct devstat *devices, 513 int numdevs, struct devstat_match *matches, int num_matches, 514 char **dev_selections, int num_dev_selections, 515 devstat_select_mode select_mode, int maxshowdevs, 516 int perf_select) 517 { 518 register int i, j, k; 519 int init_selections = 0, init_selected_var = 0; 520 struct device_selection *old_dev_select = NULL; 521 int old_num_selections = 0, old_num_selected; 522 int selection_number = 0; 523 int changed = 0, found = 0; 524 525 if ((dev_select == NULL) || (devices == NULL) || (numdevs <= 0)) 526 return(-1); 527 528 /* 529 * We always want to make sure that we have as many dev_select 530 * entries as there are devices. 531 */ 532 /* 533 * In this case, we haven't selected devices before. 534 */ 535 if (*dev_select == NULL) { 536 *dev_select = (struct device_selection *)malloc(numdevs * 537 sizeof(struct device_selection)); 538 *select_generation = current_generation; 539 init_selections = 1; 540 changed = 1; 541 /* 542 * In this case, we have selected devices before, but the device 543 * list has changed since we last selected devices, so we need to 544 * either enlarge or reduce the size of the device selection list. 545 */ 546 } else if (*num_selections != numdevs) { 547 *dev_select = (struct device_selection *)realloc(*dev_select, 548 numdevs * sizeof(struct device_selection)); 549 *select_generation = current_generation; 550 init_selections = 1; 551 /* 552 * In this case, we've selected devices before, and the selection 553 * list is the same size as it was the last time, but the device 554 * list has changed. 555 */ 556 } else if (*select_generation < current_generation) { 557 *select_generation = current_generation; 558 init_selections = 1; 559 } 560 561 /* 562 * If we're in "only" mode, we want to clear out the selected 563 * variable since we're going to select exactly what the user wants 564 * this time through. 565 */ 566 if (select_mode == DS_SELECT_ONLY) 567 init_selected_var = 1; 568 569 /* 570 * In all cases, we want to back up the number of selected devices. 571 * It is a quick and accurate way to determine whether the selected 572 * devices have changed. 573 */ 574 old_num_selected = *num_selected; 575 576 /* 577 * We want to make a backup of the current selection list if 578 * the list of devices has changed, or if we're in performance 579 * selection mode. In both cases, we don't want to make a backup 580 * if we already know for sure that the list will be different. 581 * This is certainly the case if this is our first time through the 582 * selection code. 583 */ 584 if (((init_selected_var != 0) || (init_selections != 0) 585 || (perf_select != 0)) && (changed == 0)){ 586 old_dev_select = (struct device_selection *)malloc( 587 *num_selections * sizeof(struct device_selection)); 588 old_num_selections = *num_selections; 589 bcopy(*dev_select, old_dev_select, 590 sizeof(struct device_selection) * *num_selections); 591 } 592 593 if (init_selections != 0) { 594 bzero(*dev_select, sizeof(struct device_selection) * numdevs); 595 596 for (i = 0; i < numdevs; i++) { 597 (*dev_select)[i].device_number = 598 devices[i].device_number; 599 strncpy((*dev_select)[i].device_name, 600 devices[i].device_name, 601 DEVSTAT_NAME_LEN); 602 (*dev_select)[i].device_name[DEVSTAT_NAME_LEN - 1]='\0'; 603 (*dev_select)[i].unit_number = devices[i].unit_number; 604 (*dev_select)[i].position = i; 605 } 606 *num_selections = numdevs; 607 } else if (init_selected_var != 0) { 608 for (i = 0; i < numdevs; i++) 609 (*dev_select)[i].selected = 0; 610 } 611 612 /* we haven't gotten around to selecting anything yet.. */ 613 if ((select_mode == DS_SELECT_ONLY) || (init_selections != 0) 614 || (init_selected_var != 0)) 615 *num_selected = 0; 616 617 /* 618 * Look through any devices the user specified on the command line 619 * and see if they match known devices. If so, select them. 620 */ 621 for (i = 0; (i < *num_selections) && (num_dev_selections > 0); i++) { 622 char tmpstr[80]; 623 624 snprintf(tmpstr, sizeof(tmpstr), "%s%d", 625 (*dev_select)[i].device_name, 626 (*dev_select)[i].unit_number); 627 for (j = 0; j < num_dev_selections; j++) { 628 if (strcmp(tmpstr, dev_selections[j]) == 0) { 629 /* 630 * Here we do different things based on the 631 * mode we're in. If we're in add or 632 * addonly mode, we only select this device 633 * if it hasn't already been selected. 634 * Otherwise, we would be unnecessarily 635 * changing the selection order and 636 * incrementing the selection count. If 637 * we're in only mode, we unconditionally 638 * select this device, since in only mode 639 * any previous selections are erased and 640 * manually specified devices are the first 641 * ones to be selected. If we're in remove 642 * mode, we de-select the specified device and 643 * decrement the selection count. 644 */ 645 switch(select_mode) { 646 case DS_SELECT_ADD: 647 case DS_SELECT_ADDONLY: 648 if ((*dev_select)[i].selected) 649 break; 650 /* FALLTHROUGH */ 651 case DS_SELECT_ONLY: 652 (*dev_select)[i].selected = 653 ++selection_number; 654 (*num_selected)++; 655 break; 656 case DS_SELECT_REMOVE: 657 (*dev_select)[i].selected = 0; 658 (*num_selected)--; 659 /* 660 * This isn't passed back out, we 661 * just use it to keep track of 662 * how many devices we've removed. 663 */ 664 num_dev_selections--; 665 break; 666 } 667 break; 668 } 669 } 670 } 671 672 /* 673 * Go through the user's device type expressions and select devices 674 * accordingly. We only do this if the number of devices already 675 * selected is less than the maximum number we can show. 676 */ 677 for (i = 0; (i < num_matches) && (*num_selected < maxshowdevs); i++) { 678 /* We should probably indicate some error here */ 679 if ((matches[i].match_fields == DEVSTAT_MATCH_NONE) 680 || (matches[i].num_match_categories <= 0)) 681 continue; 682 683 for (j = 0; j < numdevs; j++) { 684 int num_match_categories; 685 686 num_match_categories = matches[i].num_match_categories; 687 688 /* 689 * Determine whether or not the current device 690 * matches the given matching expression. This if 691 * statement consists of three components: 692 * - the device type check 693 * - the device interface check 694 * - the passthrough check 695 * If a the matching test is successful, it 696 * decrements the number of matching categories, 697 * and if we've reached the last element that 698 * needed to be matched, the if statement succeeds. 699 * 700 */ 701 if ((((matches[i].match_fields & DEVSTAT_MATCH_TYPE)!=0) 702 && ((devices[j].device_type & DEVSTAT_TYPE_MASK) == 703 (matches[i].device_type & DEVSTAT_TYPE_MASK)) 704 &&(((matches[i].match_fields & DEVSTAT_MATCH_PASS)!=0) 705 || (((matches[i].match_fields & 706 DEVSTAT_MATCH_PASS) == 0) 707 && ((devices[j].device_type & 708 DEVSTAT_TYPE_PASS) == 0))) 709 && (--num_match_categories == 0)) 710 || (((matches[i].match_fields & DEVSTAT_MATCH_IF) != 0) 711 && ((devices[j].device_type & DEVSTAT_TYPE_IF_MASK) == 712 (matches[i].device_type & DEVSTAT_TYPE_IF_MASK)) 713 &&(((matches[i].match_fields & DEVSTAT_MATCH_PASS)!=0) 714 || (((matches[i].match_fields & 715 DEVSTAT_MATCH_PASS) == 0) 716 && ((devices[j].device_type & 717 DEVSTAT_TYPE_PASS) == 0))) 718 && (--num_match_categories == 0)) 719 || (((matches[i].match_fields & DEVSTAT_MATCH_PASS)!=0) 720 && ((devices[j].device_type & DEVSTAT_TYPE_PASS) != 0) 721 && (--num_match_categories == 0))) { 722 723 /* 724 * This is probably a non-optimal solution 725 * to the problem that the devices in the 726 * device list will not be in the same 727 * order as the devices in the selection 728 * array. 729 */ 730 for (k = 0; k < numdevs; k++) { 731 if ((*dev_select)[k].position == j) { 732 found = 1; 733 break; 734 } 735 } 736 737 /* 738 * There shouldn't be a case where a device 739 * in the device list is not in the 740 * selection list...but it could happen. 741 */ 742 if (found != 1) { 743 fprintf(stderr, "selectdevs: couldn't" 744 " find %s%d in selection " 745 "list\n", 746 devices[j].device_name, 747 devices[j].unit_number); 748 break; 749 } 750 751 /* 752 * We do different things based upon the 753 * mode we're in. If we're in add or only 754 * mode, we go ahead and select this device 755 * if it hasn't already been selected. If 756 * it has already been selected, we leave 757 * it alone so we don't mess up the 758 * selection ordering. Manually specified 759 * devices have already been selected, and 760 * they have higher priority than pattern 761 * matched devices. If we're in remove 762 * mode, we de-select the given device and 763 * decrement the selected count. 764 */ 765 switch(select_mode) { 766 case DS_SELECT_ADD: 767 case DS_SELECT_ADDONLY: 768 case DS_SELECT_ONLY: 769 if ((*dev_select)[k].selected != 0) 770 break; 771 (*dev_select)[k].selected = 772 ++selection_number; 773 (*num_selected)++; 774 break; 775 case DS_SELECT_REMOVE: 776 (*dev_select)[k].selected = 0; 777 (*num_selected)--; 778 break; 779 } 780 } 781 } 782 } 783 784 /* 785 * Here we implement "top" mode. Devices are sorted in the 786 * selection array based on two criteria: whether or not they are 787 * selected (not selection number, just the fact that they are 788 * selected!) and the number of bytes in the "bytes" field of the 789 * selection structure. The bytes field generally must be kept up 790 * by the user. In the future, it may be maintained by library 791 * functions, but for now the user has to do the work. 792 * 793 * At first glance, it may seem wrong that we don't go through and 794 * select every device in the case where the user hasn't specified 795 * any devices or patterns. In fact, though, it won't make any 796 * difference in the device sorting. In that particular case (i.e. 797 * when we're in "add" or "only" mode, and the user hasn't 798 * specified anything) the first time through no devices will be 799 * selected, so the only criterion used to sort them will be their 800 * performance. The second time through, and every time thereafter, 801 * all devices will be selected, so again selection won't matter. 802 */ 803 if (perf_select != 0) { 804 805 /* Sort the device array by throughput */ 806 qsort(*dev_select, *num_selections, 807 sizeof(struct device_selection), 808 compare_select); 809 810 if (*num_selected == 0) { 811 /* 812 * Here we select every device in the array, if it 813 * isn't already selected. Because the 'selected' 814 * variable in the selection array entries contains 815 * the selection order, the devstats routine can show 816 * the devices that were selected first. 817 */ 818 for (i = 0; i < *num_selections; i++) { 819 if ((*dev_select)[i].selected == 0) { 820 (*dev_select)[i].selected = 821 ++selection_number; 822 (*num_selected)++; 823 } 824 } 825 } else { 826 selection_number = 0; 827 for (i = 0; i < *num_selections; i++) { 828 if ((*dev_select)[i].selected != 0) { 829 (*dev_select)[i].selected = 830 ++selection_number; 831 } 832 } 833 } 834 } 835 836 /* 837 * If we're in the "add" selection mode and if we haven't already 838 * selected maxshowdevs number of devices, go through the array and 839 * select any unselected devices. If we're in "only" mode, we 840 * obviously don't want to select anything other than what the user 841 * specifies. If we're in "remove" mode, it probably isn't a good 842 * idea to go through and select any more devices, since we might 843 * end up selecting something that the user wants removed. Through 844 * more complicated logic, we could actually figure this out, but 845 * that would probably require combining this loop with the various 846 * selections loops above. 847 */ 848 if ((select_mode == DS_SELECT_ADD) && (*num_selected < maxshowdevs)) { 849 for (i = 0; i < *num_selections; i++) 850 if ((*dev_select)[i].selected == 0) { 851 (*dev_select)[i].selected = ++selection_number; 852 (*num_selected)++; 853 } 854 } 855 856 /* 857 * Look at the number of devices that have been selected. If it 858 * has changed, set the changed variable. Otherwise, if we've 859 * made a backup of the selection list, compare it to the current 860 * selection list to see if the selected devices have changed. 861 */ 862 if ((changed == 0) && (old_num_selected != *num_selected)) 863 changed = 1; 864 else if ((changed == 0) && (old_dev_select != NULL)) { 865 /* 866 * Now we go through the selection list and we look at 867 * it three different ways. 868 */ 869 for (i = 0; (i < *num_selections) && (changed == 0) && 870 (i < old_num_selections); i++) { 871 /* 872 * If the device at index i in both the new and old 873 * selection arrays has the same device number and 874 * selection status, it hasn't changed. We 875 * continue on to the next index. 876 */ 877 if (((*dev_select)[i].device_number == 878 old_dev_select[i].device_number) 879 && ((*dev_select)[i].selected == 880 old_dev_select[i].selected)) 881 continue; 882 883 /* 884 * Now, if we're still going through the if 885 * statement, the above test wasn't true. So we 886 * check here to see if the device at index i in 887 * the current array is the same as the device at 888 * index i in the old array. If it is, that means 889 * that its selection number has changed. Set 890 * changed to 1 and exit the loop. 891 */ 892 else if ((*dev_select)[i].device_number == 893 old_dev_select[i].device_number) { 894 changed = 1; 895 break; 896 } 897 /* 898 * If we get here, then the device at index i in 899 * the current array isn't the same device as the 900 * device at index i in the old array. 901 */ 902 else { 903 int found = 0; 904 905 /* 906 * Search through the old selection array 907 * looking for a device with the same 908 * device number as the device at index i 909 * in the current array. If the selection 910 * status is the same, then we mark it as 911 * found. If the selection status isn't 912 * the same, we break out of the loop. 913 * Since found isn't set, changed will be 914 * set to 1 below. 915 */ 916 for (j = 0; j < old_num_selections; j++) { 917 if (((*dev_select)[i].device_number == 918 old_dev_select[j].device_number) 919 && ((*dev_select)[i].selected == 920 old_dev_select[j].selected)){ 921 found = 1; 922 break; 923 } 924 else if ((*dev_select)[i].device_number 925 == old_dev_select[j].device_number) 926 break; 927 } 928 if (found == 0) 929 changed = 1; 930 } 931 } 932 } 933 if (old_dev_select != NULL) 934 free(old_dev_select); 935 936 return(changed); 937 } 938 939 /* 940 * Comparison routine for qsort() above. Note that the comparison here is 941 * backwards -- generally, it should return a value to indicate whether 942 * arg1 is <, =, or > arg2. Instead, it returns the opposite. The reason 943 * it returns the opposite is so that the selection array will be sorted in 944 * order of decreasing performance. We sort on two parameters. The first 945 * sort key is whether or not one or the other of the devices in question 946 * has been selected. If one of them has, and the other one has not, the 947 * selected device is automatically more important than the unselected 948 * device. If neither device is selected, we judge the devices based upon 949 * performance. 950 */ 951 static int 952 compare_select(const void *arg1, const void *arg2) 953 { 954 if ((((struct device_selection *)arg1)->selected) 955 && (((struct device_selection *)arg2)->selected == 0)) 956 return(-1); 957 else if ((((struct device_selection *)arg1)->selected == 0) 958 && (((struct device_selection *)arg2)->selected)) 959 return(1); 960 else if (((struct device_selection *)arg2)->bytes < 961 ((struct device_selection *)arg1)->bytes) 962 return(-1); 963 else if (((struct device_selection *)arg2)->bytes > 964 ((struct device_selection *)arg1)->bytes) 965 return(1); 966 else 967 return(0); 968 } 969 970 /* 971 * Take a string with the general format "arg1,arg2,arg3", and build a 972 * device matching expression from it. 973 */ 974 int 975 devstat_buildmatch(char *match_str, struct devstat_match **matches, 976 int *num_matches) 977 { 978 char *tstr[5]; 979 char **tempstr; 980 int num_args; 981 register int i, j; 982 char *func_name = "devstat_buildmatch"; 983 984 /* We can't do much without a string to parse */ 985 if (match_str == NULL) { 986 snprintf(devstat_errbuf, sizeof(devstat_errbuf), 987 "%s: no match expression", func_name); 988 return(-1); 989 } 990 991 /* 992 * Break the (comma delimited) input string out into separate strings. 993 */ 994 for (tempstr = tstr, num_args = 0; 995 (*tempstr = strsep(&match_str, ",")) != NULL && (num_args < 5); 996 num_args++) 997 if (**tempstr != '\0') 998 if (++tempstr >= &tstr[5]) 999 break; 1000 1001 /* The user gave us too many type arguments */ 1002 if (num_args > 3) { 1003 snprintf(devstat_errbuf, sizeof(devstat_errbuf), 1004 "%s: too many type arguments", func_name); 1005 return(-1); 1006 } 1007 1008 /* 1009 * Since you can't realloc a pointer that hasn't been malloced 1010 * first, we malloc first and then realloc. 1011 */ 1012 if (*num_matches == 0) 1013 *matches = (struct devstat_match *)malloc( 1014 sizeof(struct devstat_match)); 1015 else 1016 *matches = (struct devstat_match *)realloc(*matches, 1017 sizeof(struct devstat_match) * (*num_matches + 1)); 1018 1019 /* Make sure the current entry is clear */ 1020 bzero(&matches[0][*num_matches], sizeof(struct devstat_match)); 1021 1022 /* 1023 * Step through the arguments the user gave us and build a device 1024 * matching expression from them. 1025 */ 1026 for (i = 0; i < num_args; i++) { 1027 char *tempstr2, *tempstr3; 1028 1029 /* 1030 * Get rid of leading white space. 1031 */ 1032 tempstr2 = tstr[i]; 1033 while (isspace(*tempstr2) && (*tempstr2 != '\0')) 1034 tempstr2++; 1035 1036 /* 1037 * Get rid of trailing white space. 1038 */ 1039 tempstr3 = &tempstr2[strlen(tempstr2) - 1]; 1040 1041 while ((*tempstr3 != '\0') && (tempstr3 > tempstr2) 1042 && (isspace(*tempstr3))) { 1043 *tempstr3 = '\0'; 1044 tempstr3--; 1045 } 1046 1047 /* 1048 * Go through the match table comparing the user's 1049 * arguments to known device types, interfaces, etc. 1050 */ 1051 for (j = 0; match_table[j].match_str != NULL; j++) { 1052 /* 1053 * We do case-insensitive matching, in case someone 1054 * wants to enter "SCSI" instead of "scsi" or 1055 * something like that. Only compare as many 1056 * characters as are in the string in the match 1057 * table. This should help if someone tries to use 1058 * a super-long match expression. 1059 */ 1060 if (strncasecmp(tempstr2, match_table[j].match_str, 1061 strlen(match_table[j].match_str)) == 0) { 1062 /* 1063 * Make sure the user hasn't specified two 1064 * items of the same type, like "da" and 1065 * "cd". One device cannot be both. 1066 */ 1067 if (((*matches)[*num_matches].match_fields & 1068 match_table[j].match_field) != 0) { 1069 snprintf(devstat_errbuf, 1070 sizeof(devstat_errbuf), 1071 "%s: cannot have more than " 1072 "one match item in a single " 1073 "category", func_name); 1074 return(-1); 1075 } 1076 /* 1077 * If we've gotten this far, we have a 1078 * winner. Set the appropriate fields in 1079 * the match entry. 1080 */ 1081 (*matches)[*num_matches].match_fields |= 1082 match_table[j].match_field; 1083 (*matches)[*num_matches].device_type |= 1084 match_table[j].type; 1085 (*matches)[*num_matches].num_match_categories++; 1086 break; 1087 } 1088 } 1089 /* 1090 * We should have found a match in the above for loop. If 1091 * not, that means the user entered an invalid device type 1092 * or interface. 1093 */ 1094 if ((*matches)[*num_matches].num_match_categories != (i + 1)) { 1095 snprintf(devstat_errbuf, sizeof(devstat_errbuf), 1096 "%s: unknown match item \"%s\"", func_name, 1097 tstr[i]); 1098 return(-1); 1099 } 1100 } 1101 1102 (*num_matches)++; 1103 1104 return(0); 1105 } 1106 1107 /* 1108 * Compute a number of device statistics. Only one field is mandatory, and 1109 * that is "current". Everything else is optional. The caller passes in 1110 * pointers to variables to hold the various statistics he desires. If he 1111 * doesn't want a particular staistic, he should pass in a NULL pointer. 1112 * Return values: 1113 * 0 -- success 1114 * -1 -- failure 1115 */ 1116 int 1117 compute_stats(struct devstat *current, struct devstat *previous, 1118 long double etime, u_int64_t *total_bytes, 1119 u_int64_t *total_transfers, u_int64_t *total_blocks, 1120 long double *kb_per_transfer, long double *transfers_per_second, 1121 long double *mb_per_second, long double *blocks_per_second, 1122 long double *ms_per_transaction) 1123 { 1124 return(devstat_compute_statistics(current, previous, etime, 1125 total_bytes ? DSM_TOTAL_BYTES : DSM_SKIP, 1126 total_bytes, 1127 total_transfers ? DSM_TOTAL_TRANSFERS : DSM_SKIP, 1128 total_transfers, 1129 total_blocks ? DSM_TOTAL_BLOCKS : DSM_SKIP, 1130 total_blocks, 1131 kb_per_transfer ? DSM_KB_PER_TRANSFER : DSM_SKIP, 1132 kb_per_transfer, 1133 transfers_per_second ? DSM_TRANSFERS_PER_SECOND : DSM_SKIP, 1134 transfers_per_second, 1135 mb_per_second ? DSM_MB_PER_SECOND : DSM_SKIP, 1136 mb_per_second, 1137 blocks_per_second ? DSM_BLOCKS_PER_SECOND : DSM_SKIP, 1138 blocks_per_second, 1139 ms_per_transaction ? DSM_MS_PER_TRANSACTION : DSM_SKIP, 1140 ms_per_transaction, 1141 DSM_NONE)); 1142 } 1143 1144 long double 1145 devstat_compute_etime(struct timeval cur_time, struct timeval prev_time) 1146 { 1147 struct timeval busy_time; 1148 u_int64_t busy_usec; 1149 long double etime; 1150 1151 timersub(&cur_time, &prev_time, &busy_time); 1152 1153 busy_usec = busy_time.tv_sec; 1154 busy_usec *= 1000000; 1155 busy_usec += busy_time.tv_usec; 1156 etime = busy_usec; 1157 etime /= 1000000; 1158 1159 return(etime); 1160 } 1161 1162 int 1163 devstat_compute_statistics(struct devstat *current, struct devstat *previous, 1164 long double etime, ...) 1165 { 1166 char *func_name = "devstat_compute_statistics"; 1167 u_int64_t totalbytes, totalbytesread, totalbyteswrite; 1168 u_int64_t totaltransfers, totaltransfersread, totaltransferswrite; 1169 u_int64_t totaltransfersother, totalblocks, totalblocksread; 1170 u_int64_t totalblockswrite; 1171 va_list ap; 1172 devstat_metric metric; 1173 u_int64_t *destu64; 1174 long double *destld; 1175 int retval; 1176 1177 retval = 0; 1178 1179 /* 1180 * current is the only mandatory field. 1181 */ 1182 if (current == NULL) { 1183 snprintf(devstat_errbuf, sizeof(devstat_errbuf), 1184 "%s: current stats structure was NULL", func_name); 1185 return(-1); 1186 } 1187 1188 totalbytesread = current->bytes_read - 1189 ((previous) ? previous->bytes_read : 0); 1190 totalbyteswrite = current->bytes_written - 1191 ((previous) ? previous->bytes_written : 0); 1192 1193 totalbytes = totalbytesread + totalbyteswrite; 1194 1195 totaltransfersread = current->num_reads - 1196 ((previous) ? previous->num_reads : 0); 1197 1198 totaltransferswrite = current->num_writes - 1199 ((previous) ? previous->num_writes : 0); 1200 1201 totaltransfersother = current->num_other - 1202 ((previous) ? previous->num_other : 0); 1203 1204 totaltransfers = totaltransfersread + totaltransferswrite + 1205 totaltransfersother; 1206 1207 totalblocks = totalbytes; 1208 totalblocksread = totalbytesread; 1209 totalblockswrite = totalbyteswrite; 1210 1211 if (current->block_size > 0) { 1212 totalblocks /= current->block_size; 1213 totalblocksread /= current->block_size; 1214 totalblockswrite /= current->block_size; 1215 } else { 1216 totalblocks /= 512; 1217 totalblocksread /= 512; 1218 totalblockswrite /= 512; 1219 } 1220 1221 va_start(ap, etime); 1222 1223 while ((metric = (devstat_metric)va_arg(ap, devstat_metric)) != 0) { 1224 1225 if (metric == DSM_NONE) 1226 break; 1227 1228 if (metric >= DSM_MAX) { 1229 snprintf(devstat_errbuf, sizeof(devstat_errbuf), 1230 "%s: metric %d is out of range", func_name, 1231 metric); 1232 retval = -1; 1233 goto bailout; 1234 } 1235 1236 switch (devstat_arg_list[metric].argtype) { 1237 case DEVSTAT_ARG_UINT64: 1238 destu64 = (u_int64_t *)va_arg(ap, u_int64_t *); 1239 break; 1240 case DEVSTAT_ARG_LD: 1241 destld = (long double *)va_arg(ap, long double *); 1242 break; 1243 case DEVSTAT_ARG_SKIP: 1244 destld = (long double *)va_arg(ap, long double *); 1245 break; 1246 default: 1247 retval = -1; 1248 goto bailout; 1249 break; /* NOTREACHED */ 1250 } 1251 1252 if (devstat_arg_list[metric].argtype == DEVSTAT_ARG_SKIP) 1253 continue; 1254 1255 switch (metric) { 1256 case DSM_TOTAL_BYTES: 1257 *destu64 = totalbytes; 1258 break; 1259 case DSM_TOTAL_BYTES_READ: 1260 *destu64 = totalbytesread; 1261 break; 1262 case DSM_TOTAL_BYTES_WRITE: 1263 *destu64 = totalbyteswrite; 1264 break; 1265 case DSM_TOTAL_TRANSFERS: 1266 *destu64 = totaltransfers; 1267 break; 1268 case DSM_TOTAL_TRANSFERS_READ: 1269 *destu64 = totaltransfersread; 1270 break; 1271 case DSM_TOTAL_TRANSFERS_WRITE: 1272 *destu64 = totaltransferswrite; 1273 break; 1274 case DSM_TOTAL_TRANSFERS_OTHER: 1275 *destu64 = totaltransfersother; 1276 break; 1277 case DSM_TOTAL_BLOCKS: 1278 *destu64 = totalblocks; 1279 break; 1280 case DSM_TOTAL_BLOCKS_READ: 1281 *destu64 = totalblocksread; 1282 break; 1283 case DSM_TOTAL_BLOCKS_WRITE: 1284 *destu64 = totalblockswrite; 1285 break; 1286 case DSM_KB_PER_TRANSFER: 1287 *destld = totalbytes; 1288 *destld /= 1024; 1289 if (totaltransfers > 0) 1290 *destld /= totaltransfers; 1291 else 1292 *destld = 0.0; 1293 break; 1294 case DSM_KB_PER_TRANSFER_READ: 1295 *destld = totalbytesread; 1296 *destld /= 1024; 1297 if (totaltransfersread > 0) 1298 *destld /= totaltransfersread; 1299 else 1300 *destld = 0.0; 1301 break; 1302 case DSM_KB_PER_TRANSFER_WRITE: 1303 *destld = totalbyteswrite; 1304 *destld /= 1024; 1305 if (totaltransferswrite > 0) 1306 *destld /= totaltransferswrite; 1307 else 1308 *destld = 0.0; 1309 break; 1310 case DSM_TRANSFERS_PER_SECOND: 1311 if (etime > 0.0) { 1312 *destld = totaltransfers; 1313 *destld /= etime; 1314 } else 1315 *destld = 0.0; 1316 break; 1317 case DSM_TRANSFERS_PER_SECOND_READ: 1318 if (etime > 0.0) { 1319 *destld = totaltransfersread; 1320 *destld /= etime; 1321 } else 1322 *destld = 0.0; 1323 break; 1324 case DSM_TRANSFERS_PER_SECOND_WRITE: 1325 if (etime > 0.0) { 1326 *destld = totaltransferswrite; 1327 *destld /= etime; 1328 } else 1329 *destld = 0.0; 1330 break; 1331 case DSM_TRANSFERS_PER_SECOND_OTHER: 1332 if (etime > 0.0) { 1333 *destld = totaltransfersother; 1334 *destld /= etime; 1335 } else 1336 *destld = 0.0; 1337 break; 1338 case DSM_MB_PER_SECOND: 1339 *destld = totalbytes; 1340 *destld /= 1024 * 1024; 1341 if (etime > 0.0) 1342 *destld /= etime; 1343 else 1344 *destld = 0.0; 1345 break; 1346 case DSM_MB_PER_SECOND_READ: 1347 *destld = totalbytesread; 1348 *destld /= 1024 * 1024; 1349 if (etime > 0.0) 1350 *destld /= etime; 1351 else 1352 *destld = 0.0; 1353 break; 1354 case DSM_MB_PER_SECOND_WRITE: 1355 *destld = totalbyteswrite; 1356 *destld /= 1024 * 1024; 1357 if (etime > 0.0) 1358 *destld /= etime; 1359 else 1360 *destld = 0.0; 1361 break; 1362 case DSM_BLOCKS_PER_SECOND: 1363 *destld = totalblocks; 1364 if (etime > 0.0) 1365 *destld /= etime; 1366 else 1367 *destld = 0.0; 1368 break; 1369 case DSM_BLOCKS_PER_SECOND_READ: 1370 *destld = totalblocksread; 1371 if (etime > 0.0) 1372 *destld /= etime; 1373 else 1374 *destld = 0.0; 1375 break; 1376 case DSM_BLOCKS_PER_SECOND_WRITE: 1377 *destld = totalblockswrite; 1378 if (etime > 0.0) 1379 *destld /= etime; 1380 else 1381 *destld = 0.0; 1382 break; 1383 /* 1384 * This calculation is somewhat bogus. It simply divides 1385 * the elapsed time by the total number of transactions 1386 * completed. While that does give the caller a good 1387 * picture of the average rate of transaction completion, 1388 * it doesn't necessarily give the caller a good view of 1389 * how long transactions took to complete on average. 1390 * Those two numbers will be different for a device that 1391 * can handle more than one transaction at a time. e.g. 1392 * SCSI disks doing tagged queueing. 1393 * 1394 * The only way to accurately determine the real average 1395 * time per transaction would be to compute and store the 1396 * time on a per-transaction basis. That currently isn't 1397 * done in the kernel, and would only be desireable if it 1398 * could be implemented in a somewhat non-intrusive and high 1399 * performance way. 1400 */ 1401 case DSM_MS_PER_TRANSACTION: 1402 if (totaltransfers > 0) { 1403 *destld = etime; 1404 *destld /= totaltransfers; 1405 *destld *= 1000; 1406 } else 1407 *destld = 0.0; 1408 break; 1409 /* 1410 * As above, these next two really only give the average 1411 * rate of completion for read and write transactions, not 1412 * the average time the transaction took to complete. 1413 */ 1414 case DSM_MS_PER_TRANSACTION_READ: 1415 if (totaltransfersread > 0) { 1416 *destld = etime; 1417 *destld /= totaltransfersread; 1418 *destld *= 1000; 1419 } else 1420 *destld = 0.0; 1421 break; 1422 case DSM_MS_PER_TRANSACTION_WRITE: 1423 if (totaltransferswrite > 0) { 1424 *destld = etime; 1425 *destld /= totaltransferswrite; 1426 *destld *= 1000; 1427 } else 1428 *destld = 0.0; 1429 break; 1430 default: 1431 /* 1432 * This shouldn't happen, since we should have 1433 * caught any out of range metrics at the top of 1434 * the loop. 1435 */ 1436 snprintf(devstat_errbuf, sizeof(devstat_errbuf), 1437 "%s: unknown metric %d", func_name, metric); 1438 retval = -1; 1439 goto bailout; 1440 break; /* NOTREACHED */ 1441 } 1442 } 1443 1444 bailout: 1445 1446 va_end(ap); 1447 return(retval); 1448 } 1449 1450 static int 1451 readkmem(kvm_t *kd, unsigned long addr, void *buf, size_t nbytes) 1452 { 1453 char *func_name = "readkmem"; 1454 1455 if (kvm_read(kd, addr, buf, nbytes) == -1) { 1456 snprintf(devstat_errbuf, sizeof(devstat_errbuf), 1457 "%s: error reading value (kvm_read): %s", func_name, 1458 kvm_geterr(kd)); 1459 return(-1); 1460 } 1461 return(0); 1462 } 1463 1464 static int 1465 readkmem_nl(kvm_t *kd, char *name, void *buf, size_t nbytes) 1466 { 1467 char *func_name = "readkmem_nl"; 1468 struct nlist nl[2] = { { name }, { NULL } }; 1469 1470 if (kvm_nlist(kd, nl) == -1) { 1471 snprintf(devstat_errbuf, sizeof(devstat_errbuf), 1472 "%s: error getting name list (kvm_nlist): %s", 1473 func_name, kvm_geterr(kd)); 1474 return(-1); 1475 } 1476 return(readkmem(kd, nl[0].n_value, buf, nbytes)); 1477 } 1478 1479 /* 1480 * This duplicates the functionality of the kernel sysctl handler for poking 1481 * through crash dumps. 1482 */ 1483 static char * 1484 get_devstat_kvm(kvm_t *kd) 1485 { 1486 int error, i, wp; 1487 long gen; 1488 struct devstat *nds; 1489 struct devstat ds; 1490 struct devstatlist dhead; 1491 int num_devs; 1492 char *rv = NULL; 1493 char *func_name = "get_devstat_kvm"; 1494 1495 if ((num_devs = getnumdevs()) <= 0) 1496 return(NULL); 1497 error = 0; 1498 if (KREADNL(kd, X_DEVICE_STATQ, dhead) == -1) 1499 return(NULL); 1500 1501 nds = STAILQ_FIRST(&dhead); 1502 1503 if ((rv = malloc(sizeof(gen))) == NULL) { 1504 snprintf(devstat_errbuf, sizeof(devstat_errbuf), 1505 "%s: out of memory (initial malloc failed)", 1506 func_name); 1507 return(NULL); 1508 } 1509 gen = getgeneration(); 1510 memcpy(rv, &gen, sizeof(gen)); 1511 wp = sizeof(gen); 1512 /* 1513 * Now push out all the devices. 1514 */ 1515 for (i = 0; (nds != NULL) && (i < num_devs); 1516 nds = STAILQ_NEXT(nds, dev_links), i++) { 1517 if (readkmem(kd, (long)nds, &ds, sizeof(ds)) == -1) { 1518 free(rv); 1519 return(NULL); 1520 } 1521 nds = &ds; 1522 rv = (char *)reallocf(rv, sizeof(gen) + 1523 sizeof(ds) * (i + 1)); 1524 if (rv == NULL) { 1525 snprintf(devstat_errbuf, sizeof(devstat_errbuf), 1526 "%s: out of memory (malloc failed)", 1527 func_name); 1528 return(NULL); 1529 } 1530 memcpy(rv + wp, &ds, sizeof(ds)); 1531 wp += sizeof(ds); 1532 } 1533 return(rv); 1534 } 1535 1536 /* 1537 * Compatability functions for libdevstat 2. These are deprecated and may 1538 * eventually be removed. 1539 */ 1540 int 1541 getnumdevs(void) 1542 { 1543 return(devstat_getnumdevs(NULL)); 1544 } 1545 1546 long 1547 getgeneration(void) 1548 { 1549 return(devstat_getgeneration(NULL)); 1550 } 1551 1552 int 1553 getversion(void) 1554 { 1555 return(devstat_getversion(NULL)); 1556 } 1557 1558 int 1559 checkversion(void) 1560 { 1561 return(devstat_checkversion(NULL)); 1562 } 1563 1564 int 1565 getdevs(struct statinfo *stats) 1566 { 1567 return(devstat_getdevs(NULL, stats)); 1568 } 1569 1570 int 1571 selectdevs(struct device_selection **dev_select, int *num_selected, 1572 int *num_selections, long *select_generation, 1573 long current_generation, struct devstat *devices, int numdevs, 1574 struct devstat_match *matches, int num_matches, 1575 char **dev_selections, int num_dev_selections, 1576 devstat_select_mode select_mode, int maxshowdevs, 1577 int perf_select) 1578 { 1579 1580 return(devstat_selectdevs(dev_select, num_selected, num_selections, 1581 select_generation, current_generation, devices, numdevs, 1582 matches, num_matches, dev_selections, num_dev_selections, 1583 select_mode, maxshowdevs, perf_select)); 1584 } 1585 1586 int 1587 buildmatch(char *match_str, struct devstat_match **matches, 1588 int *num_matches) 1589 { 1590 return(devstat_buildmatch(match_str, matches, num_matches)); 1591 } 1592 1593 long double 1594 compute_etime(struct timeval cur_time, struct timeval prev_time) 1595 { 1596 return(devstat_compute_etime(cur_time, prev_time)); 1597 } 1598