Pointers about CPU load measuring

Jose Marcio Martins da Cruz Jose-Marcio.Martins at ensmp.fr
Mon Apr 5 05:08:41 PDT 2004


Thanks Dan. This is the best. I also tried using kvm, but code is longer 
and the program shall run as root. I'm working on a daemon which shall 
evaluate CPU load and SHALL not run as root.

Best regards,


Dan Nelson wrote:
> In the last episode (Apr 02), Dag-Erling Smorgrav said:
>>Jose Marcio Martins da Cruz <Jose-Marcio.Martins at ensmp.fr> writes:
>>>Someone can send some pointers on how to measure global CPU load under
>>>FreeBSD from a C program ? I'm looking for values for
>>>idle/kernel/user, in a similar way as does top. Is there any pointer
>>>or doc ?. I'd like to avoir browsing top code.
>>Use sysctlbyname(3) to retrieve vm.loadavg, which is a struct loadavg
>>(defined in <sys/resource.h>)
> Actually the kern.cp_time variable might be better if you want
> idle/kernel/user values. It returns a long cp_time[CPUSTATES]; the
> states are listed in <sys/resource.h>.  Top calculates percentages by
> keeping the previous interval values, calculating diffs, totaling up
> the diffs, and setting percentage[cpustate] = diff[cpustate]/totaldiff.

  Jose Marcio MARTINS DA CRUZ           Tel. :(33)
  Ecole des Mines de Paris              http://j-chkmail.ensmp.fr
  60, bd Saint Michel                http://www.ensmp.fr/~martins
  75272 - PARIS CEDEX 06      mailto:Jose-Marcio.Martins at ensmp.fr

More information about the freebsd-hackers mailing list