xref: /csrg-svn/usr.bin/netstat/mbuf.c (revision 33451)
121989Sdist /*
2*33451Skarels  * Copyright (c) 1983,1988 Regents of the University of California.
3*33451Skarels  * All rights reserved.
4*33451Skarels  *
5*33451Skarels  * Redistribution and use in source and binary forms are permitted
6*33451Skarels  * provided that this notice is preserved and that due credit is given
7*33451Skarels  * to the University of California at Berkeley. The name of the University
8*33451Skarels  * may not be used to endorse or promote products derived from this
9*33451Skarels  * software without specific prior written permission. This software
10*33451Skarels  * is provided ``as is'' without express or implied warranty.
1121989Sdist  */
1221989Sdist 
137908Ssam #ifndef lint
14*33451Skarels static char sccsid[] = "@(#)mbuf.c	5.5 (Berkeley) 02/07/88";
1521989Sdist #endif not lint
167908Ssam 
1718594Skupfer #include <stdio.h>
189568Ssam #include <sys/param.h>
197908Ssam #include <sys/mbuf.h>
2018594Skupfer #define	YES	1
2118594Skupfer typedef int bool;
227908Ssam 
237908Ssam struct	mbstat mbstat;
247908Ssam extern	int kmem;
257908Ssam 
269837Ssam static struct mbtypes {
279837Ssam 	int	mt_type;
289837Ssam 	char	*mt_name;
299837Ssam } mbtypes[] = {
309837Ssam 	{ MT_DATA,	"data" },
319837Ssam 	{ MT_HEADER,	"packet headers" },
329837Ssam 	{ MT_SOCKET,	"socket structures" },
339837Ssam 	{ MT_PCB,	"protocol control blocks" },
349837Ssam 	{ MT_RTABLE,	"routing table entries" },
359837Ssam 	{ MT_HTABLE,	"IMP host table entries" },
369837Ssam 	{ MT_ATABLE,	"address resolution tables" },
379837Ssam 	{ MT_FTABLE,	"fragment reassembly queue headers" },
389837Ssam 	{ MT_SONAME,	"socket names and addresses" },
399837Ssam 	{ MT_SOOPTS,	"socket options" },
4017232Ssam 	{ MT_RIGHTS,	"access rights" },
4118594Skupfer 	{ MT_IFADDR,	"interface addresses" },
429837Ssam 	{ 0, 0 }
439837Ssam };
449837Ssam 
4518594Skupfer int nmbtypes = sizeof(mbstat.m_mtypes) / sizeof(short);
4618594Skupfer bool seen[256];			/* "have we seen this type yet?" */
4718594Skupfer 
487908Ssam /*
497908Ssam  * Print mbuf statistics.
507908Ssam  */
517908Ssam mbpr(mbaddr)
527908Ssam 	off_t mbaddr;
537908Ssam {
549837Ssam 	register int totmem, totfree, totmbufs;
5518594Skupfer 	register int i;
569837Ssam 	register struct mbtypes *mp;
579568Ssam 
5818594Skupfer 	if (nmbtypes != 256) {
5918594Skupfer 		fprintf(stderr, "unexpected change to mbstat; check source\n");
6018594Skupfer 		return;
6118594Skupfer 	}
627908Ssam 	if (mbaddr == 0) {
637908Ssam 		printf("mbstat: symbol not in namelist\n");
647908Ssam 		return;
657908Ssam 	}
667908Ssam 	klseek(kmem, mbaddr, 0);
6729748Skupfer 	if (read(kmem, (char *)&mbstat, sizeof (mbstat)) != sizeof (mbstat)) {
689568Ssam 		printf("mbstat: bad read\n");
699568Ssam 		return;
709568Ssam 	}
7130451Skarels 	printf("%u/%u mbufs in use:\n",
7217232Ssam 		mbstat.m_mbufs - mbstat.m_mtypes[MT_FREE], mbstat.m_mbufs);
739837Ssam 	totmbufs = 0;
749837Ssam 	for (mp = mbtypes; mp->mt_name; mp++)
759837Ssam 		if (mbstat.m_mtypes[mp->mt_type]) {
7618594Skupfer 			seen[mp->mt_type] = YES;
7730451Skarels 			printf("\t%u mbufs allocated to %s\n",
7818594Skupfer 			    mbstat.m_mtypes[mp->mt_type], mp->mt_name);
799837Ssam 			totmbufs += mbstat.m_mtypes[mp->mt_type];
809837Ssam 		}
8118594Skupfer 	seen[MT_FREE] = YES;
8218594Skupfer 	for (i = 0; i < nmbtypes; i++)
8318594Skupfer 		if (!seen[i] && mbstat.m_mtypes[i]) {
8430451Skarels 			printf("\t%u mbufs allocated to <mbuf type %d>\n",
8518594Skupfer 			    mbstat.m_mtypes[i], i);
8618594Skupfer 			totmbufs += mbstat.m_mtypes[i];
8718594Skupfer 		}
8817232Ssam 	if (totmbufs != mbstat.m_mbufs - mbstat.m_mtypes[MT_FREE])
8930451Skarels 		printf("*** %u mbufs missing ***\n",
9017232Ssam 			(mbstat.m_mbufs - mbstat.m_mtypes[MT_FREE]) - totmbufs);
9130451Skarels 	printf("%u/%u mapped pages in use\n",
929568Ssam 		mbstat.m_clusters - mbstat.m_clfree, mbstat.m_clusters);
9331962Smckusick 	totmem = mbstat.m_mbufs * MSIZE + mbstat.m_clusters * CLBYTES;
9417373Skarels 	totfree = mbstat.m_mtypes[MT_FREE]*MSIZE + mbstat.m_clfree * CLBYTES;
9530451Skarels 	printf("%u Kbytes allocated to network (%d%% in use)\n",
969576Ssam 		totmem / 1024, (totmem - totfree) * 100 / totmem);
9730451Skarels 	printf("%u requests for memory denied\n", mbstat.m_drops);
9830451Skarels 	printf("%u requests for memory delayed\n", mbstat.m_wait);
9930451Skarels 	printf("%u calls to protocol drain routines\n", mbstat.m_drain);
1007908Ssam }
101