1 /*- 2 * Copyright (c) 1980, 1992, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 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. 20 * 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. 32 */ 33 34 #ifndef lint 35 static char sccsid[] = "@(#)pigs.c 8.2 (Berkeley) 9/23/93"; 36 #endif /* not lint */ 37 38 /* 39 * Pigs display from Bill Reeves at Lucasfilm 40 */ 41 42 #include <sys/param.h> 43 #include <sys/dkstat.h> 44 #include <sys/dir.h> 45 #include <sys/time.h> 46 #include <sys/proc.h> 47 #include <sys/user.h> 48 #include <sys/sysctl.h> 49 50 #include <curses.h> 51 #include <math.h> 52 #include <nlist.h> 53 #include <pwd.h> 54 #include <stdlib.h> 55 56 #include "extern.h" 57 #include "systat.h" 58 59 int compar __P((const void *, const void *)); 60 61 static int nproc; 62 static struct p_times { 63 float pt_pctcpu; 64 struct kinfo_proc *pt_kp; 65 } *pt; 66 67 static long stime[CPUSTATES]; 68 static int fscale; 69 static double lccpu; 70 71 WINDOW * 72 openpigs() 73 { 74 return (subwin(stdscr, LINES-5-1, 0, 5, 0)); 75 } 76 77 void 78 closepigs(w) 79 WINDOW *w; 80 { 81 if (w == NULL) 82 return; 83 wclear(w); 84 wrefresh(w); 85 delwin(w); 86 } 87 88 89 void 90 showpigs() 91 { 92 register int i, j, y, k; 93 struct eproc *ep; 94 float total; 95 int factor; 96 char *uname, *pname, pidname[30]; 97 98 if (pt == NULL) 99 return; 100 /* Accumulate the percent of cpu per user. */ 101 total = 0.0; 102 for (i = 0; i <= nproc; i++) { 103 /* Accumulate the percentage. */ 104 total += pt[i].pt_pctcpu; 105 } 106 107 if (total < 1.0) 108 total = 1.0; 109 factor = 50.0/total; 110 111 qsort(pt, nproc + 1, sizeof (struct p_times), compar); 112 y = 1; 113 i = nproc + 1; 114 if (i > wnd->maxy-1) 115 i = wnd->maxy-1; 116 for (k = 0; i > 0 && pt[k].pt_pctcpu > 0.01; i--, y++, k++) { 117 if (pt[k].pt_kp == NULL) { 118 uname = ""; 119 pname = "<idle>"; 120 } 121 else { 122 ep = &pt[k].pt_kp->kp_eproc; 123 uname = (char *)user_from_uid(ep->e_ucred.cr_uid, 0); 124 pname = pt[k].pt_kp->kp_proc.p_comm; 125 } 126 wmove(wnd, y, 0); 127 wclrtoeol(wnd); 128 mvwaddstr(wnd, y, 0, uname); 129 sprintf(pidname, "%10.10s", pname, 0); 130 mvwaddstr(wnd, y, 9, pidname); 131 wmove(wnd, y, 20); 132 for (j = pt[k].pt_pctcpu*factor + 0.5; j > 0; j--) 133 waddch(wnd, 'X'); 134 } 135 wmove(wnd, y, 0); wclrtobot(wnd); 136 } 137 138 static struct nlist namelist[] = { 139 #define X_FIRST 0 140 #define X_CPTIME 0 141 { "_cp_time" }, 142 #define X_CCPU 1 143 { "_ccpu" }, 144 #define X_FSCALE 2 145 { "_fscale" }, 146 147 { "" } 148 }; 149 150 int 151 initpigs() 152 { 153 fixpt_t ccpu; 154 155 if (namelist[X_FIRST].n_type == 0) { 156 if (kvm_nlist(kd, namelist)) { 157 nlisterr(namelist); 158 return(0); 159 } 160 if (namelist[X_FIRST].n_type == 0) { 161 error("namelist failed"); 162 return(0); 163 } 164 } 165 KREAD(NPTR(X_CPTIME), stime, sizeof (stime)); 166 NREAD(X_CCPU, &ccpu, LONG); 167 NREAD(X_FSCALE, &fscale, LONG); 168 lccpu = log((double) ccpu / fscale); 169 170 return(1); 171 } 172 173 void 174 fetchpigs() 175 { 176 register int i; 177 register float time; 178 register struct proc *pp; 179 register float *pctp; 180 struct kinfo_proc *kpp; 181 long ctime[CPUSTATES]; 182 double t; 183 static int lastnproc = 0; 184 185 if (namelist[X_FIRST].n_type == 0) 186 return; 187 if ((kpp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nproc)) == NULL) { 188 error("%s", kvm_geterr(kd)); 189 if (pt) 190 free(pt); 191 return; 192 } 193 if (nproc > lastnproc) { 194 free(pt); 195 if ((pt = 196 malloc((nproc + 1) * sizeof(struct p_times))) == NULL) { 197 error("Out of memory"); 198 die(0); 199 } 200 } 201 lastnproc = nproc; 202 /* 203 * calculate %cpu for each proc 204 */ 205 for (i = 0; i < nproc; i++) { 206 pt[i].pt_kp = &kpp[i]; 207 pp = &kpp[i].kp_proc; 208 pctp = &pt[i].pt_pctcpu; 209 time = pp->p_swtime; 210 if (time == 0 || (pp->p_flag & P_INMEM) == 0) 211 *pctp = 0; 212 else 213 *pctp = ((double) pp->p_pctcpu / 214 fscale) / (1.0 - exp(time * lccpu)); 215 } 216 /* 217 * and for the imaginary "idle" process 218 */ 219 KREAD(NPTR(X_CPTIME), ctime, sizeof (ctime)); 220 t = 0; 221 for (i = 0; i < CPUSTATES; i++) 222 t += ctime[i] - stime[i]; 223 if (t == 0.0) 224 t = 1.0; 225 pt[nproc].pt_kp = NULL; 226 pt[nproc].pt_pctcpu = (ctime[CP_IDLE] - stime[CP_IDLE]) / t; 227 for (i = 0; i < CPUSTATES; i++) 228 stime[i] = ctime[i]; 229 } 230 231 void 232 labelpigs() 233 { 234 wmove(wnd, 0, 0); 235 wclrtoeol(wnd); 236 mvwaddstr(wnd, 0, 20, 237 "/0 /10 /20 /30 /40 /50 /60 /70 /80 /90 /100"); 238 } 239 240 int 241 compar(a, b) 242 const void *a, *b; 243 { 244 return (((struct p_times *) a)->pt_pctcpu > 245 ((struct p_times *) b)->pt_pctcpu)? -1: 1; 246 } 247