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>
481590Srgrimes#include <sys/socket.h>
49171465Sjhb#include <sys/socketvar.h>
5023086Swollman#include <sys/sysctl.h>
511590Srgrimes
5227753Scharnier#include <err.h>
53152378Srwatson#include <kvm.h>
54148115Srwatson#include <memstat.h>
55160787Syar#include <stdint.h>
561590Srgrimes#include <stdio.h>
57200462Sdelphij#include <stdlib.h>
58200462Sdelphij#include <string.h>
591590Srgrimes#include "netstat.h"
601590Srgrimes
611590Srgrimes/*
62152378Srwatson * Print mbuf statistics.
631590Srgrimes */
641590Srgrimesvoid
65152378Srwatsonmbpr(void *kvmd, u_long mbaddr)
661590Srgrimes{
67148115Srwatson	struct memory_type_list *mtlp;
68148115Srwatson	struct memory_type *mtp;
69160787Syar	uintmax_t mbuf_count, mbuf_bytes, mbuf_free, mbuf_failures, mbuf_size;
70248356Salfred	uintmax_t mbuf_sleeps;
71160787Syar	uintmax_t cluster_count, cluster_bytes, cluster_limit, cluster_free;
72248356Salfred	uintmax_t cluster_failures, cluster_size, cluster_sleeps;
73160787Syar	uintmax_t packet_count, packet_bytes, packet_free, packet_failures;
74248356Salfred	uintmax_t packet_sleeps;
75160787Syar	uintmax_t tag_count, tag_bytes;
76160787Syar	uintmax_t jumbop_count, jumbop_bytes, jumbop_limit, jumbop_free;
77248356Salfred	uintmax_t jumbop_failures, jumbop_sleeps, jumbop_size;
78160787Syar	uintmax_t jumbo9_count, jumbo9_bytes, jumbo9_limit, jumbo9_free;
79248356Salfred	uintmax_t jumbo9_failures, jumbo9_sleeps, jumbo9_size;
80160787Syar	uintmax_t jumbo16_count, jumbo16_bytes, jumbo16_limit, jumbo16_free;
81248356Salfred	uintmax_t jumbo16_failures, jumbo16_sleeps, jumbo16_size;
82160787Syar	uintmax_t bytes_inuse, bytes_incache, bytes_total;
83148115Srwatson	int nsfbufs, nsfbufspeak, nsfbufsused;
84148115Srwatson	struct mbstat mbstat;
85148115Srwatson	size_t mlen;
86171465Sjhb	int error;
87148115Srwatson
88148115Srwatson	mtlp = memstat_mtl_alloc();
89148115Srwatson	if (mtlp == NULL) {
90148115Srwatson		warn("memstat_mtl_alloc");
91148115Srwatson		return;
92148115Srwatson	}
93148115Srwatson
94148115Srwatson	/*
95152378Srwatson	 * Use memstat_*_all() because some mbuf-related memory is in uma(9),
96152378Srwatson	 * and some malloc(9).
97148115Srwatson	 */
98152378Srwatson	if (live) {
99152378Srwatson		if (memstat_sysctl_all(mtlp, 0) < 0) {
100152378Srwatson			warnx("memstat_sysctl_all: %s",
101152378Srwatson			    memstat_strerror(memstat_mtl_geterror(mtlp)));
102152378Srwatson			goto out;
103152378Srwatson		}
104152378Srwatson	} else {
105152378Srwatson		if (memstat_kvm_all(mtlp, kvmd) < 0) {
106152378Srwatson			error = memstat_mtl_geterror(mtlp);
107152378Srwatson			if (error == MEMSTAT_ERROR_KVM)
108152378Srwatson				warnx("memstat_kvm_all: %s",
109152378Srwatson				    kvm_geterr(kvmd));
110152378Srwatson			else
111152378Srwatson				warnx("memstat_kvm_all: %s",
112152378Srwatson				    memstat_strerror(error));
113152378Srwatson			goto out;
114152378Srwatson		}
115148115Srwatson	}
116148115Srwatson
117148115Srwatson	mtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, MBUF_MEM_NAME);
118148115Srwatson	if (mtp == NULL) {
119148115Srwatson		warnx("memstat_mtl_find: zone %s not found", MBUF_MEM_NAME);
120148115Srwatson		goto out;
121148115Srwatson	}
122148115Srwatson	mbuf_count = memstat_get_count(mtp);
123148115Srwatson	mbuf_bytes = memstat_get_bytes(mtp);
124148115Srwatson	mbuf_free = memstat_get_free(mtp);
125148115Srwatson	mbuf_failures = memstat_get_failures(mtp);
126248356Salfred	mbuf_sleeps = memstat_get_sleeps(mtp);
127148115Srwatson	mbuf_size = memstat_get_size(mtp);
128148115Srwatson
129148115Srwatson	mtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, MBUF_PACKET_MEM_NAME);
130148115Srwatson	if (mtp == NULL) {
131148115Srwatson		warnx("memstat_mtl_find: zone %s not found",
132148115Srwatson		    MBUF_PACKET_MEM_NAME);
133148115Srwatson		goto out;
134148115Srwatson	}
135148115Srwatson	packet_count = memstat_get_count(mtp);
136148115Srwatson	packet_bytes = memstat_get_bytes(mtp);
137148115Srwatson	packet_free = memstat_get_free(mtp);
138248356Salfred	packet_sleeps = memstat_get_sleeps(mtp);
139148115Srwatson	packet_failures = memstat_get_failures(mtp);
140148115Srwatson
141148115Srwatson	mtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, MBUF_CLUSTER_MEM_NAME);
142148115Srwatson	if (mtp == NULL) {
143148115Srwatson		warnx("memstat_mtl_find: zone %s not found",
144148115Srwatson		    MBUF_CLUSTER_MEM_NAME);
145148115Srwatson		goto out;
146148115Srwatson	}
147148115Srwatson	cluster_count = memstat_get_count(mtp);
148148115Srwatson	cluster_bytes = memstat_get_bytes(mtp);
149148115Srwatson	cluster_limit = memstat_get_countlimit(mtp);
150148115Srwatson	cluster_free = memstat_get_free(mtp);
151148115Srwatson	cluster_failures = memstat_get_failures(mtp);
152248356Salfred	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_count = memstat_get_count(mtp);
162148115Srwatson	tag_bytes = memstat_get_bytes(mtp);
163148115Srwatson
164155809Sandre	mtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, MBUF_JUMBOP_MEM_NAME);
165155809Sandre	if (mtp == NULL) {
166155809Sandre		warnx("memstat_mtl_find: zone %s not found",
167155809Sandre		    MBUF_JUMBOP_MEM_NAME);
168155809Sandre		goto out;
169155809Sandre	}
170155809Sandre	jumbop_count = memstat_get_count(mtp);
171155809Sandre	jumbop_bytes = memstat_get_bytes(mtp);
172155809Sandre	jumbop_limit = memstat_get_countlimit(mtp);
173155809Sandre	jumbop_free = memstat_get_free(mtp);
174155809Sandre	jumbop_failures = memstat_get_failures(mtp);
175248356Salfred	jumbop_sleeps = memstat_get_sleeps(mtp);
176155809Sandre	jumbop_size = memstat_get_size(mtp);
177155809Sandre
178155809Sandre	mtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, MBUF_JUMBO9_MEM_NAME);
179155809Sandre	if (mtp == NULL) {
180155809Sandre		warnx("memstat_mtl_find: zone %s not found",
181155809Sandre		    MBUF_JUMBO9_MEM_NAME);
182155809Sandre		goto out;
183155809Sandre	}
184155809Sandre	jumbo9_count = memstat_get_count(mtp);
185155809Sandre	jumbo9_bytes = memstat_get_bytes(mtp);
186155809Sandre	jumbo9_limit = memstat_get_countlimit(mtp);
187155809Sandre	jumbo9_free = memstat_get_free(mtp);
188155809Sandre	jumbo9_failures = memstat_get_failures(mtp);
189248356Salfred	jumbo9_sleeps = memstat_get_sleeps(mtp);
190155809Sandre	jumbo9_size = memstat_get_size(mtp);
191155809Sandre
192155809Sandre	mtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, MBUF_JUMBO16_MEM_NAME);
193155809Sandre	if (mtp == NULL) {
194155809Sandre		warnx("memstat_mtl_find: zone %s not found",
195155809Sandre		    MBUF_JUMBO16_MEM_NAME);
196155809Sandre		goto out;
197155809Sandre	}
198155809Sandre	jumbo16_count = memstat_get_count(mtp);
199155809Sandre	jumbo16_bytes = memstat_get_bytes(mtp);
200155809Sandre	jumbo16_limit = memstat_get_countlimit(mtp);
201155809Sandre	jumbo16_free = memstat_get_free(mtp);
202155809Sandre	jumbo16_failures = memstat_get_failures(mtp);
203248356Salfred	jumbo16_sleeps = memstat_get_sleeps(mtp);
204155809Sandre	jumbo16_size = memstat_get_size(mtp);
205155809Sandre
206160787Syar	printf("%ju/%ju/%ju mbufs in use (current/cache/total)\n",
207148115Srwatson	    mbuf_count + packet_count, mbuf_free + packet_free,
208148115Srwatson	    mbuf_count + packet_count + mbuf_free + packet_free);
209148115Srwatson
210160787Syar	printf("%ju/%ju/%ju/%ju mbuf clusters in use "
211148115Srwatson	    "(current/cache/total/max)\n",
212148115Srwatson	    cluster_count - packet_free, cluster_free + packet_free,
213148115Srwatson	    cluster_count + cluster_free, cluster_limit);
214148115Srwatson
215160787Syar	printf("%ju/%ju mbuf+clusters out of packet secondary zone in use "
216155811Sandre	    "(current/cache)\n",
217155811Sandre	    packet_count, packet_free);
218155811Sandre
219160787Syar	printf("%ju/%ju/%ju/%ju %juk (page size) jumbo clusters in use "
220155809Sandre	    "(current/cache/total/max)\n",
221155809Sandre	    jumbop_count, jumbop_free, jumbop_count + jumbop_free,
222155809Sandre	    jumbop_limit, jumbop_size / 1024);
223155809Sandre
224160787Syar	printf("%ju/%ju/%ju/%ju 9k jumbo clusters in use "
225155809Sandre	    "(current/cache/total/max)\n",
226155809Sandre	    jumbo9_count, jumbo9_free, jumbo9_count + jumbo9_free,
227155809Sandre	    jumbo9_limit);
228155809Sandre
229160787Syar	printf("%ju/%ju/%ju/%ju 16k jumbo clusters in use "
230155809Sandre	    "(current/cache/total/max)\n",
231155809Sandre	    jumbo16_count, jumbo16_free, jumbo16_count + jumbo16_free,
232155809Sandre	    jumbo16_limit);
233155809Sandre
234148115Srwatson#if 0
235160787Syar	printf("%ju mbuf tags in use\n", tag_count);
236148115Srwatson#endif
237148115Srwatson
238148115Srwatson	/*-
239148115Srwatson	 * Calculate in-use bytes as:
240148115Srwatson	 * - straight mbuf memory
241148115Srwatson	 * - mbuf memory in packets
242148115Srwatson	 * - the clusters attached to packets
243148115Srwatson	 * - and the rest of the non-packet-attached clusters.
244148115Srwatson	 * - m_tag memory
245148115Srwatson	 * This avoids counting the clusters attached to packets in the cache.
246148115Srwatson	 * This currently excludes sf_buf space.
247148115Srwatson	 */
248148115Srwatson	bytes_inuse =
249148115Srwatson	    mbuf_bytes +			/* straight mbuf memory */
250148115Srwatson	    packet_bytes +			/* mbufs in packets */
251148115Srwatson	    (packet_count * cluster_size) +	/* clusters in packets */
252148115Srwatson	    /* other clusters */
253148115Srwatson	    ((cluster_count - packet_count - packet_free) * cluster_size) +
254155809Sandre	    tag_bytes +
255155809Sandre	    (jumbop_count * jumbop_size) +	/* jumbo clusters */
256155809Sandre	    (jumbo9_count * jumbo9_size) +
257155809Sandre	    (jumbo16_count * jumbo16_size);
258148115Srwatson
259148115Srwatson	/*
260148115Srwatson	 * Calculate in-cache bytes as:
261148115Srwatson	 * - cached straught mbufs
262148115Srwatson	 * - cached packet mbufs
263148115Srwatson	 * - cached packet clusters
264148115Srwatson	 * - cached straight clusters
265148115Srwatson	 * This currently excludes sf_buf space.
266148115Srwatson	 */
267148115Srwatson	bytes_incache =
268148115Srwatson	    (mbuf_free * mbuf_size) +		/* straight free mbufs */
269148115Srwatson	    (packet_free * mbuf_size) +		/* mbufs in free packets */
270148115Srwatson	    (packet_free * cluster_size) +	/* clusters in free packets */
271155809Sandre	    (cluster_free * cluster_size) +	/* free clusters */
272155809Sandre	    (jumbop_free * jumbop_size) +	/* jumbo clusters */
273155809Sandre	    (jumbo9_free * jumbo9_size) +
274155809Sandre	    (jumbo16_free * jumbo16_size);
275148115Srwatson
276148115Srwatson	/*
277148115Srwatson	 * Total is bytes in use + bytes in cache.  This doesn't take into
278148115Srwatson	 * account various other misc data structures, overhead, etc, but
279148115Srwatson	 * gives the user something useful despite that.
280148115Srwatson	 */
281148115Srwatson	bytes_total = bytes_inuse + bytes_incache;
282148115Srwatson
283160787Syar	printf("%juK/%juK/%juK bytes allocated to network "
284148115Srwatson	    "(current/cache/total)\n", bytes_inuse / 1024,
285148115Srwatson	    bytes_incache / 1024, bytes_total / 1024);
286148115Srwatson
287160787Syar	printf("%ju/%ju/%ju requests for mbufs denied (mbufs/clusters/"
288148115Srwatson	    "mbuf+clusters)\n", mbuf_failures, cluster_failures,
289148115Srwatson	    packet_failures);
290248356Salfred	printf("%ju/%ju/%ju requests for mbufs delayed (mbufs/clusters/"
291248356Salfred	    "mbuf+clusters)\n", mbuf_sleeps, cluster_sleeps,
292248356Salfred	    packet_sleeps);
293148115Srwatson
294248356Salfred	printf("%ju/%ju/%ju requests for jumbo clusters delayed "
295248356Salfred	    "(%juk/9k/16k)\n", jumbop_sleeps, jumbo9_sleeps,
296248356Salfred	    jumbo16_sleeps, jumbop_size / 1024);
297160787Syar	printf("%ju/%ju/%ju requests for jumbo clusters denied "
298160787Syar	    "(%juk/9k/16k)\n", jumbop_failures, jumbo9_failures,
299155809Sandre	    jumbo16_failures, jumbop_size / 1024);
300155809Sandre
301152378Srwatson	if (live) {
302155556Srwatson		mlen = sizeof(nsfbufs);
303155556Srwatson		if (!sysctlbyname("kern.ipc.nsfbufs", &nsfbufs, &mlen, NULL,
304155556Srwatson		    0) &&
305155556Srwatson		    !sysctlbyname("kern.ipc.nsfbufsused", &nsfbufsused,
306155556Srwatson		    &mlen, NULL, 0) &&
307155556Srwatson		    !sysctlbyname("kern.ipc.nsfbufspeak", &nsfbufspeak,
308155556Srwatson		    &mlen, NULL, 0))
309155556Srwatson			printf("%d/%d/%d sfbufs in use (current/peak/max)\n",
310155556Srwatson			    nsfbufsused, nsfbufspeak, nsfbufs);
311152378Srwatson		mlen = sizeof(mbstat);
312152378Srwatson		if (sysctlbyname("kern.ipc.mbstat", &mbstat, &mlen, NULL, 0)) {
313152378Srwatson			warn("kern.ipc.mbstat");
314152378Srwatson			goto out;
315152378Srwatson		}
316152378Srwatson	} else {
317175061Sobrien		if (kread(mbaddr, (char *)&mbstat, sizeof mbstat) != 0)
318152378Srwatson			goto out;
319148115Srwatson	}
320152378Srwatson	printf("%lu requests for sfbufs denied\n", mbstat.sf_allocfail);
321152378Srwatson	printf("%lu requests for sfbufs delayed\n", mbstat.sf_allocwait);
322152378Srwatson	printf("%lu requests for I/O initiated by sendfile\n",
323152378Srwatson	    mbstat.sf_iocnt);
324152378Srwatson	printf("%lu calls to protocol drain routines\n", mbstat.m_drain);
325148115Srwatsonout:
326148115Srwatson	memstat_mtl_free(mtlp);
327148115Srwatson}
328