Introduced OpenThreads::SetProcessorAffinityMaskOfCurrentThread(unsigned long cpumask) and Threads::setProcessorAffinityMask(unsigned long cpumask) to allow finer grained control over the CPU affinity.

This commit is contained in:
Robert Osfield 2016-09-26 19:18:15 +01:00
parent cd56639e67
commit 0f8a5a86e2
3 changed files with 87 additions and 52 deletions

View File

@ -35,12 +35,18 @@ namespace OpenThreads {
extern OPENTHREAD_EXPORT_DIRECTIVE int GetNumberOfProcessors(); extern OPENTHREAD_EXPORT_DIRECTIVE int GetNumberOfProcessors();
/** /**
* Set the processor affinity of current thread. * Set the processor affinity mask of current thread. If you want to allow thread to run on any processor core use ~0ul for the cpumask
*
* Note, systems where no support exists no affinity will be set, and -1 will be returned.
*
*/ */
extern OPENTHREAD_EXPORT_DIRECTIVE int SetProcessorAffinityOfCurrentThread(unsigned int cpunum); extern OPENTHREAD_EXPORT_DIRECTIVE int SetProcessorAffinityMaskOfCurrentThread(unsigned long cpumask);
/**
* Set the processor affinity of current thread.
*/
inline int SetProcessorAffinityOfCurrentThread(unsigned int cpunum)
{
unsigned long cpumask = 1ul << cpunum;
return SetProcessorAffinityMaskOfCurrentThread(cpumask);
}
/** /**
* @class Thread * @class Thread
@ -332,15 +338,28 @@ public:
void* getImplementation(){ return _prvData; }; void* getImplementation(){ return _prvData; };
/** Thread's processor affinity method. This binds a thread to a /** Set the Thread's processor affinity to a single CPU.
* processor whenever possible. This call must be made before * This call must be made before
* start() or startThread() and has no effect after the thread * start() or startThread() and has no effect after the thread
* has been running. In the pthreads implementation, this is only * has been running. Returns 0 on success, implementation's
* implemented on sgi, through a pthread extension. On other pthread
* platforms this is ignored. Returns 0 on success, implementation's
* error on failure, or -1 if ignored. * error on failure, or -1 if ignored.
*/ */
int setProcessorAffinity( unsigned int cpunum ); int setProcessorAffinity( unsigned int cpunum )
{
unsigned long cpumask = 1ul << cpunum;
return setProcessorAffinityMask(cpumask);
}
/** Set the Thread's processor affinity to a one or more CPU's using mask.
* If you want this threadd to run on any processor core then use a cpumask of ~0ul
* This call must be made before
* start() or startThread() and has no effect after the thread
* has been running. Returns 0 on success, implementation's
* error on failure, or -1 if ignored.
*/
int setProcessorAffinityMask( unsigned long cpumask);
/** microSleep method, equivalent to the posix usleep(microsec). /** microSleep method, equivalent to the posix usleep(microsec).
* This is not strictly thread API but is used * This is not strictly thread API but is used

View File

@ -108,6 +108,23 @@ void thread_cleanup_handler(void *arg)
namespace OpenThreads namespace OpenThreads
{ {
static void setCPUMask(cpu_set_t* cpumask, unsigned long in_cpumask)
{
CPU_ZERO( cpumask );
unsigned int numprocessors = OpenThreads::GetNumberOfProcessors();
unsigned int cpunum=0;
while(in_cpumask!=0 && cpunum<numprocessors)
{
if ((in_cpumask&1)!=0)
{
CPU_SET( cpunum, cpumask );
}
in_cpumask>>=1;
++cpunum;
}
}
class ThreadPrivateActions class ThreadPrivateActions
{ {
@ -129,13 +146,11 @@ private:
PThreadPrivateData *pd = PThreadPrivateData *pd =
static_cast<PThreadPrivateData *>(thread->_prvData); static_cast<PThreadPrivateData *>(thread->_prvData);
if (pd->cpumask>=0)
if (pd->cpunum>=0)
{ {
#if defined(HAVE_PTHREAD_SETAFFINITY_NP) || defined(HAVE_THREE_PARAM_SCHED_SETAFFINITY) || defined(HAVE_TWO_PARAM_SCHED_SETAFFINITY) #if defined(HAVE_PTHREAD_SETAFFINITY_NP) || defined(HAVE_THREE_PARAM_SCHED_SETAFFINITY) || defined(HAVE_TWO_PARAM_SCHED_SETAFFINITY)
cpu_set_t cpumask; cpu_set_t cpumask;
CPU_ZERO( &cpumask ); setCPUMask( &cpumask, pd->cpumask );
CPU_SET( pd->cpunum, &cpumask );
#if defined(HAVE_PTHREAD_SETAFFINITY_NP) #if defined(HAVE_PTHREAD_SETAFFINITY_NP)
pthread_setaffinity_np( pthread_self(), sizeof(cpumask), &cpumask); pthread_setaffinity_np( pthread_self(), sizeof(cpumask), &cpumask);
@ -533,25 +548,56 @@ size_t Thread::getProcessId()
return (size_t)(pd->tid); return (size_t)(pd->tid);
} }
int OpenThreads::SetProcessorAffinityMaskOfCurrentThread(unsigned long in_cpumask)
{
Thread::Init();
Thread* thread = Thread::CurrentThread();
if (thread)
{
return thread->setProcessorAffinityMask(in_cpumask);
}
else
{
#if defined(HAVE_PTHREAD_SETAFFINITY_NP) || defined(HAVE_THREE_PARAM_SCHED_SETAFFINITY) || defined(HAVE_TWO_PARAM_SCHED_SETAFFINITY)
cpu_set_t cpumask;
setCPUMask(&cpumask, in_cpumask);
#if defined(HAVE_PTHREAD_SETAFFINITY_NP)
pthread_setaffinity_np( pthread_self(), sizeof(cpumask), &cpumask);
return 0;
#elif defined(HAVE_THREE_PARAM_SCHED_SETAFFINITY)
sched_setaffinity( 0, sizeof(cpumask), &cpumask );
return 0;
#elif defined(HAVE_TWO_PARAM_SCHED_SETAFFINITY)
sched_setaffinity( 0, &cpumask );
return 0;
#endif
#endif
}
return -1;
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// //
// Description: Set the thread's processor affinity // Description: Set the thread's processor affinity
// //
// Use: public // Use: public
// //
int Thread::setProcessorAffinity(unsigned int cpunum) int Thread::setProcessorAffinityMask(unsigned long in_cpumask)
{ {
PThreadPrivateData *pd = static_cast<PThreadPrivateData *> (_prvData); PThreadPrivateData *pd = static_cast<PThreadPrivateData *> (_prvData);
pd->cpunum = cpunum; pd->cpumask = in_cpumask;
if (pd->cpunum<0) return -1; if (pd->cpumask==0) return -1;
#if defined(HAVE_PTHREAD_SETAFFINITY_NP) || defined(HAVE_THREE_PARAM_SCHED_SETAFFINITY) || defined(HAVE_TWO_PARAM_SCHED_SETAFFINITY) #if defined(HAVE_PTHREAD_SETAFFINITY_NP) || defined(HAVE_THREE_PARAM_SCHED_SETAFFINITY) || defined(HAVE_TWO_PARAM_SCHED_SETAFFINITY)
if (pd->isRunning() && Thread::CurrentThread()==this) if (pd->isRunning() && Thread::CurrentThread()==this)
{ {
cpu_set_t cpumask; cpu_set_t cpumask;
CPU_ZERO( &cpumask ); setCPUMask(&cpumask, in_cpumask);
CPU_SET( pd->cpunum, &cpumask );
#if defined(HAVE_PTHREAD_SETAFFINITY_NP) #if defined(HAVE_PTHREAD_SETAFFINITY_NP)
return pthread_setaffinity_np (pthread_self(), sizeof(cpumask), &cpumask); return pthread_setaffinity_np (pthread_self(), sizeof(cpumask), &cpumask);
#elif defined(HAVE_THREE_PARAM_SCHED_SETAFFINITY) #elif defined(HAVE_THREE_PARAM_SCHED_SETAFFINITY)
@ -995,33 +1041,3 @@ int OpenThreads::GetNumberOfProcessors()
#endif #endif
} }
int OpenThreads::SetProcessorAffinityOfCurrentThread(unsigned int cpunum)
{
Thread::Init();
Thread* thread = Thread::CurrentThread();
if (thread)
{
return thread->setProcessorAffinity(cpunum);
}
else
{
#if defined(HAVE_PTHREAD_SETAFFINITY_NP) || defined(HAVE_THREE_PARAM_SCHED_SETAFFINITY) || defined(HAVE_TWO_PARAM_SCHED_SETAFFINITY)
cpu_set_t cpumask;
CPU_ZERO( &cpumask );
CPU_SET( cpunum, &cpumask );
#if defined(HAVE_PTHREAD_SETAFFINITY_NP)
pthread_setaffinity_np( pthread_self(), sizeof(cpumask), &cpumask);
return 0;
#elif defined(HAVE_THREE_PARAM_SCHED_SETAFFINITY)
sched_setaffinity( 0, sizeof(cpumask), &cpumask );
return 0;
#elif defined(HAVE_TWO_PARAM_SCHED_SETAFFINITY)
sched_setaffinity( 0, &cpumask );
return 0;
#endif
#endif
}
return -1;
}

View File

@ -53,7 +53,7 @@ private:
nextId++; nextId++;
threadPriority = Thread::THREAD_PRIORITY_DEFAULT; threadPriority = Thread::THREAD_PRIORITY_DEFAULT;
threadPolicy = Thread::THREAD_SCHEDULE_DEFAULT; threadPolicy = Thread::THREAD_SCHEDULE_DEFAULT;
cpunum = -1; cpumask = ~0ul;
}; };
virtual ~PThreadPrivateData() {}; virtual ~PThreadPrivateData() {};
@ -81,7 +81,7 @@ private:
volatile int uniqueId; volatile int uniqueId;
volatile int cpunum; volatile unsigned long cpumask;
static int nextId; static int nextId;