13 Replies - 11086 Views - Last Post: 10 September 2009 - 09:43 AM Rate Topic: -----

#1 viveks89  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 06-January 09

CPU usage of process using C in linux

Post icon  Posted 09 September 2009 - 09:44 AM

Hi people... I want to find the percentage CPU usage for a process(when pid is given) in linux...
I could get a few details about times like
stime and utime from the /proc/<pid>/stat file...
But i wana get the % CPU usage ...
Plz specify if any more data is needed and calculations used with this data to find the % CPU usage
Is This A Good Question/Topic? 0
  • +

Replies To: CPU usage of process using C in linux

#2 Guest_Neumann*


Reputation:

Re: CPU usage of process using C in linux

Posted 09 September 2009 - 10:05 AM

"ps" provides %CPU usage for processes, so you can use that. For example:

ps -o"%C" -p #PID

Little explanation,

-o"%C" shows only the %CPU column
-p #PID only shows the processes with a specified #PID number, you can give it a list of PIDs.

So, to show a %CPU usage for processes 1, 3, and 17 you would do (just make sure they exist):

ps -o"%C" -p 1 3 17

This post has been edited by Neumann: 09 September 2009 - 10:06 AM

Was This Post Helpful? 0

#3 viveks89  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 06-January 09

Re: CPU usage of process using C in linux

Posted 09 September 2009 - 10:06 AM

Ya... But i want to get it from a C program.. I dont wana use shell command....

I'm actually doing a project in C for monitoring an application....
I wana add this feature to it ...
So kindly suggest me only solutins using C ....

This post has been edited by viveks89: 09 September 2009 - 10:09 AM

Was This Post Helpful? 0
  • +
  • -

#4 Guest_Neumann*


Reputation:

Re: CPU usage of process using C in linux

Posted 09 September 2009 - 10:28 AM

You can still use shell from the C. Use a system() call to execute any commands. Here's a stripped down version of my previous command that only returns the number - ps -opcpu= -p #PID

If you want to write everything yourself then I can only suggest using /proc to record the time CPU has spent on the process over some time interval. Divide that by the actual time and you'll have your %CPU utilization.

This post has been edited by Neumann: 09 September 2009 - 10:31 AM

Was This Post Helpful? 0

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5643
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: CPU usage of process using C in linux

Posted 09 September 2009 - 10:29 AM

View Postviveks89, on 9 Sep, 2009 - 11:06 AM, said:

So kindly suggest me only solutins using C ....


It's a reasonable response.

Look at the ps command source and see how they do it: http://procps.sourceforge.net/

It open source; that means you should be able to find a ton of projects that already do what you're trying to do. And are willing to show you how they did it.
Was This Post Helpful? 0
  • +
  • -

#6 viveks89  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 06-January 09

Re: CPU usage of process using C in linux

Posted 09 September 2009 - 11:06 AM

View PostNeumann, on 9 Sep, 2009 - 09:28 AM, said:

You can still use shell from the C. Use a system() call to execute any commands. Here's a stripped down version of my previous command that only returns the number - ps -opcpu= -p #PID

If you want to write everything yourself then I can only suggest using /proc to record the time CPU has spent on the process over some time interval. Divide that by the actual time and you'll have your %CPU utilization.



do u mean (stime + utime)/total time????
Was This Post Helpful? 0
  • +
  • -

#7 Guest_Neumann*


Reputation:

Re: CPU usage of process using C in linux

Posted 09 September 2009 - 12:10 PM

Yes, that's what I was talking about. Consult man 5 proc for more information on all the fields in stat. Still though, it would be a lot easier to just use ps. After all, it uses stat itself.
Was This Post Helpful? 0

#8 viveks89  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 06-January 09

Re: CPU usage of process using C in linux

Posted 10 September 2009 - 05:48 AM

View PostNeumann, on 9 Sep, 2009 - 11:10 AM, said:

Yes, that's what I was talking about. Consult man 5 proc for more information on all the fields in stat. Still though, it would be a lot easier to just use ps. After all, it uses stat itself.

Ya i could have used system... But internally system forks the process and so it consumes lot of resources...
Thts the reason im going with procfs...
Does the below equation work for multi core processors???
%CPU usage of process = (stime + utime)/totaltime*100
Was This Post Helpful? 0
  • +
  • -

#9 Guest_Neumann*


Reputation:

Re: CPU usage of process using C in linux

Posted 10 September 2009 - 08:20 AM

View Postviveks89, on 10 Sep, 2009 - 04:48 AM, said:

Does the below equation work for multi core processors???
%CPU usage of process = (stime + utime)/totaltime*100


Don't quote me on this, but I'm pretty sure that the kernel takes into account multiple cores when writing out stime and utime. Otherwise you would have multiple stime/utime entries for each core. Your equation seems correct to me.
Was This Post Helpful? 1

#10 viveks89  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 06-January 09

Re: CPU usage of process using C in linux

Posted 10 September 2009 - 09:19 AM

View PostNeumann, on 10 Sep, 2009 - 07:20 AM, said:

View Postviveks89, on 10 Sep, 2009 - 04:48 AM, said:

Does the below equation work for multi core processors???
%CPU usage of process = (stime + utime)/totaltime*100


Don't quote me on this, but I'm pretty sure that the kernel takes into account multiple cores when writing out stime and utime. Otherwise you would have multiple stime/utime entries for each core. Your equation seems correct to me.


K thanx a tonn... And 1 have 1 more question... Does it give the CPU usage at that instant... Or gives the average usage???
Was This Post Helpful? 0
  • +
  • -

#11 Guest_Neumann*


Reputation:

Re: CPU usage of process using C in linux

Posted 10 September 2009 - 09:31 AM

It gives the average CPU usage over the time period of your recordings. So if you recorded the stime/utime for 100 milliseconds, then it would give the average over the 100 millisecond period.
Was This Post Helpful? 0

#12 viveks89  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 06-January 09

Re: CPU usage of process using C in linux

Posted 10 September 2009 - 09:35 AM

View PostNeumann, on 10 Sep, 2009 - 08:31 AM, said:

It gives the average CPU usage over the time period of your recordings. So if you recorded the stime/utime for 100 milliseconds, then it would give the average over the 100 millisecond period.


So stime/utime is more efficient for getting the utility of process at that average time ... got it
Your post was really helpful...

This post has been edited by viveks89: 10 September 2009 - 09:38 AM

Was This Post Helpful? 0
  • +
  • -

#13 Guest_Neumann*


Reputation:

Re: CPU usage of process using C in linux

Posted 10 September 2009 - 09:39 AM

More efficient than what? Not sure I follow you, but taking a stime/utime average over a very short period would be a good approximation of the instantaneous usage.
Was This Post Helpful? 0

#14 viveks89  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 06-January 09

Re: CPU usage of process using C in linux

Posted 10 September 2009 - 09:43 AM

View PostNeumann, on 10 Sep, 2009 - 08:39 AM, said:

More efficient than what? Not sure I follow you, but taking a stime/utime average over a very short period would be a good approximation of the instantaneous usage.


Ya by efficient i mean the difference value rather than the absolute value...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1