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