1994-05-27 12:33:43 +00:00
|
|
|
/*-
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2002-04-13 21:09:55 +00:00
|
|
|
#if 0
|
|
|
|
#ifndef lint
|
|
|
|
static char sccsid[] = "@(#)pigs.c 8.2 (Berkeley) 9/23/93";
|
|
|
|
#endif /* not lint */
|
|
|
|
#endif
|
2001-12-12 00:13:37 +00:00
|
|
|
|
2002-04-13 21:09:55 +00:00
|
|
|
#include <sys/cdefs.h>
|
2001-12-12 00:13:37 +00:00
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
/*
|
|
|
|
* Pigs display from Bill Reeves at Lucasfilm
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
2004-04-14 09:01:56 +00:00
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/sysctl.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <sys/time.h>
|
1995-10-29 09:56:53 +00:00
|
|
|
#include <sys/user.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
#include <curses.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2005-05-02 10:50:10 +00:00
|
|
|
#include "systat.h"
|
1994-05-27 12:33:43 +00:00
|
|
|
#include "extern.h"
|
|
|
|
|
2002-03-22 01:42:45 +00:00
|
|
|
int compar(const void *, const void *);
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
static int nproc;
|
|
|
|
static struct p_times {
|
|
|
|
float pt_pctcpu;
|
|
|
|
struct kinfo_proc *pt_kp;
|
|
|
|
} *pt;
|
|
|
|
|
2000-12-01 20:57:02 +00:00
|
|
|
static int fscale;
|
1994-05-27 12:33:43 +00:00
|
|
|
static double lccpu;
|
|
|
|
|
|
|
|
WINDOW *
|
2008-01-16 19:27:43 +00:00
|
|
|
openpigs(void)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2006-04-30 04:26:46 +00:00
|
|
|
return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0));
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-01-16 19:27:43 +00:00
|
|
|
closepigs(WINDOW *w)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
if (w == NULL)
|
|
|
|
return;
|
|
|
|
wclear(w);
|
|
|
|
wrefresh(w);
|
|
|
|
delwin(w);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-01-16 19:27:43 +00:00
|
|
|
showpigs(void)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2008-01-16 19:27:43 +00:00
|
|
|
int i, j, y, k;
|
2001-12-12 00:13:37 +00:00
|
|
|
const char *uname, *pname;
|
|
|
|
char pidname[30];
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
if (pt == NULL)
|
|
|
|
return;
|
|
|
|
|
2007-09-11 07:51:03 +00:00
|
|
|
qsort(pt, nproc, sizeof (struct p_times), compar);
|
1994-05-27 12:33:43 +00:00
|
|
|
y = 1;
|
2007-09-11 07:51:03 +00:00
|
|
|
i = nproc;
|
2015-09-10 22:07:52 +00:00
|
|
|
if (i > getmaxy(wnd)-2)
|
|
|
|
i = getmaxy(wnd)-2;
|
1994-05-27 12:33:43 +00:00
|
|
|
for (k = 0; i > 0 && pt[k].pt_pctcpu > 0.01; i--, y++, k++) {
|
2007-09-11 07:51:03 +00:00
|
|
|
uname = user_from_uid(pt[k].pt_kp->ki_uid, 0);
|
|
|
|
pname = pt[k].pt_kp->ki_comm;
|
1994-05-27 12:33:43 +00:00
|
|
|
wmove(wnd, y, 0);
|
|
|
|
wclrtoeol(wnd);
|
|
|
|
mvwaddstr(wnd, y, 0, uname);
|
1998-06-09 04:17:29 +00:00
|
|
|
snprintf(pidname, sizeof(pidname), "%10.10s", pname);
|
1994-05-27 12:33:43 +00:00
|
|
|
mvwaddstr(wnd, y, 9, pidname);
|
|
|
|
wmove(wnd, y, 20);
|
2007-09-11 07:51:03 +00:00
|
|
|
for (j = pt[k].pt_pctcpu * 50 + 0.5; j > 0; j--)
|
1994-05-27 12:33:43 +00:00
|
|
|
waddch(wnd, 'X');
|
|
|
|
}
|
|
|
|
wmove(wnd, y, 0); wclrtobot(wnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2008-01-16 19:27:43 +00:00
|
|
|
initpigs(void)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
fixpt_t ccpu;
|
2000-11-25 03:49:42 +00:00
|
|
|
size_t len;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
len = sizeof(ccpu);
|
|
|
|
err = sysctlbyname("kern.ccpu", &ccpu, &len, NULL, 0);
|
|
|
|
if (err || len != sizeof(ccpu)) {
|
|
|
|
perror("kern.ccpu");
|
|
|
|
return (0);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2000-11-25 03:49:42 +00:00
|
|
|
|
|
|
|
len = sizeof(fscale);
|
|
|
|
err = sysctlbyname("kern.fscale", &fscale, &len, NULL, 0);
|
|
|
|
if (err || len != sizeof(fscale)) {
|
|
|
|
perror("kern.fscale");
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
lccpu = log((double) ccpu / fscale);
|
|
|
|
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-01-16 19:27:43 +00:00
|
|
|
fetchpigs(void)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2001-12-12 00:13:37 +00:00
|
|
|
int i;
|
|
|
|
float ftime;
|
|
|
|
float *pctp;
|
1994-05-27 12:33:43 +00:00
|
|
|
struct kinfo_proc *kpp;
|
|
|
|
static int lastnproc = 0;
|
|
|
|
|
|
|
|
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 =
|
2007-09-11 07:51:03 +00:00
|
|
|
malloc(nproc * sizeof(struct p_times))) == NULL) {
|
1994-05-27 12:33:43 +00:00
|
|
|
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;
|
2001-12-12 00:13:37 +00:00
|
|
|
ftime = kpp[i].ki_swtime;
|
2007-09-17 05:31:39 +00:00
|
|
|
if (ftime == 0 || (kpp[i].ki_flag & P_INMEM) == 0)
|
1994-05-27 12:33:43 +00:00
|
|
|
*pctp = 0;
|
|
|
|
else
|
Change the proc information returned from the kernel so that it
no longer contains kernel specific data structures, but rather
only scalar values and structures that are already part of the
kernel/user interface, specifically rusage and rtprio. It no
longer contains proc, session, pcred, ucred, procsig, vmspace,
pstats, mtx, sigiolst, klist, callout, pasleep, or mdproc. If
any of these changed in size, ps, w, fstat, gcore, systat, and
top would all stop working. The new structure has over 200 bytes
of unassigned space for future values to be added, yet is nearly
100 bytes smaller per entry than the structure that it replaced.
2000-12-12 07:25:57 +00:00
|
|
|
*pctp = ((double) kpp[i].ki_pctcpu /
|
2001-12-12 00:13:37 +00:00
|
|
|
fscale) / (1.0 - exp(ftime * lccpu));
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-01-16 19:27:43 +00:00
|
|
|
labelpigs(void)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
wmove(wnd, 0, 0);
|
|
|
|
wclrtoeol(wnd);
|
|
|
|
mvwaddstr(wnd, 0, 20,
|
2006-11-27 20:19:05 +00:00
|
|
|
"/0% /10 /20 /30 /40 /50 /60 /70 /80 /90 /100");
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2008-01-16 19:27:43 +00:00
|
|
|
compar(const void *a, const void *b)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2001-12-12 00:13:37 +00:00
|
|
|
return (((const struct p_times *) a)->pt_pctcpu >
|
|
|
|
((const struct p_times *) b)->pt_pctcpu)? -1: 1;
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|