xref: /freebsd/usr.bin/netstat/mbuf.c (revision 87bf66d4a7488c496af110d4d05cc0273d49f82e)
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 #include <sys/param.h>
39 #include <sys/mbuf.h>
40 #include <sys/protosw.h>
41 #include <sys/sf_buf.h>
42 #include <sys/socket.h>
43 #include <sys/socketvar.h>
44 #include <sys/sysctl.h>
45 
46 #include <kvm.h>
47 #include <memstat.h>
48 #include <stdint.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <stdbool.h>
52 #include <string.h>
53 #include <libxo/xo.h>
54 #include "netstat.h"
55 
56 /*
57  * Print mbuf statistics.
58  */
59 void
60 mbpr(void *kvmd, u_long mbaddr)
61 {
62 	struct memory_type_list *mtlp;
63 	struct memory_type *mtp;
64 	uintmax_t mbuf_count, mbuf_bytes, mbuf_free, mbuf_failures, mbuf_size;
65 	uintmax_t mbuf_sleeps;
66 	uintmax_t cluster_count, cluster_limit, cluster_free;
67 	uintmax_t cluster_failures, cluster_size, cluster_sleeps;
68 	uintmax_t packet_count, packet_bytes, packet_free, packet_failures;
69 	uintmax_t packet_sleeps;
70 	uintmax_t tag_bytes;
71 	uintmax_t jumbop_count, jumbop_limit, jumbop_free;
72 	uintmax_t jumbop_failures, jumbop_sleeps, jumbop_size;
73 	uintmax_t jumbo9_count, jumbo9_limit, jumbo9_free;
74 	uintmax_t jumbo9_failures, jumbo9_sleeps, jumbo9_size;
75 	uintmax_t jumbo16_count, jumbo16_limit, jumbo16_free;
76 	uintmax_t jumbo16_failures, jumbo16_sleeps, jumbo16_size;
77 	uintmax_t bytes_inuse, bytes_incache, bytes_total;
78 	int nsfbufs, nsfbufspeak, nsfbufsused;
79 	struct sfstat sfstat;
80 	size_t mlen;
81 	int error;
82 
83 	mtlp = memstat_mtl_alloc();
84 	if (mtlp == NULL) {
85 		xo_warn("memstat_mtl_alloc");
86 		return;
87 	}
88 
89 	/*
90 	 * Use memstat_*_all() because some mbuf-related memory is in uma(9),
91 	 * and some malloc(9).
92 	 */
93 	if (live) {
94 		if (memstat_sysctl_all(mtlp, 0) < 0) {
95 			xo_warnx("memstat_sysctl_all: %s",
96 			    memstat_strerror(memstat_mtl_geterror(mtlp)));
97 			goto out;
98 		}
99 	} else {
100 		if (memstat_kvm_all(mtlp, kvmd) < 0) {
101 			error = memstat_mtl_geterror(mtlp);
102 			if (error == MEMSTAT_ERROR_KVM)
103 				xo_warnx("memstat_kvm_all: %s",
104 				    kvm_geterr(kvmd));
105 			else
106 				xo_warnx("memstat_kvm_all: %s",
107 				    memstat_strerror(error));
108 			goto out;
109 		}
110 	}
111 
112 	mtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, MBUF_MEM_NAME);
113 	if (mtp == NULL) {
114 		xo_warnx("memstat_mtl_find: zone %s not found", MBUF_MEM_NAME);
115 		goto out;
116 	}
117 	mbuf_count = memstat_get_count(mtp);
118 	mbuf_bytes = memstat_get_bytes(mtp);
119 	mbuf_free = memstat_get_free(mtp);
120 	mbuf_failures = memstat_get_failures(mtp);
121 	mbuf_sleeps = memstat_get_sleeps(mtp);
122 	mbuf_size = memstat_get_size(mtp);
123 
124 	mtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, MBUF_PACKET_MEM_NAME);
125 	if (mtp == NULL) {
126 		xo_warnx("memstat_mtl_find: zone %s not found",
127 		    MBUF_PACKET_MEM_NAME);
128 		goto out;
129 	}
130 	packet_count = memstat_get_count(mtp);
131 	packet_bytes = memstat_get_bytes(mtp);
132 	packet_free = memstat_get_free(mtp);
133 	packet_sleeps = memstat_get_sleeps(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 		xo_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_limit = memstat_get_countlimit(mtp);
144 	cluster_free = memstat_get_free(mtp);
145 	cluster_failures = memstat_get_failures(mtp);
146 	cluster_sleeps = memstat_get_sleeps(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 		xo_warnx("memstat_mtl_find: malloc type %s not found",
152 		    MBUF_TAG_MEM_NAME);
153 		goto out;
154 	}
155 	tag_bytes = memstat_get_bytes(mtp);
156 
157 	mtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, MBUF_JUMBOP_MEM_NAME);
158 	if (mtp == NULL) {
159 		xo_warnx("memstat_mtl_find: zone %s not found",
160 		    MBUF_JUMBOP_MEM_NAME);
161 		goto out;
162 	}
163 	jumbop_count = memstat_get_count(mtp);
164 	jumbop_limit = memstat_get_countlimit(mtp);
165 	jumbop_free = memstat_get_free(mtp);
166 	jumbop_failures = memstat_get_failures(mtp);
167 	jumbop_sleeps = memstat_get_sleeps(mtp);
168 	jumbop_size = memstat_get_size(mtp);
169 
170 	mtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, MBUF_JUMBO9_MEM_NAME);
171 	if (mtp == NULL) {
172 		xo_warnx("memstat_mtl_find: zone %s not found",
173 		    MBUF_JUMBO9_MEM_NAME);
174 		goto out;
175 	}
176 	jumbo9_count = memstat_get_count(mtp);
177 	jumbo9_limit = memstat_get_countlimit(mtp);
178 	jumbo9_free = memstat_get_free(mtp);
179 	jumbo9_failures = memstat_get_failures(mtp);
180 	jumbo9_sleeps = memstat_get_sleeps(mtp);
181 	jumbo9_size = memstat_get_size(mtp);
182 
183 	mtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, MBUF_JUMBO16_MEM_NAME);
184 	if (mtp == NULL) {
185 		xo_warnx("memstat_mtl_find: zone %s not found",
186 		    MBUF_JUMBO16_MEM_NAME);
187 		goto out;
188 	}
189 	jumbo16_count = memstat_get_count(mtp);
190 	jumbo16_limit = memstat_get_countlimit(mtp);
191 	jumbo16_free = memstat_get_free(mtp);
192 	jumbo16_failures = memstat_get_failures(mtp);
193 	jumbo16_sleeps = memstat_get_sleeps(mtp);
194 	jumbo16_size = memstat_get_size(mtp);
195 
196 	xo_open_container("mbuf-statistics");
197 
198 	xo_emit("{:mbuf-current/%ju}/{:mbuf-cache/%ju}/{:mbuf-total/%ju} "
199 	    "{N:mbufs in use (current\\/cache\\/total)}\n",
200 	    mbuf_count + packet_count, mbuf_free + packet_free,
201 	    mbuf_count + packet_count + mbuf_free + packet_free);
202 
203 	xo_emit("{:cluster-current/%ju}/{:cluster-cache/%ju}/"
204 	    "{:cluster-total/%ju}/{:cluster-max/%ju} "
205 	    "{N:mbuf clusters in use (current\\/cache\\/total\\/max)}\n",
206 	    cluster_count - packet_free, cluster_free + packet_free,
207 	    cluster_count + cluster_free, cluster_limit);
208 
209 	xo_emit("{:packet-count/%ju}/{:packet-free/%ju} "
210 	    "{N:mbuf+clusters out of packet secondary zone in use "
211 	    "(current\\/cache)}\n",
212 	    packet_count, packet_free);
213 
214 	xo_emit("{:jumbo-count/%ju}/{:jumbo-cache/%ju}/{:jumbo-total/%ju}/"
215 	    "{:jumbo-max/%ju} {:jumbo-page-size/%ju}{U:k} {N:(page size)} "
216 	    "{N:jumbo clusters in use (current\\/cache\\/total\\/max)}\n",
217 	    jumbop_count, jumbop_free, jumbop_count + jumbop_free,
218 	    jumbop_limit, jumbop_size / 1024);
219 
220 	xo_emit("{:jumbo9-count/%ju}/{:jumbo9-cache/%ju}/"
221 	    "{:jumbo9-total/%ju}/{:jumbo9-max/%ju} "
222 	    "{N:9k jumbo clusters in use (current\\/cache\\/total\\/max)}\n",
223 	    jumbo9_count, jumbo9_free, jumbo9_count + jumbo9_free,
224 	    jumbo9_limit);
225 
226 	xo_emit("{:jumbo16-count/%ju}/{:jumbo16-cache/%ju}/"
227 	    "{:jumbo16-total/%ju}/{:jumbo16-limit/%ju} "
228 	    "{N:16k jumbo clusters in use (current\\/cache\\/total\\/max)}\n",
229 	    jumbo16_count, jumbo16_free, jumbo16_count + jumbo16_free,
230 	    jumbo16_limit);
231 
232 #if 0
233 	xo_emit("{:tag-count/%ju} {N:mbuf tags in use}\n", tag_count);
234 #endif
235 
236 	/*-
237 	 * Calculate in-use bytes as:
238 	 * - straight mbuf memory
239 	 * - mbuf memory in packets
240 	 * - the clusters attached to packets
241 	 * - and the rest of the non-packet-attached clusters.
242 	 * - m_tag memory
243 	 * This avoids counting the clusters attached to packets in the cache.
244 	 * This currently excludes sf_buf space.
245 	 */
246 	bytes_inuse =
247 	    mbuf_bytes +			/* straight mbuf memory */
248 	    packet_bytes +			/* mbufs in packets */
249 	    (packet_count * cluster_size) +	/* clusters in packets */
250 	    /* other clusters */
251 	    ((cluster_count - packet_count - packet_free) * cluster_size) +
252 	    tag_bytes +
253 	    (jumbop_count * jumbop_size) +	/* jumbo clusters */
254 	    (jumbo9_count * jumbo9_size) +
255 	    (jumbo16_count * jumbo16_size);
256 
257 	/*
258 	 * Calculate in-cache bytes as:
259 	 * - cached straught mbufs
260 	 * - cached packet mbufs
261 	 * - cached packet clusters
262 	 * - cached straight clusters
263 	 * This currently excludes sf_buf space.
264 	 */
265 	bytes_incache =
266 	    (mbuf_free * mbuf_size) +		/* straight free mbufs */
267 	    (packet_free * mbuf_size) +		/* mbufs in free packets */
268 	    (packet_free * cluster_size) +	/* clusters in free packets */
269 	    (cluster_free * cluster_size) +	/* free clusters */
270 	    (jumbop_free * jumbop_size) +	/* jumbo clusters */
271 	    (jumbo9_free * jumbo9_size) +
272 	    (jumbo16_free * jumbo16_size);
273 
274 	/*
275 	 * Total is bytes in use + bytes in cache.  This doesn't take into
276 	 * account various other misc data structures, overhead, etc, but
277 	 * gives the user something useful despite that.
278 	 */
279 	bytes_total = bytes_inuse + bytes_incache;
280 
281 	xo_emit("{:bytes-in-use/%ju}{U:K}/{:bytes-in-cache/%ju}{U:K}/"
282 	    "{:bytes-total/%ju}{U:K} "
283 	    "{N:bytes allocated to network (current\\/cache\\/total)}\n",
284 	    bytes_inuse / 1024, bytes_incache / 1024, bytes_total / 1024);
285 
286 	xo_emit("{:mbuf-failures/%ju}/{:cluster-failures/%ju}/"
287 	    "{:packet-failures/%ju} {N:requests for mbufs denied "
288 	    "(mbufs\\/clusters\\/mbuf+clusters)}\n",
289 	    mbuf_failures, cluster_failures, packet_failures);
290 	xo_emit("{:mbuf-sleeps/%ju}/{:cluster-sleeps/%ju}/{:packet-sleeps/%ju} "
291 	    "{N:requests for mbufs delayed "
292 	    "(mbufs\\/clusters\\/mbuf+clusters)}\n",
293 	    mbuf_sleeps, cluster_sleeps, packet_sleeps);
294 
295 	xo_emit("{:jumbop-sleeps/%ju}/{:jumbo9-sleeps/%ju}/"
296 	    "{:jumbo16-sleeps/%ju} {N:/requests for jumbo clusters delayed "
297 	    "(%juk\\/9k\\/16k)}\n",
298 	    jumbop_sleeps, jumbo9_sleeps, jumbo16_sleeps, jumbop_size / 1024);
299 	xo_emit("{:jumbop-failures/%ju}/{:jumbo9-failures/%ju}/"
300 	    "{:jumbo16-failures/%ju} {N:/requests for jumbo clusters denied "
301 	    "(%juk\\/9k\\/16k)}\n",
302 	    jumbop_failures, jumbo9_failures, jumbo16_failures,
303 	    jumbop_size / 1024);
304 
305 	mlen = sizeof(nsfbufs);
306 	if (live &&
307 	    sysctlbyname("kern.ipc.nsfbufs", &nsfbufs, &mlen, NULL, 0) == 0 &&
308 	    sysctlbyname("kern.ipc.nsfbufsused", &nsfbufsused, &mlen,
309 	    NULL, 0) == 0 &&
310 	    sysctlbyname("kern.ipc.nsfbufspeak", &nsfbufspeak, &mlen,
311 	    NULL, 0) == 0)
312 		xo_emit("{:nsfbufs-current/%d}/{:nsfbufs-peak/%d}/"
313 		    "{:nsfbufs/%d} "
314 		    "{N:sfbufs in use (current\\/peak\\/max)}\n",
315 		    nsfbufsused, nsfbufspeak, nsfbufs);
316 
317 	if (fetch_stats("kern.ipc.sfstat", mbaddr, &sfstat, sizeof(sfstat),
318 	    kread_counters) != 0)
319 		goto out;
320 
321         xo_emit("{:sendfile-syscalls/%ju} {N:sendfile syscalls}\n",
322 	    (uintmax_t)sfstat.sf_syscalls);
323         xo_emit("{:sendfile-no-io/%ju} "
324 	    "{N:sendfile syscalls completed without I\\/O request}\n",
325             (uintmax_t)sfstat.sf_noiocnt);
326 	xo_emit("{:sendfile-io-count/%ju} "
327 	    "{N:requests for I\\/O initiated by sendfile}\n",
328 	    (uintmax_t)sfstat.sf_iocnt);
329         xo_emit("{:sendfile-pages-sent/%ju} "
330 	    "{N:pages read by sendfile as part of a request}\n",
331             (uintmax_t)sfstat.sf_pages_read);
332         xo_emit("{:sendfile-pages-valid/%ju} "
333 	    "{N:pages were valid at time of a sendfile request}\n",
334             (uintmax_t)sfstat.sf_pages_valid);
335         xo_emit("{:sendfile-pages-bogus/%ju} "
336 	    "{N:pages were valid and substituted to bogus page}\n",
337             (uintmax_t)sfstat.sf_pages_bogus);
338         xo_emit("{:sendfile-requested-readahead/%ju} "
339 	    "{N:pages were requested for read ahead by applications}\n",
340             (uintmax_t)sfstat.sf_rhpages_requested);
341         xo_emit("{:sendfile-readahead/%ju} "
342 	    "{N:pages were read ahead by sendfile}\n",
343             (uintmax_t)sfstat.sf_rhpages_read);
344 	xo_emit("{:sendfile-busy-encounters/%ju} "
345 	    "{N:times sendfile encountered an already busy page}\n",
346 	    (uintmax_t)sfstat.sf_busy);
347 	xo_emit("{:sfbufs-alloc-failed/%ju} {N:requests for sfbufs denied}\n",
348 	    (uintmax_t)sfstat.sf_allocfail);
349 	xo_emit("{:sfbufs-alloc-wait/%ju} {N:requests for sfbufs delayed}\n",
350 	    (uintmax_t)sfstat.sf_allocwait);
351 out:
352 	xo_close_container("mbuf-statistics");
353 	memstat_mtl_free(mtlp);
354 }
355