CPU usage is currently expressed as the percentage of time spent running
during the entire lifetime of a process. This is not ideal, and it does not
conform to the standards that ps otherwise conforms to. CPU usage is
unlikely to add up to exactly 100%.
And, guess you know, but you can also do:
top -p <PID>
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:
uptime = total time system has been running.
ps_time = process start time measured in seconds from boot.
pu_time = total time process has been using the CPU.
;; Seconds process has been running:
seconds = uptime - ps_time
;; Usage:
cpu_usage = pu_time * 1000 / seconds
print: cpu_usage / 10 "." cpu_usage % 10
Example:
uptime = 344,545
ps_time = 322,462
pu_time = 3,383
seconds = 344,545 - 322,462 = 22,083
cpu_usage = 3,383 * 1,000 / 22,083 = 153
print: 153 / 10 "." 153 % 10 => 15.3
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
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:
/proc/uptime The uptime of the system (seconds), and the amount of time spent in idle process (seconds).
/proc/[PID]/stat Status information about the process.
From uptime it uses the first value (uptime).
From [PID]/stat it uses the following:
# Name Description
14 utime CPU time spent in user code, measured in jiffies
15 stime CPU time spent in kernel code, measured in jiffies
16 cutime CPU time spent in user code, including time from children
17 cstime CPU time spent in kernel code, including time from children
22 starttime Time when the process started, measured in jiffies
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.
$ ./psw2 30894
System information
uptime : 353,512 seconds
idle : 0
Process information
PID : 30894
filename : plugin-containe
utime : 421,951 jiffies 4,219 seconds
stime : 63,334 jiffies 633 seconds
cutime : 0 jiffies 0 seconds
cstime : 1 jiffies 0 seconds
starttime : 32,246,240 jiffies 322,462 seconds
Process run time : 31,050
Process CPU time : 485,286 jiffies 4,852 seconds
CPU usage since birth: 15.6%
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
CODE HEADER DESCRIPTION
cputime TIME cumulative CPU time, "[DD-]hh:mm:ss" format. (alias time).
etime ELAPSED elapsed time since the process was started, [DD-]hh:]mm:ss.
etimes ELAPSED elapsed time since the process was started, in seconds.
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.:
apt-get source procps
cd procps*/ps
vim HACKING
Implementation
Preparation
To calculate CPU usage for a specific process you'll need the following:
#1 uptime of the system (seconds)
#14utime - CPU time spent in user code, measured in clock ticks
#15stime - CPU time spent in kernel code, measured in clock ticks
#16cutime - Waited-for children's CPU time spent in user code (in clock ticks)
#17cstime - Waited-for children's CPU time spent in kernel code (in clock ticks)
#22starttime - Time when the process started, measured in clock ticks
Hertz (number of clock ticks per second) of your system.
Calculation
First we determine the total time spent for the process:
total_time = utime + stime
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:
total_time = total_time + cutime + cstime
Next we get the total elapsed time in seconds since the process started: