Jump to content
Sign in to follow this  
novatek

Function _ProcessGetCPU()

Recommended Posts

novatek

$array = ProcessList("AutoIt3.exe")
$iPID = $array[1][1]
$sProcess = $iPID           ;PID number for test process name

;~ $sProcess = "Idle"       ;Other test process names
;~ $sProcess = "_Total"     ;funny one
;~ $sProcess = "ntvdm"      ;DOS process
;~ $sProcess = "AutoIt3"    ;do not assign .exe to process name


While 1
    
    $iProcessCPU = _ProcessGetCPU($sProcess, 300 )

    $sTip = "Process " & $sProcess & " CPU: " & $iProcessCPU & "%"
    traytip("", $sTip   ,1)
;~  sleep(1000)     ;set your own sleep time for LOOP mode
WEnd


Func _ProcessGetCPU($strProcess = "Idle", $iSampleTime = 500, $sComputerName = @ComputerName)
    
;~      All Parameters are optional:
;~          - Idle process will be measured if first parameter is not set
;~          - 500 ms is default sample time
;~          - This computer will be measured by default
;~      Process could be string ("Name") or PID number (1234)
;~      When more processes are runing with identical name, then the last opened is measured (use PID for other)
;~      For NORMAL MODE(one time measuring): set Sample value to more than 0 ms
;~                  ( average CPU usage will be measured during sleep time within function)
;~      For LOOP MODE (continuous measuring): set Sample value to 0 ms
;~                  ( average CPU usage will be measured between two function calls )
;~      Total CPU usage is: ( 100 - _ProcessGetCPU())
;~      Success: Returns process CPU usage in percent
;~              (Sample times below 100ms may return inaccurate results)
;~              (First result in Loop Mode may be inaccurate,
;~               because first call in Loop Mode is only used to trigger counters)
;~      Failure: Returns -1  ( wrong process name or PID )
;~             : Returns -2  ( WMI service not found or Computer not found)

    if $strProcess = "" then $strProcess = "Idle"
    if $iSampleTime = "" AND IsString($iSampleTime) then $iSampleTime = 500
    if $sComputerName = "" then $sComputerName = @ComputerName
    
    if not IsDeclared("iP1") AND $iSampleTime = 0 then      ;first time in loop mode
        $bFirstTimeInLoopMode = 1
    else
        $bFirstTimeInLoopMode = 0
    endif
    
    if not IsDeclared("iP1") then
        assign("iP1", 0, 2)     ;forced global declaration first time
        assign("iT1", 0, 2)
    endif
    
    
    $objWMIService = ObjGet("winmgmts:\\" & $sComputerName & "\root\CIMV2")
    if @error then return -2
    
    if number($strProcess) then 
        $strProcess = " WHERE IDProcess = '" & $strProcess & "'"
    else 
        $strProcess = " WHERE Name = '" & $strProcess & "'"
    endif

    if $iSampleTime OR $bFirstTimeInLoopMode = 1 then   ;skip if Loop Mode, but not First time
        
        $colItems = $objWMIService.ExecQuery ("SELECT * FROM Win32_PerfRawData_PerfProc_Process" & $strProcess)
        
        For $objItem In $colItems
            
            $iT1 = $objItem.TimeStamp_Sys100NS
            $iP1 = $objItem.PercentProcessorTime
        next

        if  $objItem = "" then return -1    ;process not found
        
        sleep($iSampleTime)
    endif
    
        
    $colItems = $objWMIService.ExecQuery ("SELECT * FROM Win32_PerfRawData_PerfProc_Process" & $strProcess)

    For $objItem In $colItems
                    
        $iP2 = $objItem.PercentProcessorTime
        $iT2 = $objItem.TimeStamp_Sys100NS  
    next

    if  $objItem = "" then return -1    ;process not found
        
    $iPP = ($iP2 - $iP1)
    $iTT = ($iT2 - $iT1)
    
    if $iTT = 0 Then return 100     ;do not divide by 0

    $iCPU = round( ($iPP/$iTT) * 100, 0)
    
    $iP1 = $iP2
    $iT1 = $iT2
    Return $iCPU
EndFunc ;==>_ProcessGetCPU() by novaTek    ...ver 0.11

This function could measure only one process at a time.

For monitoring all processes, or more processes under identical name, look for this function: Func _ProcessListCPU()

Enjoy!

Edited by novatek
  • Like 1

Share this post


Link to post
Share on other sites
Daniel W.

Hi,

thanks to garfrost

THis func is by Larry:

