[BACK]Return to mbuf.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / netstat

Annotation of src/usr.bin/netstat/mbuf.c, Revision 1.19

1.19    ! millert     1: /*     $OpenBSD: mbuf.c,v 1.18 2003/03/21 16:55:23 pb 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:  */
                     32:
                     33: #ifndef lint
                     34: #if 0
                     35: static char sccsid[] = "from: @(#)mbuf.c       8.1 (Berkeley) 6/6/93";
                     36: #else
1.19    ! millert    37: static char *rcsid = "$OpenBSD: mbuf.c,v 1.18 2003/03/21 16:55:23 pb Exp $";
1.1       deraadt    38: #endif
                     39: #endif /* not lint */
1.8       provos     40:
1.1       deraadt    41: #include <sys/param.h>
                     42: #include <sys/protosw.h>
                     43: #include <sys/socket.h>
                     44: #include <sys/mbuf.h>
1.8       provos     45: #include <sys/pool.h>
1.13      angelos    46: #include <sys/sysctl.h>
                     47: #include <sys/errno.h>
1.1       deraadt    48:
1.13      angelos    49: #include <kvm.h>
1.5       millert    50: #include <limits.h>
1.1       deraadt    51: #include <stdio.h>
1.14      deraadt    52: #include <string.h>
1.17      deraadt    53: #include <unistd.h>
1.1       deraadt    54: #include "netstat.h"
                     55:
                     56: #define        YES     1
                     57: typedef int bool;
                     58:
                     59: struct mbstat mbstat;
1.8       provos     60: struct pool mbpool, mclpool;
                     61:
1.13      angelos    62: extern kvm_t *kvmd;
1.1       deraadt    63:
                     64: static struct mbtypes {
                     65:        int     mt_type;
                     66:        char    *mt_name;
                     67: } mbtypes[] = {
                     68:        { MT_DATA,      "data" },
                     69:        { MT_OOBDATA,   "oob data" },
                     70:        { MT_CONTROL,   "ancillary data" },
                     71:        { MT_HEADER,    "packet headers" },
                     72:        { MT_FTABLE,    "fragment reassembly queue headers" },  /* XXX */
                     73:        { MT_SONAME,    "socket names and addresses" },
                     74:        { MT_SOOPTS,    "socket options" },
                     75:        { 0, 0 }
                     76: };
                     77:
                     78: int nmbtypes = sizeof(mbstat.m_mtypes) / sizeof(short);
                     79: bool seen[256];                        /* "have we seen this type yet?" */
                     80:
                     81: /*
                     82:  * Print mbuf statistics.
                     83:  */
                     84: void
