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