DragonFly submit List (threaded) for 2007-08
DragonFly BSD
DragonFly submit List (threaded) for 2007-08
[Date Prev][Date Next]  [Thread Prev][Thread Next]  [Date Index][Thread Index]

[PATCH] [V2] add options to ps(1) to display lwp data


From: "Nicolas Thery" <nthery@xxxxxxxxx>
Date: Tue, 14 Aug 2007 21:58:40 +0200

Hello,

Here is a revised patch that displays the TID when -H set.

Cheers,
Nicolas

Add '-H', 'nlwp', and 'tid' options to ps(1) to display some LWP data (inspired
from FreeBSD options).  See man page for details.

Fixed bug in libkvm when KERN_PROC_FLAG_LWP used with KERN_PROC_ALL.

Changed sysctl(KERN_PROC, ...) behaviour for kernel threads not associated
with lwps.  kinfo_lwp.kl_tid is set to -1 in this case instead of thread
structure address.  This is consistent with kinfo_proc.kp_pid, avoid
garbage values in ps -axH output and should not break anything as kl_tid
is not used anywhere in the source tree.

Index: dfly/src/bin/ps/keyword.c
===================================================================
--- dfly.orig/src/bin/ps/keyword.c	2007-08-14 21:05:22.294301000 +0200
+++ dfly/src/bin/ps/keyword.c	2007-08-14 21:12:32.000000000 +0200
@@ -127,6 +127,8 @@
 	{"nice", "NI", NULL, 0, pnice, NULL, 3, 0, 0, NULL, NULL},
 	{"nivcsw", "NIVCSW", NULL, USER, rvar, NULL, 5, ROFF(ru_nivcsw), LONG, "ld",
 		NULL},
+	{"nlwp", "NLWP", NULL, 0, pvar, NULL, 4, POFF(nthreads), INT, "d",
+		NULL},
 	{"nsignals", "", "nsigs", 0, NULL, NULL, 0, 0, 0, NULL, NULL},
 	{"nsigs", "NSIGS", NULL, USER, rvar, NULL, 4, ROFF(ru_nsignals), LONG, "ld",
 		NULL},
@@ -184,6 +186,8 @@
 		UIDFMT, NULL},
 	{"tdev", "TDEV", NULL, 0, tdev, NULL, 4, 0, 0, NULL, NULL},
 	{"tdpri", "TDPRI", NULL, 0, tdpri, NULL, 5, 0, 0, NULL, NULL},
+	{"tid", "TID", NULL, 0, lpvar, NULL, PIDLEN, LPOFF(tid), UINT, PIDFMT,
+		NULL},
 	{"time", "TIME", NULL, USER, cputime, NULL, 9, 0, 0, NULL, NULL},
 	{"tpgid", "TPGID", NULL, 0, pvar, NULL, 4, POFF(tpgid), UINT, PIDFMT, NULL},
 	{"tsess", "TSESS", NULL, 0, pvar, NULL, 6, POFF(tsid), UINT, PIDFMT, NULL},
@@ -254,6 +258,26 @@
 	free(op);
 }

