50

I'd like to know how much resource a specific command is using.

top and htop displays information on per process basis but I'd like the information to be shown on per command basis. E.g. I'd like to know how much RAM chrome is using.

brillout
  • 595
  • 1
  • 4
  • 6
  • 5
    Utilities such as `top` and `ps` vary by OS. You should tag the question with the OS you are using. – jordanm Nov 07 '12 at 22:17
  • No, but it may be coming: [#301](https://github.com/htop-dev/htop/issues/301), [#920](https://github.com/htop-dev/htop/issues/920), – Raphael Feb 04 '22 at 20:57

3 Answers3

32

Pressing H in htop group the processes by the main thread (command), actually it toggles the threads visibility.

BG Adrian
  • 445
  • 4
  • 3
  • 3
    It does hide the threads, although there can still be a tree of processes. Using tree view and collapsing the trees with `F6` works to show only the process group, but sorting disables tree view. – Codebling Oct 22 '19 at 00:17
  • 1
    I realised that in tree view, even with tree branches collapsed, it still doesn't show the totals, it's just per-process. There are columns that are supposed to list totals for children, but those don't seem to work, either. `htop` will not show totals no matter what you do. – Codebling Oct 23 '19 at 00:02
  • 1
    Partially useful, but doesn't seem to really hide all the different entries when you scroll down. – Caleb Stanford Mar 02 '21 at 18:29
  • 1
    This doesn't seem to provide cumulative resources usage. – sancho.s ReinstateMonicaCellio Sep 16 '21 at 11:01
18

This is possible in atop. Just press p when running it. From the help:

Accumulated figures:
        'u'  - total resource consumption per user
        'p'  - total resource consumption per program (i.e. same process name)
        'j'  - total resource consumption per container
Nemo
  • 522
  • 1
  • 11
  • 23
  • Only thing stopping me from using `atop` is the cron dependency, but there's an open [PR](https://github.com/Atoptool/atop/pull/55) that will hopefully fix that – Codebling Oct 22 '19 at 00:18
  • 1
    Update: the PR was accepted, and a new `atop` version will be released with no `cron` dependency. – Codebling Oct 31 '19 at 04:27
  • This might not give the correct result for many commands. For instance, `firefox` launches many `Web Content` processes (https://superuser.com/questions/1299746/whats-web-content-in-top-doing-taking-up-memory), and these would show separately. There is no a priori way to tell which to sum. – sancho.s ReinstateMonicaCellio Sep 16 '21 at 11:02
  • @sancho.sReinstateMonicaCellio that's true. You also won't see the resource usage of child processes launched by a `xargs` command, for that matter. Getting statistics about all child processes is somewhat tedious, although possible by following the breadcrumbs in [procfs](https://www.kernel.org/doc/html/latest/filesystems/proc.html). – Nemo Sep 16 '21 at 12:53
  • So there are at least two sets of processes that are not counted in the total sum. If I get it right, neither the top voted answer or this one really provide the result sought by the OP, they would actually provide a figure closer to the actual target than a "simple look". See [this](https://askubuntu.com/a/1364106/226614). – sancho.s ReinstateMonicaCellio Sep 16 '21 at 13:51
12

You could run top in batch mode -b with 1 iteration -n1. You grep it, pipe it to awk, SUM the result and print it.

top -b -n1 | grep chrome | awk '{ SUM += $9} END { print SUM }'

I don't know which column you want to output. Change $9 to fit your needs.

ThomasFrederiksen
  • 1,531
  • 1
  • 8
  • 4
  • What about converting to human readable format? – nurgasemetey Mar 05 '21 at 13:34
  • This might not give the correct result for many commands. For instance, `firefox` launches many `Web Content` processes, that would not enter the sum (https://superuser.com/questions/1299746/whats-web-content-in-top-doing-taking-up-memory). `ps -eo ,command` instead of `top -b -n1` could fix this problem. See [this](https://askubuntu.com/a/1364106/226614). – sancho.s ReinstateMonicaCellio Sep 16 '21 at 13:54