mirror of
https://github.com/oatpp/oatpp.git
synced 2025-04-12 18:50:22 +08:00
Setting affinity group for HttpConnectionHandler threads
This commit is contained in:
parent
521291ef7f
commit
22fc5bc0db
@ -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();
|
||||
}
|
||||
|
||||
|
@ -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();
|
||||
};
|
||||
|
@ -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) {
|
||||
|
@ -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;
|
||||
|
@ -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();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user