1 /*        $NetBSD: mbufs.c,v 1.16 2012/11/23 01:43:10 christos Exp $  */
2 
3 /*-
4  * Copyright (c) 1980, 1992, 1993
5  *        The Regents of the University of California.  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. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)mbufs.c     8.1 (Berkeley) 6/6/93";
36 #endif
37 __RCSID("$NetBSD: mbufs.c,v 1.16 2012/11/23 01:43:10 christos Exp $");
38 #endif /* not lint */
39 
40 #include <sys/param.h>
41 #define MBUFTYPES
42 #include <sys/mbuf.h>
43 #include <sys/sysctl.h>
44 
45 #include <stdlib.h>
46 #include <errno.h>
47 #include <string.h>
48 
49 #include "systat.h"
50 #include "extern.h"
51 
52 static struct mbstat *mb;
53 
54 #define   NNAMES    __arraycount(mbuftypes)
55 
56 WINDOW *
openmbufs(void)57 openmbufs(void)
58 {
59 
60           return (subwin(stdscr, -1, 0, 5, 0));
61 }
62 
63 void
closembufs(WINDOW * w)64 closembufs(WINDOW *w)
65 {
66 
67           if (w == NULL)
68                     return;
69           wclear(w);
70           wrefresh(w);
71           delwin(w);
72 }
73 
74 void
labelmbufs(void)75 labelmbufs(void)
76 {
77 
78           wmove(wnd, 0, 0); wclrtoeol(wnd);
79           mvwaddstr(wnd, 0, 10,
80               "/0   /5   /10  /15  /20  /25  /30  /35  /40  /45  /50  /55  /60");
81 }
82 
83 void
showmbufs(void)84 showmbufs(void)
85 {
86           int i, j, max, idx;
87           char buf[10];
88 
89           if (mb == 0)
90                     return;
91           for (j = 1; j <= getmaxy(wnd); j++) {
92                     max = 0, idx = -1;
93                     for (i = 0; i < getmaxy(wnd); i++)
94                               if (mb->m_mtypes[i] > max) {
95                                         max = mb->m_mtypes[i];
96                                         idx = i;
97                               }
98                     if (max == 0)
99                               break;
100                     if (j >= (int)NNAMES)
101                               mvwprintw(wnd, j, 0, "%10d", idx);
102                     else
103                               mvwprintw(wnd, j, 0, "%-10.10s", &mbuftypes[idx][2]);
104                     wmove(wnd, j, 10);
105                     if (max > 60) {
106                               snprintf(buf, sizeof buf, " %5d", max);
107                               max = 60;
108                               while (max--)
109                                         waddch(wnd, 'X');
110                               waddstr(wnd, buf);
111                     } else {
112                               wclrtoeol(wnd);
113                               whline(wnd, 'X', max);
114                     }
115                     mb->m_mtypes[idx] = 0;
116           }
117           wmove(wnd, j, 0);
118           wclrtobot(wnd);
119 }
120 
121 int
initmbufs(void)122 initmbufs(void)
123 {
124           if (mb == 0)
125                     mb = calloc(1, sizeof (*mb));
126           return(1);
127 }
128 
129 void
fetchmbufs(void)130 fetchmbufs(void)
131 {
132           size_t len = sizeof(*mb);
133           if (sysctlbyname("kern.mbuf.stats", mb, &len, NULL, 0))
134                     error("error getting \"kern.mbuf.stats\": %s", strerror(errno));
135 }
136