Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 3 Jul 2006 18:33:38 +0800
From:      Cheng-Lung Sung <clsung@FreeBSD.org>
To:        freebsd-hackers@freebsd.org
Subject:   [patch] display jail id in /usr/bin/top
Message-ID:  <20060703103338.GA31839@FreeBSD.csie.nctu.edu.tw>

next in thread | raw e-mail | index | archive | help

--IS0zKkzwUGydFO0o
Content-Type: text/plain; charset=big5
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

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

    Usage:
    % top -j=20
    or in=20
    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 @@
  */
=20
 static char io_header[] =3D
-	"  PID %-*.*s   VCSW  IVCSW   READ  WRITE  FAULT  TOTAL PERCENT COMMAND";
+	"  PID%s %-*.*s   VCSW  IVCSW   READ  WRITE  FAULT  TOTAL PERCENT COMMAND=
";
=20
 #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"
=20
 static char smp_header_thr[] =3D
- 	"  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[] =3D
- 	"  PID %-*.*s "   "PRI NICE   SIZE    RES STATE  C   TIME %6s COMMAND";
+ 	"  PID%s %-*.*s "   "PRI NICE   SIZE    RES STATE  C   TIME %6s COMMAND";
=20
 #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"
=20
 static char up_header_thr[] =3D
- 	"  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[] =3D
- 	"  PID %-*.*s "   "PRI NICE   SIZE    RES STATE    TIME %6s COMMAND";
+ 	"  PID%s %-*.*s "   "PRI NICE   SIZE    RES STATE    TIME %6s COMMAND";
=20
 #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"
=20
=20
 /* process state names for the "STATE" column of the display */
@@ -218,7 +218,8 @@
  */
 char *ordernames[] =3D {
 	"cpu", "size", "res", "time", "pri", "threads",
-	"total", "read", "write", "fault", "vcsw", "ivcsw", NULL
+	"total", "read", "write", "fault", "vcsw", "ivcsw",=20
+	"jid", NULL
 };
 #endif
=20
@@ -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 =3D 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];
=20
 	/* find and remember the next proc structure */
 	hp =3D (struct handle *)handle;
@@ -718,6 +721,12 @@
 		break;
 	}
=20
+	if (ps.jail =3D=3D 0)
+		jid_buf[0] =3D '\0';
+	else
+		snprintf(jid_buf, sizeof(jid_buf), " %*d",
+		    sizeof(jid_buf) - 3, pp->ki_jid);
+
 	if (displaymode =3D=3D DISP_IO) {
 		oldp =3D get_old_proc(pp);
 		if (oldp !=3D NULL) {
@@ -735,6 +744,7 @@
=20
 		sprintf(fmt, io_Proc_format,
 		    pp->ki_pid,
+		    jid_buf,
 		    namelength, namelength,
 		    (*get_userid)(pp->ki_ruid),
 		    rup->ru_nvcsw,
@@ -760,6 +770,7 @@
=20
 	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)
=20
+#define	ORDERKEY_JID(a, b) do { \
+	int diff =3D (int)(b)->ki_jid - (int)(a)->ki_jid; \
+	if (diff !=3D 0) \
+		return (diff > 0 ? 1 : -1); \
+} while (0)
+
 /* compare_cpu - the comparison function for sorting by cpu percentage */
=20
 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_iofaul=
t(), compare_vcsw(), compare_ivcsw();
+/* jail id compare routine */
+int compare_jid();
=20
 int (*compares[])() =3D {
 	compare_cpu,
@@ -932,6 +951,7 @@
 	compare_iofault,
 	compare_vcsw,
 	compare_ivcsw,
+	compare_jid,
 	NULL
 };
=20
@@ -1015,6 +1035,24 @@
 	struct kinfo_proc *p2 =3D *(struct kinfo_proc **)arg2;
=20
 	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 =3D *(struct kinfo_proc **)arg1;
+	struct kinfo_proc *p2 =3D *(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 =3D=3D -1) */
     int wcpu;		/* show weighted cpu */
+    int jail;		/* show jail id */
     char *command;	/* only this command (unless =3D=3D NULL) */
 };
=20
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;
=20
 #ifdef ORDER
-    static char command_chars[] =3D "\f qh?en#sdkriIutHmSCo";
+    static char command_chars[] =3D "\f qh?en#sdkriIutHmSCoj";
 #else
-    static char command_chars[] =3D "\f qh?en#sdkriIutHmSC";
+    static char command_chars[] =3D "\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
=20
     /* set the buffer for stdout */
 #ifdef DEBUG
@@ -252,6 +253,7 @@
     ps.uid     =3D -1;
     ps.thread  =3D No;
     ps.wcpu    =3D 1;
+    ps.jail    =3D No;
     ps.command =3D NULL;
=20
     /* get preset options from the environment */
@@ -277,7 +279,7 @@
 	    optind =3D 1;
 	}
=20
-	while ((i =3D getopt(ac, av, "CSIHbinquvs:d:U:m:o:t")) !=3D EOF)
+	while ((i =3D getopt(ac, av, "CSIHbijnquvs:d:U:m:o:t")) !=3D EOF)
 	{
 	    switch(i)
 	    {
@@ -394,6 +396,10 @@
 		ps.thread =3D !ps.thread;
 		break;
=20
+	      case 'j':
+		ps.jail =3D !ps.jail;
+		break;
+
 	      default:
 		fprintf(stderr,
 "Top version %s\n"
@@ -1044,6 +1050,15 @@
 				}
 				break;
 #endif
+			    case CMD_jidtog:
+				ps.jail =3D !ps.jail;
+				new_message(MT_standout | MT_delayed,
+				    " %sisplaying jail id.",
+				    ps.jail ? "D" : "Not d");
+				header_text =3D format_header(uname_field);
+				reset_display();
+				putchar('\r');
+				break;
 	   =20
 			    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 na=
me
-of the process's owner (if
+but it is not exactly the same.  PID is the process id,=20
+JID, when displayed, is the=20
+.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,
--=20
Cheng-Lung Sung - clsung@

--IS0zKkzwUGydFO0o
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.4 (FreeBSD)

iD8DBQFEqPKB+AeJ85Vui8ERAla1AJ0eCcBU7jJ7savEMl5qKcqSHtxIBwCgg6o3
2aS1ssGAbLN0MdGJYi0+kjQ=
=VyQS
-----END PGP SIGNATURE-----

--IS0zKkzwUGydFO0o--



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