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