Skip to content

Commit a30136a

Browse files
authored
allow set cpu affinity (#531)
1 parent 0cbb6a1 commit a30136a

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

example/benchmark.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ using namespace cinatra;
44
using namespace std::chrono_literals;
55

66
int main() {
7-
coro_http_server server(std::thread::hardware_concurrency(), 8090);
7+
coro_http_server server(std::thread::hardware_concurrency(), 8090, true);
88
server.set_http_handler<GET>(
99
"/plaintext", [](coro_http_request& req, coro_http_response& resp) {
1010
resp.get_conn()->set_multi_buf(false);

include/cinatra/coro_http_server.hpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ class coro_http_server {
3030
coro_http_server(asio::io_context &ctx, unsigned short port)
3131
: out_ctx_(&ctx), port_(port), acceptor_(ctx), check_timer_(ctx) {}
3232

33-
coro_http_server(size_t thread_num, unsigned short port)
34-
: pool_(std::make_unique<coro_io::io_context_pool>(thread_num)),
33+
coro_http_server(size_t thread_num, unsigned short port,
34+
bool cpu_affinity = false)
35+
: pool_(std::make_unique<coro_io::io_context_pool>(thread_num,
36+
cpu_affinity)),
3537
port_(port),
3638
acceptor_(pool_->get_executor()->get_asio_executor()),
3739
check_timer_(pool_->get_executor()->get_asio_executor()) {}

include/cinatra/ylt/coro_io/io_context_pool.hpp

+20-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
#include <thread>
2828
#include <type_traits>
2929
#include <vector>
30+
#ifdef __linux__
31+
#include <pthread.h>
32+
#include <sched.h>
33+
#endif
3034

3135
namespace coro_io {
3236

@@ -107,7 +111,8 @@ get_current_executor() {
107111
class io_context_pool {
108112
public:
109113
using executor_type = asio::io_context::executor_type;
110-
explicit io_context_pool(std::size_t pool_size) : next_io_context_(0) {
114+
explicit io_context_pool(std::size_t pool_size, bool cpu_affinity = false)
115+
: next_io_context_(0), cpu_affinity_(cpu_affinity) {
111116
if (pool_size == 0) {
112117
pool_size = 1; // set default value as 1
113118
}
@@ -139,6 +144,19 @@ class io_context_pool {
139144
svr->run();
140145
},
141146
io_contexts_[i]));
147+
148+
#ifdef __linux__
149+
if (cpu_affinity_) {
150+
cpu_set_t cpuset;
151+
CPU_ZERO(&cpuset);
152+
CPU_SET(i, &cpuset);
153+
int rc = pthread_setaffinity_np(threads.back()->native_handle(),
154+
sizeof(cpu_set_t), &cpuset);
155+
if (rc != 0) {
156+
std::cerr << "Error calling pthread_setaffinity_np: " << rc << "\n";
157+
}
158+
}
159+
#endif
142160
}
143161

144162
for (std::size_t i = 0; i < threads.size(); ++i) {
@@ -197,6 +215,7 @@ class io_context_pool {
197215
std::promise<void> promise_;
198216
std::atomic<bool> has_run_or_stop_ = false;
199217
std::once_flag flag_;
218+
bool cpu_affinity_ = false;
200219
};
201220

202221
class multithread_context_pool {

0 commit comments

Comments
 (0)