Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

How is CPU usage calculated on a Linux system with a single core?

Cpu(s): 28.1%us,  6.5%sy,  0.0%ni, 43.0%id, 21.6%wa,  0.0%hi,  0.5%si,  0.2%st

Please explain me how the us, sy, ni, id, wa, hi, si and st values are obtained.

share|improve this question
    
How they are obtained or what they signify? The former is detailled in the code. –  symcbean Jun 12 '12 at 14:01
    
@symcbean Is there any documentation which I can refer rather than jumping into the code? –  Supratik Jun 12 '12 at 14:43
1  
It's in the source, Luke! –  pfo Jun 12 '12 at 14:53

3 Answers 3

CPU usage is calculated and tracked by the kernel. The exact methods and processes involved in this are far beyond the scope of this site (ServerFault).

If you're wondering what the values represent, I pulled this nice list from IBM's site, though the full article is a good read:

  • us - Percentage of CPU time spent in user space.
  • sy - Percentage of CPU time spent in kernel space.
  • ni - Percentage of CPU time spent on low priority processes.
  • id - Percentage of CPU time spent idle.
  • wa - Percentage of CPU time spent in wait (on disk).
  • hi - Percentage of CPU time spent handling hardware interrupts.
  • si - Percentage of CPU time spent handling software interrupts.

(from IBM... why it's only embedded in a Java SDK guide is beyond me)

top gets its information from the /proc directory, which is a special directory used to query the kernel for, among other things, process statistics. man proc can give you more information on how /proc is set up.

share|improve this answer
    
Can you please point me to some resource which explains how the values are obtained ? I already know what these values represent. –  Supratik Jun 12 '12 at 14:22
    
@Supratik If you're asking how top itself gets its values, then I edited the question to give you more info. –  Hyppy Jun 12 '12 at 14:26
    
Yes I understand it reads all the values from /proc, but what is the logic behind all the calculations. –  Supratik Jun 12 '12 at 14:41
1  
The kernel tracks how many "time slices" the scheduler allocates to each type of process, and subtracts out any time spent in ISRs or similar overhead. This is only mostly accurate as there are context switch penalties and pipeline flushes that cause additional overhead, which gets lumped in with the processes. –  Chris S Jun 12 '12 at 14:57

/proc/[pid]/stat results are produced by this code in the Linux kernel.

http://www.cs.fsu.edu/~baker/devices/lxr/http/source/linux/fs/proc/stat.c

You can click around and get more info from in there.

share|improve this answer

This article describes this clearly.

http://www.linuxjournal.com/article/9001

share|improve this answer
1  
You should add some details to your answer, highlights from the article for example. Links break over time and if/when this one does this answer won't be of any use. –  squillman Oct 11 '13 at 13:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.