mbufs.c revision 50477
137535Sdes/*-
2135546Sdes * Copyright (c) 1980, 1992, 1993
337535Sdes *	The Regents of the University of California.  All rights reserved.
437535Sdes *
537535Sdes * Redistribution and use in source and binary forms, with or without
637535Sdes * modification, are permitted provided that the following conditions
737535Sdes * are met:
837535Sdes * 1. Redistributions of source code must retain the above copyright
937535Sdes *    notice, this list of conditions and the following disclaimer.
1037535Sdes * 2. Redistributions in binary form must reproduce the above copyright
1137535Sdes *    notice, this list of conditions and the following disclaimer in the
1237535Sdes *    documentation and/or other materials provided with the distribution.
1337535Sdes * 3. All advertising materials mentioning features or use of this software
1437535Sdes *    must display the following acknowledgement:
1563012Sdes *	This product includes software developed by the University of
1637535Sdes *	California, Berkeley and its contributors.
1737535Sdes * 4. Neither the name of the University nor the names of its contributors
1837535Sdes *    may be used to endorse or promote products derived from this software
1937535Sdes *    without specific prior written permission.
2037535Sdes *
2137535Sdes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2237535Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2337535Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2437535Sdes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2537535Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2637535Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2737535Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2837535Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2984203Sdillon * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3084203Sdillon * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3184203Sdillon * SUCH DAMAGE.
3263236Sdes */
3363236Sdes
3463236Sdes#ifndef lint
3563236Sdesstatic char sccsid[] = "@(#)mbufs.c	8.1 (Berkeley) 6/6/93";
3663236Sdesstatic const char rcsid[] =
3763236Sdes  "$FreeBSD: head/usr.bin/systat/mbufs.c 50477 1999-08-28 01:08:13Z peter $";
3863236Sdes#endif /* not lint */
3963236Sdes
4063236Sdes#include <sys/param.h>
4163236Sdes#include <sys/types.h>
4263236Sdes#include <sys/mbuf.h>
4363236Sdes#include <sys/sysctl.h>
4463236Sdes
4563236Sdes#include <errno.h>
4663236Sdes#include <stdlib.h>
4763236Sdes#include <string.h>
4863236Sdes#include <paths.h>
4990267Sdes#include "systat.h"
5063236Sdes#include "extern.h"
5163236Sdes
5263236Sdesstatic struct mbstat *mb;
5363236Sdes
5463236Sdeschar *mtnames[] = {
5563236Sdes	"free",
5663236Sdes	"data",
5763236Sdes	"headers",
5863236Sdes	"sockets",
5963236Sdes	"pcbs",
6063236Sdes	"routes",
6163236Sdes	"hosts",
6263236Sdes	"arps",
6363236Sdes	"socknames",
6437535Sdes	"zombies",
6560737Sume	"sockopts",
6637535Sdes	"frags",
6763012Sdes	"rights",
6837535Sdes	"ifaddrs",
6963012Sdes};
7060376Sdes
7160189Sdes#define	NNAMES	(sizeof (mtnames) / sizeof (mtnames[0]))
7237608Sdes
7337535SdesWINDOW *
7437535Sdesopenmbufs()
7537535Sdes{
7660376Sdes	return (subwin(stdscr, LINES-5-1, 0, 5, 0));
7737535Sdes}
7837535Sdes
79141958Skbyancvoid
80141958Skbyancclosembufs(w)
81141958Skbyanc	WINDOW *w;
8237535Sdes{
8340939Sdes	if (w == NULL)
8441862Sdes		return;
8537535Sdes	wclear(w);
8663012Sdes	wrefresh(w);
8763012Sdes	delwin(w);
8837535Sdes}
8963012Sdes
9063012Sdesvoid
9163012Sdeslabelmbufs()
9263012Sdes{
9363012Sdes	wmove(wnd, 0, 0); wclrtoeol(wnd);
9463012Sdes	mvwaddstr(wnd, 0, 10,
95169386Sdes	    "/0   /5   /10  /15  /20  /25  /30  /35  /40  /45  /50  /55  /60");
9663012Sdes}
9787317Sdes
98125696Sdesvoid
9963012Sdesshowmbufs()
10060196Sdes{
10163012Sdes	register int i, j, max, index;
10290267Sdes	char buf[10];
103169386Sdes
10490267Sdes	if (mb == 0)
10563012Sdes		return;
10688771Sdes	for (j = 0; j < wnd->maxy; j++) {
10763012Sdes		max = 0, index = -1;
10890267Sdes		for (i = 0; i < wnd->maxy; i++) {
10963012Sdes			if (i == MT_FREE)
11063012Sdes				continue;
11163012Sdes			if (mb->m_mtypes[i] > max) {
11263012Sdes				max = mb->m_mtypes[i];
11397859Sdes				index = i;
11437535Sdes			}
11597858Sdes		}
11697866Sdes		if (max == 0)
11797858Sdes			break;
11897866Sdes		if (j > NNAMES)
11997866Sdes			mvwprintw(wnd, 1+j, 0, "%10d", index);
12097866Sdes		else
12197858Sdes			mvwprintw(wnd, 1+j, 0, "%-10.10s", mtnames[index]);
12297858Sdes		wmove(wnd, 1 + j, 10);
12397858Sdes		if (max > 60) {
12463281Sdes			snprintf(buf, sizeof(buf), " %d", max);
12590267Sdes			max = 60;
12663012Sdes			while (max--)
12737535Sdes				waddch(wnd, 'X');
12837535Sdes			waddstr(wnd, buf);
12937608Sdes		} else
13063012Sdes			while (max--)
13137608Sdes				waddch(wnd, 'X');
13237608Sdes		wclrtoeol(wnd);
133174588Sdes		mb->m_mbufs -= mb->m_mtypes[index];
13437608Sdes		mb->m_mtypes[index] = 0;
13590267Sdes	}
13690267Sdes	if (mb->m_mbufs) {
137174588Sdes		mvwprintw(wnd, 1+j, 0, "%-10.10s", "free");
13890267Sdes		if (mb->m_mbufs > 60) {
13990267Sdes			snprintf(buf, sizeof(buf), " %ld", mb->m_mbufs);
140174761Sdes			mb->m_mbufs = 60;
14190267Sdes			while (mb->m_mbufs--)
14290267Sdes				waddch(wnd, 'X');
143174761Sdes			waddstr(wnd, buf);
14490267Sdes		} else {
14590267Sdes			while(mb->m_mbufs--)
146174761Sdes				waddch(wnd, 'X');
14790267Sdes		}
148174761Sdes		wclrtoeol(wnd);
14997859Sdes		j++;
15090267Sdes	}
15190267Sdes	wmove(wnd, 1+j, 0); wclrtobot(wnd);
15297859Sdes}
153176036Sdes
15490267Sdesint
15590267Sdesinitmbufs()
15690267Sdes{
15763281Sdes	size_t len;
15890267Sdes	int name[3];
15997859Sdes
16097859Sdes	name[0] = CTL_KERN;
161106207Sdes	name[1] = KERN_IPC;
16290267Sdes	name[2] = KIPC_MBSTAT;
163106207Sdes	len = 0;
164106207Sdes	if (sysctl(name, 3, 0, &len, 0, 0) < 0) {
165106207Sdes		error("sysctl getting mbstat size failed");
16690267Sdes		return 0;
16763012Sdes	}
16890267Sdes
16997859Sdes	if (mb == 0)
17037608Sdes		mb = (struct mbstat *)calloc(1, sizeof *mb);
17137608Sdes	return 1;
17237608Sdes}
17397866Sdes
17497866Sdesvoid
17597866Sdesfetchmbufs()
176174588Sdes{
17797866Sdes	int name[3];
17897866Sdes	size_t len;
17997866Sdes
18097866Sdes	name[0] = CTL_KERN;
18197866Sdes	name[1] = KERN_IPC;
18297866Sdes	name[2] = KIPC_MBSTAT;
18397866Sdes	len = sizeof *mb;
18497866Sdes
18597866Sdes	if (sysctl(name, 3, mb, &len, 0, 0) < 0)
18697866Sdes		printw("sysctl: %s", strerror(errno));
187106044Sdes}
18897866Sdes