diff --git a/usr.bin/systat/devs.c b/usr.bin/systat/devs.c index 62c511d62ae6..b293796c2c77 100644 --- a/usr.bin/systat/devs.c +++ b/usr.bin/systat/devs.c @@ -1,333 +1,443 @@ /*- * SPDX-License-Identifier: BSD-3-Clause * * Copyright (c) 1998 Kenneth D. Merry. + * 2015 Yoshihiro Ota * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ /*- * Copyright (c) 1980, 1992, 1993 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #ifdef lint static const char sccsid[] = "@(#)disks.c 8.1 (Berkeley) 6/6/93"; #endif #include #include #include #include -#include #include #include #include #include "systat.h" #include "extern.h" #include "devs.h" typedef enum { DS_MATCHTYPE_NONE, DS_MATCHTYPE_SPEC, DS_MATCHTYPE_PATTERN } last_match_type; +struct statinfo cur_dev, last_dev, run_dev; + last_match_type last_type; struct device_selection *dev_select; long generation; int num_devices, num_selected; int num_selections; long select_generation; struct devstat_match *matches = NULL; int num_matches = 0; char **specified_devices; int num_devices_specified = 0; static int dsmatchselect(const char *args, devstat_select_mode select_mode, int maxshowdevs, struct statinfo *s1); static int dsselect(const char *args, devstat_select_mode select_mode, int maxshowdevs, struct statinfo *s1); int -dsinit(int maxshowdevs, struct statinfo *s1, struct statinfo *s2 __unused, - struct statinfo *s3 __unused) +dsinit(int maxshowdevs) { - /* * 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(NULL) < 0) errx(1, "%s", devstat_errbuf); + if( cur_dev.dinfo ) // init was alreay ran + return(1); + + if ((num_devices = devstat_getnumdevs(NULL)) < 0) { + warnx("%s", devstat_errbuf); + return(0); + } + + cur_dev.dinfo = calloc(1, sizeof(struct devinfo)); + last_dev.dinfo = calloc(1, sizeof(struct devinfo)); + run_dev.dinfo = calloc(1, sizeof(struct devinfo)); + generation = 0; num_devices = 0; num_selected = 0; num_selections = 0; select_generation = 0; last_type = DS_MATCHTYPE_NONE; - if (devstat_getdevs(NULL, s1) == -1) + if (devstat_getdevs(NULL, &cur_dev) == -1) errx(1, "%s", devstat_errbuf); - num_devices = s1->dinfo->numdevs; - generation = s1->dinfo->generation; + num_devices = cur_dev.dinfo->numdevs; + generation = cur_dev.dinfo->generation; dev_select = NULL; /* * 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, s1->dinfo->devices, num_devices, + &select_generation, generation, cur_dev.dinfo->devices, num_devices, NULL, 0, NULL, 0, DS_SELECT_ADD, maxshowdevs, 0) == -1) errx(1, "%d %s", __LINE__, devstat_errbuf); return(1); } + +void +dsgetinfo(struct statinfo* dev) +{ + switch (devstat_getdevs(NULL, dev)) { + case -1: + errx(1, "%s", devstat_errbuf); + break; + case 1: + num_devices = dev->dinfo->numdevs; + generation = dev->dinfo->generation; + cmdkre("refresh", NULL); + break; + default: + break; + } +} + int dscmd(const char *cmd, const char *args, int maxshowdevs, struct statinfo *s1) { int retval; if (prefix(cmd, "display") || prefix(cmd, "add")) return(dsselect(args, DS_SELECT_ADDONLY, maxshowdevs, s1)); if (prefix(cmd, "ignore") || prefix(cmd, "delete")) return(dsselect(args, DS_SELECT_REMOVE, maxshowdevs, s1)); if (prefix(cmd, "show") || prefix(cmd, "only")) return(dsselect(args, DS_SELECT_ONLY, maxshowdevs, s1)); if (prefix(cmd, "type") || prefix(cmd, "match")) return(dsmatchselect(args, DS_SELECT_ONLY, maxshowdevs, s1)); if (prefix(cmd, "refresh")) { retval = devstat_selectdevs(&dev_select, &num_selected, &num_selections, &select_generation, generation, s1->dinfo->devices, num_devices, (last_type ==DS_MATCHTYPE_PATTERN) ? matches : NULL, (last_type ==DS_MATCHTYPE_PATTERN) ? num_matches : 0, (last_type == DS_MATCHTYPE_SPEC) ?specified_devices : NULL, (last_type == DS_MATCHTYPE_SPEC) ?num_devices_specified : 0, (last_type == DS_MATCHTYPE_NONE) ? DS_SELECT_ADD : DS_SELECT_ADDONLY, maxshowdevs, 0); if (retval == -1) { warnx("%s", devstat_errbuf); return(0); } else if (retval == 1) return(2); } if (prefix(cmd, "drives")) { int i; move(CMDLINE, 0); clrtoeol(); for (i = 0; i < num_devices; i++) { printw("%s%d ", s1->dinfo->devices[i].device_name, s1->dinfo->devices[i].unit_number); } return(1); } return(0); } static int dsmatchselect(const char *args, devstat_select_mode select_mode, int maxshowdevs, struct statinfo *s1) { char **tempstr, *tmpstr, *tmpstr1; char *tstr[100]; int num_args = 0; int i; int retval = 0; if (!args) { warnx("dsmatchselect: no arguments"); return(1); } /* * Break the (pipe delimited) input string out into separate * strings. */ tmpstr = tmpstr1 = strdup(args); for (tempstr = tstr, num_args = 0; (*tempstr = strsep(&tmpstr1, "|")) != NULL && (num_args < 100); num_args++) if (**tempstr != '\0') if (++tempstr >= &tstr[100]) break; free(tmpstr); if (num_args > 99) { warnx("dsmatchselect: too many match arguments"); return(0); } /* * If we've gone through the matching code before, clean out * previously used memory. */ if (num_matches > 0) { free(matches); matches = NULL; num_matches = 0; } for (i = 0; i < num_args; i++) { if (devstat_buildmatch(tstr[i], &matches, &num_matches) != 0) { warnx("%s", devstat_errbuf); return(0); } } if (num_args > 0) { last_type = DS_MATCHTYPE_PATTERN; retval = devstat_selectdevs(&dev_select, &num_selected, &num_selections, &select_generation, generation, s1->dinfo->devices, num_devices, matches, num_matches, NULL, 0, select_mode, maxshowdevs, 0); if (retval == -1) err(1, "device selection error"); else if (retval == 1) return(2); } return(1); } static int dsselect(const char *args, devstat_select_mode select_mode, int maxshowdevs, struct statinfo *s1) { char *cp, *tmpstr, *tmpstr1, *buffer; int i; int retval = 0; if (!args) { warnx("dsselect: no argument"); return(1); } /* * If we've gone through this code before, free previously * allocated resources. */ if (num_devices_specified > 0) { for (i = 0; i < num_devices_specified; i++) free(specified_devices[i]); free(specified_devices); specified_devices = NULL; num_devices_specified = 0; } /* do an initial malloc */ specified_devices = (char **)malloc(sizeof(char *)); tmpstr = tmpstr1 = strdup(args); cp = strchr(tmpstr1, '\n'); if (cp) *cp = '\0'; for (;;) { for (cp = tmpstr1; *cp && isspace(*cp); cp++) ; tmpstr1 = cp; for (; *cp && !isspace(*cp); cp++) ; if (*cp) *cp++ = '\0'; if (cp - tmpstr1 == 0) break; for (i = 0; i < num_devices; i++) { asprintf(&buffer, "%s%d", dev_select[i].device_name, dev_select[i].unit_number); if (strcmp(buffer, tmpstr1) == 0) { num_devices_specified++; specified_devices =(char **)realloc( specified_devices, sizeof(char *) * num_devices_specified); specified_devices[num_devices_specified -1]= strdup(tmpstr1); free(buffer); break; } else free(buffer); } if (i >= num_devices) error("%s: unknown drive", args); tmpstr1 = cp; } free(tmpstr); if (num_devices_specified > 0) { last_type = DS_MATCHTYPE_SPEC; retval = devstat_selectdevs(&dev_select, &num_selected, &num_selections, &select_generation, generation, s1->dinfo->devices, num_devices, NULL, 0, specified_devices, num_devices_specified, select_mode, maxshowdevs, 0); if (retval == -1) err(1, "%s", devstat_errbuf); else if (retval == 1) return(2); } return(1); } + + +void +dslabel(int maxdrives, int diskcol, int diskrow) +{ + int i, j; + + mvprintw(diskrow, diskcol, "Disks"); + mvprintw(diskrow + 1, diskcol, "KB/t"); + mvprintw(diskrow + 2, diskcol, "tps"); + mvprintw(diskrow + 3, diskcol, "MB/s"); + mvprintw(diskrow + 4, diskcol, "%%busy"); + /* + * For now, we don't support a fourth disk statistic. So there's + * no point in providing a label for it. If someone can think of a + * fourth useful disk statistic, there is room to add it. + */ + /* mvprintw(diskrow + 4, diskcol, " msps"); */ + j = 0; + for (i = 0; i < num_devices && j < maxdrives; i++) + if (dev_select[i].selected) { + char tmpstr[80]; + sprintf(tmpstr, "%s%d", dev_select[i].device_name, + dev_select[i].unit_number); + mvprintw(diskrow, diskcol + 5 + 6 * j, + " %5.5s", tmpstr); + j++; + } +} + +static void +dsshow2(int diskcol, int diskrow, int dn, int lc, struct statinfo *now, struct statinfo *then) +{ + long double transfers_per_second; + long double kb_per_transfer, mb_per_second; + long double elapsed_time, device_busy; + int di; + + di = dev_select[dn].position; + + if (then != NULL) { + /* Calculate relative to previous sample */ + elapsed_time = now->snap_time - then->snap_time; + } else { + /* Calculate relative to device creation */ + elapsed_time = now->snap_time - devstat_compute_etime( + &now->dinfo->devices[di].creation_time, NULL); + } + + if (devstat_compute_statistics(&now->dinfo->devices[di], then ? + &then->dinfo->devices[di] : NULL, elapsed_time, + DSM_KB_PER_TRANSFER, &kb_per_transfer, + DSM_TRANSFERS_PER_SECOND, &transfers_per_second, + DSM_MB_PER_SECOND, &mb_per_second, + DSM_BUSY_PCT, &device_busy, + DSM_NONE) != 0) + errx(1, "%s", devstat_errbuf); + + lc = diskcol + lc * 6; + putlongdouble(kb_per_transfer, diskrow + 1, lc, 5, 2, 0); + putlongdouble(transfers_per_second, diskrow + 2, lc, 5, 0, 0); + putlongdouble(mb_per_second, diskrow + 3, lc, 5, 2, 0); + putlongdouble(device_busy, diskrow + 4, lc, 5, 0, 0); +} + +static void +dsshow3(int diskcol, int diskrow, int dn, int lc, struct statinfo *now, struct statinfo *then) +{ + dsshow2(diskcol, diskrow, dn, lc, now, then); +} + +void +dsshow(int maxdrives, int diskcol, int diskrow, struct statinfo *now, struct statinfo *then) +{ + int i, lc; + + for (i = 0, lc = 0; i < num_devices && lc < maxdrives; i++) + if (dev_select[i].selected) + dsshow3(diskcol, diskrow, i, ++lc, now, then); +} diff --git a/usr.bin/systat/devs.h b/usr.bin/systat/devs.h index b25cc962bb21..cbedd844290e 100644 --- a/usr.bin/systat/devs.h +++ b/usr.bin/systat/devs.h @@ -1,32 +1,46 @@ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 1998 David E. O'Brien + * 2015 Yoshihiro Ota * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ -int dsinit(int, struct statinfo *, struct statinfo *, struct statinfo *); +#ifndef DEVS_H +#define DEVS_H + +#include + +int dsinit(int); +void dsgetinfo(struct statinfo *); int dscmd(const char *, const char *, int, struct statinfo *); + +void dslabel(int, int, int); +void dsshow(int, int, int, struct statinfo *, struct statinfo *); + +extern struct statinfo cur_dev, last_dev, run_dev; + +#endif diff --git a/usr.bin/systat/iostat.c b/usr.bin/systat/iostat.c index 2b52b26c8376..8767a046d81a 100644 --- a/usr.bin/systat/iostat.c +++ b/usr.bin/systat/iostat.c @@ -1,397 +1,389 @@ /* * SPDX-License-Identifier: BSD-3-Clause * * Copyright (c) 1998 Kenneth D. Merry. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ /* * Copyright (c) 1980, 1992, 1993 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #ifdef lint static const char sccsid[] = "@(#)iostat.c 8.1 (Berkeley) 6/6/93"; #endif #include #include #include #include #include #include #include #include #include #include "systat.h" #include "extern.h" #include "devs.h" -struct statinfo cur, last; - static int linesperregion; static double etime; static int numbers = 0; /* default display bar graphs */ static int kbpt = 0; /* default ms/seek shown */ static int barlabels(int); static void histogram(long double, int, double); static int numlabels(int); static int devstats(int, int, int); static void stat1(int, int); WINDOW * openiostat(void) { return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0)); } void closeiostat(WINDOW *w) { if (w == NULL) return; wclear(w); wrefresh(w); delwin(w); } int initiostat(void) { - if ((num_devices = devstat_getnumdevs(NULL)) < 0) - return(0); - - cur.dinfo = calloc(1, sizeof(struct devinfo)); - last.dinfo = calloc(1, sizeof(struct devinfo)); - /* * This value for maxshowdevs (100) is bogus. I'm not sure exactly * how to calculate it, though. */ - if (dsinit(100, &cur, &last, NULL) != 1) + if (dsinit(7) != 1) return(0); return(1); } void fetchiostat(void) { struct devinfo *tmp_dinfo; size_t len; - len = sizeof(cur.cp_time); - if (sysctlbyname("kern.cp_time", &cur.cp_time, &len, NULL, 0) - || len != sizeof(cur.cp_time)) { + len = sizeof(cur_dev.cp_time); + if (sysctlbyname("kern.cp_time", &cur_dev.cp_time, &len, NULL, 0) + || len != sizeof(cur_dev.cp_time)) { perror("kern.cp_time"); exit (1); } - tmp_dinfo = last.dinfo; - last.dinfo = cur.dinfo; - cur.dinfo = tmp_dinfo; + tmp_dinfo = last_dev.dinfo; + last_dev.dinfo = cur_dev.dinfo; + cur_dev.dinfo = tmp_dinfo; - last.snap_time = cur.snap_time; + last_dev.snap_time = cur_dev.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)) { + switch (devstat_getdevs(NULL, &cur_dev)) { case -1: errx(1, "%s", devstat_errbuf); break; case 1: cmdiostat("refresh", NULL); break; default: break; } - num_devices = cur.dinfo->numdevs; - generation = cur.dinfo->generation; + num_devices = cur_dev.dinfo->numdevs; + generation = cur_dev.dinfo->generation; } #define INSET 10 void labeliostat(void) { int row; row = 0; wmove(wnd, row, 0); wclrtobot(wnd); mvwaddstr(wnd, row++, INSET, "/0% /10 /20 /30 /40 /50 /60 /70 /80 /90 /100"); mvwaddstr(wnd, row++, 0, "cpu user|"); mvwaddstr(wnd, row++, 0, " nice|"); mvwaddstr(wnd, row++, 0, " system|"); mvwaddstr(wnd, row++, 0, "interrupt|"); mvwaddstr(wnd, row++, 0, " idle|"); if (numbers) row = numlabels(row + 1); else row = barlabels(row + 1); } static int numlabels(int row) { int i, _col, regions, ndrives; char tmpstr[10]; #define COLWIDTH 17 #define DRIVESPERLINE ((getmaxx(wnd) - 1 - INSET) / COLWIDTH) for (ndrives = 0, i = 0; i < num_devices; i++) if (dev_select[i].selected) ndrives++; regions = howmany(ndrives, DRIVESPERLINE); /* * Deduct -regions for blank line after each scrolling region. */ linesperregion = (getmaxy(wnd) - 1 - row - regions) / regions; /* * Minimum region contains space for two * label lines and one line of statistics. */ if (linesperregion < 3) linesperregion = 3; _col = INSET; for (i = 0; i < num_devices; i++) if (dev_select[i].selected) { if (_col + COLWIDTH >= getmaxx(wnd) - 1 - INSET) { _col = INSET, row += linesperregion + 1; if (row > getmaxy(wnd) - 1 - (linesperregion + 1)) break; } sprintf(tmpstr, "%s%d", dev_select[i].device_name, dev_select[i].unit_number); mvwaddstr(wnd, row, _col + 4, tmpstr); mvwaddstr(wnd, row + 1, _col, " KB/t tps MB/s "); _col += COLWIDTH; } if (_col) row += linesperregion + 1; return (row); } static int barlabels(int row) { int i; char tmpstr[10]; mvwaddstr(wnd, row++, INSET, "/0% /10 /20 /30 /40 /50 /60 /70 /80 /90 /100"); linesperregion = 2 + kbpt; for (i = 0; i < num_devices; i++) if (dev_select[i].selected) { if (row > getmaxy(wnd) - 1 - linesperregion) break; sprintf(tmpstr, "%s%d", dev_select[i].device_name, dev_select[i].unit_number); mvwprintw(wnd, row++, 0, "%-5.5s MB/s|", tmpstr); mvwaddstr(wnd, row++, 0, " tps|"); if (kbpt) mvwaddstr(wnd, row++, 0, " KB/t|"); } return (row); } 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 +#define X(fld) t = cur_dev.fld[i]; cur_dev.fld[i] -= last_dev.fld[i]; last_dev.fld[i] = t etime = 0; for(i = 0; i < CPUSTATES; i++) { X(cp_time); - etime += cur.cp_time[i]; + etime += cur_dev.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 > getmaxy(wnd) - 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 >= getmaxx(wnd) - 1 - INSET) { _col = INSET, row += linesperregion + 1; if (row > getmaxy(wnd) - 1 - (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; } } static int devstats(int row, int _col, int dn) { long double transfers_per_second; long double kb_per_transfer, mb_per_second; long double busy_seconds; int di; di = dev_select[dn].position; - busy_seconds = cur.snap_time - last.snap_time; + busy_seconds = cur_dev.snap_time - last_dev.snap_time; - if (devstat_compute_statistics(&cur.dinfo->devices[di], - &last.dinfo->devices[di], busy_seconds, + if (devstat_compute_statistics(&cur_dev.dinfo->devices[di], + &last_dev.dinfo->devices[di], busy_seconds, DSM_KB_PER_TRANSFER, &kb_per_transfer, DSM_TRANSFERS_PER_SECOND, &transfers_per_second, DSM_MB_PER_SECOND, &mb_per_second, DSM_NONE) != 0) errx(1, "%s", devstat_errbuf); if (numbers) { mvwprintw(wnd, row, _col, " %5.2Lf %3.0Lf %5.2Lf ", kb_per_transfer, transfers_per_second, mb_per_second); return(row); } wmove(wnd, row++, _col); histogram(mb_per_second, 50, .5); wmove(wnd, row++, _col); histogram(transfers_per_second, 50, .5); if (kbpt) { wmove(wnd, row++, _col); histogram(kb_per_transfer, 50, .5); } return(row); } static void stat1(int row, int o) { int i; double dtime; dtime = 0.0; for (i = 0; i < CPUSTATES; i++) - dtime += cur.cp_time[i]; + dtime += cur_dev.cp_time[i]; if (dtime == 0.0) dtime = 1.0; wmove(wnd, row, INSET); #define CPUSCALE 0.5 - histogram(100.0 * cur.cp_time[o] / dtime, 50, CPUSCALE); + histogram(100.0 * cur_dev.cp_time[o] / dtime, 50, CPUSCALE); } static void histogram(long double val, int colwidth, double scale) { char buf[10]; int k; int v = (int)(val * scale) + 0.5; k = MIN(v, colwidth); if (v > colwidth) { snprintf(buf, sizeof(buf), "%5.2Lf", val); k -= strlen(buf); while (k--) waddch(wnd, 'X'); waddstr(wnd, buf); return; } while (k--) waddch(wnd, 'X'); wclrtoeol(wnd); } int cmdiostat(const char *cmd, const char *args) { if (prefix(cmd, "kbpt")) kbpt = !kbpt; else if (prefix(cmd, "numbers")) numbers = 1; else if (prefix(cmd, "bars")) numbers = 0; - else if (!dscmd(cmd, args, 100, &cur)) + else if (!dscmd(cmd, args, 100, &cur_dev)) return (0); wclear(wnd); labeliostat(); refresh(); return (1); } diff --git a/usr.bin/systat/swap.c b/usr.bin/systat/swap.c index d9a58575a846..fc44db7d7d42 100644 --- a/usr.bin/systat/swap.c +++ b/usr.bin/systat/swap.c @@ -1,218 +1,233 @@ /*- * SPDX-License-Identifier: BSD-3-Clause * * Copyright (c) 1980, 1992, 1993 * The Regents of the University of California. All rights reserved. + * Copyright (c) 2017 Yoshihiro Ota * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #ifdef lint static const char sccsid[] = "@(#)swap.c 8.3 (Berkeley) 4/29/95"; #endif /* * swapinfo - based on a program of the same name by Kevin Lahey */ #include #include #include #include #include #include #include #include #include #include #include "systat.h" #include "extern.h" +#include "devs.h" static char *header; static long blocksize; static int dlen, odlen; static int hlen; static int ulen, oulen; static int pagesize; WINDOW * openswap(void) { return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0)); } void closeswap(WINDOW *w) { if (w == NULL) return; wclear(w); wrefresh(w); delwin(w); } /* * The meat of all the swap stuff is stolen from pstat(8)'s * swapmode(), which is based on a program called swapinfo written by * Kevin Lahey . */ #define NSWAP 16 static struct kvm_swap kvmsw[NSWAP]; static int kvnsw, okvnsw; static void calclens(void); #define CONVERT(v) ((int)((int64_t)(v) * pagesize / blocksize)) static void calclens(void) { int i, n; int len; dlen = sizeof("Disk"); for (i = 0; i < kvnsw; ++i) { len = strlen(kvmsw[i].ksw_devname); if (dlen < len) dlen = len; } ulen = sizeof("Used"); for (n = CONVERT(kvmsw[kvnsw].ksw_used), len = 2; n /= 10; ++len); if (ulen < len) ulen = len; } int initswap(void) { static int once = 0; if (once) return (1); header = getbsize(&hlen, &blocksize); pagesize = getpagesize(); if ((kvnsw = kvm_getswapinfo(kd, kvmsw, NSWAP, 0)) < 0) { error("systat: kvm_getswapinfo failed"); return (0); } okvnsw = kvnsw; calclens(); odlen = dlen; oulen = ulen; once = 1; + + dsinit(12); + return (1); } void fetchswap(void) { - okvnsw = kvnsw; if ((kvnsw = kvm_getswapinfo(kd, kvmsw, NSWAP, 0)) < 0) { error("systat: kvm_getswapinfo failed"); return; } odlen = dlen; oulen = ulen; calclens(); + + struct devinfo *tmp_dinfo; + + tmp_dinfo = last_dev.dinfo; + last_dev.dinfo = cur_dev.dinfo; + cur_dev.dinfo = tmp_dinfo; + + last_dev.snap_time = cur_dev.snap_time; + dsgetinfo( &cur_dev ); } void labelswap(void) { const char *name; int i; fetchswap(); werase(wnd); mvwprintw(wnd, 0, 0, "%*s%*s%*s %s", -dlen, "Disk", hlen, header, ulen, "Used", "/0% /10 /20 /30 /40 /50 /60 /70 /80 /90 /100"); for (i = 0; i <= kvnsw; ++i) { if (i == kvnsw) { if (kvnsw == 1) break; name = "Total"; } else name = kvmsw[i].ksw_devname; mvwprintw(wnd, i + 1, 0, "%*s", -dlen, name); } + dslabel(12, 0, 18); } void showswap(void) { int count; int i; if (kvnsw != okvnsw || dlen != odlen || ulen != oulen) labelswap(); for (i = 0; i <= kvnsw; ++i) { if (i == kvnsw) { if (kvnsw == 1) break; } if (kvmsw[i].ksw_total == 0) { mvwprintw( wnd, i + 1, dlen + hlen + ulen + 1, "(swap not configured)" ); continue; } wmove(wnd, i + 1, dlen); wprintw(wnd, "%*d", hlen, CONVERT(kvmsw[i].ksw_total)); wprintw(wnd, "%*d", ulen, CONVERT(kvmsw[i].ksw_used)); count = 50.0 * kvmsw[i].ksw_used / kvmsw[i].ksw_total + 1; waddch(wnd, ' '); while (count--) waddch(wnd, 'X'); wclrtoeol(wnd); } + dsshow(12, 0, 18, &cur_dev, &last_dev); } diff --git a/usr.bin/systat/systat.h b/usr.bin/systat/systat.h index cc4fc5c22680..92233e058172 100644 --- a/usr.bin/systat/systat.h +++ b/usr.bin/systat/systat.h @@ -1,70 +1,74 @@ /*- * SPDX-License-Identifier: BSD-3-Clause * * Copyright (c) 1980, 1989, 1992, 1993 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * From: @(#)systat.h 8.1 (Berkeley) 6/6/93 * $FreeBSD$ */ #include struct cmdtab { const char *c_name; /* command name */ void (*c_refresh)(void); /* display refresh */ void (*c_fetch)(void); /* sets up data structures */ void (*c_label)(void); /* label display */ int (*c_init)(void); /* initialize namelist, etc. */ WINDOW *(*c_open)(void); /* open display */ void (*c_close)(WINDOW *); /* close display */ int (*c_cmd)(const char *, const char *); /* display command interpreter */ void (*c_reset)(void); /* reset ``mode since'' display */ char c_flags; /* see below */ }; /* * If we are started with privileges, use a kmem interface for netstat handling, * otherwise use sysctl. * In case of many open sockets, the sysctl handling might become slow. */ extern int use_kvm; #define CF_INIT 0x1 /* been initialized */ #define CF_LOADAV 0x2 /* display w/ load average */ #define CF_ZFSARC 0x4 /* display w/ ZFS cache usage */ #define TCP 0x1 #define UDP 0x2 #define MAINWIN_ROW 3 /* top row for the main/lower window */ #define GETSYSCTL(name, var) getsysctl(name, &(var), sizeof(var)) #define KREAD(addr, buf, len) kvm_ckread((addr), (buf), (len)) #define NVAL(indx) namelist[(indx)].n_value #define NPTR(indx) (void *)NVAL((indx)) #define NREAD(indx, buf, len) kvm_ckread(NPTR((indx)), (buf), (len)) + +extern void putint(int, int, int, int); +extern void putfloat(double, int, int, int, int, int); +extern void putlongdouble(long double, int, int, int, int, int); diff --git a/usr.bin/systat/vmstat.c b/usr.bin/systat/vmstat.c index 149efb46760c..8a4be61508cb 100644 --- a/usr.bin/systat/vmstat.c +++ b/usr.bin/systat/vmstat.c @@ -1,918 +1,834 @@ /*- * SPDX-License-Identifier: BSD-3-Clause * * Copyright (c) 1983, 1989, 1992, 1993 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #ifdef lint static const char sccsid[] = "@(#)vmstat.c 8.2 (Berkeley) 1/12/94"; #endif /* * Cursed vmstat -- from Robert Elz. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include -#include #include "systat.h" #include "extern.h" #include "devs.h" static struct Info { long time[CPUSTATES]; uint64_t v_swtch; /* context switches */ uint64_t v_trap; /* calls to trap */ uint64_t v_syscall; /* calls to syscall() */ uint64_t v_intr; /* device interrupts */ uint64_t v_soft; /* software interrupts */ /* * Virtual memory activity. */ uint64_t v_vm_faults; /* number of address memory faults */ uint64_t v_io_faults; /* page faults requiring I/O */ uint64_t v_cow_faults; /* number of copy-on-writes */ uint64_t v_zfod; /* pages zero filled on demand */ uint64_t v_ozfod; /* optimized zero fill pages */ uint64_t v_swapin; /* swap pager pageins */ uint64_t v_swapout; /* swap pager pageouts */ uint64_t v_swappgsin; /* swap pager pages paged in */ uint64_t v_swappgsout; /* swap pager pages paged out */ uint64_t v_vnodein; /* vnode pager pageins */ uint64_t v_vnodeout; /* vnode pager pageouts */ uint64_t v_vnodepgsin; /* vnode_pager pages paged in */ uint64_t v_vnodepgsout; /* vnode pager pages paged out */ uint64_t v_intrans; /* intransit blocking page faults */ uint64_t v_reactivated; /* number of pages reactivated by pagedaemon */ uint64_t v_pdwakeups; /* number of times daemon has awaken from sleep */ uint64_t v_pdpages; /* number of pages analyzed by daemon */ uint64_t v_dfree; /* pages freed by daemon */ uint64_t v_pfree; /* pages freed by exiting processes */ uint64_t v_tfree; /* total pages freed */ /* * Distribution of page usages. */ u_int v_page_size; /* page size in bytes */ u_int v_free_count; /* number of pages free */ u_int v_wire_count; /* number of pages wired down */ u_int v_active_count; /* number of pages active */ u_int v_inactive_count; /* number of pages inactive */ u_int v_laundry_count; /* number of pages in laundry queue */ u_long v_kmem_map_size; /* Current kmem allocation size */ struct vmtotal Total; struct nchstats nchstats; long nchcount; long *intrcnt; long bufspace; int desiredvnodes; long numvnodes; long freevnodes; int numdirtybuffers; } s, s1, s2, z; static u_long kmem_size; static u_int v_page_count; -struct statinfo cur, last, run; #define total s.Total #define nchtotal s.nchstats #define oldnchtotal s1.nchstats static enum state { BOOT, TIME, RUN } state = TIME; enum divisor { IEC = 0, SI = HN_DIVISOR_1000 }; static void allocinfo(struct Info *); static void copyinfo(struct Info *, struct Info *); static float cputime(int); -static void dinfo(int, int, struct statinfo *, struct statinfo *); static void do_putuint64(uint64_t, int, int, int, int); static void getinfo(struct Info *); -static void putint(int, int, int, int); static void putuint64(uint64_t, int, int, int); -static void putfloat(double, int, int, int, int, int); -static void putlongdouble(long double, int, int, int, int, int); static int ucount(void); static int ncpu; static char buf[26]; static time_t t; static double etime; static int nintr; static long *intrloc; static char **intrname; static int nextintsrow; WINDOW * openkre(void) { return (stdscr); } void closekre(WINDOW *w) { if (w == NULL) return; wclear(w); wrefresh(w); } /* * These constants define where the major pieces are laid out */ #define STATROW 0 /* uses 1 row and 67 cols */ #define STATCOL 0 #define MEMROW 2 /* uses 4 rows and 45 cols */ #define MEMCOL 0 #define PAGEROW 2 /* uses 4 rows and 30 cols */ #define PAGECOL 47 #define INTSROW 6 /* uses all rows to bottom and 16 cols */ #define INTSCOL 64 #define PROCSROW 6 /* uses 3 rows and 19 cols */ #define PROCSCOL 0 #define GENSTATROW 7 /* uses 2 rows and 29 cols */ #define GENSTATCOL 21 #define VMSTATROW 7 /* uses 17 rows and 12-14 cols */ #define VMSTATCOL 49 /* actually 50-51 for some fields */ #define GRAPHROW 10 /* uses 3 rows and 49-51 cols */ #define GRAPHCOL 0 #define VNSTATROW 13 /* uses 4 rows and 13 columns */ #define VNSTATCOL 35 #define NAMEIROW 14 /* uses 3 rows and 32 cols */ #define NAMEICOL 0 #define DISKROW 18 /* uses 5 rows and 47 cols (for 7 drives) */ #define DISKCOL 0 #define DRIVESPACE 7 /* max # for space */ #define MAXDRIVES DRIVESPACE /* max # to display */ int initkre(void) { char *cp, *cp1, *cp2, *intrnamebuf, *nextcp; int i; size_t sz; - if ((num_devices = devstat_getnumdevs(NULL)) < 0) { - warnx("%s", devstat_errbuf); - return(0); - } - - cur.dinfo = calloc(1, sizeof(struct devinfo)); - last.dinfo = calloc(1, sizeof(struct devinfo)); - run.dinfo = calloc(1, sizeof(struct devinfo)); - - if (dsinit(MAXDRIVES, &cur, &last, &run) != 1) + if (dsinit(MAXDRIVES) != 1) return(0); if (nintr == 0) { if (sysctlbyname("hw.intrcnt", NULL, &sz, NULL, 0) == -1) { error("sysctl(hw.intrcnt...) failed: %s", strerror(errno)); return (0); } nintr = sz / sizeof(u_long); intrloc = calloc(nintr, sizeof (long)); intrname = calloc(nintr, sizeof (char *)); intrnamebuf = sysctl_dynread("hw.intrnames", NULL); if (intrnamebuf == NULL || intrname == NULL || intrloc == NULL) { error("Out of memory"); if (intrnamebuf) free(intrnamebuf); if (intrname) free(intrname); if (intrloc) free(intrloc); nintr = 0; return(0); } for (cp = intrnamebuf, i = 0; i < nintr; i++) { nextcp = cp + strlen(cp) + 1; /* Discard trailing spaces. */ for (cp1 = nextcp - 1; cp1 > cp && *(cp1 - 1) == ' '; ) *--cp1 = '\0'; /* Convert "irqN: name" to "name irqN". */ if (strncmp(cp, "irq", 3) == 0) { cp1 = cp + 3; while (isdigit((u_char)*cp1)) cp1++; if (cp1 != cp && *cp1 == ':' && *(cp1 + 1) == ' ') { sz = strlen(cp); *cp1 = '\0'; cp1 = cp1 + 2; cp2 = strdup(cp); bcopy(cp1, cp, sz - (cp1 - cp) + 1); if (sz <= 10 + 4) { strcat(cp, " "); strcat(cp, cp2 + 3); } free(cp2); } } /* * Convert "name irqN" to "name N" if the former is * longer than the field width. */ if ((cp1 = strstr(cp, "irq")) != NULL && strlen(cp) > 10) bcopy(cp1 + 3, cp1, strlen(cp1 + 3) + 1); intrname[i] = cp; cp = nextcp; } nextintsrow = INTSROW + 2; allocinfo(&s); allocinfo(&s1); allocinfo(&s2); allocinfo(&z); } GETSYSCTL("vm.kmem_size", kmem_size); GETSYSCTL("vm.stats.vm.v_page_count", v_page_count); getinfo(&s2); copyinfo(&s2, &s1); return(1); } void fetchkre(void) { time_t now; struct tm *tp; static int d_first = -1; if (d_first < 0) d_first = (*nl_langinfo(D_MD_ORDER) == 'd'); time(&now); tp = localtime(&now); (void) strftime(buf, sizeof(buf), d_first ? "%e %b %R" : "%b %e %R", tp); getinfo(&s); } void labelkre(void) { int i, j; clear(); mvprintw(STATROW, STATCOL + 6, "users Load"); mvprintw(STATROW + 1, STATCOL + 3, "Mem usage: %%Phy %%Kmem"); mvprintw(MEMROW, MEMCOL, "Mem: KB REAL VIRTUAL"); mvprintw(MEMROW + 1, MEMCOL, " Tot Share Tot Share"); mvprintw(MEMROW + 2, MEMCOL, "Act"); mvprintw(MEMROW + 3, MEMCOL, "All"); mvprintw(MEMROW + 1, MEMCOL + 41, "Free"); mvprintw(PAGEROW, PAGECOL, " VN PAGER SWAP PAGER"); mvprintw(PAGEROW + 1, PAGECOL, " in out in out"); mvprintw(PAGEROW + 2, PAGECOL, "count"); mvprintw(PAGEROW + 3, PAGECOL, "pages"); mvprintw(INTSROW, INTSCOL + 1, "Interrupts"); mvprintw(INTSROW + 1, INTSCOL + 6, "total"); mvprintw(VMSTATROW, VMSTATCOL + 9, "ioflt"); mvprintw(VMSTATROW + 1, VMSTATCOL + 9, "cow"); mvprintw(VMSTATROW + 2, VMSTATCOL + 9, "zfod"); mvprintw(VMSTATROW + 3, VMSTATCOL + 9, "ozfod"); mvprintw(VMSTATROW + 4, VMSTATCOL + 9 - 1, "%%ozfod"); mvprintw(VMSTATROW + 5, VMSTATCOL + 9, "daefr"); mvprintw(VMSTATROW + 6, VMSTATCOL + 9, "prcfr"); mvprintw(VMSTATROW + 7, VMSTATCOL + 9, "totfr"); mvprintw(VMSTATROW + 8, VMSTATCOL + 9, "react"); mvprintw(VMSTATROW + 9, VMSTATCOL + 9, "pdwak"); mvprintw(VMSTATROW + 10, VMSTATCOL + 9, "pdpgs"); mvprintw(VMSTATROW + 11, VMSTATCOL + 9, "intrn"); mvprintw(VMSTATROW + 12, VMSTATCOL + 9, "wire"); mvprintw(VMSTATROW + 13, VMSTATCOL + 9, "act"); mvprintw(VMSTATROW + 14, VMSTATCOL + 9, "inact"); mvprintw(VMSTATROW + 15, VMSTATCOL + 9, "laund"); mvprintw(VMSTATROW + 16, VMSTATCOL + 9, "free"); if (LINES - 1 > VMSTATROW + 17) mvprintw(VMSTATROW + 17, VMSTATCOL + 9, "buf"); mvprintw(GENSTATROW, GENSTATCOL, " Csw Trp Sys Int Sof Flt"); mvprintw(GRAPHROW, GRAPHCOL, " . %%Sys . %%Intr . %%User . %%Nice . %%Idle"); mvprintw(PROCSROW, PROCSCOL, "Proc:"); mvprintw(PROCSROW + 1, PROCSCOL, " r p d s w"); mvprintw(GRAPHROW + 1, GRAPHCOL, "| | | | | | | | | | |"); mvprintw(VNSTATROW, VNSTATCOL + 8, "dtbuf"); mvprintw(VNSTATROW + 1, VNSTATCOL + 8, "desvn"); mvprintw(VNSTATROW + 2, VNSTATCOL + 8, "numvn"); mvprintw(VNSTATROW + 3, VNSTATCOL + 8, "frevn"); mvprintw(NAMEIROW, NAMEICOL, "Namei Name-cache Dir-cache"); mvprintw(NAMEIROW + 1, NAMEICOL, " Calls hits %% hits %%"); - mvprintw(DISKROW, DISKCOL, "Disks"); - mvprintw(DISKROW + 1, DISKCOL, "KB/t"); - mvprintw(DISKROW + 2, DISKCOL, "tps"); - mvprintw(DISKROW + 3, DISKCOL, "MB/s"); - mvprintw(DISKROW + 4, DISKCOL, "%%busy"); - /* - * For now, we don't support a fourth disk statistic. So there's - * no point in providing a label for it. If someone can think of a - * fourth useful disk statistic, there is room to add it. - */ - /* mvprintw(DISKROW + 4, DISKCOL, " msps"); */ - j = 0; - for (i = 0; i < num_devices && j < MAXDRIVES; i++) - if (dev_select[i].selected) { - char tmpstr[80]; - sprintf(tmpstr, "%s%d", dev_select[i].device_name, - dev_select[i].unit_number); - mvprintw(DISKROW, DISKCOL + 5 + 6 * j, - " %5.5s", tmpstr); - j++; - } + dslabel(MAXDRIVES, DISKCOL, DISKROW); for (i = 0; i < nintr; i++) { if (intrloc[i] == 0) continue; mvprintw(intrloc[i], INTSCOL + 6, "%-10.10s", intrname[i]); } } #define X(fld) {t=s.fld[i]; s.fld[i]-=s1.fld[i]; if(state==TIME) s1.fld[i]=t;} -#define Q(fld) {t=cur.fld[i]; cur.fld[i]-=last.fld[i]; if(state==TIME) last.fld[i]=t;} +#define Q(fld) {t=cur_dev.fld[i]; cur_dev.fld[i]-=last_dev.fld[i]; if(state==TIME) last_dev.fld[i]=t;} #define Y(fld) {t = s.fld; s.fld -= s1.fld; if(state == TIME) s1.fld = t;} #define Z(fld) {t = s.nchstats.fld; s.nchstats.fld -= s1.nchstats.fld; \ if(state == TIME) s1.nchstats.fld = t;} #define PUTRATE(fld, l, c, w) \ do { \ Y(fld); \ putint((int)((float)s.fld/etime + 0.5), l, c, w); \ } while (0) #define MAXFAIL 5 static char cpuchar[CPUSTATES] = { '=' , '+', '>', '-', ' ' }; static char cpuorder[CPUSTATES] = { CP_SYS, CP_INTR, CP_USER, CP_NICE, CP_IDLE }; void showkre(void) { float f1, f2; int psiz, inttotal; int i, l, lc; static int failcnt = 0; etime = 0; for(i = 0; i < CPUSTATES; i++) { X(time); Q(cp_time); etime += s.time[i]; } if (etime < 5.0) { /* < 5 ticks - ignore this trash */ if (failcnt++ >= MAXFAIL) { clear(); mvprintw(2, 10, "The alternate system clock has died!"); mvprintw(3, 10, "Reverting to ``pigs'' display."); move(CMDLINE, 0); refresh(); failcnt = 0; sleep(5); command("pigs"); } return; } failcnt = 0; etime /= hertz; etime /= ncpu; inttotal = 0; for (i = 0; i < nintr; i++) { if (s.intrcnt[i] == 0) continue; X(intrcnt); l = (int)((float)s.intrcnt[i]/etime + 0.5); inttotal += l; if (intrloc[i] == 0) { if (nextintsrow == LINES) continue; intrloc[i] = nextintsrow++; mvprintw(intrloc[i], INTSCOL + 6, "%-10.10s", intrname[i]); } putint(l, intrloc[i], INTSCOL, 5); } putint(inttotal, INTSROW + 1, INTSCOL, 5); Z(ncs_goodhits); Z(ncs_badhits); Z(ncs_miss); Z(ncs_long); Z(ncs_pass2); Z(ncs_2passes); Z(ncs_neghits); s.nchcount = nchtotal.ncs_goodhits + nchtotal.ncs_badhits + nchtotal.ncs_miss + nchtotal.ncs_long + nchtotal.ncs_neghits; if (state == TIME) s1.nchcount = s.nchcount; psiz = 0; f2 = 0.0; for (lc = 0; lc < CPUSTATES; lc++) { i = cpuorder[lc]; f1 = cputime(i); f2 += f1; l = (int) ((f2 + 1.0) / 2.0) - psiz; putfloat(f1, GRAPHROW, GRAPHCOL + 10 * lc, 4, 1, 0); move(GRAPHROW + 2, psiz); psiz += l; while (l-- > 0) addch(cpuchar[lc]); } putint(ucount(), STATROW, STATCOL, 5); putfloat(avenrun[0], STATROW, STATCOL + 20, 5, 2, 0); putfloat(avenrun[1], STATROW, STATCOL + 26, 5, 2, 0); putfloat(avenrun[2], STATROW, STATCOL + 32, 5, 2, 0); mvaddstr(STATROW, STATCOL + 55, buf); #define pgtokb(pg) ((pg) * (s.v_page_size / 1024)) putfloat(100.0 * (v_page_count - total.t_free) / v_page_count, STATROW + 1, STATCOL + 15, 2, 0, 1); putfloat(100.0 * s.v_kmem_map_size / kmem_size, STATROW + 1, STATCOL + 22, 2, 0, 1); putuint64(pgtokb(total.t_arm), MEMROW + 2, MEMCOL + 4, 7); putuint64(pgtokb(total.t_armshr), MEMROW + 2, MEMCOL + 12, 7); putuint64(pgtokb(total.t_avm), MEMROW + 2, MEMCOL + 20, 8); putuint64(pgtokb(total.t_avmshr), MEMROW + 2, MEMCOL + 29, 8); putuint64(pgtokb(total.t_rm), MEMROW + 3, MEMCOL + 4, 7); putuint64(pgtokb(total.t_rmshr), MEMROW + 3, MEMCOL + 12, 7); putuint64(pgtokb(total.t_vm), MEMROW + 3, MEMCOL + 20, 8); putuint64(pgtokb(total.t_vmshr), MEMROW + 3, MEMCOL + 29, 8); putuint64(pgtokb(total.t_free), MEMROW + 2, MEMCOL + 38, 7); putint(total.t_rq - 1, PROCSROW + 2, PROCSCOL, 3); putint(total.t_pw, PROCSROW + 2, PROCSCOL + 4, 3); putint(total.t_dw, PROCSROW + 2, PROCSCOL + 8, 3); putint(total.t_sl, PROCSROW + 2, PROCSCOL + 12, 3); putint(total.t_sw, PROCSROW + 2, PROCSCOL + 16, 3); PUTRATE(v_io_faults, VMSTATROW, VMSTATCOL + 2, 8 - 2); PUTRATE(v_cow_faults, VMSTATROW + 1, VMSTATCOL + 2, 8 - 2); PUTRATE(v_zfod, VMSTATROW + 2, VMSTATCOL + 2, 8 - 2); PUTRATE(v_ozfod, VMSTATROW + 3, VMSTATCOL, 8); putint(s.v_zfod != 0 ? (int)(s.v_ozfod * 100.0 / s.v_zfod) : 0, VMSTATROW + 4, VMSTATCOL + 1, 8 - 1); PUTRATE(v_dfree, VMSTATROW + 5, VMSTATCOL + 2, 8 - 2); PUTRATE(v_pfree, VMSTATROW + 6, VMSTATCOL + 2, 8 - 2); PUTRATE(v_tfree, VMSTATROW + 7, VMSTATCOL, 8); PUTRATE(v_reactivated, VMSTATROW + 8, VMSTATCOL, 8); PUTRATE(v_pdwakeups, VMSTATROW + 9, VMSTATCOL, 8); PUTRATE(v_pdpages, VMSTATROW + 10, VMSTATCOL, 8); PUTRATE(v_intrans, VMSTATROW + 11, VMSTATCOL, 8); putuint64(pgtokb(s.v_wire_count), VMSTATROW + 12, VMSTATCOL, 8); putuint64(pgtokb(s.v_active_count), VMSTATROW + 13, VMSTATCOL, 8); putuint64(pgtokb(s.v_inactive_count), VMSTATROW + 14, VMSTATCOL, 8); putuint64(pgtokb(s.v_laundry_count), VMSTATROW + 15, VMSTATCOL, 8); putuint64(pgtokb(s.v_free_count), VMSTATROW + 16, VMSTATCOL, 8); if (LINES - 1 > VMSTATROW + 17) putuint64(s.bufspace / 1024, VMSTATROW + 17, VMSTATCOL, 8); PUTRATE(v_vnodein, PAGEROW + 2, PAGECOL + 6, 5); PUTRATE(v_vnodeout, PAGEROW + 2, PAGECOL + 12, 5); PUTRATE(v_swapin, PAGEROW + 2, PAGECOL + 19, 5); PUTRATE(v_swapout, PAGEROW + 2, PAGECOL + 25, 5); PUTRATE(v_vnodepgsin, PAGEROW + 3, PAGECOL + 6, 5); PUTRATE(v_vnodepgsout, PAGEROW + 3, PAGECOL + 12, 5); PUTRATE(v_swappgsin, PAGEROW + 3, PAGECOL + 19, 5); PUTRATE(v_swappgsout, PAGEROW + 3, PAGECOL + 25, 5); PUTRATE(v_swtch, GENSTATROW + 1, GENSTATCOL, 4); PUTRATE(v_trap, GENSTATROW + 1, GENSTATCOL + 5, 4); PUTRATE(v_syscall, GENSTATROW + 1, GENSTATCOL + 10, 4); PUTRATE(v_intr, GENSTATROW + 1, GENSTATCOL + 15, 4); PUTRATE(v_soft, GENSTATROW + 1, GENSTATCOL + 20, 4); PUTRATE(v_vm_faults, GENSTATROW + 1, GENSTATCOL + 25, 4); - for (i = 0, lc = 0; i < num_devices && lc < MAXDRIVES; i++) - if (dev_select[i].selected) { - switch(state) { - case TIME: - dinfo(i, ++lc, &cur, &last); - break; - case RUN: - dinfo(i, ++lc, &cur, &run); - break; - case BOOT: - dinfo(i, ++lc, &cur, NULL); - break; - } - } + switch(state) { + case TIME: + dsshow(MAXDRIVES, DISKCOL, DISKROW, &cur_dev, &last_dev); + break; + case RUN: + dsshow(MAXDRIVES, DISKCOL, DISKROW, &cur_dev, &run_dev); + break; + case BOOT: + dsshow(MAXDRIVES, DISKCOL, DISKROW, &cur_dev, NULL); + break; + } putint(s.numdirtybuffers, VNSTATROW, VNSTATCOL, 7); putint(s.desiredvnodes, VNSTATROW + 1, VNSTATCOL, 7); putint(s.numvnodes, VNSTATROW + 2, VNSTATCOL, 7); putint(s.freevnodes, VNSTATROW + 3, VNSTATCOL, 7); putint(s.nchcount, NAMEIROW + 2, NAMEICOL, 8); putint((nchtotal.ncs_goodhits + nchtotal.ncs_neghits), NAMEIROW + 2, NAMEICOL + 9, 7); #define nz(x) ((x) ? (x) : 1) putfloat((nchtotal.ncs_goodhits+nchtotal.ncs_neghits) * 100.0 / nz(s.nchcount), NAMEIROW + 2, NAMEICOL + 17, 3, 0, 1); putint(nchtotal.ncs_pass2, NAMEIROW + 2, NAMEICOL + 21, 7); putfloat(nchtotal.ncs_pass2 * 100.0 / nz(s.nchcount), NAMEIROW + 2, NAMEICOL + 29, 3, 0, 1); #undef nz } int cmdkre(const char *cmd, const char *args) { int retval; if (prefix(cmd, "run")) { retval = 1; copyinfo(&s2, &s1); - switch (devstat_getdevs(NULL, &run)) { + switch (devstat_getdevs(NULL, &run_dev)) { case -1: errx(1, "%s", devstat_errbuf); break; case 1: - num_devices = run.dinfo->numdevs; - generation = run.dinfo->generation; - retval = dscmd("refresh", NULL, MAXDRIVES, &cur); + num_devices = run_dev.dinfo->numdevs; + generation = run_dev.dinfo->generation; + retval = dscmd("refresh", NULL, MAXDRIVES, &cur_dev); if (retval == 2) labelkre(); break; default: break; } state = RUN; return (retval); } if (prefix(cmd, "boot")) { state = BOOT; copyinfo(&z, &s1); return (1); } if (prefix(cmd, "time")) { state = TIME; return (1); } if (prefix(cmd, "zero")) { retval = 1; if (state == RUN) { getinfo(&s1); - switch (devstat_getdevs(NULL, &run)) { + switch (devstat_getdevs(NULL, &run_dev)) { case -1: errx(1, "%s", devstat_errbuf); break; case 1: - num_devices = run.dinfo->numdevs; - generation = run.dinfo->generation; - retval = dscmd("refresh",NULL, MAXDRIVES, &cur); + num_devices = run_dev.dinfo->numdevs; + generation = run_dev.dinfo->generation; + retval = dscmd("refresh",NULL, MAXDRIVES, &cur_dev); if (retval == 2) labelkre(); break; default: break; } } return (retval); } - retval = dscmd(cmd, args, MAXDRIVES, &cur); + retval = dscmd(cmd, args, MAXDRIVES, &cur_dev); if (retval == 2) labelkre(); return(retval); } /* calculate number of users on the system */ static int ucount(void) { int nusers = 0; struct utmpx *ut; setutxent(); while ((ut = getutxent()) != NULL) if (ut->ut_type == USER_PROCESS) nusers++; endutxent(); return (nusers); } static float cputime(int indx) { double lt; int i; lt = 0; for (i = 0; i < CPUSTATES; i++) lt += s.time[i]; if (lt == 0.0) lt = 1.0; return (s.time[indx] * 100.0 / lt); } -static void +void putint(int n, int l, int lc, int w) { do_putuint64(n, l, lc, w, SI); } static void putuint64(uint64_t n, int l, int lc, int w) { do_putuint64(n, l, lc, w, IEC); } static void do_putuint64(uint64_t n, int l, int lc, int w, int div) { int snr; char b[128]; char buf[128]; move(l, lc); #ifdef DEBUG while (w-- > 0) addch('*'); return; #endif if (n == 0) { while (w-- > 0) addch(' '); return; } snr = snprintf(b, sizeof(b), "%*ju", w, (uintmax_t)n); if (snr != w) { humanize_number(buf, w, n, "", HN_AUTOSCALE, HN_NOSPACE | HN_DECIMAL | div); snr = snprintf(b, sizeof(b), "%*s", w, buf); } if (snr != w) { while (w-- > 0) addch('*'); return; } addstr(b); } -static void +void putfloat(double f, int l, int lc, int w, int d, int nz) { int snr; char b[128]; move(l, lc); #ifdef DEBUG while (--w >= 0) addch('*'); return; #endif if (nz && f == 0.0) { while (--w >= 0) addch(' '); return; } snr = snprintf(b, sizeof(b), "%*.*f", w, d, f); if (snr != w) snr = snprintf(b, sizeof(b), "%*.0f", w, f); if (snr != w) snr = snprintf(b, sizeof(b), "%*.0fk", w - 1, f / 1000); if (snr != w) snr = snprintf(b, sizeof(b), "%*.0fM", w - 1, f / 1000000); if (snr != w) { while (--w >= 0) addch('*'); return; } addstr(b); } -static void +void putlongdouble(long double f, int l, int lc, int w, int d, int nz) { int snr; char b[128]; move(l, lc); #ifdef DEBUG while (--w >= 0) addch('*'); return; #endif if (nz && f == 0.0) { while (--w >= 0) addch(' '); return; } snr = snprintf(b, sizeof(b), "%*.*Lf", w, d, f); if (snr != w) snr = snprintf(b, sizeof(b), "%*.0Lf", w, f); if (snr != w) snr = snprintf(b, sizeof(b), "%*.0Lfk", w - 1, f / 1000); if (snr != w) snr = snprintf(b, sizeof(b), "%*.0LfM", w - 1, f / 1000000); if (snr != w) { while (--w >= 0) addch('*'); return; } addstr(b); } static void getinfo(struct Info *ls) { struct devinfo *tmp_dinfo; size_t size; int mib[2]; GETSYSCTL("kern.cp_time", ls->time); - GETSYSCTL("kern.cp_time", cur.cp_time); + GETSYSCTL("kern.cp_time", cur_dev.cp_time); GETSYSCTL("vm.stats.sys.v_swtch", ls->v_swtch); GETSYSCTL("vm.stats.sys.v_trap", ls->v_trap); GETSYSCTL("vm.stats.sys.v_syscall", ls->v_syscall); GETSYSCTL("vm.stats.sys.v_intr", ls->v_intr); GETSYSCTL("vm.stats.sys.v_soft", ls->v_soft); GETSYSCTL("vm.stats.vm.v_vm_faults", ls->v_vm_faults); GETSYSCTL("vm.stats.vm.v_io_faults", ls->v_io_faults); GETSYSCTL("vm.stats.vm.v_cow_faults", ls->v_cow_faults); GETSYSCTL("vm.stats.vm.v_zfod", ls->v_zfod); GETSYSCTL("vm.stats.vm.v_ozfod", ls->v_ozfod); GETSYSCTL("vm.stats.vm.v_swapin", ls->v_swapin); GETSYSCTL("vm.stats.vm.v_swapout", ls->v_swapout); GETSYSCTL("vm.stats.vm.v_swappgsin", ls->v_swappgsin); GETSYSCTL("vm.stats.vm.v_swappgsout", ls->v_swappgsout); GETSYSCTL("vm.stats.vm.v_vnodein", ls->v_vnodein); GETSYSCTL("vm.stats.vm.v_vnodeout", ls->v_vnodeout); GETSYSCTL("vm.stats.vm.v_vnodepgsin", ls->v_vnodepgsin); GETSYSCTL("vm.stats.vm.v_vnodepgsout", ls->v_vnodepgsout); GETSYSCTL("vm.stats.vm.v_intrans", ls->v_intrans); GETSYSCTL("vm.stats.vm.v_reactivated", ls->v_reactivated); GETSYSCTL("vm.stats.vm.v_pdwakeups", ls->v_pdwakeups); GETSYSCTL("vm.stats.vm.v_pdpages", ls->v_pdpages); GETSYSCTL("vm.stats.vm.v_dfree", ls->v_dfree); GETSYSCTL("vm.stats.vm.v_pfree", ls->v_pfree); GETSYSCTL("vm.stats.vm.v_tfree", ls->v_tfree); GETSYSCTL("vm.stats.vm.v_page_size", ls->v_page_size); GETSYSCTL("vm.stats.vm.v_free_count", ls->v_free_count); GETSYSCTL("vm.stats.vm.v_wire_count", ls->v_wire_count); GETSYSCTL("vm.stats.vm.v_active_count", ls->v_active_count); GETSYSCTL("vm.stats.vm.v_inactive_count", ls->v_inactive_count); GETSYSCTL("vm.stats.vm.v_laundry_count", ls->v_laundry_count); GETSYSCTL("vfs.bufspace", ls->bufspace); GETSYSCTL("kern.maxvnodes", ls->desiredvnodes); GETSYSCTL("vfs.numvnodes", ls->numvnodes); GETSYSCTL("vfs.freevnodes", ls->freevnodes); GETSYSCTL("vfs.cache.nchstats", ls->nchstats); GETSYSCTL("vfs.numdirtybuffers", ls->numdirtybuffers); GETSYSCTL("vm.kmem_map_size", ls->v_kmem_map_size); getsysctl("hw.intrcnt", ls->intrcnt, nintr * sizeof(u_long)); size = sizeof(ls->Total); mib[0] = CTL_VM; mib[1] = VM_TOTAL; if (sysctl(mib, 2, &ls->Total, &size, NULL, 0) < 0) { error("Can't get kernel info: %s\n", strerror(errno)); bzero(&ls->Total, sizeof(ls->Total)); } size = sizeof(ncpu); if (sysctlbyname("hw.ncpu", &ncpu, &size, NULL, 0) < 0 || size != sizeof(ncpu)) ncpu = 1; - tmp_dinfo = last.dinfo; - last.dinfo = cur.dinfo; - cur.dinfo = tmp_dinfo; + tmp_dinfo = last_dev.dinfo; + last_dev.dinfo = cur_dev.dinfo; + cur_dev.dinfo = tmp_dinfo; - last.snap_time = cur.snap_time; - switch (devstat_getdevs(NULL, &cur)) { - case -1: - errx(1, "%s", devstat_errbuf); - break; - case 1: - num_devices = cur.dinfo->numdevs; - generation = cur.dinfo->generation; - cmdkre("refresh", NULL); - break; - default: - break; - } + last_dev.snap_time = cur_dev.snap_time; + dsgetinfo(&cur_dev); } static void allocinfo(struct Info *ls) { ls->intrcnt = (long *) calloc(nintr, sizeof(long)); if (ls->intrcnt == NULL) errx(2, "out of memory"); } static void copyinfo(struct Info *from, struct Info *to) { long *intrcnt; /* * time, wds, seek, and xfer are malloc'd so we have to * save the pointers before the structure copy and then * copy by hand. */ intrcnt = to->intrcnt; *to = *from; bcopy(from->intrcnt, to->intrcnt = intrcnt, nintr * sizeof (int)); } - -static void -dinfo(int dn, int lc, struct statinfo *now, struct statinfo *then) -{ - long double transfers_per_second; - long double kb_per_transfer, mb_per_second; - long double elapsed_time, device_busy; - int di; - - di = dev_select[dn].position; - - if (then != NULL) { - /* Calculate relative to previous sample */ - elapsed_time = now->snap_time - then->snap_time; - } else { - /* Calculate relative to device creation */ - elapsed_time = now->snap_time - devstat_compute_etime( - &now->dinfo->devices[di].creation_time, NULL); - } - - if (devstat_compute_statistics(&now->dinfo->devices[di], then ? - &then->dinfo->devices[di] : NULL, elapsed_time, - DSM_KB_PER_TRANSFER, &kb_per_transfer, - DSM_TRANSFERS_PER_SECOND, &transfers_per_second, - DSM_MB_PER_SECOND, &mb_per_second, - DSM_BUSY_PCT, &device_busy, - DSM_NONE) != 0) - errx(1, "%s", devstat_errbuf); - - lc = DISKCOL + lc * 6; - putlongdouble(kb_per_transfer, DISKROW + 1, lc, 5, 2, 0); - putlongdouble(transfers_per_second, DISKROW + 2, lc, 5, 0, 0); - putlongdouble(mb_per_second, DISKROW + 3, lc, 5, 2, 0); - putlongdouble(device_busy, DISKROW + 4, lc, 5, 0, 0); -} diff --git a/usr.bin/systat/zarc.c b/usr.bin/systat/zarc.c index eaf85fc221e0..ca4e0fde7724 100644 --- a/usr.bin/systat/zarc.c +++ b/usr.bin/systat/zarc.c @@ -1,222 +1,239 @@ /*- - * Copyright (c) 2014 - * The Regents of the University of California. All rights reserved. + * Copyright (c) 2014 - 2017 Yoshihiro Ota * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include +/* #include */ #include #include +#include #include "systat.h" #include "extern.h" +#include "devs.h" struct zfield{ uint64_t arcstats; uint64_t arcstats_demand_data; uint64_t arcstats_demand_metadata; uint64_t arcstats_prefetch_data; uint64_t arcstats_prefetch_metadata; uint64_t zfetchstats; uint64_t arcstats_l2; uint64_t vdev_cache_stats; }; static struct zarcstats { struct zfield hits; struct zfield misses; } curstat, initstat, oldstat; static void getinfo(struct zarcstats *ls); WINDOW * openzarc(void) { return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0)); } void closezarc(WINDOW *w) { if (w == NULL) return; wclear(w); wrefresh(w); delwin(w); } void labelzarc(void) { + int row = 1; wmove(wnd, 0, 0); wclrtoeol(wnd); mvwprintw(wnd, 0, 31+1, "%4.4s %7.7s %7.7s %12.12s %12.12s", "rate", "hits", "misses", "total hits", "total misses"); -#define L(row, str) mvwprintw(wnd, row, 5, str); \ +#define L(str) mvwprintw(wnd, row, 5, #str); \ mvwprintw(wnd, row, 31, ":"); \ - mvwprintw(wnd, row, 31+4, "%%") - L(1, "arcstats"); - L(2, "arcstats.demand_data"); - L(3, "arcstats.demand_metadata"); - L(4, "arcstats.prefetch_data"); - L(5, "arcstats.prefetch_metadata"); - L(6, "zfetchstats"); - L(7, "arcstats.l2"); - L(8, "vdev_cache_stats"); + mvwprintw(wnd, row, 31+4, "%%"); ++row + L(arcstats); + L(arcstats.demand_data); + L(arcstats.demand_metadata); + L(arcstats.prefetch_data); + L(arcstats.prefetch_metadata); + L(zfetchstats); + L(arcstats.l2); + L(vdev_cache_stats); #undef L + dslabel(12, 0, 18); } static int calc(uint64_t hits, uint64_t misses) { if( hits ) return 100 * hits / ( hits + misses ); else return 0; } static void domode(struct zarcstats *delta, struct zarcstats *rate) { #define DO(stat) \ delta->hits.stat = (curstat.hits.stat - oldstat.hits.stat); \ delta->misses.stat = (curstat.misses.stat - oldstat.misses.stat); \ rate->hits.stat = calc(delta->hits.stat, delta->misses.stat) DO(arcstats); DO(arcstats_demand_data); DO(arcstats_demand_metadata); DO(arcstats_prefetch_data); DO(arcstats_prefetch_metadata); DO(zfetchstats); DO(arcstats_l2); DO(vdev_cache_stats); DO(arcstats); DO(arcstats_demand_data); DO(arcstats_demand_metadata); DO(arcstats_prefetch_data); DO(arcstats_prefetch_metadata); DO(zfetchstats); DO(arcstats_l2); DO(vdev_cache_stats); #undef DO } void showzarc(void) { + int row = 1; struct zarcstats delta, rate; memset(&delta, 0, sizeof delta); memset(&rate, 0, sizeof rate); domode(&delta, &rate); -#define DO(stat, row, col, fmt) \ +#define DO(stat, col, fmt) \ mvwprintw(wnd, row, col, fmt, stat) -#define R(row, stat) DO(rate.hits.stat, row, 31+1, "%3"PRIu64) -#define H(row, stat) DO(delta.hits.stat, row, 31+1+5, "%7"PRIu64); \ - DO(curstat.hits.stat, row, 31+1+5+8+8, "%12"PRIu64) -#define M(row, stat) DO(delta.misses.stat, row, 31+1+5+8, "%7"PRIu64); \ - DO(curstat.misses.stat, row, 31+1+5+8+8+13, "%12"PRIu64) -#define E(row, stat) R(row, stat); H(row, stat); M(row, stat); - E(1, arcstats); - E(2, arcstats_demand_data); - E(3, arcstats_demand_metadata); - E(4, arcstats_prefetch_data); - E(5, arcstats_prefetch_metadata); - E(6, zfetchstats); - E(7, arcstats_l2); - E(8, vdev_cache_stats); +#define R(stat) DO(rate.hits.stat, 31+1, "%3"PRIu64) +#define H(stat) DO(delta.hits.stat, 31+1+5, "%7"PRIu64); \ + DO(curstat.hits.stat, 31+1+5+8+8, "%12"PRIu64) +#define M(stat) DO(delta.misses.stat, 31+1+5+8, "%7"PRIu64); \ + DO(curstat.misses.stat, 31+1+5+8+8+13, "%12"PRIu64) +#define E(stat) R(stat); H(stat); M(stat); ++row + E(arcstats); + E(arcstats_demand_data); + E(arcstats_demand_metadata); + E(arcstats_prefetch_data); + E(arcstats_prefetch_metadata); + E(zfetchstats); + E(arcstats_l2); + E(vdev_cache_stats); #undef DO #undef E #undef M #undef H #undef R + dsshow(12, 0, 18, &cur_dev, &last_dev); } int initzarc(void) { + dsinit(12); getinfo(&initstat); curstat = oldstat = initstat; + return 1; } void resetzarc(void) { initzarc(); } static void getinfo(struct zarcstats *ls) { + struct devinfo *tmp_dinfo; + + tmp_dinfo = last_dev.dinfo; + last_dev.dinfo = cur_dev.dinfo; + cur_dev.dinfo = tmp_dinfo; + + last_dev.snap_time = cur_dev.snap_time; + dsgetinfo( &cur_dev ); + size_t size = sizeof( ls->hits.arcstats ); if ( sysctlbyname("kstat.zfs.misc.arcstats.hits", &ls->hits.arcstats, &size, NULL, 0 ) != 0 ) return; GETSYSCTL("kstat.zfs.misc.arcstats.misses", ls->misses.arcstats); GETSYSCTL("kstat.zfs.misc.arcstats.demand_data_hits", ls->hits.arcstats_demand_data); GETSYSCTL("kstat.zfs.misc.arcstats.demand_data_misses", ls->misses.arcstats_demand_data); GETSYSCTL("kstat.zfs.misc.arcstats.demand_metadata_hits", ls->hits.arcstats_demand_metadata); GETSYSCTL("kstat.zfs.misc.arcstats.demand_metadata_misses", ls->misses.arcstats_demand_metadata); GETSYSCTL("kstat.zfs.misc.arcstats.prefetch_data_hits", ls->hits.arcstats_prefetch_data); GETSYSCTL("kstat.zfs.misc.arcstats.prefetch_data_misses", ls->misses.arcstats_prefetch_data); GETSYSCTL("kstat.zfs.misc.arcstats.prefetch_metadata_hits", ls->hits.arcstats_prefetch_metadata); GETSYSCTL("kstat.zfs.misc.arcstats.prefetch_metadata_misses", ls->misses.arcstats_prefetch_metadata); GETSYSCTL("kstat.zfs.misc.zfetchstats.hits", ls->hits.zfetchstats); GETSYSCTL("kstat.zfs.misc.zfetchstats.misses", ls->misses.zfetchstats); GETSYSCTL("kstat.zfs.misc.arcstats.l2_hits", ls->hits.arcstats_l2); GETSYSCTL("kstat.zfs.misc.arcstats.l2_misses", ls->misses.arcstats_l2); GETSYSCTL("kstat.zfs.misc.vdev_cache_stats.hits", ls->hits.vdev_cache_stats); GETSYSCTL("kstat.zfs.misc.vdev_cache_stats.misses", ls->misses.vdev_cache_stats); } void fetchzarc(void) { oldstat = curstat; getinfo(&curstat); }