pigs.c revision 50635
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
341590Srgrimes#ifndef lint
351590Srgrimesstatic char sccsid[] = "@(#)pigs.c	8.2 (Berkeley) 9/23/93";
361590Srgrimes#endif /* not lint */
371590Srgrimes
381590Srgrimes/*
391590Srgrimes * Pigs display from Bill Reeves at Lucasfilm
401590Srgrimes */
411590Srgrimes
421590Srgrimes#include <sys/param.h>
431590Srgrimes#include <sys/dkstat.h>
441590Srgrimes#include <sys/time.h>
451590Srgrimes#include <sys/proc.h>
4611914Sphk#include <sys/user.h>
471590Srgrimes#include <sys/sysctl.h>
481590Srgrimes
491590Srgrimes#include <curses.h>
501590Srgrimes#include <math.h>
511590Srgrimes#include <nlist.h>
521590Srgrimes#include <pwd.h>
531590Srgrimes#include <stdlib.h>
541590Srgrimes
551590Srgrimes#include "extern.h"
561590Srgrimes#include "systat.h"
571590Srgrimes
581590Srgrimesint compar __P((const void *, const void *));
591590Srgrimes
601590Srgrimesstatic int nproc;
611590Srgrimesstatic struct p_times {
621590Srgrimes	float pt_pctcpu;
631590Srgrimes	struct kinfo_proc *pt_kp;
641590Srgrimes} *pt;
651590Srgrimes
661590Srgrimesstatic long stime[CPUSTATES];
6737455Sbdestatic long    fscale;
681590Srgrimesstatic double  lccpu;
691590Srgrimes
701590SrgrimesWINDOW *
711590Srgrimesopenpigs()
721590Srgrimes{
731590Srgrimes	return (subwin(stdscr, LINES-5-1, 0, 5, 0));
741590Srgrimes}
751590Srgrimes
761590Srgrimesvoid
771590Srgrimesclosepigs(w)
781590Srgrimes	WINDOW *w;
791590Srgrimes{
801590Srgrimes	if (w == NULL)
811590Srgrimes		return;
821590Srgrimes	wclear(w);
831590Srgrimes	wrefresh(w);
841590Srgrimes	delwin(w);
851590Srgrimes}
861590Srgrimes
871590Srgrimes
881590Srgrimesvoid
891590Srgrimesshowpigs()
901590Srgrimes{
911590Srgrimes	register int i, j, y, k;
921590Srgrimes	struct	eproc *ep;
931590Srgrimes	float total;
941590Srgrimes	int factor;
951590Srgrimes	char *uname, *pname, pidname[30];
961590Srgrimes
971590Srgrimes	if (pt == NULL)
981590Srgrimes		return;
991590Srgrimes	/* Accumulate the percent of cpu per user. */
1001590Srgrimes	total = 0.0;
1011590Srgrimes	for (i = 0; i <= nproc; i++) {
1021590Srgrimes		/* Accumulate the percentage. */
1031590Srgrimes		total += pt[i].pt_pctcpu;
1041590Srgrimes	}
1051590Srgrimes
1061590Srgrimes	if (total < 1.0)
1071590Srgrimes 		total = 1.0;
1081590Srgrimes	factor = 50.0/total;
1091590Srgrimes
1101590Srgrimes        qsort(pt, nproc + 1, sizeof (struct p_times), compar);
1111590Srgrimes	y = 1;
1121590Srgrimes	i = nproc + 1;
11350635Speter	if (i > wnd->_maxy-1)
11450635Speter		i = wnd->_maxy-1;
1151590Srgrimes	for (k = 0; i > 0 && pt[k].pt_pctcpu > 0.01; i--, y++, k++) {
1161590Srgrimes		if (pt[k].pt_kp == NULL) {
1171590Srgrimes			uname = "";
1181590Srgrimes			pname = "<idle>";
1191590Srgrimes		}
1201590Srgrimes		else {
1211590Srgrimes			ep = &pt[k].pt_kp->kp_eproc;
1221590Srgrimes			uname = (char *)user_from_uid(ep->e_ucred.cr_uid, 0);
1231590Srgrimes			pname = pt[k].pt_kp->kp_proc.p_comm;
1241590Srgrimes		}
1251590Srgrimes		wmove(wnd, y, 0);
1261590Srgrimes		wclrtoeol(wnd);
1271590Srgrimes		mvwaddstr(wnd, y, 0, uname);
12836789Simp		snprintf(pidname, sizeof(pidname), "%10.10s", pname);
1291590Srgrimes		mvwaddstr(wnd, y, 9, pidname);
1301590Srgrimes		wmove(wnd, y, 20);
1311590Srgrimes		for (j = pt[k].pt_pctcpu*factor + 0.5; j > 0; j--)
1321590Srgrimes			waddch(wnd, 'X');
1331590Srgrimes	}
1341590Srgrimes	wmove(wnd, y, 0); wclrtobot(wnd);
1351590Srgrimes}
1361590Srgrimes
1371590Srgrimesstatic struct nlist namelist[] = {
1381590Srgrimes#define X_FIRST		0
1391590Srgrimes#define X_CPTIME	0
1401590Srgrimes	{ "_cp_time" },
1411590Srgrimes#define X_CCPU          1
1421590Srgrimes	{ "_ccpu" },
1431590Srgrimes#define X_FSCALE        2
1441590Srgrimes	{ "_fscale" },
1451590Srgrimes
1461590Srgrimes	{ "" }
1471590Srgrimes};
1481590Srgrimes
1491590Srgrimesint
1501590Srgrimesinitpigs()
1511590Srgrimes{
1521590Srgrimes	fixpt_t ccpu;
1531590Srgrimes
1541590Srgrimes	if (namelist[X_FIRST].n_type == 0) {
1551590Srgrimes		if (kvm_nlist(kd, namelist)) {
1561590Srgrimes			nlisterr(namelist);
1571590Srgrimes		        return(0);
1581590Srgrimes		}
1591590Srgrimes		if (namelist[X_FIRST].n_type == 0) {
1601590Srgrimes			error("namelist failed");
1611590Srgrimes			return(0);
1621590Srgrimes		}
1631590Srgrimes	}
1641590Srgrimes	KREAD(NPTR(X_CPTIME), stime, sizeof (stime));
16537455Sbde	NREAD(X_CCPU, &ccpu, sizeof(ccpu));
1661590Srgrimes	NREAD(X_FSCALE,  &fscale, LONG);
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;
1831590Srgrimes
1841590Srgrimes	if (namelist[X_FIRST].n_type == 0)
1851590Srgrimes		return;
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	 */
2181590Srgrimes	KREAD(NPTR(X_CPTIME), ctime, sizeof (ctime));
2191590Srgrimes	t = 0;
2201590Srgrimes	for (i = 0; i < CPUSTATES; i++)
2211590Srgrimes		t += ctime[i] - stime[i];
2221590Srgrimes	if (t == 0.0)
2231590Srgrimes		t = 1.0;
2241590Srgrimes	pt[nproc].pt_kp = NULL;
2251590Srgrimes	pt[nproc].pt_pctcpu = (ctime[CP_IDLE] - stime[CP_IDLE]) / t;
2261590Srgrimes	for (i = 0; i < CPUSTATES; i++)
2271590Srgrimes		stime[i] = ctime[i];
2281590Srgrimes}
2291590Srgrimes
2301590Srgrimesvoid
2311590Srgrimeslabelpigs()
2321590Srgrimes{
2331590Srgrimes	wmove(wnd, 0, 0);
2341590Srgrimes	wclrtoeol(wnd);
2351590Srgrimes	mvwaddstr(wnd, 0, 20,
2361590Srgrimes	    "/0   /10  /20  /30  /40  /50  /60  /70  /80  /90  /100");
2371590Srgrimes}
2381590Srgrimes
2391590Srgrimesint
2401590Srgrimescompar(a, b)
2411590Srgrimes	const void *a, *b;
2421590Srgrimes{
2431590Srgrimes	return (((struct p_times *) a)->pt_pctcpu >
2441590Srgrimes		((struct p_times *) b)->pt_pctcpu)? -1: 1;
2451590Srgrimes}
246