[BACK]Return to swap.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / systat

Annotation of src/usr.bin/systat/swap.c, Revision 1.22

1.22    ! canacar     1: /*     $OpenBSD: swap.c,v 1.21 2008/06/12 22:26:01 canacar Exp $       */
1.11      weingart    2: /*     $NetBSD: swap.c,v 1.9 1998/12/26 07:05:08 marc Exp $    */
1.1       deraadt     3:
                      4: /*-
1.11      weingart    5:  * Copyright (c) 1997 Matthew R. Green.  All rights reserved.
1.1       deraadt     6:  * Copyright (c) 1980, 1992, 1993
                      7:  *     The Regents of the University of California.  All rights reserved.
                      8:  *
                      9:  * Redistribution and use in source and binary forms, with or without
                     10:  * modification, are permitted provided that the following conditions
                     11:  * are met:
                     12:  * 1. Redistributions of source code must retain the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer.
                     14:  * 2. Redistributions in binary form must reproduce the above copyright
                     15:  *    notice, this list of conditions and the following disclaimer in the
                     16:  *    documentation and/or other materials provided with the distribution.
1.17      millert    17:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    18:  *    may be used to endorse or promote products derived from this software
                     19:  *    without specific prior written permission.
                     20:  *
                     21:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     22:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     23:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     24:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     25:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     26:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     27:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     28:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     29:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     30:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     31:  * SUCH DAMAGE.
                     32:  */
                     33:
1.11      weingart   34: #include <sys/cdefs.h>
1.1       deraadt    35: #include <sys/param.h>
                     36: #include <sys/buf.h>
                     37: #include <sys/conf.h>
                     38: #include <sys/ioctl.h>
                     39: #include <sys/stat.h>
1.11      weingart   40: #include <sys/swap.h>
1.1       deraadt    41:
                     42: #include <stdio.h>
                     43: #include <stdlib.h>
                     44: #include <string.h>
1.11      weingart   45: #include <errno.h>
1.1       deraadt    46: #include <unistd.h>
                     47:
                     48: #include "systat.h"
1.21      canacar    49:
1.1       deraadt    50:
1.11      weingart   51: static long blocksize;
                     52: static int hlen, nswap, rnswap;
                     53: static struct swapent *swap_devices;
1.1       deraadt    54:
1.21      canacar    55: void print_sw(void);
                     56: int read_sw(void);
                     57: int select_sw(void);
                     58: static void showswap(int i);
                     59: static void showtotal(void);
                     60:
                     61:
                     62: field_def fields_sw[] = {
                     63:        {"DISK", 6, 16, 1, FLD_ALIGN_LEFT, -1, 0, 0, 0},
                     64:        {"BLOCKS", 5, 10, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0},
                     65:        {"USED", 5, 10, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0},
                     66:        {"", 40, 80, 1, FLD_ALIGN_BAR, -1, 0, 0, 100},
                     67: };
                     68:
                     69: #define FIELD_ADDR(x) (&fields_sw[x])
                     70:
                     71: #define FLD_SW_NAME    FIELD_ADDR(0)
                     72: #define FLD_SW_BLOCKS  FIELD_ADDR(1)
                     73: #define FLD_SW_USED    FIELD_ADDR(2)
                     74: #define FLD_SW_BAR     FIELD_ADDR(3)
                     75:
                     76: /* Define views */
                     77: field_def *view_sw_0[] = {
                     78:        FLD_SW_NAME, FLD_SW_BLOCKS, FLD_SW_USED, FLD_SW_BAR, NULL
                     79: };
                     80:
                     81:
                     82: /* Define view managers */
                     83: struct view_manager swap_mgr = {
                     84:        "Swap", select_sw, read_sw, NULL, print_header,
                     85:        print_sw, keyboard_callback, NULL, NULL
                     86: };
                     87:
                     88: field_view views_sw[] = {
                     89:        {view_sw_0, "swap", '6', &swap_mgr},
                     90:        {NULL, NULL, 0, NULL}
                     91: };
1.1       deraadt    92:
                     93:
1.9       millert    94: int
1.21      canacar    95: select_sw(void)
1.1       deraadt    96: {
1.21      canacar    97:        if (swap_devices == NULL || nswap == 0)
                     98:                num_disp = 1;
                     99:        else
                    100:                num_disp = nswap;
                    101:        if (nswap > 1)
                    102:                num_disp++;
                    103:        return (0);
1.1       deraadt   104: }
                    105:
1.21      canacar   106: int
                    107: read_sw(void)
