Jump to content
Sign in to follow this  
Decipher

Get/Limit Process CPU Usage - Background Script

Recommended Posts

Decipher

I took a script written by Beege, optimized the code, and removed the global variables. Its one function, reliable, and very easy to use or modify to your needs. I decided to post it because I found it very hard to find a reliable way to get the cpu percentage of a given process. I know that there are a few different methods but I deem WMI unreliable and everything I tried before optimizing this function was to slow i.e. freezing when other CPU intensive processes were active.

When monitoring a potentially CPU intensive process, I found it necessary to increase the script's priority otherwise the script/system would freeze which is exactly what I was attempting to prevent!

See Beege's updated post: :thumbsup:

#NoTrayIcon
#include <WinAPI.au3>

HotKeySet('{ESC}', '_Exit')
Func _Exit()
    Exit
EndFunc   ;==>_Exit

_LimitProcessCPU("MediaServer.exe", 10, 1, 5)

Func _LimitProcessCPU($vProcess, $nLimit, $nInterval = 1, $nScriptPriority = 2)
    Local $nProcessCpu = -1, $nOverLimit = 0
    While ProcessExists($vProcess)
        $nProcessCpu = _ProcessCPU($vProcess, $nScriptPriority)
        If $nProcessCpu > 50 Then
            $nOverLimit += 1
        Else
            $nOverLimit = 0
        EndIf
        If $nOverLimit > $nLimit Then
            $nOverLimit = 0
            ProcessClose($vProcess) ; < - Replace this with what you need.
        EndIf
        Sleep($nInterval*1000)
    WEnd
EndFunc

Func _ProcessCPU($nPID = @AutoItPID, $nScriptPriority = 2) ; Realtime priority = 5
        ; Original Author: Beege -> http://www.autoitscript.com/forum/user/8949-beege/
    $nPID = ProcessExists($nPID)
    If Not $nPID Then
        Return SetError(1, 0, "")
    EndIf
    Local Const $tagFILETIME = "struct;dword Lo;dword Hi;endstruct", $nStructs = 7
    Local Static $aStruct[$nStructs], $aPointer[$nStructs], $aStat[4], $hProcess  = _WinAPI_OpenProcess(0x1F0FFF, 0, $nPID, True), $bFirstRun = True
    Local Enum $nIDLETIME, $nKERNELTIME, $nUSERTIME, $nPCreationTime, $nPExitTime, $nPKernelTime, $nPUserTime, _
            $nProcStartKern = 0, $nProcStartUser, $nStartKernel, $nStartUser
    If $bFirstRun Then
        For $i = 0 To $nStructs - 1 Step 1
            $aStruct[$i] = DllStructCreate($tagFILETIME)
            $aPointer[$i] = DllStructGetPtr($aStruct[$i])
        Next
    EndIf
    DllCall('Kernel32.dll', "int", "GetSystemTimes", "ptr", $aPointer[$nIDLETIME], "ptr", $aPointer[$nKERNELTIME], "ptr", $aPointer[$nUSERTIME])
    DllCall('Kernel32.dll', "int", "GetProcessTimes", "hwnd", $hProcess, "ptr", $aPointer[$nPCreationTime], "ptr", $aPointer[$nPExitTime], "ptr", _
            $aPointer[$nPKernelTime], "ptr", $aPointer[$nPUserTime])
    Local $aTemp[4] = [DllStructGetData($aStruct[$nPKernelTime], 1), DllStructGetData($aStruct[$nPUserTime], 1), DllStructGetData($aStruct[$nKERNELTIME], 1), _
            DllStructGetData($aStruct[$nUSERTIME], 1)], $tProcess, $tSystem
    If Not $bFirstRun Then
        $tProcess = ($aTemp[$nProcStartKern] - $aStat[$nProcStartKern]) + ($aTemp[$nProcStartUser] - $aStat[$nProcStartUser])
        $tSystem = ($aTemp[$nStartKernel] - $aStat[$nStartKernel]) + ($aTemp[$nStartUser] - $aStat[$nStartUser])
    Else
        ProcessSetPriority(@AutoItPID, $nScriptPriority)
        $bFirstRun = False
    EndIf
    $aStat = $aTemp
    Return Int(Round(($tProcess / $tSystem) * 100))
