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