1 /* $NetBSD: pigs.c,v 1.5 1997/05/24 00:48:29 jtc Exp $ */ 2 3 /*- 4 * Copyright (c) 1980, 1992, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 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. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #ifndef lint 37 #if 0 38 static char sccsid[] = "@(#)pigs.c 8.2 (Berkeley) 9/23/93"; 39 #endif 40 static char rcsid[] = "$NetBSD: pigs.c,v 1.5 1997/05/24 00:48:29 jtc Exp $"; 41 #endif /* not lint */ 42 43 /* 44 * Pigs display from Bill Reeves at Lucasfilm 45 */ 46 47 #include <sys/param.h> 48 #include <sys/dkstat.h> 49 #include <sys/dir.h> 50 #include <sys/time.h> 51 #include <sys/proc.h> 52 #include <sys/sysctl.h> 53 54 #include <curses.h> 55 #include <math.h> 56 #include <nlist.h> 57 #include <pwd.h> 58 #include <stdlib.h> 59 #include <string.h> 60 61 #include "extern.h" 62 #include "systat.h" 63 64 int compar __P((const void *, const void *)); 65 66 static int nproc; 67 static struct p_times { 68 float pt_pctcpu; 69 struct kinfo_proc *pt_kp; 70 } *pt; 71 72 static long stime[CPUSTATES]; 73 static int fscale; 74 static double lccpu; 75 76 WINDOW * 77 openpigs() 78 { 79 return (subwin(stdscr, LINES-5-1, 0, 5, 0)); 80 } 81 82 void 83 closepigs(w) 84 WINDOW *w; 85 { 86 if (w == NULL) 87 return; 88 wclear(w); 89 wrefresh(w); 90 delwin(w); 91 } 92 93 94 void 95 showpigs() 96 { 97 register int i, j, y, k; 98 struct eproc *ep; 99 float total; 100 int factor; 101 char *uname, *pname, pidname[30]; 102 103 if (pt == NULL) 104 return; 105 /* Accumulate the percent of cpu per user. */ 106 total = 0.0; 107 for (i = 0; i <= nproc; i++) { 108 /* Accumulate the percentage. */ 109 total += pt[i].pt_pctcpu; 110 } 111 112 if (total < 1.0) 113 total = 1.0; 114 factor = 50.0/total; 115 116 qsort(pt, nproc + 1, sizeof (struct p_times), compar); 117 y = 1; 118 i = nproc + 1; 119 if (i > getmaxy(wnd)-1) 120 i = getmaxy(wnd)-1; 121 for (k = 0; i > 0 && pt[k].pt_pctcpu > 0.01; i--, y++, k++) { 122 if (pt[k].pt_kp == NULL) { 123 uname = ""; 124 pname = "<idle>"; 125 } 126 else { 127 ep = &pt[k].pt_kp->kp_eproc; 128 uname = user_from_uid(ep->e_ucred.cr_uid, 0); 129 pname = pt[k].pt_kp->kp_proc.p_comm; 130 } 131 wmove(wnd, y, 0); 132 wclrtoeol(wnd); 133 mvwaddstr(wnd, y, 0, uname); 134 sprintf(pidname, "%10.10s", pname, 0); 135 mvwaddstr(wnd, y, 9, pidname); 136 wmove(wnd, y, 20); 137 for (j = pt[k].pt_pctcpu*factor + 0.5; j > 0; j--) 138 waddch(wnd, 'X'); 139 } 140 wmove(wnd, y, 0); wclrtobot(wnd); 141 } 142 143 static struct nlist namelist[] = { 144 #define X_FIRST 0 145 #define X_CPTIME 0 146 { "_cp_time" }, 147 #define X_CCPU 1 148 { "_ccpu" }, 149 #define X_FSCALE 2 150 { "_fscale" }, 151 152 { "" } 153 }; 154 155 int 156 initpigs() 157 { 158 fixpt_t ccpu; 159 160 if (namelist[X_FIRST].n_type == 0) { 161 if (kvm_nlist(kd, namelist)) { 162 nlisterr(namelist); 163 return(0); 164 } 165 if (namelist[X_FIRST].n_type == 0) { 166 error("namelist failed"); 167 return(0); 168 } 169 } 170 KREAD(NPTR(X_CPTIME), stime, sizeof (stime)); 171 NREAD(X_CCPU, &ccpu, LONG); 172 NREAD(X_FSCALE, &fscale, LONG); 173 lccpu = log((double) ccpu / fscale); 174 175 return(1); 176 } 177 178 void 179 fetchpigs() 180 { 181 register int i; 182 register float time; 183 register struct proc *pp; 184 register float *pctp; 185 struct kinfo_proc *kpp; 186 long ctime[CPUSTATES]; 187 double t; 188 static int lastnproc = 0; 189 190 if (namelist[X_FIRST].n_type == 0) 191 return; 192 if ((kpp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nproc)) == NULL) { 193 error("%s", kvm_geterr(kd)); 194 if (pt) 195 free(pt); 196 return; 197 } 198 if (nproc > lastnproc) { 199 free(pt); 200 if ((pt = 201 malloc((nproc + 1) * sizeof(struct p_times))) == NULL) { 202 error("Out of memory"); 203 die(0); 204 } 205 } 206 lastnproc = nproc; 207 /* 208 * calculate %cpu for each proc 209 */ 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; 214 time = pp->p_swtime; 215 if (time == 0 || (pp->p_flag & P_INMEM) == 0) 216 *pctp = 0; 217 else 218 *pctp = ((double) pp->p_pctcpu / 219 fscale) / (1.0 - exp(time * lccpu)); 220 } 221 /* 222 * and for the imaginary "idle" process 223 */ 224 KREAD(NPTR(X_CPTIME), ctime, sizeof (ctime)); 225 t = 0; 226 for (i = 0; i < CPUSTATES; i++) 227 t += ctime[i] - stime[i]; 228 if (t == 0.0) 229 t = 1.0; 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++) 233 stime[i] = ctime[i]; 234 } 235 236 void 237 labelpigs() 238 { 239 wmove(wnd, 0, 0); 240 wclrtoeol(wnd); 241 mvwaddstr(wnd, 0, 20, 242 "/0 /10 /20 /30 /40 /50 /60 /70 /80 /90 /100"); 243 } 244 245 int 246 compar(a, b) 247 const void *a, *b; 248 { 249 return (((struct p_times *) a)->pt_pctcpu > 250 ((struct p_times *) b)->pt_pctcpu)? -1: 1; 251 } 252