5

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.

ChrisF
  • 1,871
  • 1
  • 21
  • 28
Supratik
  • 2,154
  • 10
  • 51
  • 66

3 Answers3

2

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.

Hyppy
  • 15,608
  • 1
  • 38
  • 59
  • 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
1

/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.

Joel K
  • 5,853
  • 2
  • 30
  • 34
-1

This article describes this clearly.

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

Soham Chakraborty
  • 3,584
  • 17
  • 24
  • 2
    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