pigs.c revision 175387
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 175387 2008-01-16 19:27:43Z delphij $");
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 *
73175387Sdelphijopenpigs(void)
741590Srgrimes{
75158160Sbde	return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0));
761590Srgrimes}
771590Srgrimes
781590Srgrimesvoid
79175387Sdelphijclosepigs(WINDOW *w)
801590Srgrimes{
811590Srgrimes	if (w == NULL)
821590Srgrimes		return;
831590Srgrimes	wclear(w);
841590Srgrimes	wrefresh(w);
851590Srgrimes	delwin(w);
861590Srgrimes}
871590Srgrimes
881590Srgrimes
891590Srgrimesvoid
90175387Sdelphijshowpigs(void)
911590Srgrimes{
92175387Sdelphij	int i, j, y, k;
9387715Smarkm	const char *uname, *pname;
9487715Smarkm	char pidname[30];
951590Srgrimes
961590Srgrimes	if (pt == NULL)
971590Srgrimes		return;
981590Srgrimes
99172143Sru	qsort(pt, nproc, sizeof (struct p_times), compar);
1001590Srgrimes	y = 1;
101172143Sru	i = nproc;
10250635Speter	if (i > wnd->_maxy-1)
10350635Speter		i = wnd->_maxy-1;
1041590Srgrimes	for (k = 0; i > 0 && pt[k].pt_pctcpu > 0.01; i--, y++, k++) {
105172143Sru		uname = user_from_uid(pt[k].pt_kp->ki_uid, 0);
106172143Sru		pname = pt[k].pt_kp->ki_comm;
1071590Srgrimes		wmove(wnd, y, 0);
1081590Srgrimes		wclrtoeol(wnd);
1091590Srgrimes		mvwaddstr(wnd, y, 0, uname);
11036789Simp		snprintf(pidname, sizeof(pidname), "%10.10s", pname);
1111590Srgrimes		mvwaddstr(wnd, y, 9, pidname);
1121590Srgrimes		wmove(wnd, y, 20);
113172143Sru		for (j = pt[k].pt_pctcpu * 50 + 0.5; j > 0; j--)
1141590Srgrimes			waddch(wnd, 'X');
1151590Srgrimes	}
1161590Srgrimes	wmove(wnd, y, 0); wclrtobot(wnd);
1171590Srgrimes}
1181590Srgrimes
1191590Srgrimesint
120175387Sdelphijinitpigs(void)
1211590Srgrimes{
1221590Srgrimes	fixpt_t ccpu;
12369142Srwatson	size_t len;
12469142Srwatson	int err;
1251590Srgrimes
12669142Srwatson	len = sizeof(ccpu);
12769142Srwatson	err = sysctlbyname("kern.ccpu", &ccpu, &len, NULL, 0);
12869142Srwatson	if (err || len != sizeof(ccpu)) {
12969142Srwatson		perror("kern.ccpu");
13069142Srwatson		return (0);
13169142Srwatson	}
13269142Srwatson
13369142Srwatson	len = sizeof(fscale);
13469142Srwatson	err = sysctlbyname("kern.fscale", &fscale, &len, NULL, 0);
13569142Srwatson	if (err || len != sizeof(fscale)) {
13669142Srwatson		perror("kern.fscale");
13769142Srwatson		return (0);
13869142Srwatson	}
13969142Srwatson
1401590Srgrimes	lccpu = log((double) ccpu / fscale);
1411590Srgrimes
1421590Srgrimes	return(1);
1431590Srgrimes}
1441590Srgrimes
1451590Srgrimesvoid
146175387Sdelphijfetchpigs(void)
1471590Srgrimes{
14887715Smarkm	int i;
14987715Smarkm	float ftime;
15087715Smarkm	float *pctp;
1511590Srgrimes	struct kinfo_proc *kpp;
1521590Srgrimes	static int lastnproc = 0;
1531590Srgrimes
1541590Srgrimes	if ((kpp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nproc)) == NULL) {
1551590Srgrimes		error("%s", kvm_geterr(kd));
1561590Srgrimes		if (pt)
1571590Srgrimes			free(pt);
1581590Srgrimes		return;
1591590Srgrimes	}
1601590Srgrimes	if (nproc > lastnproc) {
1611590Srgrimes		free(pt);
1621590Srgrimes		if ((pt =
163172143Sru		    malloc(nproc * sizeof(struct p_times))) == NULL) {
1641590Srgrimes			error("Out of memory");
1651590Srgrimes			die(0);
1661590Srgrimes		}
1671590Srgrimes	}
1681590Srgrimes	lastnproc = nproc;
1691590Srgrimes	/*
1701590Srgrimes	 * calculate %cpu for each proc
1711590Srgrimes	 */
1721590Srgrimes	for (i = 0; i < nproc; i++) {
1731590Srgrimes		pt[i].pt_kp = &kpp[i];
1741590Srgrimes		pctp = &pt[i].pt_pctcpu;
17587715Smarkm		ftime = kpp[i].ki_swtime;
176172207Sjeff		if (ftime == 0 || (kpp[i].ki_flag & P_INMEM) == 0)
1771590Srgrimes			*pctp = 0;
1781590Srgrimes		else
17969896Smckusick			*pctp = ((double) kpp[i].ki_pctcpu /
18087715Smarkm					fscale) / (1.0 - exp(ftime * lccpu));
1811590Srgrimes	}
1821590Srgrimes}
1831590Srgrimes
1841590Srgrimesvoid
185175387Sdelphijlabelpigs(void)
1861590Srgrimes{
1871590Srgrimes	wmove(wnd, 0, 0);
1881590Srgrimes	wclrtoeol(wnd);
1891590Srgrimes	mvwaddstr(wnd, 0, 20,
190164689Syar	    "/0%  /10  /20  /30  /40  /50  /60  /70  /80  /90  /100");
1911590Srgrimes}
1921590Srgrimes
1931590Srgrimesint
194175387Sdelphijcompar(const void *a, const void *b)
1951590Srgrimes{
19687715Smarkm	return (((const struct p_times *) a)->pt_pctcpu >
19787715Smarkm		((const struct p_times *) b)->pt_pctcpu)? -1: 1;
1981590Srgrimes}
199