Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 8 Sep 2009 15:13:49 +0000 (UTC)
From:      Attilio Rao <attilio@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-6@freebsd.org
Subject:   svn commit: r196975 - in stable/6: bin/ps sys/kern
Message-ID:  <200909081513.n88FDn06051220@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: attilio
Date: Tue Sep  8 15:13:49 2009
New Revision: 196975
URL: http://svn.freebsd.org/changeset/base/196975

Log:
  MFC r189078:
  
  * Implement ucomm with a dynamic size function in order to print out the
    threads name also in the case they are not the last one shown.
  * On AMD64 pointers don't have enough space to be printed. Fix it.
  * Check a return value for malloc which wasn't checked before
  
  Sponsored by: Sandvine Incorporated

Modified:
  stable/6/bin/ps/extern.h
  stable/6/bin/ps/keyword.c
  stable/6/bin/ps/print.c
  stable/6/bin/ps/ps.c
  stable/6/sys/kern/kern_proc.c

Modified: stable/6/bin/ps/extern.h
==============================================================================
--- stable/6/bin/ps/extern.h	Tue Sep  8 14:43:42 2009	(r196974)
+++ stable/6/bin/ps/extern.h	Tue Sep  8 15:13:49 2009	(r196975)
@@ -39,7 +39,7 @@ extern fixpt_t ccpu;
 extern int cflag, eval, fscale, nlistread, rawcpu;
 extern unsigned long mempages;
 extern time_t now;
-extern int sumrusage, termwidth, totwidth;
+extern int showthreads, sumrusage, termwidth, totwidth;
 extern STAILQ_HEAD(velisthead, varent) varlist;
 
 __BEGIN_DECLS
@@ -71,6 +71,7 @@ void	 priorityr(KINFO *, VARENT *);
 void	 rgroupname(KINFO *, VARENT *);
 void	 runame(KINFO *, VARENT *);
 void	 rvar(KINFO *, VARENT *);
+int	 s_comm(KINFO *);
 int	 s_label(KINFO *);
 int	 s_rgroupname(KINFO *);
 int	 s_runame(KINFO *);

Modified: stable/6/bin/ps/keyword.c
==============================================================================
--- stable/6/bin/ps/keyword.c	Tue Sep  8 14:43:42 2009	(r196974)
+++ stable/6/bin/ps/keyword.c	Tue Sep  8 15:13:49 2009	(r196975)
@@ -79,8 +79,8 @@ static VAR var[] = {
 		CHAR, NULL, 0},
 	{"blocked", "", "sigmask", 0, NULL, NULL, 0, 0, CHAR, NULL, 0},
 	{"caught", "", "sigcatch", 0, NULL, NULL, 0, 0, CHAR, NULL, 0},
