mbufs.c revision 80399
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  "$FreeBSD: head/usr.bin/systat/mbufs.c 80399 2001-07-26 18:47:46Z bmilekic $";
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 mbpstat **mbpstat;
53static int num_objs;
54#define	GENLST	(num_objs - 1)
55
56/* XXX: mbtypes stats temporarily disabled. */
57#if 0
58static u_long *m_mbtypes;
59static int nmbtypes;
60
61static struct mtnames {
62	int mt_type;
63	char *mt_name;
64} mtnames[] = {
65	{ MT_DATA, 	"data"},
66	{ MT_HEADER,	"headers"},
67	{ MT_SONAME,	"socknames"},
68	{ MT_FTABLE,	"frags"},
69	{ MT_CONTROL,	"control"},
70	{ MT_OOBDATA,	"oobdata"}
71};
72
73#define	NNAMES	(sizeof (mtnames) / sizeof (mtnames[0]))
74#endif
75
76WINDOW *
77openmbufs()
78{
79	return (subwin(stdscr, LINES-5-1, 0, 5, 0));
80}
81
82void
83closembufs(w)
84	WINDOW *w;
85{
86	if (w == NULL)
87		return;
88	wclear(w);
89	wrefresh(w);
90	delwin(w);
91}
92
93void
94labelmbufs()
95{
96	wmove(wnd, 0, 0); wclrtoeol(wnd);
97	mvwaddstr(wnd, 0, 10,
98	    "/0   /5   /10  /15  /20  /25  /30  /35  /40  /45  /50  /55  /60");
99}
100
101void
102showmbufs()
103{
104	int i, j, max, index;
105	u_long totfree;
106	char buf[10];
107	char *mtname;
108
109/* XXX: mbtypes stats temporarily disabled (will be back soon!) */
110#if 0
111	for (j = 0; j < wnd->_maxy; j++) {
112		max = 0, index = -1;
113		for (i = 0; i < wnd->_maxy; i++) {
114			if (i == MT_FREE)
115				continue;
116			if (i >= nmbtypes)
117				break;
118			if (m_mbtypes[i] > max) {
119				max = m_mbtypes[i];
120				index = i;
121			}
122		}
123		if (max == 0)
124			break;
125
126		mtname = NULL;
127		for (i = 0; i < NNAMES; i++)
128			if (mtnames[i].mt_type == index)
129				mtname = mtnames[i].mt_name;
130		if (mtname == NULL)
131			mvwprintw(wnd, 1+j, 0, "%10d", index);
132		else
133			mvwprintw(wnd, 1+j, 0, "%-10.10s", mtname);
134		wmove(wnd, 1 + j, 10);
135		if (max > 60) {
136			snprintf(buf, sizeof(buf), " %d", max);
137			max = 60;
138			while (max--)
139				waddch(wnd, 'X');
140			waddstr(wnd, buf);
141		} else
142			while (max--)
143				waddch(wnd, 'X');
144		wclrtoeol(wnd);
145		m_mbtypes[index] = 0;
146	}
147#endif
148
149	/*
150	 * Print total number of free mbufs.
151	 */
152	totfree = mbpstat[GENLST]->mb_mbfree;
153	for (i = 0; i < (num_objs - 1); i++) {
154		if (mbpstat[i]->mb_active == 0)
155			continue;
156		totfree += mbpstat[i]->mb_mbfree;
157	}
158	j = 0;	/* XXX */
159	if (totfree > 0) {
160		mvwprintw(wnd, 1+j, 0, "%-10.10s", "free");
161		if (totfree > 60) {
162			snprintf(buf, sizeof(buf), " %lu", totfree);
163			totfree = 60;
164			while(totfree--)
165				waddch(wnd, 'X');
166			waddstr(wnd, buf);
167		} else {
168			while(totfree--)
169				waddch(wnd, 'X');
170		}
171		wclrtoeol(wnd);
172		j++;
173	}
174	wmove(wnd, 1+j, 0); wclrtobot(wnd);
175}
176
177int
178initmbufs()
179{
180	int i;
181	size_t len;
182#if 0
183	size_t mbtypeslen;
184
185	if (sysctlbyname("kern.ipc.mbtypes", NULL, &mbtypeslen, NULL, 0) < 0) {
186		error("sysctl getting mbtypes size failed");
187		return 0;
188	}
189	if ((m_mbtypes = calloc(1, mbtypeslen)) == NULL) {
190		error("calloc mbtypes failed");
191		return 0;
192	}
193	nmbtypes = mbtypeslen / sizeof(*m_mbtypes);
194#endif
195	if (sysctlbyname("kern.ipc.mb_statpcpu", NULL, &len, NULL, 0) < 0) {
196		error("sysctl getting mbpstat total size failed");
197		return 0;
198	}
199	num_objs = (int)(len / sizeof(struct mbpstat));
200	if ((mbpstat = calloc(num_objs, sizeof(struct mbpstat *))) == NULL) {
201		error("calloc mbpstat pointers failed");
202		return 0;
203	}
204	if ((mbpstat[0] = calloc(num_objs, sizeof(struct mbpstat))) == NULL) {
205		error("calloc mbpstat structures failed");
206		return 0;
207	}
208	for (i = 0; i < num_objs; i++)
209		mbpstat[i] = mbpstat[0] + i;
210
211	return 1;
212}
213
214void
215fetchmbufs()
216{
217	size_t len;
218
219	len = num_objs * sizeof(struct mbpstat);
220	if (sysctlbyname("kern.ipc.mb_statpcpu", mbpstat[0], &len, NULL, 0) < 0)
221		printw("sysctl: mbpstat: %s", strerror(errno));
222#if 0
223	len = nmbtypes * sizeof *m_mbtypes;
224	if (sysctlbyname("kern.ipc.mbtypes", m_mbtypes, &len, 0, 0) < 0)
225		printw("sysctl: mbtypes: %s", strerror(errno));
226#endif
227}
228