source: rtems-libbsd/freebsd-userspace/commands/usr.bin/netstat/mbuf.c @ 0fa7dab

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since 0fa7dab was 0fa7dab, checked in by Joel Sherrill <joel.sherrill@…>, on 10/22/12 at 15:53:17

route command: Clean up kernal symbol accesses

  • Property mode set to 100644
File size: 10.5 KB
Line 
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$");
44
45#include <sys/param.h>
46#ifdef __rtems__
47#include <stdint.h>
48#include <freebsd/sys/mbuf.h>
49#include <freebsd/sys/protosw.h>
50#else
51#include <sys/mbuf.h>
52#include <sys/protosw.h>
53#endif
54#include <sys/socket.h>
55#include <sys/socketvar.h>
56#include <sys/sysctl.h>
57
58#include <err.h>
59#include <kvm.h>
60#include <memstat.h>
61#include <stdint.h>
62#include <stdio.h>
63#include <stdlib.h>
64#include <string.h>
65#include "netstat.h"
66
67/*
68 * Print mbuf statistics.
69 */
70void
71mbpr(void *kvmd, u_long mbaddr)
72{
73        struct memory_type_list *mtlp;
74        struct memory_type *mtp;
75        uintmax_t mbuf_count, mbuf_bytes, mbuf_free, mbuf_failures, mbuf_size;
76        uintmax_t cluster_count, cluster_bytes, cluster_limit, cluster_free;
77        uintmax_t cluster_failures, cluster_size;
78        uintmax_t packet_count, packet_bytes, packet_free, packet_failures;
79        uintmax_t tag_count, tag_bytes;
80        uintmax_t jumbop_count, jumbop_bytes, jumbop_limit, jumbop_free;
81        uintmax_t jumbop_failures, jumbop_size;
82        uintmax_t jumbo9_count, jumbo9_bytes, jumbo9_limit, jumbo9_free;
83        uintmax_t jumbo9_failures, jumbo9_size;
84        uintmax_t jumbo16_count, jumbo16_bytes, jumbo16_limit, jumbo16_free;
85        uintmax_t jumbo16_failures, jumbo16_size;
86        uintmax_t bytes_inuse, bytes_incache, bytes_total;
87        int nsfbufs, nsfbufspeak, nsfbufsused;
88        struct mbstat mbstat;
89        size_t mlen;
90        int error;
91
92        mtlp = memstat_mtl_alloc();
93        if (mtlp == NULL) {
94                warn("memstat_mtl_alloc");
95                return;
96        }
97
98        /*
99         * Use memstat_*_all() because some mbuf-related memory is in uma(9),
100         * and some malloc(9).
101         */
102        if (live) {
103                if (memstat_sysctl_all(mtlp, 0) < 0) {
104                        warnx("memstat_sysctl_all: %s",
105                            memstat_strerror(memstat_mtl_geterror(mtlp)));
106                        goto out;
107                }
108        } else {
109#ifndef __rtems__
110                if (memstat_kvm_all(mtlp, kvmd) < 0) {
111                        error = memstat_mtl_geterror(mtlp);
112                        if (error == MEMSTAT_ERROR_KVM)
113                                warnx("memstat_kvm_all: %s",
114                                    kvm_geterr(kvmd));
115                        else
116                                warnx("memstat_kvm_all: %s",
117                                    memstat_strerror(error));
118                        goto out;
119                }
120#else
121                ;
122#endif
123        }
124
125        mtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, MBUF_MEM_NAME);
126        if (mtp == NULL) {
127                warnx("memstat_mtl_find: zone %s not found", MBUF_MEM_NAME);
128                goto out;
129        }
130        mbuf_count = memstat_get_count(mtp);
131        mbuf_bytes = memstat_get_bytes(mtp);
132        mbuf_free = memstat_get_free(mtp);
133        mbuf_failures = memstat_get_failures(mtp);
134        mbuf_size = memstat_get_size(mtp);
135
136        mtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, MBUF_PACKET_MEM_NAME);
137        if (mtp == NULL) {
138                warnx("memstat_mtl_find: zone %s not found",
139                    MBUF_PACKET_MEM_NAME);
140                goto out;
141        }
142        packet_count = memstat_get_count(mtp);
143        packet_bytes = memstat_get_bytes(mtp);
144        packet_free = memstat_get_free(mtp);
145        packet_failures = memstat_get_failures(mtp);
146
147        mtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, MBUF_CLUSTER_MEM_NAME);
148        if (mtp == NULL) {
149                warnx("memstat_mtl_find: zone %s not found",
150                    MBUF_CLUSTER_MEM_NAME);
151                goto out;
152        }
153        cluster_count = memstat_get_count(mtp);
154        cluster_bytes = memstat_get_bytes(mtp);
155        cluster_limit = memstat_get_countlimit(mtp);
156        cluster_free = memstat_get_free(mtp);
157        cluster_failures = memstat_get_failures(mtp);
158        cluster_size = memstat_get_size(mtp);
159
160        mtp = memstat_mtl_find(mtlp, ALLOCATOR_MALLOC, MBUF_TAG_MEM_NAME);
161        if (mtp == NULL) {
162                warnx("memstat_mtl_find: malloc type %s not found",
163                    MBUF_TAG_MEM_NAME);
164                goto out;
165        }
166        tag_count = memstat_get_count(mtp);
167        tag_bytes = memstat_get_bytes(mtp);
168
169        mtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, MBUF_JUMBOP_MEM_NAME);
170        if (mtp == NULL) {
171                warnx("memstat_mtl_find: zone %s not found",
172                    MBUF_JUMBOP_MEM_NAME);
173                goto out;
174        }
175        jumbop_count = memstat_get_count(mtp);
176        jumbop_bytes = memstat_get_bytes(mtp);
177        jumbop_limit = memstat_get_countlimit(mtp);
178        jumbop_free = memstat_get_free(mtp);
179        jumbop_failures = memstat_get_failures(mtp);
180        jumbop_size = memstat_get_size(mtp);
181
182        mtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, MBUF_JUMBO9_MEM_NAME);
183        if (mtp == NULL) {
184                warnx("memstat_mtl_find: zone %s not found",
185                    MBUF_JUMBO9_MEM_NAME);
186                goto out;
187        }
188        jumbo9_count = memstat_get_count(mtp);
189        jumbo9_bytes = memstat_get_bytes(mtp);
190        jumbo9_limit = memstat_get_countlimit(mtp);
191        jumbo9_free = memstat_get_free(mtp);
192        jumbo9_failures = memstat_get_failures(mtp);
193        jumbo9_size = memstat_get_size(mtp);
194
195        mtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, MBUF_JUMBO16_MEM_NAME);
196        if (mtp == NULL) {
197                warnx("memstat_mtl_find: zone %s not found",
198                    MBUF_JUMBO16_MEM_NAME);
199                goto out;
200        }
201        jumbo16_count = memstat_get_count(mtp);
202        jumbo16_bytes = memstat_get_bytes(mtp);
203        jumbo16_limit = memstat_get_countlimit(mtp);
204        jumbo16_free = memstat_get_free(mtp);
205        jumbo16_failures = memstat_get_failures(mtp);
206        jumbo16_size = memstat_get_size(mtp);
207
208        printf("%ju/%ju/%ju mbufs in use (current/cache/total)\n",
209            mbuf_count + packet_count, mbuf_free + packet_free,
210            mbuf_count + packet_count + mbuf_free + packet_free);
211
212        printf("%ju/%ju/%ju/%ju mbuf clusters in use "
213            "(current/cache/total/max)\n",
214            cluster_count - packet_free, cluster_free + packet_free,
215            cluster_count + cluster_free, cluster_limit);
216
217        printf("%ju/%ju mbuf+clusters out of packet secondary zone in use "
218            "(current/cache)\n",
219            packet_count, packet_free);
220
221        printf("%ju/%ju/%ju/%ju %juk (page size) jumbo clusters in use "
222            "(current/cache/total/max)\n",
223            jumbop_count, jumbop_free, jumbop_count + jumbop_free,
224            jumbop_limit, jumbop_size / 1024);
225
226        printf("%ju/%ju/%ju/%ju 9k jumbo clusters in use "
227            "(current/cache/total/max)\n",
228            jumbo9_count, jumbo9_free, jumbo9_count + jumbo9_free,
229            jumbo9_limit);
230
231        printf("%ju/%ju/%ju/%ju 16k jumbo clusters in use "
232            "(current/cache/total/max)\n",
233            jumbo16_count, jumbo16_free, jumbo16_count + jumbo16_free,
234            jumbo16_limit);
235
236#if 0
237        printf("%ju mbuf tags in use\n", tag_count);
238#endif
239
240        /*-
241         * Calculate in-use bytes as:
242         * - straight mbuf memory
243         * - mbuf memory in packets
244         * - the clusters attached to packets
245         * - and the rest of the non-packet-attached clusters.
246         * - m_tag memory
247         * This avoids counting the clusters attached to packets in the cache.
248         * This currently excludes sf_buf space.
249         */
250        bytes_inuse =
251            mbuf_bytes +                        /* straight mbuf memory */
252            packet_bytes +                      /* mbufs in packets */
253            (packet_count * cluster_size) +     /* clusters in packets */
254            /* other clusters */
255            ((cluster_count - packet_count - packet_free) * cluster_size) +
256            tag_bytes +
257            (jumbop_count * jumbop_size) +      /* jumbo clusters */
258            (jumbo9_count * jumbo9_size) +
259            (jumbo16_count * jumbo16_size);
260
261        /*
262         * Calculate in-cache bytes as:
263         * - cached straught mbufs
264         * - cached packet mbufs
265         * - cached packet clusters
266         * - cached straight clusters
267         * This currently excludes sf_buf space.
268         */
269        bytes_incache =
270            (mbuf_free * mbuf_size) +           /* straight free mbufs */
271            (packet_free * mbuf_size) +         /* mbufs in free packets */
272            (packet_free * cluster_size) +      /* clusters in free packets */
273            (cluster_free * cluster_size) +     /* free clusters */
274            (jumbop_free * jumbop_size) +       /* jumbo clusters */
275            (jumbo9_free * jumbo9_size) +
276            (jumbo16_free * jumbo16_size);
277
278        /*
279         * Total is bytes in use + bytes in cache.  This doesn't take into
280         * account various other misc data structures, overhead, etc, but
281         * gives the user something useful despite that.
282         */
283        bytes_total = bytes_inuse + bytes_incache;
284
285        printf("%juK/%juK/%juK bytes allocated to network "
286            "(current/cache/total)\n", bytes_inuse / 1024,
287            bytes_incache / 1024, bytes_total / 1024);
288
289        printf("%ju/%ju/%ju requests for mbufs denied (mbufs/clusters/"
290            "mbuf+clusters)\n", mbuf_failures, cluster_failures,
291            packet_failures);
292
293        printf("%ju/%ju/%ju requests for jumbo clusters denied "
294            "(%juk/9k/16k)\n", jumbop_failures, jumbo9_failures,
295            jumbo16_failures, jumbop_size / 1024);
296
297        if (live) {
298                mlen = sizeof(nsfbufs);
299                if (!sysctlbyname("kern.ipc.nsfbufs", &nsfbufs, &mlen, NULL,
300                    0) &&
301                    !sysctlbyname("kern.ipc.nsfbufsused", &nsfbufsused,
302                    &mlen, NULL, 0) &&
303                    !sysctlbyname("kern.ipc.nsfbufspeak", &nsfbufspeak,
304                    &mlen, NULL, 0))
305                        printf("%d/%d/%d sfbufs in use (current/peak/max)\n",
306                            nsfbufsused, nsfbufspeak, nsfbufs);
307                mlen = sizeof(mbstat);
308                if (sysctlbyname("kern.ipc.mbstat", &mbstat, &mlen, NULL, 0)) {
309                        warn("kern.ipc.mbstat");
310                        goto out;
311                }
312        } else {
313                if (kread(mbaddr, (char *)&mbstat, sizeof mbstat) != 0)
314                        goto out;
315        }
316        printf("%lu requests for sfbufs denied\n", mbstat.sf_allocfail);
317        printf("%lu requests for sfbufs delayed\n", mbstat.sf_allocwait);
318        printf("%lu requests for I/O initiated by sendfile\n",
319            mbstat.sf_iocnt);
320        printf("%lu calls to protocol drain routines\n", mbstat.m_drain);
321out:
322        memstat_mtl_free(mtlp);
323}
Note: See TracBrowser for help on using the repository browser.