Page MenuHomeFreeBSD

ps(1): extend the non-standard option -d (tree view) to work with -p
ClosedPublic

Authored by pstef on Apr 12 2020, 2:50 PM.

Details

Summary

Initially it seemed that there were multiple possible ways to do it.

Processing option -p could conditionally add selected processes and
their descendants to the list for further work, but it is not guaranteed
to know whether the -d option has been used or not, and it also doesn't
have access to the process list just yet.

There is also descendant_sort() which has access to all possibly needed
information, but serves the purely post-processing purpose of sorting
output.

Then there is the loop that uses invocation information and full process
list to create a list of processes for final display. It seems the most
natural place to implement this, but indeterminate state of the process
list and volatility of the final list that is being created obstruct
adding an elegant search for all elements of process descendancy trees.

So I opted for adding another loop, just before the one I mentioned
above. For all selected processes it conditionally adds direct
descendants to the end of this list of selected processes.

Diff Detail

Repository
rS FreeBSD src repository - subversion
Lint
Automatic diff as part of commit; lint not applicable.
Unit
Automatic diff as part of commit; unit tests not applicable.

Event Timeline

This comment was removed by pstef.

Possible use cases:

  • ps auxd -p $$
  • ps auxd -p 1
  • while x=$(pgrep svnlite); do clear; ps auxd -p $x; sleep 2; done
  • ps auxd -p `pgrep make`

output for the last one looks like this:

USER   PID %CPU %MEM   VSZ   RSS TT  STAT STARTED    TIME COMMAND
root 47503  0.0  0.0 10368  1308  5  S+   20:31   0:00.01 make -sj4 buildworld installworld WORLDFAST=1 WITHOUT_CLANG=1
root 47533  0.0  0.0 11792  3128  5  S    20:31   0:00.00 - sh -e
root 47534  0.2  0.0 11016  2252  5  S    20:31   0:00.04 `-- make -m /usr/src/share/mk -f Makefile.inc1 TARGET=amd64 TARGET_ARCH=amd64 buildworld
root 47593  0.4  0.0 11844  3140  5  S    20:31   0:00.00   `-- sh -e
root 47594  0.6  0.0 11020  2248  5  S    20:31   0:00.04     `-- make -f Makefile.inc1 BWPHASE=libraries DESTDIR=/usr/obj/usr/src/amd64.amd64/tmp -DNO_FSCHG MK_HTML=no -DNO_LINT MK_MAN=no MK_PROFI
root 47616  0.6  0.0 11880  3152  5  S    20:31   0:00.00       `-- sh -e
root 47642  1.1  0.0 10948  2244  5  S    20:31   0:00.04         `-- make -f Makefile.inc1 _startup_libs
root 47650  1.1  0.0 11880  3152  5  S    20:31   0:00.00           `-- sh -e
root 47653  3.4  0.1 19132 10680  5  D    20:31   0:00.26             `-- make MK_TESTS=no DIRPRFX=lib/libc/ all
kaktus added a subscriber: kaktus.

Nice one. LGTM.

This revision is now accepted and ready to land.Apr 14 2020, 9:46 AM
pstef reclaimed this revision.
pstef added reviewers: emaste, kevans.
This revision is now accepted and ready to land.Apr 14 2020, 9:48 AM
pstef requested review of this revision.Apr 14 2020, 9:48 AM

my goodness, phabricator

This comment was removed by kevans.

This doesn't seem to quite do what I expected, but it's incredibly close. This effectively won't work if only -d and -p are specified, because kvm_getprocs is being setup to only return the single process. I think the nselectors == 1 block just above should only be executed if nselectors == 1 && !descendancy so that we get the full list for you to filter through with the new loop.

I missed that -p with single argument prevents -d from working, since I always use -a :)

I agree with the approach kevans suggested, but maybe it would be enough to change the if statement just for the pidlist case?

This revision is now accepted and ready to land.May 6 2020, 1:41 AM