CODE
Func CurrentCPU($init = 0)
    Global $liOldIdleTime = 0
    Global $liOldSystemTime = 0
    Local $SYS_BASIC_INFO = 0
    Local $SYS_PERFORMANCE_INFO = 2
    Local $SYS_TIME_INFO = 3
    
    $SYSTEM_BASIC_INFORMATION = DllStructCreate("int;uint;uint;uint;uint;uint;uint;ptr;ptr;uint;byte;byte;short")
    $status = DllCall("ntdll.dll", "int", "NtQuerySystemInformation", "int", $SYS_BASIC_INFO, _
            "ptr", DllStructGetPtr($SYSTEM_BASIC_INFORMATION), _
            "int", DllStructGetSize($SYSTEM_BASIC_INFORMATION), _
            "int", 0)
    
    If $status[0]Then Return -1
    
    While 1
        $SYSTEM_PERFORMANCE_INFORMATION = DllStructCreate("int64;int[76]")
        $SYSTEM_TIME_INFORMATION = DllStructCreate("int64;int64;int64;uint;int")
        
        $status = DllCall("ntdll.dll", "int", "NtQuerySystemInformation", "int", $SYS_TIME_INFO, _
                "ptr", DllStructGetPtr($SYSTEM_TIME_INFORMATION), _
                "int", DllStructGetSize($SYSTEM_TIME_INFORMATION), _
                "int", 0)
        
        If $status[0]Then Return -2
        
        $status = DllCall("ntdll.dll", "int", "NtQuerySystemInformation", "int", $SYS_PERFORMANCE_INFO, _
                "ptr", DllStructGetPtr($SYSTEM_PERFORMANCE_INFORMATION), _
                "int", DllStructGetSize($SYSTEM_PERFORMANCE_INFORMATION), _
                "int", 0)
        
        If $status[0]Then Return -3
        
        If $init = 1 Or $liOldIdleTime = 0 Then
            $liOldIdleTime = DllStructGetData($SYSTEM_PERFORMANCE_INFORMATION, 1)
            $liOldSystemTime = DllStructGetData($SYSTEM_TIME_INFORMATION, 2)
            Sleep(1000)
            If $init = 1 Then Return -99
        Else
            $dbIdleTime = DllStructGetData($SYSTEM_PERFORMANCE_INFORMATION, 1) - $liOldIdleTime
            $dbSystemTime = DllStructGetData($SYSTEM_TIME_INFORMATION, 2) - $liOldSystemTime
            $liOldIdleTime = DllStructGetData($SYSTEM_PERFORMANCE_INFORMATION, 1)
            $liOldSystemTime = DllStructGetData($SYSTEM_TIME_INFORMATION, 2)
            
            $dbIdleTime = $dbIdleTime / $dbSystemTime
            
            $dbIdleTime = 100.0 - $dbIdleTime * 100.0 / DllStructGetData($SYSTEM_BASIC_INFORMATION, 11) + 0.5
            Return $dbIdleTime
        EndIf
        $SYSTEM_PERFORMANCE_INFORMATION = 0
        $SYSTEM_TIME_INFORMATION = 0
    WEnd
EndFunc   ;==>CurrentCPU

Edited by Daniel W.

--------------------------------------------------------------------------------------------------------------------------------Scripts : _Encrypt UDF_UniquePCCode UDF MS like calculatorInstall programm *UPDATED* --------------------------------------------------------------------------------------------------------------------------------[quote name='Helge' post='213117' date='Jul 26 2006, 10:22 AM']Have you ever tried surfing the internet with a milk-carton ?This is similar to what you're trying to do.[/quote]

Share this post


Link to post
Share on other sites
FastHelper

is there any script available to check ram usage percentage? or can anyone create it? i am in search of it. and want to learn about it.


sorry for my spelling mistakes. its due to be fast !!!

Share this post


Link to post
Share on other sites
Dougiefresh

I have a quad-core processor and was testing this. The tray icon kept reporting that the CPU Idle was somewhere between 380% and 400%. So I modified the routine a little. Here is the end of the function, starting at $iCPU:

$iCPU = round( ($iPP/$iTT) * 100, 0)
    if $iCPU > 100 then $iCPU = Round( $iCPU / Int(($iCPU + 99) / 100), 0 )
    
    $iP1 = $iP2
    $iT1 = $iT2
    Return $iCPU
EndFunc;==>_ProcessGetCPU() by novaTek  ...ver 0.11

It now returns numbers between 0% and 100%. Other than this small problem, this is a wonderful function! Thanks!

Share this post


Link to post
Share on other sites
duckling78

Entering "name" without the extension is a bit awkward for me. Maybe use something like the following instead?

