Where does top command get all the data for CPU Utilization?

When I run the top command, the third line is;

Cpu(s): 0.3%us, 0.3%sy, 0.0%ni, 99.2%id, 0.1%wa, 0.0%hi, 0.0%si, 0.1%st

From where does top command get this data? Does it get from /proc?, if yes, what is the exact location?


Solution 1:

You are questioning for the exact location of the CPU usage. This is /proc/stat:

$ head -n 3 /proc/stat
cpu  1751981 185577 398478 28868975 69445 32 27028 0 0 0
cpu0 954878 88888 186567 14433502 19750 0 600 0 0 0
cpu1 797103 96688 211911 14435473 49694 31 26428 0 0 0

The format is explained in the Kernel Documentation (filesystems/proc.txt); I bolded the topabbreviations:

The very first "cpu" line aggregates the numbers in all of the other "cpuN" lines. These numbers identify the amount of time the CPU has spent performing different kinds of work. Time units are in USER_HZ (typically hundredths of a second). The meanings of the columns are as follows, from left to right:

  • user: normal processes executing in user mode
  • nice: niced processes executing in user mode
  • system: processes executing in kernel mode
  • idle: twiddling thumbs
  • iowait: waiting for I/O to complete
  • irq: servicing interrupts [hard interrupts hi]
  • softirq: servicing softirqs [soft interrupts si]
  • steal: involuntary wait
  • guest: running a normal guest
  • guest_nice: running a niced guest

General informations about your CPU you can get from /proc/cpuinfo, but this is not related to the CPU usage.