Annotation of src/usr.bin/systat/swap.c, Revision 1.17
1.17 ! millert 1: /* $OpenBSD: swap.c,v 1.16 2002/06/18 00:46:48 deraadt 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:
34: #ifndef lint
35: #if 0
36: static char sccsid[] = "@(#)swap.c 8.3 (Berkeley) 4/29/95";
37: #endif
1.17 ! millert 38: static char rcsid[] = "$OpenBSD: swap.c,v 1.16 2002/06/18 00:46:48 deraadt Exp $";
1.1 deraadt 39: #endif /* not lint */
1.13 pvalchev 40:
1.11 weingart 41: #include <sys/cdefs.h>
1.1 deraadt 42: #include <sys/param.h>
43: #include <sys/buf.h>
44: #include <sys/conf.h>
45: #include <sys/ioctl.h>
46: #include <sys/stat.h>
1.11 weingart 47: #include <sys/swap.h>
1.1 deraadt 48:
49: #include <stdio.h>
50: #include <stdlib.h>
51: #include <string.h>
1.11 weingart 52: #include <errno.h>
1.1 deraadt 53: #include <unistd.h>
54:
55: #include "systat.h"
56: #include "extern.h"
57:
1.15 millert 58: void showspace(char *header, int hlen, long blocksize);
1.1 deraadt 59:
1.11 weingart 60: static long blocksize;
61: static int hlen, nswap, rnswap;
62: static int first = 1;
63: static struct swapent *swap_devices;
1.1 deraadt 64:
65: WINDOW *
1.16 deraadt 66: openswap(void)
1.1 deraadt 67: {
68: return (subwin(stdscr, LINES-5-1, 0, 5, 0));
69: }
70:
71: void
1.16 deraadt 72: closeswap(WINDOW *w)
1.1 deraadt 73: {
74: if (w == NULL)
75: return;
76: wclear(w);
77: wrefresh(w);
78: delwin(w);
79: }
80:
1.11 weingart 81: /* do nothing */
1.9 millert 82: int
1.16 deraadt 83: initswap(void)
1.1 deraadt 84: {
85: return (1);
86: }
87:
88: void
1.16 deraadt 89: fetchswap(void)
1.1 deraadt 90: {
1.11 weingart 91: int update_label = 0;
92:
93: first = 0;
94: nswap = swapctl(SWAP_NSWAP, 0, 0);
95: if (nswap < 0)
96: error("error: %s", strerror(errno));
97: if (nswap == 0)
98: return;
99: update_label = (nswap != rnswap);
1.1 deraadt 100:
1.11 weingart 101: if (swap_devices)
102: (void)free(swap_devices);
103: swap_devices = (struct swapent *)malloc(nswap * sizeof(*swap_devices));
104: if (swap_devices == NULL)
105: /* XXX */ ; /* XXX systat doesn't do errors! */
106:
107: rnswap = swapctl(SWAP_STATS, (void *)swap_devices, nswap);
108: if (nswap < 0)
109: /* XXX */ ; /* XXX systat doesn't do errors! */
110: if (nswap != rnswap)
111: /* XXX */ ; /* XXX systat doesn't do errors! */
112: if (update_label)
113: labelswap();
1.1 deraadt 114: }
115:
116: void
1.16 deraadt 117: labelswap(void)
1.1 deraadt 118: {
1.11 weingart 119: char *header;
120: int row;
1.1 deraadt 121:
122: row = 0;
1.11 weingart 123: wmove(wnd, row, 0);
124: wclrtobot(wnd);
125: if (first)
126: fetchswap();
127: if (nswap == 0) {
128: mvwprintw(wnd, row++, 0, "No swap");
129: return;
130: }
1.1 deraadt 131: header = getbsize(&hlen, &blocksize);
132: mvwprintw(wnd, row++, 0, "%-5s%*s%9s %55s",
133: "Disk", hlen, header, "Used",
134: "/0% /10% /20% /30% /40% /50% /60% /70% /80% /90% /100%");
135: }
136:
137: void
1.16 deraadt 138: showswap(void)
139: {
1.11 weingart 140: int col, div, i, j, avail, used, xsize, free;
141: struct swapent *sep;
142: char *p;
1.1 deraadt 143:
144: div = blocksize / 512;
1.11 weingart 145: free = avail = 0;
146: for (sep = swap_devices, i = 0; i < nswap; i++, sep++) {
147: if (sep == NULL)
148: continue;
149:
150: p = strrchr(sep->se_path, '/');
151: p = p ? p+1 : sep->se_path;
152:
153: mvwprintw(wnd, i + 1, 0, "%-5s", p);
154:
1.1 deraadt 155: col = 5;
1.11 weingart 156: mvwprintw(wnd, i + 1, col, "%*d", hlen, sep->se_nblks / div);
157:
1.1 deraadt 158: col += hlen;
1.11 weingart 159: xsize = sep->se_nblks;
160: used = sep->se_inuse;
161: avail += xsize;
162: free += xsize - used;
1.1 deraadt 163: mvwprintw(wnd, i + 1, col, "%9d ", used / div);
164: for (j = (100 * used / xsize + 1) / 2; j > 0; j--)
165: waddch(wnd, 'X');
1.11 weingart 166: wclrtoeol(wnd);
1.1 deraadt 167: }
1.11 weingart 168: /* do total if necessary */
169: if (nswap > 1) {
170: used = avail - free;
1.1 deraadt 171: mvwprintw(wnd, i + 1, 0, "%-5s%*d%9d ",
172: "Total", hlen, avail / div, used / div);
173: for (j = (100 * used / avail + 1) / 2; j > 0; j--)
174: waddch(wnd, 'X');
1.11 weingart 175: wclrtoeol(wnd);
1.1 deraadt 176: }
177: }