EndFunc   ;==>_ProcessCPU

Edit - To give credit for source.

Anonymous

Edited by Decipher

Spoiler

censored.jpg

 

Share this post


Link to post
Share on other sites
Ascend4nt

Interesting, I just developed a Process CPU usage tracker not long after my work on the CPU Usage tracker code.  I liked the idea of keeping it more object-oriented, and so far my system works out pretty well.  In the future I might expand it to more than one process per usage tracker, since it'd be a shame to call GetSystemTimes more times than is necessary.

Ah, and the author you should be crediting is probably Beege - see his >Process CPU Usage script.

Share this post


Link to post
Share on other sites
Decipher

Ascend4nt,

I've looked at a lot of your code recently, it looks great! Thanks for the info. ;)

If you like at this line:

DllCall('Kernel32.dll', "int", "GetSystemTimes", "ptr", $aPointer[$nIDLETIME], "ptr", $aPointer[$nKERNELTIME], "ptr", $aPointer[$nUSERTIME])

You will see that the data structures that it populates are resuable for multiple calls to <GetProcessTimes> however I wouldn't know right off wether or not you could get around that function call or not as it requires you pass a process handle.

Anonymous

Edited by Decipher

Spoiler

censored.jpg

 

Share this post


Link to post
Share on other sites
arandomguy01

How's it going?  I'm using the _ProcessCPU function here in a script.  However, occasionally I need to change the monitored Process.  I'm  not quite sure how to do this as it seems the Static variables don't allow it.  Any advice on how to do this?  Thank you for your time.

Share this post


Link to post
Share on other sites
Decipher

arandomguy01,

Here is an example that complements JohnOne's answer.

Global $nPID = "MyProcess.exe"

While 1
    ;_ProcessCPU(3) ; 3 = AboveNormal priority
    ConsoleWrite(_ProcessCPU(3) & "%" & @CRLF)
    If "condition" = "condition" Then
        $nPID = "MyOtherProcess.exe"
    EndIf
    Sleep(1000)
WEnd

Func _ProcessCPU($nScriptPriority = 2) ; Realtime priority = 5
        ; Original Author: Beege -> http://www.autoitscript.com/forum/user/8949-beege/
    $nPID = ProcessExists($nPID)
    If Not $nPID Then
        Return SetError(1, 0, "")
    EndIf
    Static $nLastProcess = $nPID
    If $nPID <> $nLastProcess Then
        $nLastProcess = $nPID
        $hProcess  = _WinAPI_OpenProcess(0x1F0FFF, 0, $nPID, True)
    EndIf
    Local Const $tagFILETIME = "struct;dword Lo;dword Hi;endstruct", $nStructs = 7
    Local Static $aStruct[$nStructs], $aPointer[$nStructs], $aStat[4], $hProcess  = _WinAPI_OpenProcess(0x1F0FFF, 0, $nPID, True), $bFirstRun = True
    Local Enum $nIDLETIME, $nKERNELTIME, $nUSERTIME, $nPCreationTime, $nPExitTime, $nPKernelTime, $nPUserTime, _
            $nProcStartKern = 0, $nProcStartUser, $nStartKernel, $nStartUser
    If $bFirstRun Then
        For $i = 0 To $nStructs - 1 Step 1
            $aStruct[$i] = DllStructCreate($tagFILETIME)
            $aPointer[$i] = DllStructGetPtr($aStruct[$i])
        Next
    EndIf
    DllCall('Kernel32.dll', "int", "GetSystemTimes", "ptr", $aPointer[$nIDLETIME], "ptr", $aPointer[$nKERNELTIME], "ptr", $aPointer[$nUSERTIME])
    DllCall('Kernel32.dll', "int", "GetProcessTimes", "hwnd", $hProcess, "ptr", $aPointer[$nPCreationTime], "ptr", $aPointer[$nPExitTime], "ptr", _
            $aPointer[$nPKernelTime], "ptr", $aPointer[$nPUserTime])
    Local $aTemp[4] = [DllStructGetData($aStruct[$nPKernelTime], 1), DllStructGetData($aStruct[$nPUserTime], 1), DllStructGetData($aStruct[$nKERNELTIME], 1), _
            DllStructGetData($aStruct[$nUSERTIME], 1)], $tProcess, $tSystem
    If Not $bFirstRun Then
        $tProcess = ($aTemp[$nProcStartKern] - $aStat[$nProcStartKern]) + ($aTemp[$nProcStartUser] - $aStat[$nProcStartUser])
        $tSystem = ($aTemp[$nStartKernel] - $aStat[$nStartKernel]) + ($aTemp[$nStartUser] - $aStat[$nStartUser])
    Else
        ProcessSetPriority(@AutoItPID, $nScriptPriority)
        $bFirstRun = False
    EndIf
    $aStat = $aTemp
    Return Int(Round(($tProcess / $tSystem) * 100))