-	{"comm", "COMMAND", NULL, LJUST, ucomm, NULL, MAXCOMLEN, 0, CHAR,
-		NULL, 0},
+	{"comm", "COMMAND", NULL, LJUST|DSIZ, ucomm, s_comm,
+		COMMLEN + OCOMMLEN + 1, 0, CHAR, NULL, 0},
 	{"command", "COMMAND", NULL, COMM|LJUST|USER, command, NULL, 16, 0,
 		CHAR, NULL, 0},
 	{"cpu", "CPU", NULL, 0, kvar, NULL, 3, KOFF(ki_estcpu), UINT, "d",
@@ -135,12 +135,13 @@ static VAR var[] = {
 		LONG, "ld", 0},
 	{"nvcsw", "NVCSW", NULL, USER, rvar, NULL, 5, ROFF(ru_nvcsw),
 		LONG, "ld", 0},
-	{"nwchan", "NWCHAN", NULL, LJUST, nwchan, NULL, 8, 0, CHAR, NULL, 0},
+	{"nwchan", "NWCHAN", NULL, LJUST, nwchan, NULL, sizeof(void *) * 2, 0,
+		CHAR, NULL, 0},
 	{"oublk", "OUBLK", NULL, USER, rvar, NULL, 4, ROFF(ru_oublock),
 		LONG, "ld", 0},
 	{"oublock", "", "oublk", 0, NULL, NULL, 0, 0, CHAR, NULL, 0},
-	{"paddr", "PADDR", NULL, 0, kvar, NULL, 8, KOFF(ki_paddr), KPTR,
-		"lx", 0},
+	{"paddr", "PADDR", NULL, 0, kvar, NULL, sizeof(void *) * 2,
+		KOFF(ki_paddr), KPTR, "lx", 0},
 	{"pagein", "PAGEIN", NULL, USER, pagein, NULL, 6, 0, CHAR, NULL, 0},
 	{"pcpu", "", "%cpu", 0, NULL, NULL, 0, 0, CHAR, NULL, 0},
 	{"pending", "", "sig", 0, NULL, NULL, 0, 0, CHAR, NULL, 0},
@@ -194,13 +195,13 @@ static VAR var[] = {
 	{"tsiz", "TSIZ", NULL, 0, kvar, NULL, 4, KOFF(ki_tsize), PGTOK, "ld", 0},
 	{"tt", "TT ", NULL, 0, tname, NULL, 4, 0, CHAR, NULL, 0},
 	{"tty", "TTY", NULL, LJUST, longtname, NULL, 8, 0, CHAR, NULL, 0},
-	{"ucomm", "UCOMM", NULL, LJUST, ucomm, NULL, MAXCOMLEN, 0, CHAR, NULL,
-		0},
+	{"ucomm", "UCOMM", NULL, LJUST|DSIZ, ucomm, s_comm,
+		COMMLEN + OCOMMLEN + 1, 0, CHAR, NULL, 0},
 	{"uid", "UID", NULL, 0, kvar, NULL, UIDLEN, KOFF(ki_uid), UINT,
 		UIDFMT, 0},
 	{"upr", "UPR", NULL, 0, upr, NULL, 3, 0, CHAR, NULL, 0},
-	{"uprocp", "UPROCP", NULL, 0, kvar, NULL, 8, KOFF(ki_paddr), KPTR,
-		"lx", 0},
+	{"uprocp", "UPROCP", NULL, 0, kvar, NULL, sizeof(void *) * 2,
+		KOFF(ki_paddr), KPTR, "lx", 0},
 	{"user", "USER", NULL, LJUST|DSIZ, uname, s_uname, USERLEN, 0, CHAR,
 		NULL, 0},
 	{"usrpri", "", "upr", 0, NULL, NULL, 0, 0, CHAR, NULL, 0},
@@ -324,6 +325,8 @@ findvar(char *p, int user, char **header
 			 */
 			rflen = strlen(v->alias) + strlen(hp) + 2;
 			realfmt = malloc(rflen);
+			if (realfmt == NULL)
+				errx(1, "malloc failed");
 			snprintf(realfmt, rflen, "%s=%s", v->alias, hp);
 			parsefmt(realfmt, user);
 		}

Modified: stable/6/bin/ps/print.c
==============================================================================
--- stable/6/bin/ps/print.c	Tue Sep  8 14:43:42 2009	(r196974)
+++ stable/6/bin/ps/print.c	Tue Sep  8 15:13:49 2009	(r196975)
@@ -175,13 +175,22 @@ command(KINFO *k, VARENT *ve)
 void
 ucomm(KINFO *k, VARENT *ve)
 {
+	char tmpbuff[COMMLEN + OCOMMLEN + 2];
 	VAR *v;
 
 	v = ve->var;
 	if (STAILQ_NEXT(ve, next_ve) == NULL)	/* last field, don't pad */
 		(void)printf("%s", k->ki_p->ki_comm);
-	else
-		(void)printf("%-*s", v->width, k->ki_p->ki_comm);
+	else {
+		bzero(tmpbuff, sizeof(tmpbuff));
+		if (showthreads && k->ki_p->ki_numthreads > 1)
+			sprintf(tmpbuff, "%s/%s", k->ki_p->ki_comm,
+			    k->ki_p->ki_ocomm);
+		else
+			sprintf(tmpbuff, "%s", k->ki_p->ki_comm);
+		(void)printf("%-*s", v->width, tmpbuff);
+	}
+
 }
 
 void
@@ -804,6 +813,20 @@ out:
 }
 
 int
+s_comm(KINFO *k)
+{
+	char tmpbuff[COMMLEN + OCOMMLEN + 2];
+
+	bzero(tmpbuff, sizeof(tmpbuff));
+	if (showthreads && k->ki_p->ki_numthreads > 1)
+		sprintf(tmpbuff, "%s/%s", k->ki_p->ki_comm,
+		    k->ki_p->ki_ocomm);
+	else
+		sprintf(tmpbuff, "%s", k->ki_p->ki_comm);
+	return (strlen(tmpbuff));
+}
+
+int
 s_label(KINFO *k)
 {
 	char *string = NULL;

Modified: stable/6/bin/ps/ps.c
==============================================================================
--- stable/6/bin/ps/ps.c	Tue Sep  8 14:43:42 2009	(r196974)
+++ stable/6/bin/ps/ps.c	Tue Sep  8 15:13:49 2009	(r196975)
@@ -98,6 +98,7 @@ int	 rawcpu;		/* -C */
 int	 sumrusage;		/* -S */
 int	 termwidth;		/* Width of the screen (0 == infinity). */
 int	 totwidth;		/* Calculated-width of requested variables. */
+int	 showthreads;		/* will threads be shown? */
 
 struct velisthead varlist = STAILQ_HEAD_INITIALIZER(varlist);
 
@@ -175,7 +176,7 @@ main(int argc, char *argv[])
 	char *cols;
 	int all, ch, elem, flag, _fmt, i, lineno;
 	int nentries, nkept, nselectors;
-	int prtheader, showthreads, wflag, what, xkeep, xkeep_implied;
+	int prtheader, wflag, what, xkeep, xkeep_implied;
 	char errbuf[_POSIX2_LINE_MAX];
 
 	(void) setlocale(LC_ALL, "");

Modified: stable/6/sys/kern/kern_proc.c
==============================================================================
--- stable/6/sys/kern/kern_proc.c	Tue Sep  8 14:43:42 2009	(r196974)
+++ stable/6/sys/kern/kern_proc.c	Tue Sep  8 15:13:49 2009	(r196975)
@@ -72,6 +72,7 @@ MALLOC_DEFINE(M_SUBPROC, "subproc", "Pro
 
 static void doenterpgrp(struct proc *, struct pgrp *);
 static void orphanpg(struct pgrp *pg);
+static void fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp);
 static void fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp);
 static void fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp);
 static void pgadjustjobc(struct pgrp *pgrp, int entering);
