Subject: Re: field widths in ps(1).
To: None <>
From: Simon Burge <>
List: tech-userlevel
Date: 06/02/2000 11:44:21
Greg A. Woods wrote:

> [ On Thursday, June 1, 2000 at 10:55:52 (+1000), Simon Burge wrote: ]
> > Subject: Re: field widths in ps(1). 
> >
> > In summary, the kernel data is only fetched once, but all data is
> > effectively formatted twice.
> That's cool.  (I'm a bit surprised at your timing numbers though --
> perhaps some optimisations can be made in the first pass when computing
> the column widths.)

First off, here's some serious timing numbers on my 128MB PPro166.
Here's the minimum and maximum times for 1000 "ps aux > /dev/null"s on
an otherwise idle machine (from a sample of 13 runs), where "old" this
the -current ps binary and "new" in my column-sizing ps binary:

	old: 0.091u 1.457s 0:20.40  7.5%        0+0k 0+11io 0pf+0w
	old: 0.145u 1.986s 0:21.82  9.7%        0+0k 0+17io 0pf+0w

	new: 7.119u 3.270s 0:30.43 34.1%        0+0k 0+17io 0pf+0w
	new: 6.664u 3.577s 0:31.40 32.5%        0+0k 0+22io 0pf+0w

and the same for 100 "ps aux > /dev/null"s on the same machine with 30
copies of the C program "main() { while (1) ; }" running (sample of 9

	old: 5.233u 5.439s 3:21.12 5.3% 0+0k 0+125io 0pf+0w
	old: 3.103u 7.500s 3:34.02 4.9% 0+0k 0+111io 0pf+0w

	new: 7.001u 3.632s 3:22.03 5.2% 0+0k 0+106io 0pf+0w
	new: 6.091u 4.463s 3:28.83 5.0% 0+0k 0+122io 0pf+0w

At this stage I'd guess that when the machine is under very heavy load,
the exec overhead is probably greater than any extra processing my
changes to ps does.  When the 30 cpu burners were running, even typing
at the shell was painful...

As to optimising the width calculations, currently it does for doubles
and ints something like:

	if (mode == WIDTHMODE) {
		asprintf(&buf, "%.*f", prec, val);
		if (buf == NULL)
			err(1, "%s", "");
		width = max(strlen(buf, width));
	} else {
		printf("%*.*f", v->width, prec, val);

so there are lots of calls to printf, malloc and free (and also some
function call overhead - the above is a little simplified) when I could
probably just use log10() to get the int and double widths with some
smart math.  Oh, ick - I don't even call free() - that's perhaps bad :(

I'll look into this more...