xref: /freebsd/usr.bin/systat/pigs.c (revision 93b9f50404f9ef0fc00a5ed39ebe09b7cf4086bb)
19b50d902SRodney W. Grimes /*-
29b50d902SRodney W. Grimes  * Copyright (c) 1980, 1992, 1993
39b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
49b50d902SRodney W. Grimes  *
59b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
69b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
79b50d902SRodney W. Grimes  * are met:
89b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
99b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
109b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
119b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
129b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
139b50d902SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
149b50d902SRodney W. Grimes  *    must display the following acknowledgement:
159b50d902SRodney W. Grimes  *	This product includes software developed by the University of
169b50d902SRodney W. Grimes  *	California, Berkeley and its contributors.
179b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
189b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
199b50d902SRodney W. Grimes  *    without specific prior written permission.
209b50d902SRodney W. Grimes  *
219b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
229b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
239b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
249b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
259b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
269b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
279b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
289b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
299b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
309b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
319b50d902SRodney W. Grimes  * SUCH DAMAGE.
329b50d902SRodney W. Grimes  */
339b50d902SRodney W. Grimes 
3472c7c2e6SDavid Malone #if 0
3572c7c2e6SDavid Malone #ifndef lint
3672c7c2e6SDavid Malone static char sccsid[] = "@(#)pigs.c	8.2 (Berkeley) 9/23/93";
3772c7c2e6SDavid Malone #endif /* not lint */
389ff712b0SMark Murray #endif
399b50d902SRodney W. Grimes 
4072c7c2e6SDavid Malone #include <sys/cdefs.h>
4172c7c2e6SDavid Malone __FBSDID("$FreeBSD$");
4272c7c2e6SDavid Malone 
439b50d902SRodney W. Grimes /*
449b50d902SRodney W. Grimes  * Pigs display from Bill Reeves at Lucasfilm
459b50d902SRodney W. Grimes  */
469b50d902SRodney W. Grimes 
479b50d902SRodney W. Grimes #include <sys/param.h>
48a1541efaSBruce Evans #include <sys/proc.h>
49a1541efaSBruce Evans #include <sys/sysctl.h>
509b50d902SRodney W. Grimes #include <sys/time.h>
5152a28d70SPoul-Henning Kamp #include <sys/user.h>
529b50d902SRodney W. Grimes 
539b50d902SRodney W. Grimes #include <curses.h>
549b50d902SRodney W. Grimes #include <math.h>
559b50d902SRodney W. Grimes #include <pwd.h>
569b50d902SRodney W. Grimes #include <stdlib.h>
579b50d902SRodney W. Grimes 
58add62273SXin LI #include "systat.h"
599b50d902SRodney W. Grimes #include "extern.h"
609b50d902SRodney W. Grimes 
613f330d7dSWarner Losh int compar(const void *, const void *);
629b50d902SRodney W. Grimes 
639b50d902SRodney W. Grimes static int nproc;
649b50d902SRodney W. Grimes static struct p_times {
659b50d902SRodney W. Grimes 	float pt_pctcpu;
669b50d902SRodney W. Grimes 	struct kinfo_proc *pt_kp;
679b50d902SRodney W. Grimes } *pt;
689b50d902SRodney W. Grimes 
6936acf3d9SAndrew Gallatin static int    fscale;
709b50d902SRodney W. Grimes static double  lccpu;
719b50d902SRodney W. Grimes 
729b50d902SRodney W. Grimes WINDOW *
7393b9f504SXin LI openpigs(void)
749b50d902SRodney W. Grimes {
758aa22952SBruce Evans 	return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0));
769b50d902SRodney W. Grimes }
779b50d902SRodney W. Grimes 
789b50d902SRodney W. Grimes void
7993b9f504SXin LI closepigs(WINDOW *w)
809b50d902SRodney W. Grimes {
819b50d902SRodney W. Grimes 	if (w == NULL)
829b50d902SRodney W. Grimes 		return;
839b50d902SRodney W. Grimes 	wclear(w);
849b50d902SRodney W. Grimes 	wrefresh(w);
859b50d902SRodney W. Grimes 	delwin(w);
869b50d902SRodney W. Grimes }
879b50d902SRodney W. Grimes 
889b50d902SRodney W. Grimes 
899b50d902SRodney W. Grimes void
9093b9f504SXin LI showpigs(void)
919b50d902SRodney W. Grimes {
9293b9f504SXin LI 	int i, j, y, k;
939ff712b0SMark Murray 	const char *uname, *pname;
949ff712b0SMark Murray 	char pidname[30];
959b50d902SRodney W. Grimes 
969b50d902SRodney W. Grimes 	if (pt == NULL)
979b50d902SRodney W. Grimes 		return;
989b50d902SRodney W. Grimes 
998be3f374SRuslan Ermilov 	qsort(pt, nproc, sizeof (struct p_times), compar);
1009b50d902SRodney W. Grimes 	y = 1;
1018be3f374SRuslan Ermilov 	i = nproc;
1023879bee4SPeter Wemm 	if (i > wnd->_maxy-1)
1033879bee4SPeter Wemm 		i = wnd->_maxy-1;
1049b50d902SRodney W. Grimes 	for (k = 0; i > 0 && pt[k].pt_pctcpu > 0.01; i--, y++, k++) {
10572c7c2e6SDavid Malone 		uname = user_from_uid(pt[k].pt_kp->ki_uid, 0);
1061f7d2501SKirk McKusick 		pname = pt[k].pt_kp->ki_comm;
1079b50d902SRodney W. Grimes 		wmove(wnd, y, 0);
1089b50d902SRodney W. Grimes 		wclrtoeol(wnd);
1099b50d902SRodney W. Grimes 		mvwaddstr(wnd, y, 0, uname);
110448b84a0SWarner Losh 		snprintf(pidname, sizeof(pidname), "%10.10s", pname);
1119b50d902SRodney W. Grimes 		mvwaddstr(wnd, y, 9, pidname);
1129b50d902SRodney W. Grimes 		wmove(wnd, y, 20);
1138be3f374SRuslan Ermilov 		for (j = pt[k].pt_pctcpu * 50 + 0.5; j > 0; j--)
1149b50d902SRodney W. Grimes 			waddch(wnd, 'X');
1159b50d902SRodney W. Grimes 	}
1169b50d902SRodney W. Grimes 	wmove(wnd, y, 0); wclrtobot(wnd);
1179b50d902SRodney W. Grimes }
1189b50d902SRodney W. Grimes 
1199b50d902SRodney W. Grimes int
12093b9f504SXin LI initpigs(void)
1219b50d902SRodney W. Grimes {
1229b50d902SRodney W. Grimes 	fixpt_t ccpu;
12300df2277SRobert Watson 	size_t len;
12400df2277SRobert Watson 	int err;
1259b50d902SRodney W. Grimes 
12600df2277SRobert Watson 	len = sizeof(ccpu);
12700df2277SRobert Watson 	err = sysctlbyname("kern.ccpu", &ccpu, &len, NULL, 0);
12800df2277SRobert Watson 	if (err || len != sizeof(ccpu)) {
12900df2277SRobert Watson 		perror("kern.ccpu");
1309b50d902SRodney W. Grimes 		return (0);
1319b50d902SRodney W. Grimes 	}
13200df2277SRobert Watson 
13300df2277SRobert Watson 	len = sizeof(fscale);
13400df2277SRobert Watson 	err = sysctlbyname("kern.fscale", &fscale, &len, NULL, 0);
13500df2277SRobert Watson 	if (err || len != sizeof(fscale)) {
13600df2277SRobert Watson 		perror("kern.fscale");
13700df2277SRobert Watson 		return (0);
1389b50d902SRodney W. Grimes 	}
13900df2277SRobert Watson 
1409b50d902SRodney W. Grimes 	lccpu = log((double) ccpu / fscale);
1419b50d902SRodney W. Grimes 
1429b50d902SRodney W. Grimes 	return(1);
1439b50d902SRodney W. Grimes }
1449b50d902SRodney W. Grimes 
1459b50d902SRodney W. Grimes void
14693b9f504SXin LI fetchpigs(void)
1479b50d902SRodney W. Grimes {
1489ff712b0SMark Murray 	int i;
1499ff712b0SMark Murray 	float ftime;
1509ff712b0SMark Murray 	float *pctp;
1519b50d902SRodney W. Grimes 	struct kinfo_proc *kpp;
1529b50d902SRodney W. Grimes 	static int lastnproc = 0;
1539b50d902SRodney W. Grimes 
1549b50d902SRodney W. Grimes 	if ((kpp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nproc)) == NULL) {
1559b50d902SRodney W. Grimes 		error("%s", kvm_geterr(kd));
1569b50d902SRodney W. Grimes 		if (pt)
1579b50d902SRodney W. Grimes 			free(pt);
1589b50d902SRodney W. Grimes 		return;
1599b50d902SRodney W. Grimes 	}
1609b50d902SRodney W. Grimes 	if (nproc > lastnproc) {
1619b50d902SRodney W. Grimes 		free(pt);
1629b50d902SRodney W. Grimes 		if ((pt =
1638be3f374SRuslan Ermilov 		    malloc(nproc * sizeof(struct p_times))) == NULL) {
1649b50d902SRodney W. Grimes 			error("Out of memory");
1659b50d902SRodney W. Grimes 			die(0);
1669b50d902SRodney W. Grimes 		}
1679b50d902SRodney W. Grimes 	}
1689b50d902SRodney W. Grimes 	lastnproc = nproc;
1699b50d902SRodney W. Grimes 	/*
1709b50d902SRodney W. Grimes 	 * calculate %cpu for each proc
1719b50d902SRodney W. Grimes 	 */
1729b50d902SRodney W. Grimes 	for (i = 0; i < nproc; i++) {
1739b50d902SRodney W. Grimes 		pt[i].pt_kp = &kpp[i];
1749b50d902SRodney W. Grimes 		pctp = &pt[i].pt_pctcpu;
1759ff712b0SMark Murray 		ftime = kpp[i].ki_swtime;
176b61ce5b0SJeff Roberson 		if (ftime == 0 || (kpp[i].ki_flag & P_INMEM) == 0)
1779b50d902SRodney W. Grimes 			*pctp = 0;
1789b50d902SRodney W. Grimes 		else
1791f7d2501SKirk McKusick 			*pctp = ((double) kpp[i].ki_pctcpu /
1809ff712b0SMark Murray 					fscale) / (1.0 - exp(ftime * lccpu));
1819b50d902SRodney W. Grimes 	}
1829b50d902SRodney W. Grimes }
1839b50d902SRodney W. Grimes 
1849b50d902SRodney W. Grimes void
18593b9f504SXin LI labelpigs(void)
1869b50d902SRodney W. Grimes {
1879b50d902SRodney W. Grimes 	wmove(wnd, 0, 0);
1889b50d902SRodney W. Grimes 	wclrtoeol(wnd);
1899b50d902SRodney W. Grimes 	mvwaddstr(wnd, 0, 20,
19065b02a0fSYaroslav Tykhiy 	    "/0%  /10  /20  /30  /40  /50  /60  /70  /80  /90  /100");
1919b50d902SRodney W. Grimes }
1929b50d902SRodney W. Grimes 
1939b50d902SRodney W. Grimes int
19493b9f504SXin LI compar(const void *a, const void *b)
1959b50d902SRodney W. Grimes {
1969ff712b0SMark Murray 	return (((const struct p_times *) a)->pt_pctcpu >
1979ff712b0SMark Murray 		((const struct p_times *) b)->pt_pctcpu)? -1: 1;
1989b50d902SRodney W. Grimes }
199