1.17      deraadt    85: mbpr(u_long mbaddr, u_long mbpooladdr, u_long mclpooladdr)
1.1       deraadt    86: {
1.9       mpech      87:        int totmem, totused, totmbufs, totpct;
1.13      angelos    88:        int i, mib[4], npools, flag = 0;
                     89:        struct pool pool;
1.9       mpech      90:        struct mbtypes *mp;
1.13      angelos    91:        size_t size;
1.10      art        92:        int page_size = getpagesize();
1.1       deraadt    93:
                     94:        if (nmbtypes != 256) {
                     95:                fprintf(stderr,
1.2       deraadt    96:                    "%s: unexpected change to mbstat; check source\n",
1.11      mickey     97:                    __progname);
1.1       deraadt    98:                return;
                     99:        }
1.13      angelos   100:
                    101:        if (kvmd == NULL) {
                    102:                if (mbaddr == 0) {
                    103:                        fprintf(stderr, "%s: mbstat: symbol not in namelist\n",
                    104:                            __progname);
                    105:                        return;
                    106:                }
                    107:
                    108:                if (kread(mbaddr, (char *)&mbstat, sizeof (mbstat)))
                    109:                        return;
                    110:                if (kread(mbpooladdr, (char *)&mbpool, sizeof (mbpool)))
                    111:                        return;
                    112:
                    113:                if (kread(mclpooladdr, (char *)&mclpool, sizeof (mclpool)))
                    114:                        return;
                    115:        } else {
1.16      angelos   116:                mib[0] = CTL_KERN;
                    117:                mib[1] = KERN_MBSTAT;
                    118:                size = sizeof(mbstat);
                    119:
                    120:                if (sysctl(mib, 2, &mbstat, &size, NULL, 0) < 0) {
                    121:                        printf("Can't retrieve mbuf statistics from the kernel: %s\n",
                    122:                            strerror(errno));
                    123:                        return;
                    124:                }
                    125:
1.13      angelos   126:                mib[0] = CTL_KERN;
                    127:                mib[1] = KERN_POOL;
1.15      angelos   128:                mib[2] = KERN_POOL_NPOOLS;
1.13      angelos   129:                size = sizeof(npools);
                    130:
                    131:                if (sysctl(mib, 3, &npools, &size, NULL, 0) < 0) {
                    132:                        printf("Can't figure out number of pools in kernel: %s\n",
                    133:                            strerror(errno));
                    134:                        return;
                    135:                }
                    136:
                    137:                for (i = 1; npools; i++) {
                    138:                        char name[32];
                    139:
                    140:                        mib[0] = CTL_KERN;
                    141:                        mib[1] = KERN_POOL;
                    142:                        mib[2] = KERN_POOL_POOL;
                    143:                        mib[3] = i;
                    144:                        size = sizeof(struct pool);
                    145:                        if (sysctl(mib, 4, &pool, &size, NULL, 0) < 0) {
                    146:                                if (errno == ENOENT)
                    147:                                        continue;
                    148:                                printf("error getting pool: %s\n",
                    149:                                    strerror(errno));
                    150:                                return;
                    151:                        }
                    152:                        npools--;
                    153:                        mib[2] = KERN_POOL_NAME;
                    154:                        size = sizeof(name);
                    155:                        if (sysctl(mib, 4, &name, &size, NULL, 0) < 0) {
                    156:                                printf("error getting pool name: %s\n",
                    157:                                    strerror(errno));
                    158:                                return;
                    159:                        }
                    160:
                    161:                        if (!strncmp(name, "mbpl", strlen("mbpl"))) {
                    162:                                bcopy(&pool, &mbpool, sizeof(struct pool));
                    163:                                flag++;
                    164:                        } else {
                    165:                                if (!strncmp(name, "mclpl", strlen("mclpl"))) {
                    166:                                        bcopy(&pool, &mclpool,
                    167:                                            sizeof(struct pool));
                    168:                                        flag++;
                    169:                                }
                    170:                        }
                    171:
                    172:                        if (flag == 2)
                    173:                                break;
                    174:                }
1.1       deraadt   175:        }
1.8       provos    176:
1.1       deraadt   177:        totmbufs = 0;
                    178:        for (mp = mbtypes; mp->mt_name; mp++)
                    179:                totmbufs += mbstat.m_mtypes[mp->mt_type];
1.6       denny     180:        printf("%u mbuf%s in use:\n", totmbufs, plural(totmbufs));
1.1       deraadt   181:        for (mp = mbtypes; mp->mt_name; mp++)
                    182:                if (mbstat.m_mtypes[mp->mt_type]) {
                    183:                        seen[mp->mt_type] = YES;
1.6       denny     184:                        printf("\t%u mbuf%s allocated to %s\n",
                    185:                            mbstat.m_mtypes[mp->mt_type],
                    186:                            plural((int)mbstat.m_mtypes[mp->mt_type]),
                    187:                            mp->mt_name);
1.1       deraadt   188:                }
                    189:        seen[MT_FREE] = YES;
                    190:        for (i = 0; i < nmbtypes; i++)
                    191:                if (!seen[i] && mbstat.m_mtypes[i]) {
1.6       denny     192:                        printf("\t%u mbuf%s allocated to <mbuf type %d>\n",
                    193:                            mbstat.m_mtypes[i],
                    194:                            plural((int)mbstat.m_mtypes[i]), i);
1.1       deraadt   195:                }
1.18      pb        196:        printf("%lu/%lu/%lu mbuf cluster pages in use (current/peak/max)\n",
1.11      mickey    197:            (u_long)(mclpool.pr_nget - mclpool.pr_nput),
1.18      pb        198:            (u_long)mclpool.pr_hiwat, (u_long)mclpool.pr_maxpages);
1.10      art       199:        totmem = (mbpool.pr_npages * page_size) +
                    200:            (mclpool.pr_npages * page_size);
1.11      mickey    201:        totused = (mbpool.pr_nget - mbpool.pr_nput) * mbpool.pr_size +
1.8       provos    202:            (mclpool.pr_nget - mclpool.pr_nput) * mclpool.pr_size;
                    203:        totpct = (totmem == 0)? 0 : ((totused * 100)/totmem);
1.1       deraadt   204:        printf("%u Kbytes allocated to network (%d%% in use)\n",
1.8       provos    205:            totmem / 1024, totpct);
1.4       millert   206:        printf("%lu requests for memory denied\n", mbstat.m_drops);
                    207:        printf("%lu requests for memory delayed\n", mbstat.m_wait);
                    208:        printf("%lu calls to protocol drain routines\n", mbstat.m_drain);
1.1       deraadt   209: }