1175061Sobrien/*-
21590Srgrimes * Copyright (c) 1983, 1988, 1993
3148115Srwatson *	The Regents of the University of California.
4148115Srwatson * Copyright (c) 2005 Robert N. M. Watson
5148115Srwatson * All rights reserved.
61590Srgrimes *
71590Srgrimes * Redistribution and use in source and binary forms, with or without
81590Srgrimes * modification, are permitted provided that the following conditions
91590Srgrimes * are met:
101590Srgrimes * 1. Redistributions of source code must retain the above copyright
111590Srgrimes *    notice, this list of conditions and the following disclaimer.
121590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
131590Srgrimes *    notice, this list of conditions and the following disclaimer in the
141590Srgrimes *    documentation and/or other materials provided with the distribution.
151590Srgrimes * 3. All advertising materials mentioning features or use of this software
161590Srgrimes *    must display the following acknowledgement:
171590Srgrimes *	This product includes software developed by the University of
181590Srgrimes *	California, Berkeley and its contributors.
191590Srgrimes * 4. Neither the name of the University nor the names of its contributors
201590Srgrimes *    may be used to endorse or promote products derived from this software
211590Srgrimes *    without specific prior written permission.
221590Srgrimes *
231590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
241590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
251590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
261590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
271590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
281590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
291590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
301590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
311590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
321590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
331590Srgrimes * SUCH DAMAGE.
341590Srgrimes */
351590Srgrimes
36132671Scharnier#if 0
371590Srgrimes#ifndef lint
381590Srgrimesstatic char sccsid[] = "@(#)mbuf.c	8.1 (Berkeley) 6/6/93";
39132671Scharnier#endif /* not lint */
4027753Scharnier#endif
411590Srgrimes
42132671Scharnier#include <sys/cdefs.h>
43132671Scharnier__FBSDID("$FreeBSD$");
44132671Scharnier
451590Srgrimes#include <sys/param.h>
4623086Swollman#include <sys/mbuf.h>
471590Srgrimes#include <sys/protosw.h>
48253351Sae#include <sys/sf_buf.h>
491590Srgrimes#include <sys/socket.h>
50171465Sjhb#include <sys/socketvar.h>
5123086Swollman#include <sys/sysctl.h>
521590Srgrimes
5327753Scharnier#include <err.h>
54152378Srwatson#include <kvm.h>
55148115Srwatson#include <memstat.h>
56160787Syar#include <stdint.h>
571590Srgrimes#include <stdio.h>
58200462Sdelphij#include <stdlib.h>
59200462Sdelphij#include <string.h>
601590Srgrimes#include "netstat.h"
611590Srgrimes
621590Srgrimes/*
63152378Srwatson * Print mbuf statistics.
641590Srgrimes */
651590Srgrimesvoid
66152378Srwatsonmbpr(void *kvmd, u_long mbaddr)
671590Srgrimes{
68148115Srwatson	struct memory_type_list *mtlp;
69148115Srwatson	struct memory_type *mtp;
70160787Syar	uintmax_t mbuf_count, mbuf_bytes, mbuf_free, mbuf_failures, mbuf_size;
71242030Salfred	uintmax_t mbuf_sleeps;
72261207Sglebius	uintmax_t cluster_count, cluster_limit, cluster_free;
73242030Salfred	uintmax_t cluster_failures, cluster_size, cluster_sleeps;
74160787Syar	uintmax_t packet_count, packet_bytes, packet_free, packet_failures;
75242030Salfred	uintmax_t packet_sleeps;
76261207Sglebius	uintmax_t tag_bytes;
77261207Sglebius	uintmax_t jumbop_count, jumbop_limit, jumbop_free;
78242030Salfred	uintmax_t jumbop_failures, jumbop_sleeps, jumbop_size;
79261207Sglebius	uintmax_t jumbo9_count, jumbo9_limit, jumbo9_free;
80242030Salfred	uintmax_t jumbo9_failures, jumbo9_sleeps, jumbo9_size;
81261207Sglebius	uintmax_t jumbo16_count, jumbo16_limit, jumbo16_free;
82242030Salfred	uintmax_t jumbo16_failures, jumbo16_sleeps, jumbo16_size;
83160787Syar	uintmax_t bytes_inuse, bytes_incache, bytes_total;
84148115Srwatson	int nsfbufs, nsfbufspeak, nsfbufsused;
85253351Sae	struct sfstat sfstat;
86148115Srwatson	size_t mlen;
87171465Sjhb	int error;
88148115Srwatson
89148115Srwatson	mtlp = memstat_mtl_alloc();
90148115Srwatson	if (mtlp == NULL) {
91148115Srwatson		warn("memstat_mtl_alloc");
92148115Srwatson		return;
93148115Srwatson	}
94148115Srwatson
95148115Srwatson	/*
96152378Srwatson	 * Use memstat_*_all() because some mbuf-related memory is in uma(9),
97152378Srwatson	 * and some malloc(9).
98148115Srwatson	 */
99152378Srwatson	if (live) {
100152378Srwatson		if (memstat_sysctl_all(mtlp, 0) < 0) {
101152378Srwatson			warnx("memstat_sysctl_all: %s",
102152378Srwatson			    memstat_strerror(memstat_mtl_geterror(mtlp)));
103152378Srwatson			goto out;
104152378Srwatson		}
105152378Srwatson	} else {
106152378Srwatson		if (memstat_kvm_all(mtlp, kvmd) < 0) {
107152378Srwatson			error = memstat_mtl_geterror(mtlp);
108152378Srwatson			if (error == MEMSTAT_ERROR_KVM)
109152378Srwatson				warnx("memstat_kvm_all: %s",
110152378Srwatson				    kvm_geterr(kvmd));
111152378Srwatson			else
112152378Srwatson				warnx("memstat_kvm_all: %s",
113152378Srwatson				    memstat_strerror(error));
114152378Srwatson			goto out;
115152378Srwatson		}
116148115Srwatson	}
117148115Srwatson
118148115Srwatson	mtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, MBUF_MEM_NAME);
119148115Srwatson	if (mtp == NULL) {
120148115Srwatson		warnx("memstat_mtl_find: zone %s not found", MBUF_MEM_NAME);
121148115Srwatson		goto out;
122148115Srwatson	}
123148115Srwatson	mbuf_count = memstat_get_count(mtp);
124148115Srwatson	mbuf_bytes = memstat_get_bytes(mtp);
125148115Srwatson	mbuf_free = memstat_get_free(mtp);
126148115Srwatson	mbuf_failures = memstat_get_failures(mtp);
127242030Salfred	mbuf_sleeps = memstat_get_sleeps(mtp);
128148115Srwatson	mbuf_size = memstat_get_size(mtp);
129148115Srwatson
130148115Srwatson	mtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, MBUF_PACKET_MEM_NAME);
131148115Srwatson	if (mtp == NULL) {
132148115Srwatson		warnx("memstat_mtl_find: zone %s not found",
133148115Srwatson		    MBUF_PACKET_MEM_NAME);
134148115Srwatson		goto out;
135148115Srwatson	}
136148115Srwatson	packet_count = memstat_get_count(mtp);
137148115Srwatson	packet_bytes = memstat_get_bytes(mtp);
138148115Srwatson	packet_free = memstat_get_free(mtp);
139242030Salfred	packet_sleeps = memstat_get_sleeps(mtp);
140148115Srwatson	packet_failures = memstat_get_failures(mtp);
141148115Srwatson
142148115Srwatson	mtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, MBUF_CLUSTER_MEM_NAME);
143148115Srwatson	if (mtp == NULL) {
144148115Srwatson		warnx("memstat_mtl_find: zone %s not found",
145148115Srwatson		    MBUF_CLUSTER_MEM_NAME);
146148115Srwatson		goto out;
147148115Srwatson	}
148148115Srwatson	cluster_count = memstat_get_count(mtp);
149148115Srwatson	cluster_limit = memstat_get_countlimit(mtp);
150148115Srwatson	cluster_free = memstat_get_free(mtp);
151148115Srwatson	cluster_failures = memstat_get_failures(mtp);
152242030Salfred	cluster_sleeps = memstat_get_sleeps(mtp);
153148115Srwatson	cluster_size = memstat_get_size(mtp);
154148115Srwatson
155148115Srwatson	mtp = memstat_mtl_find(mtlp, ALLOCATOR_MALLOC, MBUF_TAG_MEM_NAME);
156148115Srwatson	if (mtp == NULL) {
157148115Srwatson		warnx("memstat_mtl_find: malloc type %s not found",
158148115Srwatson		    MBUF_TAG_MEM_NAME);
159148115Srwatson		goto out;
160148115Srwatson	}
161148115Srwatson	tag_bytes = memstat_get_bytes(mtp);
162148115Srwatson
163155809Sandre	mtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, MBUF_JUMBOP_MEM_NAME);
164155809Sandre	if (mtp == NULL) {
165155809Sandre		warnx("memstat_mtl_find: zone %s not found",
166155809Sandre		    MBUF_JUMBOP_MEM_NAME);
167155809Sandre		goto out;
168155809Sandre	}
169155809Sandre	jumbop_count = memstat_get_count(mtp);
170155809Sandre	jumbop_limit = memstat_get_countlimit(mtp);
171155809Sandre	jumbop_free = memstat_get_free(mtp);
172155809Sandre	jumbop_failures = memstat_get_failures(mtp);
173242030Salfred	jumbop_sleeps = memstat_get_sleeps(mtp);
174155809Sandre	jumbop_size = memstat_get_size(mtp);
175155809Sandre
176155809Sandre	mtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, MBUF_JUMBO9_MEM_NAME);
177155809Sandre	if (mtp == NULL) {
178155809Sandre		warnx("memstat_mtl_find: zone %s not found",
179155809Sandre		    MBUF_JUMBO9_MEM_NAME);
180155809Sandre		goto out;
181155809Sandre	}
182155809Sandre	jumbo9_count = memstat_get_count(mtp);
183155809Sandre	jumbo9_limit = memstat_get_countlimit(mtp);
184155809Sandre	jumbo9_free = memstat_get_free(mtp);
185155809Sandre	jumbo9_failures = memstat_get_failures(mtp);
186242030Salfred	jumbo9_sleeps = memstat_get_sleeps(mtp);
187155809Sandre	jumbo9_size = memstat_get_size(mtp);
188155809Sandre
189155809Sandre	mtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, MBUF_JUMBO16_MEM_NAME);
190155809Sandre	if (mtp == NULL) {
191155809Sandre		warnx("memstat_mtl_find: zone %s not found",
192155809Sandre		    MBUF_JUMBO16_MEM_NAME);
193155809Sandre		goto out;
194155809Sandre	}
195155809Sandre	jumbo16_count = memstat_get_count(mtp);
196155809Sandre	jumbo16_limit = memstat_get_countlimit(mtp);
197155809Sandre	jumbo16_free = memstat_get_free(mtp);
198155809Sandre	jumbo16_failures = memstat_get_failures(mtp);
199242030Salfred	jumbo16_sleeps = memstat_get_sleeps(mtp);
200155809Sandre	jumbo16_size = memstat_get_size(mtp);
201155809Sandre
202160787Syar	printf("%ju/%ju/%ju mbufs in use (current/cache/total)\n",
203148115Srwatson	    mbuf_count + packet_count, mbuf_free + packet_free,
204148115Srwatson	    mbuf_count + packet_count + mbuf_free + packet_free);
205148115Srwatson
206160787Syar	printf("%ju/%ju/%ju/%ju mbuf clusters in use "
207148115Srwatson	    "(current/cache/total/max)\n",
208148115Srwatson	    cluster_count - packet_free, cluster_free + packet_free,
209148115Srwatson	    cluster_count + cluster_free, cluster_limit);
210148115Srwatson
211160787Syar	printf("%ju/%ju mbuf+clusters out of packet secondary zone in use "
212155811Sandre	    "(current/cache)\n",
213155811Sandre	    packet_count, packet_free);
214155811Sandre
215160787Syar	printf("%ju/%ju/%ju/%ju %juk (page size) jumbo clusters in use "
216155809Sandre	    "(current/cache/total/max)\n",
217155809Sandre	    jumbop_count, jumbop_free, jumbop_count + jumbop_free,
218155809Sandre	    jumbop_limit, jumbop_size / 1024);
219155809Sandre
220160787Syar	printf("%ju/%ju/%ju/%ju 9k jumbo clusters in use "
221155809Sandre	    "(current/cache/total/max)\n",
222155809Sandre	    jumbo9_count, jumbo9_free, jumbo9_count + jumbo9_free,
223155809Sandre	    jumbo9_limit);
224155809Sandre
225160787Syar	printf("%ju/%ju/%ju/%ju 16k jumbo clusters in use "
226155809Sandre	    "(current/cache/total/max)\n",
227155809Sandre	    jumbo16_count, jumbo16_free, jumbo16_count + jumbo16_free,
228155809Sandre	    jumbo16_limit);
229155809Sandre
230148115Srwatson#if 0
231160787Syar	printf("%ju mbuf tags in use\n", tag_count);
232148115Srwatson#endif
233148115Srwatson
234148115Srwatson	/*-
235148115Srwatson	 * Calculate in-use bytes as:
236148115Srwatson	 * - straight mbuf memory
237148115Srwatson	 * - mbuf memory in packets
238148115Srwatson	 * - the clusters attached to packets
239148115Srwatson	 * - and the rest of the non-packet-attached clusters.
240148115Srwatson	 * - m_tag memory
241148115Srwatson	 * This avoids counting the clusters attached to packets in the cache.
242148115Srwatson	 * This currently excludes sf_buf space.
243148115Srwatson	 */
244148115Srwatson	bytes_inuse =
245148115Srwatson	    mbuf_bytes +			/* straight mbuf memory */
246148115Srwatson	    packet_bytes +			/* mbufs in packets */
247148115Srwatson	    (packet_count * cluster_size) +	/* clusters in packets */
248148115Srwatson	    /* other clusters */
249148115Srwatson	    ((cluster_count - packet_count - packet_free) * cluster_size) +
250155809Sandre	    tag_bytes +
251155809Sandre	    (jumbop_count * jumbop_size) +	/* jumbo clusters */
252155809Sandre	    (jumbo9_count * jumbo9_size) +
253155809Sandre	    (jumbo16_count * jumbo16_size);
254148115Srwatson
255148115Srwatson	/*
256148115Srwatson	 * Calculate in-cache bytes as:
257148115Srwatson	 * - cached straught mbufs
258148115Srwatson	 * - cached packet mbufs
259148115Srwatson	 * - cached packet clusters
260148115Srwatson	 * - cached straight clusters
261148115Srwatson	 * This currently excludes sf_buf space.
262148115Srwatson	 */
263148115Srwatson	bytes_incache =
264148115Srwatson	    (mbuf_free * mbuf_size) +		/* straight free mbufs */
265148115Srwatson	    (packet_free * mbuf_size) +		/* mbufs in free packets */
266148115Srwatson	    (packet_free * cluster_size) +	/* clusters in free packets */
267155809Sandre	    (cluster_free * cluster_size) +	/* free clusters */
268155809Sandre	    (jumbop_free * jumbop_size) +	/* jumbo clusters */
269155809Sandre	    (jumbo9_free * jumbo9_size) +
270155809Sandre	    (jumbo16_free * jumbo16_size);
271148115Srwatson
272148115Srwatson	/*
273148115Srwatson	 * Total is bytes in use + bytes in cache.  This doesn't take into
274148115Srwatson	 * account various other misc data structures, overhead, etc, but
275148115Srwatson	 * gives the user something useful despite that.
276148115Srwatson	 */
277148115Srwatson	bytes_total = bytes_inuse + bytes_incache;
278148115Srwatson
279160787Syar	printf("%juK/%juK/%juK bytes allocated to network "
280148115Srwatson	    "(current/cache/total)\n", bytes_inuse / 1024,
281148115Srwatson	    bytes_incache / 1024, bytes_total / 1024);
282148115Srwatson
283160787Syar	printf("%ju/%ju/%ju requests for mbufs denied (mbufs/clusters/"
284148115Srwatson	    "mbuf+clusters)\n", mbuf_failures, cluster_failures,
285148115Srwatson	    packet_failures);
286242030Salfred	printf("%ju/%ju/%ju requests for mbufs delayed (mbufs/clusters/"
287242030Salfred	    "mbuf+clusters)\n", mbuf_sleeps, cluster_sleeps,
288242030Salfred	    packet_sleeps);
289148115Srwatson
290242030Salfred	printf("%ju/%ju/%ju requests for jumbo clusters delayed "
291242030Salfred	    "(%juk/9k/16k)\n", jumbop_sleeps, jumbo9_sleeps,
292242030Salfred	    jumbo16_sleeps, jumbop_size / 1024);
293160787Syar	printf("%ju/%ju/%ju requests for jumbo clusters denied "
294160787Syar	    "(%juk/9k/16k)\n", jumbop_failures, jumbo9_failures,
295155809Sandre	    jumbo16_failures, jumbop_size / 1024);
296155809Sandre
297152378Srwatson	if (live) {
298155556Srwatson		mlen = sizeof(nsfbufs);
299155556Srwatson		if (!sysctlbyname("kern.ipc.nsfbufs", &nsfbufs, &mlen, NULL,
300155556Srwatson		    0) &&
301155556Srwatson		    !sysctlbyname("kern.ipc.nsfbufsused", &nsfbufsused,
302155556Srwatson		    &mlen, NULL, 0) &&
303155556Srwatson		    !sysctlbyname("kern.ipc.nsfbufspeak", &nsfbufspeak,
304155556Srwatson		    &mlen, NULL, 0))
305155556Srwatson			printf("%d/%d/%d sfbufs in use (current/peak/max)\n",
306155556Srwatson			    nsfbufsused, nsfbufspeak, nsfbufs);
307253351Sae		mlen = sizeof(sfstat);
308253351Sae		if (sysctlbyname("kern.ipc.sfstat", &sfstat, &mlen, NULL, 0)) {
309253351Sae			warn("kern.ipc.sfstat");
310152378Srwatson			goto out;
311152378Srwatson		}
312152378Srwatson	} else {
313253351Sae		if (kread_counters(mbaddr, (char *)&sfstat, sizeof sfstat) != 0)
314152378Srwatson			goto out;
315148115Srwatson	}
316253351Sae	printf("%ju requests for sfbufs denied\n",
317253351Sae	    (uintmax_t)sfstat.sf_allocfail);
318253351Sae	printf("%ju requests for sfbufs delayed\n",
319253351Sae	    (uintmax_t)sfstat.sf_allocwait);
320253351Sae	printf("%ju requests for I/O initiated by sendfile\n",
321253351Sae	    (uintmax_t)sfstat.sf_iocnt);
322148115Srwatsonout:
323148115Srwatson	memstat_mtl_free(mtlp);
324148115Srwatson}
325