$objWMIService = ObjGet("winmgmts:\\" & $sComputerName & "\root\CIMV2")
    if @error then return -2
   
    if number($strProcess) then
        $strProcess = " WHERE IDProcess = '" & $strProcess & "'"
    else
        $processList = ProcessList($strProcess)
        If $processList[0][0] > 0 Then
            For $i = 1 To $processList[0][0]
                If $processList[$i][0] = $strProcess Then
                    $strProcess = " WHERE IDProcess = '" & $processList[$i][1] & "'"
                EndIf
            Next
        Else
            Return "NA" ; Return no process found.
        EndIf
    endif
Edited by duckling78

Share this post


Link to post
Share on other sites
adamdeu

$array = ProcessList("AutoIt3.exe")
$iPID = $array[1][1]
$sProcess = $iPID           ;PID number for test process name

;~ $sProcess = "Idle"       ;Other test process names
;~ $sProcess = "_Total"     ;funny one
;~ $sProcess = "ntvdm"      ;DOS process
;~ $sProcess = "AutoIt3"    ;do not assign .exe to process name


While 1
    
    $iProcessCPU = _ProcessGetCPU($sProcess, 300 )

    $sTip = "Process " & $sProcess & " CPU: " & $iProcessCPU & "%"
    traytip("", $sTip   ,1)
;~  sleep(1000)     ;set your own sleep time for LOOP mode
WEnd


Func _ProcessGetCPU($strProcess = "Idle", $iSampleTime = 500, $sComputerName = @ComputerName)
    
;~      All Parameters are optional:
;~          - Idle process will be measured if first parameter is not set
;~          - 500 ms is default sample time
;~          - This computer will be measured by default
;~      Process could be string ("Name") or PID number (1234)
;~      When more processes are runing with identical name, then the last opened is measured (use PID for other)
;~      For NORMAL MODE(one time measuring): set Sample value to more than 0 ms
;~                  ( average CPU usage will be measured during sleep time within function)
;~      For LOOP MODE (continuous measuring): set Sample value to 0 ms
;~                  ( average CPU usage will be measured between two function calls )
;~      Total CPU usage is: ( 100 - _ProcessGetCPU())
;~      Success: Returns process CPU usage in percent
;~              (Sample times below 100ms may return inaccurate results)
;~              (First result in Loop Mode may be inaccurate,
;~               because first call in Loop Mode is only used to trigger counters)
;~      Failure: Returns -1  ( wrong process name or PID )
;~             : Returns -2  ( WMI service not found or Computer not found)

    if $strProcess = "" then $strProcess = "Idle"
    if $iSampleTime = "" AND IsString($iSampleTime) then $iSampleTime = 500
    if $sComputerName = "" then $sComputerName = @ComputerName
    
    if not IsDeclared("iP1") AND $iSampleTime = 0 then      ;first time in loop mode
        $bFirstTimeInLoopMode = 1
    else
        $bFirstTimeInLoopMode = 0
    endif
    
    if not IsDeclared("iP1") then
        assign("iP1", 0, 2)     ;forced global declaration first time
        assign("iT1", 0, 2)
    endif
    
    
    $objWMIService = ObjGet("winmgmts:\\" & $sComputerName & "\root\CIMV2")
    if @error then return -2
    
    if number($strProcess) then 
        $strProcess = " WHERE IDProcess = '" & $strProcess & "'"
    else 
        $strProcess = " WHERE Name = '" & $strProcess & "'"
    endif

    if $iSampleTime OR $bFirstTimeInLoopMode = 1 then   ;skip if Loop Mode, but not First time
        
        $colItems = $objWMIService.ExecQuery ("SELECT * FROM Win32_PerfRawData_PerfProc_Process" & $strProcess)
        
        For $objItem In $colItems
            
            $iT1 = $objItem.TimeStamp_Sys100NS
            $iP1 = $objItem.PercentProcessorTime
        next

        if  $objItem = "" then return -1    ;process not found
        
        sleep($iSampleTime)
    endif
    
        
    $colItems = $objWMIService.ExecQuery ("SELECT * FROM Win32_PerfRawData_PerfProc_Process" & $strProcess)

    For $objItem In $colItems
                    
        $iP2 = $objItem.PercentProcessorTime
        $iT2 = $objItem.TimeStamp_Sys100NS  
    next

    if  $objItem = "" then return -1    ;process not found
        
    $iPP = ($iP2 - $iP1)
    $iTT = ($iT2 - $iT1)
    
    if $iTT = 0 Then return 100     ;do not divide by 0

    $iCPU = round( ($iPP/$iTT) * 100, 0)
    
    $iP1 = $iP2
    $iT1 = $iT2
    Return $iCPU
EndFunc ;==>_ProcessGetCPU() by novaTek    ...ver 0.11

This function could measure only one process at a time.

For monitoring all processes, or more processes under identical name, look for this function: Func _ProcessListCPU()

Enjoy!