EndFunc   ;==>_ProcessCPU

Edit - Do notice that I slightly modified the function for use with the global variable.

Anonymous

Edited by Decipher

Spoiler

censored.jpg

 

Share this post


Link to post
Share on other sites
Beege

Interesting, I just developed a Process CPU usage tracker not long after my work on the CPU Usage tracker code.  I liked the idea of keeping it more object-oriented, and so far my system works out pretty well.  In the future I might expand it to more than one process per usage tracker, since it'd be a shame to call GetSystemTimes more times than is necessary.

 

Ah, and the author you should be crediting is probably Beege - see his >Process CPU Usage script.

 

Thanks for the credit point out Ascend4nt. Ive thought about doing the same type of expansion myself with the code below and passing an array of process handles. My only worry was how far off would the system times be by the time I got to the last call of getprocesstimes. But might be over thinking it  :wacko:

 

 Decipher,

Heres another example you could adapt from thats a little cleaner then I wrote back then with more of the updated syntax that autoit currently has. When I originally wrote it the main thought in my head was everything could be all in one function cause Im only monitoring the process itself. Hense the static var for the process handle. If the handle might change, I would recommend functions like below that except the handle as a parameter. I realize it was just a quick fix, but a global var that changes a local static var is not a good choice. Also note the use of the _ProcessExists function Im using. Its much more efficient than autoits built in one due to how autoit enumerates all processes, then goes throught the list looking for the pid. This returns both total cpu and process cpu. 

#AutoIt3Wrapper_UseX64=n

#include <winapi.au3>
#include <Timers.au3>

;Press ESC to Exit
HotKeySet('{ESC}', '_Exit')

;Press Alt-L to generate a test load
HotKeySet('!l', '_TestLoad')

;Define requested access rights depending on which os is being used
Global $iAccess = 0x0400 ; PROCESS_QUERY_INFORMATION (xp)
If (@OSBuild > 3790) Then $iAccess = 0x1000 ; PROCESS_QUERY_LIMITED_INFORMATION (Vista and up)

;Get Handle to autoit process
Global $g_hAutoItProcess = _WinAPI_OpenProcess($iAccess, 0, @AutoItPID)
If @error Then Exit (ConsoleWrite('Error getting handle' & @LF))

;Call _GetProcessCPU() 1/sec
AdlibRegister('_ProcessCPUExample', 1000)

;Sleep till exit
While Sleep(600000)
WEnd


