Jump to content

Please help me reduce cpu usage on this litle app


Recommended Posts

Hi all

I've basically ripped this script from a post in the forum somewhere (sorry cant remember where from .. could of even been from the help file)

I have added/changed about 3 - 4 lines to get it to do what i require though

however..when i run it i notice that it uses loads of cpu time while waiting for a connection. There must be a better way for me to do this but as i'm a nooby i am looking for some someone to guide me..

CODE
TCPStartup()

;Starts the TCP connection. Basically allows other TCP functions to work

$Ip = @IPAddress1

$Port = 5900

;This waits for a connection on @IPAddress1 and port 5900, the default port

$Incoming = TCPListen($Ip, $Port)

;This "Listens" for a message from the client

While 1

;Starts the "While" loop

Do

;Declares a "Do" loop, which means it does an action until a specific condition is met

$Con = TCPAccept($Incoming)

;This "Accecpts" any incoming connections from other clients

Until $con > 0

;Does this until the variable $con has a value higher than zero

$whatever = 1

;Sets a dummy variable called $whatever. This will indicate when to exit the server

Do

;Starts another Do loop

$msg = TCPRecv($con, 5000)

;This waits for a value from the variable $con (The connected client). The value must be less than 5000 bytes

If $msg <> "" Then

;If there is ANY message then split the test into array and check what the request is and then perform the required function

$request = stringsplit($msg, "^")

sleep(500)

if $request[0] = 7 then controler($msg)

ElseIf $msg = "exit" Then

;If the client sends just the word "Exit" as the command then

$whatever = 2

;It changes the dummy variable $whatever to 2, so...

EndIf

Until $whatever <> 1

;If the Dummy doesn't equal 1 then

TCPShutdown()

;It stops the connection

Exit

;And exits

WEnd

;Stops the while loop and exits

;

;

;

;

; functions

;

;

;

func controler($msg)

$cleaned = StringReplace($msg, " ", "~")

MsgBox(0, "tcp", $cleaned,30)

$runthis = "c:\gwservers\controler.exe " & $cleaned

run($runthis)

EndFunc

thanks in advance guys..

btw there are game servers running off the same box so every ounce of cpu time is crucial :)

Edited by Rotahn
Link to comment
Share on other sites

There was a post recently from a developer explaining how GuiGetMsg() really works. I can't seem to find it.

Anyways, it said something about GuiGetMsg() will run a tight loop until another application requests CPU time. If you start another cpu intensive program you will see the cpu usage of your script drop.

You could also put a Sleep(100) inside your first Do...Until loop.

Link to post from Jon:

#341362

Edited by weaponx
Link to comment
Share on other sites

There was a post recently from a developer explaining how GuiGetMsg() really works. I can't seem to find it.

Anyways, it said something about GuiGetMsg() will run a tight loop until another application requests CPU time. If you start another cpu intensive program you will see the cpu usage of your script drop.

You could also put a Sleep(100) inside your first Do...Until loop.

Thanks..

Will give that a try

Link to comment
Share on other sites

hi again..

ok..

when i run the app the cpu time is negligable.. showing 0 most of the time which is what i would expect.. if left running listening for tcp connections after a few hours it starts using 20% cpu on a q6600 2.4!! i have tried the suggestions above but they dont seem to work. Also after killing the process either by exiting the script properly or just closing the process..sometimes.. leaves the the cores on the load of 20% still.

Now i did think that perhaps that killing the process and it still using the cpu @ 20% could be another rogue process..so.. i just booted up the machine with nothing at all loaded appart from 2003 server..ran the script and left it.. it did start using 20% after about 4 hours..i then closed the process to which it cleared all cpu usage..

so .. i loaded a game server up and ran it along side it.. after a few hours the script started to use the same amount of cpu.. i killed it and it still carried on using the same amount , but just on 2 cores. Its the game server i hear you say.. well.. this may well be but i have trioed this with 3 different game engines and all does the same..but not all the time.. its a srange one..

anyone dare to help :)

Link to comment
Share on other sites

The loop with TCPRecv is probably trapped looping as the conditions will not let it out of the loop. The small example shows you that "exit" cannot work so how can it get out of the loop.

$msg = 'exit'
If $msg <> "" Then
    MsgBox(0x40000, '', '$msg <> ""')
ElseIf $msg = 'exit' Then
    MsgBox(0x40000, '', '$msg = "exit"')
