2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1992, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 static char sccsid[] = "@(#)pigs.c 8.2 (Berkeley) 9/23/93";
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
42 * Pigs display from Bill Reeves at Lucasfilm
45 #include <sys/param.h>
47 #include <sys/sysctl.h>
59 int compar(const void *, const void *);
62 static struct p_times {
64 struct kinfo_proc *pt_kp;
73 return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0));
90 const char *uname, *pname;
96 qsort(pt, nproc, sizeof (struct p_times), compar);
99 if (i > getmaxy(wnd)-2)
101 for (k = 0; i > 0 && pt[k].pt_pctcpu > 0.01; i--, y++, k++) {
102 uname = user_from_uid(pt[k].pt_kp->ki_uid, 0);
103 pname = pt[k].pt_kp->ki_comm;
106 mvwaddstr(wnd, y, 0, uname);
107 snprintf(pidname, sizeof(pidname), "%10.10s", pname);
108 mvwaddstr(wnd, y, 9, pidname);
110 for (j = pt[k].pt_pctcpu * 50 + 0.5; j > 0; j--)
113 wmove(wnd, y, 0); wclrtobot(wnd);
124 err = sysctlbyname("kern.ccpu", &ccpu, &len, NULL, 0);
125 if (err || len != sizeof(ccpu)) {
130 len = sizeof(fscale);
131 err = sysctlbyname("kern.fscale", &fscale, &len, NULL, 0);
132 if (err || len != sizeof(fscale)) {
133 perror("kern.fscale");
137 lccpu = log((double) ccpu / fscale);
148 struct kinfo_proc *kpp;
149 static int lastnproc = 0;
151 if ((kpp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nproc)) == NULL) {
152 error("%s", kvm_geterr(kd));
157 if (nproc > lastnproc) {
160 malloc(nproc * sizeof(struct p_times))) == NULL) {
161 error("Out of memory");
167 * calculate %cpu for each proc
169 for (i = 0; i < nproc; i++) {
170 pt[i].pt_kp = &kpp[i];
171 pctp = &pt[i].pt_pctcpu;
172 ftime = kpp[i].ki_swtime;
173 if (ftime == 0 || (kpp[i].ki_flag & P_INMEM) == 0)
176 *pctp = ((double) kpp[i].ki_pctcpu /
177 fscale) / (1.0 - exp(ftime * lccpu));
186 mvwaddstr(wnd, 0, 20,
187 "/0% /10 /20 /30 /40 /50 /60 /70 /80 /90 /100");
191 compar(const void *a, const void *b)
193 return (((const struct p_times *) a)->pt_pctcpu >
194 ((const struct p_times *) b)->pt_pctcpu)? -1: 1;