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 * $FreeBSD: src/lib/libdevstat/devstat.c,v 1.6 1999/08/28 00:04:26 peter Exp $ 29 * $DragonFly: src/lib/libdevstat/devstat.c,v 1.5 2005/01/08 19:19:26 joerg Exp $ 30 */ 31 32 #include <sys/types.h> 33 #include <sys/sysctl.h> 34 #include <sys/errno.h> 35 36 #include <ctype.h> 37 #include <err.h> 38 #include <stdio.h> 39 #include <stdlib.h> 40 #include <string.h> 41 42 #include "devstat.h" 43 44 char devstat_errbuf[DEVSTAT_ERRBUF_SIZE]; 45 46 /* 47 * Table to match descriptive strings with device types. These are in 48 * order from most common to least common to speed search time. 49 */ 50 struct devstat_match_table match_table[] = { 51 {"da", DEVSTAT_TYPE_DIRECT, DEVSTAT_MATCH_TYPE}, 52 {"nvme", DEVSTAT_TYPE_DIRECT, DEVSTAT_MATCH_TYPE}, 53 {"xa", DEVSTAT_TYPE_DIRECT, DEVSTAT_MATCH_TYPE}, 54 {"cd", DEVSTAT_TYPE_CDROM, DEVSTAT_MATCH_TYPE}, 55 {"scsi", DEVSTAT_TYPE_IF_SCSI, DEVSTAT_MATCH_IF}, 56 {"ide", DEVSTAT_TYPE_IF_IDE, DEVSTAT_MATCH_IF}, 57 {"other", DEVSTAT_TYPE_IF_OTHER, DEVSTAT_MATCH_IF}, 58 {"worm", DEVSTAT_TYPE_WORM, DEVSTAT_MATCH_TYPE}, 59 {"sa", DEVSTAT_TYPE_SEQUENTIAL,DEVSTAT_MATCH_TYPE}, 60 {"pass", DEVSTAT_TYPE_PASS, DEVSTAT_MATCH_PASS}, 61 {"optical", DEVSTAT_TYPE_OPTICAL, DEVSTAT_MATCH_TYPE}, 62 {"array", DEVSTAT_TYPE_STORARRAY, DEVSTAT_MATCH_TYPE}, 63 {"changer", DEVSTAT_TYPE_CHANGER, DEVSTAT_MATCH_TYPE}, 64 {"scanner", DEVSTAT_TYPE_SCANNER, DEVSTAT_MATCH_TYPE}, 65 {"printer", DEVSTAT_TYPE_PRINTER, DEVSTAT_MATCH_TYPE}, 66 {"floppy", DEVSTAT_TYPE_FLOPPY, DEVSTAT_MATCH_TYPE}, 67 {"proc", DEVSTAT_TYPE_PROCESSOR, DEVSTAT_MATCH_TYPE}, 68 {"comm", DEVSTAT_TYPE_COMM, DEVSTAT_MATCH_TYPE}, 69 {"enclosure", DEVSTAT_TYPE_ENCLOSURE, DEVSTAT_MATCH_TYPE}, 70 {NULL, 0, 0} 71 }; 72 73 /* 74 * Local function declarations. 75 */ 76 static int compare_select(const void *arg1, const void *arg2); 77 78 int 79 getnumdevs(void) 80 { 81 size_t numdevsize; 82 int numdevs; 83 const char *func_name = "getnumdevs"; 84 85 numdevsize = sizeof(int); 86 87 /* 88 * Find out how many devices we have in the system. 89 */ 90 if (sysctlbyname("kern.devstat.numdevs", &numdevs, 91 &numdevsize, NULL, 0) == -1) { 92 sprintf(devstat_errbuf, "%s: error getting number of devices\n" 93 "%s: %s", func_name, func_name, strerror(errno)); 94 return(-1); 95 } else 96 return(numdevs); 97 } 98 99 /* 100 * This is an easy way to get the generation number, but the generation is 101 * supplied in a more atmoic manner by the kern.devstat.all sysctl. 102 * Because this generation sysctl is separate from the statistics sysctl, 103 * the device list and the generation could change between the time that 104 * this function is called and the device list is retreived. 105 */ 106 long 107 getgeneration(void) 108 { 109 size_t gensize; 110 long generation; 111 const char *func_name = "getgeneration"; 112 113 gensize = sizeof(long); 114 115 /* 116 * Get the current generation number. 117 */ 118 if (sysctlbyname("kern.devstat.generation", &generation, 119 &gensize, NULL, 0) == -1) { 120 sprintf(devstat_errbuf,"%s: error getting devstat generation\n" 121 "%s: %s", func_name, func_name, strerror(errno)); 122 return(-1); 123 } else 124 return(generation); 125 } 126 127 /* 128 * Get the current devstat version. The return value of this function 129 * should be compared with DEVSTAT_VERSION, which is defined in 130 * sys/devicestat.h. This will enable userland programs to determine 131 * whether they are out of sync with the kernel. 132 */ 133 int 134 getversion(void) 135 { 136 size_t versize; 137 int version; 138 const char *func_name = "getversion"; 139 140 versize = sizeof(int); 141 142 /* 143 * Get the current devstat version. 144 */ 145 if (sysctlbyname("kern.devstat.version", &version, &versize, 146 NULL, 0) == -1) { 147 sprintf(devstat_errbuf, "%s: error getting devstat version\n" 148 "%s: %s", func_name, func_name, strerror(errno)); 149 return(-1); 150 } else 151 return(version); 152 } 153 154 /* 155 * Check the devstat version we know about against the devstat version the 156 * kernel knows about. If they don't match, print an error into the 157 * devstat error buffer, and return -1. If they match, return 0. 158 */ 159 int 160 checkversion(void) 161 { 162 int retval = 0; 163 int errlen = 0; 164 const char *func_name = "checkversion"; 165 int version; 166 167 version = getversion(); 168 169 if (version != DEVSTAT_VERSION) { 170 int buflen = 0; 171 char tmpstr[256]; 172 173 /* 174 * This is really pretty silly, but basically the idea is 175 * that if getversion() returns an error (i.e. -1), then it 176 * has printed an error message in the buffer. Therefore, 177 * we need to add a \n to the end of that message before we 178 * print our own message in the buffer. 179 */ 180 if (version == -1) { 181 buflen = strlen(devstat_errbuf); 182 errlen = snprintf(tmpstr, sizeof(tmpstr), "\n"); 183 strncat(devstat_errbuf, tmpstr, 184 DEVSTAT_ERRBUF_SIZE - buflen - 1); 185 buflen += errlen; 186 } 187 188 errlen = snprintf(tmpstr, sizeof(tmpstr), 189 "%s: userland devstat version %d is not " 190 "the same as the kernel\n%s: devstat " 191 "version %d\n", func_name, DEVSTAT_VERSION, 192 func_name, version); 193 194 if (version == -1) { 195 strncat(devstat_errbuf, tmpstr, 196 DEVSTAT_ERRBUF_SIZE - buflen - 1); 197 buflen += errlen; 198 } else { 199 strncpy(devstat_errbuf, tmpstr, DEVSTAT_ERRBUF_SIZE); 200 devstat_errbuf[DEVSTAT_ERRBUF_SIZE - 1] = '\0'; 201 } 202 203 if (version < DEVSTAT_VERSION) 204 snprintf(tmpstr, sizeof(tmpstr), 205 "%s: libdevstat newer than kernel\n", 206 func_name); 207 else 208 snprintf(tmpstr, sizeof(tmpstr), 209 "%s: kernel newer than libdevstat\n", 210 func_name); 211 212 strncat(devstat_errbuf, tmpstr, 213 DEVSTAT_ERRBUF_SIZE - buflen - 1); 214 215 retval = -1; 216 } 217 218 return(retval); 219 } 220 221 /* 222 * Get the current list of devices and statistics, and the current 223 * generation number. 224 * 225 * Return values: 226 * -1 -- error 227 * 0 -- device list is unchanged 228 * 1 -- device list has changed 229 */ 230 int 231 getdevs(struct statinfo *stats) 232 { 233 int error; 234 size_t dssize; 235 long oldgeneration; 236 int retval = 0; 237 struct devinfo *dinfo; 238 const char *func_name = "getdevs"; 239 240 dinfo = stats->dinfo; 241 242 if (dinfo == NULL) { 243 sprintf(devstat_errbuf, "%s: stats->dinfo was NULL", func_name); 244 return(-1); 245 } 246 247 oldgeneration = dinfo->generation; 248 249 /* 250 * If this is our first time through, mem_ptr will be null. 251 */ 252 if (dinfo->mem_ptr == NULL) { 253 /* 254 * Get the number of devices. If it's negative, it's an 255 * error. Don't bother setting the error string, since 256 * getnumdevs() has already done that for us. 257 */ 258 if ((dinfo->numdevs = getnumdevs()) < 0) 259 return(-1); 260 261 /* 262 * The kern.devstat.all sysctl returns the current generation 263 * number, as well as all the devices. So we need four 264 * bytes more. 265 */ 266 dssize =(dinfo->numdevs * sizeof(struct devstat)) +sizeof(long); 267 dinfo->mem_ptr = (u_int8_t *)malloc(dssize); 268 } else 269 dssize =(dinfo->numdevs * sizeof(struct devstat)) +sizeof(long); 270 271 /* Get the current time when we get the stats */ 272 gettimeofday(&stats->busy_time, NULL); 273 274 /* 275 * Request all of the devices. We only really allow for one 276 * ENOMEM failure. It would, of course, be possible to just go in 277 * a loop and keep reallocing the device structure until we don't 278 * get ENOMEM back. I'm not sure it's worth it, though. If 279 * devices are being added to the system that quickly, maybe the 280 * user can just wait until all devices are added. 281 */ 282 if ((error = sysctlbyname("kern.devstat.all", dinfo->mem_ptr, 283 &dssize, NULL, 0)) == -1) { 284 /* 285 * If we get ENOMEM back, that means that there are 286 * more devices now, so we need to allocate more 287 * space for the device array. 288 */ 289 if (errno == ENOMEM) { 290 /* 291 * No need to set the error string here, getnumdevs() 292 * will do that if it fails. 293 */ 294 if ((dinfo->numdevs = getnumdevs()) < 0) 295 return(-1); 296 297 dssize = (dinfo->numdevs * sizeof(struct devstat)) + 298 sizeof(long); 299 dinfo->mem_ptr = (u_int8_t *)realloc(dinfo->mem_ptr, 300 dssize); 301 if ((error = sysctlbyname("kern.devstat.all", 302 dinfo->mem_ptr, &dssize, NULL, 0)) == -1) { 303 sprintf(devstat_errbuf, 304 "%s: error getting device stats\n" 305 "%s: %s", func_name, func_name, 306 strerror(errno)); 307 return(-1); 308 } 309 } else { 310 sprintf(devstat_errbuf, 311 "%s: error getting device stats\n" 312 "%s: %s", func_name, func_name, 313 strerror(errno)); 314 return(-1); 315 } 316 } 317 318 /* 319 * The sysctl spits out the generation as the first four bytes, 320 * then all of the device statistics structures. 321 */ 322 dinfo->generation = *(long *)dinfo->mem_ptr; 323 324 /* 325 * If the generation has changed, and if the current number of 326 * devices is not the same as the number of devices recorded in the 327 * devinfo structure, it is likely that the device list has shrunk. 328 * The reason that it is likely that the device list has shrunk in 329 * this case is that if the device list has grown, the sysctl above 330 * will return an ENOMEM error, and we will reset the number of 331 * devices and reallocate the device array. If the second sysctl 332 * fails, we will return an error and therefore never get to this 333 * point. If the device list has shrunk, the sysctl will not 334 * return an error since we have more space allocated than is 335 * necessary. So, in the shrinkage case, we catch it here and 336 * reallocate the array so that we don't use any more space than is 337 * necessary. 338 */ 339 if (oldgeneration != dinfo->generation) { 340 if (getnumdevs() != dinfo->numdevs) { 341 if ((dinfo->numdevs = getnumdevs()) < 0) 342 return(-1); 343 dssize = (dinfo->numdevs * sizeof(struct devstat)) + 344 sizeof(long); 345 dinfo->mem_ptr = (u_int8_t *)realloc(dinfo->mem_ptr, 346 dssize); 347 } 348 retval = 1; 349 } 350 351 dinfo->devices = (struct devstat *)(dinfo->mem_ptr + sizeof(long)); 352 353 return(retval); 354 } 355 356 /* 357 * selectdevs(): 358 * 359 * Devices are selected/deselected based upon the following criteria: 360 * - devices specified by the user on the command line 361 * - devices matching any device type expressions given on the command line 362 * - devices with the highest I/O, if 'top' mode is enabled 363 * - the first n unselected devices in the device list, if maxshowdevs 364 * devices haven't already been selected and if the user has not 365 * specified any devices on the command line and if we're in "add" mode. 366 * 367 * Input parameters: 368 * - device selection list (dev_select) 369 * - current number of devices selected (num_selected) 370 * - total number of devices in the selection list (num_selections) 371 * - devstat generation as of the last time selectdevs() was called 372 * (select_generation) 373 * - current devstat generation (current_generation) 374 * - current list of devices and statistics (devices) 375 * - number of devices in the current device list (numdevs) 376 * - compiled version of the command line device type arguments (matches) 377 * - This is optional. If the number of devices is 0, this will be ignored. 378 * - The matching code pays attention to the current selection mode. So 379 * if you pass in a matching expression, it will be evaluated based 380 * upon the selection mode that is passed in. See below for details. 381 * - number of device type matching expressions (num_matches) 382 * - Set to 0 to disable the matching code. 383 * - list of devices specified on the command line by the user (dev_selections) 384 * - number of devices selected on the command line by the user 385 * (num_dev_selections) 386 * - Our selection mode. There are four different selection modes: 387 * - add mode. (DS_SELECT_ADD) Any devices matching devices explicitly 388 * selected by the user or devices matching a pattern given by the 389 * user will be selected in addition to devices that are already 390 * selected. Additional devices will be selected, up to maxshowdevs 391 * number of devices. 392 * - only mode. (DS_SELECT_ONLY) Only devices matching devices 393 * explicitly given by the user or devices matching a pattern 394 * given by the user will be selected. No other devices will be 395 * selected. 396 * - addonly mode. (DS_SELECT_ADDONLY) This is similar to add and 397 * only. Basically, this will not de-select any devices that are 398 * current selected, as only mode would, but it will also not 399 * gratuitously select up to maxshowdevs devices as add mode would. 400 * - remove mode. (DS_SELECT_REMOVE) Any devices matching devices 401 * explicitly selected by the user or devices matching a pattern 402 * given by the user will be de-selected. 403 * - maximum number of devices we can select (maxshowdevs) 404 * - flag indicating whether or not we're in 'top' mode (perf_select) 405 * 406 * Output data: 407 * - the device selection list may be modified and passed back out 408 * - the number of devices selected and the total number of items in the 409 * device selection list may be changed 410 * - the selection generation may be changed to match the current generation 411 * 412 * Return values: 413 * -1 -- error 414 * 0 -- selected devices are unchanged 415 * 1 -- selected devices changed 416 */ 417 int 418 selectdevs(struct device_selection **dev_select, int *num_selected, 419 int *num_selections, long *select_generation, 420 long current_generation, struct devstat *devices, int numdevs, 421 struct devstat_match *matches, int num_matches, 422 char **dev_selections, int num_dev_selections, 423 devstat_select_mode select_mode, int maxshowdevs, int perf_select) 424 { 425 int i, j, k; 426 int init_selections = 0, init_selected_var = 0; 427 struct device_selection *old_dev_select = NULL; 428 int old_num_selections = 0, old_num_selected; 429 int selection_number = 0; 430 int changed = 0, found = 0; 431 432 if ((dev_select == NULL) || (devices == NULL) || (numdevs <= 0)) 433 return(-1); 434 435 /* 436 * We always want to make sure that we have as many dev_select 437 * entries as there are devices. 438 */ 439 /* 440 * In this case, we haven't selected devices before. 441 */ 442 if (*dev_select == NULL) { 443 *dev_select = (struct device_selection *)malloc(numdevs * 444 sizeof(struct device_selection)); 445 *select_generation = current_generation; 446 init_selections = 1; 447 changed = 1; 448 /* 449 * In this case, we have selected devices before, but the device 450 * list has changed since we last selected devices, so we need to 451 * either enlarge or reduce the size of the device selection list. 452 */ 453 } else if (*num_selections != numdevs) { 454 *dev_select = (struct device_selection *)realloc(*dev_select, 455 numdevs * sizeof(struct device_selection)); 456 *select_generation = current_generation; 457 init_selections = 1; 458 /* 459 * In this case, we've selected devices before, and the selection 460 * list is the same size as it was the last time, but the device 461 * list has changed. 462 */ 463 } else if (*select_generation < current_generation) { 464 *select_generation = current_generation; 465 init_selections = 1; 466 } 467 468 /* 469 * If we're in "only" mode, we want to clear out the selected 470 * variable since we're going to select exactly what the user wants 471 * this time through. 472 */ 473 if (select_mode == DS_SELECT_ONLY) 474 init_selected_var = 1; 475 476 /* 477 * In all cases, we want to back up the number of selected devices. 478 * It is a quick and accurate way to determine whether the selected 479 * devices have changed. 480 */ 481 old_num_selected = *num_selected; 482 483 /* 484 * We want to make a backup of the current selection list if 485 * the list of devices has changed, or if we're in performance 486 * selection mode. In both cases, we don't want to make a backup 487 * if we already know for sure that the list will be different. 488 * This is certainly the case if this is our first time through the 489 * selection code. 490 */ 491 if (((init_selected_var != 0) || (init_selections != 0) 492 || (perf_select != 0)) && (changed == 0)){ 493 old_dev_select = (struct device_selection *)malloc( 494 *num_selections * sizeof(struct device_selection)); 495 old_num_selections = *num_selections; 496 bcopy(*dev_select, old_dev_select, 497 sizeof(struct device_selection) * *num_selections); 498 } 499 500 if (init_selections != 0) { 501 bzero(*dev_select, sizeof(struct device_selection) * numdevs); 502 503 for (i = 0; i < numdevs; i++) { 504 (*dev_select)[i].device_number = 505 devices[i].device_number; 506 strncpy((*dev_select)[i].device_name, 507 devices[i].device_name, 508 DEVSTAT_NAME_LEN); 509 (*dev_select)[i].device_name[DEVSTAT_NAME_LEN - 1]='\0'; 510 (*dev_select)[i].unit_number = devices[i].unit_number; 511 (*dev_select)[i].position = i; 512 } 513 *num_selections = numdevs; 514 } else if (init_selected_var != 0) { 515 for (i = 0; i < numdevs; i++) 516 (*dev_select)[i].selected = 0; 517 } 518 519 /* we haven't gotten around to selecting anything yet.. */ 520 if ((select_mode == DS_SELECT_ONLY) || (init_selections != 0) 521 || (init_selected_var != 0)) 522 *num_selected = 0; 523 524 /* 525 * Look through any devices the user specified on the command line 526 * and see if they match known devices. If so, select them. 527 */ 528 for (i = 0; (i < *num_selections) && (num_dev_selections > 0); i++) { 529 char tmpstr[80]; 530 531 snprintf(tmpstr, sizeof(tmpstr), "%s%d", 532 (*dev_select)[i].device_name, 533 (*dev_select)[i].unit_number); 534 for (j = 0; j < num_dev_selections; j++) { 535 if (strcmp(tmpstr, dev_selections[j]) == 0) { 536 /* 537 * Here we do different things based on the 538 * mode we're in. If we're in add or 539 * addonly mode, we only select this device 540 * if it hasn't already been selected. 541 * Otherwise, we would be unnecessarily 542 * changing the selection order and 543 * incrementing the selection count. If 544 * we're in only mode, we unconditionally 545 * select this device, since in only mode 546 * any previous selections are erased and 547 * manually specified devices are the first 548 * ones to be selected. If we're in remove 549 * mode, we de-select the specified device and 550 * decrement the selection count. 551 */ 552 switch(select_mode) { 553 case DS_SELECT_ADD: 554 case DS_SELECT_ADDONLY: 555 if ((*dev_select)[i].selected) 556 break; 557 /* FALLTHROUGH */ 558 case DS_SELECT_ONLY: 559 (*dev_select)[i].selected = 560 ++selection_number; 561 (*num_selected)++; 562 break; 563 case DS_SELECT_REMOVE: 564 (*dev_select)[i].selected = 0; 565 (*num_selected)--; 566 /* 567 * This isn't passed back out, we 568 * just use it to keep track of 569 * how many devices we've removed. 570 */ 571 num_dev_selections--; 572 break; 573 } 574 break; 575 } 576 } 577 } 578 579 /* 580 * Go through the user's device type expressions and select devices 581 * accordingly. We only do this if the number of devices already 582 * selected is less than the maximum number we can show. 583 */ 584 for (i = 0; (i < num_matches) && (*num_selected < maxshowdevs); i++) { 585 /* We should probably indicate some error here */ 586 if ((matches[i].match_fields == DEVSTAT_MATCH_NONE) 587 || (matches[i].num_match_categories <= 0)) 588 continue; 589 590 for (j = 0; j < numdevs; j++) { 591 int num_match_categories; 592 593 num_match_categories = matches[i].num_match_categories; 594 595 /* 596 * Determine whether or not the current device 597 * matches the given matching expression. This if 598 * statement consists of three components: 599 * - the device type check 600 * - the device interface check 601 * - the passthrough check 602 * If a the matching test is successful, it 603 * decrements the number of matching categories, 604 * and if we've reached the last element that 605 * needed to be matched, the if statement succeeds. 606 * 607 */ 608 if ((((matches[i].match_fields & DEVSTAT_MATCH_TYPE)!=0) 609 && ((devices[j].device_type & DEVSTAT_TYPE_MASK) == 610 (matches[i].device_type & DEVSTAT_TYPE_MASK)) 611 &&(((matches[i].match_fields & DEVSTAT_MATCH_PASS)!=0) 612 || (((devices[j].device_type & 613 DEVSTAT_TYPE_PASS) == 0))) 614 && (--num_match_categories == 0)) 615 || (((matches[i].match_fields & DEVSTAT_MATCH_IF) != 0) 616 && ((devices[j].device_type & DEVSTAT_TYPE_IF_MASK) == 617 (matches[i].device_type & DEVSTAT_TYPE_IF_MASK)) 618 &&(((matches[i].match_fields & DEVSTAT_MATCH_PASS)!=0) 619 || (((devices[j].device_type & 620 DEVSTAT_TYPE_PASS) == 0))) 621 && (--num_match_categories == 0)) 622 || (((matches[i].match_fields & DEVSTAT_MATCH_PASS)!=0) 623 && ((devices[j].device_type & DEVSTAT_TYPE_PASS) != 0) 624 && (--num_match_categories == 0))) { 625 626 /* 627 * This is probably a non-optimal solution 628 * to the problem that the devices in the 629 * device list will not be in the same 630 * order as the devices in the selection 631 * array. 632 */ 633 for (k = 0; k < numdevs; k++) { 634 if ((*dev_select)[k].position == j) { 635 found = 1; 636 break; 637 } 638 } 639 640 /* 641 * There shouldn't be a case where a device 642 * in the device list is not in the 643 * selection list...but it could happen. 644 */ 645 if (found != 1) { 646 fprintf(stderr, "selectdevs: couldn't" 647 " find %s%d in selection " 648 "list\n", 649 devices[j].device_name, 650 devices[j].unit_number); 651 break; 652 } 653 654 /* 655 * We do different things based upon the 656 * mode we're in. If we're in add or only 657 * mode, we go ahead and select this device 658 * if it hasn't already been selected. If 659 * it has already been selected, we leave 660 * it alone so we don't mess up the 661 * selection ordering. Manually specified 662 * devices have already been selected, and 663 * they have higher priority than pattern 664 * matched devices. If we're in remove 665 * mode, we de-select the given device and 666 * decrement the selected count. 667 */ 668 switch(select_mode) { 669 case DS_SELECT_ADD: 670 case DS_SELECT_ADDONLY: 671 case DS_SELECT_ONLY: 672 if ((*dev_select)[k].selected != 0) 673 break; 674 (*dev_select)[k].selected = 675 ++selection_number; 676 (*num_selected)++; 677 break; 678 case DS_SELECT_REMOVE: 679 (*dev_select)[k].selected = 0; 680 (*num_selected)--; 681 break; 682 } 683 } 684 } 685 } 686 687 /* 688 * Here we implement "top" mode. Devices are sorted in the 689 * selection array based on two criteria: whether or not they are 690 * selected (not selection number, just the fact that they are 691 * selected!) and the number of bytes in the "bytes" field of the 692 * selection structure. The bytes field generally must be kept up 693 * by the user. In the future, it may be maintained by library 694 * functions, but for now the user has to do the work. 695 * 696 * At first glance, it may seem wrong that we don't go through and 697 * select every device in the case where the user hasn't specified 698 * any devices or patterns. In fact, though, it won't make any 699 * difference in the device sorting. In that particular case (i.e. 700 * when we're in "add" or "only" mode, and the user hasn't 701 * specified anything) the first time through no devices will be 702 * selected, so the only criterion used to sort them will be their 703 * performance. The second time through, and every time thereafter, 704 * all devices will be selected, so again selection won't matter. 705 */ 706 if (perf_select != 0) { 707 708 /* Sort the device array by throughput */ 709 qsort(*dev_select, *num_selections, 710 sizeof(struct device_selection), 711 compare_select); 712 713 if (*num_selected == 0) { 714 /* 715 * Here we select every device in the array, if it 716 * isn't already selected. Because the 'selected' 717 * variable in the selection array entries contains 718 * the selection order, the devstats routine can show 719 * the devices that were selected first. 720 */ 721 for (i = 0; i < *num_selections; i++) { 722 if ((*dev_select)[i].selected == 0) { 723 (*dev_select)[i].selected = 724 ++selection_number; 725 (*num_selected)++; 726 } 727 } 728 } else { 729 selection_number = 0; 730 for (i = 0; i < *num_selections; i++) { 731 if ((*dev_select)[i].selected != 0) { 732 (*dev_select)[i].selected = 733 ++selection_number; 734 } 735 } 736 } 737 } 738 739 /* 740 * If we're in the "add" selection mode and if we haven't already 741 * selected maxshowdevs number of devices, go through the array and 742 * select any unselected devices. If we're in "only" mode, we 743 * obviously don't want to select anything other than what the user 744 * specifies. If we're in "remove" mode, it probably isn't a good 745 * idea to go through and select any more devices, since we might 746 * end up selecting something that the user wants removed. Through 747 * more complicated logic, we could actually figure this out, but 748 * that would probably require combining this loop with the various 749 * selections loops above. 750 */ 751 if ((select_mode == DS_SELECT_ADD) && (*num_selected < maxshowdevs)) { 752 for (i = 0; i < *num_selections; i++) 753 if ((*dev_select)[i].selected == 0) { 754 (*dev_select)[i].selected = ++selection_number; 755 (*num_selected)++; 756 } 757 } 758 759 /* 760 * Look at the number of devices that have been selected. If it 761 * has changed, set the changed variable. Otherwise, if we've 762 * made a backup of the selection list, compare it to the current 763 * selection list to see if the selected devices have changed. 764 */ 765 if ((changed == 0) && (old_num_selected != *num_selected)) 766 changed = 1; 767 else if ((changed == 0) && (old_dev_select != NULL)) { 768 /* 769 * Now we go through the selection list and we look at 770 * it three different ways. 771 */ 772 for (i = 0; (i < *num_selections) && (changed == 0) && 773 (i < old_num_selections); i++) { 774 /* 775 * If the device at index i in both the new and old 776 * selection arrays has the same device number and 777 * selection status, it hasn't changed. We 778 * continue on to the next index. 779 */ 780 if (((*dev_select)[i].device_number == 781 old_dev_select[i].device_number) 782 && ((*dev_select)[i].selected == 783 old_dev_select[i].selected)) 784 continue; 785 786 /* 787 * Now, if we're still going through the if 788 * statement, the above test wasn't true. So we 789 * check here to see if the device at index i in 790 * the current array is the same as the device at 791 * index i in the old array. If it is, that means 792 * that its selection number has changed. Set 793 * changed to 1 and exit the loop. 794 */ 795 else if ((*dev_select)[i].device_number == 796 old_dev_select[i].device_number) { 797 changed = 1; 798 break; 799 } 800 /* 801 * If we get here, then the device at index i in 802 * the current array isn't the same device as the 803 * device at index i in the old array. 804 */ 805 else { 806 found = 0; 807 808 /* 809 * Search through the old selection array 810 * looking for a device with the same 811 * device number as the device at index i 812 * in the current array. If the selection 813 * status is the same, then we mark it as 814 * found. If the selection status isn't 815 * the same, we break out of the loop. 816 * Since found isn't set, changed will be 817 * set to 1 below. 818 */ 819 for (j = 0; j < old_num_selections; j++) { 820 if (((*dev_select)[i].device_number == 821 old_dev_select[j].device_number) 822 && ((*dev_select)[i].selected == 823 old_dev_select[j].selected)){ 824 found = 1; 825 break; 826 } 827 else if ((*dev_select)[i].device_number 828 == old_dev_select[j].device_number) 829 break; 830 } 831 if (found == 0) 832 changed = 1; 833 } 834 } 835 } 836 if (old_dev_select != NULL) 837 free(old_dev_select); 838 839 return(changed); 840 } 841 842 /* 843 * Comparison routine for qsort() above. Note that the comparison here is 844 * backwards -- generally, it should return a value to indicate whether 845 * arg1 is <, =, or > arg2. Instead, it returns the opposite. The reason 846 * it returns the opposite is so that the selection array will be sorted in 847 * order of decreasing performance. We sort on two parameters. The first 848 * sort key is whether or not one or the other of the devices in question 849 * has been selected. If one of them has, and the other one has not, the 850 * selected device is automatically more important than the unselected 851 * device. If neither device is selected, we judge the devices based upon 852 * performance. 853 */ 854 static int 855 compare_select(const void *arg1, const void *arg2) 856 { 857 if ((((const struct device_selection *)arg1)->selected) 858 && (((const struct device_selection *)arg2)->selected == 0)) 859 return(-1); 860 else if ((((const struct device_selection *)arg1)->selected == 0) 861 && (((const struct device_selection *)arg2)->selected)) 862 return(1); 863 else if (((const struct device_selection *)arg2)->bytes < 864 ((const struct device_selection *)arg1)->bytes) 865 return(-1); 866 else if (((const struct device_selection *)arg2)->bytes > 867 ((const struct device_selection *)arg1)->bytes) 868 return(1); 869 else 870 return(0); 871 } 872 873 /* 874 * Take a string with the general format "arg1,arg2,arg3", and build a 875 * device matching expression from it. 876 */ 877 int 878 buildmatch(const char *match_str, struct devstat_match **matches, 879 int *num_matches) 880 { 881 char *tstr[5]; 882 char **tempstr; 883 char *matchbuf_orig; /* strdup of match_str */ 884 char *matchbuf; /* allow strsep to clobber */ 885 int num_args; 886 int i, j; 887 int retval = -1; 888 889 /* We can't do much without a string to parse */ 890 if (match_str == NULL) { 891 sprintf(devstat_errbuf, "%s: no match expression", __func__); 892 return(-1); 893 } 894 895 /* 896 * Break the (comma delimited) input string out into separate strings. 897 * strsep is destructive, so copy the string first. 898 */ 899 matchbuf = matchbuf_orig = strdup(match_str); 900 if (matchbuf == NULL) { 901 sprintf(devstat_errbuf, "%s: out of memory", __func__); 902 return(-1); 903 } 904 for (tempstr = tstr, num_args = 0; 905 (*tempstr = strsep(&matchbuf, ",")) != NULL && (num_args < 5); 906 num_args++) 907 if (**tempstr != '\0') 908 if (++tempstr >= &tstr[5]) 909 break; 910 911 /* The user gave us too many type arguments */ 912 if (num_args > 3) { 913 sprintf(devstat_errbuf, "%s: too many type arguments", 914 __func__); 915 goto cleanup; 916 } 917 918 /* 919 * Since you can't realloc a pointer that hasn't been malloced 920 * first, we malloc first and then realloc. 921 */ 922 if (*num_matches == 0) 923 *matches = (struct devstat_match *)malloc( 924 sizeof(struct devstat_match)); 925 else 926 *matches = (struct devstat_match *)realloc(*matches, 927 sizeof(struct devstat_match) * (*num_matches + 1)); 928 929 /* Make sure the current entry is clear */ 930 bzero(&matches[0][*num_matches], sizeof(struct devstat_match)); 931 932 /* 933 * Step through the arguments the user gave us and build a device 934 * matching expression from them. 935 */ 936 for (i = 0; i < num_args; i++) { 937 char *tempstr2, *tempstr3; 938 939 /* 940 * Get rid of leading white space. 941 */ 942 tempstr2 = tstr[i]; 943 while (isspace(*tempstr2) && (*tempstr2 != '\0')) 944 tempstr2++; 945 946 /* 947 * Get rid of trailing white space. 948 */ 949 tempstr3 = &tempstr2[strlen(tempstr2) - 1]; 950 951 while ((*tempstr3 != '\0') && (tempstr3 > tempstr2) 952 && (isspace(*tempstr3))) { 953 *tempstr3 = '\0'; 954 tempstr3--; 955 } 956 957 /* 958 * Go through the match table comparing the user's 959 * arguments to known device types, interfaces, etc. 960 */ 961 for (j = 0; match_table[j].match_str != NULL; j++) { 962 /* 963 * We do case-insensitive matching, in case someone 964 * wants to enter "SCSI" instead of "scsi" or 965 * something like that. Only compare as many 966 * characters as are in the string in the match 967 * table. This should help if someone tries to use 968 * a super-long match expression. 969 */ 970 if (strncasecmp(tempstr2, match_table[j].match_str, 971 strlen(match_table[j].match_str)) == 0) { 972 /* 973 * Make sure the user hasn't specified two 974 * items of the same type, like "da" and 975 * "cd". One device cannot be both. 976 */ 977 if (((*matches)[*num_matches].match_fields & 978 match_table[j].match_field) != 0) { 979 sprintf(devstat_errbuf, 980 "%s: cannot have more than " 981 "one match item in a single " 982 "category", __func__); 983 goto cleanup; 984 } 985 /* 986 * If we've gotten this far, we have a 987 * winner. Set the appropriate fields in 988 * the match entry. 989 */ 990 (*matches)[*num_matches].match_fields |= 991 match_table[j].match_field; 992 (*matches)[*num_matches].device_type |= 993 match_table[j].type; 994 (*matches)[*num_matches].num_match_categories++; 995 break; 996 } 997 } 998 /* 999 * We should have found a match in the above for loop. If 1000 * not, that means the user entered an invalid device type 1001 * or interface. 1002 */ 1003 if ((*matches)[*num_matches].num_match_categories != (i + 1)) { 1004 snprintf(devstat_errbuf, sizeof(devstat_errbuf), 1005 "%s: unknown match item \"%s\"", __func__, 1006 tstr[i]); 1007 goto cleanup; 1008 } 1009 } 1010 1011 (*num_matches)++; 1012 retval = 0; 1013 cleanup: 1014 free(matchbuf_orig); 1015 return(retval); 1016 } 1017 1018 /* 1019 * Compute a number of device statistics. Only one field is mandatory, and 1020 * that is "current". Everything else is optional. The caller passes in 1021 * pointers to variables to hold the various statistics he desires. If he 1022 * doesn't want a particular staistic, he should pass in a NULL pointer. 1023 * Return values: 1024 * 0 -- success 1025 * -1 -- failure 1026 */ 1027 int 1028 compute_stats(struct devstat *current, struct devstat *previous, 1029 long double etime, u_int64_t *total_bytes, 1030 u_int64_t *total_transfers, u_int64_t *total_blocks, 1031 long double *kb_per_transfer, long double *transfers_per_second, 1032 long double *mb_per_second, long double *blocks_per_second, 1033 long double *ms_per_transaction) 1034 { 1035 u_int64_t totalbytes, totaltransfers, totalblocks; 1036 1037 /* 1038 * current is the only mandatory field. 1039 */ 1040 if (current == NULL) { 1041 sprintf(devstat_errbuf, "%s: current stats structure was NULL", 1042 __func__); 1043 return(-1); 1044 } 1045 1046 totalbytes = (current->bytes_written + current->bytes_read) - 1047 ((previous) ? (previous->bytes_written + 1048 previous->bytes_read) : 0); 1049 1050 if (total_bytes) 1051 *total_bytes = totalbytes; 1052 1053 totaltransfers = (current->num_reads + 1054 current->num_writes + 1055 current->num_other) - 1056 ((previous) ? 1057 (previous->num_reads + 1058 previous->num_writes + 1059 previous->num_other) : 0); 1060 if (total_transfers) 1061 *total_transfers = totaltransfers; 1062 1063 if (transfers_per_second) { 1064 if (etime > 0.0) { 1065 *transfers_per_second = totaltransfers; 1066 *transfers_per_second /= etime; 1067 } else 1068 *transfers_per_second = 0.0; 1069 } 1070 1071 if (kb_per_transfer) { 1072 *kb_per_transfer = totalbytes; 1073 *kb_per_transfer /= 1024; 1074 if (totaltransfers > 0) 1075 *kb_per_transfer /= totaltransfers; 1076 else 1077 *kb_per_transfer = 0.0; 1078 } 1079 1080 if (mb_per_second) { 1081 *mb_per_second = totalbytes; 1082 *mb_per_second /= 1024 * 1024; 1083 if (etime > 0.0) 1084 *mb_per_second /= etime; 1085 else 1086 *mb_per_second = 0.0; 1087 } 1088 1089 totalblocks = totalbytes; 1090 if (current->block_size > 0) 1091 totalblocks /= current->block_size; 1092 else 1093 totalblocks /= 512; 1094 1095 if (total_blocks) 1096 *total_blocks = totalblocks; 1097 1098 if (blocks_per_second) { 1099 *blocks_per_second = totalblocks; 1100 if (etime > 0.0) 1101 *blocks_per_second /= etime; 1102 else 1103 *blocks_per_second = 0.0; 1104 } 1105 1106 if (ms_per_transaction) { 1107 if (totaltransfers > 0) { 1108 *ms_per_transaction = etime; 1109 *ms_per_transaction /= totaltransfers; 1110 *ms_per_transaction *= 1000; 1111 } else 1112 *ms_per_transaction = 0.0; 1113 } 1114 1115 return(0); 1116 } 1117 1118 int 1119 compute_stats_read(struct devstat *current, struct devstat *previous, 1120 long double etime, u_int64_t *total_bytes, 1121 u_int64_t *total_transfers, u_int64_t *total_blocks, 1122 long double *kb_per_transfer, long double *transfers_per_second, 1123 long double *mb_per_second, long double *blocks_per_second, 1124 long double *ms_per_transaction) 1125 { 1126 u_int64_t totalbytes, totaltransfers, totalblocks; 1127 1128 /* 1129 * current is the only mandatory field. 1130 */ 1131 if (current == NULL) { 1132 sprintf(devstat_errbuf, "%s: current stats structure was NULL", 1133 __func__); 1134 return(-1); 1135 } 1136 1137 totalbytes = current->bytes_read - 1138 (previous ? previous->bytes_read : 0); 1139 1140 if (total_bytes) 1141 *total_bytes = totalbytes; 1142 1143 totaltransfers = current->num_reads - 1144 (previous ? previous->num_reads : 0); 1145 if (total_transfers) 1146 *total_transfers = totaltransfers; 1147 1148 if (transfers_per_second) { 1149 if (etime > 0.0) { 1150 *transfers_per_second = totaltransfers; 1151 *transfers_per_second /= etime; 1152 } else 1153 *transfers_per_second = 0.0; 1154 } 1155 1156 if (kb_per_transfer) { 1157 *kb_per_transfer = totalbytes; 1158 *kb_per_transfer /= 1024; 1159 if (totaltransfers > 0) 1160 *kb_per_transfer /= totaltransfers; 1161 else 1162 *kb_per_transfer = 0.0; 1163 } 1164 1165 if (mb_per_second) { 1166 *mb_per_second = totalbytes; 1167 *mb_per_second /= 1024 * 1024; 1168 if (etime > 0.0) 1169 *mb_per_second /= etime; 1170 else 1171 *mb_per_second = 0.0; 1172 } 1173 1174 totalblocks = totalbytes; 1175 if (current->block_size > 0) 1176 totalblocks /= current->block_size; 1177 else 1178 totalblocks /= 512; 1179 1180 if (total_blocks) 1181 *total_blocks = totalblocks; 1182 1183 if (blocks_per_second) { 1184 *blocks_per_second = totalblocks; 1185 if (etime > 0.0) 1186 *blocks_per_second /= etime; 1187 else 1188 *blocks_per_second = 0.0; 1189 } 1190 1191 if (ms_per_transaction) { 1192 if (totaltransfers > 0) { 1193 *ms_per_transaction = etime; 1194 *ms_per_transaction /= totaltransfers; 1195 *ms_per_transaction *= 1000; 1196 } else 1197 *ms_per_transaction = 0.0; 1198 } 1199 1200 return(0); 1201 } 1202 1203 int 1204 compute_stats_write(struct devstat *current, struct devstat *previous, 1205 long double etime, u_int64_t *total_bytes, 1206 u_int64_t *total_transfers, u_int64_t *total_blocks, 1207 long double *kb_per_transfer, long double *transfers_per_second, 1208 long double *mb_per_second, long double *blocks_per_second, 1209 long double *ms_per_transaction) 1210 { 1211 u_int64_t totalbytes, totaltransfers, totalblocks; 1212 1213 /* 1214 * current is the only mandatory field. 1215 */ 1216 if (current == NULL) { 1217 sprintf(devstat_errbuf, "%s: current stats structure was NULL", 1218 __func__); 1219 return(-1); 1220 } 1221 1222 totalbytes = current->bytes_written - 1223 (previous ? previous->bytes_written : 0); 1224 1225 if (total_bytes) 1226 *total_bytes = totalbytes; 1227 1228 totaltransfers = current->num_writes - 1229 (previous ? previous->num_writes : 0); 1230 if (total_transfers) 1231 *total_transfers = totaltransfers; 1232 1233 if (transfers_per_second) { 1234 if (etime > 0.0) { 1235 *transfers_per_second = totaltransfers; 1236 *transfers_per_second /= etime; 1237 } else 1238 *transfers_per_second = 0.0; 1239 } 1240 1241 if (kb_per_transfer) { 1242 *kb_per_transfer = totalbytes; 1243 *kb_per_transfer /= 1024; 1244 if (totaltransfers > 0) 1245 *kb_per_transfer /= totaltransfers; 1246 else 1247 *kb_per_transfer = 0.0; 1248 } 1249 1250 if (mb_per_second) { 1251 *mb_per_second = totalbytes; 1252 *mb_per_second /= 1024 * 1024; 1253 if (etime > 0.0) 1254 *mb_per_second /= etime; 1255 else 1256 *mb_per_second = 0.0; 1257 } 1258 1259 totalblocks = totalbytes; 1260 if (current->block_size > 0) 1261 totalblocks /= current->block_size; 1262 else 1263 totalblocks /= 512; 1264 1265 if (total_blocks) 1266 *total_blocks = totalblocks; 1267 1268 if (blocks_per_second) { 1269 *blocks_per_second = totalblocks; 1270 if (etime > 0.0) 1271 *blocks_per_second /= etime; 1272 else 1273 *blocks_per_second = 0.0; 1274 } 1275 1276 if (ms_per_transaction) { 1277 if (totaltransfers > 0) { 1278 *ms_per_transaction = etime; 1279 *ms_per_transaction /= totaltransfers; 1280 *ms_per_transaction *= 1000; 1281 } else 1282 *ms_per_transaction = 0.0; 1283 } 1284 1285 return(0); 1286 } 1287 1288 long double 1289 compute_etime(struct timeval cur_time, struct timeval prev_time) 1290 { 1291 struct timeval busy_time; 1292 u_int64_t busy_usec; 1293 long double etime; 1294 1295 timersub(&cur_time, &prev_time, &busy_time); 1296 1297 busy_usec = busy_time.tv_sec; 1298 busy_usec *= 1000000; 1299 busy_usec += busy_time.tv_usec; 1300 etime = busy_usec; 1301 etime /= 1000000; 1302 1303 return(etime); 1304 } 1305