@@ -611,6 +612,26 @@ DB_SHOW_COMMAND(pgrpdump, pgrpdump)
 #endif /* DDB */
 
 /*
+ * Rework the kinfo_proc members which need to be aggregated in the
+ * case of process-ware informations.
+ * Must be called with sched_lock held.
+ */
+static void
+fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp)
+{
+	struct thread *td;
+
+	mtx_assert(&sched_lock, MA_OWNED);
+
+	kp->ki_estcpu = 0;
+	kp->ki_pctcpu = 0;
+	FOREACH_THREAD_IN_PROC(p, td) {
+		kp->ki_pctcpu += sched_pctcpu(td);
+		kp->ki_estcpu += td->td_ksegrp->kg_estcpu;
+	}
+}
+
+/*
  * Clear kinfo_proc and fill in any information that is common
  * to all threads in the process.
  * Must be called with the target process locked.
@@ -830,8 +851,9 @@ fill_kinfo_proc(struct proc *p, struct k
 
 	fill_kinfo_proc_only(p, kp);
 	mtx_lock_spin(&sched_lock);
-	if (FIRST_THREAD_IN_PROC(p) != NULL)
-		fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), kp);
+	MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
+	fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), kp);
+	fill_kinfo_aggregate(p, kp);
 	mtx_unlock_spin(&sched_lock);
 }
 
@@ -897,27 +919,20 @@ sysctl_out_proc(struct proc *p, struct s
 
 	PROC_LOCK_ASSERT(p, MA_OWNED);
 
-	fill_kinfo_proc_only(p, &kinfo_proc);
-	if (flags & KERN_PROC_NOTHREADS) {
-		mtx_lock_spin(&sched_lock);
-		if (FIRST_THREAD_IN_PROC(p) != NULL)
-			fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), &kinfo_proc);
-		mtx_unlock_spin(&sched_lock);
+	fill_kinfo_proc(p, &kinfo_proc);
+	if (flags & KERN_PROC_NOTHREADS)
 		error = SYSCTL_OUT(req, (caddr_t)&kinfo_proc,
-				   sizeof(kinfo_proc));
-	} else {
+		    sizeof(kinfo_proc));
+	else {
 		mtx_lock_spin(&sched_lock);
-		if (FIRST_THREAD_IN_PROC(p) != NULL)
-			FOREACH_THREAD_IN_PROC(p, td) {
-				fill_kinfo_thread(td, &kinfo_proc);
-				error = SYSCTL_OUT(req, (caddr_t)&kinfo_proc,
-					   	sizeof(kinfo_proc));
-				if (error)
-					break;
-			}
-		else
+		MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
+		FOREACH_THREAD_IN_PROC(p, td) {
+			fill_kinfo_thread(td, &kinfo_proc);
 			error = SYSCTL_OUT(req, (caddr_t)&kinfo_proc,
-				   	sizeof(kinfo_proc));
+			    sizeof(kinfo_proc));
+			if (error)
+				break;
+		}
 		mtx_unlock_spin(&sched_lock);
 	}
 	PROC_UNLOCK(p);



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