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

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