mbufs.c revision 216370
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 * 4. Neither the name of the University nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#include <sys/cdefs.h>
31
32__FBSDID("$FreeBSD: head/usr.bin/systat/mbufs.c 216370 2010-12-11 08:32:16Z joel $");
33
34#ifdef lint
35static const char sccsid[] = "@(#)mbufs.c	8.1 (Berkeley) 6/6/93";
36#endif
37
38#include <sys/param.h>
39#include <sys/types.h>
40#include <sys/mbuf.h>
41#include <sys/sysctl.h>
42
43#include <errno.h>
44#include <stdlib.h>
45#include <string.h>
46#include <paths.h>
47
48#include "systat.h"
49#include "extern.h"
50
51static struct mbstat *mbstat;
52static long *m_mbtypes;
53static short nmbtypes;
54
55static struct mtnames {
56	short mt_type;
57	const char *mt_name;
58} mtnames[] = {
59	{ MT_DATA, 	"data"},
60	{ MT_HEADER,	"headers"},
61	{ MT_SONAME,	"socknames"},
62	{ MT_CONTROL,	"control"},
63	{ MT_OOBDATA,	"oobdata"}
64};
65#define	NNAMES	(sizeof (mtnames) / sizeof (mtnames[0]))
66
67WINDOW *
68openmbufs(void)
69{
70	return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0));
71}
72
73void
74closembufs(WINDOW *w)
75{
76	if (w == NULL)
77		return;
78	wclear(w);
79	wrefresh(w);
80	delwin(w);
81}
82
83void
84labelmbufs(void)
85{
86	wmove(wnd, 0, 0); wclrtoeol(wnd);
87	mvwaddstr(wnd, 0, 10,
88	    "/0   /5   /10  /15  /20  /25  /30  /35  /40  /45  /50  /55  /60");
89}
90
91void
92showmbufs(void)
93{
94	int i, j, max, idx;
95	u_long totmbufs;
96	char buf[10];
97	const char *mtname;
98
99	totmbufs = mbstat->m_mbufs;
100
101	/*
102	 * Print totals for different mbuf types.
103	 */
104	for (j = 0; j < wnd->_maxy; j++) {
105		max = 0, idx = -1;
106		for (i = 0; i < wnd->_maxy; i++) {
107			if (i == MT_NOTMBUF)
108				continue;
109			if (i >= nmbtypes)
110				break;
111			if (m_mbtypes[i] > max) {
112				max = m_mbtypes[i];
113				idx = i;
114			}
115		}
116		if (max == 0)
117			break;
118
119		mtname = NULL;
120		for (i = 0; i < (int)NNAMES; i++)
121			if (mtnames[i].mt_type == idx)
122				mtname = mtnames[i].mt_name;
123		if (mtname == NULL)
124			mvwprintw(wnd, 1+j, 0, "%10d", idx);
125		else
126			mvwprintw(wnd, 1+j, 0, "%-10.10s", mtname);
127		wmove(wnd, 1 + j, 10);
128		if (max > 60) {
129			snprintf(buf, sizeof(buf), " %d", max);
130			max = 60;
131			while (max--)
132				waddch(wnd, 'X');
133			waddstr(wnd, buf);
134		} else
135			while (max--)
136				waddch(wnd, 'X');
137		wclrtoeol(wnd);
138		m_mbtypes[idx] = 0;
139	}
140
141	/*
142	 * Print total number of free mbufs.
143	 */
144	if (totmbufs > 0) {
145		mvwprintw(wnd, 1+j, 0, "%-10.10s", "Mbufs");
146		if (totmbufs > 60) {
147			snprintf(buf, sizeof(buf), " %lu", totmbufs);
148			totmbufs = 60;
149			while(totmbufs--)
150				waddch(wnd, 'X');
151			waddstr(wnd, buf);
152		} else {
153			while(totmbufs--)
154				waddch(wnd, 'X');
155		}
156		wclrtoeol(wnd);
157		j++;
158	}
159	wmove(wnd, 1+j, 0); wclrtobot(wnd);
160}
161
162int
163initmbufs(void)
164{
165	size_t len;
166
167	len = sizeof *mbstat;
168	if ((mbstat = malloc(len)) == NULL) {
169		error("malloc mbstat failed");
170		return 0;
171	}
172	if (sysctlbyname("kern.ipc.mbstat", mbstat, &len, NULL, 0) < 0) {
173		error("sysctl retrieving mbstat");
174		return 0;
175	}
176	nmbtypes = mbstat->m_numtypes;
177	if ((m_mbtypes = calloc(nmbtypes, sizeof(long *))) == NULL) {
178		error("calloc m_mbtypes failed");
179		return 0;
180	}
181
182	return 1;
183}
184
185void
186fetchmbufs(void)
187{
188	size_t len;
189
190	len = sizeof *mbstat;
191	if (sysctlbyname("kern.ipc.mbstat", mbstat, &len, NULL, 0) < 0)
192		printw("sysctl: mbstat: %s", strerror(errno));
193}
194