xref: /freebsd/usr.bin/netstat/mbuf.c (revision 732a02b4e77866604a120a275c082bb6221bd2ff)
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (c) 1983, 1988, 1993
5  *	The Regents of the University of California.
6  * Copyright (c) 2005 Robert N. M. Watson
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed by the University of
20  *	California, Berkeley and its contributors.
21  * 4. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  */
37 
38 #if 0
39 #ifndef lint
40 static char sccsid[] = "@(#)mbuf.c	8.1 (Berkeley) 6/6/93";
41 #endif /* not lint */
42 #endif
43 
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
46 
47 #include <sys/param.h>
48 #include <sys/mbuf.h>
49 #include <sys/protosw.h>
50 #include <sys/sf_buf.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
53 #include <sys/sysctl.h>
54 
55 #include <err.h>
56 #include <kvm.h>
57 #include <memstat.h>
58 #include <stdint.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <stdbool.h>
62 #include <string.h>
63 #include <libxo/xo.h>
64 #include "netstat.h"
65 
66 /*
67  * Print mbuf statistics.
68  */
69 void
70 mbpr(void *kvmd, u_long mbaddr)
71 {
72 	struct memory_type_list *mtlp;
73 	struct memory_type *mtp;
74 	uintmax_t mbuf_count, mbuf_bytes, mbuf_free, mbuf_failures, mbuf_size;
75 	uintmax_t mbuf_sleeps;
76 	uintmax_t cluster_count, cluster_limit, cluster_free;
77 	uintmax_t cluster_failures, cluster_size, cluster_sleeps;
78 	uintmax_t packet_count, packet_bytes, packet_free, packet_failures;
79 	uintmax_t packet_sleeps;
80 	uintmax_t tag_bytes;
81 	uintmax_t jumbop_count, jumbop_limit, jumbop_free;
82 	uintmax_t jumbop_failures, jumbop_sleeps, jumbop_size;
83 	uintmax_t jumbo9_count, jumbo9_limit, jumbo9_free;
84 	uintmax_t jumbo9_failures, jumbo9_sleeps, jumbo9_size;
85 	uintmax_t jumbo16_count, jumbo16_limit, jumbo16_free;
86 	uintmax_t jumbo16_failures, jumbo16_sleeps, jumbo16_size;
87 	uintmax_t bytes_inuse, bytes_incache, bytes_total;
88 	int nsfbufs, nsfbufspeak, nsfbufsused;
89 	struct sfstat sfstat;
90 	size_t mlen;
91 	int error;
92 
93 	mtlp = memstat_mtl_alloc();
94 	if (mtlp == NULL) {
95 		xo_warn("memstat_mtl_alloc");
96 		return;
97 	}
98 
99 	/*
100 	 * Use memstat_*_all() because some mbuf-related memory is in uma(9),
101 	 * and some malloc(9).
102 	 */
103 	if (live) {
104 		if (memstat_sysctl_all(mtlp, 0) < 0) {
105 			xo_warnx("memstat_sysctl_all: %s",
106 			    memstat_strerror(memstat_mtl_geterror(mtlp)));
107 			goto out;
108 		}
109 	} else {
110 		if (memstat_kvm_all(mtlp, kvmd) < 0) {
111 			error = memstat_mtl_geterror(mtlp);
112 			if (error == MEMSTAT_ERROR_KVM)
113 				xo_warnx("memstat_kvm_all: %s",
114 				    kvm_geterr(kvmd));
115 			else
116 				xo_warnx("memstat_kvm_all: %s",
117 				    memstat_strerror(error));
118 			goto out;
119 		}
120 	}
121 
122 	mtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, MBUF_MEM_NAME);
123 	if (mtp == NULL) {
124 		xo_warnx("memstat_mtl_find: zone %s not found", MBUF_MEM_NAME);
125 		goto out;
126 	}
127 	mbuf_count = memstat_get_count(mtp);
128 	mbuf_bytes = memstat_get_bytes(mtp);
129 	mbuf_free = memstat_get_free(mtp);
130 	mbuf_failures = memstat_get_failures(mtp);
131 	mbuf_sleeps = memstat_get_sleeps(mtp);
132 	mbuf_size = memstat_get_size(mtp);
133 
134 	mtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, MBUF_PACKET_MEM_NAME);
135 	if (mtp == NULL) {
136 		xo_warnx("memstat_mtl_find: zone %s not found",
137 		    MBUF_PACKET_MEM_NAME);
138 		goto out;
139 	}
140 	packet_count = memstat_get_count(mtp);
141 	packet_bytes = memstat_get_bytes(mtp);
142 	packet_free = memstat_get_free(mtp);
143 	packet_sleeps = memstat_get_sleeps(mtp);
144 	packet_failures = memstat_get_failures(mtp);
145 
146 	mtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, MBUF_CLUSTER_MEM_NAME);
147 	if (mtp == NULL) {
148 		xo_warnx("memstat_mtl_find: zone %s not found",
149 		    MBUF_CLUSTER_MEM_NAME);
150 		goto out;
151 	}
152 	cluster_count = memstat_get_count(mtp);
153 	cluster_limit = memstat_get_countlimit(mtp);
154 	cluster_free = memstat_get_free(mtp);
155 	cluster_failures = memstat_get_failures(mtp);
156 	cluster_sleeps = memstat_get_sleeps(mtp);
157 	cluster_size = memstat_get_size(mtp);
158 
159 	mtp = memstat_mtl_find(mtlp, ALLOCATOR_MALLOC, MBUF_TAG_MEM_NAME);
160 	if (mtp == NULL) {
161 		xo_warnx("memstat_mtl_find: malloc type %s not found",
162 		    MBUF_TAG_MEM_NAME);
163 		goto out;
164 	}
165 	tag_bytes = memstat_get_bytes(mtp);
166 
167 	mtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, MBUF_JUMBOP_MEM_NAME);
168 	if (mtp == NULL) {
169 		xo_warnx("memstat_mtl_find: zone %s not found",
170 		    MBUF_JUMBOP_MEM_NAME);
171 		goto out;
172 	}
173 	jumbop_count = memstat_get_count(mtp);
174 	jumbop_limit = memstat_get_countlimit(mtp);
175 	jumbop_free = memstat_get_free(mtp);
176 	jumbop_failures = memstat_get_failures(mtp);
177 	jumbop_sleeps = memstat_get_sleeps(mtp);
178 	jumbop_size = memstat_get_size(mtp);
179 
180 	mtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, MBUF_JUMBO9_MEM_NAME);
181 	if (mtp == NULL) {
182 		xo_warnx("memstat_mtl_find: zone %s not found",
183 		    MBUF_JUMBO9_MEM_NAME);
184 		goto out;
185 	}
186 	jumbo9_count = memstat_get_count(mtp);
187 	jumbo9_limit = memstat_get_countlimit(mtp);
188 	jumbo9_free = memstat_get_free(mtp);
189 	jumbo9_failures = memstat_get_failures(mtp);
190 	jumbo9_sleeps = memstat_get_sleeps(mtp);
191 	jumbo9_size = memstat_get_size(mtp);
192 
193 	mtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, MBUF_JUMBO16_MEM_NAME);
194 	if (mtp == NULL) {
195 		xo_warnx("memstat_mtl_find: zone %s not found",
196 		    MBUF_JUMBO16_MEM_NAME);
197 		goto out;
198 	}
199 	jumbo16_count = memstat_get_count(mtp);
200 	jumbo16_limit = memstat_get_countlimit(mtp);
201 	jumbo16_free = memstat_get_free(mtp);
202 	jumbo16_failures = memstat_get_failures(mtp);
203 	jumbo16_sleeps = memstat_get_sleeps(mtp);
204 	jumbo16_size = memstat_get_size(mtp);
205 
206 	xo_open_container("mbuf-statistics");
207 
208 	xo_emit("{:mbuf-current/%ju}/{:mbuf-cache/%ju}/{:mbuf-total/%ju} "
209 	    "{N:mbufs in use (current\\/cache\\/total)}\n",
210 	    mbuf_count + packet_count, mbuf_free + packet_free,
211 	    mbuf_count + packet_count + mbuf_free + packet_free);
212 
213 	xo_emit("{:cluster-current/%ju}/{:cluster-cache/%ju}/"
214 	    "{:cluster-total/%ju}/{:cluster-max/%ju} "
215 	    "{N:mbuf clusters in use (current\\/cache\\/total\\/max)}\n",
216 	    cluster_count - packet_free, cluster_free + packet_free,
217 	    cluster_count + cluster_free, cluster_limit);
218 
219 	xo_emit("{:packet-count/%ju}/{:packet-free/%ju} "
220 	    "{N:mbuf+clusters out of packet secondary zone in use "
221 	    "(current\\/cache)}\n",
222 	    packet_count, packet_free);
223 
224 	xo_emit("{:jumbo-count/%ju}/{:jumbo-cache/%ju}/{:jumbo-total/%ju}/"
225 	    "{:jumbo-max/%ju} {:jumbo-page-size/%ju}{U:k} {N:(page size)} "
226 	    "{N:jumbo clusters in use (current\\/cache\\/total\\/max)}\n",
227 	    jumbop_count, jumbop_free, jumbop_count + jumbop_free,
228 	    jumbop_limit, jumbop_size / 1024);
229 
230 	xo_emit("{:jumbo9-count/%ju}/{:jumbo9-cache/%ju}/"
231 	    "{:jumbo9-total/%ju}/{:jumbo9-max/%ju} "
232 	    "{N:9k jumbo clusters in use (current\\/cache\\/total\\/max)}\n",
233 	    jumbo9_count, jumbo9_free, jumbo9_count + jumbo9_free,
234 	    jumbo9_limit);
235 
236 	xo_emit("{:jumbo16-count/%ju}/{:jumbo16-cache/%ju}/"
237 	    "{:jumbo16-total/%ju}/{:jumbo16-limit/%ju} "
238 	    "{N:16k jumbo clusters in use (current\\/cache\\/total\\/max)}\n",
239 	    jumbo16_count, jumbo16_free, jumbo16_count + jumbo16_free,
240 	    jumbo16_limit);
241 
242 #if 0
243 	xo_emit("{:tag-count/%ju} {N:mbuf tags in use}\n", tag_count);
244 #endif
245 
246 	/*-
247 	 * Calculate in-use bytes as:
248 	 * - straight mbuf memory
249 	 * - mbuf memory in packets
250 	 * - the clusters attached to packets
251 	 * - and the rest of the non-packet-attached clusters.
252 	 * - m_tag memory
253 	 * This avoids counting the clusters attached to packets in the cache.
254 	 * This currently excludes sf_buf space.
255 	 */
256 	bytes_inuse =
257 	    mbuf_bytes +			/* straight mbuf memory */
258 	    packet_bytes +			/* mbufs in packets */
259 	    (packet_count * cluster_size) +	/* clusters in packets */
260 	    /* other clusters */
261 	    ((cluster_count - packet_count - packet_free) * cluster_size) +
262 	    tag_bytes +
263 	    (jumbop_count * jumbop_size) +	/* jumbo clusters */
264 	    (jumbo9_count * jumbo9_size) +
265 	    (jumbo16_count * jumbo16_size);
266 
267 	/*
268 	 * Calculate in-cache bytes as:
269 	 * - cached straught mbufs
270 	 * - cached packet mbufs
271 	 * - cached packet clusters
272 	 * - cached straight clusters
273 	 * This currently excludes sf_buf space.
274 	 */
275 	bytes_incache =
276 	    (mbuf_free * mbuf_size) +		/* straight free mbufs */
277 	    (packet_free * mbuf_size) +		/* mbufs in free packets */
278 	    (packet_free * cluster_size) +	/* clusters in free packets */
279 	    (cluster_free * cluster_size) +	/* free clusters */
280 	    (jumbop_free * jumbop_size) +	/* jumbo clusters */
281 	    (jumbo9_free * jumbo9_size) +
282 	    (jumbo16_free * jumbo16_size);
283 
284 	/*
285 	 * Total is bytes in use + bytes in cache.  This doesn't take into
286 	 * account various other misc data structures, overhead, etc, but
287 	 * gives the user something useful despite that.
288 	 */
289 	bytes_total = bytes_inuse + bytes_incache;
290 
291 	xo_emit("{:bytes-in-use/%ju}{U:K}/{:bytes-in-cache/%ju}{U:K}/"
292 	    "{:bytes-total/%ju}{U:K} "
293 	    "{N:bytes allocated to network (current\\/cache\\/total)}\n",
294 	    bytes_inuse / 1024, bytes_incache / 1024, bytes_total / 1024);
295 
296 	xo_emit("{:mbuf-failures/%ju}/{:cluster-failures/%ju}/"
297 	    "{:packet-failures/%ju} {N:requests for mbufs denied "
298 	    "(mbufs\\/clusters\\/mbuf+clusters)}\n",
299 	    mbuf_failures, cluster_failures, packet_failures);
300 	xo_emit("{:mbuf-sleeps/%ju}/{:cluster-sleeps/%ju}/{:packet-sleeps/%ju} "
301 	    "{N:requests for mbufs delayed "
302 	    "(mbufs\\/clusters\\/mbuf+clusters)}\n",
303 	    mbuf_sleeps, cluster_sleeps, packet_sleeps);
304 
305 	xo_emit("{:jumbop-sleeps/%ju}/{:jumbo9-sleeps/%ju}/"
306 	    "{:jumbo16-sleeps/%ju} {N:/requests for jumbo clusters delayed "
307 	    "(%juk\\/9k\\/16k)}\n",
308 	    jumbop_sleeps, jumbo9_sleeps, jumbo16_sleeps, jumbop_size / 1024);
309 	xo_emit("{:jumbop-failures/%ju}/{:jumbo9-failures/%ju}/"
310 	    "{:jumbo16-failures/%ju} {N:/requests for jumbo clusters denied "
311 	    "(%juk\\/9k\\/16k)}\n",
312 	    jumbop_failures, jumbo9_failures, jumbo16_failures,
313 	    jumbop_size / 1024);
314 
315 	mlen = sizeof(nsfbufs);
316 	if (live &&
317 	    sysctlbyname("kern.ipc.nsfbufs", &nsfbufs, &mlen, NULL, 0) == 0 &&
318 	    sysctlbyname("kern.ipc.nsfbufsused", &nsfbufsused, &mlen,
319 	    NULL, 0) == 0 &&
320 	    sysctlbyname("kern.ipc.nsfbufspeak", &nsfbufspeak, &mlen,
321 	    NULL, 0) == 0)
322 		xo_emit("{:nsfbufs-current/%d}/{:nsfbufs-peak/%d}/"
323 		    "{:nsfbufs/%d} "
324 		    "{N:sfbufs in use (current\\/peak\\/max)}\n",
325 		    nsfbufsused, nsfbufspeak, nsfbufs);
326 
327 	if (fetch_stats("kern.ipc.sfstat", mbaddr, &sfstat, sizeof(sfstat),
328 	    kread_counters) != 0)
329 		goto out;
330 
331         xo_emit("{:sendfile-syscalls/%ju} {N:sendfile syscalls}\n",
332 	    (uintmax_t)sfstat.sf_syscalls);
333         xo_emit("{:sendfile-no-io/%ju} "
334 	    "{N:sendfile syscalls completed without I\\/O request}\n",
335             (uintmax_t)sfstat.sf_noiocnt);
336 	xo_emit("{:sendfile-io-count/%ju} "
337 	    "{N:requests for I\\/O initiated by sendfile}\n",
338 	    (uintmax_t)sfstat.sf_iocnt);
339         xo_emit("{:sendfile-pages-sent/%ju} "
340 	    "{N:pages read by sendfile as part of a request}\n",
341             (uintmax_t)sfstat.sf_pages_read);
342         xo_emit("{:sendfile-pages-valid/%ju} "
343 	    "{N:pages were valid at time of a sendfile request}\n",
344             (uintmax_t)sfstat.sf_pages_valid);
345         xo_emit("{:sendfile-pages-bogus/%ju} "
346 	    "{N:pages were valid and substituted to bogus page}\n",
347             (uintmax_t)sfstat.sf_pages_bogus);
348         xo_emit("{:sendfile-requested-readahead/%ju} "
349 	    "{N:pages were requested for read ahead by applications}\n",
350             (uintmax_t)sfstat.sf_rhpages_requested);
351         xo_emit("{:sendfile-readahead/%ju} "
352 	    "{N:pages were read ahead by sendfile}\n",
353             (uintmax_t)sfstat.sf_rhpages_read);
354 	xo_emit("{:sendfile-busy-encounters/%ju} "
355 	    "{N:times sendfile encountered an already busy page}\n",
356 	    (uintmax_t)sfstat.sf_busy);
357 	xo_emit("{:sfbufs-alloc-failed/%ju} {N:requests for sfbufs denied}\n",
358 	    (uintmax_t)sfstat.sf_allocfail);
359 	xo_emit("{:sfbufs-alloc-wait/%ju} {N:requests for sfbufs delayed}\n",
360 	    (uintmax_t)sfstat.sf_allocwait);
361 out:
362 	xo_close_container("mbuf-statistics");
363 	memstat_mtl_free(mtlp);
364 }
365