xref: /csrg-svn/usr.bin/netstat/mbuf.c (revision 21989)
1*21989Sdist /*
2*21989Sdist  * Copyright (c) 1983 Regents of the University of California.
3*21989Sdist  * All rights reserved.  The Berkeley software License Agreement
4*21989Sdist  * specifies the terms and conditions for redistribution.
5*21989Sdist  */
6*21989Sdist 
77908Ssam #ifndef lint
8*21989Sdist static char sccsid[] = "@(#)mbuf.c	5.1 (Berkeley) 06/04/85";
9*21989Sdist #endif not lint
107908Ssam 
1118594Skupfer #include <stdio.h>
129568Ssam #include <sys/param.h>
137908Ssam #include <sys/mbuf.h>
1418594Skupfer #define	YES	1
1518594Skupfer typedef int bool;
167908Ssam 
177908Ssam struct	mbstat mbstat;
187908Ssam extern	int kmem;
197908Ssam 
209837Ssam static struct mbtypes {
219837Ssam 	int	mt_type;
229837Ssam 	char	*mt_name;
239837Ssam } mbtypes[] = {
249837Ssam 	{ MT_DATA,	"data" },
259837Ssam 	{ MT_HEADER,	"packet headers" },
269837Ssam 	{ MT_SOCKET,	"socket structures" },
279837Ssam 	{ MT_PCB,	"protocol control blocks" },
289837Ssam 	{ MT_RTABLE,	"routing table entries" },
299837Ssam 	{ MT_HTABLE,	"IMP host table entries" },
309837Ssam 	{ MT_ATABLE,	"address resolution tables" },
319837Ssam 	{ MT_FTABLE,	"fragment reassembly queue headers" },
329837Ssam 	{ MT_SONAME,	"socket names and addresses" },
339837Ssam 	{ MT_ZOMBIE,	"zombie process information" },
349837Ssam 	{ MT_SOOPTS,	"socket options" },
3517232Ssam 	{ MT_RIGHTS,	"access rights" },
3618594Skupfer 	{ MT_IFADDR,	"interface addresses" },
379837Ssam 	{ 0, 0 }
389837Ssam };
399837Ssam 
4018594Skupfer int nmbtypes = sizeof(mbstat.m_mtypes) / sizeof(short);
4118594Skupfer bool seen[256];			/* "have we seen this type yet?" */
4218594Skupfer 
437908Ssam /*
447908Ssam  * Print mbuf statistics.
457908Ssam  */
467908Ssam mbpr(mbaddr)
477908Ssam 	off_t mbaddr;
487908Ssam {
499837Ssam 	register int totmem, totfree, totmbufs;
5018594Skupfer 	register int i;
519837Ssam 	register struct mbtypes *mp;
529568Ssam 
5318594Skupfer 	if (nmbtypes != 256) {
5418594Skupfer 		fprintf(stderr, "unexpected change to mbstat; check source\n");
5518594Skupfer 		return;
5618594Skupfer 	}
577908Ssam 	if (mbaddr == 0) {
587908Ssam 		printf("mbstat: symbol not in namelist\n");
597908Ssam 		return;
607908Ssam 	}
617908Ssam 	klseek(kmem, mbaddr, 0);
629568Ssam 	if (read(kmem, &mbstat, sizeof (mbstat)) != sizeof (mbstat)) {
639568Ssam 		printf("mbstat: bad read\n");
649568Ssam 		return;
659568Ssam 	}
669837Ssam 	printf("%d/%d mbufs in use:\n",
6717232Ssam 		mbstat.m_mbufs - mbstat.m_mtypes[MT_FREE], mbstat.m_mbufs);
689837Ssam 	totmbufs = 0;
699837Ssam 	for (mp = mbtypes; mp->mt_name; mp++)
709837Ssam 		if (mbstat.m_mtypes[mp->mt_type]) {
7118594Skupfer 			seen[mp->mt_type] = YES;
729837Ssam 			printf("\t%d mbufs allocated to %s\n",
7318594Skupfer 			    mbstat.m_mtypes[mp->mt_type], mp->mt_name);
749837Ssam 			totmbufs += mbstat.m_mtypes[mp->mt_type];
759837Ssam 		}
7618594Skupfer 	seen[MT_FREE] = YES;
7718594Skupfer 	for (i = 0; i < nmbtypes; i++)
7818594Skupfer 		if (!seen[i] && mbstat.m_mtypes[i]) {
7918594Skupfer 			printf("\t%d mbufs allocated to <mbuf type %d>\n",
8018594Skupfer 			    mbstat.m_mtypes[i], i);
8118594Skupfer 			totmbufs += mbstat.m_mtypes[i];
8218594Skupfer 		}
8317232Ssam 	if (totmbufs != mbstat.m_mbufs - mbstat.m_mtypes[MT_FREE])
849837Ssam 		printf("*** %d mbufs missing ***\n",
8517232Ssam 			(mbstat.m_mbufs - mbstat.m_mtypes[MT_FREE]) - totmbufs);
869596Ssam 	printf("%d/%d mapped pages in use\n",
879568Ssam 		mbstat.m_clusters - mbstat.m_clfree, mbstat.m_clusters);
8817373Skarels 	totmem = mbstat.m_mbufs * MSIZE + mbstat.m_clusters * CLBYTES;
8917373Skarels 	totfree = mbstat.m_mtypes[MT_FREE]*MSIZE + mbstat.m_clfree * CLBYTES;
909837Ssam 	printf("%d Kbytes allocated to network (%d%% in use)\n",
919576Ssam 		totmem / 1024, (totmem - totfree) * 100 / totmem);
929837Ssam 	printf("%d requests for memory denied\n", mbstat.m_drops);
937908Ssam }
94