EndIfoÝ÷ ØZ½æuçl¢g©àzÇè­ìb¶)ඥ¢ZÛ(ê-êæz+rÄÞ²Ö§vÇø­éi±«­¢+Ù%Q
AMÑÉÑÕÀ ¤Q¡¸($íMÑÉÑÌÑ¡Q
@½¹¹Ñ¥½¸¸   Í¥±±ä±±½Ý̽ѡÈQ
@չѥ½¹ÌѼݽɬ($ÀÌØí%Àô%AÉÍÌÄ($ÀÌØíA½ÉÐôÔäÀÀ($íQ¡¥ÌÝ¥Ñ̽Ƚ¹¹Ñ¥½¸½¸%AÉÍÌĹÁ½ÉÐÔäÀÀ°Ñ¡Õ±ÐÁ½ÉÐ($ÀÌØí%¹½µ¥¹ôQ
A1¥ÍѸ ÀÌØí%À°ÀÌØíA½ÉФ(%%ÀÌØí%¹½µ¥¹ô´ÄQ¡¸($%5Í    ½à ÁàÐÀÀÀÀ°Õ±Ð°ÌäíQ
A1¥ÍѸ¥±Ý¥Ñ ÉɽÈôÌäìµÀìÉɽȤ($%á¥ÐÄ(%¹%($íQ¡¥ÌÅÕ½Ðí1¥ÍѹÌÅÕ½Ðì½ÈµÍÍɽ´Ñ¡±¥¹Ð(%]¡¥±Ä($$íMÑÉÑÌÑ¡ÅÕ½Ðí]¡¥±ÅÕ½Ðì±½½À($($$$í±ÉÌÅÕ½Ðí¼ÅÕ½Ðì±½½À°Ý¡¥ µ¹Ì¥Ð½Ì¸Ñ¥½¸Õ¹Ñ¥°ÍÁ¥¥½¹¥Ñ¥½¸¥ÌµÐ($$$ÀÌØí
½¸ôQ
AÁÐ ÀÌØí%¹½µ¥¹¤($$$íQ¡¥ÌÅÕ½ÐíÁÑÌÅÕ½Ðì¹ä¥¹½µ¥¹½¹¹Ñ¥½¹Ìɽ´½Ñ¡È±¥¹ÑÌ($%U¹Ñ¥°ÀÌØí
½¸ÐìôÀ($$í½ÌÑ¡¥Ìչѥ°Ñ¡ÙÉ¥±ÀÌØí½¸¡ÌÙ±Õ¡¥¡ÈÑ¡¸éɼ($%]¡¥±Ä($$$íMÑÉÑÌ]¡¥±±½½À($$$ÀÌØíµÍôQ
AIØ ÀÌØí
½¸°ÔÀÀÀ¤($$$íQ¡¥ÌÝ¥Ñ̽ÈÙ±Õɽ´Ñ¡ÙÉ¥±ÀÌØí½¸¡Q¡½¹¹Ñ±¥¹Ð¤¸Q¡Ù±ÕµÕÍбÍÌÑ¡¸ÔÀÀÀåÑÌ($$%%ÉɽÈ=ÈÀÌØíµÍôÅÕ½Ðíá¥ÐÅÕ½ÐìQ¡¸($$$$í%Ñ¡±¥¹Ð͹̩ÕÍÐѡݽÉÅÕ½Ðíá¥ÐÅÕ½ÐìÌÑ¡½µµ¹Ñ¡¸($$$%á¥Ñ1½½À($$%±Í%ÀÌØíµÍ±ÐìÐìÅÕ½ÐìÅÕ½ÐìQ¡¸($$$$í%Ñ¡É¥Ì9dµÍÍÑ¡¸ÍÁ±¥ÐÑ¡ÑÍÐ¥¹Ñ¼ÉÉ乡¬Ý¡ÐÑ¡ÉÅÕÍХ̹ѡ¸ÁɽɴѡÉÅÕ¥Éչѥ½¸($$$$ÀÌØíÉÅÕÍÐôMÑÉ¥¹MÁ±¥Ð ÀÌØíµÍ°ÅÕ½ÐíxÅÕ½Ðì¤($$$%M±À ÔÀÀ¤($$$%%ÀÌØíÉÅÕÍÑlÁtôÜQ¡¸½¹Ñɽ±È ÀÌØíµÍ¤($$%¹%($%]¹($%á¥Ð($$í¹á¥ÑÌ(%]¹($íMѽÁÌÑ¡Ý¡¥±±½½À¹á¥ÑÌ)¹%(ì(ì(ì(ì(ìչѥ½¹Ì(ì(ì(ì)Õ¹½¹Ñɽ±È ÀÌØíµÍ¤($ÀÌØí±¹ôMÑÉ¥¹IÁ± ÀÌØíµÍ°ÅÕ½ÐìÅÕ½Ðì°ÅÕ½ÐíøÅÕ½Ðì¤(%5Í   ½à À°ÅÕ½ÐíÑÀÅÕ½Ðì°ÀÌØí±¹°ÌÀ¤($ÀÌØíÉչѡ¥ÌôÅÕ½ÐíèÀäÈíÝÍÉÙÉÌÀäÈí½¹Ñɽ±È¹áÅÕ½ÐìµÀìÀÌØí±¹(%IÕ¸ ÀÌØíÉչѡ¥Ì¤)¹Õ¹ìôôÐí½¹Ñɽ±È()Õ¹=¹Õѽ%Ñá¥Ð ¤($ìM¡Õнݸ±°½¹¹Ñ¥½¹Ì(%]¡¥±Q
AM¡Õѽݸ ¤($%M±À ÄÀÀ¤(%]¹)¹Õ¹

You certainly like plenty of comments.

:)

Link to comment
Share on other sites

hi all..

many thanks for everyones help..

it appears i wasn't doing what i wanted correctly..

I wasnt using a gui before as i just wanted it to sit in the system tray but it appears that adding the gui along with peoples help here i got it to do what i wanted

Thanks

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