xref: /csrg-svn/usr.bin/netstat/mbuf.c (revision 18594)
17908Ssam #ifndef lint
2*18594Skupfer static char sccsid[] = "@(#)mbuf.c	4.8 85/04/07";
37908Ssam #endif
47908Ssam 
5*18594Skupfer #include <stdio.h>
69568Ssam #include <sys/param.h>
77908Ssam #include <sys/mbuf.h>
8*18594Skupfer #define	YES	1
9*18594Skupfer typedef int bool;
107908Ssam 
117908Ssam struct	mbstat mbstat;
127908Ssam extern	int kmem;
137908Ssam 
149837Ssam static struct mbtypes {
159837Ssam 	int	mt_type;
169837Ssam 	char	*mt_name;
179837Ssam } mbtypes[] = {
189837Ssam 	{ MT_DATA,	"data" },
199837Ssam 	{ MT_HEADER,	"packet headers" },
209837Ssam 	{ MT_SOCKET,	"socket structures" },
219837Ssam 	{ MT_PCB,	"protocol control blocks" },
229837Ssam 	{ MT_RTABLE,	"routing table entries" },
239837Ssam 	{ MT_HTABLE,	"IMP host table entries" },
249837Ssam 	{ MT_ATABLE,	"address resolution tables" },
259837Ssam 	{ MT_FTABLE,	"fragment reassembly queue headers" },
269837Ssam 	{ MT_SONAME,	"socket names and addresses" },
279837Ssam 	{ MT_ZOMBIE,	"zombie process information" },
289837Ssam 	{ MT_SOOPTS,	"socket options" },
2917232Ssam 	{ MT_RIGHTS,	"access rights" },
30*18594Skupfer 	{ MT_IFADDR,	"interface addresses" },
319837Ssam 	{ 0, 0 }
329837Ssam };
339837Ssam 
34*18594Skupfer int nmbtypes = sizeof(mbstat.m_mtypes) / sizeof(short);
35*18594Skupfer bool seen[256];			/* "have we seen this type yet?" */
36*18594Skupfer 
377908Ssam /*
387908Ssam  * Print mbuf statistics.
397908Ssam  */
407908Ssam mbpr(mbaddr)
417908Ssam 	off_t mbaddr;
427908Ssam {
439837Ssam 	register int totmem, totfree, totmbufs;
44*18594Skupfer 	register int i;
459837Ssam 	register struct mbtypes *mp;
469568Ssam 
47*18594Skupfer 	if (nmbtypes != 256) {
48*18594Skupfer 		fprintf(stderr, "unexpected change to mbstat; check source\n");
49*18594Skupfer 		return;
50*18594Skupfer 	}
517908Ssam 	if (mbaddr == 0) {
527908Ssam 		printf("mbstat: symbol not in namelist\n");
537908Ssam 		return;
547908Ssam 	}
557908Ssam 	klseek(kmem, mbaddr, 0);
569568Ssam 	if (read(kmem, &mbstat, sizeof (mbstat)) != sizeof (mbstat)) {
579568Ssam 		printf("mbstat: bad read\n");
589568Ssam 		return;
599568Ssam 	}
609837Ssam 	printf("%d/%d mbufs in use:\n",
6117232Ssam 		mbstat.m_mbufs - mbstat.m_mtypes[MT_FREE], mbstat.m_mbufs);
629837Ssam 	totmbufs = 0;
639837Ssam 	for (mp = mbtypes; mp->mt_name; mp++)
649837Ssam 		if (mbstat.m_mtypes[mp->mt_type]) {
65*18594Skupfer 			seen[mp->mt_type] = YES;
669837Ssam 			printf("\t%d mbufs allocated to %s\n",
67*18594Skupfer 			    mbstat.m_mtypes[mp->mt_type], mp->mt_name);
689837Ssam 			totmbufs += mbstat.m_mtypes[mp->mt_type];
699837Ssam 		}
70*18594Skupfer 	seen[MT_FREE] = YES;
71*18594Skupfer 	for (i = 0; i < nmbtypes; i++)
72*18594Skupfer 		if (!seen[i] && mbstat.m_mtypes[i]) {
73*18594Skupfer 			printf("\t%d mbufs allocated to <mbuf type %d>\n",
74*18594Skupfer 			    mbstat.m_mtypes[i], i);
75*18594Skupfer 			totmbufs += mbstat.m_mtypes[i];
76*18594Skupfer 		}
7717232Ssam 	if (totmbufs != mbstat.m_mbufs - mbstat.m_mtypes[MT_FREE])
789837Ssam 		printf("*** %d mbufs missing ***\n",
7917232Ssam 			(mbstat.m_mbufs - mbstat.m_mtypes[MT_FREE]) - totmbufs);
809596Ssam 	printf("%d/%d mapped pages in use\n",
819568Ssam 		mbstat.m_clusters - mbstat.m_clfree, mbstat.m_clusters);
8217373Skarels 	totmem = mbstat.m_mbufs * MSIZE + mbstat.m_clusters * CLBYTES;
8317373Skarels 	totfree = mbstat.m_mtypes[MT_FREE]*MSIZE + mbstat.m_clfree * CLBYTES;
849837Ssam 	printf("%d Kbytes allocated to network (%d%% in use)\n",
859576Ssam 		totmem / 1024, (totmem - totfree) * 100 / totmem);
869837Ssam 	printf("%d requests for memory denied\n", mbstat.m_drops);
877908Ssam }
88