3

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.

6
  • Is top sufficient for your purposes? Commented Jan 14, 2012 at 23:16
  • Oli, hi. Problem with top is that it is too "dynamic" if you will, for my purposes. I need something like the linux time command, reporting cpu usage about that process only. Commented Jan 14, 2012 at 23:18
  • Ok, that kind of sounds like you want to know what %age of all cycles used by your process were run on CPU0, CPU1, etc.? In which case, what does 127% mean in your example above? Commented Jan 14, 2012 at 23:19
  • Ah, that's just the aggregate over all cpus. So for 4 cores (the example above), the process used an aggregate of 127% over 400% max. Does that make sense? Commented Jan 14, 2012 at 23:22
  • So are you saying that 400% would equate to all 4 CPUs fully utilised by your app measured across its total runtime? Commented Jan 14, 2012 at 23:25

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

awk can grab you the third column with $3 I believe, I forget the syntax for it :\
0

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

Comments

0

Here is a simple python i've made:

import re,time,sys cpuNum=0 if len(sys.argv)==1: print "use pidcpu <pid1,pid2,..,pidn>" sys.exit(0) pids=sys.argv.pop() def getCpuTot(): global cpuNum f=open("/proc/stat","r") ln=f.read() f.close() #cpu 858286704 148088 54216880 117129864 2806189 5046 16997674 0 0 0 r=re.findall("cpu[\d\s]{1}\s+(\d+)\s(\d+)\s(\d+)\s(\d+)\s.*?",ln,re.DOTALL) cpuNum=len(r)-1 return int(r[0][0])+int(r[0][1])+int(r[0][2])+int(r[0][3]) def getPidCPU(pid): f=open("/proc/"+ str(pid) +"/stat","r") ln=f.readline() f.close() a=ln.split(" ") return int(a[13])+int(a[14]) cpu1=getCpuTot() cpupid1=[] for pid in pids.split(","): cpupid1.append(getPidCPU(pid)) time.sleep(1) cpu2=getCpuTot() cpupid2=[] for pid in pids.split(","): cpupid2.append(getPidCPU(pid)) i=0 for pid in pids.split(","): perc=int(cpuNum*(cpupid2[i]-cpupid1[i])*100/float(cpu2-cpu1)) i+=1 print pid,perc 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.