+/*
+ * Insert TID column after PID one in selected output format.
+ */
+void
+insert_tid_in_fmt(void)
+{
+	struct varent *tidvent;
+	struct varent *vent;
+
+	if ((tidvent = makevarent("tid")) == NULL)
+		errx(1, "Not enough memory");
+
+	STAILQ_FOREACH(vent, &var_head, link) {
+		if (strcmp(vent->var->name, "pid") == 0) {
+			STAILQ_INSERT_AFTER(&var_head, vent, tidvent, link);
+			break;
+		}
+	}
+}
+
 static struct varent *
 makevarent(const char *p)
 {
Index: dfly/src/bin/ps/ps.c
===================================================================
--- dfly.orig/src/bin/ps/ps.c	2007-08-14 21:05:22.294749000 +0200
+++ dfly/src/bin/ps/ps.c	2007-08-14 21:12:32.000000000 +0200
@@ -79,10 +79,10 @@
 static int needuser, needcomm, needenv;
 #if defined(LAZY_PS)
 static int forceuread=0;
-#define PS_ARGS	"aCcefghjLlM:mN:O:o:p:rSTt:U:uvwx"
+#define PS_ARGS	"aCcefgHhjLlM:mN:O:o:p:rSTt:U:uvwx"
 #else
 static int forceuread=1;
-#define PS_ARGS	"aCceghjLlM:mN:O:o:p:rSTt:U:uvwx"
+#define PS_ARGS	"aCcegHhjLlM:mN:O:o:p:rSTt:U:uvwx"
 #endif

 enum sort { DEFAULT, SORTMEM, SORTCPU } sortby = DEFAULT;
@@ -119,8 +119,8 @@
 	dev_t ttydev;
 	pid_t pid;
 	uid_t *uids;
-	int all, ch, flag, i, fmt, lineno, nentries, nocludge, dropgid;
-	int prtheader, wflag, what, xflg, uid, nuids;
+	int all, ch, flag, i, fmt, ofmt, lineno, nentries, nocludge, dropgid;
+	int prtheader, wflag, what, xflg, uid, nuids, showtid;
 	char errbuf[_POSIX2_LINE_MAX];
 	const char *cp, *nlistf, *memf;
 	size_t btime_size = sizeof(struct timeval);
@@ -156,7 +156,7 @@
 			argv[1] = kludge_oldps_options(argv[1]);
 	}

-	all = fmt = prtheader = wflag = xflg = 0;
+	all = fmt = ofmt = prtheader = wflag = xflg = showtid = 0;
 	pid = -1;
 	nuids = 0;
 	uids = NULL;
@@ -179,6 +179,9 @@
 			break;
 		case 'g':
 			break;			/* no-op */
+		case 'H':
+			showtid = KERN_PROC_FLAG_LWP;
+			break;
 		case 'h':
 			prtheader = ws.ws_row > 5 ? ws.ws_row : 22;
 			break;
@@ -215,7 +218,7 @@
 			break;
 		case 'o':
 			parsefmt(optarg);
-			fmt = 1;
+			fmt = ofmt = 1;
 			break;
 #if defined(LAZY_PS)
 		case 'f':
@@ -315,6 +318,13 @@
 	if (!fmt)
 		parsefmt(dfmt);

+	/*
+	 * Add TID to output format if requested unless user-specific format
+	 * selected.
+	 */
+	if (showtid && !ofmt)
+		insert_tid_in_fmt();
+
 	/* XXX - should be cleaner */
 	if (!all && ttydev == NODEV && pid == -1 && !nuids) {
 		if ((uids = malloc(sizeof (*uids))) == NULL)
@@ -359,6 +369,8 @@
 		what = KERN_PROC_ALL;
 		flag = 0;
 	}
+	what |= showtid;
+
 	/*
 	 * select procs
 	 */
Index: dfly/src/bin/ps/ps.1
===================================================================
--- dfly.orig/src/bin/ps/ps.1	2007-08-14 21:12:12.000000000 +0200
+++ dfly/src/bin/ps/ps.1	2007-08-14 21:28:44.000000000 +0200
@@ -41,7 +41,7 @@
 .Nd process status
 .Sh SYNOPSIS
 .Nm
-.Op Fl aCcefhjlmrSTuvwx
+.Op Fl aCcefHhjlmrSTuvwx
 .Op Fl M Ar core
 .Op Fl N Ar system
 .Op Fl O Ar fmt
@@ -94,6 +94,15 @@
 .It Fl f
 Show commandline and environment information about swapped out processes.
 This option is honored only if the uid of the user is 0.
+.It Fl H
+Print one line per lightweight process (LWP) instead of one line per process.
+When this option is set and the
+.Fl o
+option is not set, the
+.Cm tid
+column is inserted in the output format after the
+.Cm pid
+one.
 .It Fl h
 Repeat the information header as often as necessary to guarantee one
 header per page of information.
