Subject: Re: bin/34271 (ps doesn't sort correctly)
To: Zafer Aydogan <zafer@aydogan.de>
From: Alan Barrett <apb@netbsd.org>
List: netbsd-bugs
Date: 10/01/2006 22:52:47
On Sun, 01 Oct 2006, Zafer Aydogan wrote:
> It's odd. If you separate the the options with an hyphen (-m -r), then
> only the first argument is used.
> 
> # ps -r -m -O %cpu,vsz
>  PID %CPU   VSZ TTY   STAT    TIME COMMAND
> 20182 66.6   224 ttyp1 R+   7:11.19 top
>  206  0.9 24596 ttyp0 Sa+  2:59.69 /usr/pkg/lib/firefox/firefox-bin
>  797  0.0  1332 ttyE6 S    0:05.22 icewm
> 18387  0.0   240 ttyp1 I    0:00.02 ksh
> [...]

No, both arguments are used, in the order you specified.  It first sorts
by %cpu ("-r"), then if multiple lines have teh same %cpu value, it
sorts by vsz ("-m").  This is exactly what I would expect.

> as you can see, this is sorted by -r (%cpu) and the second argument -m
> is ignored.

No, the -m is not ignored; it specifies a secondary sort key for when
multiple processes have the same %cpu.

> if I enter:
> 
> # ps -m -r -O %cpu,vsz
>  PID %CPU   VSZ TTY   STAT    TIME COMMAND
>  206  5.3 24596 ttyp0 Sa+  3:08.42 /usr/pkg/lib/firefox/firefox-bin
>  797  0.0  1332 ttyE6 S    0:05.34 icewm
> 18387  0.0   240 ttyp1 I    0:00.02 ksh
>  543  0.0   228 ttyp0 IWs  0:00.02 -sh (csh)
> 20182 59.8   224 ttyp1 R+   8:02.65 top
> 18093  0.0   220 ttyp4 Ss   0:00.02 -sh (csh)
> [...]
>
> then again the second argument -r (%cpu) is ignored and it is sorted
> my -m (vsz).

No, the second argument is not ignored.  "-m -r" means "first sort by
vsz, and if the vsz values are the same then sort by %cpu".  As far as I
can tell, that's exactly what happened.

> This is wrong. -r and -m are mutually exclusive.

Are you saying that you want "-m -r" to behave just like "-r", ignoring
the "-m"?  And similarly do you want "-r -m" to behave like "-m",
ignoring the "-r"?  Why do you expect that behaviour?

> but, if you use -mr or -rm (written together), then -m overrides -r (see 
> PR).
> 
> # ps -mr -O %cpu,vsz
>  PID %CPU   VSZ TTY   STAT     TIME COMMAND
>  206  2.5 24904 ttyp0 Sa+   4:07.61 /usr/pkg/lib/firefox/firefox-bin
>  797  0.0  1332 ttyE6 S     0:06.45 icewm
> 18387  0.0   240 ttyp1 I     0:00.02 ksh
>  543  0.0   228 ttyp0 IWs   0:00.02 -sh (csh)
> 20182 63.4   224 ttyp1 R+   17:02.07 top
> [...]

That looks fine to me.  "-mr" is treated just like "-m -r", which in turn
means the same as "-k vsz -k %cpu" or "-k vsz,%cpu", which means "primary
sort key is vsz; secondary sort key is %cpu".

> as you see, the last argument is r, but the output is sorted by m
> (vsz).  This is wrong, aslong m and r are mutually exclusive, the last
> argument wins.

No, they are both applied, in the order specified, with the first one
being the primary sort key.

--apb (Alan Barrett)