2021-11-09 12:18:02 +03:00
|
|
|
/* $NetBSD: pigs.c,v 1.34 2021/11/09 09:18:02 nia Exp $ */
|
1995-01-20 11:51:48 +03:00
|
|
|
|
1995-01-20 11:30:50 +03: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.
|
2003-08-07 15:13:06 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1995-01-20 11:30:50 +03:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
1997-07-21 11:04:56 +04:00
|
|
|
#include <sys/cdefs.h>
|
1995-01-20 11:30:50 +03:00
|
|
|
#ifndef lint
|
1995-01-20 11:51:48 +03:00
|
|
|
#if 0
|
1995-01-20 11:30:50 +03:00
|
|
|
static char sccsid[] = "@(#)pigs.c 8.2 (Berkeley) 9/23/93";
|
1995-01-20 11:51:48 +03:00
|
|
|
#endif
|
2021-11-09 12:18:02 +03:00
|
|
|
__RCSID("$NetBSD: pigs.c,v 1.34 2021/11/09 09:18:02 nia Exp $");
|
1995-01-20 11:30:50 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Pigs display from Bill Reeves at Lucasfilm
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
2000-06-04 05:53:51 +04:00
|
|
|
#include <sys/sched.h>
|
1995-01-20 11:30:50 +03:00
|
|
|
#include <sys/sysctl.h>
|
|
|
|
|
|
|
|
#include <curses.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <pwd.h>
|
2012-11-23 07:33:05 +04:00
|
|
|
#include <errno.h>
|
1995-01-20 11:30:50 +03:00
|
|
|
#include <stdlib.h>
|
1995-04-29 09:54:42 +04:00
|
|
|
#include <string.h>
|
1995-01-20 11:30:50 +03:00
|
|
|
|
|
|
|
#include "systat.h"
|
2005-12-25 00:14:50 +03:00
|
|
|
#include "extern.h"
|
1999-02-22 00:48:07 +03:00
|
|
|
#include "ps.h"
|
1995-01-20 11:30:50 +03:00
|
|
|
|
2000-07-05 15:03:20 +04:00
|
|
|
int compare_pctcpu(const void *, const void *);
|
1995-01-20 11:30:50 +03:00
|
|
|
|
1999-02-22 00:48:07 +03:00
|
|
|
int nproc;
|
|
|
|
struct p_times *pt;
|
1995-01-20 11:30:50 +03:00
|
|
|
|
2012-11-23 07:33:05 +04:00
|
|
|
uint64_t stime[CPUSTATES];
|
|
|
|
uint64_t mempages;
|
1999-02-22 00:48:07 +03:00
|
|
|
int fscale;
|
|
|
|
double lccpu;
|
1995-01-20 11:30:50 +03:00
|
|
|
|
2000-01-09 02:12:36 +03:00
|
|
|
#ifndef P_ZOMBIE
|
|
|
|
#define P_ZOMBIE(p) ((p)->p_stat == SZOMB)
|
|
|
|
#endif
|
|
|
|
|
1995-01-20 11:30:50 +03:00
|
|
|
WINDOW *
|
2000-07-05 15:03:20 +04:00
|
|
|
openpigs(void)
|
1995-01-20 11:30:50 +03:00
|
|
|
{
|
1998-07-12 09:59:00 +04:00
|
|
|
|
2003-02-24 13:10:00 +03:00
|
|
|
return (subwin(stdscr, -1, 0, 5, 0));
|
1995-01-20 11:30:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-07-05 15:03:20 +04:00
|
|
|
closepigs(WINDOW *w)
|
1995-01-20 11:30:50 +03:00
|
|
|
{
|
1998-07-12 09:59:00 +04:00
|
|
|
|
1995-01-20 11:30:50 +03:00
|
|
|
if (w == NULL)
|
|
|
|
return;
|
|
|
|
wclear(w);
|
|
|
|
wrefresh(w);
|
|
|
|
delwin(w);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2000-07-05 15:03:20 +04:00
|
|
|
showpigs(void)
|
1995-01-20 11:30:50 +03:00
|
|
|
{
|
2000-06-04 22:29:13 +04:00
|
|
|
int i, y, k;
|
2002-05-04 22:44:27 +04:00
|
|
|
struct kinfo_proc2 *kp;
|
1995-01-20 11:30:50 +03:00
|
|
|
float total;
|
|
|
|
int factor;
|
1999-02-19 07:59:00 +03:00
|
|
|
const char *pname;
|
|
|
|
char pidname[30], pidstr[7], usrstr[9];
|
1995-01-20 11:30:50 +03:00
|
|
|
|
|
|
|
if (pt == NULL)
|
|
|
|
return;
|
2004-02-13 14:36:08 +03:00
|
|
|
/* Accumulate the percent of CPU per user. */
|
1995-01-20 11:30:50 +03:00
|
|
|
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;
|
|
|
|
|
1999-02-22 00:48:07 +03:00
|
|
|
qsort(pt, nproc + 1, sizeof (struct p_times), compare_pctcpu);
|
1995-01-20 11:30:50 +03:00
|
|
|
y = 1;
|
|
|
|
i = nproc + 1;
|
1997-05-24 04:48:24 +04:00
|
|
|
if (i > getmaxy(wnd)-1)
|
|
|
|
i = getmaxy(wnd)-1;
|
1995-01-20 11:30:50 +03:00
|
|
|
for (k = 0; i > 0 && pt[k].pt_pctcpu > 0.01; i--, y++, k++) {
|
|
|
|
if (pt[k].pt_kp == NULL) {
|
|
|
|
pname = "<idle>";
|
1999-02-19 07:59:00 +03:00
|
|
|
pidstr[0] = '\0';
|
|
|
|
usrstr[0] = '\0';
|
1995-01-20 11:30:50 +03:00
|
|
|
}
|
|
|
|
else {
|
2002-05-04 22:44:27 +04:00
|
|
|
kp = pt[k].pt_kp;
|
|
|
|
pname = kp->p_comm;
|
|
|
|
snprintf(pidstr, sizeof(pidstr), "%5d", kp->p_pid);
|
|
|
|
snprintf(usrstr, sizeof(usrstr), "%8s",
|
|
|
|
user_from_uid(kp->p_uid, 0));
|
1995-01-20 11:30:50 +03:00
|
|
|
}
|
|
|
|
wmove(wnd, y, 0);
|
|
|
|
wclrtoeol(wnd);
|
1999-02-19 07:59:00 +03:00
|
|
|
mvwaddstr(wnd, y, 0, usrstr);
|
|
|
|
mvwaddstr(wnd, y, 9, pidstr);
|
|
|
|
(void)snprintf(pidname, sizeof(pidname), "%9.9s", pname);
|
|
|
|
mvwaddstr(wnd, y, 15, pidname);
|
2000-06-04 22:29:13 +04:00
|
|
|
mvwhline(wnd, y, 25, 'X', pt[k].pt_pctcpu*factor + 0.5);
|
1995-01-20 11:30:50 +03:00
|
|
|
}
|
|
|
|
wmove(wnd, y, 0); wclrtobot(wnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-07-05 15:03:20 +04:00
|
|
|
initpigs(void)
|
1995-01-20 11:30:50 +03:00
|
|
|
{
|
|
|
|
fixpt_t ccpu;
|
2012-11-23 07:33:05 +04:00
|
|
|
size_t len;
|
1995-01-20 11:30:50 +03:00
|
|
|
|
2000-06-04 20:06:25 +04:00
|
|
|
(void) fetch_cptime(stime);
|
2012-11-23 07:33:05 +04:00
|
|
|
|
|
|
|
len = sizeof(mempages);
|
2012-11-23 07:46:35 +04:00
|
|
|
if (sysctlbyname("hw.physmem64", &mempages, &len, NULL, 0))
|
|
|
|
error("can't get \"hw.physmem64\": %s", strerror(errno));
|
2012-11-23 07:33:05 +04:00
|
|
|
|
|
|
|
len = sizeof(ccpu);
|
|
|
|
if (sysctlbyname("kern.ccpu", &ccpu, &len, NULL, 0))
|
|
|
|
error("can't get \"kern.ccpu\": %s", strerror(errno));
|
|
|
|
|
|
|
|
len = sizeof(fscale);
|
|
|
|
if (sysctlbyname("kern.fscale", &fscale, &len, NULL, 0))
|
|
|
|
error("can't get \"kern.fscale\": %s", strerror(errno));
|
|
|
|
|
1995-01-20 11:30:50 +03:00
|
|
|
lccpu = log((double) ccpu / fscale);
|
|
|
|
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-07-05 15:03:20 +04:00
|
|
|
fetchpigs(void)
|
1995-01-20 11:30:50 +03:00
|
|
|
{
|
1997-10-20 03:36:20 +04:00
|
|
|
int i;
|
|
|
|
float *pctp;
|
2002-05-04 22:44:27 +04:00
|
|
|
struct kinfo_proc2 *kpp, *k;
|
2005-02-27 01:12:33 +03:00
|
|
|
u_int64_t cputime[CPUSTATES];
|
1995-01-20 11:30:50 +03:00
|
|
|
double t;
|
|
|
|
static int lastnproc = 0;
|
|
|
|
|
2002-05-04 22:44:27 +04:00
|
|
|
if ((kpp = kvm_getproc2(kd, KERN_PROC_ALL, 0, sizeof(*kpp),
|
|
|
|
&nproc)) == NULL) {
|
1995-01-20 11:30:50 +03:00
|
|
|
error("%s", kvm_geterr(kd));
|
2021-11-09 12:18:02 +03:00
|
|
|
free(pt);
|
1995-01-20 11:30:50 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (nproc > lastnproc) {
|
2021-11-09 12:18:02 +03:00
|
|
|
if (reallocarr(&pt, nproc + 1, sizeof(struct p_times)) != 0) {
|
1995-01-20 11:30:50 +03:00
|
|
|
error("Out of memory");
|
|
|
|
die(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lastnproc = nproc;
|
|
|
|
/*
|
|
|
|
* calculate %cpu for each proc
|
|
|
|
*/
|
|
|
|
for (i = 0; i < nproc; i++) {
|
2002-05-04 22:44:27 +04:00
|
|
|
pt[i].pt_kp = k = &kpp[i];
|
1995-01-20 11:30:50 +03:00
|
|
|
pctp = &pt[i].pt_pctcpu;
|
2009-10-22 01:11:57 +04:00
|
|
|
|
|
|
|
if (k->p_swtime == 0 || k->p_stat == SZOMB)
|
1995-01-20 11:30:50 +03:00
|
|
|
*pctp = 0;
|
|
|
|
else
|
2002-05-04 22:44:27 +04:00
|
|
|
*pctp = ((double) k->p_pctcpu /
|
2005-02-27 01:12:33 +03:00
|
|
|
fscale) / (1.0 - exp(k->p_swtime * lccpu));
|
1995-01-20 11:30:50 +03:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* and for the imaginary "idle" process
|
|
|
|
*/
|
2005-02-27 01:12:33 +03:00
|
|
|
(void) fetch_cptime(cputime);
|
1995-01-20 11:30:50 +03:00
|
|
|
t = 0;
|
|
|
|
for (i = 0; i < CPUSTATES; i++)
|
2005-02-27 01:12:33 +03:00
|
|
|
t += cputime[i] - stime[i];
|
1995-01-20 11:30:50 +03:00
|
|
|
if (t == 0.0)
|
|
|
|
t = 1.0;
|
|
|
|
pt[nproc].pt_kp = NULL;
|
2005-02-27 01:12:33 +03:00
|
|
|
pt[nproc].pt_pctcpu = (cputime[CP_IDLE] - stime[CP_IDLE]) / t;
|
1995-01-20 11:30:50 +03:00
|
|
|
for (i = 0; i < CPUSTATES; i++)
|
2005-02-27 01:12:33 +03:00
|
|
|
stime[i] = cputime[i];
|
1995-01-20 11:30:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-07-05 15:03:20 +04:00
|
|
|
labelpigs(void)
|
1995-01-20 11:30:50 +03:00
|
|
|
{
|
|
|
|
wmove(wnd, 0, 0);
|
|
|
|
wclrtoeol(wnd);
|
1999-02-19 07:59:00 +03:00
|
|
|
mvwaddstr(wnd, 0, 25, "/0 /10 /20 /30 /40 /50 /60 /70 /80 /90 /100");
|
1995-01-20 11:30:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-07-05 15:03:20 +04:00
|
|
|
compare_pctcpu(const void *a, const void *b)
|
1995-01-20 11:30:50 +03:00
|
|
|
{
|
2005-02-27 01:12:33 +03:00
|
|
|
return (((const struct p_times *) a)->pt_pctcpu >
|
|
|
|
((const struct p_times *) b)->pt_pctcpu)? -1: 1;
|
1995-01-20 11:30:50 +03:00
|
|
|
}
|