rootx

Which Way... to CPU usage Specific Process?

7 posts in this topic

#1 ·  Posted

I find two solution to do it, but I need to profile a specific process, 4 example notepad.exe... Maybe https://www.autoitscript.com/autoit3/docs/functions/ProcessList.htm

#Julien

Func _Processor_Usage()
Local $s_Text = ''
Dim $Obj_WMIService = ObjGet('winmgmts:\\' & @ComputerName & '\root\cimv2');
If (IsObj($Obj_WMIService)) And (Not @error) Then
    Dim $Col_Items = $Obj_WMIService.ExecQuery('SELECT * FROM Win32_PerfFormattedData_PerfOS_Processor')

    Local $Obj_Item
    For $Obj_Item In $Col_Items
        Local $s_Text = $Obj_Item.PercentProcessorTime & '%'
    Next

    Return String($s_Text)
Else
    Return 0
EndIf
EndFunc

or

$aUsage = _GetCPUUsage()
For $i = 1 To $aUsage[0]
    ConsoleWrite('CPU #' & $i & ' - ' & $aUsage[$i] & '%' & @CRLF)
Next


;#####################################################################
;# Function: _GetCPUUsage()
;# Gets the utilization of the CPU, compatible with multicore
;# Return:   Array
;#           Array[0] Count of CPU, error if negative
;#           Array[n] Utilization of CPU #n in percent
;# Error:    -1 Error at 1st Dll-Call
;#           -2 Error at 2nd Dll-Call
;#           -3 Error at 3rd Dll-Call
;# Author:   Bitboy  (AutoIt.de)
;#####################################################################
Func _GetCPUUsage()
    Local Const $SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION = 8
    Local Const $SYSTEM_TIME_INFO = 3
    Local Const $tagS_SPPI = "int64 IdleTime;int64 KernelTime;int64 UserTime;int64 DpcTime;int64 InterruptTime;long InterruptCount"

    Local $CpuNum, $IdleOldArr[1],$IdleNewArr[1], $tmpStruct
    Local $timediff = 0, $starttime = 0
    Local $S_SYSTEM_TIME_INFORMATION, $S_SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION
    Local $RetArr[1]

    Local $S_SYSTEM_INFO = DllStructCreate("ushort dwOemId;short wProcessorArchitecture;dword dwPageSize;ptr lpMinimumApplicationAddress;" & _
    "ptr lpMaximumApplicationAddress;long_ptr dwActiveProcessorMask;dword dwNumberOfProcessors;dword dwProcessorType;dword dwAllocationGranularity;" & _
    "short wProcessorLevel;short wProcessorRevision")

    $err = DllCall("Kernel32.dll", "none", "GetSystemInfo", "ptr",DllStructGetPtr($S_SYSTEM_INFO))

    If @error Or Not IsArray($err) Then
        Return $RetArr[0] = -1
    Else
        $CpuNum = DllStructGetData($S_SYSTEM_INFO, "dwNumberOfProcessors")
        ReDim $RetArr[$CpuNum+1]
        $RetArr[0] = $CpuNum
    EndIf
    $S_SYSTEM_INFO = 0

    While 1
        $S_SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION = DllStructCreate($tagS_SPPI)
        $StructSize = DllStructGetSize($S_SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION)
        $S_SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION = DllStructCreate("byte puffer[" & $StructSize * $CpuNum & "]")
        $pointer = DllStructGetPtr($S_SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION)

        $err = DllCall("ntdll.dll", "int", "NtQuerySystemInformation", _
            "int", $SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION, _
            "ptr", DllStructGetPtr($S_SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION), _
            "int", DllStructGetSize($S_SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION), _
            "int", 0)

        If $err[0] Then
            Return $RetArr[0] = -2
        EndIf

        Local $S_SYSTEM_TIME_INFORMATION = DllStructCreate("int64;int64;int64;uint;int")
        $err = DllCall("ntdll.dll", "int", "NtQuerySystemInformation", _
            "int", $SYSTEM_TIME_INFO, _
            "ptr", DllStructGetPtr($S_SYSTEM_TIME_INFORMATION), _
            "int", DllStructGetSize($S_SYSTEM_TIME_INFORMATION), _
            "int", 0)

        If $err[0] Then
            Return $RetArr[0] = -3
        EndIf

        If $starttime = 0 Then
            ReDim $IdleOldArr[$CpuNum]
            For $i = 0 to $CpuNum -1
                $tmpStruct = DllStructCreate($tagS_SPPI, $Pointer + $i*$StructSize)
                $IdleOldArr[$i] = DllStructGetData($tmpStruct,"IdleTime")
            Next
            $starttime = DllStructGetData($S_SYSTEM_TIME_INFORMATION, 2)
            Sleep(100)
        Else
            ReDim $IdleNewArr[$CpuNum]
            For $i = 0 to $CpuNum -1
                $tmpStruct = DllStructCreate($tagS_SPPI, $Pointer + $i*$StructSize)
                $IdleNewArr[$i] = DllStructGetData($tmpStruct,"IdleTime")
            Next

            $timediff = DllStructGetData($S_SYSTEM_TIME_INFORMATION, 2) - $starttime

            For $i=0 to $CpuNum -1
                $RetArr[$i+1] = Round(100-(($IdleNewArr[$i] - $IdleOldArr[$i]) * 100 / $timediff))
            Next

            Return $RetArr
        EndIf

        $S_SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION = 0
        $S_SYSTEM_TIME_INFORMATION = 0
        $tmpStruct = 0
    WEnd
