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/socket.h> 49 #include <sys/socketvar.h> 50 #include <sys/sysctl.h> 51 52 #include <err.h> 53 #include <kvm.h> 54 #include <memstat.h> 55 #include <stdint.h> 56 #include <stdio.h> 57 #include <stdlib.h> 58 #include <string.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_bytes, 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_count, tag_bytes; 76 uintmax_t jumbop_count, jumbop_bytes, jumbop_limit, jumbop_free; 77 uintmax_t jumbop_failures, jumbop_sleeps, jumbop_size; 78 uintmax_t jumbo9_count, jumbo9_bytes, jumbo9_limit, jumbo9_free; 79 uintmax_t jumbo9_failures, jumbo9_sleeps, jumbo9_size; 80 uintmax_t jumbo16_count, jumbo16_bytes, 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 mbstat mbstat; 85 size_t mlen; 86 int error; 87 88 mtlp = memstat_mtl_alloc(); 89 if (mtlp == NULL) { 90 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 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 warnx("memstat_kvm_all: %s", 109 kvm_geterr(kvmd)); 110 else 111 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 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 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 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_bytes = memstat_get_bytes(mtp); 149 cluster_limit = memstat_get_countlimit(mtp); 150 cluster_free = memstat_get_free(mtp); 151 cluster_failures = memstat_get_failures(mtp); 152 cluster_sleeps = memstat_get_sleeps(mtp); 153 cluster_size = memstat_get_size(mtp); 154 155 mtp = memstat_mtl_find(mtlp, ALLOCATOR_MALLOC, MBUF_TAG_MEM_NAME); 156 if (mtp == NULL) { 157 warnx("memstat_mtl_find: malloc type %s not found", 158 MBUF_TAG_MEM_NAME); 159 goto out; 160 } 161 tag_count = memstat_get_count(mtp); 162 tag_bytes = memstat_get_bytes(mtp); 163 164 mtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, MBUF_JUMBOP_MEM_NAME); 165 if (mtp == NULL) { 166 warnx("memstat_mtl_find: zone %s not found", 167 MBUF_JUMBOP_MEM_NAME); 168 goto out; 169 } 170 jumbop_count = memstat_get_count(mtp); 171 jumbop_bytes = memstat_get_bytes(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 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_bytes = memstat_get_bytes(mtp); 186 jumbo9_limit = memstat_get_countlimit(mtp); 187 jumbo9_free = memstat_get_free(mtp); 188 jumbo9_failures = memstat_get_failures(mtp); 189 jumbo9_sleeps = memstat_get_sleeps(mtp); 190 jumbo9_size = memstat_get_size(mtp); 191 192 mtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, MBUF_JUMBO16_MEM_NAME); 193 if (mtp == NULL) { 194 warnx("memstat_mtl_find: zone %s not found", 195 MBUF_JUMBO16_MEM_NAME); 196 goto out; 197 } 198 jumbo16_count = memstat_get_count(mtp); 199 jumbo16_bytes = memstat_get_bytes(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); 205 206 printf("%ju/%ju/%ju mbufs in use (current/cache/total)\n", 207 mbuf_count + packet_count, mbuf_free + packet_free, 208 mbuf_count + packet_count + mbuf_free + packet_free); 209 210 printf("%ju/%ju/%ju/%ju mbuf clusters in use " 211 "(current/cache/total/max)\n", 212 cluster_count - packet_free, cluster_free + packet_free, 213 cluster_count + cluster_free, cluster_limit); 214 215 printf("%ju/%ju mbuf+clusters out of packet secondary zone in use " 216 "(current/cache)\n", 217 packet_count, packet_free); 218 219 printf("%ju/%ju/%ju/%ju %juk (page size) jumbo clusters in use " 220 "(current/cache/total/max)\n", 221 jumbop_count, jumbop_free, jumbop_count + jumbop_free, 222 jumbop_limit, jumbop_size / 1024); 223 224 printf("%ju/%ju/%ju/%ju 9k jumbo clusters in use " 225 "(current/cache/total/max)\n", 226 jumbo9_count, jumbo9_free, jumbo9_count + jumbo9_free, 227 jumbo9_limit); 228 229 printf("%ju/%ju/%ju/%ju 16k jumbo clusters in use " 230 "(current/cache/total/max)\n", 231 jumbo16_count, jumbo16_free, jumbo16_count + jumbo16_free, 232 jumbo16_limit); 233 234 #if 0 235 printf("%ju mbuf tags in use\n", tag_count); 236 #endif 237 238 /*- 239 * Calculate in-use bytes as: 240 * - straight mbuf memory 241 * - mbuf memory in packets 242 * - the clusters attached to packets 243 * - and the rest of the non-packet-attached clusters. 244 * - m_tag memory 245 * This avoids counting the clusters attached to packets in the cache. 246 * This currently excludes sf_buf space. 247 */ 248 bytes_inuse = 249 mbuf_bytes + /* straight mbuf memory */ 250 packet_bytes + /* mbufs in packets */ 251 (packet_count * cluster_size) + /* clusters in packets */ 252 /* other clusters */ 253 ((cluster_count - packet_count - packet_free) * cluster_size) + 254 tag_bytes + 255 (jumbop_count * jumbop_size) + /* jumbo clusters */ 256 (jumbo9_count * jumbo9_size) + 257 (jumbo16_count * jumbo16_size); 258 259 /* 260 * Calculate in-cache bytes as: 261 * - cached straught mbufs 262 * - cached packet mbufs 263 * - cached packet clusters 264 * - cached straight clusters 265 * This currently excludes sf_buf space. 266 */ 267 bytes_incache = 268 (mbuf_free * mbuf_size) + /* straight free mbufs */ 269 (packet_free * mbuf_size) + /* mbufs in free packets */ 270 (packet_free * cluster_size) + /* clusters in free packets */ 271 (cluster_free * cluster_size) + /* free clusters */ 272 (jumbop_free * jumbop_size) + /* jumbo clusters */ 273 (jumbo9_free * jumbo9_size) + 274 (jumbo16_free * jumbo16_size); 275 276 /* 277 * Total is bytes in use + bytes in cache. This doesn't take into 278 * account various other misc data structures, overhead, etc, but 279 * gives the user something useful despite that. 280 */ 281 bytes_total = bytes_inuse + bytes_incache; 282 283 printf("%juK/%juK/%juK bytes allocated to network " 284 "(current/cache/total)\n", bytes_inuse / 1024, 285 bytes_incache / 1024, bytes_total / 1024); 286 287 printf("%ju/%ju/%ju requests for mbufs denied (mbufs/clusters/" 288 "mbuf+clusters)\n", mbuf_failures, cluster_failures, 289 packet_failures); 290 printf("%ju/%ju/%ju requests for mbufs delayed (mbufs/clusters/" 291 "mbuf+clusters)\n", mbuf_sleeps, cluster_sleeps, 292 packet_sleeps); 293 294 printf("%ju/%ju/%ju requests for jumbo clusters delayed " 295 "(%juk/9k/16k)\n", jumbop_sleeps, jumbo9_sleeps, 296 jumbo16_sleeps, jumbop_size / 1024); 297 printf("%ju/%ju/%ju requests for jumbo clusters denied " 298 "(%juk/9k/16k)\n", jumbop_failures, jumbo9_failures, 299 jumbo16_failures, jumbop_size / 1024); 300 301 if (live) { 302 mlen = sizeof(nsfbufs); 303 if (!sysctlbyname("kern.ipc.nsfbufs", &nsfbufs, &mlen, NULL, 304 0) && 305 !sysctlbyname("kern.ipc.nsfbufsused", &nsfbufsused, 306 &mlen, NULL, 0) && 307 !sysctlbyname("kern.ipc.nsfbufspeak", &nsfbufspeak, 308 &mlen, NULL, 0)) 309 printf("%d/%d/%d sfbufs in use (current/peak/max)\n", 310 nsfbufsused, nsfbufspeak, nsfbufs); 311 mlen = sizeof(mbstat); 312 if (sysctlbyname("kern.ipc.mbstat", &mbstat, &mlen, NULL, 0)) { 313 warn("kern.ipc.mbstat"); 314 goto out; 315 } 316 } else { 317 if (kread(mbaddr, (char *)&mbstat, sizeof mbstat) != 0) 318 goto out; 319 } 320 printf("%lu requests for sfbufs denied\n", mbstat.sf_allocfail); 321 printf("%lu requests for sfbufs delayed\n", mbstat.sf_allocwait); 322 printf("%lu requests for I/O initiated by sendfile\n", 323 mbstat.sf_iocnt); 324 printf("%lu calls to protocol drain routines\n", mbstat.m_drain); 325 out: 326 memstat_mtl_free(mtlp); 327 } 328