xref: /original-bsd/usr.bin/systat/mbufs.c (revision c3e32dec)
1 /*-
2  * Copyright (c) 1980, 1992, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)mbufs.c	8.1 (Berkeley) 06/06/93";
10 #endif /* not lint */
11 
12 #include <sys/param.h>
13 #include <sys/types.h>
14 #include <sys/mbuf.h>
15 
16 #include <stdlib.h>
17 #include <string.h>
18 #include <nlist.h>
19 #include <paths.h>
20 #include "systat.h"
21 #include "extern.h"
22 
23 static struct mbstat *mb;
24 
25 char *mtnames[] = {
26 	"free",
27 	"data",
28 	"headers",
29 	"sockets",
30 	"pcbs",
31 	"routes",
32 	"hosts",
33 	"arps",
34 	"socknames",
35 	"zombies",
36 	"sockopts",
37 	"frags",
38 	"rights",
39 	"ifaddrs",
40 };
41 
42 #define	NNAMES	(sizeof (mtnames) / sizeof (mtnames[0]))
43 
44 WINDOW *
45 openmbufs()
46 {
47 	return (subwin(stdscr, LINES-5-1, 0, 5, 0));
48 }
49 
50 void
51 closembufs(w)
52 	WINDOW *w;
53 {
54 	if (w == NULL)
55 		return;
56 	wclear(w);
57 	wrefresh(w);
58 	delwin(w);
59 }
60 
61 void
62 labelmbufs()
63 {
64 	wmove(wnd, 0, 0); wclrtoeol(wnd);
65 	mvwaddstr(wnd, 0, 10,
66 	    "/0   /5   /10  /15  /20  /25  /30  /35  /40  /45  /50  /55  /60");
67 }
68 
69 void
70 showmbufs()
71 {
72 	register int i, j, max, index;
73 	char buf[10];
74 
75 	if (mb == 0)
76 		return;
77 	for (j = 0; j < wnd->maxy; j++) {
78 		max = 0, index = -1;
79 		for (i = 0; i < wnd->maxy; i++)
80 			if (mb->m_mtypes[i] > max) {
81 				max = mb->m_mtypes[i];
82 				index = i;
83 			}
84 		if (max == 0)
85 			break;
86 		if (j > NNAMES)
87 			mvwprintw(wnd, 1+j, 0, "%10d", index);
88 		else
89 			mvwprintw(wnd, 1+j, 0, "%-10.10s", mtnames[index]);
90 		wmove(wnd, 1 + j, 10);
91 		if (max > 60) {
92 			sprintf(buf, " %d", max);
93 			max = 60;
94 			while (max--)
95 				waddch(wnd, 'X');
96 			waddstr(wnd, buf);
97 		} else {
98 			while (max--)
99 				waddch(wnd, 'X');
100 			wclrtoeol(wnd);
101 		}
102 		mb->m_mtypes[index] = 0;
103 	}
104 	wmove(wnd, 1+j, 0); wclrtobot(wnd);
105 }
106 
107 static struct nlist namelist[] = {
108 #define	X_MBSTAT	0
109 	{ "_mbstat" },
110 	{ "" }
111 };
112 
113 int
114 initmbufs()
115 {
116 	if (namelist[X_MBSTAT].n_type == 0) {
117 		if (kvm_nlist(kd, namelist)) {
118 			nlisterr(namelist);
119 			return(0);
120 		}
121 		if (namelist[X_MBSTAT].n_type == 0) {
122 			error("namelist on %s failed", _PATH_UNIX);
123 			return(0);
124 		}
125 	}
126 	if (mb == 0)
127 		mb = (struct mbstat *)calloc(1, sizeof (*mb));
128 	return(1);
129 }
130 
131 void
132 fetchmbufs()
133 {
134 	if (namelist[X_MBSTAT].n_type == 0)
135 		return;
136 	NREAD(X_MBSTAT, mb, sizeof (*mb));
137 }
138