pigs.c revision 128230
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. 321590Srgrimes */ 331590Srgrimes 3494610Sdwmalone#if 0 3594610Sdwmalone#ifndef lint 3694610Sdwmalonestatic char sccsid[] = "@(#)pigs.c 8.2 (Berkeley) 9/23/93"; 3794610Sdwmalone#endif /* not lint */ 3894610Sdwmalone#endif 3994610Sdwmalone 4087715Smarkm#include <sys/cdefs.h> 4187715Smarkm__FBSDID("$FreeBSD: head/usr.bin/systat/pigs.c 128230 2004-04-14 09:01:56Z bde $"); 4287715Smarkm 431590Srgrimes/* 441590Srgrimes * Pigs display from Bill Reeves at Lucasfilm 451590Srgrimes */ 461590Srgrimes 471590Srgrimes#include <sys/param.h> 48128230Sbde#include <sys/proc.h> 49128230Sbde#include <sys/sysctl.h> 501590Srgrimes#include <sys/time.h> 5111914Sphk#include <sys/user.h> 521590Srgrimes 531590Srgrimes#include <curses.h> 541590Srgrimes#include <math.h> 551590Srgrimes#include <pwd.h> 561590Srgrimes#include <stdlib.h> 571590Srgrimes 581590Srgrimes#include "extern.h" 591590Srgrimes 6092922Simpint compar(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]; 6969493Sgallatinstatic int 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 float total; 951590Srgrimes int factor; 9687715Smarkm const char *uname, *pname; 9787715Smarkm char 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 { 12394610Sdwmalone uname = user_from_uid(pt[k].pt_kp->ki_uid, 0); 12469896Smckusick pname = pt[k].pt_kp->ki_comm; 1251590Srgrimes } 1261590Srgrimes wmove(wnd, y, 0); 1271590Srgrimes wclrtoeol(wnd); 1281590Srgrimes mvwaddstr(wnd, y, 0, uname); 12936789Simp snprintf(pidname, sizeof(pidname), "%10.10s", pname); 1301590Srgrimes mvwaddstr(wnd, y, 9, pidname); 1311590Srgrimes wmove(wnd, y, 20); 1321590Srgrimes for (j = pt[k].pt_pctcpu*factor + 0.5; j > 0; j--) 1331590Srgrimes waddch(wnd, 'X'); 1341590Srgrimes } 1351590Srgrimes wmove(wnd, y, 0); wclrtobot(wnd); 1361590Srgrimes} 1371590Srgrimes 1381590Srgrimesint 1391590Srgrimesinitpigs() 1401590Srgrimes{ 1411590Srgrimes fixpt_t ccpu; 14269142Srwatson size_t len; 14369142Srwatson int err; 1441590Srgrimes 14569142Srwatson len = sizeof(stime); 14669142Srwatson err = sysctlbyname("kern.cp_time", &stime, &len, NULL, 0); 14769142Srwatson if (err || len != sizeof(stime)) { 14869142Srwatson perror("kern.cp_time"); 14969142Srwatson return (0); 1501590Srgrimes } 15169142Srwatson 15269142Srwatson len = sizeof(ccpu); 15369142Srwatson err = sysctlbyname("kern.ccpu", &ccpu, &len, NULL, 0); 15469142Srwatson if (err || len != sizeof(ccpu)) { 15569142Srwatson perror("kern.ccpu"); 15669142Srwatson return (0); 15769142Srwatson } 15869142Srwatson 15969142Srwatson len = sizeof(fscale); 16069142Srwatson err = sysctlbyname("kern.fscale", &fscale, &len, NULL, 0); 16169142Srwatson if (err || len != sizeof(fscale)) { 16269142Srwatson perror("kern.fscale"); 16369142Srwatson return (0); 16469142Srwatson } 16569142Srwatson 1661590Srgrimes lccpu = log((double) ccpu / fscale); 1671590Srgrimes 1681590Srgrimes return(1); 1691590Srgrimes} 1701590Srgrimes 1711590Srgrimesvoid 1721590Srgrimesfetchpigs() 1731590Srgrimes{ 17487715Smarkm int i; 17587715Smarkm float ftime; 17687715Smarkm float *pctp; 1771590Srgrimes struct kinfo_proc *kpp; 17887715Smarkm long c_time[CPUSTATES]; 1791590Srgrimes double t; 1801590Srgrimes static int lastnproc = 0; 18169142Srwatson size_t len; 18269142Srwatson int err; 1831590Srgrimes 1841590Srgrimes if ((kpp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nproc)) == NULL) { 1851590Srgrimes error("%s", kvm_geterr(kd)); 1861590Srgrimes if (pt) 1871590Srgrimes free(pt); 1881590Srgrimes return; 1891590Srgrimes } 1901590Srgrimes if (nproc > lastnproc) { 1911590Srgrimes free(pt); 1921590Srgrimes if ((pt = 1931590Srgrimes malloc((nproc + 1) * sizeof(struct p_times))) == NULL) { 1941590Srgrimes error("Out of memory"); 1951590Srgrimes die(0); 1961590Srgrimes } 1971590Srgrimes } 1981590Srgrimes lastnproc = nproc; 1991590Srgrimes /* 2001590Srgrimes * calculate %cpu for each proc 2011590Srgrimes */ 2021590Srgrimes for (i = 0; i < nproc; i++) { 2031590Srgrimes pt[i].pt_kp = &kpp[i]; 2041590Srgrimes pctp = &pt[i].pt_pctcpu; 20587715Smarkm ftime = kpp[i].ki_swtime; 20687715Smarkm if (ftime == 0 || (kpp[i].ki_sflag & PS_INMEM) == 0) 2071590Srgrimes *pctp = 0; 2081590Srgrimes else 20969896Smckusick *pctp = ((double) kpp[i].ki_pctcpu / 21087715Smarkm fscale) / (1.0 - exp(ftime * lccpu)); 2111590Srgrimes } 2121590Srgrimes /* 2131590Srgrimes * and for the imaginary "idle" process 2141590Srgrimes */ 21587715Smarkm len = sizeof(c_time); 21687715Smarkm err = sysctlbyname("kern.cp_time", &c_time, &len, NULL, 0); 21787715Smarkm if (err || len != sizeof(c_time)) { 21869142Srwatson perror("kern.cp_time"); 21969142Srwatson return; 22069142Srwatson } 2211590Srgrimes t = 0; 2221590Srgrimes for (i = 0; i < CPUSTATES; i++) 22387715Smarkm t += c_time[i] - stime[i]; 2241590Srgrimes if (t == 0.0) 2251590Srgrimes t = 1.0; 2261590Srgrimes pt[nproc].pt_kp = NULL; 22787715Smarkm pt[nproc].pt_pctcpu = (c_time[CP_IDLE] - stime[CP_IDLE]) / t; 2281590Srgrimes for (i = 0; i < CPUSTATES; i++) 22987715Smarkm stime[i] = c_time[i]; 2301590Srgrimes} 2311590Srgrimes 2321590Srgrimesvoid 2331590Srgrimeslabelpigs() 2341590Srgrimes{ 2351590Srgrimes wmove(wnd, 0, 0); 2361590Srgrimes wclrtoeol(wnd); 2371590Srgrimes mvwaddstr(wnd, 0, 20, 2381590Srgrimes "/0 /10 /20 /30 /40 /50 /60 /70 /80 /90 /100"); 2391590Srgrimes} 2401590Srgrimes 2411590Srgrimesint 2421590Srgrimescompar(a, b) 2431590Srgrimes const void *a, *b; 2441590Srgrimes{ 24587715Smarkm return (((const struct p_times *) a)->pt_pctcpu > 24687715Smarkm ((const struct p_times *) b)->pt_pctcpu)? -1: 1; 2471590Srgrimes} 248