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