Func _ProcessCPUExample()

    Static Local $aEnd, $aStart = _GetProcessUsage($g_hAutoItProcess)

    $aEnd = _GetProcessUsage($g_hAutoItProcess)

    ;Get both total and process cpu usage
    Local $aCPU = _CalcUsage($aStart, $aEnd)

    ;Make sure cpu values are valid
    If $aCPU[0] >= 0 And $aCPU[0] <= 100 Then
        ConsoleWrite('Total CPU Usage = ' & $aCPU[0] & '%' & @TAB & @TAB & 'AutoIt CPU Usage = ' & $aCPU[1] & '%' & @LF)
    EndIf

    ;copy last cpu times to start.
    $aStart = $aEnd

EndFunc   ;==>_GetProcessCPU

Func _GetProcessUsage($hProc)

    Static Local $tIdle = DllStructCreate("dword;dword"), $tUser = DllStructCreate("dword;dword"), $tKernel = DllStructCreate("dword;dword")
    Static Local $tCreation = DllStructCreate("dword;dword"), $tExit = DllStructCreate("dword;dword")

    ;Make sure process still exists
    If Not _ProcessExists($hProc) Then Return SetError(1, 0, -1)

    ;Array for holding idle, System and process cpu times
    Local $aTimes[3]

    ;Get system CPU times
    DllCall('Kernel32.dll', "int", "GetSystemTimes", "struct*", $tIdle, "struct*", $tUser, "struct*", $tKernel)
    $aTimes[0] = DllStructGetData($tIdle, 1)
    $aTimes[1] = DllStructGetData($tUser, 1) + DllStructGetData($tKernel, 1)

    ;Get Process cpu times
    DllCall('Kernel32.dll', "int", "GetProcessTimes", "handle", $hProc, "struct*", $tCreation, "struct*", $tExit, "struct*", $tKernel, "struct*", $tUser)
    $aTimes[2] = DllStructGetData($tUser, 1) + DllStructGetData($tKernel, 1)

    Return $aTimes

EndFunc   ;==>_GetProcessUsage

Func _CalcUsage($aCpuStart, $aCpuEnd, $nPercent = True)

    ;Calculate time difference for idle, system, process
    Local $iIdle = ($aCpuEnd[0] - $aCpuStart[0]), $iSystem = ($aCpuEnd[1] - $aCpuStart[1]), $iProcess = ($aCpuEnd[2] - $aCpuStart[2])

    ;Calculate Total and Process CPU values
    Local $aCPUTimes[2]
    $aCPUTimes[0] = ($iSystem - $iIdle) / $iSystem ;    Total CPU Usage
    $aCPUTimes[1] = $iProcess / $iSystem ;              Process CPU Usage

    ;Convert to percent
    If $nPercent Then
        $aCPUTimes[0] = Round($aCPUTimes[0] * 100)
        $aCPUTimes[1] = Round($aCPUTimes[1] * 100)
    EndIf

    Return $aCPUTimes

EndFunc   ;==>_CalcUsage

Func _ProcessExists($hProc)

    Local $aRet = DllCall('kernel32.dll', 'bool', 'GetExitCodeProcess', 'handle', $hProc, 'dword*', 0)
    If @error Or $aRet[0] = 0 Then Return SetError(1, 0, 0)

    Return ($aRet[2] = 259)

EndFunc   ;==>_ProcessExists

Func _TestLoad()
    Local $iA
    For $i = 1 To 500000
        $iA = $iA / 3
    Next
EndFunc   ;==>_TestLoad

Func _Exit()
    _WinAPI_CloseHandle($g_hAutoItProcess)
    Exit
EndFunc   ;==>_Exit
Edit: Changes to OpenProcess. Access rights should be set according to OS version used. Also debug privilages not needed in this case. Edited by Beege
  • Like 1

Share this post


Link to post
Share on other sites
Decipher

Beege,

Thanks for the awesome(efficient) code again! I'll definitely be updating my scripts. So the new dll call syntax removes the need to call DllStructGetPtr directly, hu? I had no idea that AutoIt's ProcessExists() function behaved that way. I'll be adding your functions to my reference list. ;)

