xref: /dragonfly/usr.bin/systat/pigs.c (revision f9a239ec)
1 /*-
2  * Copyright (c) 1980, 1992, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * @(#)pigs.c	8.2 (Berkeley) 9/23/93
34  *
35  * $DragonFly: src/usr.bin/systat/pigs.c,v 1.9 2004/12/22 11:01:49 joerg Exp $
36  */
37 
38 /*
39  * Pigs display from Bill Reeves at Lucasfilm
40  */
41 
42 #define _KERNEL_STRUCTURES
43 #include <sys/param.h>
44 #include <sys/time.h>
45 #include <sys/user.h>
46 #include <sys/sysctl.h>
47 
48 #include <curses.h>
49 #include <kinfo.h>
50 #include <math.h>
51 #include <nlist.h>
52 #include <pwd.h>
53 #include <stdlib.h>
54 
55 #include "extern.h"
56 #include "systat.h"
57 
58 int compar(const void *, const void *);
59 
60 static int nproc;
61 static struct p_times {
62 	float pt_pctcpu;
63 	struct kinfo_proc *pt_kp;
64 } *pt;
65 
66 struct kinfo_cputime old_cp_time;
67 static long    fscale;
68 static double  lccpu;
69 
70 WINDOW *
71 openpigs(void)
72 {
73 	return (subwin(stdscr, LINES-5-1, 0, 5, 0));
74 }
75 
76 void
77 closepigs(WINDOW *w)
78 {
79 	if (w == NULL)
80 		return;
81 	wclear(w);
82 	wrefresh(w);
83 	delwin(w);
84 }
85 
86 
87 void
88 showpigs(void)
89 {
90 	register int i, j, y, k;
91 	struct	eproc *ep;
92 	float total;
93 	int factor;
94 	char *uname, *pname, pidname[30];
95 
96 	if (pt == NULL)
97 		return;
98 	/* Accumulate the percent of cpu per user. */
99 	total = 0.0;
100 	for (i = 0; i <= nproc; i++) {
101 		/* Accumulate the percentage. */
102 		total += pt[i].pt_pctcpu;
103 	}
104 
105 	if (total < 1.0)
106  		total = 1.0;
107 	factor = 50.0/total;
108 
109         qsort(pt, nproc + 1, sizeof (struct p_times), compar);
110 	y = 1;
111 	i = nproc + 1;
112 	if (i > wnd->_maxy-1)
113 		i = wnd->_maxy-1;
114 	for (k = 0; i > 0; i--, y++, k++) {
115 		char buf[256];
116 		if (pt[k].pt_pctcpu <= 0.01 &&
117 		    (pt[k].pt_kp == NULL ||
118 		    pt[k].pt_kp->kp_proc.p_slptime > 1)
119 		) {
120 			--y;
121 			continue;
122 		}
123 		if (pt[k].pt_kp == NULL) {
124 			uname = "";
125 			pname = "<idle>";
126 		} else {
127 			ep = &pt[k].pt_kp->kp_eproc;
128 			uname = (char *)user_from_uid(ep->e_ucred.cr_uid, 0);
129 			pname = pt[k].pt_kp->kp_thread.td_comm;
130 		}
131 		wmove(wnd, y, 0);
132 		wclrtoeol(wnd);
133 		mvwaddstr(wnd, y, 0, uname);
134 		snprintf(pidname, sizeof(pidname), "%10.10s", pname);
135 		mvwaddstr(wnd, y, 9, pidname);
136 		wmove(wnd, y, 20);
137 		for (j = pt[k].pt_pctcpu*factor + 0.5; j > 0; j--)
138 			waddch(wnd, 'X');
139 	}
140 	wmove(wnd, y, 0); wclrtobot(wnd);
141 }
142 
143 static struct nlist namelist[] = {
144 #define X_FIRST		0
145 #define X_FSCALE        0
146 	{ "_fscale" },
147 
148 	{ "" }
149 };
150 
151 int
152 initpigs(void)
153 {
154 	int ccpu;
155 
156 	if (namelist[X_FIRST].n_type == 0) {
157 		if (kvm_nlist(kd, namelist)) {
158 			nlisterr(namelist);
159 		        return(0);
160 		}
161 		if (namelist[X_FIRST].n_type == 0) {
162 			error("namelist failed");
163 			return(0);
164 		}
165 	}
166 	if (kinfo_get_sched_cputime(&old_cp_time))
167 		err(1, "kinfo_get_sched_cputime");
168 	if (kinfo_get_sched_ccpu(&ccpu))
169 		err(1, "kinfo_get_sched_ccpu");
170 
171 	NREAD(X_FSCALE,  &fscale, LONG);
172 	lccpu = log((double) ccpu / fscale);
173 
174 	return(1);
175 }
176 
177 void
178 fetchpigs(void)
179 {
180 	int i;
181 	float time;
182 	struct proc *pp;
183 	float *pctp;
184 	struct kinfo_proc *kpp;
185 	struct kinfo_cputime cp_time, diff_cp_time;
186 	double t;
187 	static int lastnproc = 0;
188 
189 	if (namelist[X_FIRST].n_type == 0)
190 		return;
191 	if ((kpp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nproc)) == NULL) {
192 		error("%s", kvm_geterr(kd));
193 		if (pt)
194 			free(pt);
195 		return;
196 	}
197 	if (nproc > lastnproc) {
198 		free(pt);
199 		if ((pt =
200 		    malloc((nproc + 1) * sizeof(struct p_times))) == NULL) {
201 			error("Out of memory");
202 			die(0);
203 		}
204 	}
205 	lastnproc = nproc;
206 	/*
207 	 * calculate %cpu for each proc
208 	 */
209 	for (i = 0; i < nproc; i++) {
210 		pt[i].pt_kp = &kpp[i];
211 		pp = &kpp[i].kp_proc;
212 		pctp = &pt[i].pt_pctcpu;
213 		time = pp->p_swtime;
214 		if (time == 0 || (pp->p_flag & P_INMEM) == 0)
215 			*pctp = 0;
216 		else
217 			*pctp = ((double) pp->p_pctcpu /
218 					fscale) / (1.0 - exp(time * lccpu));
219 	}
220 	/*
221 	 * and for the imaginary "idle" process
222 	 */
223 	if (kinfo_get_sched_cputime(&cp_time))
224 		err(1, "kinfo_get_sched_cputime");
225 	diff_cp_time.cp_user = cp_time.cp_user - old_cp_time.cp_user;
226 	diff_cp_time.cp_nice = cp_time.cp_nice - old_cp_time.cp_nice;
227 	diff_cp_time.cp_sys = cp_time.cp_sys - old_cp_time.cp_sys;
228 	diff_cp_time.cp_intr = cp_time.cp_intr - old_cp_time.cp_intr;
229 	diff_cp_time.cp_idle = cp_time.cp_idle - old_cp_time.cp_idle;
230 	old_cp_time = cp_time;
231 	t = diff_cp_time.cp_user + diff_cp_time.cp_nice +
232 	    diff_cp_time.cp_sys + diff_cp_time.cp_intr +
233 	    diff_cp_time.cp_idle;
234 	if (t == 0.0)
235 		t = 1.0;
236 	pt[nproc].pt_kp = NULL;
237 	pt[nproc].pt_pctcpu = diff_cp_time.cp_idle / t;
238 }
239 
240 void
241 labelpigs(void)
242 {
243 	wmove(wnd, 0, 0);
244 	wclrtoeol(wnd);
245 	mvwaddstr(wnd, 0, 20,
246 	    "/0   /10  /20  /30  /40  /50  /60  /70  /80  /90  /100");
247 }
248 
249 int
250 compar(const void *a, const void *b)
251 {
252 	struct p_times *pta = (struct p_times *)a;
253 	struct p_times *ptb = (struct p_times *)b;
254 	float d;
255 
256 	/*
257 	 * Check overall cpu percentage first.
258 	 */
259 	d = pta->pt_pctcpu - ptb->pt_pctcpu;
260 	if (d > 0.10)
261 		return(-1);	/* a is better */
262 	else if (d < -0.10)
263 		return(1);	/* b is better */
264 
265 	if (pta->pt_kp == NULL && ptb->pt_kp == NULL)
266 		return(0);
267 	if (ptb->pt_kp == NULL)
268 		return(-1);	/* a is better */
269 	if (pta->pt_kp == NULL)
270 		return(1);	/* b is better */
271 	/*
272 	 * Then check sleep times and run status.
273 	 */
274 	if (pta->pt_kp->kp_proc.p_slptime < ptb->pt_kp->kp_proc.p_slptime)
275 		return(-1);
276 	if (pta->pt_kp->kp_proc.p_slptime > ptb->pt_kp->kp_proc.p_slptime)
277 		return(1);
278 
279 	/*
280 	 * Runnability
281 	 */
282 	if (pta->pt_kp->kp_proc.p_stat != ptb->pt_kp->kp_proc.p_stat) {
283 		if (pta->pt_kp->kp_proc.p_stat == SRUN)
284 			return(-1);
285 		if (ptb->pt_kp->kp_proc.p_stat == SRUN)
286 			return(1);
287 	}
288 	return(0);
289 }
290 
291 
292