cvs commit: src/usr.bin/top machine.c

Giorgos Keramidas keramida at ceid.upatras.gr
Sun Apr 17 06:59:13 PDT 2005


On 2005-04-17 23:01, Bruce Evans <bde at zeta.org.au> wrote:
> On Sun, 17 Apr 2005, Giorgos Keramidas wrote:
> >That's probably I could manage to do later today or tomorrow.  For the
> >moment, how does the following change look (change of NICE to NIC and
> >removal of CPU column)?
>
> The NICE column cannot be shrunk, because all 4 columns in it are used.

Ok, I didn't know that :-)

> top -S output on sledge:
>
> %%%
>   PID USERNAME    THR PRI NICE   SIZE    RES STATE  C   TIME   WCPU    CPU COMMAND
>    11 root          1 171   52     0K    16K CPU1   1  22.2H 98.97% 98.97% idle
>    12 root          1 171   52     0K    16K RUN    0  22.2H 98.93% 98.93% idle
>    59 root          1  20    0     0K    16K syncer 1   1:07  0.00%  0.00% sync
>    45 root          1 -32 -151     0K    16K WAIT   0   0:36  0.00%  0.00% swi4
>     4 root          1  -8    0     0K    16K -      1   0:16  0.00%  0.00% g_do
>     3 root          1  -8    0     0K    16K -      1   0:11  0.00%  0.00% g_up
> [...]
> %%%
>
> This also shows recent severe lossage in the COMMAND column, and bogus
> extra newlines after the "processes" and "Swap" rows (these only show
> up in output to a file).
>
> There used to be only 3 columns for NICE, but that broke when the priority
> range was expanded from [0..127] to [0..255] in rev.1.35 of machine.c and
> associated kernel changes.  Rev.1.36 of machine.c fixed this by adding a
> 4th column for NICE.  Before rev.1.36, "NICE" wasn't truncated to "NIC"
> in the header.  The value was just in the columns under "NIC" with the
> column under "E" left blank to serve as a separator.
>
> Just removing the CPU column doesn't seem right.  ps has a -C flag to
> switch between WCPU and CPU.

We can add a new toggle command to top, i.e. 'C', that toggles between
displaying WCPU or CPU.  This should regain us a lot of the space lost
from COMMAND after I added THR.

%%%
Diff also online at:
http://people.freebsd.org/~keramida/diff/top.wcputog.diff

Index: contrib/top/commands.c
===================================================================
RCS file: /home/ncvs/src/contrib/top/commands.c,v
retrieving revision 1.11
diff -u -r1.11 commands.c
--- contrib/top/commands.c	14 Apr 2005 15:02:03 -0000	1.11
+++ contrib/top/commands.c	17 Apr 2005 13:53:19 -0000
@@ -71,6 +71,7 @@
 d       - change number of displays to show\n\
 e       - list errors generated by last \"kill\" or \"renice\" command\n\
 i or I  - toggle the displaying of idle processes\n\
+C       - toggle the displaying of weighted CPU percentage\n\
 H       - toggle the displaying of threads\n\
 k       - kill processes; send a signal to a list of processes\n\
 m       - toggle the display between 'cpu' and 'io' modes\n\
Index: contrib/top/machine.h
===================================================================
RCS file: /home/ncvs/src/contrib/top/machine.h,v
retrieving revision 1.6
diff -u -r1.6 machine.h
--- contrib/top/machine.h	17 Jul 2003 23:56:40 -0000	1.6
+++ contrib/top/machine.h	17 Apr 2005 13:37:40 -0000
@@ -60,6 +60,7 @@
     int self;		/* show self */
     int system;		/* show system processes */
     int thread;		/* show threads */
+    int wcpu;		/* show weighted cpu */
     int uid;		/* only this uid (unless uid == -1) */
     char *command;	/* only this command (unless == NULL) */
 };
Index: contrib/top/top.c
===================================================================
RCS file: /home/ncvs/src/contrib/top/top.c,v
retrieving revision 1.16
diff -u -r1.16 top.c
--- contrib/top/top.c	14 Apr 2005 15:02:03 -0000	1.16
+++ contrib/top/top.c	17 Apr 2005 13:43:26 -0000
@@ -193,9 +193,9 @@
     fd_set readfds;
 
 #ifdef ORDER
-    static char command_chars[] = "\f qh?en#sdkriIutHmSo";
+    static char command_chars[] = "\f qh?en#sdkriIutHCmSo";
 #else
-    static char command_chars[] = "\f qh?en#sdkriIutHmS";
+    static char command_chars[] = "\f qh?en#sdkriIutHCmS";
 #endif
 /* these defines enumerate the "strchr"s of the commands in command_chars */
 #define CMD_redraw	0
@@ -216,10 +216,11 @@
 #define CMD_user	14
 #define CMD_selftog	15
 #define CMD_thrtog	16
-#define CMD_viewtog	17
-#define CMD_viewsys	18
+#define	CMD_wcputog	17
+#define CMD_viewtog	18
+#define CMD_viewsys	19
 #ifdef ORDER
-#define CMD_order       19
+#define CMD_order       20
 #endif
 
     /* set the buffer for stdout */
@@ -250,6 +251,7 @@
     ps.system  = No;
     ps.uid     = -1;
     ps.thread  = No;
