pigs.c revision 69142
11590Srgrimes/*- 21590Srgrimes * Copyright (c) 1980, 1992, 1993 31590Srgrimes * The Regents of the University of California. All rights reserved. 41590Srgrimes * 51590Srgrimes * Redistribution and use in source and binary forms, with or without 61590Srgrimes * modification, are permitted provided that the following conditions 71590Srgrimes * are met: 81590Srgrimes * 1. Redistributions of source code must retain the above copyright 91590Srgrimes * notice, this list of conditions and the following disclaimer. 101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111590Srgrimes * notice, this list of conditions and the following disclaimer in the 121590Srgrimes * documentation and/or other materials provided with the distribution. 131590Srgrimes * 3. All advertising materials mentioning features or use of this software 141590Srgrimes * must display the following acknowledgement: 151590Srgrimes * This product includes software developed by the University of 161590Srgrimes * California, Berkeley and its contributors. 171590Srgrimes * 4. Neither the name of the University nor the names of its contributors 181590Srgrimes * may be used to endorse or promote products derived from this software 191590Srgrimes * without specific prior written permission. 201590Srgrimes * 211590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241590Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311590Srgrimes * SUCH DAMAGE. 3269142Srwatson * 3369142Srwatson * $FreeBSD: head/usr.bin/systat/pigs.c 69142 2000-11-25 03:49:42Z rwatson $ 341590Srgrimes */ 351590Srgrimes 361590Srgrimes#ifndef lint 371590Srgrimesstatic char sccsid[] = "@(#)pigs.c 8.2 (Berkeley) 9/23/93"; 381590Srgrimes#endif /* not lint */ 391590Srgrimes 401590Srgrimes/* 411590Srgrimes * Pigs display from Bill Reeves at Lucasfilm 421590Srgrimes */ 431590Srgrimes 441590Srgrimes#include <sys/param.h> 451590Srgrimes#include <sys/dkstat.h> 461590Srgrimes#include <sys/time.h> 471590Srgrimes#include <sys/proc.h> 4811914Sphk#include <sys/user.h> 491590Srgrimes#include <sys/sysctl.h> 501590Srgrimes 511590Srgrimes#include <curses.h> 521590Srgrimes#include <math.h> 531590Srgrimes#include <nlist.h> 541590Srgrimes#include <pwd.h> 551590Srgrimes#include <stdlib.h> 561590Srgrimes 571590Srgrimes#include "extern.h" 581590Srgrimes#include "systat.h" 591590Srgrimes 601590Srgrimesint compar __P((const void *, const void *)); 611590Srgrimes 621590Srgrimesstatic int nproc; 631590Srgrimesstatic struct p_times { 641590Srgrimes float pt_pctcpu; 651590Srgrimes struct kinfo_proc *pt_kp; 661590Srgrimes} *pt; 671590Srgrimes 681590Srgrimesstatic long stime[CPUSTATES]; 6937455Sbdestatic long fscale; 701590Srgrimesstatic double lccpu; 711590Srgrimes 721590SrgrimesWINDOW * 731590Srgrimesopenpigs() 741590Srgrimes{ 751590Srgrimes return (subwin(stdscr, LINES-5-1, 0, 5, 0)); 761590Srgrimes} 771590Srgrimes 781590Srgrimesvoid 791590Srgrimesclosepigs(w) 801590Srgrimes WINDOW *w; 811590Srgrimes{ 821590Srgrimes if (w == NULL) 831590Srgrimes return; 841590Srgrimes wclear(w); 851590Srgrimes wrefresh(w); 861590Srgrimes delwin(w); 871590Srgrimes} 881590Srgrimes 891590Srgrimes 901590Srgrimesvoid 911590Srgrimesshowpigs() 921590Srgrimes{ 931590Srgrimes register int i, j, y, k; 941590Srgrimes struct eproc *ep; 951590Srgrimes float total; 961590Srgrimes int factor; 971590Srgrimes char *uname, *pname, pidname[30]; 981590Srgrimes 991590Srgrimes if (pt == NULL) 1001590Srgrimes return; 1011590Srgrimes /* Accumulate the percent of cpu per user. */ 1021590Srgrimes total = 0.0; 1031590Srgrimes for (i = 0; i <= nproc; i++) { 1041590Srgrimes /* Accumulate the percentage. */ 1051590Srgrimes total += pt[i].pt_pctcpu; 1061590Srgrimes } 1071590Srgrimes 1081590Srgrimes if (total < 1.0) 1091590Srgrimes total = 1.0; 1101590Srgrimes factor = 50.0/total; 1111590Srgrimes 1121590Srgrimes qsort(pt, nproc + 1, sizeof (struct p_times), compar); 1131590Srgrimes y = 1; 1141590Srgrimes i = nproc + 1; 11550635Speter if (i > wnd->_maxy-1) 11650635Speter i = wnd->_maxy-1; 1171590Srgrimes for (k = 0; i > 0 && pt[k].pt_pctcpu > 0.01; i--, y++, k++) { 1181590Srgrimes if (pt[k].pt_kp == NULL) { 1191590Srgrimes uname = ""; 1201590Srgrimes pname = "<idle>"; 1211590Srgrimes } 1221590Srgrimes else { 1231590Srgrimes ep = &pt[k].pt_kp->kp_eproc; 1241590Srgrimes uname = (char *)user_from_uid(ep->e_ucred.cr_uid, 0); 1251590Srgrimes pname = pt[k].pt_kp->kp_proc.p_comm; 1261590Srgrimes } 1271590Srgrimes wmove(wnd, y, 0); 1281590Srgrimes wclrtoeol(wnd); 1291590Srgrimes mvwaddstr(wnd, y, 0, uname); 13036789Simp snprintf(pidname, sizeof(pidname), "%10.10s", pname); 1311590Srgrimes mvwaddstr(wnd, y, 9, pidname); 1321590Srgrimes wmove(wnd, y, 20); 1331590Srgrimes for (j = pt[k].pt_pctcpu*factor + 0.5; j > 0; j--) 1341590Srgrimes waddch(wnd, 'X'); 1351590Srgrimes } 1361590Srgrimes wmove(wnd, y, 0); wclrtobot(wnd); 1371590Srgrimes} 1381590Srgrimes 1391590Srgrimesint 1401590Srgrimesinitpigs() 1411590Srgrimes{ 1421590Srgrimes fixpt_t ccpu; 14369142Srwatson size_t len; 14469142Srwatson int err; 1451590Srgrimes 14669142Srwatson len = sizeof(stime); 14769142Srwatson err = sysctlbyname("kern.cp_time", &stime, &len, NULL, 0); 14869142Srwatson if (err || len != sizeof(stime)) { 14969142Srwatson perror("kern.cp_time"); 15069142Srwatson return (0); 1511590Srgrimes } 15269142Srwatson 15369142Srwatson len = sizeof(ccpu); 15469142Srwatson err = sysctlbyname("kern.ccpu", &ccpu, &len, NULL, 0); 15569142Srwatson if (err || len != sizeof(ccpu)) { 15669142Srwatson perror("kern.ccpu"); 15769142Srwatson return (0); 15869142Srwatson } 15969142Srwatson 16069142Srwatson len = sizeof(fscale); 16169142Srwatson err = sysctlbyname("kern.fscale", &fscale, &len, NULL, 0); 16269142Srwatson if (err || len != sizeof(fscale)) { 16369142Srwatson perror("kern.fscale"); 16469142Srwatson return (0); 16569142Srwatson } 16669142Srwatson 1671590Srgrimes lccpu = log((double) ccpu / fscale); 1681590Srgrimes 1691590Srgrimes return(1); 1701590Srgrimes} 1711590Srgrimes 1721590Srgrimesvoid 1731590Srgrimesfetchpigs() 1741590Srgrimes{ 1751590Srgrimes register int i; 1761590Srgrimes register float time; 1771590Srgrimes register struct proc *pp; 1781590Srgrimes register float *pctp; 1791590Srgrimes struct kinfo_proc *kpp; 1801590Srgrimes long ctime[CPUSTATES]; 1811590Srgrimes double t; 1821590Srgrimes static int lastnproc = 0; 18369142Srwatson size_t len; 18469142Srwatson int err; 1851590Srgrimes 1861590Srgrimes if ((kpp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nproc)) == NULL) { 1871590Srgrimes error("%s", kvm_geterr(kd)); 1881590Srgrimes if (pt) 1891590Srgrimes free(pt); 1901590Srgrimes return; 1911590Srgrimes } 1921590Srgrimes if (nproc > lastnproc) { 1931590Srgrimes free(pt); 1941590Srgrimes if ((pt = 1951590Srgrimes malloc((nproc + 1) * sizeof(struct p_times))) == NULL) { 1961590Srgrimes error("Out of memory"); 1971590Srgrimes die(0); 1981590Srgrimes } 1991590Srgrimes } 2001590Srgrimes lastnproc = nproc; 2011590Srgrimes /* 2021590Srgrimes * calculate %cpu for each proc 2031590Srgrimes */ 2041590Srgrimes for (i = 0; i < nproc; i++) { 2051590Srgrimes pt[i].pt_kp = &kpp[i]; 2061590Srgrimes pp = &kpp[i].kp_proc; 2071590Srgrimes pctp = &pt[i].pt_pctcpu; 2081590Srgrimes time = pp->p_swtime; 2091590Srgrimes if (time == 0 || (pp->p_flag & P_INMEM) == 0) 2101590Srgrimes *pctp = 0; 2111590Srgrimes else 2128874Srgrimes *pctp = ((double) pp->p_pctcpu / 2131590Srgrimes fscale) / (1.0 - exp(time * lccpu)); 2141590Srgrimes } 2151590Srgrimes /* 2161590Srgrimes * and for the imaginary "idle" process 2171590Srgrimes */ 21869142Srwatson len = sizeof(ctime); 21969142Srwatson err = sysctlbyname("kern.cp_time", &ctime, &len, NULL, 0); 22069142Srwatson if (err || len != sizeof(ctime)) { 22169142Srwatson perror("kern.cp_time"); 22269142Srwatson return; 22369142Srwatson } 2241590Srgrimes t = 0; 2251590Srgrimes for (i = 0; i < CPUSTATES; i++) 2261590Srgrimes t += ctime[i] - stime[i]; 2271590Srgrimes if (t == 0.0) 2281590Srgrimes t = 1.0; 2291590Srgrimes pt[nproc].pt_kp = NULL; 2301590Srgrimes pt[nproc].pt_pctcpu = (ctime[CP_IDLE] - stime[CP_IDLE]) / t; 2311590Srgrimes for (i = 0; i < CPUSTATES; i++) 2321590Srgrimes stime[i] = ctime[i]; 2331590Srgrimes} 2341590Srgrimes 2351590Srgrimesvoid 2361590Srgrimeslabelpigs() 2371590Srgrimes{ 2381590Srgrimes wmove(wnd, 0, 0); 2391590Srgrimes wclrtoeol(wnd); 2401590Srgrimes mvwaddstr(wnd, 0, 20, 2411590Srgrimes "/0 /10 /20 /30 /40 /50 /60 /70 /80 /90 /100"); 2421590Srgrimes} 2431590Srgrimes 2441590Srgrimesint 2451590Srgrimescompar(a, b) 2461590Srgrimes const void *a, *b; 2471590Srgrimes{ 2481590Srgrimes return (((struct p_times *) a)->pt_pctcpu > 2491590Srgrimes ((struct p_times *) b)->pt_pctcpu)? -1: 1; 2501590Srgrimes} 251