xref: /netbsd-src/usr.bin/netstat/mbuf.c (revision b8c616269f5ebf18ab2e35cb8099d683130a177c)
1 /*	$NetBSD: mbuf.c,v 1.18 2002/12/14 11:12:24 martin Exp $	*/
2 
3 /*
4  * Copyright (c) 1983, 1988, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "from: @(#)mbuf.c	8.1 (Berkeley) 6/6/93";
40 #else
41 __RCSID("$NetBSD: mbuf.c,v 1.18 2002/12/14 11:12:24 martin Exp $");
42 #endif
43 #endif /* not lint */
44 
45 #define	__POOL_EXPOSE
46 
47 #include <sys/param.h>
48 #include <sys/protosw.h>
49 #include <sys/socket.h>
50 #include <sys/mbuf.h>
51 #include <sys/pool.h>
52 
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <limits.h>
56 #include "netstat.h"
57 
58 #define	YES	1
59 typedef int bool;
60 
61 struct	mbstat mbstat;
62 struct pool mbpool, mclpool;
63 struct pool_allocator mbpa, mclpa;
64 
65 static struct mbtypes {
66 	int	mt_type;
67 	char	*mt_name;
68 } mbtypes[] = {
69 	{ MT_DATA,	"data" },
70 	{ MT_OOBDATA,	"oob data" },
71 	{ MT_CONTROL,	"ancillary data" },
72 	{ MT_HEADER,	"packet headers" },
73 	{ MT_FTABLE,	"fragment reassembly queue headers" },	/* XXX */
74 	{ MT_SONAME,	"socket names and addresses" },
75 	{ MT_SOOPTS,	"socket options" },
76 	{ 0, 0 }
77 };
78 
79 int nmbtypes = sizeof(mbstat.m_mtypes) / sizeof(short);
80 bool seen[256];			/* "have we seen this type yet?" */
81 
82 /*
83  * Print mbuf statistics.
84  */
85 void
86 mbpr(mbaddr, msizeaddr, mclbaddr, mbpooladdr, mclpooladdr)
87 	u_long mbaddr;
88 	u_long msizeaddr, mclbaddr;
89 	u_long mbpooladdr, mclpooladdr;
90 {
91 	u_long totmem, totused, totpct;
92 	u_int totmbufs;
93 	int i;
94 	struct mbtypes *mp;
95 
96 	int	mclbytes,	msize;
97 
98 	if (nmbtypes != 256) {
99 		fprintf(stderr,
100 		    "%s: unexpected change to mbstat; check source\n",
101 		        getprogname());
102 		return;
103 	}
104 	if (mbaddr == 0) {
105 		fprintf(stderr, "%s: mbstat: symbol not in namelist\n",
106 		    getprogname());
107 		return;
108 	}
109 /*XXX*/
110 	if (msizeaddr != 0)
111 		kread(msizeaddr, (char *)&msize, sizeof (msize));
112 	else
113 		msize = MSIZE;
114 	if (mclbaddr != 0)
115 		kread(mclbaddr, (char *)&mclbytes, sizeof (mclbytes));
116 	else
117 		mclbytes = MCLBYTES;
118 /*XXX*/
119 
120 	if (kread(mbaddr, (char *)&mbstat, sizeof (mbstat)))
121 		return;
122 
123 	if (kread(mbpooladdr, (char *)&mbpool, sizeof (mbpool)))
124 		return;
125 
126 	if (kread(mclpooladdr, (char *)&mclpool, sizeof (mclpool)))
127 		return;
128 
129 	mbpooladdr = (u_long) mbpool.pr_alloc;
130 	mclpooladdr = (u_long) mclpool.pr_alloc;
131 
132 	if (kread(mbpooladdr, (char *)&mbpa, sizeof (mbpa)))
133 		return;
134 
135 	if (kread(mclpooladdr, (char *)&mclpa, sizeof (mclpa)))
136 		return;
137 
138 	totmbufs = 0;
139 	for (mp = mbtypes; mp->mt_name; mp++)
140 		totmbufs += mbstat.m_mtypes[mp->mt_type];
141 	printf("%u mbufs in use:\n", totmbufs);
142 	for (mp = mbtypes; mp->mt_name; mp++)
143 		if (mbstat.m_mtypes[mp->mt_type]) {
144 			seen[mp->mt_type] = YES;
145 			printf("\t%u mbufs allocated to %s\n",
146 			    mbstat.m_mtypes[mp->mt_type], mp->mt_name);
147 		}
148 	seen[MT_FREE] = YES;
149 	for (i = 0; i < nmbtypes; i++)
150 		if (!seen[i] && mbstat.m_mtypes[i]) {
151 			printf("\t%u mbufs allocated to <mbuf type %d>\n",
152 			    mbstat.m_mtypes[i], i);
153 		}
154 
155 	printf("%lu/%lu mapped pages in use\n",
156 	       (u_long)(mclpool.pr_nget - mclpool.pr_nput),
157 	       ((u_long)mclpool.pr_npages * mclpool.pr_itemsperpage));
158 	totmem = (mbpool.pr_npages << mbpa.pa_pageshift) +
159 	    (mclpool.pr_npages << mclpa.pa_pageshift);
160 	totused = (mbpool.pr_nget - mbpool.pr_nput) * mbpool.pr_size +
161 	    (mclpool.pr_nget - mclpool.pr_nput) * mclpool.pr_size;
162 	if (totmem == 0)
163 		totpct = 0;
164 	else if (totused < (ULONG_MAX/100))
165 		totpct = (totused * 100)/totmem;
166 	else {
167 		u_long totmem1 = totmem/100;
168 		u_long totused1 = totused/100;
169 		totpct = (totused1 * 100)/totmem1;
170 	}
171 
172 	printf("%lu Kbytes allocated to network (%lu%% in use)\n",
173 	    totmem / 1024, totpct);
174 	printf("%lu requests for memory denied\n", mbstat.m_drops);
175 	printf("%lu requests for memory delayed\n", mbstat.m_wait);
176 	printf("%lu calls to protocol drain routines\n", mbstat.m_drain);
177 }
178