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