Index | Thread | Search

From:
"Ted Unangst" <tedu@tedunangst.com>
Subject:
autoadjust ps columns
To:
tech@openbsd.org
Date:
Sat, 26 Apr 2025 12:43:10 -0400

Download raw body.

Thread
I like this more. It leaves the columns narrow unless more space is required.

Do a first pass over the procs to see how big they are.

Index: keyword.c
===================================================================
RCS file: /home/cvs/src/bin/ps/keyword.c,v
diff -u -p -r1.54 keyword.c
--- keyword.c	24 Mar 2025 21:43:40 -0000	1.54
+++ keyword.c	30 Mar 2025 02:11:03 -0000
@@ -152,7 +152,7 @@ VAR var[] = {
 	/* XXX */
 	{"rgroup", "RGROUP", NULL, LJUST, rgname, USERLEN},
 	{"rlink", "RLINK", NULL, 0, pvar, 8, 0, POFF(p_back), UINT64, "llx"},
-	{"rss", "RSS", NULL, 0, p_rssize, 6},
+	{"rss", "RSS", NULL, 0, p_rssize, 5},
 	{"rssize", "", "rsz"},
 	{"rsz", "RSZ", NULL, 0, rssize, 4},
 	{"rtable", "RTABLE", NULL, 0, pvar, 0, 0, POFF(p_rtableid), INT32, "d"},
@@ -187,7 +187,7 @@ VAR var[] = {
 	{"user", "USER", NULL, LJUST, euname, USERLEN},
 	{"usrpri", "", "upr"},
 	{"vsize", "", "vsz"},
-	{"vsz", "VSZ", NULL, 0, vsize, 6},
+	{"vsz", "VSZ", NULL, 0, vsize, 5},
 	{"wchan", "WCHAN", NULL, LJUST, wchan, WCHANLEN},
 	{"xstat", "XSTAT", NULL, 0, pvar, 4, 0, POFF(p_xstat), UINT16, "x"},
 	{""},
Index: ps.c
===================================================================
RCS file: /home/cvs/src/bin/ps/ps.c,v
diff -u -p -r1.81 ps.c
--- ps.c	18 May 2024 13:08:09 -0000	1.81
+++ ps.c	26 Apr 2025 16:34:58 -0000
@@ -65,6 +65,7 @@ int	eval;			/* exit value */
 int	sumrusage;		/* -S */
 int	termwidth;		/* width of screen (0 == infinity) */
 int	totwidth;		/* calculated width of requested variables */
+int pagesize;
 
 int	needcomm, needenv, neednlist, commandonly;
 
@@ -72,7 +73,7 @@ enum sort { DEFAULT, SORTMEM, SORTCPU } 
 
 static char	*kludge_oldps_options(char *);
 static int	 pscomp(const void *, const void *);
-static void	 scanvars(void);
+static void	 scanvars(struct kinfo_proc *kp, size_t nentries);
 static void	 forest_sort(struct pinfo *, int);
 static void	 usage(void);
 
@@ -116,6 +117,7 @@ main(int argc, char *argv[])
 		termwidth = ws.ws_col - 1;
 	if (termwidth == 0)
 		termwidth = 79;
+	pagesize = getpagesize();
 
 	if (argc > 1)
 		argv[1] = kludge_oldps_options(argv[1]);
@@ -325,12 +327,6 @@ main(int argc, char *argv[])
 		Uflag = 1;
 	}
 
-	/*
-	 * scan requested variables, noting what structures are needed,
-	 * and adjusting header widths as appropriate.
-	 */
-	scanvars();
-
 	if (neednlist && !nlistread)
 		(void) donlist();
 
@@ -364,6 +360,12 @@ main(int argc, char *argv[])
 		errx(1, "%s", kvm_geterr(kd));
 
 	/*
+	 * scan requested variables, noting what structures are needed,
+	 * and adjusting header widths as appropriate.
+	 */
+	scanvars(kp, nentries);
+
+	/*
 	 * print header
 	 */
 	printheader();
@@ -404,14 +406,31 @@ main(int argc, char *argv[])
 }
 
 static void
-scanvars(void)
+scanvars(struct kinfo_proc *kp, size_t nentries)
 {
 	struct varent *vent;
 	VAR *v;
 	int i;
+	int vszbump = 0, rssbump = 0;
 
+#define pgtok(a)    (((unsigned long long)(a)*pagesize)/1024)
+	for (i = 0; i < nentries; i++) {
+		struct kinfo_proc *ki = &kp[i];
+		if (vszbump == 0 && pgtok(ki->p_vm_dsize + ki->p_vm_ssize + ki->p_vm_tsize) >= 100000)
+			vszbump = 1;
+		if (vszbump == 1 && pgtok(ki->p_vm_dsize + ki->p_vm_ssize + ki->p_vm_tsize) >= 1000000)
+			vszbump = 2;
+		if (rssbump == 0 && pgtok(ki->p_vm_rssize) >= 100000)
+			rssbump = 1;
+		if (rssbump == 1 && pgtok(ki->p_vm_rssize) >= 1000000)
+			rssbump = 2;
+	}
 	for (vent = vhead; vent; vent = vent->next) {
 		v = vent->var;
+		if (strcmp(v->name, "vsz") == 0)
+			v->width += vszbump;
+		if (strcmp(v->name, "rss") == 0)
+			v->width += rssbump;
 		i = strlen(v->header);
 		if (v->width < i)
 			v->width = i;