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:
Eitan Adler 2018-06-09 02:14:33 +00:00
parent 0001edb823
commit fc36f5a7cb
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 - change number of seconds to delay between updates\n\
S - toggle the displaying of system processes\n\ S - toggle the displaying of system processes\n\
a - toggle the displaying of process titles\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\ t - toggle the display of this process\n\
u - display processes for only one user (+ selects all users)\n\ u - display processes for only one user (+ selects all users)\n\
w - toggle the display of swap use for each process\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). * separate lines).
*/ */
prehead = smpmode ? prehead = smpmode ?
(ps.thread ? smp_header_tid_only : smp_header_thr_and_pid) : (ps.thread_id ? smp_header_tid_only : smp_header_thr_and_pid) :
(ps.thread ? up_header_tid_only : up_header_thr_and_pid); (ps.thread_id ? up_header_tid_only : up_header_thr_and_pid);
snprintf(Header, sizeof(Header), prehead, snprintf(Header, sizeof(Header), prehead,
jidlength, ps.jail ? " JID" : "", jidlength, ps.jail ? " JID" : "",
namelength, namelength, uname_field, namelength, namelength, uname_field,
@ -828,7 +828,7 @@ get_process_info(struct system_info *si, struct process_select *sel,
/* not in use */ /* not in use */
continue; continue;
if (sel->self != -1 && pp->ki_pid == sel->self) if (!sel->self && pp->ki_pid == mypid)
/* skip self */ /* skip self */
continue; continue;

View File

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

View File

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

View File

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

View File

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