Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 17 Nov 2010 10:26:38 +0300
From:      Anonymous <swell.k@gmail.com>
To:        Xin LI <d@delphij.net>
Cc:        FreeBSD Current <freebsd-current@FreeBSD.ORG>
Subject:   Re: [PATCH] top(1) inverse display of table header
Message-ID:  <861v6kwgup.fsf@gmail.com>
References:  <4CC62413.50703@delphij.net>

next in thread | previous in thread | raw e-mail | index | archive | help
--=-=-=
Content-Type: text/plain

Xin LI <delphij@delphij.net> writes:

> Hi,
>
> Here is a patch that makes top(1) to inverse its table header (PID
> USERNAME THR, etc).
>

That MAX_COLS change in it makes `top -b' produce too much extra
whitespace that's not trimmed and spans several lines, e.g.

  $ stty -a | sed 1q
  speed 38400 baud; 57 rows; 79 columns;
  $ top -b
  last pid: 54799;  load averages:  1.90,  2.16,  2.16  up 0+04:28:34    10:18:15
  91 processes:  4 running, 85 sleeping, 2 stopped

  Mem: 540M Active, 243M Inact, 2934M Wired, 79M Cache, 417M Buf, 136M Free
  Swap: 4097M Total, 8856K Used, 4088M Free


    PID USERNAME      THR PRI NICE   SIZE    RES STATE   C   TIME   WCPU COMMAND                                                                                                                                                                                                                                                                                                                                                                                                            
   1730 holo           14  46    0   358M   197M ucond   1   1:24  0.00% firefox-bin

FYI, using inverse color for header line can also be achieved on
top-3.8b1 without touching source at all

  $ export TOPCOLORS='header=,#7'

edwin@ planned to update top(1) but stalled, not sure why.

  http://docs.freebsd.org/cgi/mid.cgi?20080928054620.GA80250

And I for one still use his branch with below diff.


--=-=-=
Content-Type: text/x-patch
Content-Disposition: inline; filename=top-3.8b1.diff
Content-Description: local hacks for top-3.8b1

against svn://svn.freebsd.org/base/user/edwin/top/top-3.8b1

    - cherry-pick changes from /head (r189626, r200979, r211419)
    - report realtime priority like /head
    - lower min_screenwidth for FLG field, for 80 col tty sake
    - use int type for counter
    - fix amd64/i386 cross-compilation
    - remove `-g', prefer DEBUG_FLAGS
    - abbreviate Kern/Kernel row like Mem/Memory
    - adjust whitespace

---
Index: contrib/top/display.c
===================================================================
--- contrib/top/display.c	(revision 215416)
+++ contrib/top/display.c	(working copy)
@@ -993,7 +993,6 @@
     if (*bt != -1)
     {
 	uptime = *tod - *bt;
-	uptime += 30;
 	uptime_days = uptime / 86400;
 
 	tm = gmtime(&uptime);
@@ -1312,7 +1311,7 @@
 }
 
 /*
- *  *_kernel(stats) - print "Kernel: " followed by the kernel summary string
+ *  *_kernel(stats) - print "Kern: " followed by the kernel summary string
  *
  *  Assumptions:  cursor is on "lastline", the previous line
  */