1.1       deraadt   108: {
1.21      canacar   109:        num_disp = 1;
1.11      weingart  110:
                    111:        nswap = swapctl(SWAP_NSWAP, 0, 0);
1.21      canacar   112:
1.11      weingart  113:        if (nswap < 0)
                    114:                error("error: %s", strerror(errno));
                    115:        if (nswap == 0)
1.21      canacar   116:                return 0;
1.1       deraadt   117:
1.11      weingart  118:        if (swap_devices)
                    119:                (void)free(swap_devices);
1.21      canacar   120:
1.20      deraadt   121:        swap_devices = (struct swapent *)calloc(nswap, sizeof(*swap_devices));
1.11      weingart  122:        if (swap_devices == NULL)
1.21      canacar   123:                return 0;
1.11      weingart  124:
                    125:        rnswap = swapctl(SWAP_STATS, (void *)swap_devices, nswap);
1.21      canacar   126:        if (rnswap < 0 || nswap != rnswap)
                    127:                return 0;
                    128:
                    129:        num_disp = nswap;
                    130:        if (nswap > 1)
                    131:                num_disp++;
                    132:
                    133:        return 0;
1.1       deraadt   134: }
                    135:
1.21      canacar   136:
1.1       deraadt   137: void
1.21      canacar   138: print_sw(void)
1.1       deraadt   139: {
1.21      canacar   140:        int n, count = 0;
1.1       deraadt   141:
1.21      canacar   142:        if (swap_devices == NULL || nswap == 0) {
                    143:                print_fld_str(FLD_SW_BAR, "No swap devices");
1.11      weingart  144:                return;
                    145:        }
1.21      canacar   146:
                    147:
                    148:        for (n = dispstart; n < num_disp; n++) {
                    149:                if (n >= nswap)
                    150:                        showtotal();
                    151:                else
                    152:                        showswap(n);
                    153:                count++;
                    154:                if (maxprint > 0 && count >= maxprint)
                    155:                        break;
                    156:        }
                    157:
                    158: }
                    159:
                    160: int
                    161: initswap(void)
                    162: {
                    163:        field_view *v;
                    164:
                    165:        char *bs = getbsize(&hlen, &blocksize);
                    166:
                    167:        FLD_SW_BLOCKS->title = strdup(bs);
                    168:
                    169:        for (v = views_sw; v->name != NULL; v++)
                    170:                add_view(v);
                    171:
                    172:        return(1);
1.1       deraadt   173: }
                    174:
1.21      canacar   175:
                    176: static void
                    177: showswap(int i)
1.16      deraadt   178: {
1.22    ! canacar   179:        int d, used, xsize;
1.11      weingart  180:        struct  swapent *sep;
                    181:        char    *p;
1.1       deraadt   182:
1.22    ! canacar   183:        d = blocksize / 512;
1.11      weingart  184:
1.21      canacar   185:        sep = &swap_devices[i];
                    186:
                    187:        p = strrchr(sep->se_path, '/');
                    188:        p = p ? p+1 : sep->se_path;
                    189:
                    190:        print_fld_str(FLD_SW_NAME, p);
                    191:
                    192:        xsize = sep->se_nblks;
                    193:        used = sep->se_inuse;
                    194:
1.22    ! canacar   195:        print_fld_uint(FLD_SW_BLOCKS, xsize / d);
        !           196:        print_fld_uint(FLD_SW_USED, used / d);
1.21      canacar   197:        print_fld_bar(FLD_SW_BAR, 100 * used / xsize);
                    198:
                    199:        end_line();
                    200: }
1.11      weingart  201:
1.21      canacar   202: static void
                    203: showtotal(void)
                    204: {
                    205:        struct  swapent *sep;
1.22    ! canacar   206:        int     d, i, avail, used, xsize, mfree;
1.11      weingart  207:
1.22    ! canacar   208:        d = blocksize / 512;
        !           209:        mfree = avail = 0;
1.11      weingart  210:
1.21      canacar   211:        for (sep = swap_devices, i = 0; i < nswap; i++, sep++) {
1.11      weingart  212:                xsize = sep->se_nblks;
                    213:                used = sep->se_inuse;
                    214:                avail += xsize;
1.22    ! canacar   215:                mfree += xsize - used;
1.1       deraadt   216:        }
1.22    ! canacar   217:        used = avail - mfree;
1.21      canacar   218:
                    219:        print_fld_str(FLD_SW_NAME, "Total");
1.22    ! canacar   220:        print_fld_uint(FLD_SW_BLOCKS, avail / d);
        !           221:        print_fld_uint(FLD_SW_USED, used / d);
1.21      canacar   222:        print_fld_bar(FLD_SW_BAR, 100 * used / avail);
                    223:
                    224:        end_line();
1.1       deraadt   225: }