mbufs.c revision 78664
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 78664 2001-06-23 17:03:27Z 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, ncpu;
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 < ncpu; i++)
154		totfree += mbpstat[i]->mb_mbfree;
155	j = 0;	/* XXX */
156	if (totfree > 0) {
157		mvwprintw(wnd, 1+j, 0, "%-10.10s", "free");
158		if (totfree > 60) {
159			snprintf(buf, sizeof(buf), " %lu", totfree);
160			totfree = 60;
161			while(totfree--)
162				waddch(wnd, 'X');
163			waddstr(wnd, buf);
164		} else {
165			while(totfree--)
166				waddch(wnd, 'X');
167		}
168		wclrtoeol(wnd);
169		j++;
170	}
171	wmove(wnd, 1+j, 0); wclrtobot(wnd);
172}
173
174int
175initmbufs()
176{
177	int i;
178	size_t len;
179#if 0
180	size_t mbtypeslen;
181
182	if (sysctlbyname("kern.ipc.mbtypes", NULL, &mbtypeslen, NULL, 0) < 0) {
183		error("sysctl getting mbtypes size failed");
184		return 0;
185	}
186	if ((m_mbtypes = calloc(1, mbtypeslen)) == NULL) {
187		error("calloc mbtypes failed");
188		return 0;
189	}
190	nmbtypes = mbtypeslen / sizeof(*m_mbtypes);
191#endif
192	len = sizeof(int);
193	if (sysctlbyname("kern.smp.cpus", &ncpu, &len, NULL, 0) < 0 &&
194	    sysctlbyname("hw.ncpu", &ncpu, &len, NULL, 0) < 0) {
195		error("sysctl getting number of cpus");
196		return 0;
197	}
198	if (sysctlbyname("kern.ipc.mb_statpcpu", NULL, &len, NULL, 0) < 0) {
199		error("sysctl getting mbpstat total size failed");
200		return 0;
201	}
202	num_objs = (int)(len / sizeof(struct mbpstat));
203	if ((mbpstat = calloc(num_objs, sizeof(struct mbpstat *))) == NULL) {
204		error("calloc mbpstat pointers failed");
205		return 0;
206	}
207	if ((mbpstat[0] = calloc(num_objs, sizeof(struct mbpstat))) == NULL) {
208		error("calloc mbpstat structures failed");
209		return 0;
210	}
211	for (i = 0; i < num_objs; i++)
212		mbpstat[i] = mbpstat[0] + i;
213
214	return 1;
215}
216
217void
218fetchmbufs()
219{
220	size_t len;
221
222	len = num_objs * sizeof(struct mbpstat);
223	if (sysctlbyname("kern.ipc.mb_statpcpu", mbpstat[0], &len, NULL, 0) < 0)
224		printw("sysctl: mbpstat: %s", strerror(errno));
225#if 0
226	len = nmbtypes * sizeof *m_mbtypes;
227	if (sysctlbyname("kern.ipc.mbtypes", m_mbtypes, &len, 0, 0) < 0)
228		printw("sysctl: mbtypes: %s", strerror(errno));
229#endif
230}
231