@@ -1323,7 +1322,7 @@
 {
     if (num_kernel > 0)
     {
-	display_write(0, y_kernel, 0, 0, "Kernel: ");
+	display_write(0, y_kernel, 0, 0, "Kern: ");
 
 	/* format and print the kernel summary */
 	summary_format(x_kernel, y_kernel, stats, kernel_names, kernel_cidx);
Index: contrib/top/username.c
===================================================================
--- contrib/top/username.c	(revision 215416)
+++ contrib/top/username.c	(working copy)
@@ -53,7 +53,6 @@
 
 #include <sys/types.h>
 #include <pwd.h>
-#include <utmp.h>
 
 #include "top.h"
 #include "utils.h"
@@ -72,7 +71,7 @@
 
 struct hash_data {
     int    uid;
-    char   name[UT_NAMESIZE + 1];  /* big enough? */
+    char   name[MAXLOGNAME];  /* big enough? */
     time_t expire;
 };
 
@@ -120,7 +119,7 @@
     {
 	if ((pw = getpwuid(uid)) != NULL)
 	{
-	    strncpy(data->name, pw->pw_name, UT_NAMESIZE);
+	    strncpy(data->name, pw->pw_name, MAXLOGNAME - 1);
 	    data->expire = now + EXPIRETIME;
 	    dprintf("username: updating %d with %s, expires %d\n",
 		    data->uid, data->name, data->expire);
Index: contrib/top/hash.c
===================================================================
--- contrib/top/hash.c	(revision 215416)
+++ contrib/top/hash.c	(working copy)
@@ -99,7 +99,7 @@
 next_prime(int x)
 
 {
-    double i, j;
+    int i, j;
     int f;
 
     i = x;
@@ -108,7 +108,7 @@
 	f=1;
 	for (j=2; j<i; j++)
 	{
-	    if ((i/j)==floor(i/j))
+	    if (((double)i/j)==floor(i/j))
 	    {
 		f=0;
 		break;
@@ -116,7 +116,7 @@
 	}
 	if (f)
 	{
-	    return (int)i;
+	    return i;
 	}
     }
     return 1;
Index: contrib/top/layout.h
===================================================================
--- contrib/top/layout.h	(revision 215416)
+++ contrib/top/layout.h	(working copy)
@@ -59,9 +59,9 @@
 #define  Y_BRKDN	1
 #define  X_CPUSTATES	0
 #define  Y_CPUSTATES	2
-#define  X_KERNEL       8
+#define  X_KERNEL       7
 #define  Y_KERNEL       3
-#define  X_MEM		5
+#define  X_MEM		7
 #define  Y_MEM		3
 #define  X_SWAP		6
 #define  Y_SWAP		4
Index: usr.bin/top/machine.c
===================================================================
--- usr.bin/top/machine.c	(revision 215416)
+++ usr.bin/top/machine.c	(working copy)
@@ -617,7 +617,68 @@
 fmt_nice(char *buf, int sz, struct kinfo_proc *pp)
 
 {
-    return snprintf(buf, sz, "%4d", PP(pp, nice) - NZERO);
+    const char *fifo, *kthread;
+    int rtpri, retval;
+
+    fifo = PRI_NEED_RR(PP(pp, pri.pri_class)) ? "" : "F";
+    kthread = (PP(pp, flag) & P_KTHREAD) ? "k" : "";
+
+    switch (PRI_BASE(PP(pp, pri.pri_class))) {
+
+    case PRI_ITHD:
+	retval = snprintf(buf, sz, "   -");
+	break;
+
+    case PRI_REALTIME:
+	/*
+	 * XXX: the kernel doesn't tell us the original rtprio and
+	 * doesn't really know what it was, so to recover it we
+	 * must be more chummy with the implementation than the
+	 * implementation is with itself.  pri_user gives a
+	 * constant "base" priority, but is only initialized
+	 * properly for user threads.  pri_native gives what the
+	 * kernel calls the "base" priority, but it isn't constant
+	 * since it is changed by priority propagation.  pri_native
+	 * also isn't properly initialized for all threads, but it
+	 * is properly initialized for kernel realtime and idletime
+	 * threads.  Thus we use pri_user for the base priority of
+	 * user threads (it is always correct) and pri_native for
+	 * the base priority of kernel realtime and idletime threads
+	 * (there is nothing better, and it is usually correct).
+	 *
+	 * The field width and thus the buffer are too small for
+	 * values like "kr31F", but such values shouldn't occur,
+	 * and if they do then the tailing "F" is not displayed.
+	 */
+	rtpri = ((PP(pp, flag) & P_KTHREAD) ? PP(pp, pri.pri_native) :
+		 PP(pp, pri.pri_user)) - PRI_MIN_REALTIME;
+	retval = snprintf(buf, sz, "%*sr%d%s", (rtpri < 10) + 1,
+			  kthread, rtpri, fifo);
+	break;
+
+    case PRI_TIMESHARE:
+	if (PP(pp, flag) & P_KTHREAD) {
+	    retval = snprintf(buf, sz, "   -");
+	    break;
+	}
+
+	retval = snprintf(buf, sz, "%4d", PP(pp, nice) - NZERO);
+	break;
+
+    case PRI_IDLE:
+	/* XXX: as above. */
+	rtpri = ((PP(pp, flag) & P_KTHREAD) ? PP(pp, pri.pri_native) :
+		 PP(pp, pri.pri_user)) - PRI_MIN_IDLE;
+	retval = snprintf(buf, sz, "%*si%d%s", (rtpri < 10) + 1,
+			  kthread, rtpri, fifo);
+	break;
+
+    default:
+	retval = snprintf(buf, sz, "?");
+	break;
+    }
+
+    return(retval);
 }
 
 int
@@ -903,7 +964,7 @@
     { "SIZE", 5, 1, 0, fmt_size },
     { "RES", 5, 1, 0, fmt_res },
     { "STATE", 6, 0, 0, fmt_state },
-    { "FLG", 3, 0, 84, fmt_flags },
+    { "FLG", 3, 0, 75, fmt_flags },
     { "C", 1, 0, 0, fmt_c },
     { "TIME", 6, 1, 0, fmt_time },
     { "CPU", 6, 1, 0, fmt_cpu },
@@ -1156,7 +1217,7 @@
 	static unsigned int swap_delay = 0;
 	static long long swapavail = 0;
 	static long long swapfree = 0;
-	static int bufspace = 0;
+	static long bufspace = 0;
 
 	get_vm_sum(&sum);
 
Index: usr.bin/top/Makefile
===================================================================
--- usr.bin/top/Makefile	(revision 215416)
+++ usr.bin/top/Makefile	(working copy)
@@ -7,8 +7,9 @@
 SRCS=	commands.c display.c machine.c screen.c top.c \
 	username.c utils.c version.c color.c hash.c
 SRCS+=	sigdesc.h config.h
+WARNS?=	2
 CFLAGS+= -DHAVE_GETOPT -DHAVE_STRERROR -DORDER -DTOPSIGWINCH -DHAS_SHOWTHREADS
-CFLAGS+= -I${.CURDIR} -I${TOPDIR} -I. -Wall -g
+CFLAGS+= -I${.CURDIR} -I${TOPDIR} -I.
 
 #
 # Uncomment the following line if you want users to be able to set -s
@@ -47,7 +48,6 @@
 	${.IMPSRC} > ${.TARGET}
 
 CLEANFILES+= config.h
-CPU!=	uname -m
 config.h: config.h.in
 	@${ECHO} Making ${.TARGET} from ${.ALLSRC:T}
 	sed \
@@ -55,7 +55,7 @@
 		-e 's/@DEFAULT_DELAY@/2/' \
 		-e 's/@HAVE_GETOPT_LONG@/1/' \
 		-e 's/@ENABLE_KILL@/1/' \
-		-e "s/@CPU@/${CPU}/" \
+		-e "s/@CPU@/${MACHINE}/" \
 		< ${.ALLSRC} > ${.TARGET}
 
 CLEANFILES+= top.1.local

--=-=-=--



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