Add a '-l' flag to show CPU load for the various states, similar to what

"top" does (thinking of it, i could have as well used the same format line!)

This only makes sense when "-w" option is also specified, because the
load is computed as the difference between subsequent samples.

I think this (and the "-d" feature which shows differences in the
network statistics counts) would also make sense in the standard
vmstat and netstat.
This commit is contained in:
Luigi Rizzo 2001-12-14 01:10:50 +00:00
parent c8b4c292c0
commit 8de7d83bc0

View File

@ -66,6 +66,7 @@
char *progname;
int iflag = 0;
int lflag = 0; /* print cpu load info */
int rflag = 0;
int sflag = 0;
int pflag = 0;
@ -78,7 +79,7 @@ extern int optind;
void
usage()
{
fprintf(stderr, "\n%s [-nrsi] [-p proto] [-w wait]\n", progname);
fprintf(stderr, "\n%s [-nrsil] [-p proto] [-w wait]\n", progname);
#ifdef BRIDGING
fprintf(stderr, " proto: {ip|tcp|udp|icmp|bdg}\n\n");
#else
@ -743,7 +744,7 @@ main(int argc, char *argv[])
progname = argv[0];
while ((c = getopt(argc, argv, "dinrsp:w:")) != -1) {
while ((c = getopt(argc, argv, "dilnrsp:w:")) != -1) {
switch (c) {
case 'd': /* print deltas in stats every w seconds */
delta++ ;
@ -759,6 +760,9 @@ main(int argc, char *argv[])
case 'i':
iflag++;
break;
case 'l':
lflag++;
break;
case 's':
sflag++;
rflag = 0;
@ -793,6 +797,7 @@ main(int argc, char *argv[])
printf("\033[H%s", ctime(&t.tv_sec));
}
print_routing(proto);
print_load_stats();
stats(proto);
if (wflag) {
sleep(wflag);
@ -801,6 +806,45 @@ main(int argc, char *argv[])
exit(0);
}
int
print_load_stats(void)
{
static u_int32_t cp_time[5];
u_int32_t new_cp_time[5];
int l;
int shz;
static int stathz ;
if (!lflag || !wflag)
return 0;
l = sizeof(new_cp_time) ;
bzero(new_cp_time, l);
if (sysctlbyname("kern.cp_time", new_cp_time, &l, NULL, 0) < 0) {
warn("sysctl: retrieving cp_time length");
return 0;
}
if (stathz == 0) {
struct clockinfo ci;
bzero (&ci, sizeof(ci));
l = sizeof(ci) ;
if (sysctlbyname("kern.clockrate", &ci, &l, NULL, 0) < 0) {
warn("sysctl: retrieving clockinfo length");
return 0;
}
stathz = ci.stathz ;
bcopy(new_cp_time, cp_time, sizeof(cp_time));
}
shz = stathz * wflag ;
if (shz == 0)
shz = 1;
#define X(i) ( (double)(new_cp_time[i] - cp_time[i])*100/shz )
printf("\nUSER %5.2f%% NICE %5.2f%% SYS %5.2f%% "
"INTR %5.2f%% IDLE %5.2f%%\n",
X(0), X(1), X(2), X(3), X(4) );
bcopy(new_cp_time, cp_time, sizeof(cp_time));
}
#ifdef BRIDGING
/* print bridge statistics */
int