[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.23

1.23    ! jasper      1: /*     $OpenBSD: swap.c,v 1.22 2008/12/07 02:56:06 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:
1.23    ! jasper     69: #define FLD_SW_NAME    FIELD_ADDR(fields_sw,0)
        !            70: #define FLD_SW_BLOCKS  FIELD_ADDR(fields_sw,1)
        !            71: #define FLD_SW_USED    FIELD_ADDR(fields_sw,2)
        !            72: #define FLD_SW_BAR     FIELD_ADDR(fields_sw,3)
1.21      canacar    73:
                     74: /* Define views */
                     75: field_def *view_sw_0[] = {
                     76:        FLD_SW_NAME, FLD_SW_BLOCKS, FLD_SW_USED, FLD_SW_BAR, NULL
                     77: };
                     78:
                     79:
                     80: /* Define view managers */
                     81: struct view_manager swap_mgr = {
                     82:        "Swap", select_sw, read_sw, NULL, print_header,
                     83:        print_sw, keyboard_callback, NULL, NULL
                     84: };
                     85:
                     86: field_view views_sw[] = {
                     87:        {view_sw_0, "swap", '6', &swap_mgr},
                     88:        {NULL, NULL, 0, NULL}
                     89: };
1.1       deraadt    90:
                     91:
1.9       millert    92: int
1.21      canacar    93: select_sw(void)
1.1       deraadt    94: {
1.21      canacar    95:        if (swap_devices == NULL || nswap == 0)
                     96:                num_disp = 1;
                     97:        else
                     98:                num_disp = nswap;
                     99:        if (nswap > 1)
                    100:                num_disp++;
                    101:        return (0);
1.1       deraadt   102: }
                    103:
1.21      canacar   104: int
                    105: read_sw(void)
1.1       deraadt   106: {
1.21      canacar   107:        num_disp = 1;
1.11      weingart  108:
                    109:        nswap = swapctl(SWAP_NSWAP, 0, 0);
1.21      canacar   110:
1.11      weingart  111:        if (nswap < 0)
                    112:                error("error: %s", strerror(errno));
                    113:        if (nswap == 0)
1.21      canacar   114:                return 0;
1.1       deraadt   115:
1.11      weingart  116:        if (swap_devices)
                    117:                (void)free(swap_devices);
1.21      canacar   118:
1.20      deraadt   119:        swap_devices = (struct swapent *)calloc(nswap, sizeof(*swap_devices));
1.11      weingart  120:        if (swap_devices == NULL)
1.21      canacar   121:                return 0;
1.11      weingart  122:
                    123:        rnswap = swapctl(SWAP_STATS, (void *)swap_devices, nswap);
1.21      canacar   124:        if (rnswap < 0 || nswap != rnswap)
                    125:                return 0;
                    126:
                    127:        num_disp = nswap;
                    128:        if (nswap > 1)
                    129:                num_disp++;
                    130:
                    131:        return 0;
1.1       deraadt   132: }
                    133:
1.21      canacar   134:
1.1       deraadt   135: void
1.21      canacar   136: print_sw(void)
1.1       deraadt   137: {
1.21      canacar   138:        int n, count = 0;
1.1       deraadt   139:
1.21      canacar   140:        if (swap_devices == NULL || nswap == 0) {
                    141:                print_fld_str(FLD_SW_BAR, "No swap devices");
1.11      weingart  142:                return;
                    143:        }
1.21      canacar   144:
                    145:
                    146:        for (n = dispstart; n < num_disp; n++) {
                    147:                if (n >= nswap)
                    148:                        showtotal();
                    149:                else
                    150:                        showswap(n);
                    151:                count++;
                    152:                if (maxprint > 0 && count >= maxprint)
                    153:                        break;
                    154:        }
                    155:
                    156: }
                    157:
                    158: int
                    159: initswap(void)
                    160: {
                    161:        field_view *v;
                    162:
                    163:        char *bs = getbsize(&hlen, &blocksize);
                    164:
                    165:        FLD_SW_BLOCKS->title = strdup(bs);
                    166:
                    167:        for (v = views_sw; v->name != NULL; v++)
                    168:                add_view(v);
                    169:
                    170:        return(1);
1.1       deraadt   171: }
                    172:
1.21      canacar   173:
                    174: static void
                    175: showswap(int i)
1.16      deraadt   176: {
1.22      canacar   177:        int d, used, xsize;
1.11      weingart  178:        struct  swapent *sep;
                    179:        char    *p;
1.1       deraadt   180:
1.22      canacar   181:        d = blocksize / 512;
1.11      weingart  182:
1.21      canacar   183:        sep = &swap_devices[i];
                    184:
                    185:        p = strrchr(sep->se_path, '/');
                    186:        p = p ? p+1 : sep->se_path;
                    187:
                    188:        print_fld_str(FLD_SW_NAME, p);
                    189:
                    190:        xsize = sep->se_nblks;
                    191:        used = sep->se_inuse;
                    192:
1.22      canacar   193:        print_fld_uint(FLD_SW_BLOCKS, xsize / d);
                    194:        print_fld_uint(FLD_SW_USED, used / d);
1.21      canacar   195:        print_fld_bar(FLD_SW_BAR, 100 * used / xsize);
                    196:
                    197:        end_line();
                    198: }
1.11      weingart  199:
1.21      canacar   200: static void
                    201: showtotal(void)
                    202: {
                    203:        struct  swapent *sep;
1.22      canacar   204:        int     d, i, avail, used, xsize, mfree;
1.11      weingart  205:
1.22      canacar   206:        d = blocksize / 512;
                    207:        mfree = avail = 0;
1.11      weingart  208:
1.21      canacar   209:        for (sep = swap_devices, i = 0; i < nswap; i++, sep++) {
1.11      weingart  210:                xsize = sep->se_nblks;
                    211:                used = sep->se_inuse;
                    212:                avail += xsize;
1.22      canacar   213:                mfree += xsize - used;
1.1       deraadt   214:        }
1.22      canacar   215:        used = avail - mfree;
1.21      canacar   216:
                    217:        print_fld_str(FLD_SW_NAME, "Total");
1.22      canacar   218:        print_fld_uint(FLD_SW_BLOCKS, avail / d);
                    219:        print_fld_uint(FLD_SW_USED, used / d);
1.21      canacar   220:        print_fld_bar(FLD_SW_BAR, 100 * used / avail);
                    221:
                    222:        end_line();
1.1       deraadt   223: }