2 * Copyright (c) 1983, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)mbuf.c 8.1 (Berkeley) 6/6/93
34 * $FreeBSD: src/usr.bin/netstat/mbuf.c,v 1.17.2.3 2001/08/10 09:07:09 ru Exp $
35 * $DragonFly: src/usr.bin/netstat/mbuf.c,v 1.7 2006/10/09 22:30:48 hsu Exp $
38 #define _KERNEL_STRUCTURES
39 #include <sys/param.h>
41 #include <sys/protosw.h>
42 #include <sys/socket.h>
43 #include <sys/sysctl.h>
53 static struct mbtypenames
{
58 { MT_OOBDATA
, "oob data" },
59 { MT_CONTROL
, "ancillary data" },
60 { MT_HEADER
, "packet headers" },
61 { MT_SONAME
, "socket names and addresses" },
66 * Print mbuf statistics.
69 mbpr(u_long mbaddr
, u_long mbtaddr
, u_long nmbcaddr
, u_long nmbufaddr
)
71 u_long totmem
, totpossible
;
74 struct mbtypenames
*mp
;
75 int name
[3], nmbclusters
, nmbufs
, nmbtypes
;
76 size_t nmbclen
, nmbuflen
, mbstatlen
, mbtypeslen
;
78 bool *seen
; /* "have we seen this type yet?" */
85 * We can't kread() mbtypeslen from a core image so we'll
86 * bogusly assume it's the same as in the running kernel.
88 if (sysctlbyname("kern.ipc.mbtypes", NULL
, &mbtypeslen
, NULL
, 0) < 0) {
89 warn("sysctl: retrieving mbtypes length");
92 if ((mbtypes
= malloc(mbtypeslen
)) == NULL
) {
93 warn("malloc: %lu bytes for mbtypes", (u_long
)mbtypeslen
);
97 nmbtypes
= mbtypeslen
/ sizeof(*mbtypes
);
98 if ((seen
= calloc(nmbtypes
, sizeof(*seen
))) == NULL
) {
104 if (kread(mbaddr
, (char *)&mbstat
, sizeof mbstat
))
106 if (kread(mbtaddr
, (char *)mbtypes
, mbtypeslen
))
108 if (kread(nmbcaddr
, (char *)&nmbclusters
, sizeof(int)))
110 if (kread(nmbufaddr
, (char *)&nmbufs
, sizeof(int)))
115 name
[2] = KIPC_MBSTAT
;
116 mbstatlen
= sizeof mbstat
;
117 if (sysctl(name
, 3, &mbstat
, &mbstatlen
, 0, 0) < 0) {
118 warn("sysctl: retrieving mbstat");
122 if (sysctlbyname("kern.ipc.mbtypes", mbtypes
, &mbtypeslen
, NULL
,
124 warn("sysctl: retrieving mbtypes");
128 name
[2] = KIPC_NMBCLUSTERS
;
129 nmbclen
= sizeof(int);
130 if (sysctl(name
, 3, &nmbclusters
, &nmbclen
, 0, 0) < 0) {
131 warn("sysctl: retrieving nmbclusters");
135 nmbuflen
= sizeof(int);
136 if (sysctlbyname("kern.ipc.nmbufs", &nmbufs
, &nmbuflen
, 0, 0) < 0) {
137 warn("sysctl: retrieving nmbufs");
143 #define MSIZE (mbstat.m_msize)
145 #define MCLBYTES (mbstat.m_mclbytes)
147 printf("%lu/%u mbufs in use (current/max):\n", mbstat
.m_mbufs
, nmbufs
);
148 printf("%lu/%u mbuf clusters in use (current/max)\n",
149 mbstat
.m_clusters
, nmbclusters
);
150 for (mp
= mbtypenames
; mp
->mt_name
; mp
++)
151 if (mbtypes
[mp
->mt_type
]) {
152 seen
[mp
->mt_type
] = YES
;
153 printf("\t%lu mbufs and mbuf clusters allocated to %s\n",
154 mbtypes
[mp
->mt_type
], mp
->mt_name
);
157 for (i
= 0; i
< nmbtypes
; i
++)
158 if (!seen
[i
] && mbtypes
[i
]) {
159 printf("\t%lu mbufs and mbuf clusters allocated to <mbuf type %d>\n",
162 totmem
= mbstat
.m_mbufs
* MSIZE
+ mbstat
.m_clusters
* MCLBYTES
;
163 totpossible
= nmbclusters
* MCLBYTES
+ MSIZE
* nmbufs
;
164 printf("%lu Kbytes allocated to network (%lu%% of mb_map in use)\n",
165 totmem
/ 1024, (totmem
* 100) / totpossible
);
166 printf("%lu requests for memory denied\n", mbstat
.m_drops
);
167 printf("%lu requests for memory delayed\n", mbstat
.m_wait
);
168 printf("%lu calls to protocol drain routines\n", mbstat
.m_drain
);