Linux 每个进程的 CPU 使用率?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8866158/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
cpu usage per process?
提问by Dervin Thunk
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:
如何获取每个进程的 CPU 使用百分比?因此,例如,我想运行我的程序prog
并获取它产生的 CPU 使用率,例如:
prog name cpu0 cpu1 cpu2 cpu3 total
prog 15 20 45 47 127%
Is there any tool for this? Thanks.
有什么工具可以做到这一点吗?谢谢。
采纳答案by Diaa Sami
linux process explorerproject provides this functionality, you can see a graph for the CPU/Memory/IO for each process in the properties dialog.
linux process explorer项目提供了这个功能,你可以在属性对话框中看到每个进程的 CPU/Memory/IO 图表。
回答by Joe
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.
您必须对第三列求和(我认为这不是一种非常简单的方法),因为它实际上向您展示了每个线程。第一列是名称,第二个处理器,第三个,%cpu。
回答by austinmarton
I think that you can make use of the information in /proc/[pid]/stat
and /proc/stat
to estimate this.
我认为您可以利用其中的信息/proc/[pid]/stat
并/proc/stat
对此进行估计。
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.
查看如何从 C 在 Linux 中通过 PID 计算进程的 CPU 使用率的好答案?其中解释了如何计算单个处理器的 CPU 使用率百分比。
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).
您从最后一个数字中的第 6 个/proc/[pid]/stat
是“处理器 %d,上次执行的 CPU 编号”(至少在 Ubuntu 12.04 上)。
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!
要扩展到多个处理器,您可以对一段时间内的 CPU 使用情况进行采样,并(非常粗略地!)估计每个处理器上的时间比例。然后使用这些比例在处理器之间分配 CPU 使用率。根据中的信息,/proc/stat
您还可以对每个处理器的总时间进行采样,然后您就拥有了所需的所有变量!
See http://linux.die.net/man/5/procfor more info about proc.
有关 proc 的更多信息,请参阅http://linux.die.net/man/5/proc。
回答by Luca
Here is a simple python i've made:
这是我制作的一个简单的python:
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