cpu usage per process?

13,193

Solution 1

linux process explorer project provides this functionality, you can see a graph for the CPU/Memory/IO for each process in the properties dialog.

enter image description here

Solution 2

I think that you can make use of the information in /proc/[pid]/stat and /proc/stat to estimate this.

Check out the great answers to How to calculate the CPU usage of a process by PID in Linux from C? which explain how to calculate CPU usage % for a single processor.

The 6th from last number you get from /proc/[pid]/stat is "processor %d, CPU number last executed on" (on Ubuntu 12.04 at least).

To extend to multiple processors, you could sample the CPU usage over a period and (very roughly!) estimate the proportion of time on each processor. Then use these proportions to split the CPU usage between the processors. Based on the info in /proc/stat you can also sample the total time for each processor and then you have all the variables you need!

See http://linux.die.net/man/5/proc for more info about proc.

Solution 3

For firefox:

while [ 1 ]; do ps --no-heading -C firefox -L -o command,psr,pcpu|sort -k 2 -n; echo; sleep 1; done

You'd have to sum the third column (which I see no ridiculously easy way to do) because it's actually showing you every thread. First column is name, second processor, third, %cpu.

Share:
13,193
Dervin Thunk
Author by

Dervin Thunk

Updated on June 04, 2022

Comments

  • Dervin Thunk
    Dervin Thunk almost 2 years

    How can I grab the percentage of cpu usage on a per process basis? So, for example, I'd like to run my program prog and get the cpu usage it incurred in, for example:

      prog name cpu0 cpu1 cpu2 cpu3  total
      prog        15   20   45   47   127%
    

    Is there any tool for this? Thanks.

  • Jesus Ramos
    Jesus Ramos over 12 years
    awk can grab you the third column with $3 I believe, I forget the syntax for it :\