xref: /freebsd/usr.bin/systat/pigs.c (revision 3a31b7eb32ad60e1e05b2b2e184ff47e4afbb874)
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  * $FreeBSD$
34  */
35 
36 #ifndef lint
37 static char sccsid[] = "@(#)pigs.c	8.2 (Berkeley) 9/23/93";
38 #endif /* not lint */
39 
40 /*
41  * Pigs display from Bill Reeves at Lucasfilm
42  */
43 
44 #include <sys/param.h>
45 #include <sys/dkstat.h>
46 #include <sys/time.h>
47 #include <sys/user.h>
48 #include <sys/sysctl.h>
49 
50 #include <curses.h>
51 #include <math.h>
52 #include <nlist.h>
53 #include <pwd.h>
54 #include <stdlib.h>
55 
56 #include "extern.h"
57 #include "systat.h"
58 
59 int compar __P((const void *, const void *));
60 
61 static int nproc;
62 static struct p_times {
63 	float pt_pctcpu;
64 	struct kinfo_proc *pt_kp;
65 } *pt;
66 
67 static long stime[CPUSTATES];
68 static int    fscale;
69 static double  lccpu;
70 
71 WINDOW *
72 openpigs()
73 {
74 	return (subwin(stdscr, LINES-5-1, 0, 5, 0));
75 }
76 
77 void
78 closepigs(w)
79 	WINDOW *w;
80 {
81 	if (w == NULL)
82 		return;
83 	wclear(w);
84 	wrefresh(w);
85 	delwin(w);
86 }
87 
88 
89 void
90 showpigs()
91 {
92 	register int i, j, y, k;
93 	float total;
94 	int factor;
95 	char *uname, *pname, pidname[30];
96 
97 	if (pt == NULL)
98 		return;
99 	/* Accumulate the percent of cpu per user. */
100 	total = 0.0;
101 	for (i = 0; i <= nproc; i++) {
102 		/* Accumulate the percentage. */
103 		total += pt[i].pt_pctcpu;
104 	}
105 
106 	if (total < 1.0)
107  		total = 1.0;
108 	factor = 50.0/total;
109 
110         qsort(pt, nproc + 1, sizeof (struct p_times), compar);
111 	y = 1;
112 	i = nproc + 1;
113 	if (i > wnd->_maxy-1)
114 		i = wnd->_maxy-1;
115 	for (k = 0; i > 0 && pt[k].pt_pctcpu > 0.01; i--, y++, k++) {
116 		if (pt[k].pt_kp == NULL) {
117 			uname = "";
118 			pname = "<idle>";
119 		}
120 		else {
121 			uname = (char *)
122 			    user_from_uid(pt[k].pt_kp->ki_uid, 0);
123 			pname = pt[k].pt_kp->ki_comm;
124 		}
125 		wmove(wnd, y, 0);
126 		wclrtoeol(wnd);
127 		mvwaddstr(wnd, y, 0, uname);
128 		snprintf(pidname, sizeof(pidname), "%10.10s", pname);
129 		mvwaddstr(wnd, y, 9, pidname);
130 		wmove(wnd, y, 20);
131 		for (j = pt[k].pt_pctcpu*factor + 0.5; j > 0; j--)
132 			waddch(wnd, 'X');
133 	}
134 	wmove(wnd, y, 0); wclrtobot(wnd);
135 }
136 
137 int
138 initpigs()
139 {
140 	fixpt_t ccpu;
141 	size_t len;
142 	int err;
143 
144 	len = sizeof(stime);
145 	err = sysctlbyname("kern.cp_time", &stime, &len, NULL, 0);
146 	if (err || len != sizeof(stime)) {
147 		perror("kern.cp_time");
148 		return (0);
149 	}
150 
151 	len = sizeof(ccpu);
152 	err = sysctlbyname("kern.ccpu", &ccpu, &len, NULL, 0);
153 	if (err || len != sizeof(ccpu)) {
154 		perror("kern.ccpu");
155 		return (0);
156 	}
157 
158 	len = sizeof(fscale);
159 	err = sysctlbyname("kern.fscale", &fscale, &len, NULL, 0);
160 	if (err || len != sizeof(fscale)) {
161 		perror("kern.fscale");
162 		return (0);
163 	}
164 
165 	lccpu = log((double) ccpu / fscale);
166 
167 	return(1);
168 }
169 
170 void
171 fetchpigs()
172 {
173 	register int i;
174 	register float time;
175 	register float *pctp;
176 	struct kinfo_proc *kpp;
177 	long ctime[CPUSTATES];
178 	double t;
179 	static int lastnproc = 0;
180 	size_t len;
181 	int err;
182 
183 	if ((kpp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nproc)) == NULL) {
184 		error("%s", kvm_geterr(kd));
185 		if (pt)
186 			free(pt);
187 		return;
188 	}
189 	if (nproc > lastnproc) {
190 		free(pt);
191 		if ((pt =
192 		    malloc((nproc + 1) * sizeof(struct p_times))) == NULL) {
193 			error("Out of memory");
194 			die(0);
195 		}
196 	}
197 	lastnproc = nproc;
198 	/*
199 	 * calculate %cpu for each proc
200 	 */
201 	for (i = 0; i < nproc; i++) {
202 		pt[i].pt_kp = &kpp[i];
203 		pctp = &pt[i].pt_pctcpu;
204 		time = kpp[i].ki_swtime;
205 		if (time == 0 || (kpp[i].ki_sflag & PS_INMEM) == 0)
206 			*pctp = 0;
207 		else
208 			*pctp = ((double) kpp[i].ki_pctcpu /
209 					fscale) / (1.0 - exp(time * lccpu));
210 	}
211 	/*
212 	 * and for the imaginary "idle" process
213 	 */
214 	len = sizeof(ctime);
215 	err = sysctlbyname("kern.cp_time", &ctime, &len, NULL, 0);
216 	if (err || len != sizeof(ctime)) {
217 		perror("kern.cp_time");
218 		return;
219 	}
220 	t = 0;
221 	for (i = 0; i < CPUSTATES; i++)
222 		t += ctime[i] - stime[i];
223 	if (t == 0.0)
224 		t = 1.0;
225 	pt[nproc].pt_kp = NULL;
226 	pt[nproc].pt_pctcpu = (ctime[CP_IDLE] - stime[CP_IDLE]) / t;
227 	for (i = 0; i < CPUSTATES; i++)
228 		stime[i] = ctime[i];
229 }
230 
231 void
232 labelpigs()
233 {
234 	wmove(wnd, 0, 0);
235 	wclrtoeol(wnd);
236 	mvwaddstr(wnd, 0, 20,
237 	    "/0   /10  /20  /30  /40  /50  /60  /70  /80  /90  /100");
238 }
239 
240 int
241 compar(a, b)
242 	const void *a, *b;
243 {
244 	return (((struct p_times *) a)->pt_pctcpu >
245 		((struct p_times *) b)->pt_pctcpu)? -1: 1;
246 }
247