Jump to content

if not processexist


Recommended Posts

  • Developers

I have heard about the page faults caused by ProcessExist() before, but what is the problem with that?

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

I have heard about the page faults caused by ProcessExist() before, but what is the problem with that?

Jos

i am getting about 200-300 page faults per second... i dunno if that's good or not, overtime it hits more than a million and continues to increase

maybe i don't understand what page faults are? please help, thanks.

Link to comment
Share on other sites

i am getting about 200-300 page faults per second... i dunno if that's good or not, overtime it hits more than a million and continues to increase

maybe i don't understand what page faults are? please help, thanks.

Welcome to the forum.

I get closer to 3000-5000 page faults per second using ProcessExist.

Here is a Wiki page that you can read more about page faults:

http://en.wikipedia.org/wiki/Page_fault

Here is a small portion from that page...

Page faults, by their very nature, degrade the performance of a program or operating system and in the degenerate case can cause thrashing. Optimizations to programs and the operating system that reduce the number of page faults that occur improve the performance of the program or even the entire system. The two primary focuses of the optimization effort focus on reducing overall memory usage and improving memory locality. Generally, making more physical memory available also reduces page faults. Many page replacement algorithms have been proposed, implementing a heuristic to reduce the incidence of page faults.

An average hard disk has an average latency of 3ms, a seek-time of 5ms, and a transfer-time of 0.05ms/page. So the total time for paging comes in near 8ms (8 000 000 ns). If the memory access time is 200ns, then the page fault would make the operation about 40,000 times slower.

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

Welcome to the forum.

I get closer to 3000-5000 page faults per second using ProcessExist.

Here is a Wiki page that you can read more about page faults:

http://en.wikipedia.org/wiki/Page_fault

Here is a small portion from that page...

i read the wiki, so would the high page fault degrade the computer's performance? is there any way way to reduce it?

Link to comment
Share on other sites

I'm not sure because I haven't worked with this memory function but this may help.

