# how does cpu utilization is calculates?



## abhiram (Mar 9, 2010)

hi,

when we execute ps command with options -aux it will display %CPU. what does this %CPU constitues of and how it is calculated? if one of the process has 100% CPU utilization, what does that mean?

Thanks,
Abhi.


----------



## Beastie (Mar 9, 2010)

It's quite self-explanatory. It's the percentage of CPU used by that particular process. And it's probably calculated by counting how long (e.g. how many ticks) has the process been running. You may check the source (/usr/src/bin/ps) if you need more information.

If a process has 100%, it mean it's using 100% of the CPU. If this is temporary, it's not a problem. If it stays there, then it might be locked up in a loop. Kill the process right away and restart it. Unless of course you're talking about the idle process.


----------



## abhiram (Mar 9, 2010)

i looked into the ps source code. following is the formula used to calculate cpu utilization:


```
%cpu utilization=(100.0 * fxtofl(k->ki_p->ki_pctcpu)/
                                       (1.0 - exp(k->ki_p->ki_swtime * log(fxtofl(ccpu)))));
```

ki_pctcpu is the % CPU for the process during ki_swtime
ki_swtime is time swapped in or out ( what does this mean i didn't understand, whether it is the time took for context switching  or time difference between swapping in and out??)

ccpu (KERN_CCPU) scheduler exponential decay value.


```
#define fxtofl(fixpt)   ((double)(fixpt) / fscale)
```

fscale (kern.fscale) is kernel fixed point scale factor.

As per this formula, is it possible for a process to have 100% CPU? if yes can you please explain me how?

Also, what does this formula mean??

Thanks,
Abhi.


----------

