Skip to content

Commit

Permalink
memory: return NUMA layout of allocated memory during initialization
Browse files Browse the repository at this point in the history
This will be used by the memory prefaulter to guide the creation
of prefault threads, one per NUMA node.

A small function to merge layouts from different shards is added
in anticipation of its use.
  • Loading branch information
avikivity committed Jul 6, 2023
1 parent eee093a commit c2baf51
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
19 changes: 18 additions & 1 deletion include/seastar/core/memory.hh
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,24 @@ static constexpr size_t huge_page_size =
#error "Huge page size is not defined for this architecture"
#endif

void configure(std::vector<resource::memory> m, bool mbind,

namespace internal {

struct memory_range {
char* start;
char* end;
unsigned numa_node_id;
};

struct numa_layout {
std::vector<memory_range> ranges;
};

numa_layout merge(numa_layout one, numa_layout two);

}

internal::numa_layout configure(std::vector<resource::memory> m, bool mbind,
std::optional<std::string> hugetlbfs_path = {});

// A deprecated alias for set_abort_on_allocation_failure(true).
Expand Down
20 changes: 17 additions & 3 deletions src/core/memory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,13 @@ __thread volatile int critical_alloc_section = 0;

#endif // SEASTAR_ENABLE_ALLOC_FAILURE_INJECTION

numa_layout
merge(numa_layout one, numa_layout two) {
// There's no chance to merge, so just concatenate
one.ranges.insert(one.ranges.end(), two.ranges.begin(), two.ranges.end());
return one;
}

} // namespace internal

}
Expand Down Expand Up @@ -1535,7 +1542,8 @@ void disable_large_allocation_warning() {
get_cpu_mem().large_allocation_warning_threshold = std::numeric_limits<size_t>::max();
}

void configure(std::vector<resource::memory> m, bool mbind,
internal::numa_layout
configure(std::vector<resource::memory> m, bool mbind,
optional<std::string> hugetlbfs_path) {
// we need to make sure cpu_mem is initialize since configure calls cpu_mem.resize
// and we might reach configure without ever allocating, hence without ever calling
Expand All @@ -1545,6 +1553,7 @@ void configure(std::vector<resource::memory> m, bool mbind,
// verify that here.
init_cpu_mem();
is_reactor_thread = true;
internal::numa_layout ret_layout;
size_t total = 0;
for (auto&& x : m) {
total += x.bytes;
Expand All @@ -1565,7 +1574,8 @@ void configure(std::vector<resource::memory> m, bool mbind,
#ifdef SEASTAR_HAVE_NUMA
unsigned long nodemask = 1UL << x.nodeid;
if (mbind) {
auto r = ::mbind(get_cpu_mem().mem() + pos, x.bytes,
auto start = get_cpu_mem().mem() + pos;
auto r = ::mbind(start, x.bytes,
MPOL_PREFERRED,
&nodemask, std::numeric_limits<unsigned long>::digits,
MPOL_MF_MOVE);
Expand All @@ -1576,10 +1586,12 @@ void configure(std::vector<resource::memory> m, bool mbind,
std::cerr << "WARNING: unable to mbind shard memory; performance may suffer: "
<< msg << std::endl;
}
ret_layout.ranges.push_back({.start = start, .end = start + x.bytes, .numa_node_id = x.nodeid});
}
#endif
pos += x.bytes;
}
return ret_layout;
}

statistics stats() {
Expand Down Expand Up @@ -2339,7 +2351,9 @@ reclaimer::~reclaimer() {
void set_reclaim_hook(std::function<void (std::function<void ()>)> hook) {
}

void configure(std::vector<resource::memory> m, bool mbind, std::optional<std::string> hugepages_path) {
internal::numa_layout
configure(std::vector<resource::memory> m, bool mbind, std::optional<std::string> hugepages_path) {
return {};
}

statistics stats() {
Expand Down

0 comments on commit c2baf51

Please sign in to comment.