pigs.c revision 172143
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 172143 2007-09-11 07:51:03Z ru $");
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
58145800Sdelphij#include "systat.h"
591590Srgrimes#include "extern.h"
601590Srgrimes
6192922Simpint compar(const void *, const void *);
621590Srgrimes
631590Srgrimesstatic int nproc;
641590Srgrimesstatic struct p_times {
651590Srgrimes	float pt_pctcpu;
661590Srgrimes	struct kinfo_proc *pt_kp;
671590Srgrimes} *pt;
681590Srgrimes
6969493Sgallatinstatic int    fscale;
701590Srgrimesstatic double  lccpu;
711590Srgrimes
721590SrgrimesWINDOW *
731590Srgrimesopenpigs()
741590Srgrimes{
75158160Sbde	return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 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;
9487715Smarkm	const char *uname, *pname;
9587715Smarkm	char pidname[30];
961590Srgrimes
971590Srgrimes	if (pt == NULL)
981590Srgrimes		return;
991590Srgrimes
100172143Sru	qsort(pt, nproc, sizeof (struct p_times), compar);
1011590Srgrimes	y = 1;
102172143Sru	i = nproc;
10350635Speter	if (i > wnd->_maxy-1)
10450635Speter		i = wnd->_maxy-1;
1051590Srgrimes	for (k = 0; i > 0 && pt[k].pt_pctcpu > 0.01; i--, y++, k++) {
106172143Sru		uname = user_from_uid(pt[k].pt_kp->ki_uid, 0);
107172143Sru		pname = pt[k].pt_kp->ki_comm;
1081590Srgrimes		wmove(wnd, y, 0);
1091590Srgrimes		wclrtoeol(wnd);
1101590Srgrimes		mvwaddstr(wnd, y, 0, uname);
11136789Simp		snprintf(pidname, sizeof(pidname), "%10.10s", pname);
1121590Srgrimes		mvwaddstr(wnd, y, 9, pidname);
1131590Srgrimes		wmove(wnd, y, 20);
114172143Sru		for (j = pt[k].pt_pctcpu * 50 + 0.5; j > 0; j--)
1151590Srgrimes			waddch(wnd, 'X');
1161590Srgrimes	}
1171590Srgrimes	wmove(wnd, y, 0); wclrtobot(wnd);
1181590Srgrimes}
1191590Srgrimes
1201590Srgrimesint
1211590Srgrimesinitpigs()
1221590Srgrimes{
1231590Srgrimes	fixpt_t ccpu;
12469142Srwatson	size_t len;
12569142Srwatson	int err;
1261590Srgrimes
12769142Srwatson	len = sizeof(ccpu);
12869142Srwatson	err = sysctlbyname("kern.ccpu", &ccpu, &len, NULL, 0);
12969142Srwatson	if (err || len != sizeof(ccpu)) {
13069142Srwatson		perror("kern.ccpu");
13169142Srwatson		return (0);
13269142Srwatson	}
13369142Srwatson
13469142Srwatson	len = sizeof(fscale);
13569142Srwatson	err = sysctlbyname("kern.fscale", &fscale, &len, NULL, 0);
13669142Srwatson	if (err || len != sizeof(fscale)) {
13769142Srwatson		perror("kern.fscale");
13869142Srwatson		return (0);
13969142Srwatson	}
14069142Srwatson
1411590Srgrimes	lccpu = log((double) ccpu / fscale);
1421590Srgrimes
1431590Srgrimes	return(1);
1441590Srgrimes}
1451590Srgrimes
1461590Srgrimesvoid
1471590Srgrimesfetchpigs()
1481590Srgrimes{
14987715Smarkm	int i;
15087715Smarkm	float ftime;
15187715Smarkm	float *pctp;
1521590Srgrimes	struct kinfo_proc *kpp;
1531590Srgrimes	static int lastnproc = 0;
1541590Srgrimes
1551590Srgrimes	if ((kpp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nproc)) == NULL) {
1561590Srgrimes		error("%s", kvm_geterr(kd));
1571590Srgrimes		if (pt)
1581590Srgrimes			free(pt);
1591590Srgrimes		return;
1601590Srgrimes	}
1611590Srgrimes	if (nproc > lastnproc) {
1621590Srgrimes		free(pt);
1631590Srgrimes		if ((pt =
164172143Sru		    malloc(nproc * sizeof(struct p_times))) == NULL) {
1651590Srgrimes			error("Out of memory");
1661590Srgrimes			die(0);
1671590Srgrimes		}
1681590Srgrimes	}
1691590Srgrimes	lastnproc = nproc;
1701590Srgrimes	/*
1711590Srgrimes	 * calculate %cpu for each proc
1721590Srgrimes	 */
1731590Srgrimes	for (i = 0; i < nproc; i++) {
1741590Srgrimes		pt[i].pt_kp = &kpp[i];
1751590Srgrimes		pctp = &pt[i].pt_pctcpu;
17687715Smarkm		ftime = kpp[i].ki_swtime;
17787715Smarkm		if (ftime == 0 || (kpp[i].ki_sflag & PS_INMEM) == 0)
1781590Srgrimes			*pctp = 0;
1791590Srgrimes		else
18069896Smckusick			*pctp = ((double) kpp[i].ki_pctcpu /
18187715Smarkm					fscale) / (1.0 - exp(ftime * lccpu));
1821590Srgrimes	}
1831590Srgrimes}
1841590Srgrimes
1851590Srgrimesvoid
1861590Srgrimeslabelpigs()
1871590Srgrimes{
1881590Srgrimes	wmove(wnd, 0, 0);
1891590Srgrimes	wclrtoeol(wnd);
1901590Srgrimes	mvwaddstr(wnd, 0, 20,
191164689Syar	    "/0%  /10  /20  /30  /40  /50  /60  /70  /80  /90  /100");
1921590Srgrimes}
1931590Srgrimes
1941590Srgrimesint
1951590Srgrimescompar(a, b)
1961590Srgrimes	const void *a, *b;
1971590Srgrimes{
19887715Smarkm	return (((const struct p_times *) a)->pt_pctcpu >
19987715Smarkm		((const struct p_times *) b)->pt_pctcpu)? -1: 1;
2001590Srgrimes}
201