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