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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 static char sccsid[] = "@(#)pigs.c 8.2 (Berkeley) 9/23/93";
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
40 * Pigs display from Bill Reeves at Lucasfilm
43 #include <sys/param.h>
45 #include <sys/sysctl.h>
57 int compar(const void *, const void *);
60 static struct p_times {
62 struct kinfo_proc *pt_kp;
71 return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0));
88 const char *uname, *pname;
94 qsort(pt, nproc, sizeof (struct p_times), compar);
97 if (i > getmaxy(wnd)-2)
99 for (k = 0; i > 0 && pt[k].pt_pctcpu > 0.01; i--, y++, k++) {
100 uname = user_from_uid(pt[k].pt_kp->ki_uid, 0);
101 pname = pt[k].pt_kp->ki_comm;
104 mvwaddstr(wnd, y, 0, uname);
105 snprintf(pidname, sizeof(pidname), "%10.10s", pname);
106 mvwaddstr(wnd, y, 9, pidname);
108 for (j = pt[k].pt_pctcpu * 50 + 0.5; j > 0; j--)
111 wmove(wnd, y, 0); wclrtobot(wnd);
122 err = sysctlbyname("kern.ccpu", &ccpu, &len, NULL, 0);
123 if (err || len != sizeof(ccpu)) {
128 len = sizeof(fscale);
129 err = sysctlbyname("kern.fscale", &fscale, &len, NULL, 0);
130 if (err || len != sizeof(fscale)) {
131 perror("kern.fscale");
135 lccpu = log((double) ccpu / fscale);
146 struct kinfo_proc *kpp;
147 static int lastnproc = 0;
149 if ((kpp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nproc)) == NULL) {
150 error("%s", kvm_geterr(kd));
155 if (nproc > lastnproc) {
158 malloc(nproc * sizeof(struct p_times))) == NULL) {
159 error("Out of memory");
165 * calculate %cpu for each proc
167 for (i = 0; i < nproc; i++) {
168 pt[i].pt_kp = &kpp[i];
169 pctp = &pt[i].pt_pctcpu;
170 ftime = kpp[i].ki_swtime;
171 if (ftime == 0 || (kpp[i].ki_flag & P_INMEM) == 0)
174 *pctp = ((double) kpp[i].ki_pctcpu /
175 fscale) / (1.0 - exp(ftime * lccpu));
184 mvwaddstr(wnd, 0, 20,
185 "/0% /10 /20 /30 /40 /50 /60 /70 /80 /90 /100");
189 compar(const void *a, const void *b)
191 return (((const struct p_times *) a)->pt_pctcpu >
192 ((const struct p_times *) b)->pt_pctcpu)? -1: 1;