how to calculate CPU utilization?

Nimnod nimnod at gnu.univ.gda.pl
Sat Feb 12 12:51:26 PST 2005


On Sat, 12 Feb 2005 14:16:51 -0600, Dan Nelson <dnelson at allantgroup.com>  
wrote:

> In the last episode (Feb 12), Nimnod said:
>> i am looking for how to accurately calculate the CPU utilization per
>> process. i have looked through the top sources, and found out it's
>> using the kvm lib, which isn't suitable for my purpose.
>
> Why not?  You don't need to be setuid root to call kvm functions; when
> possible they fetch their info via sysctl.  Neither top nor ps are
> setuid.
>
Well, unfortunately you DO need to be setuid if you want to list all  
processes and the sysctl showallprocs is set to 0.
That's the first thing. The reason why i don't want or even can't use kvm  
is because
this to be used by non-suid app which interacts with a custom kern module,  
calling custom syscall.
At the beginning of the project I considered two approaches - a suid app  
without modules, and a non-suid one
getting proc info and doing other stuff through the kernel space. You  
cannot call kvm functions from within the
module, and setuiding my app right now just for the sake of KVM, would  
prove the whole idea useless, getting me
a hybrid system, partially talking to a module, and partially making use  
of setuid feature.
Whilst, then, the module part would make more sense when moved to the app  
sources, thus, in turn, leaving the module itself useless. :)
> You need to take two samples separated by some delay (1 or 5 seconds
> maybe), then take their difference and divide by the delay.  That will
> give you the cpu usage over that period.
>
Thx for this one. About to try out.

rgds,
	Nimnod.


More information about the freebsd-hackers mailing list