Jump to content

A way to monitor page faults?


Recommended Posts

Hi all,

I've got a process that I need to keep running, but the people who wrote it (Razer) did a poor job and it needlessly causes nearly a thousand page faults per second at times. Is there any Autoit (or C/C++ for that matter) function call that anyone knows of to either tell me how many page faults total the process has caused, or even better a way to tell how many over a period of time?

The script would detect when the page faults exceed a certain number per second, then kill and restart the process. This is for the media control buttons on my Lycosa keyboard if anyone is curious. I'm using Windows Vista.

Thanks!

Edited by sharrakor
Link to comment
Share on other sites

Here the WMI and WinAPI version:

;fast hack by UEZ
#include <WinAPI.au3>

MsgBox(0, "PageFaults WMI", "Page Faults for Explorer.exe: " & _WMI_GetPageFault("explorer.exe"))
MsgBox(0, "PageFaults WinAPI", "Page Faults for Explorer.exe: " & _WinAPI_GetPageFault(ProcessExists("explorer.exe")))

Func _WMI_GetPageFault($processname, $system = ".")
    Local $PageFaults
    Local $objWMIService = ObjGet("winmgmts:{impersonationLevel = impersonate}!\\" & $system & "\root\cimv2")
    Local $colItems = $objWMIService.ExecQuery("SELECT PageFaults FROM  Win32_Process WHERE Name = '"  & $processname & "'", "WQL", 0x30)
    For $objItem In $colItems
        $PageFaults =  $objItem.PageFaults
    Next
    Return $PageFaults
EndFunc

Func _WinAPI_GetPageFault($PID) ;http://msdn.microsoft.com/en-us/library/ms683219(v=VS.85).aspx
    Local Const $PROCESS_MEMORY_COUNTERS = _
                        "DWORD cb;" & _
                        "DWORD PageFaultCount;" & _
                        "ULONG_PTR PeakWorkingSetSize;" & _
                        "ULONG_PTR WorkingSetSize;" & _
                        "ULONG_PTR QuotaPeakPagedPoolUsage;" & _
                        "ULONG_PTR QuotaPagedPoolUsage;" & _
                        "ULONG_PTR QuotaPeakNonPagedPoolUsage;" & _
                        "ULONG_PTR QuotaNonPagedPoolUsage;" & _
                        "ULONG_PTR PagefileUsage;" & _
                        "ULONG_PTR PeakPagefileUsage;"
    $tPMC = DllStructCreate($PROCESS_MEMORY_COUNTERS)
    Local $hProcess = DllCall('kernel32.dll', 'ptr', 'OpenProcess', 'dword', 0x00000410, 'int', 0, 'dword', $PID)
    Local $aRes = DllCall ("psapi.dll", "int",  "GetProcessMemoryInfo", _
                                                                            "handle", $hProcess[0], _
                                                                            "ptr", DllStructGetPtr($tPMC), _
                                                                            "dword", DllStructGetSize($tPMC))
    _WinAPI_CloseHandle($hProcess[0])
    Return DllStructGetData($tPMC, "PageFaultCount")
EndFunc


; #FUNCTION# ====================================================================================================================
; Name...........: _WinAPI_GetProcessMemoryInfo
; Description....: Retrieves information about the memory usage of the specified process.
; Syntax.........: _WinAPI_GetProcessMemoryInfo ( [$PID] )
; Parameters.....: $PID    - The PID of the process. Default (0) is the current process.
; Return values..: Success - The array that contains the following information.
;
;                            [0] - The number of page faults.
;                            [1] - The peak working set size, in bytes.
;                            [2] - The current working set size, in bytes.
;                            [3] - The peak paged pool usage, in bytes.
;                            [4] - The current paged pool usage, in bytes.
;                            [5] - The peak nonpaged pool usage, in bytes.
;                            [6] - The current nonpaged pool usage, in bytes.
;                            [7] - The current space allocated for the pagefile, in bytes.
;                            [8] - The peak space allocated for the pagefile, in bytes.
;                            [9] - The current amount of memory that cannot be shared with other processes, in bytes.
;
;                  Failure - 0 and sets the @error flag to non-zero.
; Author.........: Yashied
; Modified.......:
; Remarks........: None
; Related........:
; Link...........: @@MsdnLink@@ GetProcessMemoryInfo
; Example........: Yes
; ===============================================================================================================================

Func _WinAPI_GetProcessMemoryInfo($PID = 0)
    If Not $PID Then
        $PID = _WinAPI_GetCurrentProcessID()
        If Not $PID Then Return SetError(1, 0, 0)
    EndIf
    Local $hProcess = DllCall('kernel32.dll', 'ptr', 'OpenProcess', 'dword', 0x00000410, 'int', 0, 'dword', $PID)
    If (@error) Or (Not $hProcess[0]) Then Return SetError(1, 0, 0)
    Local $tPMC_EX = DllStructCreate('dword;dword;ulong_ptr;ulong_ptr;ulong_ptr;ulong_ptr;ulong_ptr;ulong_ptr;ulong_ptr;ulong_ptr;ulong_ptr')
    Local $Ret = DllCall(@SystemDir & '\psapi.dll', 'int', 'GetProcessMemoryInfo', 'ptr', $hProcess[0], 'ptr', DllStructGetPtr($tPMC_EX), 'int', DllStructGetSize($tPMC_EX))
    If (@error) Or (Not $Ret[0]) Then $Ret = 0
    _WinAPI_CloseHandle($hProcess[0])
    If Not IsArray($Ret) Then Return SetError(1, 0, 0)
    Local $Result[10]
    For $i = 0 To 9
        $Result[$i] = DllStructGetData($tPMC_EX, $i + 2)
    Next
    Return $Result
EndFunc   ;==>_WinAPI_GetProcessMemoryInfo

_WinAPI_GetProcessMemoryInfo() from Yashied is more complete :)

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

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...