freebsd-dev/usr.bin/systat/pigs.c
Bruce Evans 8aa22952a9 Show the load average in the tcp display (it was already shown, perhaps
not very usefully, in all other displays).  This was the original point
of the PR.

Move the load average up by 2 so that it starts in row 0 for all windows
(2 lines above it were wasted for all other windows except vmstat).
Move everything below it up by 2 or 3 (3 for icmp and icmp6 which had
an extra blank line due from not compensating for the foot-shooting in
note (3); only ip and ip6 compensated).  Reduce the magic numbers related
to this.

Notes by the submitter:
%%%
1. All the subwin() calls are identical using #define MAINWIN_ROW 3
    (systat.h).
2. The load average is at the top of the window.
3. Each display starts on the fourth line.  I made changes to those
    displays that shifted the start line (i.e., icmp).  This entailed a
    lot of changes within the comments at the top of those displays.
4. For ip6, I shifted the "Input next-header histogram" column down one
    row to separate it from "IPv6 Output".  I raised "bad scope packets"
    and "address selection failed" up one row to stay with "IPv6 Input"
    (valid?).  They were down one row to probably line up at the bottom,
    but I think they should stick with their fellow items in a column.
5. I condensed ifstat a bit.  It had a lot of empty rows.
%%%

Submitted by:	Se=E1n Farley <sean-freebsd at farley dot org>
PR:		bin/81874
2006-04-30 04:26:46 +00:00

249 lines
5.7 KiB
C

/*-
* Copyright (c) 1980, 1992, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#if 0
#ifndef lint
static char sccsid[] = "@(#)pigs.c 8.2 (Berkeley) 9/23/93";
#endif /* not lint */
#endif
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
/*
* Pigs display from Bill Reeves at Lucasfilm
*/
#include <sys/param.h>
#include <sys/proc.h>
#include <sys/sysctl.h>
#include <sys/time.h>
#include <sys/user.h>
#include <curses.h>
#include <math.h>
#include <pwd.h>
#include <stdlib.h>
#include "systat.h"
#include "extern.h"
int compar(const void *, const void *);
static int nproc;
static struct p_times {
float pt_pctcpu;
struct kinfo_proc *pt_kp;
} *pt;
static long stime[CPUSTATES];
static int fscale;
static double lccpu;
WINDOW *
openpigs()
{
return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0));
}
void
closepigs(w)
WINDOW *w;
{
if (w == NULL)
return;
wclear(w);
wrefresh(w);
delwin(w);
}
void
showpigs()
{
register int i, j, y, k;
float total;
int factor;
const char *uname, *pname;
char pidname[30];
if (pt == NULL)
return;
/* Accumulate the percent of cpu per user. */
total = 0.0;
for (i = 0; i <= nproc; i++) {
/* Accumulate the percentage. */
total += pt[i].pt_pctcpu;
}
if (total < 1.0)
total = 1.0;
factor = 50.0/total;
qsort(pt, nproc + 1, sizeof (struct p_times), compar);
y = 1;
i = nproc + 1;
if (i > wnd->_maxy-1)
i = wnd->_maxy-1;
for (k = 0; i > 0 && pt[k].pt_pctcpu > 0.01; i--, y++, k++) {
if (pt[k].pt_kp == NULL) {
uname = "";
pname = "<idle>";
}
else {
uname = user_from_uid(pt[k].pt_kp->ki_uid, 0);
pname = pt[k].pt_kp->ki_comm;
}
wmove(wnd, y, 0);
wclrtoeol(wnd);
mvwaddstr(wnd, y, 0, uname);
snprintf(pidname, sizeof(pidname), "%10.10s", pname);
mvwaddstr(wnd, y, 9, pidname);
wmove(wnd, y, 20);
for (j = pt[k].pt_pctcpu*factor + 0.5; j > 0; j--)
waddch(wnd, 'X');
}
wmove(wnd, y, 0); wclrtobot(wnd);
}
int
initpigs()
{
fixpt_t ccpu;
size_t len;
int err;
len = sizeof(stime);
err = sysctlbyname("kern.cp_time", &stime, &len, NULL, 0);
if (err || len != sizeof(stime)) {
perror("kern.cp_time");
return (0);
}
len = sizeof(ccpu);
err = sysctlbyname("kern.ccpu", &ccpu, &len, NULL, 0);
if (err || len != sizeof(ccpu)) {
perror("kern.ccpu");
return (0);
}
len = sizeof(fscale);
err = sysctlbyname("kern.fscale", &fscale, &len, NULL, 0);
if (err || len != sizeof(fscale)) {
perror("kern.fscale");
return (0);
}
lccpu = log((double) ccpu / fscale);
return(1);
}
void
fetchpigs()
{
int i;
float ftime;
float *pctp;
struct kinfo_proc *kpp;
long c_time[CPUSTATES];
double t;
static int lastnproc = 0;
size_t len;
int err;
if ((kpp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nproc)) == NULL) {
error("%s", kvm_geterr(kd));
if (pt)
free(pt);
return;
}
if (nproc > lastnproc) {
free(pt);
if ((pt =
malloc((nproc + 1) * sizeof(struct p_times))) == NULL) {
error("Out of memory");
die(0);
}
}
lastnproc = nproc;
/*
* calculate %cpu for each proc
*/
for (i = 0; i < nproc; i++) {
pt[i].pt_kp = &kpp[i];
pctp = &pt[i].pt_pctcpu;
ftime = kpp[i].ki_swtime;
if (ftime == 0 || (kpp[i].ki_sflag & PS_INMEM) == 0)
*pctp = 0;
else
*pctp = ((double) kpp[i].ki_pctcpu /
fscale) / (1.0 - exp(ftime * lccpu));
}
/*
* and for the imaginary "idle" process
*/
len = sizeof(c_time);
err = sysctlbyname("kern.cp_time", &c_time, &len, NULL, 0);
if (err || len != sizeof(c_time)) {
perror("kern.cp_time");
return;
}
t = 0;
for (i = 0; i < CPUSTATES; i++)
t += c_time[i] - stime[i];
if (t == 0.0)
t = 1.0;
pt[nproc].pt_kp = NULL;
pt[nproc].pt_pctcpu = (c_time[CP_IDLE] - stime[CP_IDLE]) / t;
for (i = 0; i < CPUSTATES; i++)
stime[i] = c_time[i];
}
void
labelpigs()
{
wmove(wnd, 0, 0);
wclrtoeol(wnd);
mvwaddstr(wnd, 0, 20,
"/0 /10 /20 /30 /40 /50 /60 /70 /80 /90 /100");
}
int
compar(a, b)
const void *a, *b;
{
return (((const struct p_times *) a)->pt_pctcpu >
((const struct p_times *) b)->pt_pctcpu)? -1: 1;
}