User Tools

Site Tools


finding_20the_20cpu_20usage

Finding the CPU usage

by Richard Russell, October 2011

The code listed below reports, once per second, the current total CPU usage:

        *FLOAT 64
 
        DIM IdleTime{l%,h%}, KernelTime{l%,h%}, UserTime{l%,h%}
 
        idletime = 0
        kerneltime = 0
        usertime = 0
        REPEAT
          WAIT 100
          SYS "GetSystemTimes", IdleTime{}, KernelTime{}, UserTime{}
 
          oldidletime = idletime
          oldkerneltime = kerneltime
          oldusertime = usertime
          idletime = IdleTime.h% * 2^32 + (IdleTime.l% >>> 1)*2
          kerneltime = KernelTime.h% * 2^32 + (KernelTime.l% >>> 1)*2
          usertime = UserTime.h% * 2^32 + (UserTime.l% >>> 1)*2
 
          idledelta = idletime-oldidletime
          kerneldelta = kerneltime-oldkerneltime
          userdelta = usertime-oldusertime
 
          cpuusage = (userdelta+kerneldelta-idledelta)/(userdelta+kerneldelta)
          PRINT "CPU usage = ";INT(100*cpuusage+0.5) "%"
 
        UNTIL FALSE
This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies
finding_20the_20cpu_20usage.txt · Last modified: 2018/04/15 11:20 by richardrussell