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 #include <sys/param.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>
65 * Print mbuf statistics.
68 mbpr(void *kvmd, u_long mbaddr)
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;
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;
91 mtlp = memstat_mtl_alloc();
93 xo_warn("memstat_mtl_alloc");
98 * Use memstat_*_all() because some mbuf-related memory is in uma(9),
102 if (memstat_sysctl_all(mtlp, 0) < 0) {
103 xo_warnx("memstat_sysctl_all: %s",
104 memstat_strerror(memstat_mtl_geterror(mtlp)));
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",
114 xo_warnx("memstat_kvm_all: %s",
115 memstat_strerror(error));
120 mtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, MBUF_MEM_NAME);
122 xo_warnx("memstat_mtl_find: zone %s not found", MBUF_MEM_NAME);
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);
132 mtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, MBUF_PACKET_MEM_NAME);
134 xo_warnx("memstat_mtl_find: zone %s not found",
135 MBUF_PACKET_MEM_NAME);
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);
144 mtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, MBUF_CLUSTER_MEM_NAME);
146 xo_warnx("memstat_mtl_find: zone %s not found",
147 MBUF_CLUSTER_MEM_NAME);
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);
157 mtp = memstat_mtl_find(mtlp, ALLOCATOR_MALLOC, MBUF_TAG_MEM_NAME);
159 xo_warnx("memstat_mtl_find: malloc type %s not found",
163 tag_bytes = memstat_get_bytes(mtp);
165 mtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, MBUF_JUMBOP_MEM_NAME);
167 xo_warnx("memstat_mtl_find: zone %s not found",
168 MBUF_JUMBOP_MEM_NAME);
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);
178 mtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, MBUF_JUMBO9_MEM_NAME);
180 xo_warnx("memstat_mtl_find: zone %s not found",
181 MBUF_JUMBO9_MEM_NAME);
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);
191 mtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, MBUF_JUMBO16_MEM_NAME);
193 xo_warnx("memstat_mtl_find: zone %s not found",
194 MBUF_JUMBO16_MEM_NAME);
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);
204 xo_open_container("mbuf-statistics");
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);
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);
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);
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);
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,
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,
241 xo_emit("{:tag-count/%ju} {N:mbuf tags in use}\n", tag_count);
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.
251 * This avoids counting the clusters attached to packets in the cache.
252 * This currently excludes sf_buf space.
255 mbuf_bytes + /* straight mbuf memory */
256 packet_bytes + /* mbufs in packets */
257 (packet_count * cluster_size) + /* clusters in packets */
259 ((cluster_count - packet_count - packet_free) * cluster_size) +
261 (jumbop_count * jumbop_size) + /* jumbo clusters */
262 (jumbo9_count * jumbo9_size) +
263 (jumbo16_count * jumbo16_size);
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.
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);
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.
287 bytes_total = bytes_inuse + bytes_incache;
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);
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);
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,
313 mlen = sizeof(nsfbufs);
315 sysctlbyname("kern.ipc.nsfbufs", &nsfbufs, &mlen, NULL, 0) == 0 &&
316 sysctlbyname("kern.ipc.nsfbufsused", &nsfbufsused, &mlen,
318 sysctlbyname("kern.ipc.nsfbufspeak", &nsfbufspeak, &mlen,
320 xo_emit("{:nsfbufs-current/%d}/{:nsfbufs-peak/%d}/"
322 "{N:sfbufs in use (current\\/peak\\/max)}\n",
323 nsfbufsused, nsfbufspeak, nsfbufs);
325 if (fetch_stats("kern.ipc.sfstat", mbaddr, &sfstat, sizeof(sfstat),
326 kread_counters) != 0)
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-pages-bogus/%ju} "
344 "{N:pages were valid and substituted to bogus page}\n",
345 (uintmax_t)sfstat.sf_pages_bogus);
346 xo_emit("{:sendfile-requested-readahead/%ju} "
347 "{N:pages were requested for read ahead by applications}\n",
348 (uintmax_t)sfstat.sf_rhpages_requested);
349 xo_emit("{:sendfile-readahead/%ju} "
350 "{N:pages were read ahead by sendfile}\n",
351 (uintmax_t)sfstat.sf_rhpages_read);
352 xo_emit("{:sendfile-busy-encounters/%ju} "
353 "{N:times sendfile encountered an already busy page}\n",
354 (uintmax_t)sfstat.sf_busy);
355 xo_emit("{:sfbufs-alloc-failed/%ju} {N:requests for sfbufs denied}\n",
356 (uintmax_t)sfstat.sf_allocfail);
357 xo_emit("{:sfbufs-alloc-wait/%ju} {N:requests for sfbufs delayed}\n",
358 (uintmax_t)sfstat.sf_allocwait);
360 xo_close_container("mbuf-statistics");
361 memstat_mtl_free(mtlp);