Fixed: #1069 Thread/process CPU affinity Implementation for Linux

This commit is contained in:
kervala 2010-08-12 19:15:29 +02:00
parent 79be5ff213
commit 9a528f3f8f
3 changed files with 57 additions and 12 deletions

View file

@ -62,7 +62,6 @@ private:
uint8 _State; // 0=not created, 1=started, 2=finished uint8 _State; // 0=not created, 1=started, 2=finished
uint32 _StackSize; uint32 _StackSize;
pthread_t _ThreadHandle; pthread_t _ThreadHandle;
}; };
/** /**
@ -77,7 +76,6 @@ public:
virtual ~CPProcess() {} virtual ~CPProcess() {}
virtual uint64 getCPUMask(); virtual uint64 getCPUMask();
virtual bool setCPUMask(uint64 mask); virtual bool setCPUMask(uint64 mask);
}; };
} // NLMISC } // NLMISC

View file

@ -111,20 +111,18 @@ public:
/** /**
* Set the CPU mask of this thread. Thread must have been started before. * Set the CPU mask of this thread. Thread must have been started before.
* The mask must be a subset of the CPU mask returned by IProcess::getCPUMask() thread process. * The mask must be a subset of the CPU mask returned by IProcess::getCPUMask() thread process.
* Not implemented under Linux.
*/ */
virtual bool setCPUMask(uint64 cpuMask)=0; virtual bool setCPUMask(uint64 cpuMask)=0;
/** /**
* Get the CPU mask of this thread. Thread must have been started before. * Get the CPU mask of this thread. Thread must have been started before.
* The mask should be a subset of the CPU mask returned by IProcess::getCPUMask() thread process. * The mask should be a subset of the CPU mask returned by IProcess::getCPUMask() thread process.
* Not implemented under Linux.
*/ */
virtual uint64 getCPUMask()=0; virtual uint64 getCPUMask()=0;
/** /**
* Get the thread user name. * Get the thread user name.
* Not implemented under linux, under windows return the name of the logon user. * Under Linux return thread owner, under windows return the name of the logon user.
*/ */
virtual std::string getUserName()=0; virtual std::string getUserName()=0;
}; };
@ -158,13 +156,11 @@ public:
/** /**
* Return process CPU mask. Each bit stand for a CPU usable by the process threads. * Return process CPU mask. Each bit stand for a CPU usable by the process threads.
* Not implemented under Linux.
*/ */
virtual uint64 getCPUMask()=0; virtual uint64 getCPUMask()=0;
/** /**
* Set the process CPU mask. Each bit stand for a CPU usable by the process threads. * Set the process CPU mask. Each bit stand for a CPU usable by the process threads.
* Not implemented under Linux.
*/ */
virtual bool setCPUMask(uint64 mask)=0; virtual bool setCPUMask(uint64 mask)=0;
}; };

View file

@ -21,6 +21,8 @@
#include "nel/misc/p_thread.h" #include "nel/misc/p_thread.h"
#include <sched.h>
#include <pwd.h>
namespace NLMISC { namespace NLMISC {
@ -147,6 +149,16 @@ void CPThread::wait ()
*/ */
bool CPThread::setCPUMask(uint64 cpuMask) bool CPThread::setCPUMask(uint64 cpuMask)
{ {
#ifdef __USE_GNU
sint res = pthread_setaffinity_np(_ThreadHandle, sizeof(uint64), (const cpu_set_t*)&cpuMask);
if (res)
{
nlwarning("pthread_setaffinity_np() returned %d", res);
return false;
}
#endif // __USE_GNU
return true; return true;
} }
@ -155,7 +167,19 @@ bool CPThread::setCPUMask(uint64 cpuMask)
*/ */
uint64 CPThread::getCPUMask() uint64 CPThread::getCPUMask()
{ {
return 1; uint64 cpuMask = 1;
#ifdef __USE_GNU
sint res = pthread_getaffinity_np(_ThreadHandle, sizeof(uint64), (cpu_set_t*)&cpuMask);
if (res)
{
nlwarning("pthread_getaffinity_np() returned %d", res);
return 0;
}
#endif // __USE_GNU
return cpuMask;
} }
/* /*
@ -163,7 +187,12 @@ uint64 CPThread::getCPUMask()
*/ */
std::string CPThread::getUserName() std::string CPThread::getUserName()
{ {
return "Not implemented"; struct passwd *pw = getpwuid(getuid());
if (!pw)
return "";
return pw->pw_name;
} }
@ -183,13 +212,35 @@ IProcess *IProcess::getCurrentProcess ()
*/ */
uint64 CPProcess::getCPUMask() uint64 CPProcess::getCPUMask()
{ {
return 1; uint64 cpuMask = 1;
#ifdef __USE_GNU
sint res = sched_getaffinity(getpid(), sizeof(uint64), (cpu_set_t*)&cpuMask);
if (res)
{
nlwarning("sched_getaffinity() returned %d, errno = %d: %s", res, errno, strerror(errno));
return 0;
}
#endif // __USE_GNU
return cpuMask;
} }
/// set the CPU mask /// set the CPU mask
bool CPProcess::setCPUMask(uint64 mask) bool CPProcess::setCPUMask(uint64 cpuMask)
{ {
return 1; #ifdef __USE_GNU
sint res = sched_setaffinity(getpid(), sizeof(uint64), (const cpu_set_t*)&cpuMask);
if (res)
{
nlwarning("sched_setaffinity() returned %d, errno = %d: %s", res, errno, strerror(errno));
return false;
}
#endif // __USE_GNU
return true;
} }