[patch] display jail id in /usr/bin/top

Cheng-Lung Sung clsung at FreeBSD.org
Mon Jul 3 10:32:12 UTC 2006


Hi, 
    The attached patch provide top(1) the ability to display 
    jail id. 

    Usage:
    % top -j 
    or in 
    interactive mode, toggle with 'j'.

    Suggestions are needed.

diff -ruN /usr/src/usr.bin/top/machine.c.orig usr.bin/top/machine.c
--- /usr/src/usr.bin/top/machine.c	Wed May 18 21:42:51 2005
+++ usr.bin/top/machine.c	Sun Jun  4 22:13:00 2006
@@ -99,26 +99,26 @@
  */
 
 static char io_header[] =
-	"  PID %-*.*s   VCSW  IVCSW   READ  WRITE  FAULT  TOTAL PERCENT COMMAND";
+	"  PID%s %-*.*s   VCSW  IVCSW   READ  WRITE  FAULT  TOTAL PERCENT COMMAND";
 
 #define io_Proc_format \
-	"%5d %-*.*s %6ld %6ld %6ld %6ld %6ld %6ld %6.2f%% %.*s"
+	"%5d%s %-*.*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 %6s COMMAND";
+ 	"  PID%s %-*.*s  THR PRI NICE   SIZE    RES STATE  C   TIME %6s COMMAND";
 static char smp_header[] =
- 	"  PID %-*.*s "   "PRI NICE   SIZE    RES STATE  C   TIME %6s COMMAND";
+ 	"  PID%s %-*.*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%% %.*s"
+ 	"%5d%s %-*.*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 %6s COMMAND";
+ 	"  PID%s %-*.*s  THR PRI NICE   SIZE    RES STATE    TIME %6s COMMAND";
 static char up_header[] =
- 	"  PID %-*.*s "   "PRI NICE   SIZE    RES STATE    TIME %6s COMMAND";
+ 	"  PID%s %-*.*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%% %.*s"
+ 	"%5d%s %-*.*s %s%3d %4d%7s %6s %-6.6s%.0d%7s %5.2f%% %.*s"
 
 
 /* process state names for the "STATE" column of the display */
@@ -218,7 +218,8 @@
  */
 char *ordernames[] = {
 	"cpu", "size", "res", "time", "pri", "threads",
-	"total", "read", "write", "fault", "vcsw", "ivcsw", NULL
+	"total", "read", "write", "fault", "vcsw", "ivcsw", 
+	"jid", NULL
 };
 #endif
 
@@ -299,12 +300,14 @@
 		    (ps.thread ? smp_header : smp_header_thr) :
 		    (ps.thread ? up_header : up_header_thr);
 		snprintf(Header, sizeof(Header), prehead,
+		    ps.jail ? " JID" : "",
 		    namelength, namelength, uname_field,
 		    ps.wcpu ? "WCPU" : "CPU");
 		break;
 	case DISP_IO:
 		prehead = io_header;
 		snprintf(Header, sizeof(Header), prehead,
+		    ps.jail ? " JID" : "",
 		    namelength, namelength, uname_field);
 		break;
 	}
@@ -657,7 +660,7 @@
 	int state;
 	struct rusage ru, *rup;
 	long p_tot, s_tot;
-	char *proc_fmt, thr_buf[6];
+	char *proc_fmt, thr_buf[6], jid_buf[6];
 
 	/* find and remember the next proc structure */
 	hp = (struct handle *)handle;
@@ -718,6 +721,12 @@
 		break;
 	}
 
