xref: /netbsd-src/usr.bin/systat/pigs.c (revision c41a4eebefede43f6950f838a387dc18c6a431bf)
1 /*	$NetBSD: pigs.c,v 1.7 1997/10/19 23:36:31 lukem Exp $	*/
2 
3 /*-
4  * Copyright (c) 1980, 1992, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)pigs.c	8.2 (Berkeley) 9/23/93";
40 #endif
41 __RCSID("$NetBSD: pigs.c,v 1.7 1997/10/19 23:36:31 lukem Exp $");
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 	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;
120 	if (i > getmaxy(wnd)-1)
121 		i = getmaxy(wnd)-1;
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);
135 		sprintf(pidname, "%10.10s", pname);
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;
160 
161 	if (namelist[X_FIRST].n_type == 0) {
162 		if (kvm_nlist(kd, namelist)) {
163 			nlisterr(namelist);
164 		        return(0);
165 		}
166 		if (namelist[X_FIRST].n_type == 0) {
167 			error("namelist failed");
168 			return(0);
169 		}
170 	}
171 	KREAD(NPTR(X_CPTIME), stime, sizeof (stime));
172 	NREAD(X_CCPU, &ccpu, LONG);
173 	NREAD(X_FSCALE,  &fscale, LONG);
174 	lccpu = log((double) ccpu / fscale);
175 
176 	return(1);
177 }
178 
179 void
180 fetchpigs()
181 {
182 	int i;
183 	float time;
184 	struct proc *pp;
185 	float *pctp;
186 	struct kinfo_proc *kpp;
187 	long ctime[CPUSTATES];
188 	double t;
189 	static int lastnproc = 0;
190 
191 	if (namelist[X_FIRST].n_type == 0)
192 		return;
193 	if ((kpp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nproc)) == NULL) {
194 		error("%s", kvm_geterr(kd));
195 		if (pt)
196 			free(pt);
197 		return;
198 	}
199 	if (nproc > lastnproc) {
200 		free(pt);
201 		if ((pt =
202 		    malloc((nproc + 1) * sizeof(struct p_times))) == NULL) {
203 			error("Out of memory");
204 			die(0);
205 		}
206 	}
207 	lastnproc = nproc;
208 	/*
209 	 * calculate %cpu for each proc
210 	 */
211 	for (i = 0; i < nproc; i++) {
212 		pt[i].pt_kp = &kpp[i];
213 		pp = &kpp[i].kp_proc;
214 		pctp = &pt[i].pt_pctcpu;
215 		time = pp->p_swtime;
216 		if (time == 0 || (pp->p_flag & P_INMEM) == 0)
217 			*pctp = 0;
218 		else
219 			*pctp = ((double) pp->p_pctcpu /
220 					fscale) / (1.0 - exp(time * lccpu));
221 	}
222 	/*
223 	 * and for the imaginary "idle" process
224 	 */
225 	KREAD(NPTR(X_CPTIME), ctime, sizeof (ctime));
226 	t = 0;
227 	for (i = 0; i < CPUSTATES; i++)
228 		t += ctime[i] - stime[i];
229 	if (t == 0.0)
230 		t = 1.0;
231 	pt[nproc].pt_kp = NULL;
232 	pt[nproc].pt_pctcpu = (ctime[CP_IDLE] - stime[CP_IDLE]) / t;
233 	for (i = 0; i < CPUSTATES; i++)
234 		stime[i] = ctime[i];
235 }
236 
237 void
238 labelpigs()
239 {
240 	wmove(wnd, 0, 0);
241 	wclrtoeol(wnd);
242 	mvwaddstr(wnd, 0, 20,
243 	    "/0   /10  /20  /30  /40  /50  /60  /70  /80  /90  /100");
244 }
245 
246 int
247 compar(a, b)
248 	const void *a, *b;
249 {
250 	return (((struct p_times *) a)->pt_pctcpu >
251 		((struct p_times *) b)->pt_pctcpu)? -1: 1;
252 }
253