Anonymous


Spoiler

censored.jpg

 

Share this post


Link to post
Share on other sites
Beege

Your very welcome. And yes, Struct* gives us the ability to pass a pointer to a struct now so no extra calls to DllStructGetPtr in most cases. 

One other tip, Static is very useful for calling function once, even if your not storing anything in the variable:

Static Local $SetPrioityOnce = ProcessSetPriority(@AutoItPID, $nScriptPriority)

Share this post


Link to post
Share on other sites
kevin3
 

 

When using the above I got occasional negative values. Found it was due to the idle time value rolling over (only the PCU percentage was checked not the Process Percentage). Also sometimes it would have no time elapse between measurements (Some OS caching the function call?) which resulted in incorrect 0% CPU time for the measured Process.
 
Here is the error checking:
Func _ProcessCPUExample()
   
    Static Local $aEnd, $aStart = _GetProcessUsage($g_hAutoItProcess)
    sleep(2000) ; make sure some time elapses between measurements. 
    $aEnd = _GetProcessUsage($g_hAutoItProcess)
    
    ;Make sure time value did not roll over
    while $aEnd[0] <= $aStart[0] or $aEnd[1] <= $aStart[1] or $aEnd[2] <= $aStart[2] 
      ConsoleWrite('ReTrying'&@CRLF)
      $aStart = _GetProcessUsage($g_hAutoItProcess)
      sleep(2000)
      $aEnd = _GetProcessUsage($g_hAutoItProcess)
    WEnd
    
    ;Get both total and process cpu usage
    Local $aCPU = _CalcUsage($aStart, $aEnd)
    ConsoleWrite('Total CPU Usage = ' & $aCPU[0] & '%' & @TAB & @TAB & 'AutoIt CPU Usage = ' & $aCPU[1] & '%' & @LF)

    ;copy last cpu times to start.
    $aStart = $aEnd

EndFunc   ;==>_GetProcessCPU 

Share this post


