mbufs.c revision 23087
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 "$Id$"; 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 <stdlib.h> 46#include <string.h> 47#include <paths.h> 48#include "systat.h" 49#include "extern.h" 50 51static struct mbstat *mb; 52 53char *mtnames[] = { 54 "free", 55 "data", 56 "headers", 57 "sockets", 58 "pcbs", 59 "routes", 60 "hosts", 61 "arps", 62 "socknames", 63 "zombies", 64 "sockopts", 65 "frags", 66 "rights", 67 "ifaddrs", 68}; 69 70#define NNAMES (sizeof (mtnames) / sizeof (mtnames[0])) 71 72WINDOW * 73openmbufs() 74{ 75 return (subwin(stdscr, LINES-5-1, 0, 5, 0)); 76} 77 78void 79closembufs(w) 80 WINDOW *w; 81{ 82 if (w == NULL) 83 return; 84 wclear(w); 85 wrefresh(w); 86 delwin(w); 87} 88 89void 90labelmbufs() 91{ 92 wmove(wnd, 0, 0); wclrtoeol(wnd); 93 mvwaddstr(wnd, 0, 10, 94 "/0 /5 /10 /15 /20 /25 /30 /35 /40 /45 /50 /55 /60"); 95} 96 97void 98showmbufs() 99{ 100 register int i, j, max, index; 101 char buf[10]; 102 103 if (mb == 0) 104 return; 105 for (j = 0; j < wnd->maxy; j++) { 106 max = 0, index = -1; 107 for (i = 0; i < wnd->maxy; i++) { 108 if (i == MT_FREE) 109 continue; 110 if (mb->m_mtypes[i] > max) { 111 max = mb->m_mtypes[i]; 112 index = i; 113 } 114 } 115 if (max == 0) 116 break; 117 if (j > NNAMES) 118 mvwprintw(wnd, 1+j, 0, "%10d", index); 119 else 120 mvwprintw(wnd, 1+j, 0, "%-10.10s", mtnames[index]); 121 wmove(wnd, 1 + j, 10); 122 if (max > 60) { 123 sprintf(buf, " %d", max); 124 max = 60; 125 while (max--) 126 waddch(wnd, 'X'); 127 waddstr(wnd, buf); 128 } else 129 while (max--) 130 waddch(wnd, 'X'); 131 wclrtoeol(wnd); 132 mb->m_mbufs -= mb->m_mtypes[index]; 133 mb->m_mtypes[index] = 0; 134 } 135 if (mb->m_mbufs) { 136 mvwprintw(wnd, 1+j, 0, "%-10.10s", "free"); 137 if (mb->m_mbufs > 60) { 138 sprintf(buf, " %d", mb->m_mbufs); 139 mb->m_mbufs = 60; 140 while (mb->m_mbufs--) 141 waddch(wnd, 'X'); 142 waddstr(wnd, buf); 143 } else { 144 while(mb->m_mbufs--) 145 waddch(wnd, 'X'); 146 } 147 wclrtoeol(wnd); 148 j++; 149 } 150 wmove(wnd, 1+j, 0); wclrtobot(wnd); 151} 152 153int 154initmbufs() 155{ 156 size_t len; 157 int name[3]; 158 159 name[0] = CTL_KERN; 160 name[1] = KERN_IPC; 161 name[2] = KIPC_MBSTAT; 162 len = 0; 163 if (sysctl(name, 3, 0, &len, 0, 0) < 0) { 164 error("sysctl getting mbstat size failed"); 165 return 0; 166 } 167 168 if (mb == 0) 169 mb = (struct mbstat *)calloc(1, sizeof *mb); 170 return 1; 171} 172 173void 174fetchmbufs() 175{ 176 int name[3]; 177 size_t len; 178 179 name[0] = CTL_KERN; 180 name[1] = KERN_IPC; 181 name[2] = KIPC_MBSTAT; 182 len = sizeof *mb; 183 184 if (sysctl(name, 3, mb, &len, 0, 0) < 0) 185 return; 186} 187