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. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#)mbuf.c 8.1 (Berkeley) 6/6/93 30 * $FreeBSD: src/usr.bin/netstat/mbuf.c,v 1.17.2.3 2001/08/10 09:07:09 ru Exp $ 31 * $DragonFly: src/usr.bin/netstat/mbuf.c,v 1.7 2006/10/09 22:30:48 hsu Exp $ 32 */ 33 34 #define _KERNEL_STRUCTURES 35 #include <sys/param.h> 36 #include <sys/mbuf.h> 37 #include <sys/protosw.h> 38 #include <sys/socket.h> 39 #include <sys/sysctl.h> 40 41 #include <err.h> 42 #include <stdio.h> 43 #include <stdlib.h> 44 #include "netstat.h" 45 46 #define YES 1 47 typedef int bool; 48 49 static struct mbtypenames { 50 int mt_type; 51 const char *mt_name; 52 } mbtypenames[] = { 53 { MT_DATA, "data" }, 54 { MT_OOBDATA, "oob data" }, 55 { MT_CONTROL, "ancillary data" }, 56 { MT_HEADER, "packet headers" }, 57 { MT_SONAME, "socket names and addresses" }, 58 { 0, 0 } 59 }; 60 61 /* 62 * Print mbuf statistics. 63 */ 64 void 65 mbpr(u_long mbaddr, u_long mbtaddr, u_long nmbcaddr, u_long nmbufaddr, 66 u_long ncpusaddr) 67 { 68 u_long totmem, totpossible; 69 struct mbstat *mbstat; 70 struct mbtypenames *mp; 71 int name[3], nmbclusters, nmbufs, nmbtypes; 72 size_t nmbclen, nmbuflen, mbstatlen, mbtypeslen; 73 u_long *mbtypes; 74 int ncpus; 75 int n; 76 int i; 77 bool *seen; /* "have we seen this type yet?" */ 78 79 mbtypes = NULL; 80 seen = NULL; 81 mbstat = NULL; 82 83 /* 84 * XXX 85 * We can't kread() mbtypeslen from a core image so we'll 86 * bogusly assume it's the same as in the running kernel. 87 */ 88 if (sysctlbyname("kern.ipc.mbtypes", NULL, &mbtypeslen, NULL, 0) < 0) { 89 warn("sysctl: retrieving mbtypes length"); 90 goto err; 91 } 92 nmbtypes = mbtypeslen / sizeof(*mbtypes); 93 if ((seen = calloc(nmbtypes, sizeof(*seen))) == NULL) { 94 warn("calloc"); 95 goto err; 96 } 97 98 if (mbaddr) { 99 if (kread(ncpusaddr, (char *)&ncpus, sizeof ncpus)) 100 goto err; 101 mbstat = malloc(sizeof(*mbstat) * ncpus); 102 if (kread(mbaddr, (char *)mbstat, sizeof *mbstat * ncpus)) 103 goto err; 104 mbtypes = malloc(mbtypeslen * ncpus); 105 if (kread(mbtaddr, (char *)mbtypes, mbtypeslen * ncpus)) 106 goto err; 107 if (kread(nmbcaddr, (char *)&nmbclusters, sizeof(int))) 108 goto err; 109 if (kread(nmbufaddr, (char *)&nmbufs, sizeof(int))) 110 goto err; 111 for (n = 1; n < ncpus; ++n) { 112 mbstat[0].m_mbufs += mbstat[n].m_mbufs; 113 mbstat[0].m_clusters += mbstat[n].m_clusters; 114 mbstat[0].m_drops += mbstat[n].m_drops; 115 mbstat[0].m_wait += mbstat[n].m_wait; 116 mbstat[0].m_drain += mbstat[n].m_drain; 117 118 for (i = 0; i < nmbtypes; ++i) { 119 mbtypes[i] += mbtypes[n * nmbtypes + i]; 120 } 121 } 122 } else { 123 name[0] = CTL_KERN; 124 name[1] = KERN_IPC; 125 name[2] = KIPC_MBSTAT; 126 mbstat = malloc(sizeof(*mbstat)); 127 mbstatlen = sizeof *mbstat; 128 /* fake ncpus, kernel will aggregate mbstat array for us */ 129 if (sysctl(name, 3, mbstat, &mbstatlen, 0, 0) < 0) { 130 warn("sysctl: retrieving mbstat"); 131 goto err; 132 } 133 134 mbtypes = malloc(mbtypeslen); 135 if (sysctlbyname("kern.ipc.mbtypes", mbtypes, &mbtypeslen, NULL, 136 0) < 0) { 137 warn("sysctl: retrieving mbtypes"); 138 goto err; 139 } 140 141 name[2] = KIPC_NMBCLUSTERS; 142 nmbclen = sizeof(int); 143 if (sysctl(name, 3, &nmbclusters, &nmbclen, 0, 0) < 0) { 144 warn("sysctl: retrieving nmbclusters"); 145 goto err; 146 } 147 148 nmbuflen = sizeof(int); 149 if (sysctlbyname("kern.ipc.nmbufs", &nmbufs, &nmbuflen, 0, 0) < 0) { 150 warn("sysctl: retrieving nmbufs"); 151 goto err; 152 } 153 } 154 155 #undef MSIZE 156 #define MSIZE (mbstat->m_msize) 157 #undef MCLBYTES 158 #define MCLBYTES (mbstat->m_mclbytes) 159 160 printf("%lu/%u mbufs in use (current/max):\n", mbstat->m_mbufs, nmbufs); 161 printf("%lu/%u mbuf clusters in use (current/max)\n", 162 mbstat->m_clusters, nmbclusters); 163 for (mp = mbtypenames; mp->mt_name; mp++) 164 if (mbtypes[mp->mt_type]) { 165 seen[mp->mt_type] = YES; 166 printf("\t%lu mbufs and mbuf clusters " 167 "allocated to %s\n", 168 mbtypes[mp->mt_type], mp->mt_name); 169 } 170 seen[MT_FREE] = YES; 171 for (i = 0; i < nmbtypes; i++) 172 if (!seen[i] && mbtypes[i]) { 173 printf("\t%lu mbufs and mbuf clusters allocated to <mbuf type %d>\n", 174 mbtypes[i], i); 175 } 176 totmem = mbstat->m_mbufs * MSIZE + mbstat->m_clusters * MCLBYTES; 177 totpossible = nmbclusters * MCLBYTES + MSIZE * nmbufs; 178 printf("%lu Kbytes allocated to network (%lu%% of mb_map in use)\n", 179 totmem / 1024, (totmem * 100) / totpossible); 180 printf("%lu requests for memory denied\n", mbstat->m_drops); 181 printf("%lu requests for memory delayed\n", mbstat->m_wait); 182 printf("%lu calls to protocol drain routines\n", mbstat->m_drain); 183 184 err: 185 if (mbstat != NULL) 186 free(mbstat); 187 if (mbtypes != NULL) 188 free(mbtypes); 189 if (seen != NULL) 190 free(seen); 191 } 192