EndFunc

 

Share this post


Link to post
Share on other sites



#2 ·  Posted

#include <WinAPISys.au3>
#include <WinAPIProc.au3>

$PID = ProcessExists("notepad.exe")

While Sleep(500)
  ToolTip(_GetProcUsage($PID))
WEnd

Func _GetProcUsage($PID)
  If Not ProcessExists($PID) Then Return -1
  Local Static $Prev1, $Prev2
  Local $Time1 = _WinAPI_GetProcessTimes($PID)
  Local $Time2 = _WinAPI_GetSystemTimes()
  If Not IsArray($Time1) Or Not IsArray($Time2) Then Return -1
  $Time1 = $Time1[1] + $Time1[2]
  $Time2 = $Time2[1] + $Time2[2]
  $CPU = Round(($Time1 - $Prev1) / ($Time2 - $Prev2) * 100)
  $Prev1 = $Time1
  $Prev2 = $Time2
  Return $CPU
EndFunc

 

Share this post


Link to post
Share on other sites

#3 ·  Posted

Thx... But I talk about CPU % USAGE and not TIME.

Share this post


Link to post
Share on other sites

#4 ·  Posted

@rootx

The function returns how much the process loads the CPU. Is it not what you want?

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

mmm.. I'm not sure I understand " how much the process loads the CPU " because I mean CPU % USAGE

to remove all doubt.. I want to run an external process and understand how much CPU and memory use

 

Cattura.JPG

Edited by rootx

Share this post


Link to post
Share on other sites

#6 ·  Posted

@rootx

Run the script and compare tooltip text with CPU value

scr.png

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

#include <WinAPISys.au3>
#include <WinAPIProc.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=

$Form1 = GUICreate("Form1", 370, 72, 192, 124)
$Label1 = GUICtrlCreateLabel("", 16, 16, 340, 17)
$bt = GUICtrlCreateButton("start", 16,40, 340, 30)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $bt
            $PID = ProcessExists("vlc.exe")
            While Sleep(500)
                GUICtrlSetData($Label1,_GetProcUsage($PID))
            WEnd
    EndSwitch
WEnd

Func _GetProcUsage($PID)
  If Not ProcessExists($PID) Then Return -1
  Local Static $Prev1, $Prev2
  Local $Time1 = _WinAPI_GetProcessTimes($PID)
  Local $Time2 = _WinAPI_GetSystemTimes()
  If Not IsArray($Time1) Or Not IsArray($Time2) Then Return -1
  $Time1 = $Time1[1] + $Time1[2]
  $Time2 = $Time2[1] + $Time2[2]
  $CPU = Round(($Time1 - $Prev1) / ($Time2 - $Prev2) * 100)
  $Prev1 = $Time1
  $Prev2 = $Time2
  Return $CPU
EndFunc

Thx, but why is not precise?

 

dd.JPG

Edited by rootx

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