mbuf.c revision 155543
1/*
2 * Copyright (c) 1983, 1988, 1993
3 *	The Regents of the University of California.
4 * Copyright (c) 2005 Robert N. M. Watson
5 * 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#if 0
37#ifndef lint
38static char sccsid[] = "@(#)mbuf.c	8.1 (Berkeley) 6/6/93";
39#endif /* not lint */
40#endif
41
42#include <sys/cdefs.h>
43__FBSDID("$FreeBSD: head/usr.bin/netstat/mbuf.c 155543 2006-02-11 17:18:36Z rwatson $");
44
45#include <sys/param.h>
46#include <sys/mbuf.h>
47#include <sys/protosw.h>
48#include <sys/socket.h>
49#include <sys/sysctl.h>
50
51#include <err.h>
52#include <kvm.h>
53#include <memstat.h>
54#include <stdio.h>
55#include <stdlib.h>
56#include <string.h>
57#include "netstat.h"
58
59/*
60 * Print mbuf statistics.
61 */
62void
63mbpr(void *kvmd, u_long mbaddr)
64{
65	struct memory_type_list *mtlp;
66	struct memory_type *mtp;
67	u_int64_t mbuf_count, mbuf_bytes, mbuf_free, mbuf_failures, mbuf_size;
68	u_int64_t cluster_count, cluster_bytes, cluster_limit, cluster_free;
69	u_int64_t cluster_failures, cluster_size;
70	u_int64_t packet_count, packet_bytes, packet_free, packet_failures;
71	u_int64_t tag_count, tag_bytes;
72	u_int64_t bytes_inuse, bytes_incache, bytes_total;
73	int nsfbufs, nsfbufspeak, nsfbufsused;
74	struct mbstat mbstat;
75	size_t mlen;
76	int error, live;
77
78	live = (kvmd == NULL);
79	mtlp = memstat_mtl_alloc();
80	if (mtlp == NULL) {
81		warn("memstat_mtl_alloc");
82		return;
83	}
84
85	/*
86	 * Use memstat_*_all() because some mbuf-related memory is in uma(9),
87	 * and some malloc(9).
88	 */
89	if (live) {
90		if (memstat_sysctl_all(mtlp, 0) < 0) {
91			warnx("memstat_sysctl_all: %s",
92			    memstat_strerror(memstat_mtl_geterror(mtlp)));
93			goto out;
94		}
95	} else {
96		if (memstat_kvm_all(mtlp, kvmd) < 0) {
97			error = memstat_mtl_geterror(mtlp);
98			if (error == MEMSTAT_ERROR_KVM)
99				warnx("memstat_kvm_all: %s",
100				    kvm_geterr(kvmd));
101			else
102				warnx("memstat_kvm_all: %s",
103				    memstat_strerror(error));
104			goto out;
105		}
106	}
107
108	mtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, MBUF_MEM_NAME);
109	if (mtp == NULL) {
110		warnx("memstat_mtl_find: zone %s not found", MBUF_MEM_NAME);
111		goto out;
112	}
113	mbuf_count = memstat_get_count(mtp);
114	mbuf_bytes = memstat_get_bytes(mtp);
115	mbuf_free = memstat_get_free(mtp);
116	mbuf_failures = memstat_get_failures(mtp);
117	mbuf_size = memstat_get_size(mtp);
118
119	mtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, MBUF_PACKET_MEM_NAME);
120	if (mtp == NULL) {
121		warnx("memstat_mtl_find: zone %s not found",
122		    MBUF_PACKET_MEM_NAME);
123		goto out;
124	}
125	packet_count = memstat_get_count(mtp);
126	packet_bytes = memstat_get_bytes(mtp);
127	packet_free = memstat_get_free(mtp);
128	packet_failures = memstat_get_failures(mtp);
129
130	mtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, MBUF_CLUSTER_MEM_NAME);
131	if (mtp == NULL) {
132		warnx("memstat_mtl_find: zone %s not found",
133		    MBUF_CLUSTER_MEM_NAME);
134		goto out;
135	}
136	cluster_count = memstat_get_count(mtp);
137	cluster_bytes = memstat_get_bytes(mtp);
138	cluster_limit = memstat_get_countlimit(mtp);
139	cluster_free = memstat_get_free(mtp);
140	cluster_failures = memstat_get_failures(mtp);
141	cluster_size = memstat_get_size(mtp);
142
143	mtp = memstat_mtl_find(mtlp, ALLOCATOR_MALLOC, MBUF_TAG_MEM_NAME);
144	if (mtp == NULL) {
145		warnx("memstat_mtl_find: malloc type %s not found",
146		    MBUF_TAG_MEM_NAME);
147		goto out;
148	}
149	tag_count = memstat_get_count(mtp);
150	tag_bytes = memstat_get_bytes(mtp);
151
152	printf("%llu/%llu/%llu mbufs in use (current/cache/total)\n",
153	    mbuf_count + packet_count, mbuf_free + packet_free,
154	    mbuf_count + packet_count + mbuf_free + packet_free);
155
156	printf("%llu/%llu/%llu/%llu mbuf clusters in use "
157	    "(current/cache/total/max)\n",
158	    cluster_count - packet_free, cluster_free + packet_free,
159	    cluster_count + cluster_free, cluster_limit);
160
161#if 0
162	printf("%llu mbuf tags in use\n", tag_count);
163#endif
164
165	if (live) {
166		mlen = sizeof(nsfbufs);
167		if (!sysctlbyname("kern.ipc.nsfbufs", &nsfbufs, &mlen, NULL,
168		    0) &&
169		    !sysctlbyname("kern.ipc.nsfbufsused", &nsfbufsused,
170		    &mlen, NULL, 0) &&
171		    !sysctlbyname("kern.ipc.nsfbufspeak", &nsfbufspeak,
172		    &mlen, NULL, 0)) {
173			printf("%d/%d/%d sfbufs in use (current/peak/max)\n",
174			    nsfbufsused, nsfbufspeak, nsfbufs);
175		}
176	}
177
178	/*-
179	 * Calculate in-use bytes as:
180	 * - straight mbuf memory
181	 * - mbuf memory in packets
182	 * - the clusters attached to packets
183	 * - and the rest of the non-packet-attached clusters.
184	 * - m_tag memory
185	 * This avoids counting the clusters attached to packets in the cache.
186	 * This currently excludes sf_buf space.
187	 */
188	bytes_inuse =
189	    mbuf_bytes +			/* straight mbuf memory */
190	    packet_bytes +			/* mbufs in packets */
191	    (packet_count * cluster_size) +	/* clusters in packets */
192	    /* other clusters */
193	    ((cluster_count - packet_count - packet_free) * cluster_size) +
194	    tag_bytes;
195
196	/*
197	 * Calculate in-cache bytes as:
198	 * - cached straught mbufs
199	 * - cached packet mbufs
200	 * - cached packet clusters
201	 * - cached straight clusters
202	 * This currently excludes sf_buf space.
203	 */
204	bytes_incache =
205	    (mbuf_free * mbuf_size) +		/* straight free mbufs */
206	    (packet_free * mbuf_size) +		/* mbufs in free packets */
207	    (packet_free * cluster_size) +	/* clusters in free packets */
208	    (cluster_free * cluster_size);	/* free clusters */
209
210	/*
211	 * Total is bytes in use + bytes in cache.  This doesn't take into
212	 * account various other misc data structures, overhead, etc, but
213	 * gives the user something useful despite that.
214	 */
215	bytes_total = bytes_inuse + bytes_incache;
216
217	printf("%lluK/%lluK/%lluK bytes allocated to network "
218	    "(current/cache/total)\n", bytes_inuse / 1024,
219	    bytes_incache / 1024, bytes_total / 1024);
220
221	printf("%llu/%llu/%llu requests for mbufs denied (mbufs/clusters/"
222	    "mbuf+clusters)\n", mbuf_failures, cluster_failures,
223	    packet_failures);
224
225	if (live) {
226		mlen = sizeof(mbstat);
227		if (sysctlbyname("kern.ipc.mbstat", &mbstat, &mlen, NULL, 0)) {
228			warn("kern.ipc.mbstat");
229			goto out;
230		}
231	} else {
232		if (kread(mbaddr, (char *)&mbstat, sizeof mbstat))
233			goto out;
234	}
235	printf("%lu requests for sfbufs denied\n", mbstat.sf_allocfail);
236	printf("%lu requests for sfbufs delayed\n", mbstat.sf_allocwait);
237	printf("%lu requests for I/O initiated by sendfile\n",
238	    mbstat.sf_iocnt);
239	printf("%lu calls to protocol drain routines\n", mbstat.m_drain);
240out:
241	memstat_mtl_free(mtlp);
242}
243