xref: /netbsd-src/usr.bin/systat/mbufs.c (revision 2696762d5fd1cb700be680c17e31b3bbcc5d1274)
1*2696762dSchristos /*	$NetBSD: mbufs.c,v 1.16 2012/11/23 01:43:10 christos Exp $	*/
22f3cb7aeSjtc 
312749a2aSjtc /*-
412749a2aSjtc  * Copyright (c) 1980, 1992, 1993
512749a2aSjtc  *	The Regents of the University of California.  All rights reserved.
612749a2aSjtc  *
712749a2aSjtc  * Redistribution and use in source and binary forms, with or without
812749a2aSjtc  * modification, are permitted provided that the following conditions
912749a2aSjtc  * are met:
1012749a2aSjtc  * 1. Redistributions of source code must retain the above copyright
1112749a2aSjtc  *    notice, this list of conditions and the following disclaimer.
1212749a2aSjtc  * 2. Redistributions in binary form must reproduce the above copyright
1312749a2aSjtc  *    notice, this list of conditions and the following disclaimer in the
1412749a2aSjtc  *    documentation and/or other materials provided with the distribution.
1589aaa1bbSagc  * 3. Neither the name of the University nor the names of its contributors
1612749a2aSjtc  *    may be used to endorse or promote products derived from this software
1712749a2aSjtc  *    without specific prior written permission.
1812749a2aSjtc  *
1912749a2aSjtc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2012749a2aSjtc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2112749a2aSjtc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2212749a2aSjtc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2312749a2aSjtc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2412749a2aSjtc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2512749a2aSjtc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2612749a2aSjtc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2712749a2aSjtc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2812749a2aSjtc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2912749a2aSjtc  * SUCH DAMAGE.
3012749a2aSjtc  */
3112749a2aSjtc 
3259413993Smrg #include <sys/cdefs.h>
3312749a2aSjtc #ifndef lint
342f3cb7aeSjtc #if 0
3512749a2aSjtc static char sccsid[] = "@(#)mbufs.c	8.1 (Berkeley) 6/6/93";
362f3cb7aeSjtc #endif
37*2696762dSchristos __RCSID("$NetBSD: mbufs.c,v 1.16 2012/11/23 01:43:10 christos Exp $");
3812749a2aSjtc #endif /* not lint */
3912749a2aSjtc 
4012749a2aSjtc #include <sys/param.h>
41*2696762dSchristos #define MBUFTYPES
4212749a2aSjtc #include <sys/mbuf.h>
43*2696762dSchristos #include <sys/sysctl.h>
4412749a2aSjtc 
45828483a7Ssimonb #include <stdlib.h>
46*2696762dSchristos #include <errno.h>
47*2696762dSchristos #include <string.h>
48828483a7Ssimonb 
4912749a2aSjtc #include "systat.h"
5012749a2aSjtc #include "extern.h"
5112749a2aSjtc 
5212749a2aSjtc static struct mbstat *mb;
5312749a2aSjtc 
54*2696762dSchristos #define	NNAMES	__arraycount(mbuftypes)
5512749a2aSjtc 
5612749a2aSjtc WINDOW *
openmbufs(void)57fc391547Sad openmbufs(void)
5812749a2aSjtc {
5947abaab8Smrg 
609f46bb07Sdsl 	return (subwin(stdscr, -1, 0, 5, 0));
6112749a2aSjtc }
6212749a2aSjtc 
6312749a2aSjtc void
closembufs(WINDOW * w)64fc391547Sad closembufs(WINDOW *w)
6512749a2aSjtc {
6647abaab8Smrg 
6712749a2aSjtc 	if (w == NULL)
6812749a2aSjtc 		return;
6912749a2aSjtc 	wclear(w);
7012749a2aSjtc 	wrefresh(w);
7112749a2aSjtc 	delwin(w);
7212749a2aSjtc }
7312749a2aSjtc 
7412749a2aSjtc void
labelmbufs(void)75fc391547Sad labelmbufs(void)
7612749a2aSjtc {
7747abaab8Smrg 
7812749a2aSjtc 	wmove(wnd, 0, 0); wclrtoeol(wnd);
7912749a2aSjtc 	mvwaddstr(wnd, 0, 10,
8012749a2aSjtc 	    "/0   /5   /10  /15  /20  /25  /30  /35  /40  /45  /50  /55  /60");
8112749a2aSjtc }
8212749a2aSjtc 
8312749a2aSjtc void
showmbufs(void)84fc391547Sad showmbufs(void)
8512749a2aSjtc {
86019733f6Sdsl 	int i, j, max, idx;
8712749a2aSjtc 	char buf[10];
8812749a2aSjtc 
8912749a2aSjtc 	if (mb == 0)
9012749a2aSjtc 		return;
91*2696762dSchristos 	for (j = 1; j <= getmaxy(wnd); j++) {
92019733f6Sdsl 		max = 0, idx = -1;
938fd3f56fSjtc 		for (i = 0; i < getmaxy(wnd); i++)
9412749a2aSjtc 			if (mb->m_mtypes[i] > max) {
9512749a2aSjtc 				max = mb->m_mtypes[i];
96019733f6Sdsl 				idx = i;
9712749a2aSjtc 			}
9812749a2aSjtc 		if (max == 0)
9912749a2aSjtc 			break;
100*2696762dSchristos 		if (j >= (int)NNAMES)
101*2696762dSchristos 			mvwprintw(wnd, j, 0, "%10d", idx);
10212749a2aSjtc 		else
103*2696762dSchristos 			mvwprintw(wnd, j, 0, "%-10.10s", &mbuftypes[idx][2]);
104*2696762dSchristos 		wmove(wnd, j, 10);
10512749a2aSjtc 		if (max > 60) {
106394d687bSbgrayson 			snprintf(buf, sizeof buf, " %5d", max);
10712749a2aSjtc 			max = 60;
10812749a2aSjtc 			while (max--)
10912749a2aSjtc 				waddch(wnd, 'X');
11012749a2aSjtc 			waddstr(wnd, buf);
11112749a2aSjtc 		} else {
11212749a2aSjtc 			wclrtoeol(wnd);
113981f9b7dSmycroft 			whline(wnd, 'X', max);
11412749a2aSjtc 		}
115019733f6Sdsl 		mb->m_mtypes[idx] = 0;
11612749a2aSjtc 	}
117*2696762dSchristos 	wmove(wnd, j, 0);
118*2696762dSchristos 	wclrtobot(wnd);
11912749a2aSjtc }
12012749a2aSjtc 
12112749a2aSjtc int
initmbufs(void)122fc391547Sad initmbufs(void)
12312749a2aSjtc {
12412749a2aSjtc 	if (mb == 0)
125*2696762dSchristos 		mb = calloc(1, sizeof (*mb));
12612749a2aSjtc 	return(1);
12712749a2aSjtc }
12812749a2aSjtc 
12912749a2aSjtc void
fetchmbufs(void)130fc391547Sad fetchmbufs(void)
13112749a2aSjtc {
132*2696762dSchristos 	size_t len = sizeof(*mb);
133*2696762dSchristos 	if (sysctlbyname("kern.mbuf.stats", mb, &len, NULL, 0))
134*2696762dSchristos 		error("error getting \"kern.mbuf.stats\": %s", strerror(errno));
13512749a2aSjtc }
136