867e11c883260f5b950b5401f263fd11fb06f166
[dragonfly.git] / usr.bin / netstat / mbuf.c
1 /*
2  * Copyright (c) 1983, 1988, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * @(#)mbuf.c   8.1 (Berkeley) 6/6/93
34  * $FreeBSD: src/usr.bin/netstat/mbuf.c,v 1.17.2.3 2001/08/10 09:07:09 ru Exp $
35  * $DragonFly: src/usr.bin/netstat/mbuf.c,v 1.7 2006/10/09 22:30:48 hsu Exp $
36  */
37
38 #define _KERNEL_STRUCTURES
39 #include <sys/param.h>
40 #include <sys/mbuf.h>
41 #include <sys/protosw.h>
42 #include <sys/socket.h>
43 #include <sys/sysctl.h>
44
45 #include <err.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include "netstat.h"
49
50 #define YES     1
51 typedef int bool;
52
53 static struct mbtypenames {
54         int     mt_type;
55         const char *mt_name;
56 } mbtypenames[] = {
57         { MT_DATA,      "data" },
58         { MT_OOBDATA,   "oob data" },
59         { MT_CONTROL,   "ancillary data" },
60         { MT_HEADER,    "packet headers" },
61         { MT_SONAME,    "socket names and addresses" },
62         { 0, 0 }
63 };
64
65 /*
66  * Print mbuf statistics.
67  */
68 void
69 mbpr(u_long mbaddr, u_long mbtaddr, u_long nmbcaddr, u_long nmbufaddr,
70      u_long ncpusaddr)
71 {
72         u_long totmem, totpossible;
73         struct mbstat *mbstat;
74         struct mbtypenames *mp;
75         int name[3], nmbclusters, nmbufs, nmbtypes;
76         size_t nmbclen, nmbuflen, mbstatlen, mbtypeslen;
77         u_long *mbtypes;
78         int ncpus;
79         int n;
80         int i;
81         bool *seen;     /* "have we seen this type yet?" */
82
83         mbtypes = NULL;
84         seen = NULL;
85         mbstat = NULL;
86
87         /*
88          * XXX
89          * We can't kread() mbtypeslen from a core image so we'll
90          * bogusly assume it's the same as in the running kernel.
91          */
92         if (sysctlbyname("kern.ipc.mbtypes", NULL, &mbtypeslen, NULL, 0) < 0) {
93                 warn("sysctl: retrieving mbtypes length");
94                 goto err;
95         }
96         nmbtypes = mbtypeslen / sizeof(*mbtypes);
97         if ((seen = calloc(nmbtypes, sizeof(*seen))) == NULL) {
98                 warn("calloc");
99                 goto err;
100         }
101
102         if (mbaddr) {
103                 if (kread(ncpusaddr, (char *)&ncpus, sizeof ncpus))
104                         goto err;
105                 mbstat = malloc(sizeof(*mbstat) * ncpus);
106                 if (kread(mbaddr, (char *)mbstat, sizeof *mbstat * ncpus))
107                         goto err;
108                 mbtypes = malloc(mbtypeslen * ncpus);
109                 if (kread(mbtaddr, (char *)mbtypes, mbtypeslen * ncpus))
110                         goto err;
111                 if (kread(nmbcaddr, (char *)&nmbclusters, sizeof(int)))
112                         goto err;
113                 if (kread(nmbufaddr, (char *)&nmbufs, sizeof(int)))
114                         goto err;
115                 for (n = 1; n < ncpus; ++n) {
116                         mbstat[0].m_mbufs += mbstat[n].m_mbufs;
117                         mbstat[0].m_clusters += mbstat[n].m_clusters;
118                         mbstat[0].m_drops += mbstat[n].m_drops;
119                         mbstat[0].m_wait += mbstat[n].m_wait;
120                         mbstat[0].m_drain += mbstat[n].m_drain;
121
122                         for (i = 0; i < nmbtypes; ++i) {
123                                 mbtypes[i] += mbtypes[n * nmbtypes + i];
124                         }
125                 }
126         } else {
127                 name[0] = CTL_KERN;
128                 name[1] = KERN_IPC;
129                 name[2] = KIPC_MBSTAT;
130                 mbstat = malloc(sizeof(*mbstat));
131                 mbstatlen = sizeof *mbstat;
132                 /* fake ncpus, kernel will aggregate mbstat array for us */
133                 if (sysctl(name, 3, mbstat, &mbstatlen, 0, 0) < 0) {
134                         warn("sysctl: retrieving mbstat");
135                         goto err;
136                 }
137
138                 mbtypes = malloc(mbtypeslen);
139                 if (sysctlbyname("kern.ipc.mbtypes", mbtypes, &mbtypeslen, NULL,
140                     0) < 0) {
141                         warn("sysctl: retrieving mbtypes");
142                         goto err;
143                 }
144                 
145                 name[2] = KIPC_NMBCLUSTERS;
146                 nmbclen = sizeof(int);
147                 if (sysctl(name, 3, &nmbclusters, &nmbclen, 0, 0) < 0) {
148                         warn("sysctl: retrieving nmbclusters");
149                         goto err;
150                 }
151
152                 nmbuflen = sizeof(int);
153                 if (sysctlbyname("kern.ipc.nmbufs", &nmbufs, &nmbuflen, 0, 0) < 0) {
154                         warn("sysctl: retrieving nmbufs");
155                         goto err;
156                 }
157         }
158
159 #undef MSIZE
160 #define MSIZE           (mbstat->m_msize)
161 #undef MCLBYTES
162 #define MCLBYTES        (mbstat->m_mclbytes)
163
164         printf("%lu/%u mbufs in use (current/max):\n", mbstat->m_mbufs, nmbufs);
165         printf("%lu/%u mbuf clusters in use (current/max)\n",
166                 mbstat->m_clusters, nmbclusters);
167         for (mp = mbtypenames; mp->mt_name; mp++)
168                 if (mbtypes[mp->mt_type]) {
169                         seen[mp->mt_type] = YES;
170                         printf("\t%lu mbufs and mbuf clusters "
171                                "allocated to %s\n",
172                                mbtypes[mp->mt_type], mp->mt_name);
173                 }
174         seen[MT_FREE] = YES;
175         for (i = 0; i < nmbtypes; i++)
176                 if (!seen[i] && mbtypes[i]) {
177                         printf("\t%lu mbufs and mbuf clusters allocated to <mbuf type %d>\n",
178                             mbtypes[i], i);
179                 }
180         totmem = mbstat->m_mbufs * MSIZE + mbstat->m_clusters * MCLBYTES;
181         totpossible = nmbclusters * MCLBYTES + MSIZE * nmbufs; 
182         printf("%lu Kbytes allocated to network (%lu%% of mb_map in use)\n",
183                 totmem / 1024, (totmem * 100) / totpossible);
184         printf("%lu requests for memory denied\n", mbstat->m_drops);
185         printf("%lu requests for memory delayed\n", mbstat->m_wait);
186         printf("%lu calls to protocol drain routines\n", mbstat->m_drain);
187
188 err:
189         if (mbstat != NULL)
190                 free(mbstat);
191         if (mbtypes != NULL)
192                 free(mbtypes);
193         if (seen != NULL)
194                 free(seen);
195 }