mbufs.c revision 63226
1/*- 2 * Copyright (c) 1980, 1992, 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 34#ifndef lint 35static char sccsid[] = "@(#)mbufs.c 8.1 (Berkeley) 6/6/93"; 36static const char rcsid[] = 37 "$FreeBSD: head/usr.bin/systat/mbufs.c 63226 2000-07-15 16:24:21Z alfred $"; 38#endif /* not lint */ 39 40#include <sys/param.h> 41#include <sys/types.h> 42#include <sys/mbuf.h> 43#include <sys/sysctl.h> 44 45#include <errno.h> 46#include <stdlib.h> 47#include <string.h> 48#include <paths.h> 49#include "systat.h" 50#include "extern.h" 51 52static struct mbstat *mb; 53static u_long *m_mbtypes; 54static int nmbtypes; 55 56static struct mtnames { 57 int mt_type; 58 char *mt_name; 59} mtnames[] = { 60 { MT_DATA, "data"}, 61 { MT_HEADER, "headers"}, 62 { MT_SONAME, "socknames"}, 63 { MT_FTABLE, "frags"}, 64 { MT_CONTROL, "control"}, 65 { MT_OOBDATA, "oobdata"} 66}; 67 68#define NNAMES (sizeof (mtnames) / sizeof (mtnames[0])) 69 70WINDOW * 71openmbufs() 72{ 73 return (subwin(stdscr, LINES-5-1, 0, 5, 0)); 74} 75 76void 77closembufs(w) 78 WINDOW *w; 79{ 80 if (w == NULL) 81 return; 82 wclear(w); 83 wrefresh(w); 84 delwin(w); 85} 86 87void 88labelmbufs() 89{ 90 wmove(wnd, 0, 0); wclrtoeol(wnd); 91 mvwaddstr(wnd, 0, 10, 92 "/0 /5 /10 /15 /20 /25 /30 /35 /40 /45 /50 /55 /60"); 93} 94 95void 96showmbufs() 97{ 98 register int i, j, max, index; 99 char buf[10]; 100 char *mtname; 101 102 if (mb == 0) 103 return; 104 for (j = 0; j < wnd->_maxy; j++) { 105 max = 0, index = -1; 106 for (i = 0; i < wnd->_maxy; i++) { 107 if (i == MT_FREE) 108 continue; 109 if (i >= nmbtypes) 110 break; 111 if (m_mbtypes[i] > max) { 112 max = m_mbtypes[i]; 113 index = i; 114 } 115 } 116 if (max == 0) 117 break; 118 119 mtname = NULL; 120 for (i = 0; i < NNAMES; i++) 121 if (mtnames[i].mt_type == index) 122 mtname = mtnames[i].mt_name; 123 if (mtname == NULL) 124 mvwprintw(wnd, 1+j, 0, "%10d", index); 125 else 126 mvwprintw(wnd, 1+j, 0, "%-10.10s", mtname); 127 wmove(wnd, 1 + j, 10); 128 if (max > 60) { 129 snprintf(buf, sizeof(buf), " %d", max); 130 max = 60; 131 while (max--) 132 waddch(wnd, 'X'); 133 waddstr(wnd, buf); 134 } else 135 while (max--) 136 waddch(wnd, 'X'); 137 wclrtoeol(wnd); 138 mb->m_mbufs -= m_mbtypes[index]; 139 m_mbtypes[index] = 0; 140 } 141 if (mb->m_mbufs) { 142 mvwprintw(wnd, 1+j, 0, "%-10.10s", "free"); 143 if (mb->m_mbufs > 60) { 144 snprintf(buf, sizeof(buf), " %ld", mb->m_mbufs); 145 mb->m_mbufs = 60; 146 while (mb->m_mbufs--) 147 waddch(wnd, 'X'); 148 waddstr(wnd, buf); 149 } else { 150 while(mb->m_mbufs--) 151 waddch(wnd, 'X'); 152 } 153 wclrtoeol(wnd); 154 j++; 155 } 156 wmove(wnd, 1+j, 0); wclrtobot(wnd); 157} 158 159int 160initmbufs() 161{ 162 size_t len, mbtypeslen; 163 164 if (sysctlbyname("kern.ipc.mbtypes", NULL, &mbtypeslen, NULL, 0) < 0) { 165 error("sysctl getting mbtypes size failed"); 166 return 0; 167 } 168 if ((m_mbtypes = calloc(1, mbtypeslen)) == NULL) { 169 error("calloc mbtypes failed"); 170 return 0; 171 } 172 nmbtypes = mbtypeslen / sizeof(*m_mbtypes); 173 174 len = 0; 175 if (sysctlbyname("kern.ipc.mbstat", 0, &len, 0, 0) < 0) { 176 error("sysctl getting mbstat size failed"); 177 return 0; 178 } 179 180 if (mb == 0) 181 mb = (struct mbstat *)calloc(1, sizeof *mb); 182 return 1; 183} 184 185void 186fetchmbufs() 187{ 188 size_t len; 189 190 len = sizeof *mb; 191 if (sysctlbyname("kern.ipc.mbstat", mb, &len, 0, 0) < 0) 192 printw("sysctl: mbstat: %s", strerror(errno)); 193 194 len = nmbtypes * sizeof *m_mbtypes; 195 if (sysctlbyname("kern.ipc.mbtypes", m_mbtypes, &len, 0, 0) < 0) 196 printw("sysctl: mbtypes: %s", strerror(errno)); 197} 198