1 /* $NetBSD: mbuf.c,v 1.28 2008/01/17 14:53:18 yamt Exp $ */
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
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
32 #include <sys/cdefs.h>
35 static char sccsid
[] = "from: @(#)mbuf.c 8.1 (Berkeley) 6/6/93";
37 __RCSID("$NetBSD: mbuf.c,v 1.28 2008/01/17 14:53:18 yamt Exp $");
43 #include <sys/param.h>
44 #include <sys/protosw.h>
45 #include <sys/socket.h>
48 #include <sys/sysctl.h>
62 struct pool mbpool
, mclpool
;
63 struct pool_allocator mbpa
, mclpa
;
65 static struct mbtypes
{
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" },
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.
89 mbpr(mbaddr
, msizeaddr
, mclbaddr
, mbpooladdr
, mclpooladdr
)
91 u_long msizeaddr
, mclbaddr
;
92 u_long mbpooladdr
, mclpooladdr
;
94 u_long totmem
, totused
, totpct
;
100 struct mowner_user
*mo
;
103 if (nmbtypes
!= 256) {
105 "%s: unexpected change to mbstat; check source\n",
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");
122 fprintf(stderr
, "%s: mbstat: symbol not in namelist\n",
128 kread(msizeaddr
, (char *)&msize
, sizeof (msize
));
132 kread(mclbaddr
, (char *)&mclbytes
, sizeof (mclbytes
));
137 if (kread(mbaddr
, (char *)&mbstat
, sizeof (mbstat
)))
140 if (kread(mbpooladdr
, (char *)&mbpool
, sizeof (mbpool
)))
143 if (kread(mclpooladdr
, (char *)&mclpool
, sizeof (mclpool
)))
146 mbpooladdr
= (u_long
) mbpool
.pr_alloc
;
147 mclpooladdr
= (u_long
) mclpool
.pr_alloc
;
149 if (kread(mbpooladdr
, (char *)&mbpa
, sizeof (mbpa
)))
152 if (kread(mclpooladdr
, (char *)&mclpa
, sizeof (mclpa
)))
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
);
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 */
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
;
185 else if (totused
< (ULONG_MAX
/100))
186 totpct
= (totused
* 100)/totmem
;
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
);
197 printf("%lu calls to protocol drain routines\n", mbstat
.m_drain
);
205 if (sysctl(mowners_ctl
, sizeof(mowners_ctl
)/sizeof(mowners_ctl
[0]),
206 NULL
, &len
, NULL
, 0) < 0) {
209 warn("mowners: sysctl test");
212 len
+= 10 * sizeof(*mo
); /* add some slop */
215 warn("malloc(%lu)", (u_long
)len
);
219 if (sysctl(mowners_ctl
, sizeof(mowners_ctl
)/sizeof(mowners_ctl
[0]),
220 data
, &len
, NULL
, 0) < 0) {
221 warn("mowners: sysctl get");
226 for (mo
= (void *) data
, lines
= 0; len
>= sizeof(*mo
);
227 len
-= sizeof(*mo
), mo
++) {
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)
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
])
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");
249 printf("%30s %-8s %10lu %10lu %10lu\n",
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
]);
259 printf("%30s %-8s %10lu %10lu %10lu\n",
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",
266 mo
->mo_counter
[MOWNER_COUNTER_RELEASES
],
267 mo
->mo_counter
[MOWNER_COUNTER_EXT_RELEASES
],
268 mo
->mo_counter
[MOWNER_COUNTER_CLUSTER_RELEASES
]);