ok quick question im obvosly new to autoit so what part of the code do u change to change process you want

to end in this code and how do you change it from 90% cpu useage to 50%?

thanks if you can help

Share this post


Link to post
Share on other sites
philphil61

How can I identify if there is something wrong with my system settings... having tried Novateks ProcessList - each process tested results with a -1 (see attached image)

Running Win7 32bit on laptop

Posted Image

I cannot yet find a method to "if process idle for ?secs close process"

$array = ProcessList("AutoIt3.exe")
$iPID = $array[1][1]
$sProcess = $iPID           ;PID number for test process name
 
;~ $sProcess = "Idle"       ;Other test process names
;~ $sProcess = "_Total"     ;funny one
;~ $sProcess = "ntvdm"      ;DOS process
;~ $sProcess = "AutoIt3"    ;do not assign .exe to process name
 
 
While 1
    
    $iProcessCPU = _ProcessGetCPU($sProcess, 300 )
 
    $sTip = "Process " & $sProcess & " CPU: " & $iProcessCPU & "%"
    traytip("", $sTip   ,1)
;~  sleep(1000)     ;set your own sleep time for LOOP mode
WEnd
 
 
Func _ProcessGetCPU($strProcess = "Idle", $iSampleTime = 500, $sComputerName = @ComputerName)
    
;~      All Parameters are optional:
;~          - Idle process will be measured if first parameter is not set
;~          - 500 ms is default sample time
;~          - This computer will be measured by default
;~      Process could be string ("Name") or PID number (1234)
;~      When more processes are runing with identical name, then the last opened is measured (use PID for other)
;~      For NORMAL MODE(one time measuring): set Sample value to more than 0 ms
;~                  ( average CPU usage will be measured during sleep time within function)
;~      For LOOP MODE (continuous measuring): set Sample value to 0 ms
;~                  ( average CPU usage will be measured between two function calls )
;~      Total CPU usage is: ( 100 - _ProcessGetCPU())
;~      Success: Returns process CPU usage in percent
;~              (Sample times below 100ms may return inaccurate results)
;~              (First result in Loop Mode may be inaccurate,
;~               because first call in Loop Mode is only used to trigger counters)
;~      Failure: Returns -1  ( wrong process name or PID )
;~             : Returns -2  ( WMI service not found or Computer not found)
 
    if $strProcess = "" then $strProcess = "Idle"
    if $iSampleTime = "" AND IsString($iSampleTime) then $iSampleTime = 500
    if $sComputerName = "" then $sComputerName = @ComputerName
    
    if not IsDeclared("iP1") AND $iSampleTime = 0 then      ;first time in loop mode
        $bFirstTimeInLoopMode = 1
    else
        $bFirstTimeInLoopMode = 0
    endif
    
    if not IsDeclared("iP1") then
        assign("iP1", 0, 2)     ;forced global declaration first time
        assign("iT1", 0, 2)
    endif
    
    
    $objWMIService = ObjGet("winmgmts:\\" & $sComputerName & "\root\CIMV2")
    if @error then return -2
    
    if number($strProcess) then
        $strProcess = " WHERE IDProcess = '" & $strProcess & "'"
    else
        $strProcess = " WHERE Name = '" & $strProcess & "'"
    endif
 
    if $iSampleTime OR $bFirstTimeInLoopMode = 1 then   ;skip if Loop Mode, but not First time
        
        $colItems = $objWMIService.ExecQuery ("SELECT * FROM Win32_PerfRawData_PerfProc_Process" & $strProcess)
        
        For $objItem In $colItems
            
            $iT1 = $objItem.TimeStamp_Sys100NS
            $iP1 = $objItem.PercentProcessorTime
        next
 
        if  $objItem = "" then return -1    ;process not found
        
        sleep($iSampleTime)
    endif
    
        
    $colItems = $objWMIService.ExecQuery ("SELECT * FROM Win32_PerfRawData_PerfProc_Process" & $strProcess)
 
    For $objItem In $colItems
                    
        $iP2 = $objItem.PercentProcessorTime
        $iT2 = $objItem.TimeStamp_Sys100NS  
    next
 
    if  $objItem = "" then return -1    ;process not found
        
    $iPP = ($iP2 - $iP1)
    $iTT = ($iT2 - $iT1)
    
    if $iTT = 0 Then return 100     ;do not divide by 0
 
    $iCPU = round( ($iPP/$iTT) * 100, 0)
    
    $iP1 = $iP2
    $iT1 = $iT2
    Return $iCPU
EndFunc ;==>_ProcessGetCPU() by novaTek ...ver 0.11

This function could measure only one process at a time.

For monitoring all processes, or more processes under identical name, look for this function: Func _ProcessListCPU()

Enjoy!

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  

×