1 /* 2 * Copyright (c) 1997, 1998, 2000, 2001 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$ 29 */ 30 /* 31 * Parts of this program are derived from the original FreeBSD iostat 32 * program: 33 */ 34 /*- 35 * Copyright (c) 1986, 1991, 1993 36 * The Regents of the University of California. All rights reserved. 37 * 38 * Redistribution and use in source and binary forms, with or without 39 * modification, are permitted provided that the following conditions 40 * are met: 41 * 1. Redistributions of source code must retain the above copyright 42 * notice, this list of conditions and the following disclaimer. 43 * 2. Redistributions in binary form must reproduce the above copyright 44 * notice, this list of conditions and the following disclaimer in the 45 * documentation and/or other materials provided with the distribution. 46 * 3. All advertising materials mentioning features or use of this software 47 * must display the following acknowledgement: 48 * This product includes software developed by the University of 49 * California, Berkeley and its contributors. 50 * 4. Neither the name of the University nor the names of its contributors 51 * may be used to endorse or promote products derived from this software 52 * without specific prior written permission. 53 * 54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 64 * SUCH DAMAGE. 65 */ 66 /* 67 * Ideas for the new iostat statistics output modes taken from the NetBSD 68 * version of iostat: 69 */ 70 /* 71 * Copyright (c) 1996 John M. Vinopal 72 * All rights reserved. 73 * 74 * Redistribution and use in source and binary forms, with or without 75 * modification, are permitted provided that the following conditions 76 * are met: 77 * 1. Redistributions of source code must retain the above copyright 78 * notice, this list of conditions and the following disclaimer. 79 * 2. Redistributions in binary form must reproduce the above copyright 80 * notice, this list of conditions and the following disclaimer in the 81 * documentation and/or other materials provided with the distribution. 82 * 3. All advertising materials mentioning features or use of this software 83 * must display the following acknowledgement: 84 * This product includes software developed for the NetBSD Project 85 * by John M. Vinopal. 86 * 4. The name of the author may not be used to endorse or promote products 87 * derived from this software without specific prior written permission. 88 * 89 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 90 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 91 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 92 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 93 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 94 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 95 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 96 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 97 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 98 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 99 * SUCH DAMAGE. 100 */ 101 102 103 #include <sys/param.h> 104 #include <sys/errno.h> 105 #include <sys/resource.h> 106 #include <sys/sysctl.h> 107 108 #include <err.h> 109 #include <ctype.h> 110 #include <fcntl.h> 111 #include <kvm.h> 112 #include <nlist.h> 113 #include <stdio.h> 114 #include <stdlib.h> 115 #include <string.h> 116 #include <unistd.h> 117 #include <limits.h> 118 #include <devstat.h> 119 #include <math.h> 120 121 struct nlist namelist[] = { 122 #define X_TK_NIN 0 123 { "_tk_nin" }, 124 #define X_TK_NOUT 1 125 { "_tk_nout" }, 126 #define X_CP_TIME 2 127 { "_cp_time" }, 128 #define X_BOOTTIME 3 129 { "_boottime" }, 130 #define X_END 3 131 { NULL }, 132 }; 133 134 struct statinfo cur, last; 135 int num_devices; 136 struct device_selection *dev_select; 137 int maxshowdevs; 138 volatile sig_atomic_t headercount; 139 int dflag = 0, Iflag = 0, Cflag = 0, Tflag = 0, oflag = 0, Kflag = 0; 140 141 /* local function declarations */ 142 static void usage(void); 143 static void needhdr(int signo); 144 static void phdr(void); 145 static void devstats(int perf_select, long double etime, int havelast); 146 static void cpustats(void); 147 static int readvar(kvm_t *kd, const char *name, int nlid, void *ptr, 148 size_t len); 149 150 static void 151 usage(void) 152 { 153 /* 154 * We also support the following 'traditional' syntax: 155 * iostat [drives] [wait [count]] 156 * This isn't mentioned in the man page, or the usage statement, 157 * but it is supported. 158 */ 159 fprintf(stderr, "usage: iostat [-CdhIKoT?] [-c count] [-M core]" 160 " [-n devs] [-N system]\n" 161 "\t [-t type,if,pass] [-w wait] [drives]\n"); 162 } 163 164 int 165 main(int argc, char **argv) 166 { 167 int c; 168 register int i; 169 int tflag = 0, hflag = 0, cflag = 0, wflag = 0, nflag = 0; 170 int count = 0, waittime = 0; 171 char *memf = NULL, *nlistf = NULL; 172 struct devstat_match *matches; 173 int num_matches = 0; 174 char errbuf[_POSIX2_LINE_MAX]; 175 kvm_t *kd = NULL; 176 long generation; 177 int num_devices_specified; 178 int num_selected, num_selections; 179 long select_generation; 180 char **specified_devices; 181 devstat_select_mode select_mode; 182 int havelast = 0; 183 184 matches = NULL; 185 maxshowdevs = 3; 186 187 while ((c = getopt(argc, argv, "c:CdhIKM:n:N:ot:Tw:?")) != -1) { 188 switch(c) { 189 case 'c': 190 cflag++; 191 count = atoi(optarg); 192 if (count < 1) 193 errx(1, "count %d is < 1", count); 194 break; 195 case 'C': 196 Cflag++; 197 break; 198 case 'd': 199 dflag++; 200 break; 201 case 'h': 202 hflag++; 203 break; 204 case 'I': 205 Iflag++; 206 break; 207 case 'K': 208 Kflag++; 209 break; 210 case 'M': 211 memf = optarg; 212 break; 213 case 'n': 214 nflag++; 215 maxshowdevs = atoi(optarg); 216 if (maxshowdevs < 0) 217 errx(1, "number of devices %d is < 0", 218 maxshowdevs); 219 break; 220 case 'N': 221 nlistf = optarg; 222 break; 223 case 'o': 224 oflag++; 225 break; 226 case 't': 227 tflag++; 228 if (devstat_buildmatch(optarg, &matches, 229 &num_matches) != 0) 230 errx(1, "%s", devstat_errbuf); 231 break; 232 case 'T': 233 Tflag++; 234 break; 235 case 'w': 236 wflag++; 237 waittime = atoi(optarg); 238 if (waittime < 1) 239 errx(1, "wait time is < 1"); 240 break; 241 default: 242 usage(); 243 exit(1); 244 break; 245 } 246 } 247 248 argc -= optind; 249 argv += optind; 250 251 if (nlistf != NULL || memf != NULL) { 252 kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf); 253 254 if (kd == NULL) 255 errx(1, "kvm_openfiles: %s", errbuf); 256 257 if (kvm_nlist(kd, namelist) == -1) 258 errx(1, "kvm_nlist: %s", kvm_geterr(kd)); 259 } 260 261 /* 262 * Make sure that the userland devstat version matches the kernel 263 * devstat version. If not, exit and print a message informing 264 * the user of his mistake. 265 */ 266 if (devstat_checkversion(kd) < 0) 267 errx(1, "%s", devstat_errbuf); 268 269 /* 270 * Make sure Tflag and/or Cflag are set if dflag == 0. If dflag is 271 * greater than 0, they may be 0 or non-zero. 272 */ 273 if (dflag == 0) { 274 Cflag = 1; 275 Tflag = 1; 276 } 277 278 /* 279 * Figure out how many devices we should display. 280 */ 281 if (nflag == 0) { 282 if (oflag > 0) { 283 if ((dflag > 0) && (Cflag == 0) && (Tflag == 0)) 284 maxshowdevs = 5; 285 else if ((dflag > 0) && (Tflag > 0) && (Cflag == 0)) 286 maxshowdevs = 5; 287 else 288 maxshowdevs = 4; 289 } else { 290 if ((dflag > 0) && (Cflag == 0)) 291 maxshowdevs = 4; 292 else 293 maxshowdevs = 3; 294 } 295 } 296 297 /* find out how many devices we have */ 298 if ((num_devices = devstat_getnumdevs(kd)) < 0) 299 err(1, "can't get number of devices"); 300 301 cur.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo)); 302 if (cur.dinfo == NULL) 303 err(1, "malloc failed"); 304 305 last.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo)); 306 if (last.dinfo == NULL) 307 err(1, "malloc failed"); 308 309 bzero(cur.dinfo, sizeof(struct devinfo)); 310 bzero(last.dinfo, sizeof(struct devinfo)); 311 312 /* 313 * Grab all the devices. We don't look to see if the list has 314 * changed here, since it almost certainly has. We only look for 315 * errors. 316 */ 317 if (devstat_getdevs(kd, &cur) == -1) 318 errx(1, "%s", devstat_errbuf); 319 320 num_devices = cur.dinfo->numdevs; 321 generation = cur.dinfo->generation; 322 323 /* 324 * If the user specified any devices on the command line, see if 325 * they are in the list of devices we have now. 326 */ 327 specified_devices = (char **)malloc(sizeof(char *)); 328 if (specified_devices == NULL) 329 err(1, "malloc failed"); 330 331 for (num_devices_specified = 0; *argv; ++argv) { 332 if (isdigit(**argv)) 333 break; 334 num_devices_specified++; 335 specified_devices = (char **)realloc(specified_devices, 336 sizeof(char *) * 337 num_devices_specified); 338 if (specified_devices == NULL) 339 err(1, "realloc failed"); 340 341 specified_devices[num_devices_specified - 1] = *argv; 342 343 } 344 if (nflag == 0 && maxshowdevs < num_devices_specified) 345 maxshowdevs = num_devices_specified; 346 347 dev_select = NULL; 348 349 if ((num_devices_specified == 0) && (num_matches == 0)) 350 select_mode = DS_SELECT_ADD; 351 else 352 select_mode = DS_SELECT_ONLY; 353 354 /* 355 * At this point, selectdevs will almost surely indicate that the 356 * device list has changed, so we don't look for return values of 0 357 * or 1. If we get back -1, though, there is an error. 358 */ 359 if (devstat_selectdevs(&dev_select, &num_selected, 360 &num_selections, &select_generation, generation, 361 cur.dinfo->devices, num_devices, matches, 362 num_matches, specified_devices, 363 num_devices_specified, select_mode, maxshowdevs, 364 hflag) == -1) 365 errx(1, "%s", devstat_errbuf); 366 367 /* 368 * Look for the traditional wait time and count arguments. 369 */ 370 if (*argv) { 371 waittime = atoi(*argv); 372 373 /* Let the user know he goofed, but keep going anyway */ 374 if (wflag != 0) 375 warnx("discarding previous wait interval, using" 376 " %d instead", waittime); 377 wflag++; 378 379 if (*++argv) { 380 count = atoi(*argv); 381 if (cflag != 0) 382 warnx("discarding previous count, using %d" 383 " instead", count); 384 cflag++; 385 } else 386 count = -1; 387 } 388 389 /* 390 * If the user specified a count, but not an interval, we default 391 * to an interval of 1 second. 392 */ 393 if ((wflag == 0) && (cflag > 0)) 394 waittime = 1; 395 396 /* 397 * If the user specified a wait time, but not a count, we want to 398 * go on ad infinitum. This can be redundant if the user uses the 399 * traditional method of specifying the wait, since in that case we 400 * already set count = -1 above. Oh well. 401 */ 402 if ((wflag > 0) && (cflag == 0)) 403 count = -1; 404 405 bzero(&cur.cp_time, sizeof(cur.cp_time)); 406 cur.tk_nout = 0; 407 cur.tk_nin = 0; 408 409 /* 410 * Set the snap time to the system boot time (ie: zero), so the 411 * stats are calculated since system boot. 412 */ 413 cur.snap_time = 0; 414 415 /* 416 * If the user stops the program (control-Z) and then resumes it, 417 * print out the header again. 418 */ 419 (void)signal(SIGCONT, needhdr); 420 421 for (headercount = 1;;) { 422 struct devinfo *tmp_dinfo; 423 long tmp; 424 long double etime; 425 426 if (Tflag > 0) { 427 if ((readvar(kd, "kern.tty_nin", X_TK_NIN, &cur.tk_nin, 428 sizeof(cur.tk_nin)) != 0) 429 || (readvar(kd, "kern.tty_nout", X_TK_NOUT, 430 &cur.tk_nout, sizeof(cur.tk_nout))!= 0)) { 431 Tflag = 0; 432 warnx("disabling TTY statistics"); 433 } 434 } 435 436 if (Cflag > 0) { 437 if (readvar(kd, "kern.cp_time", X_CP_TIME, 438 &cur.cp_time, sizeof(cur.cp_time)) != 0) { 439 Cflag = 0; 440 warnx("disabling CPU time statistics"); 441 } 442 } 443 444 if (!--headercount) { 445 phdr(); 446 headercount = 20; 447 } 448 449 tmp_dinfo = last.dinfo; 450 last.dinfo = cur.dinfo; 451 cur.dinfo = tmp_dinfo; 452 453 last.snap_time = cur.snap_time; 454 455 /* 456 * Here what we want to do is refresh our device stats. 457 * devstat_getdevs() returns 1 when the device list has changed. 458 * If the device list has changed, we want to go through 459 * the selection process again, in case a device that we 460 * were previously displaying has gone away. 461 */ 462 switch (devstat_getdevs(kd, &cur)) { 463 case -1: 464 errx(1, "%s", devstat_errbuf); 465 break; 466 case 1: { 467 int retval; 468 469 num_devices = cur.dinfo->numdevs; 470 generation = cur.dinfo->generation; 471 retval = devstat_selectdevs(&dev_select, &num_selected, 472 &num_selections, 473 &select_generation, 474 generation, 475 cur.dinfo->devices, 476 num_devices, matches, 477 num_matches, 478 specified_devices, 479 num_devices_specified, 480 select_mode, maxshowdevs, 481 hflag); 482 switch(retval) { 483 case -1: 484 errx(1, "%s", devstat_errbuf); 485 break; 486 case 1: 487 phdr(); 488 headercount = 20; 489 break; 490 default: 491 break; 492 } 493 break; 494 } 495 default: 496 break; 497 } 498 499 /* 500 * We only want to re-select devices if we're in 'top' 501 * mode. This is the only mode where the devices selected 502 * could actually change. 503 */ 504 if (hflag > 0) { 505 int retval; 506 retval = devstat_selectdevs(&dev_select, &num_selected, 507 &num_selections, 508 &select_generation, 509 generation, 510 cur.dinfo->devices, 511 num_devices, matches, 512 num_matches, 513 specified_devices, 514 num_devices_specified, 515 select_mode, maxshowdevs, 516 hflag); 517 switch(retval) { 518 case -1: 519 errx(1,"%s", devstat_errbuf); 520 break; 521 case 1: 522 phdr(); 523 headercount = 20; 524 break; 525 default: 526 break; 527 } 528 } 529 530 if (Tflag > 0) { 531 tmp = cur.tk_nin; 532 cur.tk_nin -= last.tk_nin; 533 last.tk_nin = tmp; 534 tmp = cur.tk_nout; 535 cur.tk_nout -= last.tk_nout; 536 last.tk_nout = tmp; 537 } 538 539 etime = cur.snap_time - last.snap_time; 540 541 if (etime == 0.0) 542 etime = 1.0; 543 544 for (i = 0; i < CPUSTATES; i++) { 545 tmp = cur.cp_time[i]; 546 cur.cp_time[i] -= last.cp_time[i]; 547 last.cp_time[i] = tmp; 548 } 549 550 if (Tflag > 0) 551 printf("%4.0Lf%5.0Lf", cur.tk_nin / etime, 552 cur.tk_nout/etime); 553 554 devstats(hflag, etime, havelast); 555 556 if (Cflag > 0) 557 cpustats(); 558 559 printf("\n"); 560 fflush(stdout); 561 562 if (count >= 0 && --count <= 0) 563 break; 564 565 sleep(waittime); 566 havelast = 1; 567 } 568 569 exit(0); 570 } 571 572 /* 573 * Force a header to be prepended to the next output. 574 */ 575 void 576 needhdr(int signo) 577 { 578 579 headercount = 1; 580 } 581 582 static void 583 phdr(void) 584 { 585 register int i; 586 int printed; 587 588 if (Tflag > 0) 589 (void)printf(" tty"); 590 for (i = 0, printed=0;(i < num_devices) && (printed < maxshowdevs);i++){ 591 int di; 592 if ((dev_select[i].selected != 0) 593 && (dev_select[i].selected <= maxshowdevs)) { 594 di = dev_select[i].position; 595 if (oflag > 0) 596 (void)printf("%12.6s%d ", 597 cur.dinfo->devices[di].device_name, 598 cur.dinfo->devices[di].unit_number); 599 else 600 printf("%15.6s%d ", 601 cur.dinfo->devices[di].device_name, 602 cur.dinfo->devices[di].unit_number); 603 printed++; 604 } 605 } 606 if (Cflag > 0) 607 (void)printf(" cpu\n"); 608 else 609 (void)printf("\n"); 610 611 if (Tflag > 0) 612 (void)printf(" tin tout"); 613 614 for (i=0, printed = 0;(i < num_devices) && (printed < maxshowdevs);i++){ 615 if ((dev_select[i].selected != 0) 616 && (dev_select[i].selected <= maxshowdevs)) { 617 if (oflag > 0) { 618 if (Iflag == 0) 619 (void)printf(" sps tps msps "); 620 else 621 (void)printf(" blk xfr msps "); 622 } else { 623 if (Iflag == 0) 624 printf(" KB/t tps MB/s "); 625 else 626 printf(" KB/t xfrs MB "); 627 } 628 printed++; 629 } 630 } 631 if (Cflag > 0) 632 (void)printf(" us ni sy in id\n"); 633 else 634 printf("\n"); 635 636 } 637 638 static void 639 devstats(int perf_select, long double etime, int havelast) 640 { 641 register int dn; 642 long double transfers_per_second; 643 long double kb_per_transfer, mb_per_second; 644 u_int64_t total_bytes, total_transfers, total_blocks; 645 long double total_mb; 646 long double blocks_per_second, ms_per_transaction; 647 648 for (dn = 0; dn < num_devices; dn++) { 649 int di; 650 651 if (((perf_select == 0) && (dev_select[dn].selected == 0)) 652 || (dev_select[dn].selected > maxshowdevs)) 653 continue; 654 655 di = dev_select[dn].position; 656 657 if (devstat_compute_statistics(&cur.dinfo->devices[di], 658 havelast ? &last.dinfo->devices[di] : NULL, etime, 659 DSM_TOTAL_BYTES, &total_bytes, 660 DSM_TOTAL_TRANSFERS, &total_transfers, 661 DSM_TOTAL_BLOCKS, &total_blocks, 662 DSM_KB_PER_TRANSFER, &kb_per_transfer, 663 DSM_TRANSFERS_PER_SECOND, &transfers_per_second, 664 DSM_MB_PER_SECOND, &mb_per_second, 665 DSM_BLOCKS_PER_SECOND, &blocks_per_second, 666 DSM_MS_PER_TRANSACTION, &ms_per_transaction, 667 DSM_NONE) != 0) 668 errx(1, "%s", devstat_errbuf); 669 670 if (perf_select != 0) { 671 dev_select[dn].bytes = total_bytes; 672 if ((dev_select[dn].selected == 0) 673 || (dev_select[dn].selected > maxshowdevs)) 674 continue; 675 } 676 677 if (Kflag) { 678 int block_size = cur.dinfo->devices[di].block_size; 679 total_blocks = total_blocks * (block_size ? 680 block_size : 512) / 1024; 681 } 682 683 if (oflag > 0) { 684 int msdig = (ms_per_transaction < 100.0) ? 1 : 0; 685 686 if (Iflag == 0) 687 printf("%4.0Lf%4.0Lf%5.*Lf ", 688 blocks_per_second, 689 transfers_per_second, 690 msdig, 691 ms_per_transaction); 692 else 693 printf("%4.1qu%4.1qu%5.*Lf ", 694 total_blocks, 695 total_transfers, 696 msdig, 697 ms_per_transaction); 698 } else { 699 if (Iflag == 0) 700 printf(" %5.2Lf %3.0Lf %5.2Lf ", 701 kb_per_transfer, 702 transfers_per_second, 703 mb_per_second); 704 else { 705 total_mb = total_bytes; 706 total_mb /= 1024 * 1024; 707 708 printf(" %5.2Lf %3.1qu %5.2Lf ", 709 kb_per_transfer, 710 total_transfers, 711 total_mb); 712 } 713 } 714 } 715 } 716 717 static void 718 cpustats(void) 719 { 720 register int state; 721 double time; 722 723 time = 0.0; 724 725 for (state = 0; state < CPUSTATES; ++state) 726 time += cur.cp_time[state]; 727 for (state = 0; state < CPUSTATES; ++state) 728 printf(" %2.0f", 729 rint(100. * cur.cp_time[state] / (time ? time : 1))); 730 } 731 732 static int 733 readvar(kvm_t *kd, const char *name, int nlid, void *ptr, size_t len) 734 { 735 if (kd != NULL) { 736 ssize_t nbytes; 737 738 nbytes = kvm_read(kd, nlid, ptr, len); 739 740 if (nbytes == 0) { 741 warnx("kvm_read(%s): %s", name, kvm_geterr(kd)); 742 return (1); 743 } 744 if (nbytes != len) { 745 warnx("kvm_read(%s): expected %lu bytes, got %ld bytes", 746 name, (unsigned long)len, (long)nbytes); 747 return (1); 748 } 749 } else { 750 size_t nlen = len; 751 752 if (sysctlbyname(name, ptr, &nlen, NULL, 0) == -1) { 753 warn("sysctl(%s...) failed", name); 754 return (1); 755 } 756 if (nlen != len) { 757 warnx("sysctl(%s...): expected %lu, got %lu", name, 758 (unsigned long)len, (unsigned long)nlen); 759 return (1); 760 } 761 } 762 return (0); 763 } 764