xref: /openbsd/usr.bin/systat/mbufs.c (revision df930be7)
1 /*	$NetBSD: mbufs.c,v 1.2 1995/01/20 08:52:02 jtc 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 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)mbufs.c	8.1 (Berkeley) 6/6/93";
39 #endif
40 static char rcsid[] = "$NetBSD: mbufs.c,v 1.2 1995/01/20 08:52:02 jtc Exp $";
41 #endif /* not lint */
42 
43 #include <sys/param.h>
44 #include <sys/types.h>
45 #include <sys/mbuf.h>
46 
47 #include <stdlib.h>
48 #include <string.h>
49 #include <nlist.h>
50 #include <paths.h>
51 #include "systat.h"
52 #include "extern.h"
53 
54 static struct mbstat *mb;
55 
56 char *mtnames[] = {
57 	"free",
58 	"data",
59 	"headers",
60 	"sockets",
61 	"pcbs",
62 	"routes",
63 	"hosts",
64 	"arps",
65 	"socknames",
66 	"zombies",
67 	"sockopts",
68 	"frags",
69 	"rights",
70 	"ifaddrs",
71 };
72 
73 #define	NNAMES	(sizeof (mtnames) / sizeof (mtnames[0]))
74 
75 WINDOW *
76 openmbufs()
77 {
78 	return (subwin(stdscr, LINES-5-1, 0, 5, 0));
79 }
80 
81 void
82 closembufs(w)
83 	WINDOW *w;
84 {
85 	if (w == NULL)
86 		return;
87 	wclear(w);
88 	wrefresh(w);
89 	delwin(w);
90 }
91 
92 void
93 labelmbufs()
94 {
95 	wmove(wnd, 0, 0); wclrtoeol(wnd);
96 	mvwaddstr(wnd, 0, 10,
97 	    "/0   /5   /10  /15  /20  /25  /30  /35  /40  /45  /50  /55  /60");
98 }
99 
100 void
101 showmbufs()
102 {
103 	register int i, j, max, index;
104 	char buf[10];
105 
106 	if (mb == 0)
107 		return;
108 	for (j = 0; j < wnd->maxy; j++) {
109 		max = 0, index = -1;
110 		for (i = 0; i < wnd->maxy; i++)
111 			if (mb->m_mtypes[i] > max) {
112 				max = mb->m_mtypes[i];
113 				index = i;
114 			}
115 		if (max == 0)
116 			break;
117 		if (j > NNAMES)
118 			mvwprintw(wnd, 1+j, 0, "%10d", index);
119 		else
120 			mvwprintw(wnd, 1+j, 0, "%-10.10s", mtnames[index]);
121 		wmove(wnd, 1 + j, 10);
122 		if (max > 60) {
123 			sprintf(buf, " %d", max);
124 			max = 60;
125 			while (max--)
126 				waddch(wnd, 'X');
127 			waddstr(wnd, buf);
128 		} else {
129 			while (max--)
130 				waddch(wnd, 'X');
131 			wclrtoeol(wnd);
132 		}
133 		mb->m_mtypes[index] = 0;
134 	}
135 	wmove(wnd, 1+j, 0); wclrtobot(wnd);
136 }
137 
138 static struct nlist namelist[] = {
139 #define	X_MBSTAT	0
140 	{ "_mbstat" },
141 	{ "" }
142 };
143 
144 int
145 initmbufs()
146 {
147 	if (namelist[X_MBSTAT].n_type == 0) {
148 		if (kvm_nlist(kd, namelist)) {
149 			nlisterr(namelist);
150 			return(0);
151 		}
152 		if (namelist[X_MBSTAT].n_type == 0) {
153 			error("namelist on %s failed", _PATH_UNIX);
154 			return(0);
155 		}
156 	}
157 	if (mb == 0)
158 		mb = (struct mbstat *)calloc(1, sizeof (*mb));
159 	return(1);
160 }
161 
162 void
163 fetchmbufs()
164 {
165 	if (namelist[X_MBSTAT].n_type == 0)
166 		return;
167 	NREAD(X_MBSTAT, mb, sizeof (*mb));
168 }
169