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