Esempio n. 1
0
static void
display(void)
{
	double	etime;

	/* Sum up the elapsed ticks. */
	etime = cur.cp_etime;

	/*
	 * If we're showing totals only, then don't divide by the
	 * system time.
	 */
	if (ISSET(todo, SHOW_TOTALS))
		etime = 1.0;

	if (ISSET(todo, SHOW_STATS_X)) {
		drive_statsx(etime);
		goto out;
	}

	if (ISSET(todo, SHOW_TTY))
		printf("%4.0f %5.0f", cur.tk_nin / etime, cur.tk_nout / etime);

	if (ISSET(todo, SHOW_STATS_1)) {
		drive_stats(etime);
	}


	if (ISSET(todo, SHOW_STATS_2)) {
		drive_stats2(etime);
	}


	if (ISSET(todo, SHOW_CPU))
		cpustats();

	(void)printf("\n");

out:
	(void)fflush(stdout);
}
Esempio n. 2
0
int
main(int argc, char **argv)
{
    int c;
    int hflag = 0, cflag = 0, wflag = 0, nflag = 0;
    int count = 0, waittime = 0;
    int headercount;
    int num_devices_specified;
    int havelast = 0;

    maxshowdevs = 3;

    while ((c = getopt(argc, argv, "c:CdIKM:n:oTw:?")) != -1) {
        switch(c) {
        case 'c':
            cflag++;
            count = atoi(optarg);
            if (count < 1)
                errx(1, "count %d is < 1", count);
            break;
        case 'C':
            Cflag++;
            break;
        case 'd':
            dflag++;
            break;
        case 'I':
            Iflag++;
            break;
        case 'K':
            Kflag++;
            break;
        case 'n':
            nflag++;
            maxshowdevs = atoi(optarg);
            if (maxshowdevs < 0)
                errx(1, "number of devices %d is < 0",
                     maxshowdevs);
            break;
        case 'o':
            oflag++;
            break;
        case 'T':
            Tflag++;
            break;
        case 'w':
            wflag++;
            waittime = atoi(optarg);
            if (waittime < 1)
                errx(1, "wait time is < 1");
            break;
        default:
            usage();
            exit(1);
            break;
        }
    }

    argc -= optind;
    argv += optind;

    /*
     * Get the Mach private port.
     */
    host_priv_port = mach_host_self();

    /*
     * Get the I/O Kit communication handle.
     */
    IOMasterPort(bootstrap_port, &masterPort);

    /*
     * Make sure Tflag and/or Cflag are set if dflag == 0.  If dflag is
     * greater than 0, they may be 0 or non-zero.
     */
    if (dflag == 0) {
        Cflag = 1;
        Tflag = 1;
    }

    /*
     * Figure out how many devices we should display if not given
     * an explicit value.
     */
    if (nflag == 0) {
        if (oflag > 0) {
            if ((dflag > 0) && (Cflag == 0) && (Tflag == 0))
                maxshowdevs = 5;
            else if ((dflag > 0) && (Tflag > 0) && (Cflag == 0))
                maxshowdevs = 5;
            else
                maxshowdevs = 4;
        } else {
            if ((dflag > 0) && (Cflag == 0))
                maxshowdevs = 4;
            else
                maxshowdevs = 3;
        }
    }

    /*
     * If the user specified any devices on the command line, record
     * them for monitoring.
     */
    for (num_devices_specified = 0; *argv; ++argv) {
        if (isdigit(**argv))
            break;
        if (record_one_device(*argv))
            errx(1, "can't record '%s' for monitoring");
        num_devices_specified++;
    }
    if (nflag == 0 && maxshowdevs < num_devices_specified)
        maxshowdevs = num_devices_specified;

    /* if no devices were specified, pick them ourselves */
    if ((num_devices_specified == 0) && record_all_devices())
        err(1, "can't find any devices to display");

    /*
     * Look for the traditional wait time and count arguments.
     */
    if (*argv) {
        waittime = atoi(*argv);

        /* Let the user know he goofed, but keep going anyway */
        if (wflag != 0)
            warnx("discarding previous wait interval, using"
                  " %d instead", waittime);
        wflag++;

        if (*++argv) {
            count = atoi(*argv);
            if (cflag != 0)
                warnx("discarding previous count, using %d"
                      " instead", count);
            cflag++;
        } else
            count = -1;
    }

    /*
     * If the user specified a count, but not an interval, we default
     * to an interval of 1 second.
     */
    if ((wflag == 0) && (cflag > 0))
        waittime = 1;

    /*
     * If the user specified a wait time, but not a count, we want to
     * go on ad infinitum.  This can be redundant if the user uses the
     * traditional method of specifying the wait, since in that case we
     * already set count = -1 above.  Oh well.
     */
    if ((wflag > 0) && (cflag == 0))
        count = -1;

    cur.tk_nout = 0;
    cur.tk_nin = 0;

    /*
     * Set the busy time to the system boot time, so the stats are
     * calculated since system boot.
     */
    if (readvar("kern.boottime", &cur_time,	sizeof(cur_time)) != 0)
        exit(1);

    /*
     * If the user stops the program (control-Z) and then resumes it,
     * print out the header again.
     */
    (void)signal(SIGCONT, phdr);

    for (headercount = 1;;) {
        long tmp;
        long double etime;

        if (Tflag > 0) {
            if ((readvar("kern.tty_nin", &cur.tk_nin,
                         sizeof(cur.tk_nin)) != 0)
                    || (readvar("kern.tty_nout",
                                &cur.tk_nout, sizeof(cur.tk_nout))!= 0)) {
                Tflag = 0;
                warnx("disabling TTY statistics");
            }
        }

        if (phdr_flag) {
            phdr_flag = 0;
            do_phdr();
        }

        if (!--headercount) {
            do_phdr();
            headercount = 20;
        }

        last_time = cur_time;
        gettimeofday(&cur_time, NULL);

        if (Tflag > 0) {
            tmp = cur.tk_nin;
            cur.tk_nin -= last.tk_nin;
            last.tk_nin = tmp;
            tmp = cur.tk_nout;
            cur.tk_nout -= last.tk_nout;
            last.tk_nout = tmp;
        }

        etime = compute_etime(cur_time, last_time);

        if (etime == 0.0)
            etime = 1.0;

        if (Tflag > 0)
            printf("%4.0Lf%5.0Lf", cur.tk_nin / etime,
                   cur.tk_nout / etime);

        devstats(hflag, etime, havelast);

        if (Cflag > 0)
            cpustats();

        printf("\n");
        fflush(stdout);

        if (count >= 0 && --count <= 0)
            break;

        sleep(waittime);
        havelast = 1;
    }

    exit(0);
}
Esempio n. 3
0
static void
devstats(int perf_select, long double etime, int havelast)
{
    int dn;
    long double transfers_per_second, transfers_per_second_read;
    long double transfers_per_second_write;
    long double kb_per_transfer, mb_per_second, mb_per_second_read;
    long double mb_per_second_write;
    u_int64_t total_bytes, total_transfers, total_blocks;
    u_int64_t total_bytes_read, total_transfers_read;
    u_int64_t total_bytes_write, total_transfers_write;
    long double busy_pct, busy_time;
    u_int64_t queue_len;
    long double total_mb, blocks_per_second, total_duration;
    long double ms_per_other, ms_per_read, ms_per_write, ms_per_transaction;
    int firstline = 1;
    char *devicename;

    if (xflag > 0) {
        printf("                        extended device statistics  ");
        if (Tflag > 0)
            printf("      tty ");
        if (Cflag > 0)
            printf("           cpu ");
        printf("\n");
        if (Iflag == 0) {
            printf("device     r/s   w/s     kr/s     kw/s "
                   " ms/r  ms/w  ms/o  ms/t qlen  %%b  ");
        } else {
            printf("device           r/i         w/i         kr/i"
                   "         kw/i qlen   tsvc_t/i      sb/i  ");
        }
        if (Tflag > 0)
            printf("tin  tout ");
        if (Cflag > 0)
            printf("us ni sy in id ");
        printf("\n");
    }

    for (dn = 0; dn < num_devices; dn++) {
        int di;

        if (((perf_select == 0) && (dev_select[dn].selected == 0))
                || (dev_select[dn].selected > maxshowdevs))
            continue;

        di = dev_select[dn].position;

        if (devstat_compute_statistics(&cur.dinfo->devices[di],
                                       havelast ? &last.dinfo->devices[di] : NULL, etime,
                                       DSM_TOTAL_BYTES, &total_bytes,
                                       DSM_TOTAL_BYTES_READ, &total_bytes_read,
                                       DSM_TOTAL_BYTES_WRITE, &total_bytes_write,
                                       DSM_TOTAL_TRANSFERS, &total_transfers,
                                       DSM_TOTAL_TRANSFERS_READ, &total_transfers_read,
                                       DSM_TOTAL_TRANSFERS_WRITE, &total_transfers_write,
                                       DSM_TOTAL_BLOCKS, &total_blocks,
                                       DSM_KB_PER_TRANSFER, &kb_per_transfer,
                                       DSM_TRANSFERS_PER_SECOND, &transfers_per_second,
                                       DSM_TRANSFERS_PER_SECOND_READ, &transfers_per_second_read,
                                       DSM_TRANSFERS_PER_SECOND_WRITE, &transfers_per_second_write,
                                       DSM_MB_PER_SECOND, &mb_per_second,
                                       DSM_MB_PER_SECOND_READ, &mb_per_second_read,
                                       DSM_MB_PER_SECOND_WRITE, &mb_per_second_write,
                                       DSM_BLOCKS_PER_SECOND, &blocks_per_second,
                                       DSM_MS_PER_TRANSACTION, &ms_per_transaction,
                                       DSM_MS_PER_TRANSACTION_READ, &ms_per_read,
                                       DSM_MS_PER_TRANSACTION_WRITE, &ms_per_write,
                                       DSM_MS_PER_TRANSACTION_OTHER, &ms_per_other,
                                       DSM_BUSY_PCT, &busy_pct,
                                       DSM_QUEUE_LENGTH, &queue_len,
                                       DSM_TOTAL_DURATION, &total_duration,
                                       DSM_TOTAL_BUSY_TIME, &busy_time,
                                       DSM_NONE) != 0)
            errx(1, "%s", devstat_errbuf);

        if (perf_select != 0) {
            dev_select[dn].bytes = total_bytes;
            if ((dev_select[dn].selected == 0)
                    || (dev_select[dn].selected > maxshowdevs))
                continue;
        }

        if (Kflag > 0 || xflag > 0) {
            int block_size = cur.dinfo->devices[di].block_size;
            total_blocks = total_blocks * (block_size ?
                                           block_size : 512) / 1024;
        }

        if (xflag > 0) {
            if (asprintf(&devicename, "%s%d",
                         cur.dinfo->devices[di].device_name,
                         cur.dinfo->devices[di].unit_number) == -1)
                err(1, "asprintf");
            /*
             * If zflag is set, skip any devices with zero I/O.
             */
            if (zflag == 0 || transfers_per_second_read > 0.05 ||
                    transfers_per_second_write > 0.05 ||
                    mb_per_second_read > ((long double).0005)/1024 ||
                    mb_per_second_write > ((long double).0005)/1024 ||
                    busy_pct > 0.5) {
                if (Iflag == 0)
                    printf("%-8.8s %5d %5d %8.1Lf "
                           "%8.1Lf %5d %5d %5d %5d "
                           "%4" PRIu64 " %3.0Lf ",
                           devicename,
                           (int)transfers_per_second_read,
                           (int)transfers_per_second_write,
                           mb_per_second_read * 1024,
                           mb_per_second_write * 1024,
                           (int)ms_per_read, (int)ms_per_write,
                           (int)ms_per_other,
                           (int)ms_per_transaction,
                           queue_len, busy_pct);
                else
                    printf("%-8.8s %11.1Lf %11.1Lf "
                           "%12.1Lf %12.1Lf %4" PRIu64
                           " %10.1Lf %9.1Lf ",
                           devicename,
                           (long double)total_transfers_read,
                           (long double)total_transfers_write,
                           (long double)
                           total_bytes_read / 1024,
                           (long double)
                           total_bytes_write / 1024,
                           queue_len,
                           total_duration, busy_time);
                if (firstline) {
                    /*
                     * If this is the first device
                     * we're printing, also print
                     * CPU or TTY stats if requested.
                     */
                    firstline = 0;
                    if (Tflag > 0)
                        printf("%4.0Lf%5.0Lf",
                               cur.tk_nin / etime,
                               cur.tk_nout / etime);
                    if (Cflag > 0)
                        cpustats();
                }
                printf("\n");
            }
            free(devicename);
        } else if (oflag > 0) {
            int msdig = (ms_per_transaction < 100.0) ? 1 : 0;

            if (Iflag == 0)
                printf("%4.0Lf%4.0Lf%5.*Lf ",
                       blocks_per_second,
                       transfers_per_second,
                       msdig,
                       ms_per_transaction);
            else
                printf("%4.1" PRIu64 "%4.1" PRIu64 "%5.*Lf ",
                       total_blocks,
                       total_transfers,
                       msdig,
                       ms_per_transaction);
        } else {
            if (Iflag == 0)
                printf(" %5.2Lf %3.0Lf %5.2Lf ",
                       kb_per_transfer,
                       transfers_per_second,
                       mb_per_second);
            else {
                total_mb = total_bytes;
                total_mb /= 1024 * 1024;

                printf(" %5.2Lf %3.1" PRIu64 " %5.2Lf ",
                       kb_per_transfer,
                       total_transfers,
                       total_mb);
            }
        }
    }
    if (xflag > 0 && zflag > 0 && firstline == 1 &&
            (Tflag > 0 || Cflag > 0)) {
        /*
         * If zflag is set and we did not print any device
         * lines I/O because they were all zero,
         * print TTY/CPU stats.
         */
        printf("%52s","");
        if (Tflag > 0)
            printf("%4.0Lf %5.0Lf", cur.tk_nin / etime,
                   cur.tk_nout / etime);
        if (Cflag > 0)
            cpustats();
        printf("\n");
    }
}
Esempio n. 4
0
int
main(int argc, char **argv)
{
    int c, i;
    int tflag = 0, hflag = 0, cflag = 0, wflag = 0, nflag = 0;
    int count = 0, waittime = 0;
    char *memf = NULL, *nlistf = NULL;
    struct devstat_match *matches;
    struct itimerval alarmspec;
    int num_matches = 0;
    char errbuf[_POSIX2_LINE_MAX];
    kvm_t *kd = NULL;
    long generation;
    int num_devices_specified;
    int num_selected, num_selections;
    long select_generation;
    char **specified_devices;
    devstat_select_mode select_mode;
    float f;
    int havelast = 0;

    matches = NULL;
    maxshowdevs = 3;

    while ((c = getopt(argc, argv, "c:CdhIKM:n:N:ot:Tw:xz?")) != -1) {
        switch(c) {
        case 'c':
            cflag++;
            count = atoi(optarg);
            if (count < 1)
                errx(1, "count %d is < 1", count);
            break;
        case 'C':
            Cflag++;
            break;
        case 'd':
            dflag++;
            break;
        case 'h':
            hflag++;
            break;
        case 'I':
            Iflag++;
            break;
        case 'K':
            Kflag++;
            break;
        case 'M':
            memf = optarg;
            break;
        case 'n':
            nflag++;
            maxshowdevs = atoi(optarg);
            if (maxshowdevs < 0)
                errx(1, "number of devices %d is < 0",
                     maxshowdevs);
            break;
        case 'N':
            nlistf = optarg;
            break;
        case 'o':
            oflag++;
            break;
        case 't':
            tflag++;
            if (devstat_buildmatch(optarg, &matches,
                                   &num_matches) != 0)
                errx(1, "%s", devstat_errbuf);
            break;
        case 'T':
            Tflag++;
            break;
        case 'w':
            wflag++;
            f = atof(optarg);
            waittime = f * 1000;
            if (waittime < 1)
                errx(1, "wait time is < 1ms");
            break;
        case 'x':
            xflag++;
            break;
        case 'z':
            zflag++;
            break;
        default:
            usage();
            exit(1);
            break;
        }
    }

    argc -= optind;
    argv += optind;

    if (nlistf != NULL || memf != NULL) {
        kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);

        if (kd == NULL)
            errx(1, "kvm_openfiles: %s", errbuf);

        if (kvm_nlist(kd, namelist) == -1)
            errx(1, "kvm_nlist: %s", kvm_geterr(kd));
    }

    /*
     * Make sure that the userland devstat version matches the kernel
     * devstat version.  If not, exit and print a message informing
     * the user of his mistake.
     */
    if (devstat_checkversion(kd) < 0)
        errx(1, "%s", devstat_errbuf);

    /*
     * Make sure Tflag and/or Cflag are set if dflag == 0.  If dflag is
     * greater than 0, they may be 0 or non-zero.
     */
    if (dflag == 0 && xflag == 0) {
        Cflag = 1;
        Tflag = 1;
    }

    /* find out how many devices we have */
    if ((num_devices = devstat_getnumdevs(kd)) < 0)
        err(1, "can't get number of devices");

    /*
     * Figure out how many devices we should display.
     */
    if (nflag == 0) {
        if (xflag > 0)
            maxshowdevs = num_devices;
        else if (oflag > 0) {
            if ((dflag > 0) && (Cflag == 0) && (Tflag == 0))
                maxshowdevs = 5;
            else if ((dflag > 0) && (Tflag > 0) && (Cflag == 0))
                maxshowdevs = 5;
            else
                maxshowdevs = 4;
        } else {
            if ((dflag > 0) && (Cflag == 0))
                maxshowdevs = 4;
            else
                maxshowdevs = 3;
        }
    }

    cur.dinfo = (struct devinfo *)calloc(1, sizeof(struct devinfo));
    if (cur.dinfo == NULL)
        err(1, "calloc failed");

    last.dinfo = (struct devinfo *)calloc(1, sizeof(struct devinfo));
    if (last.dinfo == NULL)
        err(1, "calloc failed");

    /*
     * Grab all the devices.  We don't look to see if the list has
     * changed here, since it almost certainly has.  We only look for
     * errors.
     */
    if (devstat_getdevs(kd, &cur) == -1)
        errx(1, "%s", devstat_errbuf);

    num_devices = cur.dinfo->numdevs;
    generation = cur.dinfo->generation;

    /*
     * If the user specified any devices on the command line, see if
     * they are in the list of devices we have now.
     */
    specified_devices = (char **)malloc(sizeof(char *));
    if (specified_devices == NULL)
        err(1, "malloc failed");

    for (num_devices_specified = 0; *argv; ++argv) {
        if (isdigit(**argv))
            break;
        num_devices_specified++;
        specified_devices = (char **)realloc(specified_devices,
                                             sizeof(char *) *
                                             num_devices_specified);
        if (specified_devices == NULL)
            err(1, "realloc failed");

        specified_devices[num_devices_specified - 1] = *argv;

    }
    if (nflag == 0 && maxshowdevs < num_devices_specified)
        maxshowdevs = num_devices_specified;

    dev_select = NULL;

    if ((num_devices_specified == 0) && (num_matches == 0))
        select_mode = DS_SELECT_ADD;
    else
        select_mode = DS_SELECT_ONLY;

    /*
     * At this point, selectdevs will almost surely indicate that the
     * device list has changed, so we don't look for return values of 0
     * or 1.  If we get back -1, though, there is an error.
     */
    if (devstat_selectdevs(&dev_select, &num_selected,
                           &num_selections, &select_generation, generation,
                           cur.dinfo->devices, num_devices, matches,
                           num_matches, specified_devices,
                           num_devices_specified, select_mode, maxshowdevs,
                           hflag) == -1)
        errx(1, "%s", devstat_errbuf);

    /*
     * Look for the traditional wait time and count arguments.
     */
    if (*argv) {
        f = atof(*argv);
        waittime = f * 1000;

        /* Let the user know he goofed, but keep going anyway */
        if (wflag != 0)
            warnx("discarding previous wait interval, using"
                  " %g instead", waittime / 1000.0);
        wflag++;

        if (*++argv) {
            count = atoi(*argv);
            if (cflag != 0)
                warnx("discarding previous count, using %d"
                      " instead", count);
            cflag++;
        } else
            count = -1;
    }

    /*
     * If the user specified a count, but not an interval, we default
     * to an interval of 1 second.
     */
    if ((wflag == 0) && (cflag > 0))
        waittime = 1 * 1000;

    /*
     * If the user specified a wait time, but not a count, we want to
     * go on ad infinitum.  This can be redundant if the user uses the
     * traditional method of specifying the wait, since in that case we
     * already set count = -1 above.  Oh well.
     */
    if ((wflag > 0) && (cflag == 0))
        count = -1;

    bzero(cur.cp_time, sizeof(cur.cp_time));
    cur.tk_nout = 0;
    cur.tk_nin = 0;

    /*
     * Set the snap time to the system boot time (ie: zero), so the
     * stats are calculated since system boot.
     */
    cur.snap_time = 0;

    /*
     * If the user stops the program (control-Z) and then resumes it,
     * print out the header again.
     */
    (void)signal(SIGCONT, needhdr);

    /*
     * If our standard output is a tty, then install a SIGWINCH handler
     * and set wresized so that our first iteration through the main
     * iostat loop will peek at the terminal's current rows to find out
     * how many lines can fit in a screenful of output.
     */
    if (isatty(fileno(stdout)) != 0) {
        wresized = 1;
        (void)signal(SIGWINCH, needresize);
    } else {
        wresized = 0;
        wrows = IOSTAT_DEFAULT_ROWS;
    }

    /*
     * Register a SIGINT handler so that we can print out final statistics
     * when we get that signal
     */
    (void)signal(SIGINT, needreturn);

    /*
     * Register a SIGALRM handler to implement sleeps if the user uses the
     * -c or -w options
     */
    (void)signal(SIGALRM, alarm_clock);
    alarmspec.it_interval.tv_sec = waittime / 1000;
    alarmspec.it_interval.tv_usec = 1000 * (waittime % 1000);
    alarmspec.it_value.tv_sec = waittime / 1000;
    alarmspec.it_value.tv_usec = 1000 * (waittime % 1000);
    setitimer(ITIMER_REAL, &alarmspec, NULL);

    for (headercount = 1;;) {
        struct devinfo *tmp_dinfo;
        long tmp;
        long double etime;
        sigset_t sigmask, oldsigmask;

        if (Tflag > 0) {
            if ((readvar(kd, "kern.tty_nin", X_TTY_NIN, &cur.tk_nin,
                         sizeof(cur.tk_nin)) != 0)
                    || (readvar(kd, "kern.tty_nout", X_TTY_NOUT,
                                &cur.tk_nout, sizeof(cur.tk_nout))!= 0)) {
                Tflag = 0;
                warnx("disabling TTY statistics");
            }
        }

        if (Cflag > 0) {
            if (kd == NULL) {
                if (readvar(kd, "kern.cp_time", 0,
                            &cur.cp_time, sizeof(cur.cp_time)) != 0)
                    Cflag = 0;
            } else {
                if (kvm_getcptime(kd, cur.cp_time) < 0) {
                    warnx("kvm_getcptime: %s",
                          kvm_geterr(kd));
                    Cflag = 0;
                }
            }
            if (Cflag == 0)
                warnx("disabling CPU time statistics");
        }

        if (!--headercount) {
            phdr();
            if (wresized != 0)
                doresize();
            headercount = wrows;
        }

        tmp_dinfo = last.dinfo;
        last.dinfo = cur.dinfo;
        cur.dinfo = tmp_dinfo;

        last.snap_time = cur.snap_time;

        /*
         * Here what we want to do is refresh our device stats.
         * devstat_getdevs() returns 1 when the device list has changed.
         * If the device list has changed, we want to go through
         * the selection process again, in case a device that we
         * were previously displaying has gone away.
         */
        switch (devstat_getdevs(kd, &cur)) {
        case -1:
            errx(1, "%s", devstat_errbuf);
            break;
        case 1: {
            int retval;

            num_devices = cur.dinfo->numdevs;
            generation = cur.dinfo->generation;
            retval = devstat_selectdevs(&dev_select, &num_selected,
                                        &num_selections,
                                        &select_generation,
                                        generation,
                                        cur.dinfo->devices,
                                        num_devices, matches,
                                        num_matches,
                                        specified_devices,
                                        num_devices_specified,
                                        select_mode, maxshowdevs,
                                        hflag);
            switch(retval) {
            case -1:
                errx(1, "%s", devstat_errbuf);
                break;
            case 1:
                phdr();
                if (wresized != 0)
                    doresize();
                headercount = wrows;
                break;
            default:
                break;
            }
            break;
        }
        default:
            break;
        }

        /*
         * We only want to re-select devices if we're in 'top'
         * mode.  This is the only mode where the devices selected
         * could actually change.
         */
        if (hflag > 0) {
            int retval;
            retval = devstat_selectdevs(&dev_select, &num_selected,
                                        &num_selections,
                                        &select_generation,
                                        generation,
                                        cur.dinfo->devices,
                                        num_devices, matches,
                                        num_matches,
                                        specified_devices,
                                        num_devices_specified,
                                        select_mode, maxshowdevs,
                                        hflag);
            switch(retval) {
            case -1:
                errx(1,"%s", devstat_errbuf);
                break;
            case 1:
                phdr();
                if (wresized != 0)
                    doresize();
                headercount = wrows;
                break;
            default:
                break;
            }
        }

        if (Tflag > 0) {
            tmp = cur.tk_nin;
            cur.tk_nin -= last.tk_nin;
            last.tk_nin = tmp;
            tmp = cur.tk_nout;
            cur.tk_nout -= last.tk_nout;
            last.tk_nout = tmp;
        }

        etime = cur.snap_time - last.snap_time;

        if (etime == 0.0)
            etime = 1.0;

        for (i = 0; i < CPUSTATES; i++) {
            tmp = cur.cp_time[i];
            cur.cp_time[i] -= last.cp_time[i];
            last.cp_time[i] = tmp;
        }

        if (xflag == 0 && Tflag > 0)
            printf("%4.0Lf %5.0Lf", cur.tk_nin / etime,
                   cur.tk_nout / etime);

        devstats(hflag, etime, havelast);

        if (xflag == 0) {
            if (Cflag > 0)
                cpustats();

            printf("\n");
        }
        fflush(stdout);

        if ((count >= 0 && --count <= 0) || return_requested)
            break;

        /*
         * Use sigsuspend to safely sleep until either signal is
         * received
         */
        alarm_rang = 0;
        sigemptyset(&sigmask);
        sigaddset(&sigmask, SIGINT);
        sigaddset(&sigmask, SIGALRM);
        sigprocmask(SIG_BLOCK, &sigmask, &oldsigmask);
        while (! (alarm_rang || return_requested) ) {
            sigsuspend(&oldsigmask);
        }
        sigprocmask(SIG_UNBLOCK, &sigmask, NULL);

        havelast = 1;
    }

    exit(0);
}
Esempio n. 5
0
static void
dovmstat(unsigned int interval, int reps)
{
	struct vmtotal total;
	time_t uptime, halfuptime;
	struct devinfo *tmp_dinfo;
	size_t size;
	int ncpus, maxid;
	u_long cpumask;
	int rate_adj;

	uptime = getuptime() / 1000000000LL;
	halfuptime = uptime / 2;
	rate_adj = 1;
	ncpus = 1;
	maxid = 0;

	/*
	 * If the user stops the program (control-Z) and then resumes it,
	 * print out the header again.
	 */
	(void)signal(SIGCONT, needhdr);

	/*
	 * If our standard output is a tty, then install a SIGWINCH handler
	 * and set wresized so that our first iteration through the main
	 * vmstat loop will peek at the terminal's current rows to find out
	 * how many lines can fit in a screenful of output.
	 */
	if (isatty(fileno(stdout)) != 0) {
		wresized = 1;
		(void)signal(SIGWINCH, needresize);
	} else {
		wresized = 0;
		winlines = VMSTAT_DEFAULT_LINES;
	}

	if (kd != NULL) {
		if (namelist[X_STATHZ].n_type != 0 &&
		    namelist[X_STATHZ].n_value != 0)
			kread(X_STATHZ, &hz, sizeof(hz));
		if (!hz)
			kread(X_HZ, &hz, sizeof(hz));
	} else {
		struct clockinfo clockrate;

		size = sizeof(clockrate);
		mysysctl("kern.clockrate", &clockrate, &size, NULL, 0);
		if (size != sizeof(clockrate))
			errx(1, "clockrate size mismatch");
		hz = clockrate.hz;
	}

	if (Pflag) {
		ncpus = getcpuinfo(&cpumask, &maxid);
		size_cp_times = sizeof(long) * (maxid + 1) * CPUSTATES;
		cur_cp_times = calloc(1, size_cp_times);
		last_cp_times = calloc(1, size_cp_times);
	}
	for (hdrcnt = 1;;) {
		if (!--hdrcnt)
			printhdr(maxid, cpumask);
		if (kd != NULL) {
			if (kvm_getcptime(kd, cur.cp_time) < 0)
				errx(1, "kvm_getcptime: %s", kvm_geterr(kd));
		} else {
			size = sizeof(cur.cp_time);
			mysysctl("kern.cp_time", &cur.cp_time, &size, NULL, 0);
			if (size != sizeof(cur.cp_time))
				errx(1, "cp_time size mismatch");
		}
		if (Pflag) {
			size = size_cp_times;
			mysysctl("kern.cp_times", cur_cp_times, &size, NULL, 0);
			if (size != size_cp_times)
				errx(1, "cp_times mismatch");
		}

		tmp_dinfo = last.dinfo;
		last.dinfo = cur.dinfo;
		cur.dinfo = tmp_dinfo;
		last.snap_time = cur.snap_time;

		/*
		 * Here what we want to do is refresh our device stats.
		 * getdevs() returns 1 when the device list has changed.
		 * If the device list has changed, we want to go through
		 * the selection process again, in case a device that we
		 * were previously displaying has gone away.
		 */
		switch (devstat_getdevs(NULL, &cur)) {
		case -1:
			errx(1, "%s", devstat_errbuf);
			break;
		case 1: {
			int retval;

			num_devices = cur.dinfo->numdevs;
			generation = cur.dinfo->generation;

			retval = devstat_selectdevs(&dev_select, &num_selected,
					    &num_selections, &select_generation,
					    generation, cur.dinfo->devices,
					    num_devices, matches, num_matches,
					    specified_devices,
					    num_devices_specified, select_mode,
					    maxshowdevs, 0);
			switch (retval) {
			case -1:
				errx(1, "%s", devstat_errbuf);
				break;
			case 1:
				printhdr(maxid, cpumask);
				break;
			default:
				break;
			}
		}
		default:
			break;
		}

		fill_vmmeter(&sum);
		fill_vmtotal(&total);
		(void)printf("%1d %1d %1d",
		    total.t_rq - 1, total.t_dw + total.t_pw, total.t_sw);
#define vmstat_pgtok(a) ((a) * (sum.v_page_size >> 10))
#define	rate(x)	(((x) * rate_adj + halfuptime) / uptime)	/* round */
		if (hflag) {
			printf("");
			prthuman(total.t_avm * (u_int64_t)sum.v_page_size, 5);
			printf(" ");
			prthuman(total.t_free * (u_int64_t)sum.v_page_size, 5);
			printf(" ");
			(void)printf("%5lu ",
			    (unsigned long)rate(sum.v_vm_faults -
			    osum.v_vm_faults));
		} else {
			printf(" %7d", vmstat_pgtok(total.t_avm));
			printf(" %7d ", vmstat_pgtok(total.t_free));
			(void)printf("%4lu ",
			    (unsigned long)rate(sum.v_vm_faults -
			    osum.v_vm_faults));
		}
		(void)printf("%3lu ",
		    (unsigned long)rate(sum.v_reactivated - osum.v_reactivated));
		(void)printf("%3lu ",
		    (unsigned long)rate(sum.v_swapin + sum.v_vnodein -
		    (osum.v_swapin + osum.v_vnodein)));
		(void)printf("%3lu ",
		    (unsigned long)rate(sum.v_swapout + sum.v_vnodeout -
		    (osum.v_swapout + osum.v_vnodeout)));
		(void)printf("%5lu ",
		    (unsigned long)rate(sum.v_tfree - osum.v_tfree));
		(void)printf("%4lu ",
		    (unsigned long)rate(sum.v_pdpages - osum.v_pdpages));
		devstats();
		(void)printf("%4lu %5lu %5lu",
		    (unsigned long)rate(sum.v_intr - osum.v_intr),
		    (unsigned long)rate(sum.v_syscall - osum.v_syscall),
		    (unsigned long)rate(sum.v_swtch - osum.v_swtch));
		if (Pflag)
			pcpustats(ncpus, cpumask, maxid);
		else
			cpustats();
		(void)printf("\n");
		(void)fflush(stdout);
		if (reps >= 0 && --reps <= 0)
			break;
		osum = sum;
		uptime = interval;
		rate_adj = 1000;
		/*
		 * We round upward to avoid losing low-frequency events
		 * (i.e., >= 1 per interval but < 1 per millisecond).
		 */
		if (interval != 1)
			halfuptime = (uptime + 1) / 2;
		else
			halfuptime = 0;
		(void)usleep(interval * 1000);
	}
}
Esempio n. 6
0
static void
dovmstat(u_int interval, int reps)
{
	struct vmtotal total;
	struct devinfo *tmp_dinfo;
	size_t vmm_size = sizeof(vmm);
	size_t vms_size = sizeof(vms);
	size_t vmt_size = sizeof(total);
	int initial = 1;

	signal(SIGCONT, needhdr);

	for (hdrcnt = 1;;) {
		if (!--hdrcnt)
			printhdr();
		if (kinfo_get_sched_cputime(&cp_time))
			err(1, "kinfo_get_sched_cputime");

		tmp_dinfo = last.dinfo;
		last.dinfo = cur.dinfo;
		cur.dinfo = tmp_dinfo;
		last.busy_time = cur.busy_time;

		/*
		 * Here what we want to do is refresh our device stats.
		 * getdevs() returns 1 when the device list has changed.
		 * If the device list has changed, we want to go through
		 * the selection process again, in case a device that we
		 * were previously displaying has gone away.
		 */
		switch (getdevs(&cur)) {
		case -1:
			errx(1, "%s", devstat_errbuf);
			break;
		case 1: {
			int retval;

			num_devices = cur.dinfo->numdevs;
			generation = cur.dinfo->generation;

			retval = selectdevs(&dev_select, &num_selected,
					    &num_selections, &select_generation,
					    generation, cur.dinfo->devices,
					    num_devices, matches, num_matches,
					    specified_devices,
					    num_devices_specified, select_mode,
					    maxshowdevs, 0);
			switch (retval) {
			case -1:
				errx(1, "%s", devstat_errbuf);
				break;
			case 1:
				printhdr();
				break;
			default:
				break;
			}
		}
		default:
			break;
		}

		if (sysctlbyname("vm.vmstats", &vms, &vms_size, NULL, 0)) {
			perror("sysctlbyname: vm.vmstats");
			exit(1);
		}
		if (sysctlbyname("vm.vmmeter", &vmm, &vmm_size, NULL, 0)) {
			perror("sysctlbyname: vm.vmmeter");
			exit(1);
		} 
		if (sysctlbyname("vm.vmtotal", &total, &vmt_size, NULL, 0)) {
			perror("sysctlbyname: vm.vmtotal");
			exit(1);
		} 
		printf("%2ld %1ld %1ld",
		    total.t_rq - 1, total.t_dw + total.t_pw, total.t_sw);

#define vmstat_pgtok(a)	\
	(intmax_t)(((intmax_t)(a) * vms.v_page_size) >> 10)
#define rate(x)		\
	(intmax_t)(initial ? (x) : ((intmax_t)(x) * 1000 + interval / 2) \
				   / interval)

		printf(" %7jd %6jd ",
		       vmstat_pgtok(total.t_avm),
		       vmstat_pgtok(total.t_free));
		printf("%4ju ",
		       rate(vmm.v_vm_faults - ovmm.v_vm_faults));
		printf("%3ju ",
		       rate(vmm.v_reactivated - ovmm.v_reactivated));
		printf("%3ju ",
		       rate(vmm.v_swapin + vmm.v_vnodein -
			    (ovmm.v_swapin + ovmm.v_vnodein)));
		printf("%3ju ",
		       rate(vmm.v_swapout + vmm.v_vnodeout -
			    (ovmm.v_swapout + ovmm.v_vnodeout)));
		printf("%3ju ",
		       rate(vmm.v_tfree - ovmm.v_tfree));
		printf("%3ju ",
		       rate(vmm.v_pdpages - ovmm.v_pdpages));
		devstats();
		printf("%4ju %4ju %3ju ",
		       rate(vmm.v_intr - ovmm.v_intr),
		       rate(vmm.v_syscall - ovmm.v_syscall),
		       rate(vmm.v_swtch - ovmm.v_swtch));
		cpustats();
		printf("\n");
		fflush(stdout);
		if (reps >= 0 && --reps <= 0)
			break;
		ovmm = vmm;
		usleep(interval * 1000);
		initial = 0;
	}
}
Esempio n. 7
0
int main(int argc, char** argv) {
    bool cpu_stats = false;
    bool mem_stats = false;
    zx_duration_t delay = ZX_SEC(1);
    int num_loops = -1;
    bool timestamp = false;

    int c;
    while ((c = getopt(argc, argv, "cd:n:hmt")) > 0) {
        switch (c) {
            case 'c':
                cpu_stats = true;
                break;
            case 'd':
                delay = ZX_SEC(atoi(optarg));
                if (delay == 0) {
                    fprintf(stderr, "Bad -d value '%s'\n", optarg);
                    print_help(stderr);
                    return 1;
                }
                break;
            case 'n':
                num_loops = atoi(optarg);
                if (num_loops == 0) {
                    fprintf(stderr, "Bad -n value '%s'\n", optarg);
                    print_help(stderr);
                    return 1;
                }
                break;
            case 'h':
                print_help(stdout);
                return 0;
            case 'm':
                mem_stats = true;
                break;
            case 't':
                timestamp = true;
                break;
            default:
                fprintf(stderr, "Unknown option\n");
                print_help(stderr);
                return 1;
        }
    }

    if (!cpu_stats && !mem_stats) {
        fprintf(stderr, "No statistics selected\n");
        print_help(stderr);
        return 1;
    }

    zx_handle_t root_resource;
    zx_status_t ret = get_root_resource(&root_resource);
    if (ret != ZX_OK) {
        return ret;
    }

    // set stdin to non blocking so we can intercept ctrl-c.
    // TODO: remove once ctrl-c works in the shell
    fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);

    for (;;) {
        zx_time_t next_deadline = zx_deadline_after(delay);

        // Print the current UTC time with milliseconds as
        // an ISO 8601 string.
        if (timestamp) {
            struct timespec now;
            timespec_get(&now, TIME_UTC);
            struct tm nowtm;
            gmtime_r(&now.tv_sec, &nowtm);
            char tbuf[40];
            strftime(tbuf, sizeof(tbuf), "%FT%T", &nowtm);
            printf("\n--- %s.%03ldZ ---\n", tbuf, now.tv_nsec / (1000 * 1000));
        }

        if (cpu_stats) {
            ret |= cpustats(root_resource, delay);
        }
        if (mem_stats) {
            ret |= memstats(root_resource);
        }

        if (ret != ZX_OK)
            break;

        if (num_loops > 0) {
            if (--num_loops == 0) {
                break;
            }
        } else {
            // TODO: replace once ctrl-c works in the shell
            char c;
            int err;
            while ((err = read(STDIN_FILENO, &c, 1)) > 0) {
                if (c == 0x3)
                    return 0;
            }
        }

        zx_nanosleep(next_deadline);
    }

    zx_handle_close(root_resource);

    return ret;
}
Esempio n. 8
0
void
dovmstat(u_int interval, int reps)
{
	time_t uptime, halfuptime;
	struct clockinfo clkinfo;
	struct vmtotal total;
	size_t size;
	int mib[2];

	uptime = getuptime();
	halfuptime = uptime / 2;
	(void)signal(SIGCONT, needhdr);

	mib[0] = CTL_KERN;
	mib[1] = KERN_CLOCKRATE;
	size = sizeof(clkinfo);
	if (sysctl(mib, 2, &clkinfo, &size, NULL, 0) < 0) {
		warn("could not read kern.clockrate");
		return;
	}
	hz = clkinfo.stathz;

	for (hdrcnt = 1;;) {
		/* Read new disk statistics */
		dkreadstats();
		if (!--hdrcnt || last.dk_ndrive != cur.dk_ndrive)
			printhdr();
		if (nlistf == NULL && memf == NULL) {
			size = sizeof(struct uvmexp);
			mib[0] = CTL_VM;
			mib[1] = VM_UVMEXP;
			if (sysctl(mib, 2, &uvmexp, &size, NULL, 0) < 0) {
				warn("could not get vm.uvmexp");
				bzero(&uvmexp, sizeof(struct uvmexp));
			}
		} else {
			kread(X_UVMEXP, &uvmexp, sizeof(struct uvmexp));
		}
		size = sizeof(total);
		mib[0] = CTL_VM;
		mib[1] = VM_METER;
		if (sysctl(mib, 2, &total, &size, NULL, 0) < 0) {
			warn("could not read vm.vmmeter");
			bzero(&total, sizeof(total));
		}
		(void)printf(" %u %u %u ",
		    total.t_rq - 1, total.t_dw + total.t_pw, total.t_sw);
#define	rate(x)	((unsigned)((((unsigned)x) + halfuptime) / uptime)) /* round */
#define pgtok(a) ((a) * ((unsigned int)uvmexp.pagesize >> 10))
		(void)printf("%6u %7u ",
		    pgtok(uvmexp.active + uvmexp.swpginuse),
		    pgtok(uvmexp.free));
		(void)printf("%4u ", rate(uvmexp.faults - ouvmexp.faults));
		(void)printf("%3u ", rate(uvmexp.pdreact - ouvmexp.pdreact));
		(void)printf("%3u ", rate(uvmexp.pageins - ouvmexp.pageins));
		(void)printf("%3u %3u ",
		    rate(uvmexp.pdpageouts - ouvmexp.pdpageouts), 0);
		(void)printf("%3u ", rate(uvmexp.pdscans - ouvmexp.pdscans));
		dkstats();
		(void)printf("%4u %5u %4u ",
		    rate(uvmexp.intrs - ouvmexp.intrs),
		    rate(uvmexp.syscalls - ouvmexp.syscalls),
		    rate(uvmexp.swtch - ouvmexp.swtch));
		cpustats();
		(void)printf("\n");
		(void)fflush(stdout);
		if (reps >= 0 && --reps <= 0)
			break;
		ouvmexp = uvmexp;
		uptime = interval;
		/*
		 * We round upward to avoid losing low-frequency events
		 * (i.e., >= 1 per interval but < 1 per second).
		 */
		halfuptime = uptime == 1 ? 0 : (uptime + 1) / 2;
		(void)sleep(interval);
	}
}