Skip to content

Commit

Permalink
Optionally restore arbitrary sleep between frames in dedicated server
Browse files Browse the repository at this point in the history
This was removed while introducing com_busyWait (PR JACoders#695). Before
merging PR JACoders#695, client behaviour was equivalent to com_busyWait 1,
while dedicated server behaviour was broadly similar to com_busyWait 0
(run when there is network I/O, or as much as is necessary for sv_fps).
However, dedicated servers also had a hard-coded 5ms sleep before each
frame during which they did nothing, not even processing network I/O,
resulting in the dedicated server never using as much as a full CPU
core per process even if the operating system scheduler would have
allowed it. That sleep was not reflected in the new code path for
com_busyWait 0.

This commit makes the sleep time configurable via a new cvar.
com_dedicated_sleep 0 is the same as PR JACoders#695 and ioquake3 (best
performance), while com_dedicated_sleep 5 is the same as historical
OpenJK behaviour (reduced performance, increased latency, ensures
that CPU time is made available to other processes). Larger values
result in greater reductions to both CPU usage and performance.
  • Loading branch information
smcv committed Apr 5, 2016
1 parent 0bbbccf commit e1ee901
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
7 changes: 7 additions & 0 deletions codemp/qcommon/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ fileHandle_t com_journalDataFile; // config files are written here
cvar_t *com_speeds;
cvar_t *com_developer;
cvar_t *com_dedicated;
cvar_t *com_dedicated_sleep;
cvar_t *com_timescale;
cvar_t *com_fixedtime;
cvar_t *com_journal;
Expand Down Expand Up @@ -1200,6 +1201,12 @@ void Com_Init( char *commandLine ) {
com_dedicated = Cvar_Get ("_dedicated", "0", CVAR_ROM|CVAR_INIT|CVAR_PROTECTED);
// Cvar_CheckRange( com_dedicated, 0, 2, qtrue );
#endif

// If positive, the dedicated server will sleep this long
// before each frame, reducing CPU usage but increasing
// latency. This is basically cooperative multi-tasking.
com_dedicated_sleep = Cvar_Get("com_dedicated_sleep", "0", CVAR_ARCHIVE);

// allocate the stack based hunk allocator
Com_InitHunkMemory();

Expand Down
1 change: 1 addition & 0 deletions codemp/qcommon/qcommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,7 @@ void Com_StartupVariable( const char *match );

extern cvar_t *com_developer;
extern cvar_t *com_dedicated;
extern cvar_t *com_dedicated_sleep;
extern cvar_t *com_speeds;
extern cvar_t *com_timescale;
extern cvar_t *com_sv_running;
Expand Down
27 changes: 12 additions & 15 deletions shared/sys/sys_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -768,26 +768,23 @@ int main ( int argc, char* argv[] )
// main game loop
while (1)
{
if ( com_busyWait->integer )
{
bool shouldSleep = false;
int yield_msec;

#if !defined(_JK2EXE)
if ( com_dedicated->integer )
{
shouldSleep = true;
}
if ( com_dedicated->integer && com_dedicated_sleep->integer )
{
yield_msec = com_dedicated_sleep->integer;
}
#endif

if ( com_minimized->integer )
{
shouldSleep = true;
}
if ( com_minimized->integer && com_busyWait->integer )
{
yield_msec = 5;
}

if ( shouldSleep )
{
Sys_Sleep( 5 );
}
if ( yield_msec > 0 )
{
Sys_Sleep( yield_msec );
}

// make sure mouse and joystick are only called once a frame
Expand Down

0 comments on commit e1ee901

Please sign in to comment.