Jump to content

Process CPU Usage


Beege
 Share

Recommended Posts

Here is a example for calculating how much CPU your script is using and total cpu usage. Any Ideas on how to make this a better Adlib would be great.. Thanks for looking!

#include <WinAPI.au3>
#include <array.au3>
HotKeySet('{esc}', '_exit')

Global $ProcessCpu, $TotalCpu, $nProcessCpu, $StartUser, $StartIdle, $StartKernel, $ProcStartKern, $ProcStartUser, $bFirstRun = True
AdlibEnable("_ProcessCalc", 1000)

;A little bit of work to see if its working.
Dim $comboarray, $array[6] = [1, 2, 3, 4, 5, 6]
$comboarray = _ArrayCombinations($array, 3)

SplashTextOn('Splash', 'message')
While 1
    For $i = 1 To $comboarray[0]
        ControlSetText("Splash", "", "Static1", $comboarray[$i])
        If $nProcessCpu <> $ProcessCpu Then
            $nProcessCpu = $ProcessCpu
            ConsoleWrite('Script Cpu Usage = ' & Round($nProcessCpu, 2) & '%' & @CRLF)
            ConsoleWrite('Total Cpu Usage = ' & Round($TotalCpu, 2) & '%' & @CRLF)
        EndIf
    Next
WEnd


Func _ProcessCalc()
    Local $tProcess, $tSystem, $tSystemt, $hScripthandle = _WinAPI_GetCurrentProcess()
    Local $IDLETIME = DllStructCreate($tagFILETIME), $KERNELTIME = DllStructCreate($tagFILETIME), $USERTIME = DllStructCreate($tagFILETIME)
    Local $PCreationTime = DllStructCreate($tagFILETIME), $PExitTime = DllStructCreate($tagFILETIME), $PKernelTime = DllStructCreate($tagFILETIME), $PUserTime = DllStructCreate($tagFILETIME)
    DllCall('Kernel32.dll', "int", "GetSystemTimes", "ptr", DllStructGetPtr($IDLETIME), "ptr", DllStructGetPtr($KERNELTIME), "ptr", DllStructGetPtr($USERTIME))
    DllCall('Kernel32.dll', "int", "GetProcessTimes", "hwnd", $hScripthandle, "ptr", DllStructGetPtr($PCreationTime), "ptr", DllStructGetPtr($PExitTime), "ptr", DllStructGetPtr($PKernelTime), "ptr", DllStructGetPtr($PUserTime))
    If Not $bFirstRun Then
        $tProcess = (DllStructGetData($PKernelTime, 1) - $ProcStartKern) + (DllStructGetData($PUserTime, 1) - $ProcStartUser)
        $tSystem = (DllStructGetData($KERNELTIME, 1) - $StartKernel) + (DllStructGetData($USERTIME, 1) - $StartUser)
        $tSystemt = (($tSystem - (DllStructGetData($IDLETIME, 1) - $StartIdle)) * (100/$tSystem))
    EndIf
    $ProcStartKern = DllStructGetData($PKernelTime, 1)
    $ProcStartUser = DllStructGetData($PUserTime, 1)
    $StartIdle = DllStructGetData($IDLETIME, 1)
    $StartKernel = DllStructGetData($KERNELTIME, 1)
    $StartUser = DllStructGetData($USERTIME, 1)
    If $bFirstRun Then $bFirstRun = False
    $ProcessCpu = ($tProcess / $tSystem) * 100
    $TotalCpu = $tSystemt
EndFunc   ;==>_ProcessCalc


Func _exit()
    Exit
EndFunc   ;==>_exit
Edited by bchris01
Link to comment
Share on other sites

Interesting technique. You need a 'sleep' in the main While loop, and you need to consider multiple CPU's and hyperthreading in order to match it up to the results you see in Task Manager.

..Or you could use something like, say, PDH Performance Counters...hehe. Otherwise, its a pretty cool approach..

Link to comment
Share on other sites

Interesting technique. You need a 'sleep' in the main While loop, and you need to consider multiple CPU's and hyperthreading in order to match it up to the results you see in Task Manager.

..Or you could use something like, say, PDH Performance Counters...hehe. Otherwise, its a pretty cool approach..

Im not sure what you mean about the sleep. The main While loop is just there to put a load on the cpu (testing Purposes). If I put a sleep in I would have no load and would make it hard to see if its working. And I'm pretty sure I read that the dll calls "Getsystemtimes" and "GetProcessTimes" return total kernel and user times. Not just single processors. I could be wrong though.. Ill look. Thanks for the comments.

Edited by bchris01
Link to comment
Share on other sites

Ahh, okay, didn't get the processor load thing.

*edit: just tried again, and it's reporting relatively correct %'s. Did you change something? I'm certain I was getting 100% usage the first time I ran this on my Quad core CPU.. odd :)

Edited by Ascend4nt
Link to comment
Share on other sites

Lol no I didn't change anything. I've been looking at your PDH Performance Counters udf which is sweet. I can't believe I haven't seen that before!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...