MFC rev 1.2:
[dragonfly.git] / usr.bin / systat / pigs.c
blobe88be1d6dcd6f26ca86fc1ed7b3c13e5d7572c06
1 /*-
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
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.
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.
33 * @(#)pigs.c 8.2 (Berkeley) 9/23/93
35 * $DragonFly: src/usr.bin/systat/pigs.c,v 1.14 2007/02/16 23:11:40 corecode Exp $
39 * Pigs display from Bill Reeves at Lucasfilm
42 #define _KERNEL_STRUCTURES
43 #include <sys/param.h>
44 #include <sys/time.h>
45 #include <sys/user.h>
46 #include <sys/sysctl.h>
48 #include <curses.h>
49 #include <kinfo.h>
50 #include <math.h>
51 #include <nlist.h>
52 #include <pwd.h>
53 #include <stdlib.h>
55 #include "systat.h"
56 #include "extern.h"
58 int compar(const void *, const void *);
60 static int nproc;
61 static struct p_times {
62 float pt_pctcpu;
63 struct kinfo_proc *pt_kp;
64 } *pt;
66 struct kinfo_cputime old_cp_time;
67 static long fscale;
68 static double lccpu;
70 WINDOW *
71 openpigs(void)
73 return (subwin(stdscr, LINES-5-1, 0, 5, 0));
76 void
77 closepigs(WINDOW *w)
79 if (w == NULL)
80 return;
81 wclear(w);
82 wrefresh(w);
83 delwin(w);
87 void
88 showpigs(void)
90 register int i, j, y, k;
91 float total;
92 int factor;
93 char *uname, *pname, pidname[30];
95 if (pt == NULL)
96 return;
97 /* Accumulate the percent of cpu per user. */
98 total = 0.0;
99 for (i = 0; i <= nproc; i++) {
100 /* Accumulate the percentage. */
101 total += pt[i].pt_pctcpu;
104 if (total < 1.0)
105 total = 1.0;
106 factor = 50.0/total;
108 qsort(pt, nproc + 1, sizeof (struct p_times), compar);
109 y = 1;
110 i = nproc + 1;
111 if (i > wnd->_maxy-1)
112 i = wnd->_maxy-1;
113 for (k = 0; i > 0; i--, y++, k++) {
114 char buf[256];
115 if (pt[k].pt_pctcpu <= 0.01 &&
116 (pt[k].pt_kp == NULL ||
117 pt[k].pt_kp->kp_lwp.kl_slptime > 1)
119 --y;
120 continue;
122 if (pt[k].pt_kp == NULL) {
123 uname = "";
124 pname = "<idle>";
125 } else {
126 uname = (char *)user_from_uid(pt[k].pt_kp->kp_uid, 0);
127 pname = pt[k].pt_kp->kp_comm;
129 wmove(wnd, y, 0);
130 wclrtoeol(wnd);
131 mvwaddstr(wnd, y, 0, uname);
132 snprintf(pidname, sizeof(pidname), "%10.10s", pname);
133 mvwaddstr(wnd, y, 9, pidname);
134 wmove(wnd, y, 20);
135 for (j = pt[k].pt_pctcpu*factor + 0.5; j > 0; j--)
136 waddch(wnd, 'X');
138 wmove(wnd, y, 0); wclrtobot(wnd);
141 static struct nlist namelist[] = {
142 #define X_FIRST 0
143 #define X_FSCALE 0
144 { "_fscale" },
146 { "" }
150 initpigs(void)
152 int ccpu;
154 if (namelist[X_FIRST].n_type == 0) {
155 if (kvm_nlist(kd, namelist)) {
156 nlisterr(namelist);
157 return(0);
159 if (namelist[X_FIRST].n_type == 0) {
160 error("namelist failed");
161 return(0);
164 if (kinfo_get_sched_cputime(&old_cp_time))
165 err(1, "kinfo_get_sched_cputime");
166 if (kinfo_get_sched_ccpu(&ccpu))
167 err(1, "kinfo_get_sched_ccpu");
169 NREAD(X_FSCALE, &fscale, LONG);
170 lccpu = log((double) ccpu / fscale);
172 return(1);
175 void
176 fetchpigs(void)
178 int i;
179 float time;
180 float *pctp;
181 struct kinfo_proc *kpp, *pp;
182 struct kinfo_cputime cp_time, diff_cp_time;
183 double t;
184 static int lastnproc = 0;
186 if (namelist[X_FIRST].n_type == 0)
187 return;
188 if ((kpp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nproc)) == NULL) {
189 error("%s", kvm_geterr(kd));
190 if (pt)
191 free(pt);
192 return;
194 if (nproc > lastnproc) {
195 free(pt);
196 if ((pt =
197 malloc((nproc + 1) * sizeof(struct p_times))) == NULL) {
198 error("Out of memory");
199 die(0);
202 lastnproc = nproc;
204 * calculate %cpu for each proc
206 for (i = 0; i < nproc; i++) {
207 pt[i].pt_kp = &kpp[i];
208 pp = &kpp[i];
209 pctp = &pt[i].pt_pctcpu;
210 time = pp->kp_swtime;
211 if (time == 0 || (pp->kp_flags & P_SWAPPEDOUT))
212 *pctp = 0;
213 else
214 *pctp = ((double) pp->kp_lwp.kl_pctcpu /
215 fscale) / (1.0 - exp(time * lccpu));
218 * and for the imaginary "idle" process
220 if (kinfo_get_sched_cputime(&cp_time))
221 err(1, "kinfo_get_sched_cputime");
222 diff_cp_time.cp_user = cp_time.cp_user - old_cp_time.cp_user;
223 diff_cp_time.cp_nice = cp_time.cp_nice - old_cp_time.cp_nice;
224 diff_cp_time.cp_sys = cp_time.cp_sys - old_cp_time.cp_sys;
225 diff_cp_time.cp_intr = cp_time.cp_intr - old_cp_time.cp_intr;
226 diff_cp_time.cp_idle = cp_time.cp_idle - old_cp_time.cp_idle;
227 old_cp_time = cp_time;
228 t = diff_cp_time.cp_user + diff_cp_time.cp_nice +
229 diff_cp_time.cp_sys + diff_cp_time.cp_intr +
230 diff_cp_time.cp_idle;
231 if (t == 0.0)
232 t = 1.0;
233 pt[nproc].pt_kp = NULL;
234 pt[nproc].pt_pctcpu = diff_cp_time.cp_idle / t;
237 void
238 labelpigs(void)
240 wmove(wnd, 0, 0);
241 wclrtoeol(wnd);
242 mvwaddstr(wnd, 0, 20,
243 "/0 /10 /20 /30 /40 /50 /60 /70 /80 /90 /100");
247 compar(const void *a, const void *b)
249 struct p_times *pta = (struct p_times *)a;
250 struct p_times *ptb = (struct p_times *)b;
251 float d;
254 * Check overall cpu percentage first.
256 d = pta->pt_pctcpu - ptb->pt_pctcpu;
257 if (d > 0.10)
258 return(-1); /* a is better */
259 else if (d < -0.10)
260 return(1); /* b is better */
262 if (pta->pt_kp == NULL && ptb->pt_kp == NULL)
263 return(0);
264 if (ptb->pt_kp == NULL)
265 return(-1); /* a is better */
266 if (pta->pt_kp == NULL)
267 return(1); /* b is better */
269 * Then check sleep times and run status.
271 if (pta->pt_kp->kp_lwp.kl_slptime < ptb->pt_kp->kp_lwp.kl_slptime)
272 return(-1);
273 if (pta->pt_kp->kp_lwp.kl_slptime > ptb->pt_kp->kp_lwp.kl_slptime)
274 return(1);
277 * Runnability
279 if (pta->pt_kp->kp_lwp.kl_stat != ptb->pt_kp->kp_lwp.kl_stat) {
280 if (pta->pt_kp->kp_lwp.kl_stat == LSRUN)
281 return(-1);
282 if (ptb->pt_kp->kp_lwp.kl_stat == LSRUN)
283 return(1);
285 return(0);