mbufs.c revision 40060
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 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35static char sccsid[] = "@(#)mbufs.c	8.1 (Berkeley) 6/6/93";
36static const char rcsid[] =
37	"$Id: mbufs.c,v 1.7 1998/06/09 04:17:21 imp 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 <errno.h>
46#include <stdlib.h>
47#include <string.h>
48#include <paths.h>
49#include "systat.h"
50#include "extern.h"
51
52static struct mbstat *mb;
53
54char *mtnames[] = {
55	"free",
56	"data",
57	"headers",
58	"sockets",
59	"pcbs",
60	"routes",
61	"hosts",
62	"arps",
63	"socknames",
64	"zombies",
65	"sockopts",
66	"frags",
67	"rights",
68	"ifaddrs",
69};
70
71#define	NNAMES	(sizeof (mtnames) / sizeof (mtnames[0]))
72
73WINDOW *
74openmbufs()
75{
76	return (subwin(stdscr, LINES-5-1, 0, 5, 0));
77}
78
79void
80closembufs(w)
81	WINDOW *w;
82{
83	if (w == NULL)
84		return;
85	wclear(w);
86	wrefresh(w);
87	delwin(w);
88}
89
90void
91labelmbufs()
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
98void
99showmbufs()
100{
101	register int i, j, max, index;
102	char buf[10];
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 (i == MT_FREE)
110				continue;
111			if (mb->m_mtypes[i] > max) {
112				max = mb->m_mtypes[i];
113				index = i;
114			}
115		}
116		if (max == 0)
117			break;
118		if (j > NNAMES)
119			mvwprintw(wnd, 1+j, 0, "%10d", index);
120		else
121			mvwprintw(wnd, 1+j, 0, "%-10.10s", mtnames[index]);
122		wmove(wnd, 1 + j, 10);
123		if (max > 60) {
124			snprintf(buf, sizeof(buf), " %d", max);
125			max = 60;
126			while (max--)
127				waddch(wnd, 'X');
128			waddstr(wnd, buf);
129		} else
130			while (max--)
131				waddch(wnd, 'X');
132		wclrtoeol(wnd);
133		mb->m_mbufs -= mb->m_mtypes[index];
134		mb->m_mtypes[index] = 0;
135	}
136	if (mb->m_mbufs) {
137		mvwprintw(wnd, 1+j, 0, "%-10.10s", "free");
138		if (mb->m_mbufs > 60) {
139			snprintf(buf, sizeof(buf), " %ld", mb->m_mbufs);
140			mb->m_mbufs = 60;
141			while (mb->m_mbufs--)
142				waddch(wnd, 'X');
143			waddstr(wnd, buf);
144		} else {
145			while(mb->m_mbufs--)
146				waddch(wnd, 'X');
147		}
148		wclrtoeol(wnd);
149		j++;
150	}
151	wmove(wnd, 1+j, 0); wclrtobot(wnd);
152}
153
154int
155initmbufs()
156{
157	size_t len;
158	int name[3];
159
160	name[0] = CTL_KERN;
161	name[1] = KERN_IPC;
162	name[2] = KIPC_MBSTAT;
163	len = 0;
164	if (sysctl(name, 3, 0, &len, 0, 0) < 0) {
165		error("sysctl getting mbstat size failed");
166		return 0;
167	}
168
169	if (mb == 0)
170		mb = (struct mbstat *)calloc(1, sizeof *mb);
171	return 1;
172}
173
174void
175fetchmbufs()
176{
177	int name[3];
178	size_t len;
179
180	name[0] = CTL_KERN;
181	name[1] = KERN_IPC;
182	name[2] = KIPC_MBSTAT;
183	len = sizeof *mb;
184
185	if (sysctl(name, 3, mb, &len, 0, 0) < 0)
186		printw("sysctl: %s", strerror(errno));
187}
188