[osg-users] Processor Affinity and forked processes

Pete Black pete at marchingcubes.com
Sat Nov 28 14:48:32 PST 2015


The logic seems to be if there are more than 1 CPU, and the threadingModel is set to singlethreaded (the default), then set CPU affinity (on what is very likely the main thread of the application) to 0.

This really seems to be a poor idea - it might help a specific OSG app configuration, but silently causes unexpected and almost certainly undesirable behaviour in situations where the programmer is doing their own multithreading.

Its fine to say 'you can reset the CPU affinity manually' but why would anyone think they would need to do this? The only way you notice this is that your app turns into a jerkfest and 'top' puzzlingly shows the OSG app pegging a single core while the others sit idle. 

Running multiple instances of an OSG-based app where the viewer is hardcoding CPU affinity to 0 on both instances is also not going to be pretty, despite the app seemingly working acceptably during single-process testing.

Moving a line of code to create a thread from before the Viewer is initialised to after it will also do very different things w/regard to CPU resource usage.

This also introduces discrepancies between behaviour on different platforms - as setting CPU affinity on OS X does basically nothing, so the same app running on OS X will scale to use all cores just fine - however if/when you did add support for OS X setCPUAffinity to OpenThreads, you would be forcing programmers to write platform-specific hacks around OSG's hardcoded behaviour (since there is no 'unset cpu affinity' function in OpenThreads) , instead of treating OSG as a cross-platform library.

Surely if the programmer wishes CPU affinity to be set, this should be explicit, and should return some kind of status so the programmer is aware the operation did not succeed (e.g. on OS X).

It seems to me that there is no reason apps  cannot make the same direct OpenThreads call to set CPU affinity, if this is desired, rather than baking it into the guts of osgViewer.



Code:

void ViewerBase::setUpThreading()
{
    ...
    if (_threadingModel==SingleThreaded)
    {
        if (_threadsRunning) stopThreading();
        else
        {
            // we'll set processor affinity here to help single threaded apps
            // with multiple processor cores, and using the database pager.
            int numProcessors = OpenThreads::GetNumberOfProcessors();
            bool affinity = numProcessors>1;
            if (affinity)
            {
                OpenThreads::SetProcessorAffinityOfCurrentThread(0);
                ...
            }




------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=65784#65784








More information about the osg-users mailing list