[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.40

1.40    ! mikeb       1: /*     $OpenBSD: mbuf.c,v 1.39 2017/02/04 13:17:08 jsg 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/socket.h>
1.37      sthen      34: #include <sys/queue.h>
1.1       deraadt    35: #include <sys/mbuf.h>
1.35      deraadt    36: #include <sys/protosw.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.39      jsg        40: #include <netinet/in.h>
1.1       deraadt    41:
1.24      millert    42: #include <errno.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.32      dlg        53: struct kinfo_pool mbpool, mclpools[MCLPOOLS];
1.27      deraadt    54: int    mclp;
                     55: char   *mclnames[] = {
1.38      dlg        56:        "mcl2k",
                     57:        "mcl2k2",
                     58:        "mcl4k",
                     59:        "mcl8k",
                     60:        "mcl9k",
                     61:        "mcl12k",
                     62:        "mcl16k",
                     63:        "mcl64k"
1.27      deraadt    64: };
                     65: char   **mclnamep = mclnames;
1.1       deraadt    66:
                     67: static struct mbtypes {
                     68:        int     mt_type;
                     69:        char    *mt_name;
                     70: } mbtypes[] = {
                     71:        { MT_DATA,      "data" },
                     72:        { MT_OOBDATA,   "oob data" },
                     73:        { MT_CONTROL,   "ancillary data" },
                     74:        { MT_HEADER,    "packet headers" },
                     75:        { MT_FTABLE,    "fragment reassembly queue headers" },  /* XXX */
                     76:        { MT_SONAME,    "socket names and addresses" },
                     77:        { MT_SOOPTS,    "socket options" },
                     78:        { 0, 0 }
                     79: };
                     80:
                     81: int nmbtypes = sizeof(mbstat.m_mtypes) / sizeof(short);
                     82: bool seen[256];                        /* "have we seen this type yet?" */
                     83:
                     84: /*
                     85:  * Print mbuf statistics.
                     86:  */
                     87: void
