Fix up mix of man(7)/mdoc(7).
[netbsd-mini2440.git] / usr.bin / netstat / mbuf.c
blob35167855b113b5b6e3c913059d2b715737795fac
1 /* $NetBSD: mbuf.c,v 1.28 2008/01/17 14:53:18 yamt Exp $ */
3 /*
4 * Copyright (c) 1983, 1988, 1993
5 * The Regents of the University of California. All rights reserved.
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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "from: @(#)mbuf.c 8.1 (Berkeley) 6/6/93";
36 #else
37 __RCSID("$NetBSD: mbuf.c,v 1.28 2008/01/17 14:53:18 yamt Exp $");
38 #endif
39 #endif /* not lint */
41 #define __POOL_EXPOSE
43 #include <sys/param.h>
44 #include <sys/protosw.h>
45 #include <sys/socket.h>
46 #include <sys/mbuf.h>
47 #include <sys/pool.h>
48 #include <sys/sysctl.h>
50 #include <stdio.h>
51 #include <kvm.h>
52 #include <stdlib.h>
53 #include <limits.h>
54 #include <errno.h>
55 #include <err.h>
56 #include <stdbool.h>
57 #include "netstat.h"
59 #define YES 1
61 struct mbstat mbstat;
62 struct pool mbpool, mclpool;
63 struct pool_allocator mbpa, mclpa;
65 static struct mbtypes {
66 int mt_type;
67 const 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 }
79 const int nmbtypes = sizeof(mbstat.m_mtypes) / sizeof(short);
80 bool seen[256]; /* "have we seen this type yet?" */
82 int mbstats_ctl[] = { CTL_KERN, KERN_MBUF, MBUF_STATS };
83 int mowners_ctl[] = { CTL_KERN, KERN_MBUF, MBUF_MOWNERS };
86 * Print mbuf statistics.
88 void
89 mbpr(mbaddr, msizeaddr, mclbaddr, mbpooladdr, mclpooladdr)
90 u_long mbaddr;
91 u_long msizeaddr, mclbaddr;
92 u_long mbpooladdr, mclpooladdr;
94 u_long totmem, totused, totpct;
95 u_int totmbufs;
96 int i, lines;
97 struct mbtypes *mp;
98 size_t len;
99 void *data;
100 struct mowner_user *mo;
101 int mclbytes, msize;
103 if (nmbtypes != 256) {
104 fprintf(stderr,
105 "%s: unexpected change to mbstat; check source\n",
106 getprogname());
107 return;
110 if (use_sysctl) {
111 size_t mbstatlen = sizeof(mbstat);
112 if (sysctl(mbstats_ctl,
113 sizeof(mbstats_ctl) / sizeof(mbstats_ctl[0]),
114 &mbstat, &mbstatlen, NULL, 0) < 0) {
115 warn("mbstat: sysctl failed");
116 return;
118 goto printit;
121 if (mbaddr == 0) {
122 fprintf(stderr, "%s: mbstat: symbol not in namelist\n",
123 getprogname());
124 return;
126 /*XXX*/
127 if (msizeaddr != 0)
128 kread(msizeaddr, (char *)&msize, sizeof (msize));
129 else
130 msize = MSIZE;
131 if (mclbaddr != 0)
132 kread(mclbaddr, (char *)&mclbytes, sizeof (mclbytes));
133 else
134 mclbytes = MCLBYTES;
135 /*XXX*/
137 if (kread(mbaddr, (char *)&mbstat, sizeof (mbstat)))
138 return;
140 if (kread(mbpooladdr, (char *)&mbpool, sizeof (mbpool)))
141 return;
143 if (kread(mclpooladdr, (char *)&mclpool, sizeof (mclpool)))
144 return;
146 mbpooladdr = (u_long) mbpool.pr_alloc;
147 mclpooladdr = (u_long) mclpool.pr_alloc;
149 if (kread(mbpooladdr, (char *)&mbpa, sizeof (mbpa)))
150 return;
152 if (kread(mclpooladdr, (char *)&mclpa, sizeof (mclpa)))
153 return;
155 printit:
156 totmbufs = 0;
157 for (mp = mbtypes; mp->mt_name; mp++)
158 totmbufs += mbstat.m_mtypes[mp->mt_type];
159 printf("%u mbufs in use:\n", totmbufs);
160 for (mp = mbtypes; mp->mt_name; mp++)
161 if (mbstat.m_mtypes[mp->mt_type]) {
162 seen[mp->mt_type] = YES;
163 printf("\t%u mbufs allocated to %s\n",
164 mbstat.m_mtypes[mp->mt_type], mp->mt_name);
166 seen[MT_FREE] = YES;
167 for (i = 0; i < nmbtypes; i++)
168 if (!seen[i] && mbstat.m_mtypes[i]) {
169 printf("\t%u mbufs allocated to <mbuf type %d>\n",
170 mbstat.m_mtypes[i], i);
173 if (use_sysctl) /* XXX */
174 goto dump_drain;
176 printf("%lu/%lu mapped pages in use\n",
177 (u_long)(mclpool.pr_nget - mclpool.pr_nput),
178 ((u_long)mclpool.pr_npages * mclpool.pr_itemsperpage));
179 totmem = (mbpool.pr_npages << mbpa.pa_pageshift) +
180 (mclpool.pr_npages << mclpa.pa_pageshift);
181 totused = (mbpool.pr_nget - mbpool.pr_nput) * mbpool.pr_size +
182 (mclpool.pr_nget - mclpool.pr_nput) * mclpool.pr_size;
183 if (totmem == 0)
184 totpct = 0;
185 else if (totused < (ULONG_MAX/100))
186 totpct = (totused * 100)/totmem;
187 else {
188 u_long totmem1 = totmem/100;
189 u_long totused1 = totused/100;
190 totpct = (totused1 * 100)/totmem1;
193 printf("%lu Kbytes allocated to network (%lu%% in use)\n",
194 totmem / 1024, totpct);
196 dump_drain:
197 printf("%lu calls to protocol drain routines\n", mbstat.m_drain);
199 if (sflag < 2)
200 return;
202 if (!use_sysctl)
203 return;
205 if (sysctl(mowners_ctl, sizeof(mowners_ctl)/sizeof(mowners_ctl[0]),
206 NULL, &len, NULL, 0) < 0) {
207 if (errno == ENOENT)
208 return;
209 warn("mowners: sysctl test");
210 return;
212 len += 10 * sizeof(*mo); /* add some slop */
213 data = malloc(len);
214 if (data == NULL) {
215 warn("malloc(%lu)", (u_long)len);
216 return;
219 if (sysctl(mowners_ctl, sizeof(mowners_ctl)/sizeof(mowners_ctl[0]),
220 data, &len, NULL, 0) < 0) {
221 warn("mowners: sysctl get");
222 free(data);
223 return;
226 for (mo = (void *) data, lines = 0; len >= sizeof(*mo);
227 len -= sizeof(*mo), mo++) {
228 char buf[32];
229 if (vflag == 1 &&
230 mo->mo_counter[MOWNER_COUNTER_CLAIMS] == 0 &&
231 mo->mo_counter[MOWNER_COUNTER_EXT_CLAIMS] == 0 &&
232 mo->mo_counter[MOWNER_COUNTER_CLUSTER_CLAIMS] == 0)
233 continue;
234 if (vflag == 0 &&
235 mo->mo_counter[MOWNER_COUNTER_CLAIMS] ==
236 mo->mo_counter[MOWNER_COUNTER_RELEASES] &&
237 mo->mo_counter[MOWNER_COUNTER_EXT_CLAIMS] ==
238 mo->mo_counter[MOWNER_COUNTER_EXT_RELEASES] &&
239 mo->mo_counter[MOWNER_COUNTER_CLUSTER_CLAIMS] ==
240 mo->mo_counter[MOWNER_COUNTER_CLUSTER_RELEASES])
241 continue;
242 snprintf(buf, sizeof(buf), "%16s %-13s",
243 mo->mo_name, mo->mo_descr);
244 if ((lines % 24) == 0 || lines > 24) {
245 printf("%30s %-8s %10s %10s %10s\n",
246 "", "", "small", "ext", "cluster");
247 lines = 1;
249 printf("%30s %-8s %10lu %10lu %10lu\n",
250 buf, "inuse",
251 mo->mo_counter[MOWNER_COUNTER_CLAIMS] -
252 mo->mo_counter[MOWNER_COUNTER_RELEASES],
253 mo->mo_counter[MOWNER_COUNTER_EXT_CLAIMS] -
254 mo->mo_counter[MOWNER_COUNTER_EXT_RELEASES],
255 mo->mo_counter[MOWNER_COUNTER_CLUSTER_CLAIMS] -
256 mo->mo_counter[MOWNER_COUNTER_CLUSTER_RELEASES]);
257 lines++;
258 if (vflag) {
259 printf("%30s %-8s %10lu %10lu %10lu\n",
260 "", "claims",
261 mo->mo_counter[MOWNER_COUNTER_CLAIMS],
262 mo->mo_counter[MOWNER_COUNTER_EXT_CLAIMS],
263 mo->mo_counter[MOWNER_COUNTER_CLUSTER_CLAIMS]);
264 printf("%30s %-8s %10lu %10lu %10lu\n",
265 "", "releases",
266 mo->mo_counter[MOWNER_COUNTER_RELEASES],
267 mo->mo_counter[MOWNER_COUNTER_EXT_RELEASES],
268 mo->mo_counter[MOWNER_COUNTER_CLUSTER_RELEASES]);
269 lines += 2;
272 free(data);