Annotation of src/usr.bin/systat/swap.c, Revision 1.25
1.25 ! guenther 1: /* $OpenBSD: swap.c,v 1.24 2012/12/05 23:20:26 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: #include <sys/param.h>
35: #include <sys/conf.h>
36: #include <sys/ioctl.h>
37: #include <sys/stat.h>
1.11 weingart 38: #include <sys/swap.h>
1.1 deraadt 39:
40: #include <stdio.h>
41: #include <stdlib.h>
42: #include <string.h>
1.11 weingart 43: #include <errno.h>
1.1 deraadt 44: #include <unistd.h>
45:
46: #include "systat.h"
1.21 canacar 47:
1.1 deraadt 48:
1.11 weingart 49: static long blocksize;
50: static int hlen, nswap, rnswap;
51: static struct swapent *swap_devices;
1.1 deraadt 52:
1.21 canacar 53: void print_sw(void);
54: int read_sw(void);
55: int select_sw(void);
56: static void showswap(int i);
57: static void showtotal(void);
58:
59:
60: field_def fields_sw[] = {
61: {"DISK", 6, 16, 1, FLD_ALIGN_LEFT, -1, 0, 0, 0},
62: {"BLOCKS", 5, 10, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0},
63: {"USED", 5, 10, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0},
64: {"", 40, 80, 1, FLD_ALIGN_BAR, -1, 0, 0, 100},
65: };
66:
1.23 jasper 67: #define FLD_SW_NAME FIELD_ADDR(fields_sw,0)
68: #define FLD_SW_BLOCKS FIELD_ADDR(fields_sw,1)
69: #define FLD_SW_USED FIELD_ADDR(fields_sw,2)
70: #define FLD_SW_BAR FIELD_ADDR(fields_sw,3)
1.21 canacar 71:
72: /* Define views */
73: field_def *view_sw_0[] = {
74: FLD_SW_NAME, FLD_SW_BLOCKS, FLD_SW_USED, FLD_SW_BAR, NULL
75: };
76:
77:
78: /* Define view managers */
79: struct view_manager swap_mgr = {
80: "Swap", select_sw, read_sw, NULL, print_header,
81: print_sw, keyboard_callback, NULL, NULL
82: };
83:
84: field_view views_sw[] = {
85: {view_sw_0, "swap", '6', &swap_mgr},
86: {NULL, NULL, 0, NULL}
87: };
1.1 deraadt 88:
89:
1.9 millert 90: int
1.21 canacar 91: select_sw(void)
1.1 deraadt 92: {
1.21 canacar 93: if (swap_devices == NULL || nswap == 0)
94: num_disp = 1;
95: else
96: num_disp = nswap;
97: if (nswap > 1)
98: num_disp++;
99: return (0);
1.1 deraadt 100: }
101:
1.21 canacar 102: int
103: read_sw(void)
1.1 deraadt 104: {
1.21 canacar 105: num_disp = 1;
1.11 weingart 106:
107: nswap = swapctl(SWAP_NSWAP, 0, 0);
1.21 canacar 108:
1.11 weingart 109: if (nswap < 0)
110: error("error: %s", strerror(errno));
111: if (nswap == 0)
1.21 canacar 112: return 0;
1.1 deraadt 113:
1.11 weingart 114: if (swap_devices)
115: (void)free(swap_devices);
1.21 canacar 116:
1.20 deraadt 117: swap_devices = (struct swapent *)calloc(nswap, sizeof(*swap_devices));
1.11 weingart 118: if (swap_devices == NULL)
1.21 canacar 119: return 0;
1.11 weingart 120:
121: rnswap = swapctl(SWAP_STATS, (void *)swap_devices, nswap);
1.21 canacar 122: if (rnswap < 0 || nswap != rnswap)
123: return 0;
124:
125: num_disp = nswap;
126: if (nswap > 1)
127: num_disp++;
128:
129: return 0;
1.1 deraadt 130: }
131:
1.21 canacar 132:
1.1 deraadt 133: void
1.21 canacar 134: print_sw(void)
1.1 deraadt 135: {
1.21 canacar 136: int n, count = 0;
1.1 deraadt 137:
1.21 canacar 138: if (swap_devices == NULL || nswap == 0) {
139: print_fld_str(FLD_SW_BAR, "No swap devices");
1.11 weingart 140: return;
141: }
1.21 canacar 142:
143:
144: for (n = dispstart; n < num_disp; n++) {
145: if (n >= nswap)
146: showtotal();
147: else
148: showswap(n);
149: count++;
150: if (maxprint > 0 && count >= maxprint)
151: break;
152: }
153:
154: }
155:
156: int
157: initswap(void)
158: {
159: field_view *v;
160:
161: char *bs = getbsize(&hlen, &blocksize);
162:
163: FLD_SW_BLOCKS->title = strdup(bs);
164:
165: for (v = views_sw; v->name != NULL; v++)
166: add_view(v);
167:
168: return(1);
1.1 deraadt 169: }
170:
1.21 canacar 171:
172: static void
173: showswap(int i)
1.16 deraadt 174: {
1.22 canacar 175: int d, used, xsize;
1.11 weingart 176: struct swapent *sep;
177: char *p;
1.1 deraadt 178:
1.22 canacar 179: d = blocksize / 512;
1.11 weingart 180:
1.21 canacar 181: sep = &swap_devices[i];
182:
183: p = strrchr(sep->se_path, '/');
184: p = p ? p+1 : sep->se_path;
185:
186: print_fld_str(FLD_SW_NAME, p);
187:
188: xsize = sep->se_nblks;
189: used = sep->se_inuse;
190:
1.22 canacar 191: print_fld_uint(FLD_SW_BLOCKS, xsize / d);
192: print_fld_uint(FLD_SW_USED, used / d);
1.21 canacar 193: print_fld_bar(FLD_SW_BAR, 100 * used / xsize);
194:
195: end_line();
196: }
1.11 weingart 197:
1.21 canacar 198: static void
199: showtotal(void)
200: {
201: struct swapent *sep;
1.22 canacar 202: int d, i, avail, used, xsize, mfree;
1.11 weingart 203:
1.22 canacar 204: d = blocksize / 512;
205: mfree = avail = 0;
1.11 weingart 206:
1.21 canacar 207: for (sep = swap_devices, i = 0; i < nswap; i++, sep++) {
1.11 weingart 208: xsize = sep->se_nblks;
209: used = sep->se_inuse;
210: avail += xsize;
1.22 canacar 211: mfree += xsize - used;
1.1 deraadt 212: }
1.22 canacar 213: used = avail - mfree;
1.21 canacar 214:
215: print_fld_str(FLD_SW_NAME, "Total");
1.22 canacar 216: print_fld_uint(FLD_SW_BLOCKS, avail / d);
217: print_fld_uint(FLD_SW_USED, used / d);
1.21 canacar 218: print_fld_bar(FLD_SW_BAR, 100 * used / avail);
219:
220: end_line();
1.1 deraadt 221: }