Annotation of src/usr.bin/systat/pigs.c, Revision 1.7
1.7 ! ericj 1: /* $OpenBSD: pigs.c,v 1.6 2000/06/18 17:59:55 niklas Exp $ */
1.1 deraadt 2: /* $NetBSD: pigs.c,v 1.3 1995/04/29 05:54:50 cgd 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.
16: * 3. All advertising materials mentioning features or use of this software
17: * must display the following acknowledgement:
18: * This product includes software developed by the University of
19: * California, Berkeley and its contributors.
20: * 4. Neither the name of the University nor the names of its contributors
21: * may be used to endorse or promote products derived from this software
22: * without specific prior written permission.
23: *
24: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34: * SUCH DAMAGE.
35: */
36:
37: #ifndef lint
38: #if 0
39: static char sccsid[] = "@(#)pigs.c 8.2 (Berkeley) 9/23/93";
40: #endif
1.7 ! ericj 41: static char rcsid[] = "$OpenBSD: pigs.c,v 1.6 2000/06/18 17:59:55 niklas Exp $";
1.1 deraadt 42: #endif /* not lint */
43:
44: /*
45: * Pigs display from Bill Reeves at Lucasfilm
46: */
47:
48: #include <sys/param.h>
49: #include <sys/dkstat.h>
50: #include <sys/dir.h>
51: #include <sys/time.h>
52: #include <sys/proc.h>
53: #include <sys/sysctl.h>
54:
55: #include <curses.h>
56: #include <math.h>
57: #include <nlist.h>
58: #include <pwd.h>
59: #include <stdlib.h>
60: #include <string.h>
61:
62: #include "extern.h"
63: #include "systat.h"
64:
65: int compar __P((const void *, const void *));
66:
67: static int nproc;
68: static struct p_times {
69: float pt_pctcpu;
70: struct kinfo_proc *pt_kp;
71: } *pt;
72:
73: static long stime[CPUSTATES];
74: static int fscale;
75: static double lccpu;
76:
77: WINDOW *
78: openpigs()
79: {
80: return (subwin(stdscr, LINES-5-1, 0, 5, 0));
81: }
82:
83: void
84: closepigs(w)
85: WINDOW *w;
86: {
87: if (w == NULL)
88: return;
89: wclear(w);
90: wrefresh(w);
91: delwin(w);
92: }
93:
94:
95: void
96: showpigs()
97: {
98: register int i, j, y, k;
99: struct eproc *ep;
100: float total;
101: int factor;
102: char *uname, *pname, pidname[30];
103:
104: if (pt == NULL)
105: return;
106: /* Accumulate the percent of cpu per user. */
107: total = 0.0;
108: for (i = 0; i <= nproc; i++) {
109: /* Accumulate the percentage. */
110: total += pt[i].pt_pctcpu;
111: }
112:
113: if (total < 1.0)
114: total = 1.0;
115: factor = 50.0/total;
116:
117: qsort(pt, nproc + 1, sizeof (struct p_times), compar);
118: y = 1;
119: i = nproc + 1;
1.2 mickey 120: if (i > wnd->_maxy-1)
121: i = wnd->_maxy-1;
1.1 deraadt 122: for (k = 0; i > 0 && pt[k].pt_pctcpu > 0.01; i--, y++, k++) {
123: if (pt[k].pt_kp == NULL) {
124: uname = "";
125: pname = "<idle>";
126: }
127: else {
128: ep = &pt[k].pt_kp->kp_eproc;
129: uname = user_from_uid(ep->e_ucred.cr_uid, 0);
130: pname = pt[k].pt_kp->kp_proc.p_comm;
131: }
132: wmove(wnd, y, 0);
133: wclrtoeol(wnd);
134: mvwaddstr(wnd, y, 0, uname);
1.5 deraadt 135: snprintf(pidname, sizeof pidname, "%10.10s", pname);
1.1 deraadt 136: mvwaddstr(wnd, y, 9, pidname);
137: wmove(wnd, y, 20);
138: for (j = pt[k].pt_pctcpu*factor + 0.5; j > 0; j--)
139: waddch(wnd, 'X');
140: }
141: wmove(wnd, y, 0); wclrtobot(wnd);
142: }
143:
144: static struct nlist namelist[] = {
145: #define X_FIRST 0
146: #define X_CPTIME 0
147: { "_cp_time" },
148: #define X_CCPU 1
149: { "_ccpu" },
150: #define X_FSCALE 2
151: { "_fscale" },
152:
153: { "" }
154: };
155:
156: int
157: initpigs()
158: {
159: fixpt_t ccpu;
1.7 ! ericj 160: int ret;
1.1 deraadt 161:
162: if (namelist[X_FIRST].n_type == 0) {
1.7 ! ericj 163: if ((ret = kvm_nlist(kd, namelist)) == -1)
! 164: errx(1, "%s", kvm_geterr(kd));
! 165: else if (ret)
1.1 deraadt 166: nlisterr(namelist);
167: if (namelist[X_FIRST].n_type == 0) {
168: error("namelist failed");
169: return(0);
170: }
171: }
172: KREAD(NPTR(X_CPTIME), stime, sizeof (stime));
173: NREAD(X_CCPU, &ccpu, LONG);
174: NREAD(X_FSCALE, &fscale, LONG);
175: lccpu = log((double) ccpu / fscale);
176:
177: return(1);
178: }
179:
180: void
181: fetchpigs()
182: {
183: register int i;
184: register float time;
185: register struct proc *pp;
186: register float *pctp;
187: struct kinfo_proc *kpp;
188: long ctime[CPUSTATES];
189: double t;
190: static int lastnproc = 0;
191:
192: if (namelist[X_FIRST].n_type == 0)
193: return;
1.6 niklas 194: if ((kpp = kvm_getprocs(kd, KERN_PROC_KTHREAD, 0, &nproc)) == NULL) {
1.1 deraadt 195: error("%s", kvm_geterr(kd));
196: if (pt)
197: free(pt);
198: return;
199: }
200: if (nproc > lastnproc) {
201: free(pt);
202: if ((pt =
203: malloc((nproc + 1) * sizeof(struct p_times))) == NULL) {
204: error("Out of memory");
205: die(0);
206: }
207: }
208: lastnproc = nproc;
209: /*
210: * calculate %cpu for each proc
211: */
212: for (i = 0; i < nproc; i++) {
213: pt[i].pt_kp = &kpp[i];
214: pp = &kpp[i].kp_proc;
215: pctp = &pt[i].pt_pctcpu;
216: time = pp->p_swtime;
217: if (time == 0 || (pp->p_flag & P_INMEM) == 0)
218: *pctp = 0;
219: else
220: *pctp = ((double) pp->p_pctcpu /
221: fscale) / (1.0 - exp(time * lccpu));
222: }
223: /*
224: * and for the imaginary "idle" process
225: */
226: KREAD(NPTR(X_CPTIME), ctime, sizeof (ctime));
227: t = 0;
228: for (i = 0; i < CPUSTATES; i++)
229: t += ctime[i] - stime[i];
230: if (t == 0.0)
231: t = 1.0;
232: pt[nproc].pt_kp = NULL;
233: pt[nproc].pt_pctcpu = (ctime[CP_IDLE] - stime[CP_IDLE]) / t;
234: for (i = 0; i < CPUSTATES; i++)
235: stime[i] = ctime[i];
236: }
237:
238: void
239: labelpigs()
240: {
241: wmove(wnd, 0, 0);
242: wclrtoeol(wnd);
243: mvwaddstr(wnd, 0, 20,
244: "/0 /10 /20 /30 /40 /50 /60 /70 /80 /90 /100");
245: }
246:
247: int
248: compar(a, b)
249: const void *a, *b;
250: {
251: return (((struct p_times *) a)->pt_pctcpu >
252: ((struct p_times *) b)->pt_pctcpu)? -1: 1;
253: }