11590Srgrimes/*-
21590Srgrimes * Copyright (c) 1980, 1992, 1993
31590Srgrimes *	The Regents of the University of California.  All rights reserved.
41590Srgrimes *
51590Srgrimes * Redistribution and use in source and binary forms, with or without
61590Srgrimes * modification, are permitted provided that the following conditions
71590Srgrimes * are met:
81590Srgrimes * 1. Redistributions of source code must retain the above copyright
91590Srgrimes *    notice, this list of conditions and the following disclaimer.
101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111590Srgrimes *    notice, this list of conditions and the following disclaimer in the
121590Srgrimes *    documentation and/or other materials provided with the distribution.
131590Srgrimes * 4. Neither the name of the University nor the names of its contributors
141590Srgrimes *    may be used to endorse or promote products derived from this software
151590Srgrimes *    without specific prior written permission.
161590Srgrimes *
171590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271590Srgrimes * SUCH DAMAGE.
281590Srgrimes */
291590Srgrimes
3087715Smarkm#include <sys/cdefs.h>
311590Srgrimes
3287715Smarkm__FBSDID("$FreeBSD$");
3387715Smarkm
3487715Smarkm#ifdef lint
3587715Smarkmstatic const char sccsid[] = "@(#)mbufs.c	8.1 (Berkeley) 6/6/93";
3687715Smarkm#endif
3787715Smarkm
381590Srgrimes#include <sys/param.h>
391590Srgrimes#include <sys/types.h>
401590Srgrimes#include <sys/mbuf.h>
4123087Swollman#include <sys/sysctl.h>
421590Srgrimes
4329759Swollman#include <errno.h>
441590Srgrimes#include <stdlib.h>
451590Srgrimes#include <string.h>
46200462Sdelphij#include <paths.h>
4787715Smarkm
481590Srgrimes#include "systat.h"
491590Srgrimes#include "extern.h"
501590Srgrimes
5184153Sbmilekicstatic struct mbstat *mbstat;
5284153Sbmilekicstatic long *m_mbtypes;
5384153Sbmilekicstatic short nmbtypes;
5478592Sbmilekic
5563226Salfredstatic struct mtnames {
5684153Sbmilekic	short mt_type;
5787715Smarkm	const char *mt_name;
5863226Salfred} mtnames[] = {
5963226Salfred	{ MT_DATA, 	"data"},
6063226Salfred	{ MT_HEADER,	"headers"},
6163226Salfred	{ MT_SONAME,	"socknames"},
6263226Salfred	{ MT_CONTROL,	"control"},
6363226Salfred	{ MT_OOBDATA,	"oobdata"}
641590Srgrimes};
651590Srgrimes#define	NNAMES	(sizeof (mtnames) / sizeof (mtnames[0]))
661590Srgrimes
671590SrgrimesWINDOW *
68175387Sdelphijopenmbufs(void)
691590Srgrimes{
70158160Sbde	return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0));
711590Srgrimes}
721590Srgrimes
731590Srgrimesvoid
74175387Sdelphijclosembufs(WINDOW *w)
751590Srgrimes{
761590Srgrimes	if (w == NULL)
771590Srgrimes		return;
781590Srgrimes	wclear(w);
791590Srgrimes	wrefresh(w);
801590Srgrimes	delwin(w);
811590Srgrimes}
821590Srgrimes
831590Srgrimesvoid
84175387Sdelphijlabelmbufs(void)
851590Srgrimes{
861590Srgrimes	wmove(wnd, 0, 0); wclrtoeol(wnd);
871590Srgrimes	mvwaddstr(wnd, 0, 10,
881590Srgrimes	    "/0   /5   /10  /15  /20  /25  /30  /35  /40  /45  /50  /55  /60");
891590Srgrimes}
901590Srgrimes
911590Srgrimesvoid
92175387Sdelphijshowmbufs(void)
931590Srgrimes{
9487715Smarkm	int i, j, max, idx;
95129906Sbmilekic	u_long totmbufs;
961590Srgrimes	char buf[10];
9787715Smarkm	const char *mtname;
981590Srgrimes
99129906Sbmilekic	totmbufs = mbstat->m_mbufs;
10084153Sbmilekic
10184153Sbmilekic	/*
10284153Sbmilekic	 * Print totals for different mbuf types.
10384153Sbmilekic	 */
10450635Speter	for (j = 0; j < wnd->_maxy; j++) {
10587715Smarkm		max = 0, idx = -1;
10650635Speter		for (i = 0; i < wnd->_maxy; i++) {
10784153Sbmilekic			if (i == MT_NOTMBUF)
10823087Swollman				continue;
10963226Salfred			if (i >= nmbtypes)
11063226Salfred				break;
11163226Salfred			if (m_mbtypes[i] > max) {
11263226Salfred				max = m_mbtypes[i];
11387715Smarkm				idx = i;
1141590Srgrimes			}
11523087Swollman		}
1161590Srgrimes		if (max == 0)
1171590Srgrimes			break;
11863226Salfred
11963226Salfred		mtname = NULL;
12087715Smarkm		for (i = 0; i < (int)NNAMES; i++)
12187715Smarkm			if (mtnames[i].mt_type == idx)
12263226Salfred				mtname = mtnames[i].mt_name;
12363226Salfred		if (mtname == NULL)
12487715Smarkm			mvwprintw(wnd, 1+j, 0, "%10d", idx);
1251590Srgrimes		else
12663226Salfred			mvwprintw(wnd, 1+j, 0, "%-10.10s", mtname);
1271590Srgrimes		wmove(wnd, 1 + j, 10);
1281590Srgrimes		if (max > 60) {
12936789Simp			snprintf(buf, sizeof(buf), " %d", max);
1301590Srgrimes			max = 60;
1311590Srgrimes			while (max--)
1321590Srgrimes				waddch(wnd, 'X');
1331590Srgrimes			waddstr(wnd, buf);
13415938Sjdp		} else
1351590Srgrimes			while (max--)
1361590Srgrimes				waddch(wnd, 'X');
13715938Sjdp		wclrtoeol(wnd);
13887715Smarkm		m_mbtypes[idx] = 0;
1391590Srgrimes	}
14078592Sbmilekic
14178592Sbmilekic	/*
14278592Sbmilekic	 * Print total number of free mbufs.
14378592Sbmilekic	 */
144129906Sbmilekic	if (totmbufs > 0) {
145129906Sbmilekic		mvwprintw(wnd, 1+j, 0, "%-10.10s", "Mbufs");
146129906Sbmilekic		if (totmbufs > 60) {
147129906Sbmilekic			snprintf(buf, sizeof(buf), " %lu", totmbufs);
148129906Sbmilekic			totmbufs = 60;
149129906Sbmilekic			while(totmbufs--)
15023087Swollman				waddch(wnd, 'X');
15123087Swollman			waddstr(wnd, buf);
15223087Swollman		} else {
153129906Sbmilekic			while(totmbufs--)
15423087Swollman				waddch(wnd, 'X');
15523087Swollman		}
15623087Swollman		wclrtoeol(wnd);
15723087Swollman		j++;
15823087Swollman	}
1591590Srgrimes	wmove(wnd, 1+j, 0); wclrtobot(wnd);
1601590Srgrimes}
1611590Srgrimes
1621590Srgrimesint
163175387Sdelphijinitmbufs(void)
1641590Srgrimes{
16578592Sbmilekic	size_t len;
16623087Swollman
16784153Sbmilekic	len = sizeof *mbstat;
16884153Sbmilekic	if ((mbstat = malloc(len)) == NULL) {
16984153Sbmilekic		error("malloc mbstat failed");
17063226Salfred		return 0;
17163226Salfred	}
17284153Sbmilekic	if (sysctlbyname("kern.ipc.mbstat", mbstat, &len, NULL, 0) < 0) {
17384153Sbmilekic		error("sysctl retrieving mbstat");
17463226Salfred		return 0;
17563226Salfred	}
17684153Sbmilekic	nmbtypes = mbstat->m_numtypes;
17784153Sbmilekic	if ((m_mbtypes = calloc(nmbtypes, sizeof(long *))) == NULL) {
17884153Sbmilekic		error("calloc m_mbtypes failed");
17984153Sbmilekic		return 0;
18084153Sbmilekic	}
18184153Sbmilekic
18223087Swollman	return 1;
1831590Srgrimes}
1841590Srgrimes
1851590Srgrimesvoid
186175387Sdelphijfetchmbufs(void)
1871590Srgrimes{
18823087Swollman	size_t len;
18923087Swollman
190129906Sbmilekic	len = sizeof *mbstat;
191129906Sbmilekic	if (sysctlbyname("kern.ipc.mbstat", mbstat, &len, NULL, 0) < 0)
192129906Sbmilekic		printw("sysctl: mbstat: %s", strerror(errno));
1931590Srgrimes}
194