xref: /freebsd/usr.bin/netstat/mbuf.c (revision 4f29da19bd44f0e99f021510460a81bf754c21d2)
1 /*
2  * Copyright (c) 1983, 1988, 1993
3  *	The Regents of the University of California.
4  * Copyright (c) 2005 Robert N. M. Watson
5  * 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 #if 0
37 #ifndef lint
38 static char sccsid[] = "@(#)mbuf.c	8.1 (Berkeley) 6/6/93";
39 #endif /* not lint */
40 #endif
41 
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
44 
45 #include <sys/param.h>
46 #include <sys/mbuf.h>
47 #include <sys/protosw.h>
48 #include <sys/socket.h>
49 #include <sys/sysctl.h>
50 
51 #include <err.h>
52 #include <kvm.h>
53 #include <memstat.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include "netstat.h"
58 
59 /*
60  * Print mbuf statistics.
61  */
62 void
63 mbpr(void *kvmd, u_long mbaddr)
64 {
65 	struct memory_type_list *mtlp;
66 	struct memory_type *mtp;
67 	u_int64_t mbuf_count, mbuf_bytes, mbuf_free, mbuf_failures, mbuf_size;
68 	u_int64_t cluster_count, cluster_bytes, cluster_limit, cluster_free;
69 	u_int64_t cluster_failures, cluster_size;
70 	u_int64_t packet_count, packet_bytes, packet_free, packet_failures;
71 	u_int64_t tag_count, tag_bytes;
72 	u_int64_t jumbop_count, jumbop_bytes, jumbop_limit, jumbop_free;
73 	u_int64_t jumbop_failures, jumbop_size;
74 	u_int64_t jumbo9_count, jumbo9_bytes, jumbo9_limit, jumbo9_free;
75 	u_int64_t jumbo9_failures, jumbo9_size;
76 	u_int64_t jumbo16_count, jumbo16_bytes, jumbo16_limit, jumbo16_free;
77 	u_int64_t jumbo16_failures, jumbo16_size;
78 	u_int64_t bytes_inuse, bytes_incache, bytes_total;
79 	int nsfbufs, nsfbufspeak, nsfbufsused;
80 	struct mbstat mbstat;
81 	size_t mlen;
82 	int error, live;
83 
84 	live = (kvmd == NULL);
85 	mtlp = memstat_mtl_alloc();
86 	if (mtlp == NULL) {
87 		warn("memstat_mtl_alloc");
88 		return;
89 	}
90 
91 	/*
92 	 * Use memstat_*_all() because some mbuf-related memory is in uma(9),
93 	 * and some malloc(9).
94 	 */
95 	if (live) {
96 		if (memstat_sysctl_all(mtlp, 0) < 0) {
97 			warnx("memstat_sysctl_all: %s",
98 			    memstat_strerror(memstat_mtl_geterror(mtlp)));
99 			goto out;
100 		}
101 	} else {
102 		if (memstat_kvm_all(mtlp, kvmd) < 0) {
103 			error = memstat_mtl_geterror(mtlp);
104 			if (error == MEMSTAT_ERROR_KVM)
105 				warnx("memstat_kvm_all: %s",
106 				    kvm_geterr(kvmd));
107 			else
108 				warnx("memstat_kvm_all: %s",
109 				    memstat_strerror(error));
110 			goto out;
111 		}
112 	}
113 
114 	mtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, MBUF_MEM_NAME);
115 	if (mtp == NULL) {
116 		warnx("memstat_mtl_find: zone %s not found", MBUF_MEM_NAME);
117 		goto out;
118 	}
119 	mbuf_count = memstat_get_count(mtp);
120 	mbuf_bytes = memstat_get_bytes(mtp);
121 	mbuf_free = memstat_get_free(mtp);
122 	mbuf_failures = memstat_get_failures(mtp);
123 	mbuf_size = memstat_get_size(mtp);
124 
125 	mtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, MBUF_PACKET_MEM_NAME);
126 	if (mtp == NULL) {
127 		warnx("memstat_mtl_find: zone %s not found",
128 		    MBUF_PACKET_MEM_NAME);
129 		goto out;
130 	}
131 	packet_count = memstat_get_count(mtp);
132 	packet_bytes = memstat_get_bytes(mtp);
133 	packet_free = memstat_get_free(mtp);
134 	packet_failures = memstat_get_failures(mtp);
135 
136 	mtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, MBUF_CLUSTER_MEM_NAME);
137 	if (mtp == NULL) {
138 		warnx("memstat_mtl_find: zone %s not found",
139 		    MBUF_CLUSTER_MEM_NAME);
140 		goto out;
141 	}
142 	cluster_count = memstat_get_count(mtp);
143 	cluster_bytes = memstat_get_bytes(mtp);
144 	cluster_limit = memstat_get_countlimit(mtp);
145 	cluster_free = memstat_get_free(mtp);
146 	cluster_failures = memstat_get_failures(mtp);
147 	cluster_size = memstat_get_size(mtp);
148 
149 	mtp = memstat_mtl_find(mtlp, ALLOCATOR_MALLOC, MBUF_TAG_MEM_NAME);
150 	if (mtp == NULL) {
151 		warnx("memstat_mtl_find: malloc type %s not found",
152 		    MBUF_TAG_MEM_NAME);
153 		goto out;
154 	}
155 	tag_count = memstat_get_count(mtp);
156 	tag_bytes = memstat_get_bytes(mtp);
157 
158 	mtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, MBUF_JUMBOP_MEM_NAME);
159 	if (mtp == NULL) {
160 		warnx("memstat_mtl_find: zone %s not found",
161 		    MBUF_JUMBOP_MEM_NAME);
162 		goto out;
163 	}
164 	jumbop_count = memstat_get_count(mtp);
165 	jumbop_bytes = memstat_get_bytes(mtp);
166 	jumbop_limit = memstat_get_countlimit(mtp);
167 	jumbop_free = memstat_get_free(mtp);
168 	jumbop_failures = memstat_get_failures(mtp);
169 	jumbop_size = memstat_get_size(mtp);
170 
171 	mtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, MBUF_JUMBO9_MEM_NAME);
172 	if (mtp == NULL) {
173 		warnx("memstat_mtl_find: zone %s not found",
174 		    MBUF_JUMBO9_MEM_NAME);
175 		goto out;
176 	}
177 	jumbo9_count = memstat_get_count(mtp);
178 	jumbo9_bytes = memstat_get_bytes(mtp);
179 	jumbo9_limit = memstat_get_countlimit(mtp);
180 	jumbo9_free = memstat_get_free(mtp);
181 	jumbo9_failures = memstat_get_failures(mtp);
182 	jumbo9_size = memstat_get_size(mtp);
183 
184 	mtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, MBUF_JUMBO16_MEM_NAME);
185 	if (mtp == NULL) {
186 		warnx("memstat_mtl_find: zone %s not found",
187 		    MBUF_JUMBO16_MEM_NAME);
188 		goto out;
189 	}
190 	jumbo16_count = memstat_get_count(mtp);
191 	jumbo16_bytes = memstat_get_bytes(mtp);
192 	jumbo16_limit = memstat_get_countlimit(mtp);
193 	jumbo16_free = memstat_get_free(mtp);
194 	jumbo16_failures = memstat_get_failures(mtp);
195 	jumbo16_size = memstat_get_size(mtp);
196 
197 	printf("%llu/%llu/%llu mbufs in use (current/cache/total)\n",
198 	    mbuf_count + packet_count, mbuf_free + packet_free,
199 	    mbuf_count + packet_count + mbuf_free + packet_free);
200 
201 	printf("%llu/%llu/%llu/%llu mbuf clusters in use "
202 	    "(current/cache/total/max)\n",
203 	    cluster_count - packet_free, cluster_free + packet_free,
204 	    cluster_count + cluster_free, cluster_limit);
205 
206 	printf("%llu/%llu mbuf+clusters out of packet secondary zone in use "
207 	    "(current/cache)\n",
208 	    packet_count, packet_free);
209 
210 	printf("%llu/%llu/%llu/%llu %lluk (page size) jumbo clusters in use "
211 	    "(current/cache/total/max)\n",
212 	    jumbop_count, jumbop_free, jumbop_count + jumbop_free,
213 	    jumbop_limit, jumbop_size / 1024);
214 
215 	printf("%llu/%llu/%llu/%llu 9k jumbo clusters in use "
216 	    "(current/cache/total/max)\n",
217 	    jumbo9_count, jumbo9_free, jumbo9_count + jumbo9_free,
218 	    jumbo9_limit);
219 
220 	printf("%llu/%llu/%llu/%llu 16k jumbo clusters in use "
221 	    "(current/cache/total/max)\n",
222 	    jumbo16_count, jumbo16_free, jumbo16_count + jumbo16_free,
223 	    jumbo16_limit);
224 
225 #if 0
226 	printf("%llu mbuf tags in use\n", tag_count);
227 #endif
228 
229 	/*-
230 	 * Calculate in-use bytes as:
231 	 * - straight mbuf memory
232 	 * - mbuf memory in packets
233 	 * - the clusters attached to packets
234 	 * - and the rest of the non-packet-attached clusters.
235 	 * - m_tag memory
236 	 * This avoids counting the clusters attached to packets in the cache.
237 	 * This currently excludes sf_buf space.
238 	 */
239 	bytes_inuse =
240 	    mbuf_bytes +			/* straight mbuf memory */
241 	    packet_bytes +			/* mbufs in packets */
242 	    (packet_count * cluster_size) +	/* clusters in packets */
243 	    /* other clusters */
244 	    ((cluster_count - packet_count - packet_free) * cluster_size) +
245 	    tag_bytes +
246 	    (jumbop_count * jumbop_size) +	/* jumbo clusters */
247 	    (jumbo9_count * jumbo9_size) +
248 	    (jumbo16_count * jumbo16_size);
249 
250 	/*
251 	 * Calculate in-cache bytes as:
252 	 * - cached straught mbufs
253 	 * - cached packet mbufs
254 	 * - cached packet clusters
255 	 * - cached straight clusters
256 	 * This currently excludes sf_buf space.
257 	 */
258 	bytes_incache =
259 	    (mbuf_free * mbuf_size) +		/* straight free mbufs */
260 	    (packet_free * mbuf_size) +		/* mbufs in free packets */
261 	    (packet_free * cluster_size) +	/* clusters in free packets */
262 	    (cluster_free * cluster_size) +	/* free clusters */
263 	    (jumbop_free * jumbop_size) +	/* jumbo clusters */
264 	    (jumbo9_free * jumbo9_size) +
265 	    (jumbo16_free * jumbo16_size);
266 
267 	/*
268 	 * Total is bytes in use + bytes in cache.  This doesn't take into
269 	 * account various other misc data structures, overhead, etc, but
270 	 * gives the user something useful despite that.
271 	 */
272 	bytes_total = bytes_inuse + bytes_incache;
273 
274 	printf("%lluK/%lluK/%lluK bytes allocated to network "
275 	    "(current/cache/total)\n", bytes_inuse / 1024,
276 	    bytes_incache / 1024, bytes_total / 1024);
277 
278 	printf("%llu/%llu/%llu requests for mbufs denied (mbufs/clusters/"
279 	    "mbuf+clusters)\n", mbuf_failures, cluster_failures,
280 	    packet_failures);
281 
282 	printf("%llu/%llu/%llu requests for jumbo clusters denied "
283 	    "(%lluk/9k/16k)\n", jumbop_failures, jumbo9_failures,
284 	    jumbo16_failures, jumbop_size / 1024);
285 
286 	if (live) {
287 		mlen = sizeof(nsfbufs);
288 		if (!sysctlbyname("kern.ipc.nsfbufs", &nsfbufs, &mlen, NULL,
289 		    0) &&
290 		    !sysctlbyname("kern.ipc.nsfbufsused", &nsfbufsused,
291 		    &mlen, NULL, 0) &&
292 		    !sysctlbyname("kern.ipc.nsfbufspeak", &nsfbufspeak,
293 		    &mlen, NULL, 0))
294 			printf("%d/%d/%d sfbufs in use (current/peak/max)\n",
295 			    nsfbufsused, nsfbufspeak, nsfbufs);
296 		mlen = sizeof(mbstat);
297 		if (sysctlbyname("kern.ipc.mbstat", &mbstat, &mlen, NULL, 0)) {
298 			warn("kern.ipc.mbstat");
299 			goto out;
300 		}
301 	} else {
302 		if (kread(mbaddr, (char *)&mbstat, sizeof mbstat))
303 			goto out;
304 	}
305 	printf("%lu requests for sfbufs denied\n", mbstat.sf_allocfail);
306 	printf("%lu requests for sfbufs delayed\n", mbstat.sf_allocwait);
307 	printf("%lu requests for I/O initiated by sendfile\n",
308 	    mbstat.sf_iocnt);
309 	printf("%lu calls to protocol drain routines\n", mbstat.m_drain);
310 out:
311 	memstat_mtl_free(mtlp);
312 }
313