Sign in to follow this  
Followers 0
sno

CPU load of a process

7 posts in this topic

I would like to capture the cpu load of some processes. Depending on the average load (measured by a couple of samples) one action must take place.

How do I take samples of the cpu load regarding a process?


Grtx Sno. The Novice Newbie.

Share this post


Link to post
Share on other sites



No luck so far, i have found out that PDH.dll can give me some performance information. But as i posted b4 that goes beyond my understanding. Maybe some1 can point me a bit furder into the right direction? Thx.

The PdhGetCounterInfo function retrieves information about a counter, such as data size, counter type, path, and user-supplied data values.

PDH_STATUS PdhGetCounterInfo(

    IN HQUERY hCounter, // handle of the counter

    IN BOOLEAN bRetrieveExplainText, // TRUE to retrieve explain text

    IN LPDWORD pdwBufferSize, // pointer to size of lpBuffer

    IN PPDH_COUNTER_INFO lpBuffer // buffer for counter information

  );

Parameters

hCounter

The handle of the counter from which to retrieve the information.

bRetrieveExplainText

Determines whether explain text is retrieved. If you set this parameter to TRUE, then the explain text for the counter is retrieved. If you set this parameter to FALSE, the field in the returned buffer is NULL.

pdwBufferSize

A pointer to the size, in bytes, of the buffer passed in lpBuffer. If the counter requires a buffer larger than is indicated by pdwBufferSize, then PdhGetCounterInfo will return the required buffer size in this field. If the function succeeds, then this field will contain the size of the data returned in lpBuffer. If the size is zero, then no data will be returned in the buffer (in fact, lpBuffer can be NULL); and the size, in bytes, will be returned in the DWORD.

lpBuffer

A pointer to the data buffer to receive the counter information. The buffer returned is variable-length, because the string data is appended to the end of the fixed-format portion of the structure. This is done so that all data is returned in a single buffer allocated by the caller.


Grtx Sno. The Novice Newbie.

Share this post


Link to post
Share on other sites

kick :(


Grtx Sno. The Novice Newbie.

Share this post


Link to post
Share on other sites

I espected this to be a simple question for some guys around, gues not. And I will be glad to get some help on this one if u have some free time wouter.


Grtx Sno. The Novice Newbie.

Share this post


Link to post
Share on other sites

@sno

This will you help starting. The code brings you the performance info for all processes running at that time.

So If you now the PID of that process (see via taksmanager) you can select only that process you want and querie for that process.

You need to convert this script to an EXE and schedule it to run every X minutes, to update the data.

If the condition is met you take action

$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = "localhost"

$Output=""
$Output = $Output & "Computer: " & $strComputer  & @CRLF
$Output = $Output & "==========================================" & @CRLF
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_PerfFormattedData_PerfProc_Process", "WQL", _
                                          $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

If IsObj($colItems) then
   For $objItem In $colItems
      $Output = $Output & "Caption: " & $objItem.Caption & @CRLF
      $Output = $Output & "CreatingProcessID: " & $objItem.CreatingProcessID & @CRLF
      $Output = $Output & "Description: " & $objItem.Description & @CRLF
      $Output = $Output & "ElapsedTime: " & $objItem.ElapsedTime & @CRLF
      $Output = $Output & "Frequency_Object: " & $objItem.Frequency_Object & @CRLF
      $Output = $Output & "Frequency_PerfTime: " & $objItem.Frequency_PerfTime & @CRLF
      $Output = $Output & "Frequency_Sys100NS: " & $objItem.Frequency_Sys100NS & @CRLF
      $Output = $Output & "HandleCount: " & $objItem.HandleCount & @CRLF
      $Output = $Output & "IDProcess: " & $objItem.IDProcess & @CRLF
      $Output = $Output & "IODataBytesPersec: " & $objItem.IODataBytesPersec & @CRLF
      $Output = $Output & "IODataOperationsPersec: " & $objItem.IODataOperationsPersec & @CRLF
      $Output = $Output & "IOOtherBytesPersec: " & $objItem.IOOtherBytesPersec & @CRLF
      $Output = $Output & "IOOtherOperationsPersec: " & $objItem.IOOtherOperationsPersec & @CRLF
      $Output = $Output & "IOReadBytesPersec: " & $objItem.IOReadBytesPersec & @CRLF
      $Output = $Output & "IOReadOperationsPersec: " & $objItem.IOReadOperationsPersec & @CRLF
      $Output = $Output & "IOWriteBytesPersec: " & $objItem.IOWriteBytesPersec & @CRLF
      $Output = $Output & "IOWriteOperationsPersec: " & $objItem.IOWriteOperationsPersec & @CRLF
      $Output = $Output & "Name: " & $objItem.Name & @CRLF
      $Output = $Output & "PageFaultsPersec: " & $objItem.PageFaultsPersec & @CRLF
      $Output = $Output & "PageFileBytes: " & $objItem.PageFileBytes & @CRLF
      $Output = $Output & "PageFileBytesPeak: " & $objItem.PageFileBytesPeak & @CRLF
      $Output = $Output & "PercentPrivilegedTime: " & $objItem.PercentPrivilegedTime & @CRLF
      $Output = $Output & "PercentProcessorTime: " & $objItem.PercentProcessorTime & @CRLF
      $Output = $Output & "PercentUserTime: " & $objItem.PercentUserTime & @CRLF
      $Output = $Output & "PoolNonpagedBytes: " & $objItem.PoolNonpagedBytes & @CRLF
      $Output = $Output & "PoolPagedBytes: " & $objItem.PoolPagedBytes & @CRLF
      $Output = $Output & "PriorityBase: " & $objItem.PriorityBase & @CRLF
      $Output = $Output & "PrivateBytes: " & $objItem.PrivateBytes & @CRLF
      $Output = $Output & "ThreadCount: " & $objItem.ThreadCount & @CRLF
      $Output = $Output & "Timestamp_Object: " & $objItem.Timestamp_Object & @CRLF
      $Output = $Output & "Timestamp_PerfTime: " & $objItem.Timestamp_PerfTime & @CRLF
      $Output = $Output & "Timestamp_Sys100NS: " & $objItem.Timestamp_Sys100NS & @CRLF
      $Output = $Output & "VirtualBytes: " & $objItem.VirtualBytes & @CRLF
      $Output = $Output & "VirtualBytesPeak: " & $objItem.VirtualBytesPeak & @CRLF
      $Output = $Output & "WorkingSet: " & $objItem.WorkingSet & @CRLF
      $Output = $Output & "WorkingSetPeak: " & $objItem.WorkingSetPeak & @CRLF
      if Msgbox(1,"WMI Output",$Output) = 2 then ExitLoop
      $Output=""
   Next
Else
   Msgbox(0,"WMI Output","No WMI Objects Found for class: " & "Win32_PerfFormattedData_PerfProc_Process" )
Endif

More parameters are available, too many to list here.

See WMI for more info (AutoIT scriptomatic Tool).

Share this post


Link to post
Share on other sites

Thx for the idea.

One question tho, does compiling the wmi code into a AU exe require the pc to be wmi enabled?


Grtx Sno. The Novice Newbie.

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  
Followers 0