LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-28-2009, 11:31 AM   #1
Aztral
LQ Newbie
 
Registered: Jul 2009
Posts: 18

Rep: Reputation: 0
Percentage of CPU used on a single process


I know that the utime and stime (user and system) jiffies can be obtained in /proc/pid/stat. So to obtain percentage of cpu used on a single process the options are to do

(process utime + stime)/(total jiffies spent over a time interval),

which would give me the percentage of total available CPU that process used), or to do

(utime + stime)/(total jiffies - idle jiffies), which would give me the percentage of actual used CPU over that interval that was used on the specific process.

Is my logic correct here? In the end I would like to come up with something similar to top, but neither value is similar at all.

As far as I can tell top just adds the process utime+stime, subtracts the old utime+stime, then divides by elapsed where elapsed is:

elapsed = timediff.tv_sec * HZ + (timediff.tv_usec * HZ) / 1000000;

I'm not sure what is being done here. elapsed = the number of HZ that have occurred since the last time top was updated? Why would that be used rather than the total jiffies spent (or total - idle)? Guess I'm asking for someone to explain the fallacies in my logic =).

Thanks
 
Old 09-04-2009, 08:38 AM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
Code:
elapsed = timediff.tv_sec * HZ + (timediff.tv_usec * HZ) / 1000000;
elapsed is the number of jiffies that have occurred since the last sampling by top. It is simply calculating:

Code:
elapsed (jiffies) = time (s) * HZ (jiffies/s)
What top is giving is an approximation of the instantaneous CPU usage by the task (as a percentage):

Code:
usage% = 100% * elapsed_utime_stime (jiffies) / elapsed (jiffies)
If you instead use the cumulative utime_stime, you would need to divide by the cumulative elapsed jiffies since the start of the process, and you would be measuring the average CPU usage of that process. This is less useful, since other processes may have been running on and off during that time, and so the average will not be consistent. Instead, top reports the (more useful) total CPU time that the process has used:

Code:
cumulative (s) = cumulative_utime_stime (jiffies) / HZ (jiffies/s)

Last edited by neonsignal; 09-04-2009 at 08:39 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Percentage of CPU utilization using SNMP Coallanz Linux - Newbie 1 04-19-2009 07:50 PM
CPU utolization in percentage for each process nsyed4 Linux - Newbie 1 12-04-2007 05:51 PM
getting the cpu usage of a single process kits123 Linux - Newbie 2 07-25-2007 10:50 PM
`top` Irregularities - cpu count, percentage, process lists ColinAnderson Linux - Software 4 05-09-2007 06:41 PM
ps -aux CPU usage percentage depdiver Linux - General 1 02-25-2006 02:02 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration