2 * Copyright (c) 1980, 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
33 * @(#)pigs.c 8.2 (Berkeley) 9/23/93
35 * $DragonFly: src/usr.bin/systat/pigs.c,v 1.8 2003/11/21 22:46:14 dillon Exp $
39 * Pigs display from Bill Reeves at Lucasfilm
42 #define _KERNEL_STRUCTURES
43 #include <sys/param.h>
44 #include <sys/dkstat.h>
47 #include <sys/sysctl.h>
58 int compar(const void *, const void *);
61 static struct p_times {
63 struct kinfo_proc *pt_kp;
66 static long stime[CPUSTATES];
73 return (subwin(stdscr, LINES-5-1, 0, 5, 0));
90 register int i, j, y, k;
94 char *uname, *pname, pidname[30];
98 /* Accumulate the percent of cpu per user. */
100 for (i = 0; i <= nproc; i++) {
101 /* Accumulate the percentage. */
102 total += pt[i].pt_pctcpu;
109 qsort(pt, nproc + 1, sizeof (struct p_times), compar);
112 if (i > wnd->_maxy-1)
114 for (k = 0; i > 0; i--, y++, k++) {
116 if (pt[k].pt_pctcpu <= 0.01 &&
117 (pt[k].pt_kp == NULL ||
118 pt[k].pt_kp->kp_proc.p_slptime > 1)
123 if (pt[k].pt_kp == NULL) {
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;
133 mvwaddstr(wnd, y, 0, uname);
134 snprintf(pidname, sizeof(pidname), "%10.10s", pname);
135 mvwaddstr(wnd, y, 9, pidname);
137 for (j = pt[k].pt_pctcpu*factor + 0.5; j > 0; j--)
140 wmove(wnd, y, 0); wclrtobot(wnd);
143 static struct nlist namelist[] = {
160 if (namelist[X_FIRST].n_type == 0) {
161 if (kvm_nlist(kd, namelist)) {
165 if (namelist[X_FIRST].n_type == 0) {
166 error("namelist failed");
170 KREAD(NPTR(X_CPTIME), stime, sizeof (stime));
171 NREAD(X_CCPU, &ccpu, sizeof(ccpu));
172 NREAD(X_FSCALE, &fscale, LONG);
173 lccpu = log((double) ccpu / fscale);
185 struct kinfo_proc *kpp;
186 long ctime[CPUSTATES];
188 static int lastnproc = 0;
190 if (namelist[X_FIRST].n_type == 0)
192 if ((kpp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nproc)) == NULL) {
193 error("%s", kvm_geterr(kd));
198 if (nproc > lastnproc) {
201 malloc((nproc + 1) * sizeof(struct p_times))) == NULL) {
202 error("Out of memory");
208 * calculate %cpu for each proc
210 for (i = 0; i < nproc; i++) {
211 pt[i].pt_kp = &kpp[i];
212 pp = &kpp[i].kp_proc;
213 pctp = &pt[i].pt_pctcpu;
215 if (time == 0 || (pp->p_flag & P_INMEM) == 0)
218 *pctp = ((double) pp->p_pctcpu /
219 fscale) / (1.0 - exp(time * lccpu));
222 * and for the imaginary "idle" process
224 KREAD(NPTR(X_CPTIME), ctime, sizeof (ctime));
226 for (i = 0; i < CPUSTATES; i++)
227 t += ctime[i] - stime[i];
230 pt[nproc].pt_kp = NULL;
231 pt[nproc].pt_pctcpu = (ctime[CP_IDLE] - stime[CP_IDLE]) / t;
232 for (i = 0; i < CPUSTATES; i++)
241 mvwaddstr(wnd, 0, 20,
242 "/0 /10 /20 /30 /40 /50 /60 /70 /80 /90 /100");
246 compar(const void *a, const void *b)
248 struct p_times *pta = (struct p_times *)a;
249 struct p_times *ptb = (struct p_times *)b;
253 * Check overall cpu percentage first.
255 d = pta->pt_pctcpu - ptb->pt_pctcpu;
257 return(-1); /* a is better */
259 return(1); /* b is better */
261 if (pta->pt_kp == NULL && ptb->pt_kp == NULL)
263 if (ptb->pt_kp == NULL)
264 return(-1); /* a is better */
265 if (pta->pt_kp == NULL)
266 return(1); /* b is better */
268 * Then check sleep times and run status.
270 if (pta->pt_kp->kp_proc.p_slptime < ptb->pt_kp->kp_proc.p_slptime)
272 if (pta->pt_kp->kp_proc.p_slptime > ptb->pt_kp->kp_proc.p_slptime)
278 if (pta->pt_kp->kp_proc.p_stat != ptb->pt_kp->kp_proc.p_stat) {
279 if (pta->pt_kp->kp_proc.p_stat == SRUN)
281 if (ptb->pt_kp->kp_proc.p_stat == SRUN)