xref: /netbsd/usr.bin/systat/pigs.c (revision bf9ec67e)
1 /*	$NetBSD: pigs.c,v 1.22 2002/05/04 18:44:27 thorpej 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.22 2002/05/04 18:44:27 thorpej 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 kinfo_proc2 *kp;
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 			kp = pt[k].pt_kp;
131 			pname = kp->p_comm;
132 			snprintf(pidstr, sizeof(pidstr), "%5d", kp->p_pid);
133 			snprintf(usrstr, sizeof(usrstr), "%8s",
134 			    user_from_uid(kp->p_uid, 0));
135 		}
136 		wmove(wnd, y, 0);
137 		wclrtoeol(wnd);
138 		mvwaddstr(wnd, y, 0, usrstr);
139 		mvwaddstr(wnd, y, 9, pidstr);
140 		(void)snprintf(pidname, sizeof(pidname), "%9.9s", pname);
141 		mvwaddstr(wnd, y, 15, pidname);
142 		mvwhline(wnd, y, 25, 'X', pt[k].pt_pctcpu*factor + 0.5);
143 	}
144 	wmove(wnd, y, 0); wclrtobot(wnd);
145 }
146 
147 static struct nlist namelist[] = {
148 #define X_FIRST		0
149 #define X_CCPU          0
150 	{ "_ccpu" },
151 #define X_FSCALE        1
152 	{ "_fscale" },
153 #define X_PHYSMEM	2
154 	{ "_physmem" },
155 	{ "" }
156 };
157 
158 int
159 initpigs(void)
160 {
161 	fixpt_t ccpu;
162 
163 	if (namelist[X_FIRST].n_type == 0) {
164 		if (kvm_nlist(kd, namelist)) {
165 			nlisterr(namelist);
166 		        return(0);
167 		}
168 		if (namelist[X_FIRST].n_type == 0) {
169 			error("namelist failed");
170 			return(0);
171 		}
172 	}
173 	(void) fetch_cptime(stime);
174 	KREAD(NPTR(X_PHYSMEM), &mempages, sizeof (mempages));
175 	NREAD(X_CCPU, &ccpu, sizeof ccpu);
176 	NREAD(X_FSCALE,  &fscale, sizeof fscale);
177 	lccpu = log((double) ccpu / fscale);
178 
179 	return(1);
180 }
181 
182 void
183 fetchpigs(void)
184 {
185 	int i;
186 	float time;
187 	float *pctp;
188 	struct kinfo_proc2 *kpp, *k;
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_getproc2(kd, KERN_PROC_ALL, 0, sizeof(*kpp),
196 				&nproc)) == NULL) {
197 		error("%s", kvm_geterr(kd));
198 		if (pt)
199 			free(pt);
200 		return;
201 	}
202 	if (nproc > lastnproc) {
203 		free(pt);
204 		if ((pt =
205 		    malloc((nproc + 1) * sizeof(struct p_times))) == NULL) {
206 			error("Out of memory");
207 			die(0);
208 		}
209 	}
210 	lastnproc = nproc;
211 	/*
212 	 * calculate %cpu for each proc
213 	 */
214 	for (i = 0; i < nproc; i++) {
215 		pt[i].pt_kp = k = &kpp[i];
216 		pctp = &pt[i].pt_pctcpu;
217 		time = k->p_swtime;
218 		/* XXX - I don't like this */
219 		if (k->p_swtime == 0 || (k->p_flag & P_INMEM) == 0 ||
220 		    k->p_stat == SZOMB || k->p_stat == SDEAD)
221 			*pctp = 0;
222 		else
223 			*pctp = ((double) k->p_pctcpu /
224 					fscale) / (1.0 - exp(time * lccpu));
225 	}
226 	/*
227 	 * and for the imaginary "idle" process
228 	 */
229 	(void) fetch_cptime(ctime);
230 	t = 0;
231 	for (i = 0; i < CPUSTATES; i++)
232 		t += ctime[i] - stime[i];
233 	if (t == 0.0)
234 		t = 1.0;
235 	pt[nproc].pt_kp = NULL;
236 	pt[nproc].pt_pctcpu = (ctime[CP_IDLE] - stime[CP_IDLE]) / t;
237 	for (i = 0; i < CPUSTATES; i++)
238 		stime[i] = ctime[i];
239 }
240 
241 void
242 labelpigs(void)
243 {
244 	wmove(wnd, 0, 0);
245 	wclrtoeol(wnd);
246 	mvwaddstr(wnd, 0, 25, "/0   /10  /20  /30  /40  /50  /60  /70  /80  /90  /100");
247 }
248 
249 int
250 compare_pctcpu(const void *a, const void *b)
251 {
252 	return (((struct p_times *) a)->pt_pctcpu >
253 		((struct p_times *) b)->pt_pctcpu)? -1: 1;
254 }
255