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.11 2005/11/14 18:50:17 dillon Exp $
39 * Pigs display from Bill Reeves at Lucasfilm
42 #define _KERNEL_STRUCTURES
43 #include <sys/param.h>
46 #include <sys/sysctl.h>
58 int compar(const void *, const void *);
61 static struct p_times {
63 struct kinfo_proc *pt_kp;
66 struct kinfo_cputime old_cp_time;
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[] = {
156 if (namelist[X_FIRST].n_type == 0) {
157 if (kvm_nlist(kd, namelist)) {
161 if (namelist[X_FIRST].n_type == 0) {
162 error("namelist failed");
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");
171 NREAD(X_FSCALE, &fscale, LONG);
172 lccpu = log((double) ccpu / fscale);
184 struct kinfo_proc *kpp;
185 struct kinfo_cputime cp_time, diff_cp_time;
187 static int lastnproc = 0;
189 if (namelist[X_FIRST].n_type == 0)
191 if ((kpp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nproc)) == NULL) {
192 error("%s", kvm_geterr(kd));
197 if (nproc > lastnproc) {
200 malloc((nproc + 1) * sizeof(struct p_times))) == NULL) {
201 error("Out of memory");
207 * calculate %cpu for each proc
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;
214 if (time == 0 || (pp->p_flag & P_SWAPPEDOUT))
217 *pctp = ((double) pp->p_pctcpu /
218 fscale) / (1.0 - exp(time * lccpu));
221 * and for the imaginary "idle" process
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;
236 pt[nproc].pt_kp = NULL;
237 pt[nproc].pt_pctcpu = diff_cp_time.cp_idle / t;
245 mvwaddstr(wnd, 0, 20,
246 "/0 /10 /20 /30 /40 /50 /60 /70 /80 /90 /100");
250 compar(const void *a, const void *b)
252 struct p_times *pta = (struct p_times *)a;
253 struct p_times *ptb = (struct p_times *)b;
257 * Check overall cpu percentage first.
259 d = pta->pt_pctcpu - ptb->pt_pctcpu;
261 return(-1); /* a is better */
263 return(1); /* b is better */
265 if (pta->pt_kp == NULL && ptb->pt_kp == NULL)
267 if (ptb->pt_kp == NULL)
268 return(-1); /* a is better */
269 if (pta->pt_kp == NULL)
270 return(1); /* b is better */
272 * Then check sleep times and run status.
274 if (pta->pt_kp->kp_proc.p_slptime < ptb->pt_kp->kp_proc.p_slptime)
276 if (pta->pt_kp->kp_proc.p_slptime > ptb->pt_kp->kp_proc.p_slptime)
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)
285 if (ptb->pt_kp->kp_proc.p_stat == SRUN)