>> main >> Tips & Articles >> Linux Tips & Tricks >> Getting CPU load percentage from software


Getting CPU load percentage from software

CPU load can be seen from the terminal using the top command, however sometimes you only want to see the load or on embedded system you may not have the top command or you may not have the ncurses library needed for the top command.
Sometimes you might also want to measure CPU load grammatically (from your application).
The CPU time statistics are stored in /proc/stat, so you can read this file and calculate the load.
The first line of /proc/stat contains CPU times for all CPU's in the system.
This line contains 4 numbers in units of jiffies (usually 1/100 of second).
The numbers are: user, nice, system, idle
  • user - Time spent in user space (for all processes)
  • nice - Time spent in niced tasks (tasks with positive nice value)
  • system - Time spent in Kernel space
  • idle - Time the processor was not busy with any running process.
Summing up all these times gives you the system uptime in jiffies, this is what the uptime command do.
If we divide the idle time in total time we get the percentage of time the processor was idle.
We can get the load percentage by subtracting this number from 100.

Add comment