Global $hPid = ShellExecute("helloworld.exe")
while 1
Sleep("1000")
If Not ProcessExist($hPid) Then
   _ReduceMemory(($hPid)
   $hPid = ShellExecute($hPid)
EndIf
WEnd

Func _ReduceMemory($i_PID);; Hammerfist (I think)
    If $i_PID <> -1 Then
        Local $ai_Handle = DllCall("kernel32.dll", 'int', 'OpenProcess', 'int', 0x1f0fff, 'int', False, 'int', $i_PID)
        Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', $ai_Handle[0])
        DllCall('kernel32.dll', 'int', 'CloseHandle', 'int', $ai_Handle[0])
    Else
        Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', -1)
    EndIf

    Return $ai_Return[0]
EndFunc ;==>_ReduceMemory

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

geosoft, i tried your script:

Global $hPid = ShellExecute("helloworld.exe")
while 1
Sleep(1000)
If Not ProcessExists($hPid) Then
   _ReduceMemory($hPid)
   $hPid = ShellExecute($hPid)
EndIf
WEnd

Func _ReduceMemory($i_PID);; Hammerfist (I think)
    If $i_PID <> -1 Then
        Local $ai_Handle = DllCall("kernel32.dll", 'int', 'OpenProcess', 'int', 0x1f0fff, 'int', False, 'int', $i_PID)
        Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', $ai_Handle[0])
        DllCall('kernel32.dll', 'int', 'CloseHandle', 'int', $ai_Handle[0])
    Else
        Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', -1)
    EndIf

    Return $ai_Return[0]
EndFunc ;==>_ReduceMemory

together with a compiled "helloworld.exe" script:

While 1
    
Sleep(1000)
    
WEnd

but i get this errors:

Windows cannot find '1'. Make sure you typed the name correctly, and then try again. To search for a file, click the Start button, and then click Search.

&

Windows cannot find '0'. Make sure you typed the name correctly, and then try again. To search for a file, click the Start button, and then click Search.

Link to comment
Share on other sites

geosoft, i tried your script:

Global $hPid = ShellExecute("helloworld.exe")
while 1
Sleep(1000)
If Not ProcessExists($hPid) Then
   _ReduceMemory($hPid)
   $hPid = ShellExecute($hPid)
EndIf
WEnd

Func _ReduceMemory($i_PID);; Hammerfist (I think)
    If $i_PID <> -1 Then
        Local $ai_Handle = DllCall("kernel32.dll", 'int', 'OpenProcess', 'int', 0x1f0fff, 'int', False, 'int', $i_PID)
        Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', $ai_Handle[0])
        DllCall('kernel32.dll', 'int', 'CloseHandle', 'int', $ai_Handle[0])
    Else
        Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', -1)
    EndIf

    Return $ai_Return[0]
EndFunc ;==>_ReduceMemory

together with a compiled "helloworld.exe" script:

While 1
    
Sleep(1000)
    
WEnd

but i get this errors:

My bad!

Of course it won't run. Change it to this.

Global $sApp = "helloworld.exe"
Global $hPid = ShellExecute($sApp)
while 1
Sleep(1000)
If Not ProcessExists($hPid) Then
   _ReduceMemory($hPid)
   $hPid = ShellExecute($sApp)
EndIf
WEnd

Func _ReduceMemory($i_PID);; Hammerfist (I think)
    If $i_PID <> -1 Then
        Local $ai_Handle = DllCall("kernel32.dll", 'int', 'OpenProcess', 'int', 0x1f0fff, 'int', False, 'int', $i_PID)
        Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', $ai_Handle[0])
        DllCall('kernel32.dll', 'int', 'CloseHandle', 'int', $ai_Handle[0])
    Else
        Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', -1)
    EndIf

    Return $ai_Return[0]
EndFunc ;==>_ReduceMemory

On a side note. Don't be too surprised if one of the grammar cops comes along and nails you for not using caps where they should be used.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

My bad!

Of course it won't run. Change it to this.

Global $sApp = "helloworld.exe"
Global $hPid = ShellExecute($sApp)
while 1
Sleep(1000)
If Not ProcessExists($hPid) Then
   _ReduceMemory($hPid)
   $hPid = ShellExecute($sApp)
EndIf
WEnd

Func _ReduceMemory($i_PID);; Hammerfist (I think)
    If $i_PID <> -1 Then
        Local $ai_Handle = DllCall("kernel32.dll", 'int', 'OpenProcess', 'int', 0x1f0fff, 'int', False, 'int', $i_PID)
        Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', $ai_Handle[0])
        DllCall('kernel32.dll', 'int', 'CloseHandle', 'int', $ai_Handle[0])
    Else
        Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', -1)
    EndIf

    Return $ai_Return[0]
EndFunc ;==>_ReduceMemory

On a side note. Don't be too surprised if one of the grammar cops comes along and nails you for not using caps where they should be used.

I'm getting this error when I run the script:

Line -1

Variable used without being declared

At the mean time, I'm increasing the sleep duration to 5secs to slow down the Page Fault accumulation...
Link to comment
Share on other sites

That error is typical of a compiled script. Did you compile what I gave you or not? If not the error is coming from helloworld.exe

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Just a few thoughts:

  • ShellExecute does not return a process ID.
  • Error checking should be used to ensure that DllCalls are returning arrays.
#1 - Correct so the ShellExecute() lines should be changed to Run();; Note to self: That's insane (Feature request)

#2 - Not my function so I'm not adding in the Error handling.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

... Func _ReduceMemory($i_PID);; Hammerfist (I think) ...

See here:

http://www.autoitscript.com/forum/index.php?showtopic=13399&view=findpost&p=683969

I've sent a PM to invite "them" to join the thread & maybe add the error checking that zorphnog mentioned, but I would expect the end result of this script to actually make more page faults - not fewer... but I might not be understanding the UDF's function.

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

via PM from Prog@ndy

Func _ReduceMemory($ProcID = 0)
    ; Original version : w_Outer
    ; modified by Rajesh V R to include process ID
    ; modified by Prog@ndy
    Local $ai_Return, $ai_Handle
    If $ProcID <= 0 Then ; No process id specified - use current process instead.
        $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'ptr', -1)
        If @error Then Return SetError(1, 0, 0)
        Return $ai_Return[0]
    EndIf

    $ProcID = ProcessExists($ProcID)
    If $ProcID = 0 Then Return SetError(2, 0, 0)
    $ai_Handle = DllCall("kernel32.dll", 'ptr', 'OpenProcess', 'dword', 0x1f0fff, 'int', False, 'dword', $ProcID)
    If @error Or $ai_Handle[0] = 0 Then Return SetError(3, 0, 0)
    $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'ptr', $ai_Handle[0])
    If @error Then
        DllCall('kernel32.dll', 'int', 'CloseHandle', 'ptr', $ai_Handle[0])
        Return SetError(4, 0, 0)
    EndIf
    DllCall('kernel32.dll', 'int', 'CloseHandle', 'ptr', $ai_Handle[0])
    Return $ai_Return[0]

EndFunc   ;==>_ReduceMemory

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

That error is typical of a compiled script. Did you compile what I gave you or not? If not the error is coming from helloworld.exe

My mistake. I forgot to add '1' after 'While' in helloworld.exe. But the file is spawning too many helloworld.exes!

See here:

http://www.autoitscript.com/forum/index....p?showtopic=13399&view=findpost&p=683969

