Annotation of src/usr.bin/systat/mbufs.c, Revision 1.18
1.18 ! canacar 1: /* $OpenBSD: mbufs.c,v 1.17 2007/02/25 18:21:24 deraadt Exp $ */
1.1 deraadt 2: /* $NetBSD: mbufs.c,v 1.2 1995/01/20 08:52:02 jtc Exp $ */
3:
4: /*-
5: * Copyright (c) 1980, 1992, 1993
6: * The Regents of the University of California. All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
1.13 millert 16: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 17: * may be used to endorse or promote products derived from this software
18: * without specific prior written permission.
19: *
20: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30: * SUCH DAMAGE.
31: */
32:
33: #include <sys/param.h>
34: #include <sys/types.h>
35: #include <sys/mbuf.h>
1.10 angelos 36: #include <sys/sysctl.h>
1.1 deraadt 37:
38: #include <stdlib.h>
39: #include <string.h>
1.9 deraadt 40: #include <err.h>
1.1 deraadt 41: #include <paths.h>
42: #include "systat.h"
1.18 ! canacar 43:
! 44:
! 45: void print_mb(void);
! 46: int read_mb(void);
! 47: int select_mb(void);
! 48: static void showmbuf(int);
1.1 deraadt 49:
1.15 deraadt 50: static struct mbstat mb;
1.1 deraadt 51:
52: char *mtnames[] = {
53: "free",
54: "data",
55: "headers",
56: "sockets",
57: "pcbs",
58: "routes",
59: "hosts",
60: "arps",
61: "socknames",
62: "zombies",
63: "sockopts",
64: "frags",
65: "rights",
66: "ifaddrs",
67: };
68:
1.18 ! canacar 69: #define NUM_TYPES (sizeof(mb.m_mtypes) / sizeof(mb.m_mtypes[0]))
1.1 deraadt 70: #define NNAMES (sizeof (mtnames) / sizeof (mtnames[0]))
71:
1.18 ! canacar 72: int mb_index[NUM_TYPES];
! 73: int mbuf_cnt = 0;
! 74:
! 75:
! 76: field_def fields_mb[] = {
! 77: {"TYPE", 6, 16, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0},
! 78: {"VALUE", 5, 8, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0},
! 79: {"", 40, 80, 1, FLD_ALIGN_BAR, -1, 0, 0, 60},
! 80: };
! 81:
! 82: #define FIELD_ADDR(x) (&fields_mb[x])
! 83:
! 84: #define FLD_MB_NAME FIELD_ADDR(0)
! 85: #define FLD_MB_VALUE FIELD_ADDR(1)
! 86: #define FLD_MB_BAR FIELD_ADDR(2)
! 87:
! 88: /* Define views */
! 89: field_def *view_mb_0[] = {
! 90: FLD_MB_NAME, FLD_MB_VALUE, FLD_MB_BAR, NULL
! 91: };
! 92:
! 93:
! 94: /* Define view managers */
! 95: struct view_manager mbuf_mgr = {
! 96: "Mbufs", select_mb, read_mb, NULL, print_header,
! 97: print_mb, keyboard_callback, NULL, NULL
! 98: };
! 99:
! 100: field_view views_mb[] = {
! 101: {view_mb_0, "mbufs", '4', &mbuf_mgr},
! 102: {NULL, NULL, 0, NULL}
! 103: };
! 104:
! 105:
! 106: int
! 107: select_mb(void)
1.1 deraadt 108: {
1.18 ! canacar 109: int i, w = 50;
! 110:
! 111: read_mb();
! 112: for (i = 0; i < NUM_TYPES; i++)
! 113: if (w < (5 * mb.m_mtypes[i] / 4))
! 114: w = 5 * mb.m_mtypes[i] / 4;
! 115:
! 116: w -= w % 10;
! 117: FLD_MB_BAR->arg = w;
! 118:
! 119: return (0);
1.1 deraadt 120: }
121:
1.18 ! canacar 122: int
! 123: read_mb(void)
1.1 deraadt 124: {
1.18 ! canacar 125: int mib[2], i;
! 126: size_t size = sizeof (mb);
! 127:
! 128: mib[0] = CTL_KERN;
! 129: mib[1] = KERN_MBSTAT;
! 130:
! 131: if (sysctl(mib, 2, &mb, &size, NULL, 0) < 0) {
! 132: error("sysctl(KERN_MBSTAT) failed");
! 133: return 1;
! 134: }
! 135:
! 136: mbuf_cnt = 0;
! 137: memset(mb_index, 0, sizeof(mb_index));
! 138:
! 139: for (i = 0; i < NUM_TYPES; i++) {
! 140: if (mb.m_mtypes[i])
! 141: mb_index[mbuf_cnt++] = i;
! 142: }
! 143:
! 144: num_disp = mbuf_cnt;
! 145:
! 146: return 0;
1.1 deraadt 147: }
148:
149:
150: void
1.18 ! canacar 151: print_mb(void)
1.1 deraadt 152: {
1.18 ! canacar 153: int n, count = 0;
1.1 deraadt 154:
1.18 ! canacar 155: for (n = dispstart; n < num_disp; n++) {
! 156: showmbuf(n);
! 157: count++;
! 158: if (maxprint > 0 && count >= maxprint)
1.1 deraadt 159: break;
160: }
161: }
162:
1.18 ! canacar 163: int
! 164: initmembufs(void)
! 165: {
! 166: field_view *v;
1.1 deraadt 167:
1.18 ! canacar 168: for (v = views_mb; v->name != NULL; v++)
! 169: add_view(v);
1.5 ericj 170:
1.18 ! canacar 171: return(1);
1.1 deraadt 172: }
173:
1.18 ! canacar 174:
! 175: static void
! 176: showmbuf(int m)
1.1 deraadt 177: {
1.18 ! canacar 178: int i;
! 179:
! 180: i = mb_index[m];
! 181:
! 182: if (i < NNAMES)
! 183: print_fld_str(FLD_MB_NAME, mtnames[i]);
! 184: else
! 185: print_fld_uint(FLD_MB_NAME, i);
! 186:
! 187: print_fld_uint(FLD_MB_VALUE, mb.m_mtypes[i]);
! 188: print_fld_bar(FLD_MB_BAR, mb.m_mtypes[i]);
! 189:
! 190: end_line();
1.1 deraadt 191: }
1.18 ! canacar 192:
! 193: