Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow set cpu affinity #531

Merged
merged 1 commit into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example/benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ using namespace cinatra;
using namespace std::chrono_literals;

int main() {
coro_http_server server(std::thread::hardware_concurrency(), 8090);
coro_http_server server(std::thread::hardware_concurrency(), 8090, true);
server.set_http_handler<GET>(
"/plaintext", [](coro_http_request& req, coro_http_response& resp) {
resp.get_conn()->set_multi_buf(false);
Expand Down
6 changes: 4 additions & 2 deletions include/cinatra/coro_http_server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ class coro_http_server {
coro_http_server(asio::io_context &ctx, unsigned short port)
: out_ctx_(&ctx), port_(port), acceptor_(ctx), check_timer_(ctx) {}

coro_http_server(size_t thread_num, unsigned short port)
: pool_(std::make_unique<coro_io::io_context_pool>(thread_num)),
coro_http_server(size_t thread_num, unsigned short port,
bool cpu_affinity = false)
: pool_(std::make_unique<coro_io::io_context_pool>(thread_num,
cpu_affinity)),
port_(port),
acceptor_(pool_->get_executor()->get_asio_executor()),
check_timer_(pool_->get_executor()->get_asio_executor()) {}
Expand Down
21 changes: 20 additions & 1 deletion include/cinatra/ylt/coro_io/io_context_pool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
#include <thread>
#include <type_traits>
#include <vector>
#ifdef __linux__
#include <pthread.h>
#include <sched.h>
#endif

namespace coro_io {

Expand Down Expand Up @@ -107,7 +111,8 @@ get_current_executor() {
class io_context_pool {
public:
using executor_type = asio::io_context::executor_type;
explicit io_context_pool(std::size_t pool_size) : next_io_context_(0) {
explicit io_context_pool(std::size_t pool_size, bool cpu_affinity = false)
: next_io_context_(0), cpu_affinity_(cpu_affinity) {
if (pool_size == 0) {
pool_size = 1; // set default value as 1
}
Expand Down Expand Up @@ -139,6 +144,19 @@ class io_context_pool {
svr->run();
},
io_contexts_[i]));

#ifdef __linux__
if (cpu_affinity_) {
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(i, &cpuset);
int rc = pthread_setaffinity_np(threads.back()->native_handle(),
sizeof(cpu_set_t), &cpuset);
if (rc != 0) {
std::cerr << "Error calling pthread_setaffinity_np: " << rc << "\n";
}
}
#endif
}

for (std::size_t i = 0; i < threads.size(); ++i) {
Expand Down Expand Up @@ -197,6 +215,7 @@ class io_context_pool {
std::promise<void> promise_;
std::atomic<bool> has_run_or_stop_ = false;
std::once_flag flag_;
bool cpu_affinity_ = false;
};

class multithread_context_pool {
Expand Down
Loading