@@ -382,6 +391,8 @@
 .Cm ni )
 .It Cm nivcsw
 total involuntary context switches
+.It Cm nlwp
+number of lightweight processes
 .It Cm nsigs
 total signals taken (alias
 .Cm nsignals )
@@ -455,6 +466,8 @@
 control terminal device number
 .It Cm tdpri
 LWKT thread priority (0-31, 31 highest), and critical section count
+.It Cm tid
+thread ID (aka lightweight process ID)
 .It Cm time
 accumulated CPU time, user + system (alias
 .Cm cputime )
Index: dfly/src/lib/libkvm/kvm_proc.c
===================================================================
--- dfly.orig/src/lib/libkvm/kvm_proc.c	2007-08-14 21:05:22.302733000 +0200
+++ dfly/src/lib/libkvm/kvm_proc.c	2007-08-14 21:12:32.000000000 +0200
@@ -333,6 +333,7 @@
 kvm_getprocs(kvm_t *kd, int op, int arg, int *cnt)
 {
 	int mib[4], st, nprocs;
+	int miblen = ((op & ~KERN_PROC_FLAGMASK) == KERN_PROC_ALL) ? 3 : 4;
 	size_t size;

 	if (kd->procbase != 0) {
@@ -349,7 +350,7 @@
 		mib[1] = KERN_PROC;
 		mib[2] = op;
 		mib[3] = arg;
-		st = sysctl(mib, op == KERN_PROC_ALL ? 3 : 4, NULL, &size, NULL, 0);
+		st = sysctl(mib, miblen, NULL, &size, NULL, 0);
 		if (st == -1) {
 			_kvm_syserr(kd, kd->program, "kvm_getprocs");
 			return (0);
@@ -360,8 +361,7 @@
 			    _kvm_realloc(kd, kd->procbase, size);
 			if (kd->procbase == 0)
 				return (0);
-			st = sysctl(mib, op == KERN_PROC_ALL ? 3 : 4,
-			    kd->procbase, &size, NULL, 0);
+			st = sysctl(mib, miblen, kd->procbase, &size, NULL, 0);
 		} while (st == -1 && errno == ENOMEM);
 		if (st == -1) {
 			_kvm_syserr(kd, kd->program, "kvm_getprocs");
Index: dfly/src/bin/ps/extern.h
===================================================================
--- dfly.orig/src/bin/ps/extern.h	2007-08-14 21:05:22.295408000 +0200
+++ dfly/src/bin/ps/extern.h	2007-08-14 21:12:32.000000000 +0200
@@ -63,6 +63,7 @@
 void	 p_rssize(const KINFO *, const struct varent *);
 void	 pagein(const KINFO *, const struct varent *);
 void	 parsefmt(const char *);
+void	 insert_tid_in_fmt(void);
 void	 pcpu(const KINFO *, const struct varent *);
 void	 pnice(const KINFO *, const struct varent *);
 void	 pmem(const KINFO *, const struct varent *);
Index: dfly/src/sys/kern/kern_kinfo.c
===================================================================
--- dfly.orig/src/sys/kern/kern_kinfo.c	2007-08-14 21:35:18.000000000 +0200
+++ dfly/src/sys/kern/kern_kinfo.c	2007-08-14 21:36:39.000000000 +0200
@@ -218,7 +218,7 @@
 	kp->kp_stat = SACTIVE;

 	kp->kp_lwp.kl_pid = -1;
-	kp->kp_lwp.kl_tid = (uintptr_t)td;
+	kp->kp_lwp.kl_tid = -1;
 	kp->kp_lwp.kl_tdflags = td->td_flags;
 #ifdef SMP
 	kp->kp_lwp.kl_mpcount = td->td_mpcount;



[Date Prev][Date Next]  [Thread Prev][Thread Next]  [Date Index][Thread Index]