1 /* 2 * Copyright (c) 1983, 1988, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #ifndef lint 35 #if 0 36 static char sccsid[] = "@(#)mbuf.c 8.1 (Berkeley) 6/6/93"; 37 #endif 38 static const char rcsid[] = 39 "$FreeBSD$"; 40 #endif /* not lint */ 41 42 #include <sys/param.h> 43 #include <sys/mbuf.h> 44 #include <sys/protosw.h> 45 #include <sys/socket.h> 46 #include <sys/sysctl.h> 47 48 #include <err.h> 49 #include <stdio.h> 50 #include <stdlib.h> 51 #include <string.h> 52 #include "netstat.h" 53 54 #define YES 1 55 typedef int bool; 56 57 static struct mbtypenames { 58 short mt_type; 59 const char *mt_name; 60 } mbtypenames[] = { 61 { MT_DATA, "data" }, 62 { MT_OOBDATA, "oob data" }, 63 { MT_CONTROL, "ancillary data" }, 64 { MT_HEADER, "packet headers" }, 65 #ifdef MT_SOCKET 66 { MT_SOCKET, "socket structures" }, /* XXX */ 67 #endif 68 #ifdef MT_PCB 69 { MT_PCB, "protocol control blocks" }, /* XXX */ 70 #endif 71 #ifdef MT_RTABLE 72 { MT_RTABLE, "routing table entries" }, /* XXX */ 73 #endif 74 #ifdef MT_HTABLE 75 { MT_HTABLE, "IMP host table entries" }, /* XXX */ 76 #endif 77 #ifdef MT_ATABLE 78 { MT_ATABLE, "address resolution tables" }, 79 #endif 80 { MT_FTABLE, "fragment reassembly queue headers" }, /* XXX */ 81 { MT_SONAME, "socket names and addresses" }, 82 #ifdef MT_SOOPTS 83 { MT_SOOPTS, "socket options" }, 84 #endif 85 #ifdef MT_RIGHTS 86 { MT_RIGHTS, "access rights" }, 87 #endif 88 #ifdef MT_IFADDR 89 { MT_IFADDR, "interface addresses" }, /* XXX */ 90 #endif 91 { 0, 0 } 92 }; 93 94 /* 95 * Print mbuf statistics. 96 */ 97 void 98 mbpr(u_long mbaddr, u_long mbtaddr __unused, u_long nmbcaddr, u_long nmbufaddr, 99 u_long mbhiaddr, u_long clhiaddr, u_long mbloaddr, u_long clloaddr, 100 u_long cpusaddr __unused, u_long pgsaddr, u_long mbpaddr) 101 { 102 int i, j, nmbufs, nmbclusters, page_size, num_objs; 103 u_int mbuf_hiwm, clust_hiwm, mbuf_lowm, clust_lowm; 104 u_long totspace[2], totused[2], totnum, totfree; 105 short nmbtypes; 106 size_t mlen; 107 long *mbtypes = NULL; 108 struct mbstat *mbstat = NULL; 109 struct mbpstat **mbpstat = NULL; 110 struct mbtypenames *mp; 111 bool *seen = NULL; 112 113 mlen = sizeof *mbstat; 114 if ((mbstat = malloc(mlen)) == NULL) { 115 warn("malloc: cannot allocate memory for mbstat"); 116 goto err; 117 } 118 119 /* 120 * XXX: Unfortunately, for the time being, we have to fetch 121 * the total length of the per-CPU stats area via sysctl 122 * (regardless of whether we're looking at a core or not. 123 */ 124 if (sysctlbyname("kern.ipc.mb_statpcpu", NULL, &mlen, NULL, 0) < 0) { 125 warn("sysctl: retrieving mb_statpcpu len"); 126 goto err; 127 } 128 num_objs = (int)(mlen / sizeof(struct mbpstat)); 129 if ((mbpstat = calloc(num_objs, sizeof(struct mbpstat *))) == NULL) { 130 warn("calloc: cannot allocate memory for mbpstats pointers"); 131 goto err; 132 } 133 if ((mbpstat[0] = calloc(num_objs, sizeof(struct mbpstat))) == NULL) { 134 warn("calloc: cannot allocate memory for mbpstats"); 135 goto err; 136 } 137 138 if (mbaddr) { 139 if (kread(mbpaddr, (char *)mbpstat[0], mlen)) 140 goto err; 141 if (kread(mbaddr, (char *)mbstat, sizeof mbstat)) 142 goto err; 143 if (kread(nmbcaddr, (char *)&nmbclusters, sizeof(int))) 144 goto err; 145 if (kread(nmbufaddr, (char *)&nmbufs, sizeof(int))) 146 goto err; 147 if (kread(mbhiaddr, (char *)&mbuf_hiwm, sizeof(u_int))) 148 goto err; 149 if (kread(clhiaddr, (char *)&clust_hiwm, sizeof(u_int))) 150 goto err; 151 if (kread(mbloaddr, (char *)&mbuf_lowm, sizeof(u_int))) 152 goto err; 153 if (kread(clloaddr, (char *)&clust_lowm, sizeof(u_int))) 154 goto err; 155 if (kread(pgsaddr, (char *)&page_size, sizeof(int))) 156 goto err; 157 } else { 158 if (sysctlbyname("kern.ipc.mb_statpcpu", mbpstat[0], &mlen, 159 NULL, 0) < 0) { 160 warn("sysctl: retrieving mb_statpcpu"); 161 goto err; 162 } 163 mlen = sizeof *mbstat; 164 if (sysctlbyname("kern.ipc.mbstat", mbstat, &mlen, NULL, 0) 165 < 0) { 166 warn("sysctl: retrieving mbstat"); 167 goto err; 168 } 169 mlen = sizeof(int); 170 if (sysctlbyname("kern.ipc.nmbclusters", &nmbclusters, &mlen, 171 NULL, 0) < 0) { 172 warn("sysctl: retrieving nmbclusters"); 173 goto err; 174 } 175 mlen = sizeof(int); 176 if (sysctlbyname("kern.ipc.nmbufs", &nmbufs, &mlen, NULL, 0) 177 < 0) { 178 warn("sysctl: retrieving nmbufs"); 179 goto err; 180 } 181 mlen = sizeof(u_int); 182 if (sysctlbyname("kern.ipc.mbuf_hiwm", &mbuf_hiwm, &mlen, 183 NULL, 0) < 0) { 184 warn("sysctl: retrieving mbuf_hiwm"); 185 goto err; 186 } 187 mlen = sizeof(u_int); 188 if (sysctlbyname("kern.ipc.clust_hiwm", &clust_hiwm, &mlen, 189 NULL, 0) < 0) { 190 warn("sysctl: retrieving clust_hiwm"); 191 goto err; 192 } 193 mlen = sizeof(u_int); 194 if (sysctlbyname("kern.ipc.mbuf_lowm", &mbuf_lowm, &mlen, 195 NULL, 0) < 0) { 196 warn("sysctl: retrieving mbuf_lowm"); 197 goto err; 198 } 199 mlen = sizeof(u_int); 200 if (sysctlbyname("kern.ipc.clust_lowm", &clust_lowm, &mlen, 201 NULL, 0) < 0) { 202 warn("sysctl: retrieving clust_lowm"); 203 goto err; 204 } 205 mlen = sizeof(int); 206 if (sysctlbyname("hw.pagesize", &page_size, &mlen, NULL, 0) 207 < 0) { 208 warn("sysctl: retrieving hw.pagesize"); 209 goto err; 210 } 211 } 212 213 nmbtypes = mbstat->m_numtypes; 214 if ((seen = calloc(nmbtypes, sizeof(*seen))) == NULL) { 215 warn("calloc: cannot allocate memory for mbtypes seen flag"); 216 goto err; 217 } 218 if ((mbtypes = calloc(nmbtypes, sizeof(long *))) == NULL) { 219 warn("calloc: cannot allocate memory for mbtypes"); 220 goto err; 221 } 222 223 for (i = 0; i < num_objs; i++) 224 mbpstat[i] = mbpstat[0] + i; 225 226 #undef MSIZE 227 #define MSIZE (mbstat->m_msize) 228 #undef MCLBYTES 229 #define MCLBYTES (mbstat->m_mclbytes) 230 #define GENLST (num_objs - 1) 231 232 printf("mbuf usage:\n"); 233 totnum = mbpstat[GENLST]->mb_mbbucks * mbstat->m_mbperbuck; 234 totfree = mbpstat[GENLST]->mb_mbfree; 235 printf("\tGEN cache:\t%lu/%lu (in use/in pool)\n", 236 totnum - totfree, totnum); 237 for (j = 1; j < nmbtypes; j++) 238 mbtypes[j] += mbpstat[GENLST]->mb_mbtypes[j]; 239 totspace[0] = mbpstat[GENLST]->mb_mbbucks * mbstat->m_mbperbuck * MSIZE; 240 for (i = 0; i < (num_objs - 1); i++) { 241 if (mbpstat[i]->mb_active == 0) 242 continue; 243 printf("\tCPU #%d cache:\t%lu/%lu (in use/in pool)\n", i, 244 (mbpstat[i]->mb_mbbucks * mbstat->m_mbperbuck - 245 mbpstat[i]->mb_mbfree), 246 (mbpstat[i]->mb_mbbucks * mbstat->m_mbperbuck)); 247 totspace[0] += mbpstat[i]->mb_mbbucks*mbstat->m_mbperbuck*MSIZE; 248 totnum += mbpstat[i]->mb_mbbucks * mbstat->m_mbperbuck; 249 totfree += mbpstat[i]->mb_mbfree; 250 for (j = 1; j < nmbtypes; j++) 251 mbtypes[j] += mbpstat[i]->mb_mbtypes[j]; 252 } 253 totused[0] = totnum - totfree; 254 printf("\tTotal:\t\t%lu/%lu (in use/in pool)\n", totused[0], totnum); 255 printf("\tMbuf cache high watermark: %d\n", mbuf_hiwm); 256 printf("\tMbuf cache low watermark: %d\n", mbuf_lowm); 257 printf("\tMaximum possible: %d\n", nmbufs); 258 printf("\tAllocated mbuf types:\n"); 259 for (mp = mbtypenames; mp->mt_name; mp++) { 260 if (mbtypes[mp->mt_type]) { 261 seen[mp->mt_type] = YES; 262 printf("\t %lu mbufs allocated to %s\n", 263 mbtypes[mp->mt_type], mp->mt_name); 264 } 265 } 266 for (i = 1; i < nmbtypes; i++) { 267 if (!seen[i] && mbtypes[i]) 268 printf("\t %lu mbufs allocated to <mbuf type: %d>\n", 269 mbtypes[i], i); 270 } 271 printf("\t%lu%% of mbuf map consumed\n", ((totspace[0] * 100) / (nmbufs 272 * MSIZE))); 273 274 printf("mbuf cluster usage:\n"); 275 totnum = mbpstat[GENLST]->mb_clbucks * mbstat->m_clperbuck; 276 totfree = mbpstat[GENLST]->mb_clfree; 277 printf("\tGEN cache:\t%lu/%lu (in use/in pool)\n", 278 totnum - totfree, totnum); 279 totspace[1] = mbpstat[GENLST]->mb_clbucks*mbstat->m_clperbuck*MCLBYTES; 280 for (i = 0; i < (num_objs - 1); i++) { 281 if (mbpstat[i]->mb_active == 0) 282 continue; 283 printf("\tCPU #%d cache:\t%lu/%lu (in use/in pool)\n", i, 284 (mbpstat[i]->mb_clbucks * mbstat->m_clperbuck - 285 mbpstat[i]->mb_clfree), 286 (mbpstat[i]->mb_clbucks * mbstat->m_clperbuck)); 287 totspace[1] += mbpstat[i]->mb_clbucks * mbstat->m_clperbuck 288 * MCLBYTES; 289 totnum += mbpstat[i]->mb_clbucks * mbstat->m_clperbuck; 290 totfree += mbpstat[i]->mb_clfree; 291 } 292 totused[1] = totnum - totfree; 293 printf("\tTotal:\t\t%lu/%lu (in use/in pool)\n", totused[1], totnum); 294 printf("\tCluster cache high watermark: %d\n", clust_hiwm); 295 printf("\tCluster cache low watermark: %d\n", clust_lowm); 296 printf("\tMaximum possible: %d\n", nmbclusters); 297 printf("\t%lu%% of cluster map consumed\n", ((totspace[1] * 100) / 298 (nmbclusters * MCLBYTES))); 299 300 printf("%lu KBytes of wired memory reserved (%lu%% in use)\n", 301 (totspace[0] + totspace[1]) / 1024, ((totused[0] * MSIZE + 302 totused[1] * MCLBYTES) * 100) / (totspace[0] + totspace[1])); 303 printf("%lu requests for memory denied\n", mbstat->m_drops); 304 printf("%lu requests for memory delayed\n", mbstat->m_wait); 305 printf("%lu calls to protocol drain routines\n", mbstat->m_drain); 306 307 err: 308 if (mbtypes != NULL) 309 free(mbtypes); 310 if (seen != NULL) 311 free(seen); 312 if (mbstat != NULL) 313 free(mbstat); 314 if (mbpstat != NULL) { 315 if (mbpstat[0] != NULL) 316 free(mbpstat[0]); 317 free(mbpstat); 318 } 319 320 return; 321 } 322