• Advertisement
Sign in to follow this  

How to minimize server CPU usage?

This topic is 4552 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Now i programming a server for my game, and this server running with 100%(cpu usage). Server Main Loop: while( msg.message != WM_QUIT ) { if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); }else { ProcessStackMessage();//Process the next 100 packets in the stack if(Server.server_on)//if server is On-Line { if((GetTickCount() - timelast) >= 200) { //Internal Updates UpdateStunGunParaliseList(); RespawnPlayers(); SystemAdmin(); ItemAdminInsert(false); timelast=GetTickCount(); } } } } Why my server is using 100% of the CPU? Thanks =|

Share this post


Link to post
Share on other sites
Advertisement
I don't see you giving any time away, so your loop will go as fast it can - if you start two copies they'll both use 50% CPU. I'd suggest you introduce a sleep call if you want to give up some time - however if your app is the only thing on the server, or if you can use priority levels with other tasks that will be running 24/7, using 100% may be ok, just like it is with games. The only problem is it would make remote terminals laggy.

Share this post


Link to post
Share on other sites
What is the allowed latency on the server? If you can allow 50 milliseconds or so, you can post a WM_TIMER event to yourself, and use GetMessage() instead of PeekMessage(). That will cause most of the CPU to be given up to other applications.

If you need better performance, then I would structure the loop something like:


int idleCount = 0;
forever {
++idleCount;
while( PeekMessage() ) {
Do Message
idleCount = 0;
}
if( HandleAnyNetworkEvents() ) {
idleCount = 0;
}
if( idleCount ) {
MsgWaitForMultipleObjectsEx( ... ); // with timeout 10 ms
}
}


Note that MsgWaitForMulitpleObjectsEx is a better way to sleep than just calling Sleep() (or SleepEx()). For more details, check MSDN, or my Windows Event Loop page.

Share this post


Link to post
Share on other sites
I used a very primitive method to reduce my server cpu usage. In the network thread loop, if I dont receive a packet I introduce an small delay of 25 ms. That was enough to lower cpu usage form 100% to an insignificant level.

Share this post


Link to post
Share on other sites
while( msg.message != WM_QUIT )
{
if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}else
{

ProcessStackMessage();//Process the next 100 packets in the stack


if(Server.server_on)//if server is On-Line
{

if((GetTickCount() - timelast) >= 200)
{
//Internal Updates
UpdateStunGunParaliseList();
RespawnPlayers();
SystemAdmin();
ItemAdminInsert(false);
timelast=GetTickCount();
}else{
sleep(GetTickCount() - timelast) //HERES The new line
}

}
}
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement