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