Skip to content

Commit

Permalink
posix: add posix_thread attribute for thread affinity
Browse files Browse the repository at this point in the history
First use will be to bind the prefault thread to its NUMA node.
  • Loading branch information
avikivity committed Jul 6, 2023
1 parent c2baf51 commit 920a748
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
3 changes: 3 additions & 0 deletions include/seastar/core/posix.hh
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include <functional>
#include <memory>
#include <set>
#include <optional>
#endif
#include "abort_on_ebadf.hh"
#include <seastar/core/sstring.hh>
Expand Down Expand Up @@ -442,8 +443,10 @@ public:
set(std::forward<Rest>(rest)...);
}
void set(stack_size ss) { _stack_size = ss; }
void set(cpu_set_t affinity) { _affinity = affinity; }
private:
stack_size _stack_size;
std::optional<cpu_set_t> _affinity;
friend class posix_thread;
};
};
Expand Down
5 changes: 5 additions & 0 deletions src/core/posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ posix_thread::posix_thread(attr a, std::function<void ()> func)
}
#endif

if (a._affinity) {
auto& cpuset = *a._affinity;
pthread_attr_setaffinity_np(&pa, sizeof(cpuset), &cpuset);
}

r = pthread_create(&_pthread, &pa,
&posix_thread::start_routine, _func.get());
if (r) {
Expand Down

0 comments on commit 920a748

Please sign in to comment.