How to minimize server CPU usage?
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 =|
Programming is my life!
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.
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:
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.
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.
enum Bool { True, False, FileNotFound };
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.
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
}
}
}
}
{
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
}
}
}
}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement