Annotation of src/usr.bin/systat/mbufs.c, Revision 1.12
1.12 ! deraadt 1: /* $OpenBSD: mbufs.c,v 1.11 2002/06/18 00:46:47 deraadt Exp $ */
1.1 deraadt 2: /* $NetBSD: mbufs.c,v 1.2 1995/01/20 08:52:02 jtc Exp $ */
3:
4: /*-
5: * Copyright (c) 1980, 1992, 1993
6: * The Regents of the University of California. All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
16: * 3. All advertising materials mentioning features or use of this software
17: * must display the following acknowledgement:
18: * This product includes software developed by the University of
19: * California, Berkeley and its contributors.
20: * 4. Neither the name of the University nor the names of its contributors
21: * may be used to endorse or promote products derived from this software
22: * without specific prior written permission.
23: *
24: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34: * SUCH DAMAGE.
35: */
36:
37: #ifndef lint
38: #if 0
39: static char sccsid[] = "@(#)mbufs.c 8.1 (Berkeley) 6/6/93";
40: #endif
1.12 ! deraadt 41: static char rcsid[] = "$OpenBSD: mbufs.c,v 1.11 2002/06/18 00:46:47 deraadt Exp $";
1.1 deraadt 42: #endif /* not lint */
43:
44: #include <sys/param.h>
45: #include <sys/types.h>
46: #include <sys/mbuf.h>
1.10 angelos 47: #include <sys/sysctl.h>
1.1 deraadt 48:
49: #include <stdlib.h>
50: #include <string.h>
51: #include <nlist.h>
1.9 deraadt 52: #include <err.h>
1.1 deraadt 53: #include <paths.h>
54: #include "systat.h"
55: #include "extern.h"
56:
57: static struct mbstat *mb;
58:
59: char *mtnames[] = {
60: "free",
61: "data",
62: "headers",
63: "sockets",
64: "pcbs",
65: "routes",
66: "hosts",
67: "arps",
68: "socknames",
69: "zombies",
70: "sockopts",
71: "frags",
72: "rights",
73: "ifaddrs",
74: };
75:
76: #define NNAMES (sizeof (mtnames) / sizeof (mtnames[0]))
77:
78: WINDOW *
1.11 deraadt 79: openmbufs(void)
1.1 deraadt 80: {
81: return (subwin(stdscr, LINES-5-1, 0, 5, 0));
82: }
83:
84: void
1.11 deraadt 85: closembufs(WINDOW *w)
1.1 deraadt 86: {
87: if (w == NULL)
88: return;
89: wclear(w);
90: wrefresh(w);
91: delwin(w);
92: }
93:
94: void
1.11 deraadt 95: labelmbufs(void)
1.1 deraadt 96: {
97: wmove(wnd, 0, 0); wclrtoeol(wnd);
98: mvwaddstr(wnd, 0, 10,
99: "/0 /5 /10 /15 /20 /25 /30 /35 /40 /45 /50 /55 /60");
100: }
101:
102: void
1.11 deraadt 103: showmbufs(void)
1.1 deraadt 104: {
1.6 mpech 105: int i, j, max, index;
1.4 deraadt 106: char buf[13];
1.1 deraadt 107:
108: if (mb == 0)
109: return;
1.2 mickey 110: for (j = 0; j < wnd->_maxy; j++) {
1.9 deraadt 111: max = 0, index = -1;
1.2 mickey 112: for (i = 0; i < wnd->_maxy; i++)
1.1 deraadt 113: if (mb->m_mtypes[i] > max) {
114: max = mb->m_mtypes[i];
115: index = i;
116: }
117: if (max == 0)
118: break;
119: if (j > NNAMES)
120: mvwprintw(wnd, 1+j, 0, "%10d", index);
121: else
122: mvwprintw(wnd, 1+j, 0, "%-10.10s", mtnames[index]);
123: wmove(wnd, 1 + j, 10);
124: if (max > 60) {
1.4 deraadt 125: snprintf(buf, sizeof buf, " %d", max);
1.1 deraadt 126: max = 60;
127: while (max--)
128: waddch(wnd, 'X');
129: waddstr(wnd, buf);
130: } else {
131: while (max--)
132: waddch(wnd, 'X');
133: wclrtoeol(wnd);
134: }
135: mb->m_mtypes[index] = 0;
136: }
137: wmove(wnd, 1+j, 0); wclrtobot(wnd);
138: }
139:
140: static struct nlist namelist[] = {
141: #define X_MBSTAT 0
1.12 ! deraadt 142: { "_mbstat" }, /* sysctl */
1.1 deraadt 143: { "" }
144: };
145:
146: int
1.11 deraadt 147: initmbufs(void)
1.1 deraadt 148: {
1.5 ericj 149: int ret;
150:
1.10 angelos 151: if (kd != NULL) {
1.1 deraadt 152: if (namelist[X_MBSTAT].n_type == 0) {
1.10 angelos 153: if ((ret = kvm_nlist(kd, namelist)) == -1)
154: errx(1, "%s", kvm_geterr(kd));
155: else if (ret)
156: nlisterr(namelist);
157: if (namelist[X_MBSTAT].n_type == 0) {
158: error("namelist on %s failed", _PATH_UNIX);
159: return(0);
160: }
1.1 deraadt 161: }
162: }
163: if (mb == 0)
164: mb = (struct mbstat *)calloc(1, sizeof (*mb));
165: return(1);
166: }
167:
168: void
1.11 deraadt 169: fetchmbufs(void)
1.1 deraadt 170: {
1.10 angelos 171: int mib[2];
172: size_t size = sizeof (*mb);
173:
174: if (kd == NULL) {
175: mib[0] = CTL_KERN;
176: mib[1] = KERN_MBSTAT;
177: if (sysctl(mib, 2, mb, &size, NULL, 0) < 0)
178: err(1, "sysctl(KERN_MBSTAT) failed");
179: } else {
180: if (namelist[X_MBSTAT].n_type == 0)
181: return;
182: NREAD(X_MBSTAT, mb, size);
183: }
1.1 deraadt 184: }