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 * 4. Neither the name of the University nor the names of its contributors 141590Srgrimes * may be used to endorse or promote products derived from this software 151590Srgrimes * without specific prior written permission. 161590Srgrimes * 171590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 181590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 191590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 201590Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 211590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 221590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 231590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 241590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 251590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 261590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 271590Srgrimes * SUCH DAMAGE. 281590Srgrimes */ 291590Srgrimes 3094610Sdwmalone#if 0 3194610Sdwmalone#ifndef lint 3294610Sdwmalonestatic char sccsid[] = "@(#)pigs.c 8.2 (Berkeley) 9/23/93"; 3394610Sdwmalone#endif /* not lint */ 3494610Sdwmalone#endif 3594610Sdwmalone 3687715Smarkm#include <sys/cdefs.h> 3787715Smarkm__FBSDID("$FreeBSD$"); 3887715Smarkm 391590Srgrimes/* 401590Srgrimes * Pigs display from Bill Reeves at Lucasfilm 411590Srgrimes */ 421590Srgrimes 431590Srgrimes#include <sys/param.h> 44128230Sbde#include <sys/proc.h> 45128230Sbde#include <sys/sysctl.h> 461590Srgrimes#include <sys/time.h> 4711914Sphk#include <sys/user.h> 481590Srgrimes 491590Srgrimes#include <curses.h> 501590Srgrimes#include <math.h> 511590Srgrimes#include <pwd.h> 521590Srgrimes#include <stdlib.h> 531590Srgrimes 54145800Sdelphij#include "systat.h" 551590Srgrimes#include "extern.h" 561590Srgrimes 5792922Simpint compar(const void *, const void *); 581590Srgrimes 591590Srgrimesstatic int nproc; 601590Srgrimesstatic struct p_times { 611590Srgrimes float pt_pctcpu; 621590Srgrimes struct kinfo_proc *pt_kp; 631590Srgrimes} *pt; 641590Srgrimes 6569493Sgallatinstatic int fscale; 661590Srgrimesstatic double lccpu; 671590Srgrimes 681590SrgrimesWINDOW * 69175387Sdelphijopenpigs(void) 701590Srgrimes{ 71158160Sbde return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0)); 721590Srgrimes} 731590Srgrimes 741590Srgrimesvoid 75175387Sdelphijclosepigs(WINDOW *w) 761590Srgrimes{ 771590Srgrimes if (w == NULL) 781590Srgrimes return; 791590Srgrimes wclear(w); 801590Srgrimes wrefresh(w); 811590Srgrimes delwin(w); 821590Srgrimes} 831590Srgrimes 841590Srgrimesvoid 85175387Sdelphijshowpigs(void) 861590Srgrimes{ 87175387Sdelphij int i, j, y, k; 8887715Smarkm const char *uname, *pname; 8987715Smarkm char pidname[30]; 901590Srgrimes 911590Srgrimes if (pt == NULL) 921590Srgrimes return; 931590Srgrimes 94172143Sru qsort(pt, nproc, sizeof (struct p_times), compar); 951590Srgrimes y = 1; 96172143Sru i = nproc; 9750635Speter if (i > wnd->_maxy-1) 9850635Speter i = wnd->_maxy-1; 991590Srgrimes for (k = 0; i > 0 && pt[k].pt_pctcpu > 0.01; i--, y++, k++) { 100172143Sru uname = user_from_uid(pt[k].pt_kp->ki_uid, 0); 101172143Sru pname = pt[k].pt_kp->ki_comm; 1021590Srgrimes wmove(wnd, y, 0); 1031590Srgrimes wclrtoeol(wnd); 1041590Srgrimes mvwaddstr(wnd, y, 0, uname); 10536789Simp snprintf(pidname, sizeof(pidname), "%10.10s", pname); 1061590Srgrimes mvwaddstr(wnd, y, 9, pidname); 1071590Srgrimes wmove(wnd, y, 20); 108172143Sru for (j = pt[k].pt_pctcpu * 50 + 0.5; j > 0; j--) 1091590Srgrimes waddch(wnd, 'X'); 1101590Srgrimes } 1111590Srgrimes wmove(wnd, y, 0); wclrtobot(wnd); 1121590Srgrimes} 1131590Srgrimes 1141590Srgrimesint 115175387Sdelphijinitpigs(void) 1161590Srgrimes{ 1171590Srgrimes fixpt_t ccpu; 11869142Srwatson size_t len; 11969142Srwatson int err; 1201590Srgrimes 12169142Srwatson len = sizeof(ccpu); 12269142Srwatson err = sysctlbyname("kern.ccpu", &ccpu, &len, NULL, 0); 12369142Srwatson if (err || len != sizeof(ccpu)) { 12469142Srwatson perror("kern.ccpu"); 12569142Srwatson return (0); 12669142Srwatson } 12769142Srwatson 12869142Srwatson len = sizeof(fscale); 12969142Srwatson err = sysctlbyname("kern.fscale", &fscale, &len, NULL, 0); 13069142Srwatson if (err || len != sizeof(fscale)) { 13169142Srwatson perror("kern.fscale"); 13269142Srwatson return (0); 13369142Srwatson } 13469142Srwatson 1351590Srgrimes lccpu = log((double) ccpu / fscale); 1361590Srgrimes 1371590Srgrimes return(1); 1381590Srgrimes} 1391590Srgrimes 1401590Srgrimesvoid 141175387Sdelphijfetchpigs(void) 1421590Srgrimes{ 14387715Smarkm int i; 14487715Smarkm float ftime; 14587715Smarkm float *pctp; 1461590Srgrimes struct kinfo_proc *kpp; 1471590Srgrimes static int lastnproc = 0; 1481590Srgrimes 1491590Srgrimes if ((kpp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nproc)) == NULL) { 1501590Srgrimes error("%s", kvm_geterr(kd)); 1511590Srgrimes if (pt) 1521590Srgrimes free(pt); 1531590Srgrimes return; 1541590Srgrimes } 1551590Srgrimes if (nproc > lastnproc) { 1561590Srgrimes free(pt); 1571590Srgrimes if ((pt = 158172143Sru malloc(nproc * sizeof(struct p_times))) == NULL) { 1591590Srgrimes error("Out of memory"); 1601590Srgrimes die(0); 1611590Srgrimes } 1621590Srgrimes } 1631590Srgrimes lastnproc = nproc; 1641590Srgrimes /* 1651590Srgrimes * calculate %cpu for each proc 1661590Srgrimes */ 1671590Srgrimes for (i = 0; i < nproc; i++) { 1681590Srgrimes pt[i].pt_kp = &kpp[i]; 1691590Srgrimes pctp = &pt[i].pt_pctcpu; 17087715Smarkm ftime = kpp[i].ki_swtime; 171172207Sjeff if (ftime == 0 || (kpp[i].ki_flag & P_INMEM) == 0) 1721590Srgrimes *pctp = 0; 1731590Srgrimes else 17469896Smckusick *pctp = ((double) kpp[i].ki_pctcpu / 17587715Smarkm fscale) / (1.0 - exp(ftime * lccpu)); 1761590Srgrimes } 1771590Srgrimes} 1781590Srgrimes 1791590Srgrimesvoid 180175387Sdelphijlabelpigs(void) 1811590Srgrimes{ 1821590Srgrimes wmove(wnd, 0, 0); 1831590Srgrimes wclrtoeol(wnd); 1841590Srgrimes mvwaddstr(wnd, 0, 20, 185164689Syar "/0% /10 /20 /30 /40 /50 /60 /70 /80 /90 /100"); 1861590Srgrimes} 1871590Srgrimes 1881590Srgrimesint 189175387Sdelphijcompar(const void *a, const void *b) 1901590Srgrimes{ 19187715Smarkm return (((const struct p_times *) a)->pt_pctcpu > 19287715Smarkm ((const struct p_times *) b)->pt_pctcpu)? -1: 1; 1931590Srgrimes} 194