Index: stable/12/usr.bin/top/top.1 =================================================================== --- stable/12/usr.bin/top/top.1 (revision 352888) +++ stable/12/usr.bin/top/top.1 (revision 352889) @@ -1,445 +1,445 @@ .\" $FreeBSD$ .Dd October 2, 2018 .Dt TOP 1 .Os .Sh NAME .Nm top .Nd display and update information about the top cpu processes .Sh SYNOPSIS .Nm .Op Fl CHIPSTabijnpqtuvxz .Op Fl J Ar jail .Op Fl U Ar uid .Op Fl d Ar count .Op Fl m Ar cpu|io .Op Fl s Ar time .Op Fl o Ar field .Op Fl p Ar pid -.Op Ar count +.Op Ar number .Sh DESCRIPTION .Nm displays the top processes on the system and periodically updates this information. If standard output is an intelligent terminal (see below) then as many processes as will fit on the terminal screen are displayed by default. Otherwise, a good number of them are shown (around 20). Raw cpu percentage is used to rank the processes. If .Ar number is given, then the top .Ar number processes will be displayed instead of the default. .Pp .Nm makes a distinction between terminals that support advanced capabilities and those that do not. This distinction affects the choice of defaults for certain options. In the remainder of this document, an \*(lqintelligent\*(rq terminal is one that supports cursor addressing, clear screen, and clear to end of line. Conversely, a \*(lqdumb\*(rq terminal is one that does not support such features. If the output of .Nm is redirected to a file, it acts as if it were being run on a dumb terminal. .Bl -tag -width indent -compact .It Fl C Toggle CPU display mode. By default top displays the weighted CPU percentage in the WCPU column (this is the same value that .Xr ps 1 displays as CPU). Each time .Fl C flag is passed it toggles between \*(lqraw cpu\*(rq mode and \*(lqweighted cpu\*(rq mode, showing the \*(lqCPU\*(rq or the \*(lqWCPU\*(rq column respectively. .It Fl S Show system processes in the display. Normally, system processes such as the pager and the swapper are not shown. This option makes them visible. .It Fl a Display command names derived from the argv[] vector, rather than real executable name. It it useful when you want to watch applications, that puts their status information there. If the real name differs from argv[0], it will be displayed in parenthesis. .It Fl b Use \*(lqbatch\*(rq mode. In this mode, all input from the terminal is ignored. Interrupt characters (such as ^C and ^\e) still have an effect. This is the default on a dumb terminal, or when the output is not a terminal. .It Fl H Display each thread for a multithreaded process individually. By default a single summary line is displayed for each process. .It Fl i Use \*(lqinteractive\*(rq mode. In this mode, any input is immediately read for processing. See the section on \*(lqInteractive Mode\*(rq for an explanation of which keys perform what functions. After the command is processed, the screen will immediately be updated, even if the command was not understood. This mode is the default when standard output is an intelligent terminal. .It Fl I Do not display idle processes. By default, top displays both active and idle processes. .It Fl j Display the .Xr jail 8 ID. .It Fl T Toggle displaying thread ID (tid) instead of process id (pid). .It Fl t Do not display the .Nm process itself. .It Fl display Display either 'cpu' or 'io' statistics. Default is 'cpu'. .It Fl n Use \*(lqnon-interactive\*(rq mode. This is identical to \*(lqbatch\*(rq mode. .It Fl P Display per-cpu CPU usage statistics. .It Fl q Renice .Nm to -20 so that it will run faster. This can be used when the system is being very sluggish to improve the possibility of discovering the problem. This option can only be used by root. .It Fl u Do not map uid numbers to usernames. Normally, .Nm will read as much of the file \*(lq/etc/passwd\*(rq as is necessary to map all the user id numbers it encounters into login names. This option disables all that, while possibly decreasing execution time. The uid numbers are displayed instead of the names. .It Fl v Write version number information to stderr then exit immediately. .It Fl w Display approximate swap usage for each process. .It Fl z Do not display the system idle process. .It Fl d Ar count Show only .Ar count displays, then exit. A display is considered to be one update of the screen. The default is 1 for dumb terminals. Note that for .Ar count = 1 no information is available about the percentage of time spent by the CPU in every state. .It Fl s Ar time Set the delay between screen updates to .Ar time -seconds. +seconds, which may be fractional. The default delay between updates is 1 second. .It Fl o Ar field Sort the process display area on the specified field. The field name is the name of the column as seen in the output, but in lower case: \*(lqcpu\*(lq, \*(rqsize\*(lq, \*(rqres\*(lq, \*(rqtime\*(lq, \*(rqpri\*(lq, \*(rqthreads\*(lq, \*(lqtotal\*(lq, \*(rqread\*(lq, \*(rqwrite\*(lq, \*(rqfault\*(lq, \*(rqvcsw\*(lq, \*(rqivcsw\*(lq, \*(lqjid\*(lq, \*(rqswap\*(lq or \*(rqpid\*(lq. .It Fl p Ar pid Show only the process .Ar pid . .It Fl J Ar jail Show only those processes owned by .Ar jail . This may be either the .Ar jid or .Ar name of the jail. Use 0 to limit to host processes. Using this option implies .Fl j . .Pp .It Fl U Ar username Show only those processes owned by .Ar username . This option currently only accepts usernames and will not understand uid numbers. .El .Pp Both .Ar count and .Ar number fields can be specified as \*(lqinfinite\*(rq, indicating that they can stretch as far as possible. This is accomplished by using any proper prefix of the keywords \*(lqinfinity\*(rq, \*(lqmaximum\*(rq, or \*(lqall\*(rq. Boolean flags are toggles. A second specification of any of these options will negate the first. .Sh "INTERACTIVE MODE" When .Nm is running in \*(lqinteractive mode\*(rq, it reads commands from the terminal and acts upon them accordingly. In this mode, the terminal is put in \*(lqCBREAK\*(rq, so that a character will be processed as soon as it is typed. Almost always, a key will be pressed when .Nm is between displays; that is, while it is waiting for .Ar time seconds to elapse. If this is the case, the command will be processed and the display will be updated immediately thereafter (reflecting any changes that the command may have specified). This happens even if the command was incorrect. If a key is pressed while .Nm is in the middle of updating the display, it will finish the update and then process the command. Some commands require additional information, and the user will be prompted accordingly. While typing this information in, the user's erase and kill keys (as set up by the command .Xr stty 1 ) are recognized, and a newline terminates the input. .Pp These commands are currently recognized (^L refers to control-L): .Bl -tag -width indent .It ^L Redraw the screen. .It h Display a summary of the commands (help screen). Version information is included in this display. .It q Quit .Nm .It d Change the number of displays to show (prompt for new number). Remember that the next display counts as one, so typing .It d1 will make .Nm show one final display and then immediately exit. .It m Toggle the display between 'cpu' and 'io' modes. .It n or # Change the number of processes to display (prompt for new number). .It s Change the number of seconds to delay between displays (prompt for new number). .It S Toggle the display of system processes. .It a Toggle the display of process titles. .It k Send a signal (\*(lqkill\*(rq by default) to a list of processes. This acts similarly to the command .Xr kill 1 . .It r Change the priority (the \*(lqnice\*(rq) of a list of processes. This acts similarly to .Xr renice 8 . .It u Display only processes owned by a specific set of usernames (prompt for username). If the username specified is simply \*(lq+\*(rq or \*(lq-\*(rq, then processes belonging to all users will be displayed. Usernames can be added to and removed from the set by prepending them with \*(lq+\*(rq and \*(lq-\*(rq, respectively. .It o Change the order in which the display is sorted. The sort key names include \*(lqcpu\*(rq, \*(lqres\*(rq, \*(lqsize\*(rq, \*(lqtime\*(rq. The default is cpu. .It p Display a specific process (prompt for pid). If the pid specified is simply \*(lq+\*(rq, then show all processes. .It e Display a list of system errors (if any) generated by the last command. .It B H Toggle the display of threads. .It i or I Toggle the display of idle processes. .It j Toggle the display of .Xr jail 8 ID. .It J Display only processes owned by a specific jail (prompt for jail). If the jail specified is simply \*(lq+\*(rq, then processes belonging to all jails and the host will be displayed. This will also enable the display of JID. .It P Toggle the display of per-CPU statistics. .It T Toggle display of TID and PID .It t Toggle the display of the .Nm process. .It w Toggle the display of swap usage. .It z Toggle the display of the system idle process. .El .Sh "THE DISPLAY" The top few lines of the display show general information about the state of the system, including the last process id assigned to a process (on most systems), the three load averages, the current time, the number of existing processes, the number of processes in each state (sleeping, running, starting, zombies, and stopped), and a percentage of time spent in each of the processor states (user, nice, system, and idle). It also includes information about physical and virtual memory allocation. .Pp The remainder of the screen displays information about individual processes. This display is similar in spirit to .Xr ps 1 but it is not exactly the same. PID is the process id, JID, when displayed, is the .Xr jail 8 ID corresponding to the process, USERNAME is the name of the process's owner (if .Fl u is specified, a UID column will be substituted for USERNAME), PRI is the current priority of the process, NICE is the .Xr nice 1 amount, SIZE is the total size of the process (text, data, and stack), RES is the current amount of resident memory, SWAP is the approximate amount of swap, if enabled (SIZE, RES and SWAP are given in kilobytes), STATE is the current state (one of \*(lqSTART\*(rq, \*(lqRUN\*(rq (shown as \*(lqCPUn\*(rq on SMP systems), \*(lqSLEEP\*(rq, \*(lqSTOP\*(rq, \*(lqZOMB\*(rq, \*(lqWAIT\*(rq, \*(lqLOCK\*(rq or the event on which the process waits), C is the processor number on which the process is executing (visible only on SMP systems), TIME is the number of system and user cpu seconds that the process has used, WCPU, when displayed, is the weighted cpu percentage (this is the same value that .Xr ps 1 displays as CPU), CPU is the raw percentage and is the field that is sorted to determine the order of the processes, and COMMAND is the name of the command that the process is currently running (if the process is swapped out, this column is marked \*(lq\*(rq). .Pp If a process is in the \*(lqSLEEP\*(rq or \*(lqLOCK\*(rq state, the state column will report the name of the event or lock on which the process is waiting. Lock names are prefixed with an asterisk \*(lq*\*(rq while sleep events are not. .Sh DESCRIPTION OF MEMORY .Bd -literal Mem: 61M Active, 86M Inact, 368K Laundry, 22G Wired, 102G Free ARC: 15G Total, 9303M MFU, 6155M MRU, 1464K Anon, 98M Header, 35M Other 15G Compressed, 27G Uncompressed, 1.75:1 Ratio, 174M Overhead Swap: 4096M Total, 532M Free, 13% Inuse, 80K In, 104K Out .Ed .Ss Physical Memory Stats .Bl -tag -width "Uncompressed" -compact .It Em Active number of bytes active .It Em Inact number of clean bytes inactive .It Em Laundry number of dirty bytes queued for laundering .It Em Wired number of bytes wired down, including IO-level cached file data pages .It Em Buf number of bytes used for IO-level disk caching .It Em Free number of bytes free .El .Ss ZFS ARC Stats These stats are only displayed when the ARC is in use. .Pp .Bl -tag -width "Uncompressed" -compact .It Em Total number of wired bytes used for the ZFS ARC .It Em MRU number of ARC bytes holding most recently used data .It Em MFU number of ARC bytes holding most frequently used data .It Em Anon number of ARC bytes holding in flight data .It Em Header number of ARC bytes holding headers .It Em Other miscellaneous ARC bytes .It Em Compressed bytes of memory used by ARC caches .It Em Uncompressed bytes of data stored in ARC caches before compression .It Em Ratio compression ratio of data cached in the ARC .El .Ss Swap Stats .Bl -tag -width "Uncompressed" -compact .It Em Total total available swap usage .It Em Free total free swap usage .It Em Inuse swap usage .It Em \&In bytes paged in from swap devices (last interval) .It Em Out bytes paged out to swap devices (last interval) .El .Sh ENVIRONMENT .Bl -tag -width "Uncompressed" .It Ev TOP Default set of arguments to .Nm . .El .Sh SEE ALSO .Xr kill 1 , .Xr ps 1 , .Xr stty 1 , .Xr getrusage 2 , .Xr humanize_number 3 , .Xr mem 4 , .Xr renice 8 .Sh AUTHORS .An William LeFebvre, EECS Department, Northwestern University .Sh BUGS The command name for swapped processes should be tracked down, but this would make the program run slower. .Pp As with .Xr ps 1 , things can change while .Nm is collecting information for an update. The picture it gives is only a close approximation to reality. Index: stable/12/usr.bin/top/top.c =================================================================== --- stable/12/usr.bin/top/top.c (revision 352888) +++ stable/12/usr.bin/top/top.c (revision 352889) @@ -1,1198 +1,1212 @@ /*- * 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 * 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 * * $FreeBSD$ */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "commands.h" #include "display.h" /* interface to display package */ #include "screen.h" /* interface to screen package */ #include "top.h" #include "machine.h" #include "utils.h" #include "username.h" /* Size of the stdio buffer given to stdout */ #define Buffersize 2048 char copyright[] = "Copyright (c) 1984 through 1996, William LeFebvre"; typedef void sigret_t; /* The buffer that stdio will use */ static char stdoutbuf[Buffersize]; static int fmt_flags = 0; int pcpu_stats = false; /* signal handling routines */ static sigret_t leave(int); static sigret_t tstop(int); static sigret_t top_winch(int); static volatile sig_atomic_t leaveflag; static volatile sig_atomic_t tstopflag; static volatile sig_atomic_t winchflag; /* values which need to be accessed by signal handlers */ static int max_topn; /* maximum displayable processes */ /* miscellaneous things */ struct process_select ps; pid_t mypid; /* pointers to display routines */ 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; static void (*d_header)(const char *text) = i_header; static void (*d_process)(int line, char *thisline) = i_process; static void reset_display(void); 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' }, { "jail", required_argument, NULL, 'J' }, { "per-cpu", no_argument, NULL, 'P' }, { "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' }, { "swap", no_argument, NULL, 'w' }, { "system-idle-procs", no_argument, NULL, 'z' }, { NULL, 0, NULL, 0 } }; static void reset_uids(void) { 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): "); if (readline(buf, buflen, false) <= 0) { clear_message(); return (rc); } 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'); return (rc); } int main(int argc, const char *argv[]) { int i; int active_procs; struct system_info system_info; struct statics statics; void * processes; static char tempbuf1[50]; static char tempbuf2[50]; sigset_t old_sigmask, new_sigmask; int topn = Infinity; - double delay = 2; + struct timeval delay = { 2, 0 }; int displays = 0; /* indicates unspecified */ int sel_ret = 0; time_t curr_time; char *(*get_userid)(int) = username; const char *uname_field = "USERNAME"; const char *header_text; char *env_top; const char **preset_argv; int preset_argc = 0; const char **av = NULL; int ac = -1; bool do_unames = true; char interactive = 2; char warnings = 0; char topn_specified = false; char ch; char no_command = 1; struct timeval timeout; char *order_name = NULL; int order_index = 0; fd_set readfds; char *nptr; /* set the buffer for stdout */ #ifdef DEBUG extern FILE *debug; debug = fopen("debug.run", "w"); setbuffer(stdout, NULL, 0); #else setbuffer(stdout, stdoutbuf, Buffersize); #endif mypid = getpid(); /* get our name */ /* initialize some selection options */ ps.idle = true; ps.self = true; ps.system = false; reset_uids(); ps.thread = false; ps.wcpu = 1; ps.jid = -1; ps.jail = false; ps.swap = false; ps.kidle = true; ps.pid = -1; ps.command = NULL; ps.thread_id = false; /* 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; } while ((i = getopt_long(ac, __DECONST(char * const *, av), "CSIHPabijJ:nquvzs:d:U:m:o:p:Ttw", longopts, NULL)) != EOF) { switch(i) { case 'v': /* show version number */ errx(0, "version FreeBSD"); break; case 'u': /* toggle uid/username display */ do_unames = !do_unames; break; case 'U': /* display only username's processes */ if ((ps.uid[0] = userid(optarg)) == -1) { errx(1, "%s: unknown user\n", optarg); } break; case 'S': /* show system processes */ ps.system = true; break; case 'I': /* show idle processes */ ps.idle = !ps.idle; break; case 'i': /* go interactive regardless */ interactive = 1; break; case 'n': /* batch, or non-interactive */ case 'b': interactive = 0; break; case 'a': fmt_flags ^= FMT_SHOWARGS; break; case 'd': /* number of displays to show */ if ((i = atoiwi(optarg)) == Invalid || i == 0) { warnx("warning: display count should be positive -- option ignored"); warnings++; } else { displays = i; } break; 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; } - case 's': - delay = strtod(optarg, &nptr); - if (nptr == optarg) { - warnx("warning: invalid delay"); - delay = 2; - warnings++; - } - if (delay < 0) { - warnx("warning: seconds delay should be positive -- using default"); - delay = 2; - warnings++; - } + case 's': + { + double delay_d = strtod(optarg, &nptr); + if (nptr == optarg) + { + warnx("warning: invalid delay"); + warnings++; + } + else if (delay_d <= 0) + { + warnx("warning: seconds delay should be positive -- using default"); + warnings++; + } + else + { + delay.tv_sec = delay_d; + delay.tv_usec = (delay_d - delay.tv_sec) * 1e6; + } + break; + } - break; - case 'q': /* be quick about it */ errno = 0; i = setpriority(PRIO_PROCESS, 0, PRIO_MIN); if (i == -1 && errno != 0) { warnx("warning: `-q' option failed (%m)"); warnings++; } break; case 'm': /* select display mode */ if (strcmp(optarg, "io") == 0) { displaymode = DISP_IO; } else if (strcmp(optarg, "cpu") == 0) { displaymode = DISP_CPU; } else { errx(1, "warning: `-m' option can only take args 'io' or 'cpu'"); } break; case 'o': /* select sort order */ order_name = optarg; break; case 't': ps.self = !ps.self; break; case 'C': ps.wcpu = !ps.wcpu; break; case 'H': ps.thread = !ps.thread; break; case 'T': ps.thread_id = !ps.thread_id; break; case 'j': ps.jail = !ps.jail; break; 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; case 'P': pcpu_stats = !pcpu_stats; break; case 'w': ps.swap = 1; break; case 'z': ps.kidle = !ps.kidle; break; default: errx(1, "[-abCHIijnPqStuvwz] [-d count] [-m io | cpu] [-o field] [-p pid]\n" " [-s time] [-J jail] [-U username] [number]"); } } /* get count of top processes to display (if any) */ if (optind < ac) { if ((topn = atoiwi(av[optind])) == Invalid) { warnx("warning: process display count should be non-negative -- using default"); warnings++; } else { topn_specified = true; } } /* 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 */ if (machine_init(&statics) == -1) { exit(1); } /* determine sorting order index, if necessary */ if (order_name != NULL) { if ((order_index = string_index(order_name, statics.order_names)) == -1) { const char * const *pp; warnx("'%s' is not a recognized sorting order.", order_name); 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) { errx(4, "can't allocate sufficient memory"); } /* print warning if user requested more processes than we can display */ if (topn > max_topn) { warnx("warning: this terminal can only display %d processes.", max_topn); warnings++; } /* 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 */ if (interactive == 2) { 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 */ sigemptyset(&new_sigmask); sigaddset(&new_sigmask, SIGINT); sigaddset(&new_sigmask, SIGQUIT); sigaddset(&new_sigmask, SIGTSTP); sigprocmask(SIG_BLOCK, &new_sigmask, &old_sigmask); init_screen(); signal(SIGINT, leave); signal(SIGQUIT, leave); signal(SIGTSTP, tstop); signal(SIGWINCH, top_winch); sigprocmask(SIG_SETMASK, &old_sigmask, NULL); if (warnings) { fputs("....", stderr); fflush(stderr); sleep(3 * warnings); fputc('\n', stderr); } restart: /* * main loop -- repeat while display count is positive or while it * indicates infinity (by being -1) */ while ((displays == -1) || (displays-- > 0)) { int (*compare)(const void * const, const void * const); /* get the current stats */ get_system_info(&system_info); compare = compares[order_index]; /* get the current set of processes */ processes = get_process_info(&system_info, &ps, compare); /* 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); i_uptime(&system_info.boottime, &curr_time); i_timeofday(&curr_time); /* display process state breakdown */ (*d_procstates)(system_info.p_total, system_info.procstates); (*d_cpustates)(system_info.cpustates); /* display memory stats */ (*d_memory)(system_info.memory); (*d_arc)(system_info.arc); (*d_carc)(system_info.carc); /* display swap stats */ (*d_swap)(system_info.swap); /* handle message area */ (*d_message)(); /* update the header area */ (*d_header)(header_text); 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 */ active_procs = system_info.p_pactive; 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++) { (*d_process)(i, format_next_process(processes, get_userid, fmt_flags)); } } else { i = 0; } /* do end-screen processing */ u_endscreen(i); /* now, flush the output buffer */ if (fflush(stdout) != 0) { new_message(MT_standout, " Write error on stdout"); putchar('\r'); quit(1); } /* 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; d_arc = u_arc; d_carc = u_carc; d_swap = u_swap; d_message = u_message; d_header = u_header; d_process = u_process; } } no_command = true; if (!interactive) { - usleep(delay * 1e6); + timeout = delay; + select(0, NULL, NULL, NULL, &timeout); if (leaveflag) { end_screen(); exit(0); } } else while (no_command) { /* assume valid command unless told otherwise */ no_command = false; /* set up arguments for select with timeout */ FD_ZERO(&readfds); FD_SET(0, &readfds); /* for standard input */ - timeout.tv_sec = delay; - timeout.tv_usec = 0; + timeout = delay; if (leaveflag) { end_screen(); exit(0); } if (tstopflag) { /* move to the lower left */ end_screen(); fflush(stdout); /* default the signal handler action */ signal(SIGTSTP, SIG_DFL); /* unblock the signal and send ourselves one */ sigsetmask(sigblock(0) & ~(1 << (SIGTSTP - 1))); kill(0, SIGTSTP); /* reset the signal handler */ signal(SIGTSTP, tstop); /* 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 */ signal(SIGWINCH, top_winch); reset_display(); winchflag = 0; goto restart; } /* wait for either input or the end of the delay period */ sel_ret = select(2, &readfds, NULL, NULL, &timeout); if (sel_ret < 0 && errno != EINTR) quit(0); if (sel_ret > 0) { int newval; const char *errmsg; const struct command *cptr; /* 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) */ if (read(0, &ch, 1) != 1) { /* read error: either 0 or -1 */ new_message(MT_standout, " Read error on stdin"); putchar('\r'); quit(1); } if (ch == '\r' || ch == '\n') { continue; } cptr = all_commands; while (cptr->c != '\0') { if (cptr->c == ch) { break; } cptr++; } if (cptr->c == '\0') { new_message(MT_standout, " Command not understood"); putchar('\r'); no_command = true; } if (overstrike && !cptr->available_to_dumb) { new_message(MT_standout, " Command cannot be handled by this terminal"); putchar('\r'); no_command = true; } if (!no_command) { switch(cptr->id) { case CMD_redraw: /* redraw screen */ reset_display(); break; case CMD_update: /* merely update display */ break; case CMD_quit: quit(0); break; case CMD_help: reset_display(); top_clear(); show_help(); top_standout("Hit any key to continue: "); fflush(stdout); read(0, &ch, 1); break; case CMD_errors: /* show errors */ if (error_count() == 0) { new_message(MT_standout, " Currently no errors to report."); putchar('\r'); no_command = true; } else { reset_display(); top_clear(); show_errors(); top_standout("Hit any key to continue: "); fflush(stdout); read(0, &ch, 1); } break; case CMD_number: new_message(MT_standout, "Number of processes to show: "); newval = readline(tempbuf1, 8, true); 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 */ display_header(false); } else if (newval > topn && topn == 0) { /* redraw the header */ display_header(true); d_header = i_header; } topn = newval; } break; case CMD_delay: /* new seconds delay */ new_message(MT_standout, "Seconds to delay: "); - if ((i = readline(tempbuf1, 8, true)) > -1) + if ((i = readline(tempbuf1, 8, false)) > 0) { - if ((delay = i) == 0) + double delay_d = strtod(tempbuf1, &nptr); + if (nptr == tempbuf1 || delay_d <= 0) { - delay = 1; + new_message(MT_standout, " Invalid delay"); + putchar('\r'); + no_command = true; } + else + { + delay.tv_sec = delay_d; + delay.tv_usec = (delay_d - delay.tv_sec) * 1e6; + clear_message(); + } } - clear_message(); break; case CMD_displays: /* change display count */ new_message(MT_standout, "Displays to show (currently %s): ", displays == -1 ? "infinite" : itoa(displays)); if ((i = readline(tempbuf1, 10, true)) > 0) { displays = i; } else if (i == 0) { quit(0); } clear_message(); break; case CMD_kill: /* kill program */ new_message(0, "kill "); if (readline(tempbuf2, sizeof(tempbuf2), false) > 0) { if ((errmsg = kill_procs(tempbuf2)) != NULL) { new_message(MT_standout, "%s", errmsg); putchar('\r'); no_command = true; } } else { clear_message(); } break; case CMD_renice: /* renice program */ new_message(0, "renice "); if (readline(tempbuf2, sizeof(tempbuf2), false) > 0) { if ((errmsg = renice_procs(tempbuf2)) != NULL) { new_message(MT_standout, "%s", errmsg); putchar('\r'); no_command = true; } } 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; case CMD_selftog: ps.self = !ps.self; new_message(MT_standout | MT_delayed, " %sisplaying self.", (ps.self) ? "D" : "Not d"); putchar('\r'); break; case CMD_user: if (handle_user(tempbuf2, sizeof(tempbuf2))) no_command = true; break; case CMD_thrtog: ps.thread = !ps.thread; new_message(MT_standout | MT_delayed, " Displaying threads %s", ps.thread ? "separately" : "as a count"); header_text = format_header(uname_field); reset_display(); putchar('\r'); break; 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; case CMD_wcputog: ps.wcpu = !ps.wcpu; new_message(MT_standout | MT_delayed, " Displaying %s CPU", ps.wcpu ? "weighted" : "raw"); header_text = format_header(uname_field); reset_display(); putchar('\r'); break; case CMD_viewtog: displaymode = displaymode == DISP_IO ? DISP_CPU : DISP_IO; new_message(MT_standout | MT_delayed, " Displaying %s statistics.", displaymode == DISP_IO ? "IO" : "CPU"); header_text = format_header(uname_field); display_header(true); d_header = i_header; reset_display(); break; case CMD_viewsys: ps.system = !ps.system; new_message(MT_standout | MT_delayed, " %sisplaying system processes.", ps.system ? "D" : "Not d"); break; case CMD_showargs: fmt_flags ^= FMT_SHOWARGS; new_message(MT_standout | MT_delayed, " %sisplaying process arguments.", fmt_flags & FMT_SHOWARGS ? "D" : "Not d"); break; case CMD_order: new_message(MT_standout, "Order to sort: "); if (readline(tempbuf2, sizeof(tempbuf2), false) > 0) { if ((i = string_index(tempbuf2, statics.order_names)) == -1) { new_message(MT_standout, " %s: unrecognized sorting order", tempbuf2); no_command = true; } else { order_index = i; } putchar('\r'); } else { clear_message(); } break; case CMD_jidtog: ps.jail = !ps.jail; new_message(MT_standout | MT_delayed, " %sisplaying jail ID.", ps.jail ? "D" : "Not d"); header_text = format_header(uname_field); reset_display(); putchar('\r'); break; case CMD_jail: new_message(MT_standout, "Jail to show (+ for all): "); if (readline(tempbuf2, sizeof(tempbuf2), false) > 0) { if (tempbuf2[0] == '+' && tempbuf2[1] == '\0') { ps.jid = -1; } else if ((i = jail_getid(tempbuf2)) == -1) { new_message(MT_standout, " %s: unknown jail", tempbuf2); no_command = true; } 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; 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; case CMD_pcputog: pcpu_stats = !pcpu_stats; new_message(MT_standout | MT_delayed, " Displaying %sCPU statistics.", pcpu_stats ? "per-" : "global "); toggle_pcpustats(); max_topn = display_updatecpus(&statics); reset_display(); putchar('\r'); break; 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; 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; case CMD_NONE: assert(false && "reached switch without command"); } } } /* flush out stuff that may have been written */ fflush(stdout); } } } #ifdef DEBUG fclose(debug); #endif quit(0); } /* * reset_display() - reset all the display routine pointers so that entire * screen will get redrawn. */ static void reset_display(void) { d_loadave = i_loadave; d_procstates = i_procstates; d_cpustates = i_cpustates; d_memory = i_memory; d_arc = i_arc; d_carc = i_carc; d_swap = i_swap; d_message = i_message; d_header = i_header; d_process = i_process; } /* * signal handlers */ static sigret_t leave(int i __unused) /* exit under normal conditions -- INT handler */ { leaveflag = 1; } static sigret_t tstop(int i __unused) /* SIGTSTP handler */ { tstopflag = 1; } static sigret_t top_winch(int i __unused) /* SIGWINCH handler */ { winchflag = 1; } void __dead2 quit(int status) /* exit under duress */ { end_screen(); exit(status); } Index: stable/12 =================================================================== --- stable/12 (revision 352888) +++ stable/12 (revision 352889) Property changes on: stable/12 ___________________________________________________________________ Modified: svn:mergeinfo ## -0,0 +0,1 ## Merged /head:r352804,352818-352819