Home | History | Annotate | Line # | Download | only in netstat
mbuf.c revision 1.22
      1 /*	$NetBSD: mbuf.c,v 1.22 2003/10/18 12:26:26 enami Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1983, 1988, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 #if 0
     35 static char sccsid[] = "from: @(#)mbuf.c	8.1 (Berkeley) 6/6/93";
     36 #else
     37 __RCSID("$NetBSD: mbuf.c,v 1.22 2003/10/18 12:26:26 enami Exp $");
     38 #endif
     39 #endif /* not lint */
     40 
     41 #define	__POOL_EXPOSE
     42 
     43 #include <sys/param.h>
     44 #include <sys/protosw.h>
     45 #include <sys/socket.h>
     46 #include <sys/mbuf.h>
     47 #include <sys/pool.h>
     48 #include <sys/sysctl.h>
     49 
     50 #include <stdio.h>
     51 #include <stdlib.h>
     52 #include <limits.h>
     53 #include <errno.h>
     54 #include <err.h>
     55 #include "netstat.h"
     56 
     57 #define	YES	1
     58 typedef int bool;
     59 
     60 struct	mbstat mbstat;
     61 struct pool mbpool, mclpool;
     62 struct pool_allocator mbpa, mclpa;
     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 const int nmbtypes = sizeof(mbstat.m_mtypes) / sizeof(short);
     79 bool seen[256];			/* "have we seen this type yet?" */
     80 
     81 int mbstats_ctl[] = { CTL_KERN, KERN_MBUF, MBUF_STATS };
     82 int mowners_ctl[] = { CTL_KERN, KERN_MBUF, MBUF_MOWNERS };
     83 
     84 /*
     85  * Print mbuf statistics.
     86  */
     87 void
     88 mbpr(mbaddr, msizeaddr, mclbaddr, mbpooladdr, mclpooladdr)
     89 	u_long mbaddr;
     90 	u_long msizeaddr, mclbaddr;
     91 	u_long mbpooladdr, mclpooladdr;
     92 {
     93 	u_long totmem, totused, totpct;
     94 	u_int totmbufs;
     95 	int i, lines;
     96 	struct mbtypes *mp;
     97 	size_t len;
     98 	void *data;
     99 	struct mowner *mo;
    100 	int mclbytes, msize;
    101 
    102 	if (nmbtypes != 256) {
    103 		fprintf(stderr,
    104 		    "%s: unexpected change to mbstat; check source\n",
    105 		        getprogname());
    106 		return;
    107 	}
    108 
    109 	if (use_sysctl) {
    110 		size_t mbstatlen = sizeof(mbstat);
    111 		if (sysctl(mbstats_ctl,
    112 			    sizeof(mbstats_ctl) / sizeof(mbstats_ctl[0]),
    113 			    &mbstat, &mbstatlen, NULL, 0) < 0) {
    114 			warn("mbstat: sysctl failed");
    115 			return;
    116 		}
    117 		goto printit;
    118 	}
    119 
    120 	if (mbaddr == 0) {
    121 		fprintf(stderr, "%s: mbstat: symbol not in namelist\n",
    122 		    getprogname());
    123 		return;
    124 	}
    125 /*XXX*/
    126 	if (msizeaddr != 0)
    127 		kread(msizeaddr, (char *)&msize, sizeof (msize));
    128 	else
    129 		msize = MSIZE;
    130 	if (mclbaddr != 0)
    131 		kread(mclbaddr, (char *)&mclbytes, sizeof (mclbytes));
    132 	else
    133 		mclbytes = MCLBYTES;
    134 /*XXX*/
    135 
    136 	if (kread(mbaddr, (char *)&mbstat, sizeof (mbstat)))
    137 		return;
    138 
    139 	if (kread(mbpooladdr, (char *)&mbpool, sizeof (mbpool)))
    140 		return;
    141 
    142 	if (kread(mclpooladdr, (char *)&mclpool, sizeof (mclpool)))
    143 		return;
    144 
    145 	mbpooladdr = (u_long) mbpool.pr_alloc;
    146 	mclpooladdr = (u_long) mclpool.pr_alloc;
    147 
    148 	if (kread(mbpooladdr, (char *)&mbpa, sizeof (mbpa)))
    149 		return;
    150 
    151 	if (kread(mclpooladdr, (char *)&mclpa, sizeof (mclpa)))
    152 		return;
    153 
    154     printit:
    155 	totmbufs = 0;
    156 	for (mp = mbtypes; mp->mt_name; mp++)
    157 		totmbufs += mbstat.m_mtypes[mp->mt_type];
    158 	printf("%u mbufs in use:\n", totmbufs);
    159 	for (mp = mbtypes; mp->mt_name; mp++)
    160 		if (mbstat.m_mtypes[mp->mt_type]) {
    161 			seen[mp->mt_type] = YES;
    162 			printf("\t%u mbufs allocated to %s\n",
    163 			    mbstat.m_mtypes[mp->mt_type], mp->mt_name);
    164 		}
    165 	seen[MT_FREE] = YES;
    166 	for (i = 0; i < nmbtypes; i++)
    167 		if (!seen[i] && mbstat.m_mtypes[i]) {
    168 			printf("\t%u mbufs allocated to <mbuf type %d>\n",
    169 			    mbstat.m_mtypes[i], i);
    170 		}
    171 
    172 	if (use_sysctl)		/* XXX */
    173 		goto dump_drain;
    174 
    175 	printf("%lu/%lu mapped pages in use\n",
    176 	       (u_long)(mclpool.pr_nget - mclpool.pr_nput),
    177 	       ((u_long)mclpool.pr_npages * mclpool.pr_itemsperpage));
    178 	totmem = (mbpool.pr_npages << mbpa.pa_pageshift) +
    179 	    (mclpool.pr_npages << mclpa.pa_pageshift);
    180 	totused = (mbpool.pr_nget - mbpool.pr_nput) * mbpool.pr_size +
    181 	    (mclpool.pr_nget - mclpool.pr_nput) * mclpool.pr_size;
    182 	if (totmem == 0)
    183 		totpct = 0;
    184 	else if (totused < (ULONG_MAX/100))
    185 		totpct = (totused * 100)/totmem;
    186 	else {
    187 		u_long totmem1 = totmem/100;
    188 		u_long totused1 = totused/100;
    189 		totpct = (totused1 * 100)/totmem1;
    190 	}
    191 
    192 	printf("%lu Kbytes allocated to network (%lu%% in use)\n",
    193 	    totmem / 1024, totpct);
    194 	printf("%lu requests for memory denied\n", mbstat.m_drops);
    195 	printf("%lu requests for memory delayed\n", mbstat.m_wait);
    196 
    197 dump_drain:
    198 	printf("%lu calls to protocol drain routines\n", mbstat.m_drain);
    199 
    200  	if (sflag < 2)
    201 		return;
    202 
    203 	if (!use_sysctl)
    204 		return;
    205 
    206 	if (sysctl(mowners_ctl, sizeof(mowners_ctl)/sizeof(mowners_ctl[0]),
    207 		    NULL, &len, NULL, 0) < 0) {
    208 		if (errno == ENOPROTOOPT)
    209 			return;
    210 		warn("mowners: sysctl test");
    211 		return;
    212 	}
    213 	len += 10 * sizeof(mo);		/* add some slop */
    214 	data = malloc(len);
    215 	if (data == NULL) {
    216 		warn("malloc(%lu)", (u_long)len);
    217 		return;
    218 	}
    219 
    220 	if (sysctl(mowners_ctl, sizeof(mowners_ctl)/sizeof(mowners_ctl[0]),
    221 		    data, &len, NULL, 0) < 0) {
    222 		warn("mowners: sysctl get");
    223 		free(data);
    224 		return;
    225 	}
    226 
    227 	for (mo = (void *) data, lines = 0; len >= sizeof(*mo);
    228 	    len -= sizeof(*mo), mo++) {
    229 		char buf[32];
    230 		if (vflag == 1 &&
    231 		    mo->mo_claims == 0 &&
    232 		    mo->mo_ext_claims == 0 &&
    233 		    mo->mo_cluster_claims == 0)
    234 			continue;
    235 		if (vflag == 0 &&
    236 		    mo->mo_claims == mo->mo_releases &&
    237 		    mo->mo_ext_claims == mo->mo_ext_releases &&
    238 		    mo->mo_cluster_claims == mo->mo_cluster_releases)
    239 			continue;
    240 		snprintf(buf, sizeof(buf), "%16s %-13s",
    241 		    mo->mo_name, mo->mo_descr);
    242 		if ((lines % 24) == 0 || lines > 24) {
    243 			printf("%30s %-8s %10s %10s %10s\n",
    244 			    "", "", "small", "ext", "cluster");
    245 			lines = 1;
    246 		}
    247 		printf("%30s %-8s %10lu %10lu %10lu\n",
    248 		    buf, "inuse",
    249 		    mo->mo_claims - mo->mo_releases,
    250 		    mo->mo_ext_claims - mo->mo_ext_releases,
    251 		    mo->mo_cluster_claims - mo->mo_cluster_releases);
    252 		lines++;
    253 		if (vflag) {
    254 			printf("%30s %-8s %10lu %10lu %10lu\n",
    255 			    "", "claims",
    256 			    mo->mo_claims,
    257 			    mo->mo_ext_claims,
    258 			    mo->mo_cluster_claims);
    259 			printf("%30s %-8s %10lu %10lu %10lu\n",
    260 			    "", "releases",
    261 			    mo->mo_releases,
    262 			    mo->mo_ext_releases,
    263 			    mo->mo_cluster_releases);
    264 			lines += 2;
    265 		}
    266 	}
    267 	free(data);
    268 }
    269