top(1): use a different command to toggle tid vs pid

- By popular demand, implement a different switch ("T") for toggling
between thread id and process id.
- Add an assert that the size of command chars is as expected.
- Also clean up some messiness I found when implementing this.
- Further document the new flag.

Requested by:	flo, ronald-lists@klop.ws, bapt
PR:		139389 (for the record)
X-MFC-With:	r334474
This commit is contained in:
eadler 2018-06-09 02:14:33 +00:00
parent 617832d02f
commit ed8df841b1
6 changed files with 59 additions and 30 deletions

View File

@ -99,6 +99,7 @@ r - renice a process\n\
s - change number of seconds to delay between updates\n\
S - toggle the displaying of system processes\n\
a - toggle the displaying of process titles\n\
T - toggle the displaying of thread IDs\n\
t - toggle the display of this process\n\
u - display processes for only one user (+ selects all users)\n\
w - toggle the display of swap use for each process\n\

View File

@ -433,8 +433,8 @@ format_header(const char *uname_field)
* separate lines).
*/
prehead = smpmode ?
(ps.thread ? smp_header_tid_only : smp_header_thr_and_pid) :
(ps.thread ? up_header_tid_only : up_header_thr_and_pid);
(ps.thread_id ? smp_header_tid_only : smp_header_thr_and_pid) :
(ps.thread_id ? up_header_tid_only : up_header_thr_and_pid);
snprintf(Header, sizeof(Header), prehead,
jidlength, ps.jail ? " JID" : "",
namelength, namelength, uname_field,
@ -828,7 +828,7 @@ get_process_info(struct system_info *si, struct process_select *sel,
/* not in use */
continue;
if (sel->self != -1 && pp->ki_pid == sel->self)
if (!sel->self && pp->ki_pid == mypid)
/* skip self */
continue;

View File

@ -58,25 +58,26 @@ struct system_info
*/
/*
* the process_select struct tells get_process_info what processes we
* are interested in seeing
* the process_select struct tells get_process_info what processes
* and information we are interested in seeing
*/
struct process_select
{
int idle; /* show idle processes */
int self; /* show self */
int system; /* show system processes */
int thread; /* show threads */
bool idle; /* show idle processes */
bool self; /* show self */
bool system; /* show system processes */
bool thread; /* show threads */
bool thread_id; /* show thread ids */
#define TOP_MAX_UIDS 8
int uid[TOP_MAX_UIDS]; /* only these uids (unless uid[0] == -1) */
int wcpu; /* show weighted cpu */
bool wcpu; /* show weighted cpu */
int jid; /* only this jid (unless jid == -1) */
int jail; /* show jail ID */
int swap; /* show swap usage */
int kidle; /* show per-CPU idle threads */
pid_t pid; /* only this pid (unless pid == -1) */
char *command; /* only this command (unless == NULL) */
bool jail; /* show jail ID */
bool swap; /* show swap usage */
bool kidle; /* show per-CPU idle threads */
int pid; /* only this pid (unless pid == -1) */
const char *command; /* only this command (unless == NULL) */
};
/* routines defined by the machine dependent module */

View File

@ -116,6 +116,9 @@ Display the
.IR jail (8)
ID.
.TP
.B \-T
Toggle displaying thread ID (tid) instead of process id (pid).
.TP
.B \-t
Do not display the
.I top
@ -238,6 +241,7 @@ The options
.BR \-j ,
.BR \-P ,
.BR \-S ,
.BR \-T ,
.BR \-t ,
.BR \-u ,
.BR \-w ,
@ -342,7 +346,6 @@ command.
.TP
.B H
Toggle the display of threads.
Also toggles the display of PID or TID.
.TP
.B i
(or
@ -363,6 +366,9 @@ This will also enable the display of JID.
.B P
Toggle the display of per-CPU statistics.
.TP
.B T
Toggle display of TID vs PID
.TP
.B t
Toggle the display of the
.I top

View File

@ -70,6 +70,7 @@ static int max_topn; /* maximum displayable processes */
/* miscellaneous things */
struct process_select ps;
const char * myname = "top";
pid_t mypid;
/* pointers to display routines */
static void (*d_loadave)(int mpid, double *avenrun) = i_loadave;
@ -230,7 +231,7 @@ main(int argc, char *argv[])
fd_set readfds;
char old_system = false;
static const char command_chars[] = "\f qh?en#sdkriIutHmSCajzPJwop";
static const char command_chars[] = "\f qh?en#sdkriIutHmSCajzPJwopT";
/* these defines enumerate the "strchr"s of the commands in command_chars */
#define CMD_redraw 0
#define CMD_update 1
@ -261,6 +262,9 @@ main(int argc, char *argv[])
#define CMD_swaptog 25
#define CMD_order 26
#define CMD_pid 27
#define CMD_toggletid 28
_Static_assert(sizeof(command_chars) == CMD_toggletid + 2, "command chars size");
/* set the buffer for stdout */
#ifdef DEBUG
@ -271,7 +275,6 @@ main(int argc, char *argv[])
setbuffer(stdout, stdoutbuf, Buffersize);
#endif
/* get our name */
if (argc > 0)
{
if ((myname = strrchr(argv[0], '/')) == 0)
@ -284,9 +287,12 @@ main(int argc, char *argv[])
}
}
mypid = getpid();
/* get our name */
/* initialize some selection options */
ps.idle = true;
ps.self = -1;
ps.self = false;
ps.system = false;
reset_uids();
ps.thread = false;
@ -297,6 +303,7 @@ main(int argc, char *argv[])
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)
@ -437,7 +444,7 @@ main(int argc, char *argv[])
break;
case 't':
ps.self = (ps.self == -1) ? getpid() : -1;
ps.self = !ps.self;
break;
case 'C':
@ -448,6 +455,10 @@ main(int argc, char *argv[])
ps.thread = !ps.thread;
break;
case 'T':
ps.thread_id = !ps.thread_id;
break;
case 'j':
ps.jail = !ps.jail;
break;
@ -712,7 +723,6 @@ restart:
new_message(MT_standout, " Write error on stdout");
putchar('\r');
quit(1);
/*NOTREACHED*/
}
/* only do the rest if we have more displays to show */
@ -809,7 +819,7 @@ restart:
if (sel_ret > 0)
{
int newval;
char *errmsg;
const char *errmsg;
/* something to read -- clear the message area first */
clear_message();
@ -822,7 +832,6 @@ restart:
new_message(MT_standout, " Read error on stdin");
putchar('\r');
quit(1);
/*NOTREACHED*/
}
if ((iptr = strchr(command_chars, ch)) == NULL)
{
@ -863,7 +872,6 @@ restart:
case CMD_quit: /* quit */
quit(0);
/*NOTREACHED*/
break;
case CMD_help1: /* help */
@ -997,10 +1005,10 @@ restart:
break;
case CMD_selftog:
ps.self = (ps.self == -1) ? getpid() : -1;
ps.self = !ps.self;
new_message(MT_standout | MT_delayed,
" %sisplaying self.",
(ps.self == -1) ? "D" : "Not d");
(ps.self) ? "D" : "Not d");
putchar('\r');
break;
@ -1018,6 +1026,17 @@ restart:
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,
@ -1184,7 +1203,6 @@ restart:
fclose(debug);
#endif
quit(0);
/*NOTREACHED*/
}
/*

View File

@ -10,6 +10,8 @@
#ifndef TOP_H
#define TOP_H
#include <unistd.h>
#define Default_DELAY 2
/* Number of lines of header information on the standard screen */
@ -42,7 +44,9 @@ enum displaymodes { DISP_CPU = 0, DISP_IO, DISP_MAX };
extern enum displaymodes displaymode;
extern int pcpu_stats;
extern int overstrike;
extern int overstrike;
extern pid_t mypid;
extern const char * myname;
@ -52,9 +56,8 @@ char* kill_procs(char *);
char* renice_procs(char *);
extern char copyright[];
/* internal routines */
void quit(int);
void quit(int);
/*
* The space command forces an immediate update. Sometimes, on loaded