2018-06-08 01:55:47 +00:00
|
|
|
/*-
|
1997-03-23 18:51:21 +00:00
|
|
|
* Top users/processes display for Unix
|
|
|
|
*
|
|
|
|
* This program may be freely redistributed,
|
|
|
|
* but this entire comment MUST remain intact.
|
|
|
|
*
|
|
|
|
* Copyright (c) 1984, 1989, William LeFebvre, Rice University
|
2002-01-24 17:55:40 +00:00
|
|
|
* Copyright (c) 1989 - 1994, William LeFebvre, Northwestern University
|
|
|
|
* Copyright (c) 1994, 1995, William LeFebvre, Argonne National Laboratory
|
|
|
|
* Copyright (c) 1996, William LeFebvre, Group sys Consulting
|
2000-10-04 23:34:16 +00:00
|
|
|
*
|
|
|
|
* $FreeBSD$
|
1997-03-23 18:51:21 +00:00
|
|
|
*/
|
|
|
|
|
2018-05-19 23:19:24 +00:00
|
|
|
#include <sys/types.h>
|
1997-03-23 18:51:21 +00:00
|
|
|
#include <sys/time.h>
|
2018-06-04 04:59:32 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#include <sys/limits.h>
|
2018-06-10 09:15:13 +00:00
|
|
|
#include <sys/resource.h>
|
2018-06-04 04:59:32 +00:00
|
|
|
#include <sys/select.h>
|
|
|
|
#include <sys/signal.h>
|
2016-05-22 04:17:00 +00:00
|
|
|
|
2018-06-12 06:53:31 +00:00
|
|
|
#include <assert.h>
|
2018-08-22 09:49:38 +00:00
|
|
|
#include <err.h>
|
2016-05-22 04:17:00 +00:00
|
|
|
#include <errno.h>
|
2018-06-09 21:40:24 +00:00
|
|
|
#include <getopt.h>
|
2014-05-02 23:30:39 +00:00
|
|
|
#include <jail.h>
|
2018-06-04 04:59:32 +00:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
2018-05-19 23:19:24 +00:00
|
|
|
#include <stdlib.h>
|
2016-05-22 04:17:00 +00:00
|
|
|
#include <signal.h>
|
2018-05-19 23:19:24 +00:00
|
|
|
#include <string.h>
|
2016-05-22 04:17:00 +00:00
|
|
|
#include <unistd.h>
|
1997-03-23 18:51:21 +00:00
|
|
|
|
2016-05-22 04:17:00 +00:00
|
|
|
#include "commands.h"
|
1997-03-23 18:51:21 +00:00
|
|
|
#include "display.h" /* interface to display package */
|
|
|
|
#include "screen.h" /* interface to screen package */
|
|
|
|
#include "top.h"
|
|
|
|
#include "machine.h"
|
|
|
|
#include "utils.h"
|
2016-05-22 04:17:00 +00:00
|
|
|
#include "username.h"
|
1997-03-23 18:51:21 +00:00
|
|
|
|
|
|
|
/* Size of the stdio buffer given to stdout */
|
|
|
|
#define Buffersize 2048
|
|
|
|
|
2018-05-21 09:25:21 +00:00
|
|
|
char copyright[] =
|
2018-05-21 03:36:16 +00:00
|
|
|
"Copyright (c) 1984 through 1996, William LeFebvre";
|
|
|
|
|
2018-05-19 23:19:24 +00:00
|
|
|
typedef void sigret_t;
|
|
|
|
|
1997-03-23 18:51:21 +00:00
|
|
|
/* The buffer that stdio will use */
|
2018-05-21 03:36:16 +00:00
|
|
|
static char stdoutbuf[Buffersize];
|
1997-03-23 18:51:21 +00:00
|
|
|
|
2007-04-14 10:16:52 +00:00
|
|
|
static int fmt_flags = 0;
|
2018-06-02 03:25:15 +00:00
|
|
|
int pcpu_stats = false;
|
2007-04-14 10:16:52 +00:00
|
|
|
|
1997-03-23 18:51:21 +00:00
|
|
|
/* signal handling routines */
|
2018-05-21 00:53:42 +00:00
|
|
|
static sigret_t leave(int);
|
|
|
|
static sigret_t tstop(int);
|
|
|
|
static sigret_t top_winch(int);
|
1997-03-23 18:51:21 +00:00
|
|
|
|
2018-05-21 01:16:26 +00:00
|
|
|
static volatile sig_atomic_t leaveflag;
|
|
|
|
static volatile sig_atomic_t tstopflag;
|
|
|
|
static volatile sig_atomic_t winchflag;
|
1997-03-23 18:51:21 +00:00
|
|
|
|
|
|
|
/* values which need to be accessed by signal handlers */
|
|
|
|
static int max_topn; /* maximum displayable processes */
|
|
|
|
|
|
|
|
/* miscellaneous things */
|
2005-04-14 15:02:03 +00:00
|
|
|
struct process_select ps;
|
2018-06-09 02:14:33 +00:00
|
|
|
pid_t mypid;
|
1997-03-23 18:51:21 +00:00
|
|
|
|
|
|
|
/* pointers to display routines */
|
2018-05-21 03:36:16 +00:00
|
|
|
static void (*d_loadave)(int mpid, double *avenrun) = i_loadave;
|
|
|
|
static void (*d_procstates)(int total, int *brkdn) = i_procstates;
|
|
|
|
static void (*d_cpustates)(int *states) = i_cpustates;
|
|
|
|
static void (*d_memory)(int *stats) = i_memory;
|
|
|
|
static void (*d_arc)(int *stats) = i_arc;
|
|
|
|
static void (*d_carc)(int *stats) = i_carc;
|
|
|
|
static void (*d_swap)(int *stats) = i_swap;
|
|
|
|
static void (*d_message)(void) = i_message;
|
2018-06-03 22:42:54 +00:00
|
|
|
static void (*d_header)(const char *text) = i_header;
|
2018-05-21 03:36:16 +00:00
|
|
|
static void (*d_process)(int line, char *thisline) = i_process;
|
|
|
|
|
|
|
|
static void reset_display(void);
|
1997-03-23 18:51:21 +00:00
|
|
|
|
2018-06-09 21:40:24 +00:00
|
|
|
static const struct option longopts[] = {
|
|
|
|
{ "cpu-display-mode", no_argument, NULL, 'C' }, /* differs from orignal */
|
|
|
|
/* D reserved */
|
|
|
|
{ "thread", no_argument, NULL, 'H' },
|
|
|
|
{ "idle-procs", no_argument, NULL, 'I' },
|
2018-06-09 22:30:10 +00:00
|
|
|
{ "jail", required_argument, NULL, 'J' },
|
|
|
|
{ "per-cpu", no_argument, NULL, 'P' },
|
2018-06-09 21:40:24 +00:00
|
|
|
{ "system-procs", no_argument, NULL, 'S' },
|
|
|
|
{ "thread-id", no_argument, NULL, 'T' }, /* differs from orignal */
|
|
|
|
{ "user", required_argument, NULL, 'U' },
|
|
|
|
{ "all", no_argument, NULL, 'a' },
|
|
|
|
{ "batch", no_argument, NULL, 'b' },
|
|
|
|
/* c reserved */
|
|
|
|
{ "displays", required_argument, NULL, 'd' },
|
|
|
|
{ "interactive", no_argument, NULL, 'i' },
|
|
|
|
{ "jail-id", no_argument, NULL, 'j' },
|
|
|
|
{ "display-mode", required_argument, NULL, 'm' },
|
|
|
|
/* n is identical to batch */
|
|
|
|
{ "sort-order", required_argument, NULL, 'o' },
|
|
|
|
{ "pid", required_argument, NULL, 'p' },
|
|
|
|
{ "quick", no_argument, NULL, 'q' },
|
|
|
|
{ "delay", required_argument, NULL, 's' },
|
|
|
|
{ "threads", no_argument, NULL, 't' },
|
|
|
|
{ "uids", no_argument, NULL, 'u' },
|
|
|
|
{ "version", no_argument, NULL, 'v' },
|
2018-06-09 22:30:10 +00:00
|
|
|
{ "swap", no_argument, NULL, 'w' },
|
2019-01-18 23:26:31 +00:00
|
|
|
{ "system-idle-procs", no_argument, NULL, 'z' },
|
|
|
|
{ NULL, 0, NULL, 0 }
|
2018-06-09 21:40:24 +00:00
|
|
|
};
|
|
|
|
|
2017-08-07 08:45:08 +00:00
|
|
|
static void
|
2018-05-22 02:13:04 +00:00
|
|
|
reset_uids(void)
|
2017-08-07 08:45:08 +00:00
|
|
|
{
|
|
|
|
for (size_t i = 0; i < TOP_MAX_UIDS; ++i)
|
|
|
|
ps.uid[i] = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
add_uid(int uid)
|
|
|
|
{
|
|
|
|
size_t i = 0;
|
|
|
|
|
|
|
|
/* Add the uid if there's room */
|
|
|
|
for (; i < TOP_MAX_UIDS; ++i)
|
|
|
|
{
|
|
|
|
if (ps.uid[i] == -1 || ps.uid[i] == uid)
|
|
|
|
{
|
|
|
|
ps.uid[i] = uid;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (i == TOP_MAX_UIDS);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
rem_uid(int uid)
|
|
|
|
{
|
|
|
|
size_t i = 0;
|
|
|
|
size_t where = TOP_MAX_UIDS;
|
|
|
|
|
|
|
|
/* Look for the user to remove - no problem if it's not there */
|
|
|
|
for (; i < TOP_MAX_UIDS; ++i)
|
|
|
|
{
|
|
|
|
if (ps.uid[i] == -1)
|
|
|
|
break;
|
|
|
|
if (ps.uid[i] == uid)
|
|
|
|
where = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure we don't leave a hole in the middle */
|
|
|
|
if (where != TOP_MAX_UIDS)
|
|
|
|
{
|
|
|
|
ps.uid[where] = ps.uid[i-1];
|
|
|
|
ps.uid[i-1] = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
handle_user(char *buf, size_t buflen)
|
|
|
|
{
|
|
|
|
int rc = 0;
|
|
|
|
int uid = -1;
|
|
|
|
char *buf2 = buf;
|
|
|
|
|
|
|
|
new_message(MT_standout, "Username to show (+ for all): ");
|
2018-06-02 03:25:15 +00:00
|
|
|
if (readline(buf, buflen, false) <= 0)
|
2017-08-07 08:45:08 +00:00
|
|
|
{
|
|
|
|
clear_message();
|
2018-06-08 01:55:47 +00:00
|
|
|
return (rc);
|
2017-08-07 08:45:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (buf[0] == '+' || buf[0] == '-')
|
|
|
|
{
|
|
|
|
if (buf[1] == '\0')
|
|
|
|
{
|
|
|
|
reset_uids();
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
++buf2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((uid = userid(buf2)) == -1)
|
|
|
|
{
|
|
|
|
new_message(MT_standout, " %s: unknown user", buf2);
|
|
|
|
rc = 1;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buf2 == buf)
|
|
|
|
{
|
|
|
|
reset_uids();
|
|
|
|
ps.uid[0] = uid;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buf[0] == '+')
|
|
|
|
{
|
|
|
|
if (add_uid(uid))
|
|
|
|
{
|
|
|
|
new_message(MT_standout, " too many users, reset with '+'");
|
|
|
|
rc = 1;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
rem_uid(uid);
|
|
|
|
|
|
|
|
end:
|
|
|
|
putchar('\r');
|
2018-06-08 01:55:47 +00:00
|
|
|
return (rc);
|
2017-08-07 08:45:08 +00:00
|
|
|
}
|
1997-03-23 18:51:21 +00:00
|
|
|
|
2016-05-22 04:17:00 +00:00
|
|
|
int
|
2019-02-10 13:31:08 +00:00
|
|
|
main(int argc, const char *argv[])
|
1997-03-23 18:51:21 +00:00
|
|
|
{
|
2018-05-20 01:32:27 +00:00
|
|
|
int i;
|
|
|
|
int active_procs;
|
1997-03-23 18:51:21 +00:00
|
|
|
|
|
|
|
struct system_info system_info;
|
|
|
|
struct statics statics;
|
2018-05-21 09:25:21 +00:00
|
|
|
void * processes;
|
1997-03-23 18:51:21 +00:00
|
|
|
|
|
|
|
static char tempbuf1[50];
|
|
|
|
static char tempbuf2[50];
|
2018-06-18 02:06:16 +00:00
|
|
|
sigset_t old_sigmask, new_sigmask;
|
2018-05-20 23:37:30 +00:00
|
|
|
int topn = Infinity;
|
2018-06-10 00:02:56 +00:00
|
|
|
double delay = 2;
|
1997-03-23 18:51:21 +00:00
|
|
|
int displays = 0; /* indicates unspecified */
|
2001-11-04 21:15:52 +00:00
|
|
|
int sel_ret = 0;
|
1997-03-23 18:51:21 +00:00
|
|
|
time_t curr_time;
|
2018-06-03 05:20:11 +00:00
|
|
|
char *(*get_userid)(int) = username;
|
2018-06-03 22:42:54 +00:00
|
|
|
const char *uname_field = "USERNAME";
|
|
|
|
const char *header_text;
|
1997-03-23 18:51:21 +00:00
|
|
|
char *env_top;
|
2018-06-03 23:40:54 +00:00
|
|
|
const char **preset_argv;
|
1997-03-23 18:51:21 +00:00
|
|
|
int preset_argc = 0;
|
2018-06-13 00:45:38 +00:00
|
|
|
const char **av = NULL;
|
|
|
|
int ac = -1;
|
2018-06-02 03:25:15 +00:00
|
|
|
bool do_unames = true;
|
|
|
|
char interactive = 2;
|
1997-03-23 18:51:21 +00:00
|
|
|
char warnings = 0;
|
2018-06-02 03:25:15 +00:00
|
|
|
char topn_specified = false;
|
1997-03-23 18:51:21 +00:00
|
|
|
char ch;
|
|
|
|
char no_command = 1;
|
|
|
|
struct timeval timeout;
|
|
|
|
char *order_name = NULL;
|
|
|
|
int order_index = 0;
|
|
|
|
fd_set readfds;
|
2018-08-22 09:49:38 +00:00
|
|
|
char *nptr;
|
1997-03-23 18:51:21 +00:00
|
|
|
|
|
|
|
/* set the buffer for stdout */
|
|
|
|
#ifdef DEBUG
|
2002-01-24 17:55:40 +00:00
|
|
|
extern FILE *debug;
|
|
|
|
debug = fopen("debug.run", "w");
|
1997-03-23 18:51:21 +00:00
|
|
|
setbuffer(stdout, NULL, 0);
|
|
|
|
#else
|
|
|
|
setbuffer(stdout, stdoutbuf, Buffersize);
|
|
|
|
#endif
|
|
|
|
|
2018-06-09 02:14:33 +00:00
|
|
|
mypid = getpid();
|
|
|
|
|
|
|
|
/* get our name */
|
1997-03-23 18:51:21 +00:00
|
|
|
/* initialize some selection options */
|
2018-06-02 03:25:15 +00:00
|
|
|
ps.idle = true;
|
2018-06-22 00:02:36 +00:00
|
|
|
ps.self = true;
|
2018-06-02 03:25:15 +00:00
|
|
|
ps.system = false;
|
2017-08-07 08:45:08 +00:00
|
|
|
reset_uids();
|
2018-06-02 03:25:15 +00:00
|
|
|
ps.thread = false;
|
2005-05-18 13:30:08 +00:00
|
|
|
ps.wcpu = 1;
|
2014-05-02 23:30:39 +00:00
|
|
|
ps.jid = -1;
|
2018-06-02 03:25:15 +00:00
|
|
|
ps.jail = false;
|
|
|
|
ps.swap = false;
|
|
|
|
ps.kidle = true;
|
2018-06-12 07:31:59 +00:00
|
|
|
ps.pid = -1;
|
1997-03-23 18:51:21 +00:00
|
|
|
ps.command = NULL;
|
2018-06-09 02:14:33 +00:00
|
|
|
ps.thread_id = false;
|
1997-03-23 18:51:21 +00:00
|
|
|
|
|
|
|
/* get preset options from the environment */
|
|
|
|
if ((env_top = getenv("TOP")) != NULL)
|
|
|
|
{
|
|
|
|
av = preset_argv = argparse(env_top, &preset_argc);
|
|
|
|
ac = preset_argc;
|
|
|
|
|
|
|
|
/* set the dummy argument to an explanatory message, in case
|
|
|
|
getopt encounters a bad argument */
|
|
|
|
preset_argv[0] = "while processing environment";
|
|
|
|
}
|
|
|
|
|
|
|
|
/* process options */
|
|
|
|
do {
|
|
|
|
/* if we're done doing the presets, then process the real arguments */
|
|
|
|
if (preset_argc == 0)
|
|
|
|
{
|
|
|
|
ac = argc;
|
|
|
|
av = argv;
|
|
|
|
|
|
|
|
/* this should keep getopt happy... */
|
|
|
|
optind = 1;
|
|
|
|
}
|
|
|
|
|
2019-02-10 13:31:08 +00:00
|
|
|
while ((i = getopt_long(ac, __DECONST(char * const *, av), "CSIHPabijJ:nquvzs:d:U:m:o:p:Ttw", longopts, NULL)) != EOF)
|
1997-03-23 18:51:21 +00:00
|
|
|
{
|
|
|
|
switch(i)
|
|
|
|
{
|
2002-01-24 17:55:40 +00:00
|
|
|
case 'v': /* show version number */
|
2018-08-22 10:07:15 +00:00
|
|
|
errx(0, "version FreeBSD");
|
|
|
|
break;
|
2002-01-24 17:55:40 +00:00
|
|
|
|
1997-03-23 18:51:21 +00:00
|
|
|
case 'u': /* toggle uid/username display */
|
|
|
|
do_unames = !do_unames;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'U': /* display only username's processes */
|
2017-08-07 08:45:08 +00:00
|
|
|
if ((ps.uid[0] = userid(optarg)) == -1)
|
1997-03-23 18:51:21 +00:00
|
|
|
{
|
2018-08-22 10:07:15 +00:00
|
|
|
errx(1, "%s: unknown user\n", optarg);
|
1997-03-23 18:51:21 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'S': /* show system processes */
|
2018-06-02 15:52:18 +00:00
|
|
|
ps.system = true;
|
1997-03-23 18:51:21 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'I': /* show idle processes */
|
|
|
|
ps.idle = !ps.idle;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'i': /* go interactive regardless */
|
2018-06-02 03:25:15 +00:00
|
|
|
interactive = 1;
|
1997-03-23 18:51:21 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'n': /* batch, or non-interactive */
|
|
|
|
case 'b':
|
2018-06-02 03:25:15 +00:00
|
|
|
interactive = 0;
|
1997-03-23 18:51:21 +00:00
|
|
|
break;
|
|
|
|
|
2007-04-14 10:16:52 +00:00
|
|
|
case 'a':
|
|
|
|
fmt_flags ^= FMT_SHOWARGS;
|
|
|
|
break;
|
|
|
|
|
1997-03-23 18:51:21 +00:00
|
|
|
case 'd': /* number of displays to show */
|
|
|
|
if ((i = atoiwi(optarg)) == Invalid || i == 0)
|
|
|
|
{
|
2018-08-22 10:07:15 +00:00
|
|
|
warnx("warning: display count should be positive -- option ignored");
|
1997-03-23 18:51:21 +00:00
|
|
|
warnings++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
displays = i;
|
|
|
|
}
|
|
|
|
break;
|
2018-06-02 15:52:18 +00:00
|
|
|
case 'p': {
|
|
|
|
unsigned long long num;
|
|
|
|
const char *errstr;
|
|
|
|
|
|
|
|
num = strtonum(optarg, 0, INT_MAX, &errstr);
|
|
|
|
if (errstr != NULL || !find_pid(num)) {
|
|
|
|
fprintf(stderr, "%s: unknown pid\n", optarg);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
ps.pid = (pid_t)num;
|
|
|
|
ps.system = true;
|
|
|
|
break;
|
|
|
|
}
|
1997-03-23 18:51:21 +00:00
|
|
|
|
2018-06-10 00:02:56 +00:00
|
|
|
case 's':
|
2018-08-22 09:49:38 +00:00
|
|
|
delay = strtod(optarg, &nptr);
|
|
|
|
if (nptr == optarg) {
|
|
|
|
warnx("warning: invalid delay");
|
|
|
|
delay = 2;
|
|
|
|
warnings++;
|
|
|
|
}
|
2018-06-10 00:02:56 +00:00
|
|
|
if (delay < 0) {
|
2018-08-22 10:07:15 +00:00
|
|
|
warnx("warning: seconds delay should be positive -- using default");
|
2018-06-10 00:02:56 +00:00
|
|
|
delay = 2;
|
|
|
|
warnings++;
|
|
|
|
}
|
|
|
|
|
1997-03-23 18:51:21 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'q': /* be quick about it */
|
2018-06-10 09:15:13 +00:00
|
|
|
errno = 0;
|
|
|
|
i = setpriority(PRIO_PROCESS, 0, PRIO_MIN);
|
|
|
|
if (i == -1 && errno != 0) {
|
2018-08-22 10:07:15 +00:00
|
|
|
warnx("warning: `-q' option failed (%m)");
|
2018-06-10 09:15:13 +00:00
|
|
|
warnings++;
|
|
|
|
}
|
1997-03-23 18:51:21 +00:00
|
|
|
break;
|
|
|
|
|
2004-07-05 12:19:53 +00:00
|
|
|
case 'm': /* select display mode */
|
2004-07-01 09:12:38 +00:00
|
|
|
if (strcmp(optarg, "io") == 0) {
|
|
|
|
displaymode = DISP_IO;
|
|
|
|
} else if (strcmp(optarg, "cpu") == 0) {
|
|
|
|
displaymode = DISP_CPU;
|
|
|
|
} else {
|
2018-08-22 10:07:15 +00:00
|
|
|
errx(1, "warning: `-m' option can only take args 'io' or 'cpu'");
|
2004-07-01 09:12:38 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
1997-03-23 18:51:21 +00:00
|
|
|
case 'o': /* select sort order */
|
|
|
|
order_name = optarg;
|
|
|
|
break;
|
|
|
|
|
1998-08-04 14:10:48 +00:00
|
|
|
case 't':
|
2018-06-09 02:14:33 +00:00
|
|
|
ps.self = !ps.self;
|
1998-08-04 14:10:48 +00:00
|
|
|
break;
|
2005-05-18 13:30:08 +00:00
|
|
|
|
|
|
|
case 'C':
|
|
|
|
ps.wcpu = !ps.wcpu;
|
|
|
|
break;
|
|
|
|
|
2003-07-17 23:56:40 +00:00
|
|
|
case 'H':
|
|
|
|
ps.thread = !ps.thread;
|
|
|
|
break;
|
2005-05-18 13:30:08 +00:00
|
|
|
|
2018-06-09 02:14:33 +00:00
|
|
|
case 'T':
|
|
|
|
ps.thread_id = !ps.thread_id;
|
|
|
|
break;
|
|
|
|
|
2007-04-17 03:12:39 +00:00
|
|
|
case 'j':
|
|
|
|
ps.jail = !ps.jail;
|
|
|
|
break;
|
|
|
|
|
2014-05-02 23:30:39 +00:00
|
|
|
case 'J': /* display only jail's processes */
|
|
|
|
if ((ps.jid = jail_getid(optarg)) == -1)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: unknown jail\n", optarg);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
ps.jail = 1;
|
|
|
|
break;
|
|
|
|
|
2008-01-18 01:43:14 +00:00
|
|
|
case 'P':
|
2011-07-11 16:48:52 +00:00
|
|
|
pcpu_stats = !pcpu_stats;
|
2008-01-18 01:43:14 +00:00
|
|
|
break;
|
|
|
|
|
2016-09-05 08:27:04 +00:00
|
|
|
case 'w':
|
|
|
|
ps.swap = 1;
|
|
|
|
break;
|
|
|
|
|
2011-05-31 15:11:23 +00:00
|
|
|
case 'z':
|
|
|
|
ps.kidle = !ps.kidle;
|
|
|
|
break;
|
|
|
|
|
1997-03-23 18:51:21 +00:00
|
|
|
default:
|
2018-08-22 10:07:15 +00:00
|
|
|
errx(1,
|
|
|
|
"[-abCHIijnPqStuvwz] [-d count] [-m io | cpu] [-o field] [-p pid]\n"
|
|
|
|
" [-s time] [-J jail] [-U username] [number]");
|
1997-03-23 18:51:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get count of top processes to display (if any) */
|
|
|
|
if (optind < ac)
|
|
|
|
{
|
|
|
|
if ((topn = atoiwi(av[optind])) == Invalid)
|
|
|
|
{
|
2018-08-22 10:07:15 +00:00
|
|
|
warnx("warning: process display count should be non-negative -- using default");
|
|
|
|
warnings++;
|
1997-03-23 18:51:21 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-06-02 03:25:15 +00:00
|
|
|
topn_specified = true;
|
1997-03-23 18:51:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* tricky: remember old value of preset_argc & set preset_argc = 0 */
|
|
|
|
i = preset_argc;
|
|
|
|
preset_argc = 0;
|
|
|
|
|
|
|
|
/* repeat only if we really did the preset arguments */
|
|
|
|
} while (i != 0);
|
|
|
|
|
|
|
|
/* set constants for username/uid display correctly */
|
|
|
|
if (!do_unames)
|
|
|
|
{
|
|
|
|
uname_field = " UID ";
|
|
|
|
get_userid = itoa7;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* initialize the kernel memory interface */
|
2018-05-20 23:19:09 +00:00
|
|
|
if (machine_init(&statics) == -1)
|
1997-03-23 18:51:21 +00:00
|
|
|
{
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* determine sorting order index, if necessary */
|
|
|
|
if (order_name != NULL)
|
|
|
|
{
|
|
|
|
if ((order_index = string_index(order_name, statics.order_names)) == -1)
|
|
|
|
{
|
2018-06-08 01:55:47 +00:00
|
|
|
const char * const *pp;
|
1997-03-23 18:51:21 +00:00
|
|
|
|
2018-08-22 10:07:15 +00:00
|
|
|
warnx("'%s' is not a recognized sorting order.", order_name);
|
1997-03-23 18:51:21 +00:00
|
|
|
fprintf(stderr, "\tTry one of these:");
|
|
|
|
pp = statics.order_names;
|
|
|
|
while (*pp != NULL)
|
|
|
|
{
|
|
|
|
fprintf(stderr, " %s", *pp++);
|
|
|
|
}
|
|
|
|
fputc('\n', stderr);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* initialize termcap */
|
|
|
|
init_termcap(interactive);
|
|
|
|
|
|
|
|
/* get the string to use for the process area header */
|
|
|
|
header_text = format_header(uname_field);
|
|
|
|
|
|
|
|
/* initialize display interface */
|
|
|
|
if ((max_topn = display_init(&statics)) == -1)
|
|
|
|
{
|
2018-08-22 10:07:15 +00:00
|
|
|
errx(4, "can't allocate sufficient memory");
|
1997-03-23 18:51:21 +00:00
|
|
|
}
|
2018-06-12 07:31:59 +00:00
|
|
|
|
1997-03-23 18:51:21 +00:00
|
|
|
/* print warning if user requested more processes than we can display */
|
|
|
|
if (topn > max_topn)
|
|
|
|
{
|
2018-08-22 10:07:15 +00:00
|
|
|
warnx("warning: this terminal can only display %d processes.", max_topn);
|
|
|
|
warnings++;
|
1997-03-23 18:51:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* adjust for topn == Infinity */
|
|
|
|
if (topn == Infinity)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* For smart terminals, infinity really means everything that can
|
|
|
|
* be displayed, or Largest.
|
|
|
|
* On dumb terminals, infinity means every process in the system!
|
|
|
|
* We only really want to do that if it was explicitly specified.
|
|
|
|
* This is always the case when "Default_TOPN != Infinity". But if
|
|
|
|
* topn wasn't explicitly specified and we are on a dumb terminal
|
|
|
|
* and the default is Infinity, then (and only then) we use
|
|
|
|
* "Nominal_TOPN" instead.
|
|
|
|
*/
|
|
|
|
topn = smart_terminal ? Largest :
|
|
|
|
(topn_specified ? Largest : Nominal_TOPN);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set header display accordingly */
|
|
|
|
display_header(topn > 0);
|
|
|
|
|
|
|
|
/* determine interactive state */
|
2018-06-02 03:25:15 +00:00
|
|
|
if (interactive == 2)
|
1997-03-23 18:51:21 +00:00
|
|
|
{
|
|
|
|
interactive = smart_terminal;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if # of displays not specified, fill it in */
|
|
|
|
if (displays == 0)
|
|
|
|
{
|
|
|
|
displays = smart_terminal ? Infinity : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* hold interrupt signals while setting up the screen and the handlers */
|
2018-06-18 02:06:16 +00:00
|
|
|
|
|
|
|
sigemptyset(&new_sigmask);
|
|
|
|
sigaddset(&new_sigmask, SIGINT);
|
|
|
|
sigaddset(&new_sigmask, SIGQUIT);
|
|
|
|
sigaddset(&new_sigmask, SIGTSTP);
|
|
|
|
sigprocmask(SIG_BLOCK, &new_sigmask, &old_sigmask);
|
1997-03-23 18:51:21 +00:00
|
|
|
init_screen();
|
2018-05-21 00:53:42 +00:00
|
|
|
signal(SIGINT, leave);
|
|
|
|
signal(SIGQUIT, leave);
|
|
|
|
signal(SIGTSTP, tstop);
|
|
|
|
signal(SIGWINCH, top_winch);
|
2018-06-18 02:06:16 +00:00
|
|
|
sigprocmask(SIG_SETMASK, &old_sigmask, NULL);
|
1997-03-23 18:51:21 +00:00
|
|
|
if (warnings)
|
|
|
|
{
|
|
|
|
fputs("....", stderr);
|
2018-06-10 06:21:51 +00:00
|
|
|
fflush(stderr);
|
|
|
|
sleep(3 * warnings);
|
1997-03-23 18:51:21 +00:00
|
|
|
fputc('\n', stderr);
|
|
|
|
}
|
|
|
|
|
2001-08-06 03:19:22 +00:00
|
|
|
restart:
|
1997-03-23 18:51:21 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* main loop -- repeat while display count is positive or while it
|
|
|
|
* indicates infinity (by being -1)
|
|
|
|
*/
|
|
|
|
|
|
|
|
while ((displays == -1) || (displays-- > 0))
|
|
|
|
{
|
2018-05-21 03:58:15 +00:00
|
|
|
int (*compare)(const void * const, const void * const);
|
2004-07-01 09:12:38 +00:00
|
|
|
|
2018-06-12 07:31:59 +00:00
|
|
|
|
1997-03-23 18:51:21 +00:00
|
|
|
/* get the current stats */
|
|
|
|
get_system_info(&system_info);
|
|
|
|
|
2004-08-16 07:51:22 +00:00
|
|
|
compare = compares[order_index];
|
2004-07-01 09:12:38 +00:00
|
|
|
|
|
|
|
/* get the current set of processes */
|
|
|
|
processes =
|
|
|
|
get_process_info(&system_info, &ps, compare);
|
1997-03-23 18:51:21 +00:00
|
|
|
|
|
|
|
/* display the load averages */
|
|
|
|
(*d_loadave)(system_info.last_pid,
|
|
|
|
system_info.load_avg);
|
|
|
|
|
|
|
|
/* display the current time */
|
|
|
|
/* this method of getting the time SHOULD be fairly portable */
|
|
|
|
time(&curr_time);
|
1999-01-09 20:25:02 +00:00
|
|
|
i_uptime(&system_info.boottime, &curr_time);
|
1997-03-23 18:51:21 +00:00
|
|
|
i_timeofday(&curr_time);
|
|
|
|
|
|
|
|
/* display process state breakdown */
|
|
|
|
(*d_procstates)(system_info.p_total,
|
|
|
|
system_info.procstates);
|
2018-06-23 03:17:11 +00:00
|
|
|
(*d_cpustates)(system_info.cpustates);
|
1997-03-23 18:51:21 +00:00
|
|
|
|
|
|
|
/* display memory stats */
|
|
|
|
(*d_memory)(system_info.memory);
|
2012-06-27 18:08:48 +00:00
|
|
|
(*d_arc)(system_info.arc);
|
Add ZFS compressed ARC stats to top(1)
Provides:
amount of compressed data
logical size of compressed data (how much it would have taken uncompressed)
compression ratio (logical size : total ARC size)
Overhead (space consumed for compression headers)
Example output:
ARC: 31G Total, 18G MFU, 9067M MRU, 2236K Anon, 615M Header, 2947M Other
25G Compressed, 54G Uncompressed, 1.76:1 Ratio, 2265M Overhead
Reviewed by: jpaetzel, smh, imp, jhb (previous version)
MFC after: 2 week
Relnotes: yes
Sponsored by: ScaleEngine Inc.
Differential Revision: https://reviews.freebsd.org/D9829
2017-03-17 00:46:50 +00:00
|
|
|
(*d_carc)(system_info.carc);
|
1997-03-23 18:51:21 +00:00
|
|
|
|
1997-03-23 18:53:01 +00:00
|
|
|
/* display swap stats */
|
|
|
|
(*d_swap)(system_info.swap);
|
|
|
|
|
1997-03-23 18:51:21 +00:00
|
|
|
/* handle message area */
|
|
|
|
(*d_message)();
|
|
|
|
|
|
|
|
/* update the header area */
|
|
|
|
(*d_header)(header_text);
|
2018-06-12 07:31:59 +00:00
|
|
|
|
1997-03-23 18:51:21 +00:00
|
|
|
if (topn > 0)
|
|
|
|
{
|
|
|
|
/* determine number of processes to actually display */
|
|
|
|
/* this number will be the smallest of: active processes,
|
|
|
|
number user requested, number current screen accomodates */
|
2018-05-21 04:40:20 +00:00
|
|
|
active_procs = system_info.p_pactive;
|
1997-03-23 18:51:21 +00:00
|
|
|
if (active_procs > topn)
|
|
|
|
{
|
|
|
|
active_procs = topn;
|
|
|
|
}
|
|
|
|
if (active_procs > max_topn)
|
|
|
|
{
|
|
|
|
active_procs = max_topn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now show the top "n" processes. */
|
|
|
|
for (i = 0; i < active_procs; i++)
|
|
|
|
{
|
2007-04-14 10:16:52 +00:00
|
|
|
(*d_process)(i, format_next_process(processes, get_userid,
|
|
|
|
fmt_flags));
|
1997-03-23 18:51:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* do end-screen processing */
|
|
|
|
u_endscreen(i);
|
|
|
|
|
|
|
|
/* now, flush the output buffer */
|
2002-01-24 17:55:40 +00:00
|
|
|
if (fflush(stdout) != 0)
|
|
|
|
{
|
|
|
|
new_message(MT_standout, " Write error on stdout");
|
|
|
|
putchar('\r');
|
|
|
|
quit(1);
|
|
|
|
}
|
1997-03-23 18:51:21 +00:00
|
|
|
|
|
|
|
/* only do the rest if we have more displays to show */
|
|
|
|
if (displays)
|
|
|
|
{
|
|
|
|
/* switch out for new display on smart terminals */
|
|
|
|
if (smart_terminal)
|
|
|
|
{
|
|
|
|
if (overstrike)
|
|
|
|
{
|
|
|
|
reset_display();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
d_loadave = u_loadave;
|
|
|
|
d_procstates = u_procstates;
|
|
|
|
d_cpustates = u_cpustates;
|
|
|
|
d_memory = u_memory;
|
2012-06-27 18:08:48 +00:00
|
|
|
d_arc = u_arc;
|
Add ZFS compressed ARC stats to top(1)
Provides:
amount of compressed data
logical size of compressed data (how much it would have taken uncompressed)
compression ratio (logical size : total ARC size)
Overhead (space consumed for compression headers)
Example output:
ARC: 31G Total, 18G MFU, 9067M MRU, 2236K Anon, 615M Header, 2947M Other
25G Compressed, 54G Uncompressed, 1.76:1 Ratio, 2265M Overhead
Reviewed by: jpaetzel, smh, imp, jhb (previous version)
MFC after: 2 week
Relnotes: yes
Sponsored by: ScaleEngine Inc.
Differential Revision: https://reviews.freebsd.org/D9829
2017-03-17 00:46:50 +00:00
|
|
|
d_carc = u_carc;
|
1997-03-23 18:53:01 +00:00
|
|
|
d_swap = u_swap;
|
1997-03-23 18:51:21 +00:00
|
|
|
d_message = u_message;
|
|
|
|
d_header = u_header;
|
|
|
|
d_process = u_process;
|
|
|
|
}
|
|
|
|
}
|
2018-06-12 07:31:59 +00:00
|
|
|
|
2018-06-02 03:25:15 +00:00
|
|
|
no_command = true;
|
1997-03-23 18:51:21 +00:00
|
|
|
if (!interactive)
|
|
|
|
{
|
2018-06-10 00:02:56 +00:00
|
|
|
usleep(delay * 1e6);
|
2012-03-07 18:05:45 +00:00
|
|
|
if (leaveflag) {
|
|
|
|
end_screen();
|
|
|
|
exit(0);
|
|
|
|
}
|
1997-03-23 18:51:21 +00:00
|
|
|
}
|
|
|
|
else while (no_command)
|
|
|
|
{
|
|
|
|
/* assume valid command unless told otherwise */
|
2018-06-02 03:25:15 +00:00
|
|
|
no_command = false;
|
1997-03-23 18:51:21 +00:00
|
|
|
|
|
|
|
/* set up arguments for select with timeout */
|
|
|
|
FD_ZERO(&readfds);
|
2002-01-24 17:55:40 +00:00
|
|
|
FD_SET(0, &readfds); /* for standard input */
|
1997-03-23 18:51:21 +00:00
|
|
|
timeout.tv_sec = delay;
|
|
|
|
timeout.tv_usec = 0;
|
|
|
|
|
2001-08-06 03:19:22 +00:00
|
|
|
if (leaveflag) {
|
|
|
|
end_screen();
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tstopflag) {
|
|
|
|
/* move to the lower left */
|
|
|
|
end_screen();
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
/* default the signal handler action */
|
2018-05-21 04:40:20 +00:00
|
|
|
signal(SIGTSTP, SIG_DFL);
|
2001-08-06 03:19:22 +00:00
|
|
|
|
|
|
|
/* unblock the signal and send ourselves one */
|
2018-05-21 04:40:20 +00:00
|
|
|
sigsetmask(sigblock(0) & ~(1 << (SIGTSTP - 1)));
|
|
|
|
kill(0, SIGTSTP);
|
2001-08-06 03:19:22 +00:00
|
|
|
|
|
|
|
/* reset the signal handler */
|
2018-05-21 04:40:20 +00:00
|
|
|
signal(SIGTSTP, tstop);
|
2001-08-06 03:19:22 +00:00
|
|
|
|
|
|
|
/* reinit screen */
|
|
|
|
reinit_screen();
|
|
|
|
reset_display();
|
|
|
|
tstopflag = 0;
|
|
|
|
goto restart;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (winchflag) {
|
|
|
|
/* reascertain the screen dimensions */
|
|
|
|
get_screensize();
|
|
|
|
|
|
|
|
/* tell display to resize */
|
|
|
|
max_topn = display_resize();
|
|
|
|
|
|
|
|
/* reset the signal handler */
|
2018-05-21 04:40:20 +00:00
|
|
|
signal(SIGWINCH, top_winch);
|
2001-08-06 03:19:22 +00:00
|
|
|
|
|
|
|
reset_display();
|
|
|
|
winchflag = 0;
|
|
|
|
goto restart;
|
|
|
|
}
|
|
|
|
|
1997-03-23 18:51:21 +00:00
|
|
|
/* wait for either input or the end of the delay period */
|
2001-11-04 21:15:52 +00:00
|
|
|
sel_ret = select(2, &readfds, NULL, NULL, &timeout);
|
|
|
|
if (sel_ret < 0 && errno != EINTR)
|
|
|
|
quit(0);
|
|
|
|
if (sel_ret > 0)
|
1997-03-23 18:51:21 +00:00
|
|
|
{
|
|
|
|
int newval;
|
2018-06-09 02:14:33 +00:00
|
|
|
const char *errmsg;
|
2018-06-12 06:53:31 +00:00
|
|
|
const struct command *cptr;
|
2018-06-12 07:31:59 +00:00
|
|
|
|
1997-03-23 18:51:21 +00:00
|
|
|
/* something to read -- clear the message area first */
|
|
|
|
clear_message();
|
|
|
|
|
|
|
|
/* now read it and convert to command strchr */
|
|
|
|
/* (use "change" as a temporary to hold strchr) */
|
2001-11-04 21:15:52 +00:00
|
|
|
if (read(0, &ch, 1) != 1)
|
2002-01-24 17:55:40 +00:00
|
|
|
{
|
|
|
|
/* read error: either 0 or -1 */
|
|
|
|
new_message(MT_standout, " Read error on stdin");
|
|
|
|
putchar('\r');
|
|
|
|
quit(1);
|
|
|
|
}
|
2018-06-12 06:53:31 +00:00
|
|
|
if (ch == '\r' || ch == '\n') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
cptr = all_commands;
|
|
|
|
while (cptr->c != '\0') {
|
|
|
|
if (cptr->c == ch) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
cptr++;
|
|
|
|
}
|
|
|
|
if (cptr->c == '\0') {
|
1997-03-23 18:53:01 +00:00
|
|
|
new_message(MT_standout, " Command not understood");
|
2018-06-12 06:53:31 +00:00
|
|
|
putchar('\r');
|
|
|
|
no_command = true;
|
1997-03-23 18:53:01 +00:00
|
|
|
}
|
2018-06-12 06:53:31 +00:00
|
|
|
if (overstrike && !cptr->available_to_dumb)
|
1997-03-23 18:51:21 +00:00
|
|
|
{
|
|
|
|
new_message(MT_standout,
|
|
|
|
" Command cannot be handled by this terminal");
|
|
|
|
putchar('\r');
|
2018-06-12 06:53:31 +00:00
|
|
|
no_command = true;
|
1997-03-23 18:51:21 +00:00
|
|
|
}
|
2018-06-12 06:53:31 +00:00
|
|
|
if (!no_command) {
|
|
|
|
switch(cptr->id)
|
1997-03-23 18:51:21 +00:00
|
|
|
{
|
|
|
|
case CMD_redraw: /* redraw screen */
|
|
|
|
reset_display();
|
|
|
|
break;
|
2018-06-12 07:31:59 +00:00
|
|
|
|
1997-03-23 18:51:21 +00:00
|
|
|
case CMD_update: /* merely update display */
|
|
|
|
break;
|
2018-06-12 07:31:59 +00:00
|
|
|
|
2018-06-12 06:53:31 +00:00
|
|
|
case CMD_quit:
|
1997-03-23 18:51:21 +00:00
|
|
|
quit(0);
|
|
|
|
break;
|
2018-06-12 07:31:59 +00:00
|
|
|
|
2018-06-12 06:53:31 +00:00
|
|
|
case CMD_help:
|
1997-03-23 18:51:21 +00:00
|
|
|
reset_display();
|
2018-04-18 13:17:14 +00:00
|
|
|
top_clear();
|
1997-03-23 18:51:21 +00:00
|
|
|
show_help();
|
2018-04-18 13:17:14 +00:00
|
|
|
top_standout("Hit any key to continue: ");
|
1997-03-23 18:51:21 +00:00
|
|
|
fflush(stdout);
|
2018-05-21 04:40:20 +00:00
|
|
|
read(0, &ch, 1);
|
1997-03-23 18:51:21 +00:00
|
|
|
break;
|
2018-06-12 07:31:59 +00:00
|
|
|
|
1997-03-23 18:51:21 +00:00
|
|
|
case CMD_errors: /* show errors */
|
|
|
|
if (error_count() == 0)
|
|
|
|
{
|
|
|
|
new_message(MT_standout,
|
|
|
|
" Currently no errors to report.");
|
|
|
|
putchar('\r');
|
2018-06-02 03:25:15 +00:00
|
|
|
no_command = true;
|
1997-03-23 18:51:21 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
reset_display();
|
2018-04-18 13:17:14 +00:00
|
|
|
top_clear();
|
1997-03-23 18:51:21 +00:00
|
|
|
show_errors();
|
2018-04-18 13:17:14 +00:00
|
|
|
top_standout("Hit any key to continue: ");
|
1997-03-23 18:51:21 +00:00
|
|
|
fflush(stdout);
|
2018-05-21 04:40:20 +00:00
|
|
|
read(0, &ch, 1);
|
1997-03-23 18:51:21 +00:00
|
|
|
}
|
|
|
|
break;
|
2018-06-12 07:31:59 +00:00
|
|
|
|
2018-06-12 06:53:31 +00:00
|
|
|
case CMD_number:
|
1997-03-23 18:51:21 +00:00
|
|
|
new_message(MT_standout,
|
|
|
|
"Number of processes to show: ");
|
2018-06-02 03:25:15 +00:00
|
|
|
newval = readline(tempbuf1, 8, true);
|
1997-03-23 18:51:21 +00:00
|
|
|
if (newval > -1)
|
|
|
|
{
|
|
|
|
if (newval > max_topn)
|
|
|
|
{
|
|
|
|
new_message(MT_standout | MT_delayed,
|
|
|
|
" This terminal can only display %d processes.",
|
|
|
|
max_topn);
|
|
|
|
putchar('\r');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newval == 0)
|
|
|
|
{
|
|
|
|
/* inhibit the header */
|
2018-06-02 03:25:15 +00:00
|
|
|
display_header(false);
|
1997-03-23 18:51:21 +00:00
|
|
|
}
|
|
|
|
else if (newval > topn && topn == 0)
|
|
|
|
{
|
|
|
|
/* redraw the header */
|
2018-06-02 03:25:15 +00:00
|
|
|
display_header(true);
|
1997-03-23 18:51:21 +00:00
|
|
|
d_header = i_header;
|
|
|
|
}
|
|
|
|
topn = newval;
|
|
|
|
}
|
|
|
|
break;
|
2018-06-12 07:31:59 +00:00
|
|
|
|
1997-03-23 18:51:21 +00:00
|
|
|
case CMD_delay: /* new seconds delay */
|
|
|
|
new_message(MT_standout, "Seconds to delay: ");
|
2018-06-02 03:25:15 +00:00
|
|
|
if ((i = readline(tempbuf1, 8, true)) > -1)
|
1997-03-23 18:51:21 +00:00
|
|
|
{
|
2018-06-12 07:14:12 +00:00
|
|
|
if ((delay = i) == 0)
|
2002-01-24 17:55:40 +00:00
|
|
|
{
|
|
|
|
delay = 1;
|
|
|
|
}
|
1997-03-23 18:51:21 +00:00
|
|
|
}
|
|
|
|
clear_message();
|
|
|
|
break;
|
2018-06-12 07:31:59 +00:00
|
|
|
|
1997-03-23 18:51:21 +00:00
|
|
|
case CMD_displays: /* change display count */
|
|
|
|
new_message(MT_standout,
|
|
|
|
"Displays to show (currently %s): ",
|
|
|
|
displays == -1 ? "infinite" :
|
|
|
|
itoa(displays));
|
2018-06-02 03:25:15 +00:00
|
|
|
if ((i = readline(tempbuf1, 10, true)) > 0)
|
1997-03-23 18:51:21 +00:00
|
|
|
{
|
|
|
|
displays = i;
|
|
|
|
}
|
|
|
|
else if (i == 0)
|
|
|
|
{
|
|
|
|
quit(0);
|
|
|
|
}
|
|
|
|
clear_message();
|
|
|
|
break;
|
2018-06-12 07:31:59 +00:00
|
|
|
|
1997-03-23 18:51:21 +00:00
|
|
|
case CMD_kill: /* kill program */
|
|
|
|
new_message(0, "kill ");
|
2018-06-02 03:25:15 +00:00
|
|
|
if (readline(tempbuf2, sizeof(tempbuf2), false) > 0)
|
1997-03-23 18:51:21 +00:00
|
|
|
{
|
|
|
|
if ((errmsg = kill_procs(tempbuf2)) != NULL)
|
|
|
|
{
|
2000-10-04 23:34:16 +00:00
|
|
|
new_message(MT_standout, "%s", errmsg);
|
1997-03-23 18:51:21 +00:00
|
|
|
putchar('\r');
|
2018-06-02 03:25:15 +00:00
|
|
|
no_command = true;
|
1997-03-23 18:51:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
clear_message();
|
|
|
|
}
|
|
|
|
break;
|
2018-06-12 07:31:59 +00:00
|
|
|
|
1997-03-23 18:51:21 +00:00
|
|
|
case CMD_renice: /* renice program */
|
|
|
|
new_message(0, "renice ");
|
2018-06-02 03:25:15 +00:00
|
|
|
if (readline(tempbuf2, sizeof(tempbuf2), false) > 0)
|
1997-03-23 18:51:21 +00:00
|
|
|
{
|
|
|
|
if ((errmsg = renice_procs(tempbuf2)) != NULL)
|
|
|
|
{
|
2000-11-03 22:00:10 +00:00
|
|
|
new_message(MT_standout, "%s", errmsg);
|
1997-03-23 18:51:21 +00:00
|
|
|
putchar('\r');
|
2018-06-02 03:25:15 +00:00
|
|
|
no_command = true;
|
1997-03-23 18:51:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
clear_message();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CMD_idletog:
|
|
|
|
ps.idle = !ps.idle;
|
|
|
|
new_message(MT_standout | MT_delayed,
|
|
|
|
" %sisplaying idle processes.",
|
|
|
|
ps.idle ? "D" : "Not d");
|
|
|
|
putchar('\r');
|
|
|
|
break;
|
|
|
|
|
1998-08-04 14:10:48 +00:00
|
|
|
case CMD_selftog:
|
2018-06-09 02:14:33 +00:00
|
|
|
ps.self = !ps.self;
|
1998-08-04 14:10:48 +00:00
|
|
|
new_message(MT_standout | MT_delayed,
|
|
|
|
" %sisplaying self.",
|
2018-06-09 02:14:33 +00:00
|
|
|
(ps.self) ? "D" : "Not d");
|
1998-08-04 14:10:48 +00:00
|
|
|
putchar('\r');
|
|
|
|
break;
|
|
|
|
|
1997-03-23 18:51:21 +00:00
|
|
|
case CMD_user:
|
2017-08-07 08:45:08 +00:00
|
|
|
if (handle_user(tempbuf2, sizeof(tempbuf2)))
|
2018-06-02 03:25:15 +00:00
|
|
|
no_command = true;
|
1997-03-23 18:51:21 +00:00
|
|
|
break;
|
2018-06-12 07:31:59 +00:00
|
|
|
|
2003-07-17 23:56:40 +00:00
|
|
|
case CMD_thrtog:
|
|
|
|
ps.thread = !ps.thread;
|
|
|
|
new_message(MT_standout | MT_delayed,
|
2011-07-11 16:51:29 +00:00
|
|
|
" Displaying threads %s",
|
2005-04-14 15:02:03 +00:00
|
|
|
ps.thread ? "separately" : "as a count");
|
|
|
|
header_text = format_header(uname_field);
|
2005-05-18 13:30:08 +00:00
|
|
|
reset_display();
|
|
|
|
putchar('\r');
|
|
|
|
break;
|
2018-06-09 02:14:33 +00:00
|
|
|
|
|
|
|
case CMD_toggletid:
|
|
|
|
ps.thread_id = !ps.thread_id;
|
|
|
|
new_message(MT_standout | MT_delayed,
|
|
|
|
" Displaying %s",
|
|
|
|
ps.thread_id ? "tid" : "pid");
|
|
|
|
header_text = format_header(uname_field);
|
|
|
|
reset_display();
|
|
|
|
putchar('\r');
|
|
|
|
break;
|
|
|
|
|
2005-05-18 13:30:08 +00:00
|
|
|
case CMD_wcputog:
|
|
|
|
ps.wcpu = !ps.wcpu;
|
|
|
|
new_message(MT_standout | MT_delayed,
|
2011-07-18 21:08:14 +00:00
|
|
|
" Displaying %s CPU",
|
|
|
|
ps.wcpu ? "weighted" : "raw");
|
2005-05-18 13:30:08 +00:00
|
|
|
header_text = format_header(uname_field);
|
2005-04-14 15:02:03 +00:00
|
|
|
reset_display();
|
2003-07-17 23:56:40 +00:00
|
|
|
putchar('\r');
|
|
|
|
break;
|
2004-07-01 09:12:38 +00:00
|
|
|
case CMD_viewtog:
|
2018-06-12 07:14:12 +00:00
|
|
|
displaymode = displaymode == DISP_IO ? DISP_CPU : DISP_IO;
|
2018-11-30 18:02:59 +00:00
|
|
|
new_message(MT_standout | MT_delayed,
|
|
|
|
" Displaying %s statistics.",
|
|
|
|
displaymode == DISP_IO ? "IO" : "CPU");
|
2004-07-01 09:12:38 +00:00
|
|
|
header_text = format_header(uname_field);
|
2018-06-02 03:25:15 +00:00
|
|
|
display_header(true);
|
2004-07-01 09:12:38 +00:00
|
|
|
d_header = i_header;
|
|
|
|
reset_display();
|
|
|
|
break;
|
2004-07-12 03:00:50 +00:00
|
|
|
case CMD_viewsys:
|
|
|
|
ps.system = !ps.system;
|
2018-11-30 18:02:59 +00:00
|
|
|
new_message(MT_standout | MT_delayed,
|
|
|
|
" %sisplaying system processes.",
|
|
|
|
ps.system ? "D" : "Not d");
|
2004-07-12 03:00:50 +00:00
|
|
|
break;
|
2007-04-14 10:16:52 +00:00
|
|
|
case CMD_showargs:
|
|
|
|
fmt_flags ^= FMT_SHOWARGS;
|
2018-11-30 18:02:59 +00:00
|
|
|
new_message(MT_standout | MT_delayed,
|
|
|
|
" %sisplaying process arguments.",
|
|
|
|
fmt_flags & FMT_SHOWARGS ? "D" : "Not d");
|
2007-04-14 10:16:52 +00:00
|
|
|
break;
|
1997-03-23 18:51:21 +00:00
|
|
|
case CMD_order:
|
|
|
|
new_message(MT_standout,
|
|
|
|
"Order to sort: ");
|
2018-06-02 03:25:15 +00:00
|
|
|
if (readline(tempbuf2, sizeof(tempbuf2), false) > 0)
|
1997-03-23 18:51:21 +00:00
|
|
|
{
|
|
|
|
if ((i = string_index(tempbuf2, statics.order_names)) == -1)
|
|
|
|
{
|
|
|
|
new_message(MT_standout,
|
|
|
|
" %s: unrecognized sorting order", tempbuf2);
|
2018-06-02 03:25:15 +00:00
|
|
|
no_command = true;
|
1997-03-23 18:51:21 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
order_index = i;
|
|
|
|
}
|
|
|
|
putchar('\r');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
clear_message();
|
|
|
|
}
|
|
|
|
break;
|
2007-04-17 03:12:39 +00:00
|
|
|
case CMD_jidtog:
|
|
|
|
ps.jail = !ps.jail;
|
|
|
|
new_message(MT_standout | MT_delayed,
|
2007-05-04 15:42:58 +00:00
|
|
|
" %sisplaying jail ID.",
|
2007-04-17 03:12:39 +00:00
|
|
|
ps.jail ? "D" : "Not d");
|
|
|
|
header_text = format_header(uname_field);
|
|
|
|
reset_display();
|
|
|
|
putchar('\r');
|
|
|
|
break;
|
2014-05-02 23:30:39 +00:00
|
|
|
|
|
|
|
case CMD_jail:
|
|
|
|
new_message(MT_standout,
|
2014-05-02 23:32:44 +00:00
|
|
|
"Jail to show (+ for all): ");
|
2018-06-02 03:25:15 +00:00
|
|
|
if (readline(tempbuf2, sizeof(tempbuf2), false) > 0)
|
2014-05-02 23:30:39 +00:00
|
|
|
{
|
|
|
|
if (tempbuf2[0] == '+' &&
|
|
|
|
tempbuf2[1] == '\0')
|
|
|
|
{
|
|
|
|
ps.jid = -1;
|
|
|
|
}
|
|
|
|
else if ((i = jail_getid(tempbuf2)) == -1)
|
|
|
|
{
|
|
|
|
new_message(MT_standout,
|
|
|
|
" %s: unknown jail", tempbuf2);
|
2018-06-02 03:25:15 +00:00
|
|
|
no_command = true;
|
2014-05-02 23:30:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ps.jid = i;
|
|
|
|
}
|
|
|
|
if (ps.jail == 0) {
|
|
|
|
ps.jail = 1;
|
|
|
|
new_message(MT_standout |
|
|
|
|
MT_delayed, " Displaying jail "
|
|
|
|
"ID.");
|
|
|
|
header_text =
|
|
|
|
format_header(uname_field);
|
|
|
|
reset_display();
|
|
|
|
}
|
|
|
|
putchar('\r');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
clear_message();
|
|
|
|
}
|
|
|
|
break;
|
2018-06-12 07:31:59 +00:00
|
|
|
|
2011-05-31 15:11:23 +00:00
|
|
|
case CMD_kidletog:
|
|
|
|
ps.kidle = !ps.kidle;
|
|
|
|
new_message(MT_standout | MT_delayed,
|
|
|
|
" %sisplaying system idle process.",
|
|
|
|
ps.kidle ? "D" : "Not d");
|
|
|
|
putchar('\r');
|
|
|
|
break;
|
2011-07-11 16:48:52 +00:00
|
|
|
case CMD_pcputog:
|
|
|
|
pcpu_stats = !pcpu_stats;
|
|
|
|
new_message(MT_standout | MT_delayed,
|
|
|
|
" Displaying %sCPU statistics.",
|
|
|
|
pcpu_stats ? "per-" : "global ");
|
2011-07-18 21:15:47 +00:00
|
|
|
toggle_pcpustats();
|
2011-07-11 16:48:52 +00:00
|
|
|
max_topn = display_updatecpus(&statics);
|
|
|
|
reset_display();
|
|
|
|
putchar('\r');
|
|
|
|
break;
|
2016-09-05 08:27:04 +00:00
|
|
|
case CMD_swaptog:
|
|
|
|
ps.swap = !ps.swap;
|
|
|
|
new_message(MT_standout | MT_delayed,
|
|
|
|
" %sisplaying per-process swap usage.",
|
|
|
|
ps.swap ? "D" : "Not d");
|
|
|
|
header_text = format_header(uname_field);
|
|
|
|
reset_display();
|
|
|
|
putchar('\r');
|
|
|
|
break;
|
2018-06-02 15:52:18 +00:00
|
|
|
case CMD_pid:
|
|
|
|
new_message(MT_standout,
|
|
|
|
"Process id to show (+ for all): ");
|
|
|
|
if (readline(tempbuf2, sizeof(tempbuf2), false) > 0) {
|
|
|
|
if (tempbuf2[0] == '+' &&
|
|
|
|
tempbuf2[1] == '\0') {
|
|
|
|
ps.pid = (pid_t)-1;
|
|
|
|
} else {
|
|
|
|
unsigned long long num;
|
|
|
|
const char *errstr;
|
|
|
|
|
|
|
|
num = strtonum(tempbuf2, 0, INT_MAX,
|
|
|
|
&errstr);
|
|
|
|
if (errstr != NULL || !find_pid(num)) {
|
|
|
|
new_message(MT_standout,
|
|
|
|
" %s: unknown pid",
|
|
|
|
tempbuf2);
|
|
|
|
no_command = true;
|
|
|
|
} else {
|
|
|
|
ps.pid = (pid_t)num;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
putchar('\r');
|
|
|
|
} else
|
|
|
|
clear_message();
|
|
|
|
break;
|
2018-06-12 06:53:31 +00:00
|
|
|
case CMD_NONE:
|
2018-06-19 05:01:07 +00:00
|
|
|
assert(false && "reached switch without command");
|
2018-06-12 06:53:31 +00:00
|
|
|
}
|
1997-03-23 18:51:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* flush out stuff that may have been written */
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-01-24 17:55:40 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
fclose(debug);
|
|
|
|
#endif
|
1997-03-23 18:51:21 +00:00
|
|
|
quit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* reset_display() - reset all the display routine pointers so that entire
|
|
|
|
* screen will get redrawn.
|
|
|
|
*/
|
|
|
|
|
2018-05-21 03:36:16 +00:00
|
|
|
static void
|
2018-06-02 00:02:15 +00:00
|
|
|
reset_display(void)
|
1997-03-23 18:51:21 +00:00
|
|
|
{
|
|
|
|
d_loadave = i_loadave;
|
|
|
|
d_procstates = i_procstates;
|
|
|
|
d_cpustates = i_cpustates;
|
|
|
|
d_memory = i_memory;
|
2012-06-27 18:08:48 +00:00
|
|
|
d_arc = i_arc;
|
Add ZFS compressed ARC stats to top(1)
Provides:
amount of compressed data
logical size of compressed data (how much it would have taken uncompressed)
compression ratio (logical size : total ARC size)
Overhead (space consumed for compression headers)
Example output:
ARC: 31G Total, 18G MFU, 9067M MRU, 2236K Anon, 615M Header, 2947M Other
25G Compressed, 54G Uncompressed, 1.76:1 Ratio, 2265M Overhead
Reviewed by: jpaetzel, smh, imp, jhb (previous version)
MFC after: 2 week
Relnotes: yes
Sponsored by: ScaleEngine Inc.
Differential Revision: https://reviews.freebsd.org/D9829
2017-03-17 00:46:50 +00:00
|
|
|
d_carc = i_carc;
|
1997-03-23 18:53:01 +00:00
|
|
|
d_swap = i_swap;
|
1997-03-23 18:51:21 +00:00
|
|
|
d_message = i_message;
|
|
|
|
d_header = i_header;
|
|
|
|
d_process = i_process;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* signal handlers
|
|
|
|
*/
|
|
|
|
|
2018-05-21 00:53:42 +00:00
|
|
|
static sigret_t
|
|
|
|
leave(int i __unused) /* exit under normal conditions -- INT handler */
|
1997-03-23 18:51:21 +00:00
|
|
|
{
|
top(1): Quiesce several warnings
This is all warnings at level six (6) that are not
char-subscripts, incompatible-pointer-types,
sign-compare, switch, int-conversion,
missing-variable-declarations, cast-qual, cast-align
Some warnings that are fixed by this commit are:
shadow, strict-prototypes, missing-prototypes, pointer-arith,
unused-parameter, unused-const-variable, and several others
2018-05-20 02:14:27 +00:00
|
|
|
|
2001-08-06 03:19:22 +00:00
|
|
|
leaveflag = 1;
|
1997-03-23 18:51:21 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 00:53:42 +00:00
|
|
|
static sigret_t
|
|
|
|
tstop(int i __unused) /* SIGTSTP handler */
|
1997-03-23 18:51:21 +00:00
|
|
|
{
|
top(1): Quiesce several warnings
This is all warnings at level six (6) that are not
char-subscripts, incompatible-pointer-types,
sign-compare, switch, int-conversion,
missing-variable-declarations, cast-qual, cast-align
Some warnings that are fixed by this commit are:
shadow, strict-prototypes, missing-prototypes, pointer-arith,
unused-parameter, unused-const-variable, and several others
2018-05-20 02:14:27 +00:00
|
|
|
|
2001-08-06 03:19:22 +00:00
|
|
|
tstopflag = 1;
|
1997-03-23 18:51:21 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 00:53:42 +00:00
|
|
|
static sigret_t
|
|
|
|
top_winch(int i __unused) /* SIGWINCH handler */
|
1997-03-23 18:51:21 +00:00
|
|
|
{
|
top(1): Quiesce several warnings
This is all warnings at level six (6) that are not
char-subscripts, incompatible-pointer-types,
sign-compare, switch, int-conversion,
missing-variable-declarations, cast-qual, cast-align
Some warnings that are fixed by this commit are:
shadow, strict-prototypes, missing-prototypes, pointer-arith,
unused-parameter, unused-const-variable, and several others
2018-05-20 02:14:27 +00:00
|
|
|
|
2001-08-06 03:19:22 +00:00
|
|
|
winchflag = 1;
|
1997-03-23 18:51:21 +00:00
|
|
|
}
|
|
|
|
|
2018-06-13 00:45:38 +00:00
|
|
|
void __dead2
|
2018-05-21 00:53:42 +00:00
|
|
|
quit(int status) /* exit under duress */
|
1997-03-23 18:51:21 +00:00
|
|
|
{
|
|
|
|
end_screen();
|
|
|
|
exit(status);
|
|
|
|
}
|