Annotation of src/usr.bin/netstat/mbuf.c, Revision 1.29
1.29 ! claudio 1: /* $OpenBSD: mbuf.c,v 1.28 2008/12/04 06:00:47 deraadt Exp $ */
1.2 deraadt 2: /* $NetBSD: mbuf.c,v 1.9 1996/05/07 02:55:03 thorpej Exp $ */
1.1 deraadt 3:
4: /*
5: * Copyright (c) 1983, 1988, 1993
6: * The Regents of the University of California. All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
1.19 millert 16: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 17: * may be used to endorse or promote products derived from this software
18: * without specific prior written permission.
19: *
20: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30: * SUCH DAMAGE.
31: */
1.8 provos 32:
1.1 deraadt 33: #include <sys/param.h>
34: #include <sys/protosw.h>
35: #include <sys/socket.h>
36: #include <sys/mbuf.h>
1.8 provos 37: #include <sys/pool.h>
1.13 angelos 38: #include <sys/sysctl.h>
1.27 deraadt 39: #include <net/if.h>
1.1 deraadt 40:
1.24 millert 41: #include <errno.h>
1.13 angelos 42: #include <kvm.h>
1.5 millert 43: #include <limits.h>
1.1 deraadt 44: #include <stdio.h>
1.14 deraadt 45: #include <string.h>
1.17 deraadt 46: #include <unistd.h>
1.1 deraadt 47: #include "netstat.h"
48:
49: #define YES 1
50: typedef int bool;
51:
52: struct mbstat mbstat;
1.27 deraadt 53: struct pool mbpool, mclpools[MCLPOOLS];
54: int mclp;
55: char *mclnames[] = {
56: "mcl2k", "mcl4k", "mcl8k", "mcl9k", "mcl12k", "mcl16k", "mcl64k"
57: };
58: char **mclnamep = mclnames;
1.8 provos 59:
1.13 angelos 60: extern kvm_t *kvmd;
1.1 deraadt 61:
62: static struct mbtypes {
63: int mt_type;
64: char *mt_name;
65: } mbtypes[] = {
66: { MT_DATA, "data" },
67: { MT_OOBDATA, "oob data" },
68: { MT_CONTROL, "ancillary data" },
69: { MT_HEADER, "packet headers" },
70: { MT_FTABLE, "fragment reassembly queue headers" }, /* XXX */
71: { MT_SONAME, "socket names and addresses" },
72: { MT_SOOPTS, "socket options" },
73: { 0, 0 }
74: };
75:
76: int nmbtypes = sizeof(mbstat.m_mtypes) / sizeof(short);
77: bool seen[256]; /* "have we seen this type yet?" */
78:
79: /*
80: * Print mbuf statistics.
81: */
82: void
1.25 deraadt 83: mbpr(void)
1.1 deraadt 84: {
1.29 ! claudio 85: unsigned long totmem, totused, totmbufs;
! 86: int totpct;
1.27 deraadt 87: int i, mib[4], npools;
1.13 angelos 88: struct pool pool;
1.9 mpech 89: struct mbtypes *mp;
1.13 angelos 90: size_t size;
1.10 art 91: int page_size = getpagesize();
1.1 deraadt 92:
93: if (nmbtypes != 256) {
94: fprintf(stderr,
1.2 deraadt 95: "%s: unexpected change to mbstat; check source\n",
1.11 mickey 96: __progname);
1.1 deraadt 97: return;
98: }
1.13 angelos 99:
1.23 marius 100: mib[0] = CTL_KERN;
101: mib[1] = KERN_MBSTAT;
102: size = sizeof(mbstat);
103:
104: if (sysctl(mib, 2, &mbstat, &size, NULL, 0) < 0) {
105: printf("Can't retrieve mbuf statistics from the kernel: %s\n",
106: strerror(errno));
107: return;
108: }
109:
110: mib[0] = CTL_KERN;
111: mib[1] = KERN_POOL;
112: mib[2] = KERN_POOL_NPOOLS;
113: size = sizeof(npools);
114:
115: if (sysctl(mib, 3, &npools, &size, NULL, 0) < 0) {
116: printf("Can't figure out number of pools in kernel: %s\n",
117: strerror(errno));
118: return;
119: }
1.13 angelos 120:
1.23 marius 121: for (i = 1; npools; i++) {
122: char name[32];
1.13 angelos 123:
1.16 angelos 124: mib[0] = CTL_KERN;
1.23 marius 125: mib[1] = KERN_POOL;
126: mib[2] = KERN_POOL_POOL;
127: mib[3] = i;
128: size = sizeof(struct pool);
129: if (sysctl(mib, 4, &pool, &size, NULL, 0) < 0) {
130: if (errno == ENOENT)
131: continue;
132: printf("error getting pool: %s\n",
1.16 angelos 133: strerror(errno));
134: return;
135: }
1.23 marius 136: npools--;
137: mib[2] = KERN_POOL_NAME;
138: size = sizeof(name);
139: if (sysctl(mib, 4, &name, &size, NULL, 0) < 0) {
140: printf("error getting pool name: %s\n",
1.13 angelos 141: strerror(errno));
142: return;
143: }
144:
1.27 deraadt 145: if (!strncmp(name, "mbpl", strlen("mbpl")))
1.23 marius 146: bcopy(&pool, &mbpool, sizeof(struct pool));
1.27 deraadt 147: else if (mclp < nitems(mclpools) &&
148: !strncmp(name, *mclnamep, strlen(*mclnamep))) {
149: bcopy(&pool, &mclpools[mclp++],
150: sizeof(struct pool));
151: mclnamep++;
1.23 marius 152: }
1.1 deraadt 153: }
1.8 provos 154:
1.1 deraadt 155: totmbufs = 0;
156: for (mp = mbtypes; mp->mt_name; mp++)
1.29 ! claudio 157: totmbufs += (unsigned int)mbstat.m_mtypes[mp->mt_type];
! 158: printf("%lu mbuf%s in use:\n", totmbufs, plural(totmbufs));
1.1 deraadt 159: for (mp = mbtypes; mp->mt_name; mp++)
160: if (mbstat.m_mtypes[mp->mt_type]) {
161: seen[mp->mt_type] = YES;
1.6 denny 162: printf("\t%u mbuf%s allocated to %s\n",
163: mbstat.m_mtypes[mp->mt_type],
1.29 ! claudio 164: plural(mbstat.m_mtypes[mp->mt_type]),
1.6 denny 165: mp->mt_name);
1.1 deraadt 166: }
167: seen[MT_FREE] = YES;
168: for (i = 0; i < nmbtypes; i++)
169: if (!seen[i] && mbstat.m_mtypes[i]) {
1.6 denny 170: printf("\t%u mbuf%s allocated to <mbuf type %d>\n",
171: mbstat.m_mtypes[i],
1.29 ! claudio 172: plural(mbstat.m_mtypes[i]), i);
1.1 deraadt 173: }
1.29 ! claudio 174: totmem = (mbpool.pr_npages * (unsigned long)page_size);
1.27 deraadt 175: totused = mbpool.pr_nout * mbpool.pr_size;
176: for (i = 0; i < mclp; i++) {
1.29 ! claudio 177: printf("%u/%lu/%lu mbuf %d byte clusters in use (current/peak/max)\n",
! 178: mclpools[i].pr_nout,
! 179: (u_long)mclpools[i].pr_hiwat * mclpools[i].pr_itemsperpage,
! 180: (u_long)mclpools[i].pr_maxpages * mclpools[i].pr_itemsperpage,
1.27 deraadt 181: mclpools[i].pr_size);
1.29 ! claudio 182: totmem += (mclpools[i].pr_npages * (unsigned long)page_size);
1.27 deraadt 183: totused += mclpools[i].pr_nout * mclpools[i].pr_size;
184: }
185:
1.29 ! claudio 186: totpct = (totmem == 0)? 0 : (totused/(totmem / 100));
! 187: printf("%lu Kbytes allocated to network (%d%% in use)\n",
1.8 provos 188: totmem / 1024, totpct);
1.4 millert 189: printf("%lu requests for memory denied\n", mbstat.m_drops);
190: printf("%lu requests for memory delayed\n", mbstat.m_wait);
191: printf("%lu calls to protocol drain routines\n", mbstat.m_drain);
1.1 deraadt 192: }