Summarize CPU load for a full process tree in htop or other tool

Solution 1:

If you need a tool that does this, and frequently updates the info as htop, there might be no answer. But if you are ok with executing a command and getting the result, as a snapshot, there are a few options.

One way is by processing the output of "the basic command" ps

$ ps -eo user,pid,ppid,pcpu,command

There are two ways to process the information.

The simple method

grep all lines with a given command, and sum pcpu usage. E.g., for firefox with this simple version (you don't need pid,ppid)

$ ps -eo pcpu,command --sort -pcpu | grep firefox | awk '{p=$1 ; sum +=p} END {print sum}'

This method could give a result that might be off the actual target, due to:

  1. Two separate processes with the same name, overestimating the actual result. Sometimes you may know if you are in one such case, and if so this might not be an issue.
  2. Child processes not grepped (I am not certain this could ever happen), underestimating the actual result.

The method below takes care of both potential issues.

The complete method

Navigate the process tree, starting with the PID (process ID, say, 10000) of your target process of interest. You would then grep all other processes which have PPID (parent process ID) 10000, and record their PIDs. Recursively obtain the whole tree, and add all values of pcpu.

You would have to write a small program for this. I am not certain some items included in this sum would not be included in the simple way above.

Notes on "the basic command":

  1. It is important to list command (or cmd, args). Column comm lists the process name, which may be different (see this). For instance, firefox launches many Web Content processes, and this teases the excellent tool atop + P, e.g.
  2. For other interesting analyses you could add ,size,%mem as columns to show and process. See also below.

The memory-side of resources usage

There is an extensive "discussion" here, which applies to memory usage. I am not sure if any of the claims there apply in any similar way (even considering the differences cpu usage vs. memory usage) here.

Related:

  1. https://unix.stackexchange.com/questions/209689/sum-the-memory-usages-of-all-the-processes-of-a-program
  2. https://unix.stackexchange.com/questions/55148/top-htop-group-process-by-command/471925#471925
  3. https://www.networkworld.com/article/3516319/showing-memory-usage-in-linux-by-process-and-user.html