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