Skip to content

Commit

Permalink
[Shared] Set CPU affinity on com_affinity modify
Browse files Browse the repository at this point in the history
CPU affinity of the current process is now updated when com_affinity is
changed, instead of only on startup. Setting com_affinity to 0 now means
use all cores instead of "do nothing", which does not work now that the
affinity can be updated at run-time.
  • Loading branch information
xycaleth committed Mar 8, 2015
1 parent bd71338 commit 73d5136
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
6 changes: 6 additions & 0 deletions code/qcommon/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1497,6 +1497,12 @@ void Com_Frame( void ) {
c_pointcontents = 0;
}

if ( com_affinity->modified )
{
com_affinity->modified = qfalse;
Sys_SetProcessorAffinity();
}

com_frameNumber++;
}
catch ( int code )
Expand Down
6 changes: 6 additions & 0 deletions codemp/qcommon/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1575,6 +1575,12 @@ void Com_Frame( void ) {
c_pointcontents = 0;
}

if ( com_affinity->modified )
{
com_affinity->modified = qfalse;
Sys_SetProcessorAffinity();
}

com_frameNumber++;
}
catch (int code) {
Expand Down
4 changes: 2 additions & 2 deletions shared/sys/sys_unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -487,10 +487,10 @@ void Sys_SetProcessorAffinity( void ) {
if ( sscanf( com_affinity->string, "%X", &cores ) != 1 )
cores = 1; // set to first core only

const long numCores = sysconf( _SC_NPROCESSORS_ONLN );
if ( !cores )
return;
cores = (1 << numCores) - 1; // use all cores

const long numCores = sysconf( _SC_NPROCESSORS_ONLN );
cpu_set_t set;
CPU_ZERO( &set );
for ( int i = 0; i < numCores; i++ ) {
Expand Down
9 changes: 6 additions & 3 deletions shared/sys/sys_win32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,17 +178,20 @@ static const char *GetErrorString( DWORD error ) {
}

void Sys_SetProcessorAffinity( void ) {
DWORD_PTR processMask, dummy;
DWORD_PTR processMask, processAffinityMask, systemAffinityMask;
HANDLE handle = GetCurrentProcess();

if ( !GetProcessAffinityMask( handle, &dummy, &dummy ) )
if ( !GetProcessAffinityMask( handle, &processAffinityMask, &systemAffinityMask ) )
return;

if ( sscanf( com_affinity->string, "%X", &processMask ) != 1 )
processMask = 1; // set to first core only

if ( !processMask )
return;
processMask = systemAffinityMask; // use all the cores available to the system

if ( processMask == processAffinityMask )
return; // no change

if ( !SetProcessAffinityMask( handle, processMask ) )
Com_DPrintf( "Setting affinity mask failed (%s)\n", GetErrorString( GetLastError() ) );
Expand Down

0 comments on commit 73d5136

Please sign in to comment.