Annotation of src/usr.bin/systat/swap.c, Revision 1.16
1.16 ! deraadt 1: /* $OpenBSD: swap.c,v 1.15 2002/02/16 21:27:54 millert 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.
17: * 3. All advertising materials mentioning features or use of this software
18: * must display the following acknowledgement:
19: * This product includes software developed by the University of
20: * California, Berkeley and its contributors.
21: * 4. Neither the name of the University nor the names of its contributors
22: * may be used to endorse or promote products derived from this software
23: * without specific prior written permission.
24: *
25: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35: * SUCH DAMAGE.
36: */
37:
38: #ifndef lint
39: #if 0
40: static char sccsid[] = "@(#)swap.c 8.3 (Berkeley) 4/29/95";
41: #endif
1.16 ! deraadt 42: static char rcsid[] = "$OpenBSD: swap.c,v 1.15 2002/02/16 21:27:54 millert Exp $";
1.1 deraadt 43: #endif /* not lint */
1.13 pvalchev 44:
1.11 weingart 45: #include <sys/cdefs.h>
1.1 deraadt 46: #include <sys/param.h>
47: #include <sys/buf.h>
48: #include <sys/conf.h>
49: #include <sys/ioctl.h>
50: #include <sys/stat.h>
1.11 weingart 51: #include <sys/swap.h>
1.1 deraadt 52:
53: #include <stdio.h>
54: #include <stdlib.h>
55: #include <string.h>
1.11 weingart 56: #include <errno.h>
1.1 deraadt 57: #include <unistd.h>
58:
59: #include "systat.h"
60: #include "extern.h"
61:
1.15 millert 62: void showspace(char *header, int hlen, long blocksize);
1.1 deraadt 63:
1.11 weingart 64: static long blocksize;
65: static int hlen, nswap, rnswap;
66: static int first = 1;
67: static struct swapent *swap_devices;
1.1 deraadt 68:
69: WINDOW *
1.16 ! deraadt 70: openswap(void)
1.1 deraadt 71: {
72: return (subwin(stdscr, LINES-5-1, 0, 5, 0));
73: }
74:
75: void
1.16 ! deraadt 76: closeswap(WINDOW *w)
1.1 deraadt 77: {
78: if (w == NULL)
79: return;
80: wclear(w);
81: wrefresh(w);
82: delwin(w);
83: }
84:
1.11 weingart 85: /* do nothing */
1.9 millert 86: int
1.16 ! deraadt 87: initswap(void)
1.1 deraadt 88: {
89: return (1);
90: }
91:
92: void
1.16 ! deraadt 93: fetchswap(void)
1.1 deraadt 94: {
1.11 weingart 95: int update_label = 0;
96:
97: first = 0;
98: nswap = swapctl(SWAP_NSWAP, 0, 0);
99: if (nswap < 0)
100: error("error: %s", strerror(errno));
101: if (nswap == 0)
102: return;
103: update_label = (nswap != rnswap);
1.1 deraadt 104:
1.11 weingart 105: if (swap_devices)
106: (void)free(swap_devices);
107: swap_devices = (struct swapent *)malloc(nswap * sizeof(*swap_devices));
108: if (swap_devices == NULL)
109: /* XXX */ ; /* XXX systat doesn't do errors! */
110:
111: rnswap = swapctl(SWAP_STATS, (void *)swap_devices, nswap);
112: if (nswap < 0)
113: /* XXX */ ; /* XXX systat doesn't do errors! */
114: if (nswap != rnswap)
115: /* XXX */ ; /* XXX systat doesn't do errors! */
116: if (update_label)
117: labelswap();
1.1 deraadt 118: }
119:
120: void
1.16 ! deraadt 121: labelswap(void)
1.1 deraadt 122: {
1.11 weingart 123: char *header;
124: int row;
1.1 deraadt 125:
126: row = 0;
1.11 weingart 127: wmove(wnd, row, 0);
128: wclrtobot(wnd);
129: if (first)
130: fetchswap();
131: if (nswap == 0) {
132: mvwprintw(wnd, row++, 0, "No swap");
133: return;
134: }
1.1 deraadt 135: header = getbsize(&hlen, &blocksize);
136: mvwprintw(wnd, row++, 0, "%-5s%*s%9s %55s",
137: "Disk", hlen, header, "Used",
138: "/0% /10% /20% /30% /40% /50% /60% /70% /80% /90% /100%");
139: }
140:
141: void
1.16 ! deraadt 142: showswap(void)
! 143: {
1.11 weingart 144: int col, div, i, j, avail, used, xsize, free;
145: struct swapent *sep;
146: char *p;
1.1 deraadt 147:
148: div = blocksize / 512;
1.11 weingart 149: free = avail = 0;
150: for (sep = swap_devices, i = 0; i < nswap; i++, sep++) {
151: if (sep == NULL)
152: continue;
153:
154: p = strrchr(sep->se_path, '/');
155: p = p ? p+1 : sep->se_path;
156:
157: mvwprintw(wnd, i + 1, 0, "%-5s", p);
158:
1.1 deraadt 159: col = 5;
1.11 weingart 160: mvwprintw(wnd, i + 1, col, "%*d", hlen, sep->se_nblks / div);
161:
1.1 deraadt 162: col += hlen;
1.11 weingart 163: xsize = sep->se_nblks;
164: used = sep->se_inuse;
165: avail += xsize;
166: free += xsize - used;
1.1 deraadt 167: mvwprintw(wnd, i + 1, col, "%9d ", used / div);
168: for (j = (100 * used / xsize + 1) / 2; j > 0; j--)
169: waddch(wnd, 'X');
1.11 weingart 170: wclrtoeol(wnd);
1.1 deraadt 171: }
1.11 weingart 172: /* do total if necessary */
173: if (nswap > 1) {
174: used = avail - free;
1.1 deraadt 175: mvwprintw(wnd, i + 1, 0, "%-5s%*d%9d ",
176: "Total", hlen, avail / div, used / div);
177: for (j = (100 * used / avail + 1) / 2; j > 0; j--)
178: waddch(wnd, 'X');
1.11 weingart 179: wclrtoeol(wnd);
1.1 deraadt 180: }
181: }