Setting affinity group for HttpConnectionHandler threads

This commit is contained in:
lganzzzo 2018-09-24 00:02:30 +03:00
parent 521291ef7f
commit 22fc5bc0db
5 changed files with 13 additions and 6 deletions

View File

@ -32,7 +32,7 @@ oatpp::concurrency::SpinLock::Atom MemoryPool::POOLS_ATOM(false);
std::unordered_map<v_int64, MemoryPool*> MemoryPool::POOLS;
std::atomic<v_int64> MemoryPool::poolIdCounter(0);
ThreadDistributedMemoryPool::ThreadDistributedMemoryPool(const std::string& name, v_int32 entrySize, v_int32 chunkSize, v_word32 shardsCount)
ThreadDistributedMemoryPool::ThreadDistributedMemoryPool(const std::string& name, v_int32 entrySize, v_int32 chunkSize, v_int32 shardsCount)
: m_shardsCount(shardsCount)
, m_shards(new MemoryPool*[m_shardsCount])
{
@ -50,7 +50,7 @@ ThreadDistributedMemoryPool::~ThreadDistributedMemoryPool(){
void* ThreadDistributedMemoryPool::obtain() {
static std::hash<std::thread::id> hashFunction;
static thread_local v_word32 hash = hashFunction(std::this_thread::get_id()) % m_shardsCount;
static thread_local v_int32 hash = hashFunction(std::this_thread::get_id()) % m_shardsCount;
return m_shards[hash]->obtain();
}

View File

@ -175,7 +175,7 @@ private:
MemoryPool** m_shards;
public:
ThreadDistributedMemoryPool(const std::string& name, v_int32 entrySize, v_int32 chunkSize,
v_word32 shardsCount = OATPP_THREAD_DISTRIBUTED_MEM_POOL_SHARDS_COUNT);
v_int32 shardsCount = OATPP_THREAD_DISTRIBUTED_MEM_POOL_SHARDS_COUNT);
virtual ~ThreadDistributedMemoryPool();
void* obtain();
};

View File

@ -38,12 +38,19 @@ v_int32 Thread::getThreadSuggestedCpuIndex(std::thread::id threadId, v_int32 cpu
}
v_int32 Thread::assignThreadToCpu(std::thread::native_handle_type nativeHandle, v_int32 cpuIndex) {
return assignThreadToCpuRange(nativeHandle, cpuIndex, cpuIndex);
}
v_int32 Thread::assignThreadToCpuRange(std::thread::native_handle_type nativeHandle, v_int32 fromCpu, v_int32 toCpu) {
#if defined(_GNU_SOURCE)
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(cpuIndex, &cpuset);
for(v_int32 i = fromCpu; i <= toCpu; i++) {
CPU_SET(cpuIndex, &cpuset);
}
v_int32 result = pthread_setaffinity_np(nativeHandle, sizeof(cpu_set_t), &cpuset);
if (result != 0) {

View File

@ -44,6 +44,7 @@ private:
public:
static v_int32 getThreadSuggestedCpuIndex(std::thread::id threadId, v_int32 cpuCount);
static v_int32 assignThreadToCpu(std::thread::native_handle_type nativeHandle, v_int32 cpuIndex);
static v_int32 assignThreadToCpuRange(std::thread::native_handle_type nativeHandle, v_int32 fromCpu, v_int32 toCpu);
static v_int32 getHardwareConcurrency();
private:
std::thread m_thread;

View File

@ -75,8 +75,7 @@ void HttpConnectionHandler::handleConnection(const std::shared_ptr<oatpp::data::
concurrency -= 1;
}
v_int32 cpu = oatpp::concurrency::Thread::getThreadSuggestedCpuIndex(thread.getStdThread()->get_id(), concurrency);
oatpp::concurrency::Thread::assignThreadToCpu(thread.getStdThread()->native_handle(), cpu);
oatpp::concurrency::Thread::assignThreadToCpuRange(thread.getStdThread()->native_handle(), 0, concurrency);
thread.detach();
}