Annotation of src/usr.bin/systat/pigs.c, Revision 1.8
1.8 ! pvalchev 1: /* $OpenBSD: pigs.c,v 1.7 2001/05/04 16:48:34 ericj 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.8 ! pvalchev 41: static char rcsid[] = "$OpenBSD: pigs.c,v 1.7 2001/05/04 16:48:34 ericj 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>
1.8 ! pvalchev 59: #include <err.h>
1.1 deraadt 60: #include <stdlib.h>
61: #include <string.h>
62:
63: #include "extern.h"
64: #include "systat.h"
65:
66: int compar __P((const void *, const void *));
67:
68: static int nproc;
69: static struct p_times {
70: float pt_pctcpu;
71: struct kinfo_proc *pt_kp;
72: } *pt;
73:
74: static long stime[CPUSTATES];
75: static int fscale;
76: static double lccpu;
77:
78: WINDOW *
79: openpigs()
80: {
81: return (subwin(stdscr, LINES-5-1, 0, 5, 0));
82: }
83:
84: void
85: closepigs(w)
86: WINDOW *w;
87: {
88: if (w == NULL)
89: return;
90: wclear(w);
91: wrefresh(w);
92: delwin(w);
93: }
94:
95:
96: void
97: showpigs()
98: {
99: register int i, j, y, k;
100: struct eproc *ep;
101: float total;
102: int factor;
103: char *uname, *pname, pidname[30];
104:
105: if (pt == NULL)
106: return;
107: /* Accumulate the percent of cpu per user. */
108: total = 0.0;
109: for (i = 0; i <= nproc; i++) {
110: /* Accumulate the percentage. */
111: total += pt[i].pt_pctcpu;
112: }
113:
114: if (total < 1.0)
115: total = 1.0;
116: factor = 50.0/total;
117:
118: qsort(pt, nproc + 1, sizeof (struct p_times), compar);
119: y = 1;
120: i = nproc + 1;
1.2 mickey 121: if (i > wnd->_maxy-1)
122: i = wnd->_maxy-1;
1.1 deraadt 123: for (k = 0; i > 0 && pt[k].pt_pctcpu > 0.01; i--, y++, k++) {
124: if (pt[k].pt_kp == NULL) {
125: uname = "";
126: pname = "<idle>";
127: }
128: else {
129: ep = &pt[k].pt_kp->kp_eproc;
130: uname = user_from_uid(ep->e_ucred.cr_uid, 0);
131: pname = pt[k].pt_kp->kp_proc.p_comm;
132: }
133: wmove(wnd, y, 0);
134: wclrtoeol(wnd);
135: mvwaddstr(wnd, y, 0, uname);
1.5 deraadt 136: snprintf(pidname, sizeof pidname, "%10.10s", pname);
1.1 deraadt 137: mvwaddstr(wnd, y, 9, pidname);
138: wmove(wnd, y, 20);
139: for (j = pt[k].pt_pctcpu*factor + 0.5; j > 0; j--)
140: waddch(wnd, 'X');
141: }
142: wmove(wnd, y, 0); wclrtobot(wnd);
143: }
144:
145: static struct nlist namelist[] = {
146: #define X_FIRST 0
147: #define X_CPTIME 0
148: { "_cp_time" },
149: #define X_CCPU 1
150: { "_ccpu" },
151: #define X_FSCALE 2
152: { "_fscale" },
153:
154: { "" }
155: };
156:
157: int
158: initpigs()
159: {
160: fixpt_t ccpu;
1.7 ericj 161: int ret;
1.1 deraadt 162:
163: if (namelist[X_FIRST].n_type == 0) {
1.7 ericj 164: if ((ret = kvm_nlist(kd, namelist)) == -1)
165: errx(1, "%s", kvm_geterr(kd));
166: else if (ret)
1.1 deraadt 167: nlisterr(namelist);
168: if (namelist[X_FIRST].n_type == 0) {
169: error("namelist failed");
170: return(0);
171: }
172: }
173: KREAD(NPTR(X_CPTIME), stime, sizeof (stime));
174: NREAD(X_CCPU, &ccpu, LONG);
175: NREAD(X_FSCALE, &fscale, LONG);
176: lccpu = log((double) ccpu / fscale);
177:
178: return(1);
179: }
180:
181: void
182: fetchpigs()
183: {
184: register int i;
185: register float time;
186: register struct proc *pp;
187: register float *pctp;
188: struct kinfo_proc *kpp;
189: long ctime[CPUSTATES];
190: double t;
191: static int lastnproc = 0;
192:
193: if (namelist[X_FIRST].n_type == 0)
194: return;
1.6 niklas 195: if ((kpp = kvm_getprocs(kd, KERN_PROC_KTHREAD, 0, &nproc)) == NULL) {
1.1 deraadt 196: error("%s", kvm_geterr(kd));
197: if (pt)
198: free(pt);
199: return;
200: }
201: if (nproc > lastnproc) {
202: free(pt);
203: if ((pt =
204: malloc((nproc + 1) * sizeof(struct p_times))) == NULL) {
205: error("Out of memory");
206: die(0);
207: }
208: }
209: lastnproc = nproc;
210: /*
211: * calculate %cpu for each proc
212: */
213: for (i = 0; i < nproc; i++) {
214: pt[i].pt_kp = &kpp[i];
215: pp = &kpp[i].kp_proc;
216: pctp = &pt[i].pt_pctcpu;
217: time = pp->p_swtime;
218: if (time == 0 || (pp->p_flag & P_INMEM) == 0)
219: *pctp = 0;
220: else
221: *pctp = ((double) pp->p_pctcpu /
222: fscale) / (1.0 - exp(time * lccpu));
223: }
224: /*
225: * and for the imaginary "idle" process
226: */
227: KREAD(NPTR(X_CPTIME), ctime, sizeof (ctime));
228: t = 0;
229: for (i = 0; i < CPUSTATES; i++)
230: t += ctime[i] - stime[i];
231: if (t == 0.0)
232: t = 1.0;
233: pt[nproc].pt_kp = NULL;
234: pt[nproc].pt_pctcpu = (ctime[CP_IDLE] - stime[CP_IDLE]) / t;
235: for (i = 0; i < CPUSTATES; i++)
236: stime[i] = ctime[i];
237: }
238:
239: void
240: labelpigs()
241: {
242: wmove(wnd, 0, 0);
243: wclrtoeol(wnd);
244: mvwaddstr(wnd, 0, 20,
245: "/0 /10 /20 /30 /40 /50 /60 /70 /80 /90 /100");
246: }
247:
248: int
249: compar(a, b)
250: const void *a, *b;
251: {
252: return (((struct p_times *) a)->pt_pctcpu >
253: ((struct p_times *) b)->pt_pctcpu)? -1: 1;
254: }