Calculate Cpu Usage on Linux as Top
Calculate Cpu Usage on Linux as Top
man ps
in NOTES
section.
And, guess you know, but you can also do:
Edit: as to your comment on other answer;
"Hmm yeah i Wonder how to get that (the instant CPU percentage) from ps"
Short answer: you can't.
Why is it so?
It is like asking someone to calculate the speed of a car from a picture.
While top
is a monitoring tool, ps
is a snapshot tool. Think of it like this: At any given moment a process either uses the CPU or not. Thus you have either 0% or 100% load in that exact moment.
Giving: If ps
should give instant CPU usage it would be either 0% or 100%.
top
on the other hand keep polling numbers and calculate load over time.
ps
could have given current usage – but that would require it to read data multiple times and sleep between each read. It doesn't.
Calculation for ps %cpu
ps
calculates CPU usage in the following manner:
So the number printed is: time the process has been using the CPU during it's lifetime. As in the example above. It has done so in 15.3% of its lifetime. In 84,7% of the time it has not been bugging on the CPU.
Data retrieval
ps
, as well as top
, uses data from files stored under /proc/
- or the process information pseudo-file system.
You have some files in root of /proc/
that have various information about the overall state of the system. In addition each process has its own sub folder /proc/<PID>/
where process specific data is stored. So, for example the process from your question had a folder at /proc/3038/
.
When ps
calculates CPU usage it uses two files:
From
uptime
it uses the first value (uptime).From
[PID]/stat
it uses the following:
A jiffie is clock tick. So in addition it uses various methods, ie., sysconf(_SC_CLK_TCK)
, to get system's Hertz (number of ticks per second) - ultimately using 100 as a fall-back after exhausting other options.
So if utime
is 1234 and Hertz is 100 then:
The actual calculation is done by:
Example (Output from a custom Bash script):
Calculating "current" load with ps
This is a (bit?) shady endeavour but OK. Lets have a go.
One could use times provided by ps
and calculate CPU usage from this. When thinking about it it could actually be rather useful, with some limitations.
This could be useful to calculate CPU usage over a longer period. I.e. say you want to monitor the average CPU load of plugin-container
in Firefox while doing some Firefox-related task.
By using output from:
$ ps -p -o cputime,etimes
I use etime
over etimes
in this sample, on calculations, only to be a bit more clear. Also I add %cpu for "fun". In i.e. a bash script one would obviously use etimes
- or better read from /proc/<PID>/
etc.
Process was using the CPU 38% of the time during this period.
Look at the code
If you want to know how ps
does it, and know a little C, do (looks like you run Gnome Debain deriavnt) - nice attitude in the code regarding comments etc.:
Implementation
Preparation
To calculate CPU usage for a specific process you'll need the following:
#1
uptime of the system (seconds)
#14
utime
- CPU time spent in user code, measured in clock ticks#15
stime
- CPU time spent in kernel code, measured in clock ticks#16
cutime
- Waited-for children's CPU time spent in user code (in clock ticks)#17
cstime
- Waited-for children's CPU time spent in kernel code (in clock ticks)#22
starttime
- Time when the process started, measured in clock ticks
Hertz (number of clock ticks per second) of your system.
In most cases,
getconf CLK_TCK
can be used to return the number of clock ticks.The
sysconf(_SC_CLK_TCK)
C function call may also be used to return the hertz value.
Calculation
First we determine the total time spent for the process:
We also have to decide whether we want to include the time from children processes. If we do, then we add those values to total_time
:
Next we get the total elapsed time in seconds since the process started:
Finally we calculate the CPU usage percentage:
Sample Code on Cpp
Last updated