Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 9 Jun 2018 02:14:34 +0000 (UTC)
From:      Eitan Adler <eadler@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r334864 - head/usr.bin/top
Message-ID:  <201806090214.w592EYsJ056425@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: eadler
Date: Sat Jun  9 02:14:33 2018
New Revision: 334864
URL: https://svnweb.freebsd.org/changeset/base/334864

Log:
  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

Modified:
  head/usr.bin/top/commands.c
  head/usr.bin/top/machine.c
  head/usr.bin/top/machine.h
  head/usr.bin/top/top.1
  head/usr.bin/top/top.c
  head/usr.bin/top/top.h

Modified: head/usr.bin/top/commands.c
==============================================================================
--- head/usr.bin/top/commands.c	Fri Jun  8 22:06:32 2018	(r334863)
+++ head/usr.bin/top/commands.c	Sat Jun  9 02:14:33 2018	(r334864)
@@ -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\

Modified: head/usr.bin/top/machine.c
==============================================================================
--- head/usr.bin/top/machine.c	Fri Jun  8 22:06:32 2018	(r334863)
+++ head/usr.bin/top/machine.c	Sat Jun  9 02:14:33 2018	(r334864)
@@ -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 proces
 			/* not in use */
 			continue;
 
-		if (sel->self != -1 && pp->ki_pid == sel->self)
+		if (!sel->self && pp->ki_pid == mypid)
 			/* skip self */
 			continue;
 

Modified: head/usr.bin/top/machine.h
==============================================================================
--- head/usr.bin/top/machine.h	Fri Jun  8 22:06:32 2018	(r334863)
+++ head/usr.bin/top/machine.h	Sat Jun  9 02:14:33 2018	(r334864)
@@ -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 */

Modified: head/usr.bin/top/top.1
==============================================================================
--- head/usr.bin/top/top.1	Fri Jun  8 22:06:32 2018	(r334863)
+++ head/usr.bin/top/top.1	Sat Jun  9 02:14:33 2018	(r334864)
@@ -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
@@ -362,6 +365,9 @@ This will also enable the display of JID.
 .TP
 .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

Modified: head/usr.bin/top/top.c
==============================================================================
--- head/usr.bin/top/top.c	Fri Jun  8 22:06:32 2018	(r334863)
+++ head/usr.bin/top/top.c	Sat Jun  9 02:14:33 2018	(r334864)
@@ -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,7 +262,10 @@ 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
     extern FILE *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*/
 }
 
 /*

Modified: head/usr.bin/top/top.h
==============================================================================
--- head/usr.bin/top/top.h	Fri Jun  8 22:06:32 2018	(r334863)
+++ head/usr.bin/top/top.h	Sat Jun  9 02:14:33 2018	(r334864)
@@ -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,8 +44,10 @@ 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;
 
 extern int (*compares[])(const void*, const void*);
@@ -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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201806090214.w592EYsJ056425>