I've sent a PM to invite "them" to join the thread & maybe add the error checking that zorphnog mentioned, but I would expect the end result of this script to actually make more page faults - not fewer... but I might not be understanding the UDF's function.

via PM from Prog@ndy

Func _ReduceMemory($ProcID = 0)
    ; Original version : w_Outer
    ; modified by Rajesh V R to include process ID
    ; modified by Prog@ndy
    Local $ai_Return, $ai_Handle
    If $ProcID <= 0 Then ; No process id specified - use current process instead.
        $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'ptr', -1)
        If @error Then Return SetError(1, 0, 0)
        Return $ai_Return[0]
    EndIf

    $ProcID = ProcessExists($ProcID)
    If $ProcID = 0 Then Return SetError(2, 0, 0)
    $ai_Handle = DllCall("kernel32.dll", 'ptr', 'OpenProcess', 'dword', 0x1f0fff, 'int', False, 'dword', $ProcID)
    If @error Or $ai_Handle[0] = 0 Then Return SetError(3, 0, 0)
    $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'ptr', $ai_Handle[0])
    If @error Then
        DllCall('kernel32.dll', 'int', 'CloseHandle', 'ptr', $ai_Handle[0])
        Return SetError(4, 0, 0)
    EndIf
    DllCall('kernel32.dll', 'int', 'CloseHandle', 'ptr', $ai_Handle[0])
    Return $ai_Return[0]

EndFunc   ;==>_ReduceMemory

How do you use the script? Is it the same as the ones above?
Link to comment
Share on other sites

@olympus,

Let's start from the beginning. First, despite what I quoted from Wiki, certain types of page faults do not hurt system performance by any noticeable amount. At least I cannot measure any performance hit because of the page faults that my script causes.

Second, the Wiki suggests giving the most RAM available to the app causing the page faults. That is what GEOSoft was attempting to do with the reduce memory UDF. Personally, I don't think it is a good idea to mess with the memory management that XP already has in place... but it is only software and should not hurt anything to try.

Third, what are you attempting to do with the sample code in your first post? Why is the "If ProcessExists" line in a loop? Those are somewhat rhetorical questions - unless my assumption is wrong, you are attempting to keep some app running at all times. But in case I've guessed wrong, then please enlighten me.

Caveat - I'm not sure about this info:

As far as how to use the _ReduceMemory UDF, since the ProcessExists function is the cause of the pagefaults, the UDF needs to be fed the process ID (PID) of the script, not the application that the script is looking for. Fortunately for us, the UDF will find the PID of the script calling it all by itself... hence my calling it with no parameter passed on... i.e. () and not ($PID)

If Not _ReduceMemory() Then MsgBox(0, "Error", "The _ReduceMemory UDF did not work as expected.")

Global $hPid = Run("notepad.exe")

If Not ProcessWait("notepad.exe", 5) Then
    MsgBox(0, "Error", "Notepad did not start in time.")
    Exit
EndIf

While 1
    Sleep(1000)
    If Not ProcessExists($hPid) Then $hPid = Run($hPid)
WEnd

Func _ReduceMemory($ProcID = 0)
    ; Original version : w_Outer
    ; modified by Rajesh V R to include process ID
    ; modified by Prog@ndy
    Local $ai_Return, $ai_Handle
    If $ProcID <= 0 Then ; No process id specified - use current process instead.
        $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'ptr', -1)
        If @error Then Return SetError(1, 0, 0)
        Return $ai_Return[0]
    EndIf

    $ProcID = ProcessExists($ProcID)
    If $ProcID = 0 Then Return SetError(2, 0, 0)
    $ai_Handle = DllCall("kernel32.dll", 'ptr', 'OpenProcess', 'dword', 0x1f0fff, 'int', False, 'dword', $ProcID)
    If @error Or $ai_Handle[0] = 0 Then Return SetError(3, 0, 0)
    $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'ptr', $ai_Handle[0])
    If @error Then
        DllCall('kernel32.dll', 'int', 'CloseHandle', 'ptr', $ai_Handle[0])
        Return SetError(4, 0, 0)
    EndIf
    DllCall('kernel32.dll', 'int', 'CloseHandle', 'ptr', $ai_Handle[0])
    Return $ai_Return[0]

EndFunc   ;==>_ReduceMemory
The end result is - there are still page faults and I cannot tell you if the are less than before or not. If you want to measure the with UDF and without UDF delta, then increase the sleep to 10 seconds or so and manually record the increase in page faults each time the ProcessExits line runs. Next, remark out the UDF and repeat the data collection.

Clear as mud?

[size="1"][font="Arial"].[u].[/u][/font][/size]

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