Operating System - Linux
1752793 Members
6015 Online
108789 Solutions
New Discussion юеВ

Re: How much CPU time take a process?

 
SOLVED
Go to solution
Vitaly Karasik_1
Honored Contributor

How much CPU time take a process?

I'd like to monitor CPU consumption of specific process.
I use "ps -eo pcpu,pid,user,args " - and get 0.5% CPU usage for my process.

But in the same time "top" shows that the same process takes ~10% CPU!

Can someone point me so same explanation of this phenomen?
7 REPLIES 7
Ivan Ferreira
Honored Contributor

Re: How much CPU time take a process?

Your best option to monitor the cpu time for a process is using the time command. You can then use this information to calculate the %CPU.

The cpu percentage is computed (usr+sys)/elapsed.
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
skt_skt
Honored Contributor

Re: How much CPU time take a process?




# time -p ls -lrt /var/crash

real 24.65
user 0.16
sys 0.43
Vitaly Karasik_1
Honored Contributor

Re: How much CPU time take a process?

thank you!
But I need to build a metric for Ganlgia for monitoring some long-running processes, so "time" cannot help me.
Huc_1
Honored Contributor
Solution

Re: How much CPU time take a process?

maybe this will get you on the right track!

http://linux.derkeiler.com/Mailing-Lists/SuSE/2005-08/2084.html

Jean-Pierre Huc
Smile I will feel the difference
larstr
Advisor

Re: How much CPU time take a process?

Using linux on the x86 architecture using tools that uses the proc nodes to determine the cpu usage will always give you an estimate on what the cpu usage is, and not an accurate answer. Sometimes this estimate will give you a good hint of the usage, and sometimes it will be bad. Certain workloads will give you worse results than others.

Most tools today (top, ps) are using the proc nodes for this. This is also similar on windows using for example task manager.

This issue was discussed in the latest Linux Journal.

The case you're mentioning here with the difference between top and ps (both uses the same data for showing you the load) is because both these programs need to take two samples while your app is running to calculate the cpu usage. As long as these samples were not the exact same samples (time+duration), your results will differ.

Hope this clears things up a bit.

Lars
Vitaly Karasik_1
Honored Contributor

Re: How much CPU time take a process?

Thank you! I'm happy to submit your first points!

>Using linux on the x86 architecture using >tools that uses the proc nodes to >determine the cpu usage will always give >you an estimate on what the cpu usage is, >
>and not an accurate answer.

I understand this, but we cannot get more accurate results without kernel hacks.

> As long as these samples were not the >exact same samples (time+duration), your >results will differ.

Make sense! But probably top & ps use different algorithms too?
Of course, I cannot say that my comparison is 100% accurate, but my Ganglia graphs "ps -opcpu" output ( once a minute) and in the same time I running "top" on my terminal - the difference is *5 .
larstr
Advisor

Re: How much CPU time take a process?

You could always calculate this yourself too.

If you do a ps -ax you get a TIME field where is shows how much time of the cpu each process have been running. Now, convert this into seconds, and collect this value every minute, and you'll be able to calculate for each minute.

If you want a higher detail level, you can also grab the actual values from /proc//stat. (100x more precision)

Now that you know how to calculate how many seconds per minute a process is running, I guess it shouldn't be a big deal to convert it into % ;-)

Lars