1.25      deraadt    88: mbpr(void)
1.1       deraadt    89: {
1.40    ! mikeb      90:        unsigned long totmem, totpeak, totmbufs;
        !            91:        int i, maxclusters, mib[4], npools;
1.32      dlg        92:        struct kinfo_pool pool;
1.9       mpech      93:        struct mbtypes *mp;
1.13      angelos    94:        size_t size;
1.1       deraadt    95:
                     96:        if (nmbtypes != 256) {
                     97:                fprintf(stderr,
1.2       deraadt    98:                    "%s: unexpected change to mbstat; check source\n",
1.11      mickey     99:                    __progname);
1.1       deraadt   100:                return;
                    101:        }
1.13      angelos   102:
1.23      marius    103:        mib[0] = CTL_KERN;
1.40    ! mikeb     104:        mib[1] = KERN_MAXCLUSTERS;
        !           105:        size = sizeof(maxclusters);
        !           106:
        !           107:        if (sysctl(mib, 2, &maxclusters, &size, NULL, 0) < 0) {
        !           108:                printf("Can't retrieve value of maxclusters from the "
        !           109:                    "kernel: %s\n",  strerror(errno));
        !           110:                return;
        !           111:        }
        !           112:
        !           113:        mib[0] = CTL_KERN;
1.23      marius    114:        mib[1] = KERN_MBSTAT;
                    115:        size = sizeof(mbstat);
                    116:
                    117:        if (sysctl(mib, 2, &mbstat, &size, NULL, 0) < 0) {
                    118:                printf("Can't retrieve mbuf statistics from the kernel: %s\n",
                    119:                    strerror(errno));
                    120:                return;
                    121:        }
                    122:
                    123:        mib[0] = CTL_KERN;
                    124:        mib[1] = KERN_POOL;
                    125:        mib[2] = KERN_POOL_NPOOLS;
                    126:        size = sizeof(npools);
                    127:
                    128:        if (sysctl(mib, 3, &npools, &size, NULL, 0) < 0) {
                    129:                printf("Can't figure out number of pools in kernel: %s\n",
                    130:                    strerror(errno));
                    131:                return;
                    132:        }
1.13      angelos   133:
1.23      marius    134:        for (i = 1; npools; i++) {
                    135:                char name[32];
1.13      angelos   136:
1.16      angelos   137:                mib[0] = CTL_KERN;
1.23      marius    138:                mib[1] = KERN_POOL;
                    139:                mib[2] = KERN_POOL_POOL;
                    140:                mib[3] = i;
1.32      dlg       141:                size = sizeof(pool);
1.23      marius    142:                if (sysctl(mib, 4, &pool, &size, NULL, 0) < 0) {
                    143:                        if (errno == ENOENT)
                    144:                                continue;
                    145:                        printf("error getting pool: %s\n",
1.16      angelos   146:                            strerror(errno));
                    147:                        return;
                    148:                }
1.23      marius    149:                npools--;
                    150:                mib[2] = KERN_POOL_NAME;
                    151:                size = sizeof(name);
                    152:                if (sysctl(mib, 4, &name, &size, NULL, 0) < 0) {
                    153:                        printf("error getting pool name: %s\n",
1.13      angelos   154:                            strerror(errno));
                    155:                        return;
                    156:                }
                    157:
1.33      dlg       158:                if (!strncmp(name, "mbufpl", strlen("mbufpl")))
1.32      dlg       159:                        bcopy(&pool, &mbpool, sizeof(pool));
1.30      deraadt   160:                else if (mclp < sizeof(mclpools) / sizeof(mclpools[0]) &&
1.27      deraadt   161:                    !strncmp(name, *mclnamep, strlen(*mclnamep))) {
                    162:                        bcopy(&pool, &mclpools[mclp++],
1.32      dlg       163:                            sizeof(pool));
1.27      deraadt   164:                        mclnamep++;
1.23      marius    165:                }
1.1       deraadt   166:        }
1.8       provos    167:
1.1       deraadt   168:        totmbufs = 0;
                    169:        for (mp = mbtypes; mp->mt_name; mp++)
1.29      claudio   170:                totmbufs += (unsigned int)mbstat.m_mtypes[mp->mt_type];
                    171:        printf("%lu mbuf%s in use:\n", totmbufs, plural(totmbufs));
1.1       deraadt   172:        for (mp = mbtypes; mp->mt_name; mp++)
                    173:                if (mbstat.m_mtypes[mp->mt_type]) {
                    174:                        seen[mp->mt_type] = YES;
1.6       denny     175:                        printf("\t%u mbuf%s allocated to %s\n",
                    176:                            mbstat.m_mtypes[mp->mt_type],
1.29      claudio   177:                            plural(mbstat.m_mtypes[mp->mt_type]),
1.6       denny     178:                            mp->mt_name);
1.1       deraadt   179:                }
                    180:        seen[MT_FREE] = YES;
                    181:        for (i = 0; i < nmbtypes; i++)
                    182:                if (!seen[i] && mbstat.m_mtypes[i]) {
1.6       denny     183:                        printf("\t%u mbuf%s allocated to <mbuf type %d>\n",
                    184:                            mbstat.m_mtypes[i],
1.29      claudio   185:                            plural(mbstat.m_mtypes[i]), i);
1.1       deraadt   186:                }
1.36      mpi       187:        totmem = (mbpool.pr_npages * mbpool.pr_pgsize);
1.40    ! mikeb     188:        totpeak = mbpool.pr_hiwat * mbpool.pr_pgsize;
1.27      deraadt   189:        for (i = 0; i < mclp; i++) {
1.40    ! mikeb     190:                printf("%u/%lu mbuf %d byte clusters in use"
        !           191:                    " (current/peak)\n",
1.29      claudio   192:                    mclpools[i].pr_nout,
1.36      mpi       193:                    (unsigned long)
                    194:                        (mclpools[i].pr_hiwat * mclpools[i].pr_itemsperpage),
1.27      deraadt   195:                    mclpools[i].pr_size);
1.36      mpi       196:                totmem += (mclpools[i].pr_npages * mclpools[i].pr_pgsize);
1.40    ! mikeb     197:                totpeak += mclpools[i].pr_hiwat * mclpools[i].pr_pgsize;
1.27      deraadt   198:        }
                    199:
1.40    ! mikeb     200:        printf("%lu/%lu/%lu Kbytes allocated to network "
        !           201:            "(current/peak/max)\n", totmem / 1024, totpeak / 1024,
        !           202:            (unsigned long)(maxclusters * MCLBYTES) / 1024);
1.4       millert   203:        printf("%lu requests for memory denied\n", mbstat.m_drops);
                    204:        printf("%lu requests for memory delayed\n", mbstat.m_wait);
                    205:        printf("%lu calls to protocol drain routines\n", mbstat.m_drain);
1.1       deraadt   206: }