xref: /netbsd-src/usr.bin/systat/pigs.c (revision 3b01aba77a7a698587faaae455bbfe740923c1f5)
1 /*	$NetBSD: pigs.c,v 1.21 2000/12/01 02:19:44 simonb 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.21 2000/12/01 02:19:44 simonb 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/sched.h>
50 #include <sys/sysctl.h>
51 
52 #include <curses.h>
53 #include <math.h>
54 #include <pwd.h>
55 #include <stdlib.h>
56 #include <string.h>
57 
58 #include "extern.h"
59 #include "systat.h"
60 #include "ps.h"
61 
62 int compare_pctcpu(const void *, const void *);
63 
64 int nproc;
65 struct p_times *pt;
66 
67 u_int64_t stime[CPUSTATES];
68 long	mempages;
69 int     fscale;
70 double  lccpu;
71 
72 #ifndef P_ZOMBIE
73 #define P_ZOMBIE(p)	((p)->p_stat == SZOMB)
74 #endif
75 
76 WINDOW *
77 openpigs(void)
78 {
79 
80 	return (subwin(stdscr, LINES-5-1, 0, 5, 0));
81 }
82 
83 void
84 closepigs(WINDOW *w)
85 {
86 
87 	if (w == NULL)
88 		return;
89 	wclear(w);
90 	wrefresh(w);
91 	delwin(w);
92 }
93 
94 
95 void
96 showpigs(void)
97 {
98 	int i, y, k;
99 	struct	eproc *ep;
100 	float total;
101 	int factor;
102 	const char *pname;
103 	char pidname[30], pidstr[7], usrstr[9];
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), compare_pctcpu);
119 	y = 1;
120 	i = nproc + 1;
121 	if (i > getmaxy(wnd)-1)
122 		i = getmaxy(wnd)-1;
123 	for (k = 0; i > 0 && pt[k].pt_pctcpu > 0.01; i--, y++, k++) {
124 		if (pt[k].pt_kp == NULL) {
125 			pname = "<idle>";
126 			pidstr[0] = '\0';
127 			usrstr[0] = '\0';
128 		}
129 		else {
130 			ep = &pt[k].pt_kp->kp_eproc;
131 			pname = pt[k].pt_kp->kp_proc.p_comm;
132 			snprintf(pidstr, sizeof(pidstr), "%5d", pt[k].pt_kp->kp_proc.p_pid);
133 			snprintf(usrstr, sizeof(usrstr), "%8s", user_from_uid(ep->e_ucred.cr_uid, 0));
134 		}
135 		wmove(wnd, y, 0);
136 		wclrtoeol(wnd);
137 		mvwaddstr(wnd, y, 0, usrstr);
138 		mvwaddstr(wnd, y, 9, pidstr);
139 		(void)snprintf(pidname, sizeof(pidname), "%9.9s", pname);
140 		mvwaddstr(wnd, y, 15, pidname);
141 		mvwhline(wnd, y, 25, 'X', pt[k].pt_pctcpu*factor + 0.5);
142 	}
143 	wmove(wnd, y, 0); wclrtobot(wnd);
144 }
145 
146 static struct nlist namelist[] = {
147 #define X_FIRST		0
148 #define X_CCPU          0
149 	{ "_ccpu" },
150 #define X_FSCALE        1
151 	{ "_fscale" },
152 #define X_PHYSMEM	2
153 	{ "_physmem" },
154 	{ "" }
155 };
156 
157 int
158 initpigs(void)
159 {
160 	fixpt_t ccpu;
161 
162 	if (namelist[X_FIRST].n_type == 0) {
163 		if (kvm_nlist(kd, namelist)) {
164 			nlisterr(namelist);
165 		        return(0);
166 		}
167 		if (namelist[X_FIRST].n_type == 0) {
168 			error("namelist failed");
169 			return(0);
170 		}
171 	}
172 	(void) fetch_cptime(stime);
173 	KREAD(NPTR(X_PHYSMEM), &mempages, sizeof (mempages));
174 	NREAD(X_CCPU, &ccpu, sizeof ccpu);
175 	NREAD(X_FSCALE,  &fscale, sizeof fscale);
176 	lccpu = log((double) ccpu / fscale);
177 
178 	return(1);
179 }
180 
181 void
182 fetchpigs(void)
183 {
184 	int i;
185 	float time;
186 	struct proc *pp;
187 	float *pctp;
188 	struct kinfo_proc *kpp;
189 	u_int64_t ctime[CPUSTATES];
190 	double t;
191 	static int lastnproc = 0;
192 
193 	if (namelist[X_FIRST].n_type == 0)
194 		return;
195 	if ((kpp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nproc)) == NULL) {
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 (P_ZOMBIE(pp) ||
219 		    time == 0 || (pp->p_flag & P_INMEM) == 0)
220 			*pctp = 0;
221 		else
222 			*pctp = ((double) pp->p_pctcpu /
223 					fscale) / (1.0 - exp(time * lccpu));
224 	}
225 	/*
226 	 * and for the imaginary "idle" process
227 	 */
228 	(void) fetch_cptime(ctime);
229 	t = 0;
230 	for (i = 0; i < CPUSTATES; i++)
231 		t += ctime[i] - stime[i];
232 	if (t == 0.0)
233 		t = 1.0;
234 	pt[nproc].pt_kp = NULL;
235 	pt[nproc].pt_pctcpu = (ctime[CP_IDLE] - stime[CP_IDLE]) / t;
236 	for (i = 0; i < CPUSTATES; i++)
237 		stime[i] = ctime[i];
238 }
239 
240 void
241 labelpigs(void)
242 {
243 	wmove(wnd, 0, 0);
244 	wclrtoeol(wnd);
245 	mvwaddstr(wnd, 0, 25, "/0   /10  /20  /30  /40  /50  /60  /70  /80  /90  /100");
246 }
247 
248 int
249 compare_pctcpu(const void *a, const void *b)
250 {
251 	return (((struct p_times *) a)->pt_pctcpu >
252 		((struct p_times *) b)->pt_pctcpu)? -1: 1;
253 }
254