+    ps.wcpu    = 1;
     ps.command = NULL;
 
     /* get preset options from the environment */
@@ -275,7 +277,7 @@
 	    optind = 1;
 	}
 
-	while ((i = getopt(ac, av, "SIHbinquvs:d:U:m:o:t")) != EOF)
+	while ((i = getopt(ac, av, "CSIHbinquvs:d:U:m:o:t")) != EOF)
 	{
 	    switch(i)
 	    {
@@ -383,15 +385,19 @@
 	      case 't':
 		ps.self = (ps.self == -1) ? getpid() : -1;
 		break;
-		
+
+	      case 'C':
+		ps.wcpu = !ps.wcpu;
+		break;
+
 	      case 'H':
 		ps.thread = !ps.thread;
 		break;
-		
+
 	      default:
 		fprintf(stderr, "\
 Top version %s\n\
-Usage: %s [-HISbinqut] [-d x] [-s x] [-o field] [-U username] [number]\n",
+Usage: %s [-CHISbinqut] [-d x] [-s x] [-o field] [-U username] [number]\n",
 			version_string(), myname);
 		exit(1);
 	    }
@@ -993,6 +999,15 @@
 				reset_display();
 				putchar('\r');
 				break;
+			    case CMD_wcputog:
+				ps.wcpu = !ps.wcpu;
+				new_message(MT_standout | MT_delayed,
+				    "Displaying %sCPU",
+				    ps.wcpu ? "W" : "");
+				header_text = format_header(uname_field);
+				reset_display();
+				putchar('\r');
+				break;
 			    case CMD_viewtog:
 				if (++displaymode == DISP_MAX)
 					displaymode = 0;
Index: usr.bin/top/machine.c
===================================================================
RCS file: /home/ncvs/src/usr.bin/top/machine.c,v
retrieving revision 1.71
diff -u -r1.71 machine.c
--- usr.bin/top/machine.c	16 Apr 2005 15:43:38 -0000	1.71
+++ usr.bin/top/machine.c	17 Apr 2005 13:51:12 -0000
@@ -103,20 +103,20 @@
 	"%5d %-*.*s %6ld %6ld %6ld %6ld %6ld %6ld %6.2f%% %.*s"
 
 static char smp_header_thr[] =
-	"  PID %-*.*s  THR PRI NICE   SIZE    RES STATE  C   TIME   WCPU    CPU COMMAND";
+	"  PID %-*.*s  THR PRI NICE   SIZE    RES STATE  C   TIME %6s COMMAND";
 static char smp_header[] =
-	"  PID %-*.*s "   "PRI NICE   SIZE    RES STATE  C   TIME   WCPU    CPU COMMAND";
+	"  PID %-*.*s "   "PRI NICE   SIZE    RES STATE  C   TIME %6s COMMAND";
 
 #define smp_Proc_format \
-	"%5d %-*.*s %s%3d %4d%7s %6s %-6.6s %1x%7s %5.2f%% %5.2f%% %.*s"
+	"%5d %-*.*s %s%3d %4d%7s %6s %-6.6s %1x%7s %5.2f%% %.*s"
 
 static char up_header_thr[] =
-	"  PID %-*.*s  THR PRI NICE   SIZE    RES STATE    TIME   WCPU    CPU COMMAND";
+	"  PID %-*.*s  THR PRI NICE   SIZE    RES STATE    TIME %6s COMMAND";
 static char up_header[] =
-	"  PID %-*.*s "   "PRI NICE   SIZE    RES STATE    TIME   WCPU    CPU COMMAND";
+	"  PID %-*.*s "   "PRI NICE   SIZE    RES STATE    TIME %6s COMMAND";
 
 #define up_Proc_format \
-	"%5d %-*.*s %s%3d %4d%7s %6s %-6.6s%.0d%7s %5.2f%% %5.2f%% %.*s"
+	"%5d %-*.*s %s%3d %4d%7s %6s %-6.6s%.0d%7s %5.2f%% %.*s"
 
 
 /* process state names for the "STATE" column of the display */
@@ -298,17 +298,17 @@
 		prehead = smpmode ?
 		    (ps.thread ? smp_header : smp_header_thr) :
 		    (ps.thread ? up_header : up_header_thr);
+		snprintf(Header, sizeof(Header), prehead,
+		    namelength, namelength, uname_field,
+		    ps.wcpu ? "WCPU" : "CPU");
 		break;
 	case DISP_IO:
 		prehead = io_header;
+		snprintf(Header, sizeof(Header), prehead,
+		    namelength, namelength, uname_field);
 		break;
 	}
-
-	snprintf(Header, sizeof(Header), prehead,
-	    namelength, namelength, uname_field);
-
 	cmdlengthdelta = strlen(Header) - 7;
-
 	return (Header);
 }
 
@@ -780,8 +780,7 @@
 	    status,
 	    smpmode ? pp->ki_lastcpu : 0,
 	    format_time(cputime),
-	    100.0 * weighted_cpu(pct, pp),
-	    100.0 * pct,
+	    ps.wcpu ? (100.0 * weighted_cpu(pct, pp)) : (100.0 * pct),
 	    screen_width > cmdlengthdelta ?
 	    screen_width - cmdlengthdelta :
 	    0,
%%%


More information about the cvs-src mailing list