+	if (ps.jail == 0)
+		jid_buf[0] = '\0';
+	else
+		snprintf(jid_buf, sizeof(jid_buf), " %*d",
+		    sizeof(jid_buf) - 3, pp->ki_jid);
+
 	if (displaymode == DISP_IO) {
 		oldp = get_old_proc(pp);
 		if (oldp != NULL) {
@@ -735,6 +744,7 @@
 
 		sprintf(fmt, io_Proc_format,
 		    pp->ki_pid,
+		    jid_buf,
 		    namelength, namelength,
 		    (*get_userid)(pp->ki_ruid),
 		    rup->ru_nvcsw,
@@ -760,6 +770,7 @@
 
 	sprintf(fmt, proc_fmt,
 	    pp->ki_pid,
+	    jid_buf,
 	    namelength, namelength,
 	    (*get_userid)(pp->ki_ruid),
 	    thr_buf,
@@ -891,6 +902,12 @@
 		return (diff > 0 ? 1 : -1); \
 } while (0)
 
+#define	ORDERKEY_JID(a, b) do { \
+	int diff = (int)(b)->ki_jid - (int)(a)->ki_jid; \
+	if (diff != 0) \
+		return (diff > 0 ? 1 : -1); \
+} while (0)
+
 /* compare_cpu - the comparison function for sorting by cpu percentage */
 
 int
@@ -918,6 +935,8 @@
 int compare_size(), compare_res(), compare_time(), compare_prio(), compare_threads();
 /* io compare routines */
 int compare_iototal(), compare_ioread(), compare_iowrite(), compare_iofault(), compare_vcsw(), compare_ivcsw();
+/* jail id compare routine */
+int compare_jid();
 
 int (*compares[])() = {
 	compare_cpu,
@@ -932,6 +951,7 @@
 	compare_iofault,
 	compare_vcsw,
 	compare_ivcsw,
+	compare_jid,
 	NULL
 };
 
@@ -1015,6 +1035,24 @@
 	struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
 
 	ORDERKEY_THREADS(p1, p2);
+	ORDERKEY_PCTCPU(p1, p2);
+	ORDERKEY_CPTICKS(p1, p2);
+	ORDERKEY_STATE(p1, p2);
+	ORDERKEY_PRIO(p1, p2);
+	ORDERKEY_RSSIZE(p1, p2);
+	ORDERKEY_MEM(p1, p2);
+
+	return (0);
+}
+
+/* compare_jid - the comparison function for sorting by jid */
+int
+compare_jid(void *arg1, void *arg2)
+{
+	struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
+	struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
+
+	ORDERKEY_JID(p1, p2);
 	ORDERKEY_PCTCPU(p1, p2);
 	ORDERKEY_CPTICKS(p1, p2);
 	ORDERKEY_STATE(p1, p2);
diff -ruN /usr/src/contrib/top/machine.h.orig contrib/top/machine.h
--- /usr/src/contrib/top/machine.h.orig	Wed May 18 21:30:08 2005
+++ contrib/top/machine.h	Sat Jun  3 12:25:15 2006
@@ -62,6 +62,7 @@
     int thread;		/* show threads */
     int uid;		/* only this uid (unless uid == -1) */
     int wcpu;		/* show weighted cpu */
+    int jail;		/* show jail id */
     char *command;	/* only this command (unless == NULL) */
 };
 
diff -ruN /usr/src/contrib/top/top.c.orig contrib/top/top.c
--- /usr/src/contrib/top/top.c.orig	Sun Apr 30 14:06:33 2006
+++ contrib/top/top.c	Sat Jun  3 12:38:45 2006
@@ -193,9 +193,9 @@
     fd_set readfds;
 
 #ifdef ORDER
-    static char command_chars[] = "\f qh?en#sdkriIutHmSCo";
+    static char command_chars[] = "\f qh?en#sdkriIutHmSCoj";
 #else
-    static char command_chars[] = "\f qh?en#sdkriIutHmSC";
+    static char command_chars[] = "\f qh?en#sdkriIutHmSCj";
 #endif
 /* these defines enumerate the "strchr"s of the commands in command_chars */
 #define CMD_redraw	0
@@ -222,6 +222,7 @@
 #ifdef ORDER
 #define CMD_order       20
 #endif
+#define	CMD_jidtog	21
 
     /* set the buffer for stdout */
 #ifdef DEBUG
@@ -252,6 +253,7 @@
     ps.uid     = -1;
     ps.thread  = No;
     ps.wcpu    = 1;
+    ps.jail    = No;
     ps.command = NULL;
 
     /* get preset options from the environment */
@@ -277,7 +279,7 @@
 	    optind = 1;
 	}
 
-	while ((i = getopt(ac, av, "CSIHbinquvs:d:U:m:o:t")) != EOF)
+	while ((i = getopt(ac, av, "CSIHbijnquvs:d:U:m:o:t")) != EOF)
 	{
 	    switch(i)
 	    {
@@ -394,6 +396,10 @@
 		ps.thread = !ps.thread;
 		break;
 
+	      case 'j':
+		ps.jail = !ps.jail;
+		break;
+
 	      default:
 		fprintf(stderr,
 "Top version %s\n"
@@ -1044,6 +1050,15 @@
 				}
 				break;
 #endif
+			    case CMD_jidtog:
+				ps.jail = !ps.jail;
+				new_message(MT_standout | MT_delayed,
+				    " %sisplaying jail id.",
+				    ps.jail ? "D" : "Not d");
+				header_text = format_header(uname_field);
+				reset_display();
+				putchar('\r');
+				break;
 	    
 			    default:
 				new_message(MT_standout, " BAD CASE IN SWITCH!");

diff -ruN /usr/src/contrib/top/top.X.orig contrib/top/top.X
--- /usr/src/contrib/top/top.X.orig	Wed Jun 28 08:57:54 2006
+++ contrib/top/top.X	Fri Jun 30 13:15:10 2006
@@ -96,6 +96,11 @@
 Do not display idle processes.
 By default, top displays both active and idle processes.
 .TP
+.B \-j
+Display the
+.IR jail (8)
+id.
+.TP
 .B \-t
 Do not display the
 .I top
@@ -170,7 +175,8 @@
 The default for
 .I count
 on an intelligent terminal is, in fact,
-.BI infinity .
+.BI infinity
+.
 .PP
 The environment variable
 .B TOP
@@ -221,7 +227,8 @@
 .TP
 .B q
 Quit
-.IR top.
+.IR top
+.
 .TP
 .B d
 Change the number of displays to show (prompt for new number).
@@ -277,6 +284,11 @@
 .BR I )
 Toggle the display of idle processes.
 .TP
+.B j
+Toggle the display of
+.IR jail (8)
+id.
+.TP
 .B t
 Toggle the display of the
 .I top
@@ -302,8 +314,11 @@
 The remainder of the screen displays information about individual
 processes.  This display is similar in spirit to
 .IR ps (1)
-but it is not exactly the same.  PID is the process id, USERNAME is the name
-of the process's owner (if
+but it is not exactly the same.  PID is the process id, 
+JID, when displayed, is the 
+.IR jail (8)
+ID corresponding to the process,
+USERNAME is the name of the process's owner (if
 .B \-u
 is specified, a UID column will be substituted for USERNAME),
 PRI is the current priority of the process,
-- 
Cheng-Lung Sung - clsung@
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 187 bytes
Desc: not available
Url : http://lists.freebsd.org/pipermail/freebsd-hackers/attachments/20060703/1b81465e/attachment.pgp


More information about the freebsd-hackers mailing list