Пример #1
0
void
showiostat(void)
{
	int i, row, _col;
	struct kinfo_cputime diff_cp_time;
	uint64_t cp_total;

	diff_cp_time.cp_user = cp_time.cp_user - old_cp_time.cp_user;
	diff_cp_time.cp_nice = cp_time.cp_nice - old_cp_time.cp_nice;
	diff_cp_time.cp_sys = cp_time.cp_sys - old_cp_time.cp_sys;
	diff_cp_time.cp_intr = cp_time.cp_intr - old_cp_time.cp_intr;
	diff_cp_time.cp_idle = cp_time.cp_idle - old_cp_time.cp_idle;
	old_cp_time = cp_time;

	row = 1;
	cp_total = diff_cp_time.cp_user + diff_cp_time.cp_nice +
	    diff_cp_time.cp_sys + diff_cp_time.cp_intr + diff_cp_time.cp_idle;
	stat1(row++, diff_cp_time.cp_user, cp_total);
	stat1(row++, diff_cp_time.cp_nice, cp_total);
	stat1(row++, diff_cp_time.cp_sys,  cp_total);
	stat1(row++, diff_cp_time.cp_intr, cp_total);
	stat1(row++, diff_cp_time.cp_idle, cp_total);
	if (!numbers) {
		row += 2;
		for (i = 0; i < num_devices; i++)
			if (dev_select[i].selected) {
				if (row > wnd->_maxy - linesperregion)
					break;
				row = devstats(row, INSET, i);
			}
		return;
	}
	_col = INSET;
	wmove(wnd, row + linesperregion, 0);
	wdeleteln(wnd);
	wmove(wnd, row + 3, 0);
	winsertln(wnd);
	for (i = 0; i < num_devices; i++)
		if (dev_select[i].selected) {
			if (_col + COLWIDTH >= wnd->_maxx - INSET) {
				_col = INSET, row += linesperregion + 1;
				if (row > wnd->_maxy - (linesperregion + 1))
					break;
				wmove(wnd, row + linesperregion, 0);
				wdeleteln(wnd);
				wmove(wnd, row + 3, 0);
				winsertln(wnd);
			}
			(void) devstats(row + 3, _col, i);
			_col += COLWIDTH;
		}
}
Пример #2
0
void
showiostat(void)
{
	long t;
	int i, row, _col;

#define X(fld)	t = cur.fld[i]; cur.fld[i] -= last.fld[i]; last.fld[i] = t
	etime = 0;
	for(i = 0; i < CPUSTATES; i++) {
		X(cp_time);
		etime += cur.cp_time[i];
	}
	if (etime == 0.0)
		etime = 1.0;
	etime /= hertz;
	row = 1;
	for (i = 0; i < CPUSTATES; i++)
		stat1(row++, i);
	if (!numbers) {
		row += 2;
		for (i = 0; i < num_devices; i++)
			if (dev_select[i].selected) {
				if (row > wnd->_maxy - linesperregion)
					break;
				row = devstats(row, INSET, i);
			}
		return;
	}
	_col = INSET;
	wmove(wnd, row + linesperregion, 0);
	wdeleteln(wnd);
	wmove(wnd, row + 3, 0);
	winsertln(wnd);
	for (i = 0; i < num_devices; i++)
		if (dev_select[i].selected) {
			if (_col + COLWIDTH >= wnd->_maxx - INSET) {
				_col = INSET, row += linesperregion + 1;
				if (row > wnd->_maxy - (linesperregion + 1))
					break;
				wmove(wnd, row + linesperregion, 0);
				wdeleteln(wnd);
				wmove(wnd, row + 3, 0);
				winsertln(wnd);
			}
			(void) devstats(row + 3, _col, i);
			_col += COLWIDTH;
		}
}
Пример #3
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);
}
Пример #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);
}
Пример #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);
	}
}
Пример #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;
	}
}