Link to post
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
Sign in to follow this  

  • Similar Content

    • Terenz
      By Terenz
      Hello,
      Someone is so kind to compile for me this DLL?
      https://github.com/stephane/libmodbus/tree/master/src/win32
      Many thanks!
    • Miliardsto
      By Miliardsto
      I want to detect if exact process or window uses directx or opengl or maybe something else library used in applications.
      Thats becouse there could be many windows with same names and different names and the same with process. I got so much process names I want to my script works with all, so i want standardize.
      All of this processes uses DirectX or OpenGL so then If I check this window/process uses these libraries I will be sure thats the right process
    • TryWare90Days
      By TryWare90Days
      I'm trying to kill a malware process, that I can't remove with my www.sophus.com/hom antivirus.

      The malware is known as coinminer,config and my Sophus only creates popups of blocking the malware.

      I know that the malware is constantly launching a svchost *32.exe processes, where the svchost.exe processes are from my Windows 7 operating system.
      I have with no luck tried to do this:
      Global $_bStatus = False
      While $_bStatus = False
                 Global $_iPid
                 Global $_sActiveTitleNew = "svchost *32.exe"
                 $_iPid = WinGetProcess($_sActiveTitleNew)
                 If $_iPid <> -1 Then $_bStatus = ProcessClose($_iPid)   
      Wend
      EXIT
       
      But the $_iPid doesn't ever show anything else than  -1, even if I can see the svchost *32.exe process in my TaskManager
       
      YES - I know I shouldn't EXIT after killing the first malware detection, but it is easier to explain the above for you, so I can get a solution.
    • cdeb
      By cdeb
      I'm trying to verify which is the default scanner and if it is different from what I need to propose a choice among those available.
      All this is possible with eztw32.dll however I can not make certain functions work
      Are these:
      int TWAIN_GetDefaultSourceName(LPSTR pzName)
      string TWAIN_DefaultSourceName()
      int TWAIN_GetSourceList(void)
      source: http://www.eztwain.com/pub/eztp/EZTwain_User_Guide.pdf
      I tried this way but they do not work:
      Local $pzName
      $pzName = DllCall($pathDLL,"int","TWAIN_DefaultSourceName") ;
      $rc = DllCall($pathDLL,"int","TWAIN_GetDefaultSourceName", "str*", $pzName) ;
      $rc = DllCall($pathDLL,"int","TWAIN_GetDefaultSourceName","str", $pzName) ;
      $rc = DllCall($pathDLL,"int","TWAIN_GetSourceList")
      $rc = DllCall($pathDLL,"int","TWAIN_GetSourceList", "NONE", "") ;
       
      Can anyone help me?
      Everything else works, here is the code:
       
      Local $pathDLL = @ScriptDir&"\_res\eztw32.dll" Local $rc = DllCall($pathDLL,'int','TWAIN_EasyVersion') if @error <> 0 then MsgBox(16+262144, "DllCall Error", "Error load eztw32.dll") else ConsoleWrite('--> eztw32.dll version: ' & $rc[0]/100 & @CRLF) $rc = DllCall($pathDLL,"int","TWAIN_IsAvailable") If @error > 0 Then MsgBox(16+262144, "DllCall Error", "Error call DLL - TWAIN_IsAvailable") if $rc[0] <> 0 then ConsoleWrite('--> TWAIN available: RC = ' & $rc[0]&@CRLF) ;;;;;;;;;;;;;;;;;;;;;; I NEED HERE ;manual select scanner Local $hwnd $rc = DllCall($pathDLL,"long","TWAIN_SelectImageSource",'hwnd',$hwnd) ConsoleWrite('--> TWAIN_SelectImageSource = ' & $rc[0]&@CRLF) if $rc[0] <> 0 then ConsoleWrite("--> TWAIN device selected"&@CRLF) else ConsoleWrite("--> no TWAIN device selected"&@CRLF) endif Else ConsoleWrite('--> TWAIN NOT available: RC = ' & $rc[0]&@CRLF) ; es: 0 endif endif thank you
    • Mannyfresh31
      By Mannyfresh31
      Hi Everybody I want know if there is someone kind enough to help me translate this code in to Autoit please don't blame me cause I'm a newbe 
      here is the code I need to translate also I want know if it's possible to look through a DLL  functions
       
      Declaration in Visual Basic 6: 
      Simply add the DLL import unit 'cashdll.bas' to your project, 
      or add following line to the declarations part: 
      Declare Function CASH_OPENCOM Lib "CASH.DLL" (ByVal CASH_COMPORT As Long) As Long 
       
      Declaration in Visual Basic 6: 
      Simply add the DLL import unit 'cashdll.bas' to your project, 
      or add following line to the declarations part: 
      Declare Sub CASH_CLOSECOM Lib "CASH.DLL" ()
       
       
      and here is the code I have written so far
      down below is the DLL attached also a screen shot of the  exit code of SciTE
      #include<Array.au3> $CashDLL = DllOpen("C:\Users\manue\Downloads\Casll DLL\cash.dll") if $CashDLL Then MsgBox (0,"","CashDLL Open",0) $HOPPER_OPENCOM = DllCall($CashDLL,"LONG","HOPPER_OPENCOM") if isarray ($HOPPER_OPENCOM) Then _Arraydisplay ($HOPPER_OPENCOM) Else MsgBox (0,"",@error,0) EndIf $HOPPER_CLOSECOM = DllCall($CashDLL,"NONE","HOPPER_CLOSECOM") if isarray ($HOPPER_CLOSECOM) Then _Arraydisplay ($HOPPER_CLOSECOM) Else MsgBox (0,"",@error,0) EndIf DllClose ($CashDLL)  
       

      cash.dll
×