Remove advertising header from all userland binaries.
[dragonfly.git] / usr.bin / systat / swap.c
1 /*-
2  * Copyright (c) 1980, 1992, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * @(#)swap.c   8.3 (Berkeley) 4/29/95
30  * $FreeBSD: src/usr.bin/systat/swap.c,v 1.12.2.2 2001/07/04 22:54:14 kris Exp $
31  * $DragonFly: src/usr.bin/systat/swap.c,v 1.5 2008/11/10 04:59:45 swildner Exp $
32  */
33
34 /*
35  * swapinfo - based on a program of the same name by Kevin Lahey
36  */
37
38 #include <sys/param.h>
39 #include <sys/conf.h>
40 #include <sys/ioctl.h>
41 #include <sys/stat.h>
42
43 #include <kvm.h>
44 #include <nlist.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <unistd.h>
48 #include <string.h>
49 #include <err.h>
50
51 #include "systat.h"
52 #include "extern.h"
53
54 void showspace(char *header, int hlen, long blocksize);
55
56 kvm_t   *kd;
57
58 static long blocksize;
59 static int hlen;
60
61 WINDOW *
62 openswap(void)
63 {
64         return (subwin(stdscr, LINES-5-1, 0, 5, 0));
65 }
66
67 void
68 closeswap(WINDOW *w)
69 {
70         if (w == NULL)
71                 return;
72         wclear(w);
73         wrefresh(w);
74         delwin(w);
75 }
76
77 /*
78  * The meat of all the swap stuff is stolen from pstat(8)'s
79  * swapmode(), which is based on a program called swapinfo written by
80  * Kevin Lahey <kml@rokkaku.atl.ga.us>.
81  */
82
83 int
84 initswap(void)
85 {
86         char msgbuf[BUFSIZ];
87         static int once = 0;
88         struct kvm_swap dummy;
89
90         if (once)
91                 return (1);
92
93         if (kvm_getswapinfo(kd, &dummy, 1, 0) < 0) {
94                 snprintf(msgbuf, sizeof(msgbuf), "systat: kvm_getswapinfo failed");
95                 error("%s", msgbuf);
96                 return (0);
97         }
98
99         once = 1;
100         return (1);
101 }
102
103 static struct kvm_swap  kvmsw[16];
104 static int kvnsw;
105
106 void
107 fetchswap(void)
108 {
109         kvnsw = kvm_getswapinfo(kd, kvmsw, 16, 0);
110 }
111
112 void
113 labelswap(void)
114 {
115         char *header;
116         int row, i;
117
118         fetchswap();
119
120         row = 0;
121         wmove(wnd, row, 0); wclrtobot(wnd);
122         header = getbsize(&hlen, &blocksize);
123         mvwprintw(wnd, row++, 0, "%-5s%*s%9s %55s",
124             "Disk", hlen, header, "Used",
125             "/0%  /10% /20% /30% /40% /50% /60% /70% /80% /90% /100");
126
127         for (i = 0; i < kvnsw; ++i) {
128                 mvwprintw(wnd, i + 1, 0, "%-5s", kvmsw[i].ksw_devname);
129         }
130 }
131
132 void
133 showswap(void)
134 {
135         int i;
136         int pagesize = getpagesize();
137
138 #define CONVERT(v)      ((int)((quad_t)(v) * pagesize / blocksize))
139
140         for (i = 0; i <= kvnsw; ++i) {
141                 int _col = 5;
142                 int count;
143
144                 if (i == kvnsw) {
145                         if (kvnsw == 1)
146                                 break;
147                         mvwprintw(
148                             wnd,
149                             i + 1,
150                             _col,
151                             "%-5s",
152                             "Total"
153                         );
154                         _col += 5;
155                 }
156                 if (kvmsw[i].ksw_total == 0) {
157                         mvwprintw(
158                             wnd,
159                             i + 1,
160                             _col + 5,
161                             "(swap not configured)"
162                         );
163                         continue;
164                 }
165
166                 mvwprintw(
167                     wnd, 
168                     i + 1, 
169                     _col,
170                     "%*d",
171                     hlen, 
172                     CONVERT(kvmsw[i].ksw_total)
173                 );
174                 _col += hlen;
175
176                 mvwprintw(
177                     wnd,
178                     i + 1,
179                     _col, 
180                     "%9d  ",
181                     CONVERT(kvmsw[i].ksw_used)
182                 );
183
184                 count = (int)((double)kvmsw[i].ksw_used * 49.999 /
185                     (double)kvmsw[i].ksw_total);
186
187                 while (count >= 0) {
188                         waddch(wnd, 'X');
189                         --count;
190                 }
191         }
192 }