2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (c) 1983, 1988, 1993
5 * The Regents of the University of California.
6 * Copyright (c) 2005 Robert N. M. Watson
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
40 static char sccsid[] = "@(#)mbuf.c 8.1 (Berkeley) 6/6/93";
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
47 #include <sys/param.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>
67 * Print mbuf statistics.
70 mbpr(void *kvmd, u_long mbaddr)
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;
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;
93 mtlp = memstat_mtl_alloc();
95 xo_warn("memstat_mtl_alloc");
100 * Use memstat_*_all() because some mbuf-related memory is in uma(9),
101 * and some malloc(9).
104 if (memstat_sysctl_all(mtlp, 0) < 0) {
105 xo_warnx("memstat_sysctl_all: %s",
106 memstat_strerror(memstat_mtl_geterror(mtlp)));
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",
116 xo_warnx("memstat_kvm_all: %s",
117 memstat_strerror(error));
122 mtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, MBUF_MEM_NAME);
124 xo_warnx("memstat_mtl_find: zone %s not found", MBUF_MEM_NAME);
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);
134 mtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, MBUF_PACKET_MEM_NAME);
136 xo_warnx("memstat_mtl_find: zone %s not found",
137 MBUF_PACKET_MEM_NAME);
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);
146 mtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, MBUF_CLUSTER_MEM_NAME);
148 xo_warnx("memstat_mtl_find: zone %s not found",
149 MBUF_CLUSTER_MEM_NAME);
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);
159 mtp = memstat_mtl_find(mtlp, ALLOCATOR_MALLOC, MBUF_TAG_MEM_NAME);
161 xo_warnx("memstat_mtl_find: malloc type %s not found",
165 tag_bytes = memstat_get_bytes(mtp);
167 mtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, MBUF_JUMBOP_MEM_NAME);
169 xo_warnx("memstat_mtl_find: zone %s not found",
170 MBUF_JUMBOP_MEM_NAME);
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);
180 mtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, MBUF_JUMBO9_MEM_NAME);
182 xo_warnx("memstat_mtl_find: zone %s not found",
183 MBUF_JUMBO9_MEM_NAME);
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);
193 mtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, MBUF_JUMBO16_MEM_NAME);
195 xo_warnx("memstat_mtl_find: zone %s not found",
196 MBUF_JUMBO16_MEM_NAME);
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);
206 xo_open_container("mbuf-statistics");
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);
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);
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);
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);
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,
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,
243 xo_emit("{:tag-count/%ju} {N:mbuf tags in use}\n", tag_count);
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.
253 * This avoids counting the clusters attached to packets in the cache.
254 * This currently excludes sf_buf space.
257 mbuf_bytes + /* straight mbuf memory */
258 packet_bytes + /* mbufs in packets */
259 (packet_count * cluster_size) + /* clusters in packets */
261 ((cluster_count - packet_count - packet_free) * cluster_size) +
263 (jumbop_count * jumbop_size) + /* jumbo clusters */
264 (jumbo9_count * jumbo9_size) +
265 (jumbo16_count * jumbo16_size);
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.
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);
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.
289 bytes_total = bytes_inuse + bytes_incache;
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);
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);
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,
315 mlen = sizeof(nsfbufs);
317 sysctlbyname("kern.ipc.nsfbufs", &nsfbufs, &mlen, NULL, 0) == 0 &&
318 sysctlbyname("kern.ipc.nsfbufsused", &nsfbufsused, &mlen,
320 sysctlbyname("kern.ipc.nsfbufspeak", &nsfbufspeak, &mlen,
322 xo_emit("{:nsfbufs-current/%d}/{:nsfbufs-peak/%d}/"
324 "{N:sfbufs in use (current\\/peak\\/max)}\n",
325 nsfbufsused, nsfbufspeak, nsfbufs);
327 if (fetch_stats("kern.ipc.sfstat", mbaddr, &sfstat, sizeof(sfstat),
328 kread_counters) != 0)
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);
362 xo_close_container("mbuf-statistics");
363 memstat_mtl_free(mtlp);